2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / trans-expr.c
blob2f42c04436ad1839729b0549515fc9c649ca3b7f
1 /* Expression translation
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
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 "gfortran.h"
28 #include "alias.h"
29 #include "tree.h"
30 #include "options.h"
31 #include "fold-const.h"
32 #include "stringpool.h"
33 #include "diagnostic-core.h" /* For fatal_error. */
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "arith.h"
37 #include "constructor.h"
38 #include "trans.h"
39 #include "trans-const.h"
40 #include "trans-types.h"
41 #include "trans-array.h"
42 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
43 #include "trans-stmt.h"
44 #include "dependency.h"
45 #include "gimplify.h"
47 /* Convert a scalar to an array descriptor. To be used for assumed-rank
48 arrays. */
50 static tree
51 get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
53 enum gfc_array_kind akind;
55 if (attr.pointer)
56 akind = GFC_ARRAY_POINTER_CONT;
57 else if (attr.allocatable)
58 akind = GFC_ARRAY_ALLOCATABLE;
59 else
60 akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
62 if (POINTER_TYPE_P (TREE_TYPE (scalar)))
63 scalar = TREE_TYPE (scalar);
64 return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, 0, NULL, NULL, 1,
65 akind, !(attr.pointer || attr.target));
68 tree
69 gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
71 tree desc, type;
73 type = get_scalar_to_descriptor_type (scalar, attr);
74 desc = gfc_create_var (type, "desc");
75 DECL_ARTIFICIAL (desc) = 1;
77 if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
78 scalar = gfc_build_addr_expr (NULL_TREE, scalar);
79 gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
80 gfc_get_dtype (type));
81 gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
83 /* Copy pointer address back - but only if it could have changed and
84 if the actual argument is a pointer and not, e.g., NULL(). */
85 if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
86 gfc_add_modify (&se->post, scalar,
87 fold_convert (TREE_TYPE (scalar),
88 gfc_conv_descriptor_data_get (desc)));
89 return desc;
93 /* This is the seed for an eventual trans-class.c
95 The following parameters should not be used directly since they might
96 in future implementations. Use the corresponding APIs. */
97 #define CLASS_DATA_FIELD 0
98 #define CLASS_VPTR_FIELD 1
99 #define CLASS_LEN_FIELD 2
100 #define VTABLE_HASH_FIELD 0
101 #define VTABLE_SIZE_FIELD 1
102 #define VTABLE_EXTENDS_FIELD 2
103 #define VTABLE_DEF_INIT_FIELD 3
104 #define VTABLE_COPY_FIELD 4
105 #define VTABLE_FINAL_FIELD 5
108 tree
109 gfc_class_set_static_fields (tree decl, tree vptr, tree data)
111 tree tmp;
112 tree field;
113 vec<constructor_elt, va_gc> *init = NULL;
115 field = TYPE_FIELDS (TREE_TYPE (decl));
116 tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
117 CONSTRUCTOR_APPEND_ELT (init, tmp, data);
119 tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
120 CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
122 return build_constructor (TREE_TYPE (decl), init);
126 tree
127 gfc_class_data_get (tree decl)
129 tree data;
130 if (POINTER_TYPE_P (TREE_TYPE (decl)))
131 decl = build_fold_indirect_ref_loc (input_location, decl);
132 data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
133 CLASS_DATA_FIELD);
134 return fold_build3_loc (input_location, COMPONENT_REF,
135 TREE_TYPE (data), decl, data,
136 NULL_TREE);
140 tree
141 gfc_class_vptr_get (tree decl)
143 tree vptr;
144 /* For class arrays decl may be a temporary descriptor handle, the vptr is
145 then available through the saved descriptor. */
146 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl)
147 && GFC_DECL_SAVED_DESCRIPTOR (decl))
148 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
149 if (POINTER_TYPE_P (TREE_TYPE (decl)))
150 decl = build_fold_indirect_ref_loc (input_location, decl);
151 vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
152 CLASS_VPTR_FIELD);
153 return fold_build3_loc (input_location, COMPONENT_REF,
154 TREE_TYPE (vptr), decl, vptr,
155 NULL_TREE);
159 tree
160 gfc_class_len_get (tree decl)
162 tree len;
163 /* For class arrays decl may be a temporary descriptor handle, the len is
164 then available through the saved descriptor. */
165 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl)
166 && GFC_DECL_SAVED_DESCRIPTOR (decl))
167 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
168 if (POINTER_TYPE_P (TREE_TYPE (decl)))
169 decl = build_fold_indirect_ref_loc (input_location, decl);
170 len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
171 CLASS_LEN_FIELD);
172 return fold_build3_loc (input_location, COMPONENT_REF,
173 TREE_TYPE (len), decl, len,
174 NULL_TREE);
178 /* Get the specified FIELD from the VPTR. */
180 static tree
181 vptr_field_get (tree vptr, int fieldno)
183 tree field;
184 vptr = build_fold_indirect_ref_loc (input_location, vptr);
185 field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
186 fieldno);
187 field = fold_build3_loc (input_location, COMPONENT_REF,
188 TREE_TYPE (field), vptr, field,
189 NULL_TREE);
190 gcc_assert (field);
191 return field;
195 /* Get the field from the class' vptr. */
197 static tree
198 class_vtab_field_get (tree decl, int fieldno)
200 tree vptr;
201 vptr = gfc_class_vptr_get (decl);
202 return vptr_field_get (vptr, fieldno);
206 /* Define a macro for creating the class_vtab_* and vptr_* accessors in
207 unison. */
208 #define VTAB_GET_FIELD_GEN(name, field) tree \
209 gfc_class_vtab_## name ##_get (tree cl) \
211 return class_vtab_field_get (cl, field); \
214 tree \
215 gfc_vptr_## name ##_get (tree vptr) \
217 return vptr_field_get (vptr, field); \
220 VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
221 VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
222 VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
223 VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
224 VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
227 /* The size field is returned as an array index type. Therefore treat
228 it and only it specially. */
230 tree
231 gfc_class_vtab_size_get (tree cl)
233 tree size;
234 size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
235 /* Always return size as an array index type. */
236 size = fold_convert (gfc_array_index_type, size);
237 gcc_assert (size);
238 return size;
241 tree
242 gfc_vptr_size_get (tree vptr)
244 tree size;
245 size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
246 /* Always return size as an array index type. */
247 size = fold_convert (gfc_array_index_type, size);
248 gcc_assert (size);
249 return size;
253 #undef CLASS_DATA_FIELD
254 #undef CLASS_VPTR_FIELD
255 #undef VTABLE_HASH_FIELD
256 #undef VTABLE_SIZE_FIELD
257 #undef VTABLE_EXTENDS_FIELD
258 #undef VTABLE_DEF_INIT_FIELD
259 #undef VTABLE_COPY_FIELD
260 #undef VTABLE_FINAL_FIELD
263 /* Search for the last _class ref in the chain of references of this
264 expression and cut the chain there. Albeit this routine is similiar
265 to class.c::gfc_add_component_ref (), is there a significant
266 difference: gfc_add_component_ref () concentrates on an array ref to
267 be the last ref in the chain. This routine is oblivious to the kind
268 of refs following. */
270 gfc_expr *
271 gfc_find_and_cut_at_last_class_ref (gfc_expr *e)
273 gfc_expr *base_expr;
274 gfc_ref *ref, *class_ref, *tail;
276 /* Find the last class reference. */
277 class_ref = NULL;
278 for (ref = e->ref; ref; ref = ref->next)
280 if (ref->type == REF_COMPONENT
281 && ref->u.c.component->ts.type == BT_CLASS)
282 class_ref = ref;
284 if (ref->next == NULL)
285 break;
288 /* Remove and store all subsequent references after the
289 CLASS reference. */
290 if (class_ref)
292 tail = class_ref->next;
293 class_ref->next = NULL;
295 else
297 tail = e->ref;
298 e->ref = NULL;
301 base_expr = gfc_expr_to_initialize (e);
303 /* Restore the original tail expression. */
304 if (class_ref)
306 gfc_free_ref_list (class_ref->next);
307 class_ref->next = tail;
309 else
311 gfc_free_ref_list (e->ref);
312 e->ref = tail;
314 return base_expr;
318 /* Reset the vptr to the declared type, e.g. after deallocation. */
320 void
321 gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
323 gfc_expr *rhs, *lhs = gfc_copy_expr (e);
324 gfc_symbol *vtab;
325 tree tmp;
326 gfc_ref *ref;
328 /* If we have a class array, we need go back to the class
329 container. */
330 if (lhs->ref && lhs->ref->next && !lhs->ref->next->next
331 && lhs->ref->next->type == REF_ARRAY
332 && lhs->ref->next->u.ar.type == AR_FULL
333 && lhs->ref->type == REF_COMPONENT
334 && strcmp (lhs->ref->u.c.component->name, "_data") == 0)
336 gfc_free_ref_list (lhs->ref);
337 lhs->ref = NULL;
339 else
340 for (ref = lhs->ref; ref; ref = ref->next)
341 if (ref->next && ref->next->next && !ref->next->next->next
342 && ref->next->next->type == REF_ARRAY
343 && ref->next->next->u.ar.type == AR_FULL
344 && ref->next->type == REF_COMPONENT
345 && strcmp (ref->next->u.c.component->name, "_data") == 0)
347 gfc_free_ref_list (ref->next);
348 ref->next = NULL;
351 gfc_add_vptr_component (lhs);
353 if (UNLIMITED_POLY (e))
354 rhs = gfc_get_null_expr (NULL);
355 else
357 vtab = gfc_find_derived_vtab (e->ts.u.derived);
358 rhs = gfc_lval_expr_from_sym (vtab);
360 tmp = gfc_trans_pointer_assignment (lhs, rhs);
361 gfc_add_expr_to_block (block, tmp);
362 gfc_free_expr (lhs);
363 gfc_free_expr (rhs);
367 /* Reset the len for unlimited polymorphic objects. */
369 void
370 gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
372 gfc_expr *e;
373 gfc_se se_len;
374 e = gfc_find_and_cut_at_last_class_ref (expr);
375 gfc_add_len_component (e);
376 gfc_init_se (&se_len, NULL);
377 gfc_conv_expr (&se_len, e);
378 gfc_add_modify (block, se_len.expr,
379 fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
380 gfc_free_expr (e);
384 /* Obtain the vptr of the last class reference in an expression.
385 Return NULL_TREE if no class reference is found. */
387 tree
388 gfc_get_vptr_from_expr (tree expr)
390 tree tmp;
391 tree type;
393 for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
395 type = TREE_TYPE (tmp);
396 while (type)
398 if (GFC_CLASS_TYPE_P (type))
399 return gfc_class_vptr_get (tmp);
400 if (type != TYPE_CANONICAL (type))
401 type = TYPE_CANONICAL (type);
402 else
403 type = NULL_TREE;
405 if (TREE_CODE (tmp) == VAR_DECL)
406 break;
408 return NULL_TREE;
412 static void
413 class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
414 bool lhs_type)
416 tree tmp, tmp2, type;
418 gfc_conv_descriptor_data_set (block, lhs_desc,
419 gfc_conv_descriptor_data_get (rhs_desc));
420 gfc_conv_descriptor_offset_set (block, lhs_desc,
421 gfc_conv_descriptor_offset_get (rhs_desc));
423 gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
424 gfc_conv_descriptor_dtype (rhs_desc));
426 /* Assign the dimension as range-ref. */
427 tmp = gfc_get_descriptor_dimension (lhs_desc);
428 tmp2 = gfc_get_descriptor_dimension (rhs_desc);
430 type = lhs_type ? TREE_TYPE (tmp) : TREE_TYPE (tmp2);
431 tmp = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp,
432 gfc_index_zero_node, NULL_TREE, NULL_TREE);
433 tmp2 = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp2,
434 gfc_index_zero_node, NULL_TREE, NULL_TREE);
435 gfc_add_modify (block, tmp, tmp2);
439 /* Takes a derived type expression and returns the address of a temporary
440 class object of the 'declared' type. If vptr is not NULL, this is
441 used for the temporary class object.
442 optional_alloc_ptr is false when the dummy is neither allocatable
443 nor a pointer; that's only relevant for the optional handling. */
444 void
445 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
446 gfc_typespec class_ts, tree vptr, bool optional,
447 bool optional_alloc_ptr)
449 gfc_symbol *vtab;
450 tree cond_optional = NULL_TREE;
451 gfc_ss *ss;
452 tree ctree;
453 tree var;
454 tree tmp;
456 /* The derived type needs to be converted to a temporary
457 CLASS object. */
458 tmp = gfc_typenode_for_spec (&class_ts);
459 var = gfc_create_var (tmp, "class");
461 /* Set the vptr. */
462 ctree = gfc_class_vptr_get (var);
464 if (vptr != NULL_TREE)
466 /* Use the dynamic vptr. */
467 tmp = vptr;
469 else
471 /* In this case the vtab corresponds to the derived type and the
472 vptr must point to it. */
473 vtab = gfc_find_derived_vtab (e->ts.u.derived);
474 gcc_assert (vtab);
475 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
477 gfc_add_modify (&parmse->pre, ctree,
478 fold_convert (TREE_TYPE (ctree), tmp));
480 /* Now set the data field. */
481 ctree = gfc_class_data_get (var);
483 if (optional)
484 cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
486 if (parmse->ss && parmse->ss->info->useflags)
488 /* For an array reference in an elemental procedure call we need
489 to retain the ss to provide the scalarized array reference. */
490 gfc_conv_expr_reference (parmse, e);
491 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
492 if (optional)
493 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
494 cond_optional, tmp,
495 fold_convert (TREE_TYPE (tmp), null_pointer_node));
496 gfc_add_modify (&parmse->pre, ctree, tmp);
499 else
501 ss = gfc_walk_expr (e);
502 if (ss == gfc_ss_terminator)
504 parmse->ss = NULL;
505 gfc_conv_expr_reference (parmse, e);
507 /* Scalar to an assumed-rank array. */
508 if (class_ts.u.derived->components->as)
510 tree type;
511 type = get_scalar_to_descriptor_type (parmse->expr,
512 gfc_expr_attr (e));
513 gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
514 gfc_get_dtype (type));
515 if (optional)
516 parmse->expr = build3_loc (input_location, COND_EXPR,
517 TREE_TYPE (parmse->expr),
518 cond_optional, parmse->expr,
519 fold_convert (TREE_TYPE (parmse->expr),
520 null_pointer_node));
521 gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
523 else
525 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
526 if (optional)
527 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
528 cond_optional, tmp,
529 fold_convert (TREE_TYPE (tmp),
530 null_pointer_node));
531 gfc_add_modify (&parmse->pre, ctree, tmp);
534 else
536 stmtblock_t block;
537 gfc_init_block (&block);
539 parmse->ss = ss;
540 gfc_conv_expr_descriptor (parmse, e);
542 if (e->rank != class_ts.u.derived->components->as->rank)
544 gcc_assert (class_ts.u.derived->components->as->type
545 == AS_ASSUMED_RANK);
546 class_array_data_assign (&block, ctree, parmse->expr, false);
548 else
550 if (gfc_expr_attr (e).codimension)
551 parmse->expr = fold_build1_loc (input_location,
552 VIEW_CONVERT_EXPR,
553 TREE_TYPE (ctree),
554 parmse->expr);
555 gfc_add_modify (&block, ctree, parmse->expr);
558 if (optional)
560 tmp = gfc_finish_block (&block);
562 gfc_init_block (&block);
563 gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
565 tmp = build3_v (COND_EXPR, cond_optional, tmp,
566 gfc_finish_block (&block));
567 gfc_add_expr_to_block (&parmse->pre, tmp);
569 else
570 gfc_add_block_to_block (&parmse->pre, &block);
574 if (class_ts.u.derived->components->ts.type == BT_DERIVED
575 && class_ts.u.derived->components->ts.u.derived
576 ->attr.unlimited_polymorphic)
578 /* Take care about initializing the _len component correctly. */
579 ctree = gfc_class_len_get (var);
580 if (UNLIMITED_POLY (e))
582 gfc_expr *len;
583 gfc_se se;
585 len = gfc_copy_expr (e);
586 gfc_add_len_component (len);
587 gfc_init_se (&se, NULL);
588 gfc_conv_expr (&se, len);
589 if (optional)
590 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
591 cond_optional, se.expr,
592 fold_convert (TREE_TYPE (se.expr),
593 integer_zero_node));
594 else
595 tmp = se.expr;
597 else
598 tmp = integer_zero_node;
599 gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree),
600 tmp));
602 /* Pass the address of the class object. */
603 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
605 if (optional && optional_alloc_ptr)
606 parmse->expr = build3_loc (input_location, COND_EXPR,
607 TREE_TYPE (parmse->expr),
608 cond_optional, parmse->expr,
609 fold_convert (TREE_TYPE (parmse->expr),
610 null_pointer_node));
614 /* Create a new class container, which is required as scalar coarrays
615 have an array descriptor while normal scalars haven't. Optionally,
616 NULL pointer checks are added if the argument is OPTIONAL. */
618 static void
619 class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
620 gfc_typespec class_ts, bool optional)
622 tree var, ctree, tmp;
623 stmtblock_t block;
624 gfc_ref *ref;
625 gfc_ref *class_ref;
627 gfc_init_block (&block);
629 class_ref = NULL;
630 for (ref = e->ref; ref; ref = ref->next)
632 if (ref->type == REF_COMPONENT
633 && ref->u.c.component->ts.type == BT_CLASS)
634 class_ref = ref;
637 if (class_ref == NULL
638 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
639 tmp = e->symtree->n.sym->backend_decl;
640 else
642 /* Remove everything after the last class reference, convert the
643 expression and then recover its tailend once more. */
644 gfc_se tmpse;
645 ref = class_ref->next;
646 class_ref->next = NULL;
647 gfc_init_se (&tmpse, NULL);
648 gfc_conv_expr (&tmpse, e);
649 class_ref->next = ref;
650 tmp = tmpse.expr;
653 var = gfc_typenode_for_spec (&class_ts);
654 var = gfc_create_var (var, "class");
656 ctree = gfc_class_vptr_get (var);
657 gfc_add_modify (&block, ctree,
658 fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
660 ctree = gfc_class_data_get (var);
661 tmp = gfc_conv_descriptor_data_get (gfc_class_data_get (tmp));
662 gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
664 /* Pass the address of the class object. */
665 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
667 if (optional)
669 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
670 tree tmp2;
672 tmp = gfc_finish_block (&block);
674 gfc_init_block (&block);
675 tmp2 = gfc_class_data_get (var);
676 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
677 null_pointer_node));
678 tmp2 = gfc_finish_block (&block);
680 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
681 cond, tmp, tmp2);
682 gfc_add_expr_to_block (&parmse->pre, tmp);
684 else
685 gfc_add_block_to_block (&parmse->pre, &block);
689 /* Takes an intrinsic type expression and returns the address of a temporary
690 class object of the 'declared' type. */
691 void
692 gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
693 gfc_typespec class_ts)
695 gfc_symbol *vtab;
696 gfc_ss *ss;
697 tree ctree;
698 tree var;
699 tree tmp;
701 /* The intrinsic type needs to be converted to a temporary
702 CLASS object. */
703 tmp = gfc_typenode_for_spec (&class_ts);
704 var = gfc_create_var (tmp, "class");
706 /* Set the vptr. */
707 ctree = gfc_class_vptr_get (var);
709 vtab = gfc_find_vtab (&e->ts);
710 gcc_assert (vtab);
711 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
712 gfc_add_modify (&parmse->pre, ctree,
713 fold_convert (TREE_TYPE (ctree), tmp));
715 /* Now set the data field. */
716 ctree = gfc_class_data_get (var);
717 if (parmse->ss && parmse->ss->info->useflags)
719 /* For an array reference in an elemental procedure call we need
720 to retain the ss to provide the scalarized array reference. */
721 gfc_conv_expr_reference (parmse, e);
722 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
723 gfc_add_modify (&parmse->pre, ctree, tmp);
725 else
727 ss = gfc_walk_expr (e);
728 if (ss == gfc_ss_terminator)
730 parmse->ss = NULL;
731 gfc_conv_expr_reference (parmse, e);
732 if (class_ts.u.derived->components->as
733 && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
735 tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
736 gfc_expr_attr (e));
737 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
738 TREE_TYPE (ctree), tmp);
740 else
741 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
742 gfc_add_modify (&parmse->pre, ctree, tmp);
744 else
746 parmse->ss = ss;
747 parmse->use_offset = 1;
748 gfc_conv_expr_descriptor (parmse, e);
749 if (class_ts.u.derived->components->as->rank != e->rank)
751 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
752 TREE_TYPE (ctree), parmse->expr);
753 gfc_add_modify (&parmse->pre, ctree, tmp);
755 else
756 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
760 gcc_assert (class_ts.type == BT_CLASS);
761 if (class_ts.u.derived->components->ts.type == BT_DERIVED
762 && class_ts.u.derived->components->ts.u.derived
763 ->attr.unlimited_polymorphic)
765 ctree = gfc_class_len_get (var);
766 /* When the actual arg is a char array, then set the _len component of the
767 unlimited polymorphic entity, too. */
768 if (e->ts.type == BT_CHARACTER)
770 /* Start with parmse->string_length because this seems to be set to a
771 correct value more often. */
772 if (parmse->string_length)
773 tmp = parmse->string_length;
774 /* When the string_length is not yet set, then try the backend_decl of
775 the cl. */
776 else if (e->ts.u.cl->backend_decl)
777 tmp = e->ts.u.cl->backend_decl;
778 /* If both of the above approaches fail, then try to generate an
779 expression from the input, which is only feasible currently, when the
780 expression can be evaluated to a constant one. */
781 else
783 /* Try to simplify the expression. */
784 gfc_simplify_expr (e, 0);
785 if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
787 /* Amazingly all data is present to compute the length of a
788 constant string, but the expression is not yet there. */
789 e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER, 4,
790 &e->where);
791 mpz_set_ui (e->ts.u.cl->length->value.integer,
792 e->value.character.length);
793 gfc_conv_const_charlen (e->ts.u.cl);
794 e->ts.u.cl->resolved = 1;
795 tmp = e->ts.u.cl->backend_decl;
797 else
799 gfc_error ("Can't compute the length of the char array at %L.",
800 &e->where);
804 else
805 tmp = integer_zero_node;
807 gfc_add_modify (&parmse->pre, ctree, tmp);
809 else if (class_ts.type == BT_CLASS
810 && class_ts.u.derived->components
811 && class_ts.u.derived->components->ts.u
812 .derived->attr.unlimited_polymorphic)
814 ctree = gfc_class_len_get (var);
815 gfc_add_modify (&parmse->pre, ctree,
816 fold_convert (TREE_TYPE (ctree),
817 integer_zero_node));
819 /* Pass the address of the class object. */
820 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
824 /* Takes a scalarized class array expression and returns the
825 address of a temporary scalar class object of the 'declared'
826 type.
827 OOP-TODO: This could be improved by adding code that branched on
828 the dynamic type being the same as the declared type. In this case
829 the original class expression can be passed directly.
830 optional_alloc_ptr is false when the dummy is neither allocatable
831 nor a pointer; that's relevant for the optional handling.
832 Set copyback to true if class container's _data and _vtab pointers
833 might get modified. */
835 void
836 gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
837 bool elemental, bool copyback, bool optional,
838 bool optional_alloc_ptr)
840 tree ctree;
841 tree var;
842 tree tmp;
843 tree vptr;
844 tree cond = NULL_TREE;
845 tree slen = NULL_TREE;
846 gfc_ref *ref;
847 gfc_ref *class_ref;
848 stmtblock_t block;
849 bool full_array = false;
851 gfc_init_block (&block);
853 class_ref = NULL;
854 for (ref = e->ref; ref; ref = ref->next)
856 if (ref->type == REF_COMPONENT
857 && ref->u.c.component->ts.type == BT_CLASS)
858 class_ref = ref;
860 if (ref->next == NULL)
861 break;
864 if ((ref == NULL || class_ref == ref)
865 && (!class_ts.u.derived->components->as
866 || class_ts.u.derived->components->as->rank != -1))
867 return;
869 /* Test for FULL_ARRAY. */
870 if (e->rank == 0 && gfc_expr_attr (e).codimension
871 && gfc_expr_attr (e).dimension)
872 full_array = true;
873 else
874 gfc_is_class_array_ref (e, &full_array);
876 /* The derived type needs to be converted to a temporary
877 CLASS object. */
878 tmp = gfc_typenode_for_spec (&class_ts);
879 var = gfc_create_var (tmp, "class");
881 /* Set the data. */
882 ctree = gfc_class_data_get (var);
883 if (class_ts.u.derived->components->as
884 && e->rank != class_ts.u.derived->components->as->rank)
886 if (e->rank == 0)
888 tree type = get_scalar_to_descriptor_type (parmse->expr,
889 gfc_expr_attr (e));
890 gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
891 gfc_get_dtype (type));
893 tmp = gfc_class_data_get (parmse->expr);
894 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
895 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
897 gfc_conv_descriptor_data_set (&block, ctree, tmp);
899 else
900 class_array_data_assign (&block, ctree, parmse->expr, false);
902 else
904 if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
905 parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
906 TREE_TYPE (ctree), parmse->expr);
907 gfc_add_modify (&block, ctree, parmse->expr);
910 /* Return the data component, except in the case of scalarized array
911 references, where nullification of the cannot occur and so there
912 is no need. */
913 if (!elemental && full_array && copyback)
915 if (class_ts.u.derived->components->as
916 && e->rank != class_ts.u.derived->components->as->rank)
918 if (e->rank == 0)
919 gfc_add_modify (&parmse->post, gfc_class_data_get (parmse->expr),
920 gfc_conv_descriptor_data_get (ctree));
921 else
922 class_array_data_assign (&parmse->post, parmse->expr, ctree, true);
924 else
925 gfc_add_modify (&parmse->post, parmse->expr, ctree);
928 /* Set the vptr. */
929 ctree = gfc_class_vptr_get (var);
931 /* The vptr is the second field of the actual argument.
932 First we have to find the corresponding class reference. */
934 tmp = NULL_TREE;
935 if (class_ref == NULL
936 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
938 tmp = e->symtree->n.sym->backend_decl;
939 if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
940 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
941 slen = integer_zero_node;
943 else
945 /* Remove everything after the last class reference, convert the
946 expression and then recover its tailend once more. */
947 gfc_se tmpse;
948 ref = class_ref->next;
949 class_ref->next = NULL;
950 gfc_init_se (&tmpse, NULL);
951 gfc_conv_expr (&tmpse, e);
952 class_ref->next = ref;
953 tmp = tmpse.expr;
954 slen = tmpse.string_length;
957 gcc_assert (tmp != NULL_TREE);
959 /* Dereference if needs be. */
960 if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
961 tmp = build_fold_indirect_ref_loc (input_location, tmp);
963 vptr = gfc_class_vptr_get (tmp);
964 gfc_add_modify (&block, ctree,
965 fold_convert (TREE_TYPE (ctree), vptr));
967 /* Return the vptr component, except in the case of scalarized array
968 references, where the dynamic type cannot change. */
969 if (!elemental && full_array && copyback)
970 gfc_add_modify (&parmse->post, vptr,
971 fold_convert (TREE_TYPE (vptr), ctree));
973 /* For unlimited polymorphic objects also set the _len component. */
974 if (class_ts.type == BT_CLASS
975 && class_ts.u.derived->components
976 && class_ts.u.derived->components->ts.u
977 .derived->attr.unlimited_polymorphic)
979 ctree = gfc_class_len_get (var);
980 if (UNLIMITED_POLY (e))
981 tmp = gfc_class_len_get (tmp);
982 else if (e->ts.type == BT_CHARACTER)
984 gcc_assert (slen != NULL_TREE);
985 tmp = slen;
987 else
988 tmp = integer_zero_node;
989 gfc_add_modify (&parmse->pre, ctree,
990 fold_convert (TREE_TYPE (ctree), tmp));
993 if (optional)
995 tree tmp2;
997 cond = gfc_conv_expr_present (e->symtree->n.sym);
998 /* parmse->pre may contain some preparatory instructions for the
999 temporary array descriptor. Those may only be executed when the
1000 optional argument is set, therefore add parmse->pre's instructions
1001 to block, which is later guarded by an if (optional_arg_given). */
1002 gfc_add_block_to_block (&parmse->pre, &block);
1003 block.head = parmse->pre.head;
1004 parmse->pre.head = NULL_TREE;
1005 tmp = gfc_finish_block (&block);
1007 if (optional_alloc_ptr)
1008 tmp2 = build_empty_stmt (input_location);
1009 else
1011 gfc_init_block (&block);
1013 tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
1014 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
1015 null_pointer_node));
1016 tmp2 = gfc_finish_block (&block);
1019 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1020 cond, tmp, tmp2);
1021 gfc_add_expr_to_block (&parmse->pre, tmp);
1023 else
1024 gfc_add_block_to_block (&parmse->pre, &block);
1026 /* Pass the address of the class object. */
1027 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1029 if (optional && optional_alloc_ptr)
1030 parmse->expr = build3_loc (input_location, COND_EXPR,
1031 TREE_TYPE (parmse->expr),
1032 cond, parmse->expr,
1033 fold_convert (TREE_TYPE (parmse->expr),
1034 null_pointer_node));
1038 /* Given a class array declaration and an index, returns the address
1039 of the referenced element. */
1041 tree
1042 gfc_get_class_array_ref (tree index, tree class_decl)
1044 tree data = gfc_class_data_get (class_decl);
1045 tree size = gfc_class_vtab_size_get (class_decl);
1046 tree offset = fold_build2_loc (input_location, MULT_EXPR,
1047 gfc_array_index_type,
1048 index, size);
1049 tree ptr;
1050 data = gfc_conv_descriptor_data_get (data);
1051 ptr = fold_convert (pvoid_type_node, data);
1052 ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
1053 return fold_convert (TREE_TYPE (data), ptr);
1057 /* Copies one class expression to another, assuming that if either
1058 'to' or 'from' are arrays they are packed. Should 'from' be
1059 NULL_TREE, the initialization expression for 'to' is used, assuming
1060 that the _vptr is set. */
1062 tree
1063 gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
1065 tree fcn;
1066 tree fcn_type;
1067 tree from_data;
1068 tree from_len;
1069 tree to_data;
1070 tree to_len;
1071 tree to_ref;
1072 tree from_ref;
1073 vec<tree, va_gc> *args;
1074 tree tmp;
1075 tree stdcopy;
1076 tree extcopy;
1077 tree index;
1079 args = NULL;
1080 /* To prevent warnings on uninitialized variables. */
1081 from_len = to_len = NULL_TREE;
1083 if (from != NULL_TREE)
1084 fcn = gfc_class_vtab_copy_get (from);
1085 else
1086 fcn = gfc_class_vtab_copy_get (to);
1088 fcn_type = TREE_TYPE (TREE_TYPE (fcn));
1090 if (from != NULL_TREE)
1091 from_data = gfc_class_data_get (from);
1092 else
1093 from_data = gfc_class_vtab_def_init_get (to);
1095 if (unlimited)
1097 if (from != NULL_TREE && unlimited)
1098 from_len = gfc_class_len_get (from);
1099 else
1100 from_len = integer_zero_node;
1103 to_data = gfc_class_data_get (to);
1104 if (unlimited)
1105 to_len = gfc_class_len_get (to);
1107 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
1109 stmtblock_t loopbody;
1110 stmtblock_t body;
1111 stmtblock_t ifbody;
1112 gfc_loopinfo loop;
1114 gfc_init_block (&body);
1115 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1116 gfc_array_index_type, nelems,
1117 gfc_index_one_node);
1118 nelems = gfc_evaluate_now (tmp, &body);
1119 index = gfc_create_var (gfc_array_index_type, "S");
1121 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data)))
1123 from_ref = gfc_get_class_array_ref (index, from);
1124 vec_safe_push (args, from_ref);
1126 else
1127 vec_safe_push (args, from_data);
1129 to_ref = gfc_get_class_array_ref (index, to);
1130 vec_safe_push (args, to_ref);
1132 tmp = build_call_vec (fcn_type, fcn, args);
1134 /* Build the body of the loop. */
1135 gfc_init_block (&loopbody);
1136 gfc_add_expr_to_block (&loopbody, tmp);
1138 /* Build the loop and return. */
1139 gfc_init_loopinfo (&loop);
1140 loop.dimen = 1;
1141 loop.from[0] = gfc_index_zero_node;
1142 loop.loopvar[0] = index;
1143 loop.to[0] = nelems;
1144 gfc_trans_scalarizing_loops (&loop, &loopbody);
1145 gfc_init_block (&ifbody);
1146 gfc_add_block_to_block (&ifbody, &loop.pre);
1147 stdcopy = gfc_finish_block (&ifbody);
1148 /* In initialization mode from_len is a constant zero. */
1149 if (unlimited && !integer_zerop (from_len))
1151 vec_safe_push (args, from_len);
1152 vec_safe_push (args, to_len);
1153 tmp = build_call_vec (fcn_type, fcn, args);
1154 /* Build the body of the loop. */
1155 gfc_init_block (&loopbody);
1156 gfc_add_expr_to_block (&loopbody, tmp);
1158 /* Build the loop and return. */
1159 gfc_init_loopinfo (&loop);
1160 loop.dimen = 1;
1161 loop.from[0] = gfc_index_zero_node;
1162 loop.loopvar[0] = index;
1163 loop.to[0] = nelems;
1164 gfc_trans_scalarizing_loops (&loop, &loopbody);
1165 gfc_init_block (&ifbody);
1166 gfc_add_block_to_block (&ifbody, &loop.pre);
1167 extcopy = gfc_finish_block (&ifbody);
1169 tmp = fold_build2_loc (input_location, GT_EXPR,
1170 boolean_type_node, from_len,
1171 integer_zero_node);
1172 tmp = fold_build3_loc (input_location, COND_EXPR,
1173 void_type_node, tmp, extcopy, stdcopy);
1174 gfc_add_expr_to_block (&body, tmp);
1175 tmp = gfc_finish_block (&body);
1177 else
1179 gfc_add_expr_to_block (&body, stdcopy);
1180 tmp = gfc_finish_block (&body);
1182 gfc_cleanup_loop (&loop);
1184 else
1186 gcc_assert (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data)));
1187 vec_safe_push (args, from_data);
1188 vec_safe_push (args, to_data);
1189 stdcopy = build_call_vec (fcn_type, fcn, args);
1191 /* In initialization mode from_len is a constant zero. */
1192 if (unlimited && !integer_zerop (from_len))
1194 vec_safe_push (args, from_len);
1195 vec_safe_push (args, to_len);
1196 extcopy = build_call_vec (fcn_type, fcn, args);
1197 tmp = fold_build2_loc (input_location, GT_EXPR,
1198 boolean_type_node, from_len,
1199 integer_zero_node);
1200 tmp = fold_build3_loc (input_location, COND_EXPR,
1201 void_type_node, tmp, extcopy, stdcopy);
1203 else
1204 tmp = stdcopy;
1207 /* Only copy _def_init to to_data, when it is not a NULL-pointer. */
1208 if (from == NULL_TREE)
1210 tree cond;
1211 cond = fold_build2_loc (input_location, NE_EXPR,
1212 boolean_type_node,
1213 from_data, null_pointer_node);
1214 tmp = fold_build3_loc (input_location, COND_EXPR,
1215 void_type_node, cond,
1216 tmp, build_empty_stmt (input_location));
1219 return tmp;
1223 static tree
1224 gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
1226 gfc_actual_arglist *actual;
1227 gfc_expr *ppc;
1228 gfc_code *ppc_code;
1229 tree res;
1231 actual = gfc_get_actual_arglist ();
1232 actual->expr = gfc_copy_expr (rhs);
1233 actual->next = gfc_get_actual_arglist ();
1234 actual->next->expr = gfc_copy_expr (lhs);
1235 ppc = gfc_copy_expr (obj);
1236 gfc_add_vptr_component (ppc);
1237 gfc_add_component_ref (ppc, "_copy");
1238 ppc_code = gfc_get_code (EXEC_CALL);
1239 ppc_code->resolved_sym = ppc->symtree->n.sym;
1240 /* Although '_copy' is set to be elemental in class.c, it is
1241 not staying that way. Find out why, sometime.... */
1242 ppc_code->resolved_sym->attr.elemental = 1;
1243 ppc_code->ext.actual = actual;
1244 ppc_code->expr1 = ppc;
1245 /* Since '_copy' is elemental, the scalarizer will take care
1246 of arrays in gfc_trans_call. */
1247 res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
1248 gfc_free_statements (ppc_code);
1250 if (UNLIMITED_POLY(obj))
1252 /* Check if rhs is non-NULL. */
1253 gfc_se src;
1254 gfc_init_se (&src, NULL);
1255 gfc_conv_expr (&src, rhs);
1256 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1257 tree cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1258 src.expr, fold_convert (TREE_TYPE (src.expr),
1259 null_pointer_node));
1260 res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
1261 build_empty_stmt (input_location));
1264 return res;
1267 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
1268 A MEMCPY is needed to copy the full data from the default initializer
1269 of the dynamic type. */
1271 tree
1272 gfc_trans_class_init_assign (gfc_code *code)
1274 stmtblock_t block;
1275 tree tmp;
1276 gfc_se dst,src,memsz;
1277 gfc_expr *lhs, *rhs, *sz;
1279 gfc_start_block (&block);
1281 lhs = gfc_copy_expr (code->expr1);
1282 gfc_add_data_component (lhs);
1284 rhs = gfc_copy_expr (code->expr1);
1285 gfc_add_vptr_component (rhs);
1287 /* Make sure that the component backend_decls have been built, which
1288 will not have happened if the derived types concerned have not
1289 been referenced. */
1290 gfc_get_derived_type (rhs->ts.u.derived);
1291 gfc_add_def_init_component (rhs);
1292 /* The _def_init is always scalar. */
1293 rhs->rank = 0;
1295 if (code->expr1->ts.type == BT_CLASS
1296 && CLASS_DATA (code->expr1)->attr.dimension)
1297 tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
1298 else
1300 sz = gfc_copy_expr (code->expr1);
1301 gfc_add_vptr_component (sz);
1302 gfc_add_size_component (sz);
1304 gfc_init_se (&dst, NULL);
1305 gfc_init_se (&src, NULL);
1306 gfc_init_se (&memsz, NULL);
1307 gfc_conv_expr (&dst, lhs);
1308 gfc_conv_expr (&src, rhs);
1309 gfc_conv_expr (&memsz, sz);
1310 gfc_add_block_to_block (&block, &src.pre);
1311 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1313 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
1315 if (UNLIMITED_POLY(code->expr1))
1317 /* Check if _def_init is non-NULL. */
1318 tree cond = fold_build2_loc (input_location, NE_EXPR,
1319 boolean_type_node, src.expr,
1320 fold_convert (TREE_TYPE (src.expr),
1321 null_pointer_node));
1322 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
1323 tmp, build_empty_stmt (input_location));
1327 if (code->expr1->symtree->n.sym->attr.optional
1328 || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master)
1330 tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
1331 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
1332 present, tmp,
1333 build_empty_stmt (input_location));
1336 gfc_add_expr_to_block (&block, tmp);
1338 return gfc_finish_block (&block);
1342 /* Translate an assignment to a CLASS object
1343 (pointer or ordinary assignment). */
1345 tree
1346 gfc_trans_class_assign (gfc_expr *expr1, gfc_expr *expr2, gfc_exec_op op)
1348 stmtblock_t block;
1349 tree tmp;
1350 gfc_expr *lhs;
1351 gfc_expr *rhs;
1352 gfc_ref *ref;
1354 gfc_start_block (&block);
1356 ref = expr1->ref;
1357 while (ref && ref->next)
1358 ref = ref->next;
1360 /* Class valued proc_pointer assignments do not need any further
1361 preparation. */
1362 if (ref && ref->type == REF_COMPONENT
1363 && ref->u.c.component->attr.proc_pointer
1364 && expr2->expr_type == EXPR_VARIABLE
1365 && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE
1366 && op == EXEC_POINTER_ASSIGN)
1367 goto assign;
1369 if (expr2->ts.type != BT_CLASS)
1371 /* Insert an additional assignment which sets the '_vptr' field. */
1372 gfc_symbol *vtab = NULL;
1373 gfc_symtree *st;
1375 lhs = gfc_copy_expr (expr1);
1376 gfc_add_vptr_component (lhs);
1378 if (UNLIMITED_POLY (expr1)
1379 && expr2->expr_type == EXPR_NULL && expr2->ts.type == BT_UNKNOWN)
1381 rhs = gfc_get_null_expr (&expr2->where);
1382 goto assign_vptr;
1385 if (expr2->expr_type == EXPR_NULL)
1386 vtab = gfc_find_vtab (&expr1->ts);
1387 else
1388 vtab = gfc_find_vtab (&expr2->ts);
1389 gcc_assert (vtab);
1391 rhs = gfc_get_expr ();
1392 rhs->expr_type = EXPR_VARIABLE;
1393 gfc_find_sym_tree (vtab->name, vtab->ns, 1, &st);
1394 rhs->symtree = st;
1395 rhs->ts = vtab->ts;
1396 assign_vptr:
1397 tmp = gfc_trans_pointer_assignment (lhs, rhs);
1398 gfc_add_expr_to_block (&block, tmp);
1400 gfc_free_expr (lhs);
1401 gfc_free_expr (rhs);
1403 else if (expr1->ts.type == BT_DERIVED && UNLIMITED_POLY (expr2))
1405 /* F2003:C717 only sequence and bind-C types can come here. */
1406 gcc_assert (expr1->ts.u.derived->attr.sequence
1407 || expr1->ts.u.derived->attr.is_bind_c);
1408 gfc_add_data_component (expr2);
1409 goto assign;
1411 else if (CLASS_DATA (expr2)->attr.dimension && expr2->expr_type != EXPR_FUNCTION)
1413 /* Insert an additional assignment which sets the '_vptr' field. */
1414 lhs = gfc_copy_expr (expr1);
1415 gfc_add_vptr_component (lhs);
1417 rhs = gfc_copy_expr (expr2);
1418 gfc_add_vptr_component (rhs);
1420 tmp = gfc_trans_pointer_assignment (lhs, rhs);
1421 gfc_add_expr_to_block (&block, tmp);
1423 gfc_free_expr (lhs);
1424 gfc_free_expr (rhs);
1427 /* Do the actual CLASS assignment. */
1428 if (expr2->ts.type == BT_CLASS
1429 && !CLASS_DATA (expr2)->attr.dimension)
1430 op = EXEC_ASSIGN;
1431 else if (expr2->expr_type != EXPR_FUNCTION || expr2->ts.type != BT_CLASS
1432 || !CLASS_DATA (expr2)->attr.dimension)
1433 gfc_add_data_component (expr1);
1435 assign:
1437 if (op == EXEC_ASSIGN)
1438 tmp = gfc_trans_assignment (expr1, expr2, false, true);
1439 else if (op == EXEC_POINTER_ASSIGN)
1440 tmp = gfc_trans_pointer_assignment (expr1, expr2);
1441 else
1442 gcc_unreachable();
1444 gfc_add_expr_to_block (&block, tmp);
1446 return gfc_finish_block (&block);
1450 /* End of prototype trans-class.c */
1453 static void
1454 realloc_lhs_warning (bt type, bool array, locus *where)
1456 if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
1457 gfc_warning (OPT_Wrealloc_lhs,
1458 "Code for reallocating the allocatable array at %L will "
1459 "be added", where);
1460 else if (warn_realloc_lhs_all)
1461 gfc_warning (OPT_Wrealloc_lhs_all,
1462 "Code for reallocating the allocatable variable at %L "
1463 "will be added", where);
1467 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
1468 gfc_expr *);
1470 /* Copy the scalarization loop variables. */
1472 static void
1473 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
1475 dest->ss = src->ss;
1476 dest->loop = src->loop;
1480 /* Initialize a simple expression holder.
1482 Care must be taken when multiple se are created with the same parent.
1483 The child se must be kept in sync. The easiest way is to delay creation
1484 of a child se until after after the previous se has been translated. */
1486 void
1487 gfc_init_se (gfc_se * se, gfc_se * parent)
1489 memset (se, 0, sizeof (gfc_se));
1490 gfc_init_block (&se->pre);
1491 gfc_init_block (&se->post);
1493 se->parent = parent;
1495 if (parent)
1496 gfc_copy_se_loopvars (se, parent);
1500 /* Advances to the next SS in the chain. Use this rather than setting
1501 se->ss = se->ss->next because all the parents needs to be kept in sync.
1502 See gfc_init_se. */
1504 void
1505 gfc_advance_se_ss_chain (gfc_se * se)
1507 gfc_se *p;
1508 gfc_ss *ss;
1510 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
1512 p = se;
1513 /* Walk down the parent chain. */
1514 while (p != NULL)
1516 /* Simple consistency check. */
1517 gcc_assert (p->parent == NULL || p->parent->ss == p->ss
1518 || p->parent->ss->nested_ss == p->ss);
1520 /* If we were in a nested loop, the next scalarized expression can be
1521 on the parent ss' next pointer. Thus we should not take the next
1522 pointer blindly, but rather go up one nest level as long as next
1523 is the end of chain. */
1524 ss = p->ss;
1525 while (ss->next == gfc_ss_terminator && ss->parent != NULL)
1526 ss = ss->parent;
1528 p->ss = ss->next;
1530 p = p->parent;
1535 /* Ensures the result of the expression as either a temporary variable
1536 or a constant so that it can be used repeatedly. */
1538 void
1539 gfc_make_safe_expr (gfc_se * se)
1541 tree var;
1543 if (CONSTANT_CLASS_P (se->expr))
1544 return;
1546 /* We need a temporary for this result. */
1547 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
1548 gfc_add_modify (&se->pre, var, se->expr);
1549 se->expr = var;
1553 /* Return an expression which determines if a dummy parameter is present.
1554 Also used for arguments to procedures with multiple entry points. */
1556 tree
1557 gfc_conv_expr_present (gfc_symbol * sym)
1559 tree decl, cond;
1561 gcc_assert (sym->attr.dummy);
1562 decl = gfc_get_symbol_decl (sym);
1564 /* Intrinsic scalars with VALUE attribute which are passed by value
1565 use a hidden argument to denote the present status. */
1566 if (sym->attr.value && sym->ts.type != BT_CHARACTER
1567 && sym->ts.type != BT_CLASS && sym->ts.type != BT_DERIVED
1568 && !sym->attr.dimension)
1570 char name[GFC_MAX_SYMBOL_LEN + 2];
1571 tree tree_name;
1573 gcc_assert (TREE_CODE (decl) == PARM_DECL);
1574 name[0] = '_';
1575 strcpy (&name[1], sym->name);
1576 tree_name = get_identifier (name);
1578 /* Walk function argument list to find hidden arg. */
1579 cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
1580 for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
1581 if (DECL_NAME (cond) == tree_name)
1582 break;
1584 gcc_assert (cond);
1585 return cond;
1588 if (TREE_CODE (decl) != PARM_DECL)
1590 /* Array parameters use a temporary descriptor, we want the real
1591 parameter. */
1592 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
1593 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
1594 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
1597 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
1598 fold_convert (TREE_TYPE (decl), null_pointer_node));
1600 /* Fortran 2008 allows to pass null pointers and non-associated pointers
1601 as actual argument to denote absent dummies. For array descriptors,
1602 we thus also need to check the array descriptor. For BT_CLASS, it
1603 can also occur for scalars and F2003 due to type->class wrapping and
1604 class->class wrapping. Note further that BT_CLASS always uses an
1605 array descriptor for arrays, also for explicit-shape/assumed-size. */
1607 if (!sym->attr.allocatable
1608 && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
1609 || (sym->ts.type == BT_CLASS
1610 && !CLASS_DATA (sym)->attr.allocatable
1611 && !CLASS_DATA (sym)->attr.class_pointer))
1612 && ((gfc_option.allow_std & GFC_STD_F2008) != 0
1613 || sym->ts.type == BT_CLASS))
1615 tree tmp;
1617 if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
1618 || sym->as->type == AS_ASSUMED_RANK
1619 || sym->attr.codimension))
1620 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
1622 tmp = build_fold_indirect_ref_loc (input_location, decl);
1623 if (sym->ts.type == BT_CLASS)
1624 tmp = gfc_class_data_get (tmp);
1625 tmp = gfc_conv_array_data (tmp);
1627 else if (sym->ts.type == BT_CLASS)
1628 tmp = gfc_class_data_get (decl);
1629 else
1630 tmp = NULL_TREE;
1632 if (tmp != NULL_TREE)
1634 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
1635 fold_convert (TREE_TYPE (tmp), null_pointer_node));
1636 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1637 boolean_type_node, cond, tmp);
1641 return cond;
1645 /* Converts a missing, dummy argument into a null or zero. */
1647 void
1648 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
1650 tree present;
1651 tree tmp;
1653 present = gfc_conv_expr_present (arg->symtree->n.sym);
1655 if (kind > 0)
1657 /* Create a temporary and convert it to the correct type. */
1658 tmp = gfc_get_int_type (kind);
1659 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
1660 se->expr));
1662 /* Test for a NULL value. */
1663 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
1664 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
1665 tmp = gfc_evaluate_now (tmp, &se->pre);
1666 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
1668 else
1670 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
1671 present, se->expr,
1672 build_zero_cst (TREE_TYPE (se->expr)));
1673 tmp = gfc_evaluate_now (tmp, &se->pre);
1674 se->expr = tmp;
1677 if (ts.type == BT_CHARACTER)
1679 tmp = build_int_cst (gfc_charlen_type_node, 0);
1680 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
1681 present, se->string_length, tmp);
1682 tmp = gfc_evaluate_now (tmp, &se->pre);
1683 se->string_length = tmp;
1685 return;
1689 /* Get the character length of an expression, looking through gfc_refs
1690 if necessary. */
1692 tree
1693 gfc_get_expr_charlen (gfc_expr *e)
1695 gfc_ref *r;
1696 tree length;
1698 gcc_assert (e->expr_type == EXPR_VARIABLE
1699 && e->ts.type == BT_CHARACTER);
1701 length = NULL; /* To silence compiler warning. */
1703 if (is_subref_array (e) && e->ts.u.cl->length)
1705 gfc_se tmpse;
1706 gfc_init_se (&tmpse, NULL);
1707 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
1708 e->ts.u.cl->backend_decl = tmpse.expr;
1709 return tmpse.expr;
1712 /* First candidate: if the variable is of type CHARACTER, the
1713 expression's length could be the length of the character
1714 variable. */
1715 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
1716 length = e->symtree->n.sym->ts.u.cl->backend_decl;
1718 /* Look through the reference chain for component references. */
1719 for (r = e->ref; r; r = r->next)
1721 switch (r->type)
1723 case REF_COMPONENT:
1724 if (r->u.c.component->ts.type == BT_CHARACTER)
1725 length = r->u.c.component->ts.u.cl->backend_decl;
1726 break;
1728 case REF_ARRAY:
1729 /* Do nothing. */
1730 break;
1732 default:
1733 /* We should never got substring references here. These will be
1734 broken down by the scalarizer. */
1735 gcc_unreachable ();
1736 break;
1740 gcc_assert (length != NULL);
1741 return length;
1745 /* Return for an expression the backend decl of the coarray. */
1747 tree
1748 gfc_get_tree_for_caf_expr (gfc_expr *expr)
1750 tree caf_decl;
1751 bool found = false;
1752 gfc_ref *ref, *comp_ref = NULL;
1754 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
1756 /* Not-implemented diagnostic. */
1757 for (ref = expr->ref; ref; ref = ref->next)
1758 if (ref->type == REF_COMPONENT)
1760 comp_ref = ref;
1761 if ((ref->u.c.component->ts.type == BT_CLASS
1762 && !CLASS_DATA (ref->u.c.component)->attr.codimension
1763 && (CLASS_DATA (ref->u.c.component)->attr.pointer
1764 || CLASS_DATA (ref->u.c.component)->attr.allocatable))
1765 || (ref->u.c.component->ts.type != BT_CLASS
1766 && !ref->u.c.component->attr.codimension
1767 && (ref->u.c.component->attr.pointer
1768 || ref->u.c.component->attr.allocatable)))
1769 gfc_error ("Sorry, coindexed access to a pointer or allocatable "
1770 "component of the coindexed coarray at %L is not yet "
1771 "supported", &expr->where);
1773 if ((!comp_ref
1774 && ((expr->symtree->n.sym->ts.type == BT_CLASS
1775 && CLASS_DATA (expr->symtree->n.sym)->attr.alloc_comp)
1776 || (expr->symtree->n.sym->ts.type == BT_DERIVED
1777 && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)))
1778 || (comp_ref
1779 && ((comp_ref->u.c.component->ts.type == BT_CLASS
1780 && CLASS_DATA (comp_ref->u.c.component)->attr.alloc_comp)
1781 || (comp_ref->u.c.component->ts.type == BT_DERIVED
1782 && comp_ref->u.c.component->ts.u.derived->attr.alloc_comp))))
1783 gfc_error ("Sorry, coindexed coarray at %L with allocatable component is "
1784 "not yet supported", &expr->where);
1786 if (expr->rank)
1788 /* Without the new array descriptor, access like "caf[i]%a(:)%b" is in
1789 general not possible as the required stride multiplier might be not
1790 a multiple of c_sizeof(b). In case of noncoindexed access, the
1791 scalarizer often takes care of it - for coarrays, it always fails. */
1792 for (ref = expr->ref; ref; ref = ref->next)
1793 if (ref->type == REF_COMPONENT
1794 && ((ref->u.c.component->ts.type == BT_CLASS
1795 && CLASS_DATA (ref->u.c.component)->attr.codimension)
1796 || (ref->u.c.component->ts.type != BT_CLASS
1797 && ref->u.c.component->attr.codimension)))
1798 break;
1799 if (ref == NULL)
1800 ref = expr->ref;
1801 for ( ; ref; ref = ref->next)
1802 if (ref->type == REF_ARRAY && ref->u.ar.dimen)
1803 break;
1804 for ( ; ref; ref = ref->next)
1805 if (ref->type == REF_COMPONENT)
1806 gfc_error ("Sorry, coindexed access at %L to a scalar component "
1807 "with an array partref is not yet supported",
1808 &expr->where);
1811 caf_decl = expr->symtree->n.sym->backend_decl;
1812 gcc_assert (caf_decl);
1813 if (expr->symtree->n.sym->ts.type == BT_CLASS)
1814 caf_decl = gfc_class_data_get (caf_decl);
1815 if (expr->symtree->n.sym->attr.codimension)
1816 return caf_decl;
1818 /* The following code assumes that the coarray is a component reachable via
1819 only scalar components/variables; the Fortran standard guarantees this. */
1821 for (ref = expr->ref; ref; ref = ref->next)
1822 if (ref->type == REF_COMPONENT)
1824 gfc_component *comp = ref->u.c.component;
1826 if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
1827 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1828 caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
1829 TREE_TYPE (comp->backend_decl), caf_decl,
1830 comp->backend_decl, NULL_TREE);
1831 if (comp->ts.type == BT_CLASS)
1832 caf_decl = gfc_class_data_get (caf_decl);
1833 if (comp->attr.codimension)
1835 found = true;
1836 break;
1839 gcc_assert (found && caf_decl);
1840 return caf_decl;
1844 /* Obtain the Coarray token - and optionally also the offset. */
1846 void
1847 gfc_get_caf_token_offset (tree *token, tree *offset, tree caf_decl, tree se_expr,
1848 gfc_expr *expr)
1850 tree tmp;
1852 /* Coarray token. */
1853 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
1855 gcc_assert (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl))
1856 == GFC_ARRAY_ALLOCATABLE
1857 || expr->symtree->n.sym->attr.select_type_temporary);
1858 *token = gfc_conv_descriptor_token (caf_decl);
1860 else if (DECL_LANG_SPECIFIC (caf_decl)
1861 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
1862 *token = GFC_DECL_TOKEN (caf_decl);
1863 else
1865 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
1866 && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
1867 *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
1870 if (offset == NULL)
1871 return;
1873 /* Offset between the coarray base address and the address wanted. */
1874 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
1875 && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
1876 || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
1877 *offset = build_int_cst (gfc_array_index_type, 0);
1878 else if (DECL_LANG_SPECIFIC (caf_decl)
1879 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
1880 *offset = GFC_DECL_CAF_OFFSET (caf_decl);
1881 else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
1882 *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
1883 else
1884 *offset = build_int_cst (gfc_array_index_type, 0);
1886 if (POINTER_TYPE_P (TREE_TYPE (se_expr))
1887 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
1889 tmp = build_fold_indirect_ref_loc (input_location, se_expr);
1890 tmp = gfc_conv_descriptor_data_get (tmp);
1892 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
1893 tmp = gfc_conv_descriptor_data_get (se_expr);
1894 else
1896 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
1897 tmp = se_expr;
1900 *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1901 *offset, fold_convert (gfc_array_index_type, tmp));
1903 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
1904 tmp = gfc_conv_descriptor_data_get (caf_decl);
1905 else
1907 gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
1908 tmp = caf_decl;
1911 *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1912 fold_convert (gfc_array_index_type, *offset),
1913 fold_convert (gfc_array_index_type, tmp));
1917 /* Convert the coindex of a coarray into an image index; the result is
1918 image_num = (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
1919 + (idx(3)-lcobound(3))*extend(1)*extent(2) + ... */
1921 tree
1922 gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
1924 gfc_ref *ref;
1925 tree lbound, ubound, extent, tmp, img_idx;
1926 gfc_se se;
1927 int i;
1929 for (ref = e->ref; ref; ref = ref->next)
1930 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
1931 break;
1932 gcc_assert (ref != NULL);
1934 img_idx = integer_zero_node;
1935 extent = integer_one_node;
1936 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
1937 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
1939 gfc_init_se (&se, NULL);
1940 gfc_conv_expr_type (&se, ref->u.ar.start[i], integer_type_node);
1941 gfc_add_block_to_block (block, &se.pre);
1942 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
1943 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1944 integer_type_node, se.expr,
1945 fold_convert(integer_type_node, lbound));
1946 tmp = fold_build2_loc (input_location, MULT_EXPR, integer_type_node,
1947 extent, tmp);
1948 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1949 img_idx, tmp);
1950 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
1952 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
1953 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1954 tmp = fold_convert (integer_type_node, tmp);
1955 extent = fold_build2_loc (input_location, MULT_EXPR,
1956 integer_type_node, extent, tmp);
1959 else
1960 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
1962 gfc_init_se (&se, NULL);
1963 gfc_conv_expr_type (&se, ref->u.ar.start[i], integer_type_node);
1964 gfc_add_block_to_block (block, &se.pre);
1965 lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
1966 lbound = fold_convert (integer_type_node, lbound);
1967 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1968 integer_type_node, se.expr, lbound);
1969 tmp = fold_build2_loc (input_location, MULT_EXPR, integer_type_node,
1970 extent, tmp);
1971 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1972 img_idx, tmp);
1973 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
1975 ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
1976 ubound = fold_convert (integer_type_node, ubound);
1977 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1978 integer_type_node, ubound, lbound);
1979 tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1980 tmp, integer_one_node);
1981 extent = fold_build2_loc (input_location, MULT_EXPR,
1982 integer_type_node, extent, tmp);
1985 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1986 img_idx, integer_one_node);
1987 return img_idx;
1991 /* For each character array constructor subexpression without a ts.u.cl->length,
1992 replace it by its first element (if there aren't any elements, the length
1993 should already be set to zero). */
1995 static void
1996 flatten_array_ctors_without_strlen (gfc_expr* e)
1998 gfc_actual_arglist* arg;
1999 gfc_constructor* c;
2001 if (!e)
2002 return;
2004 switch (e->expr_type)
2007 case EXPR_OP:
2008 flatten_array_ctors_without_strlen (e->value.op.op1);
2009 flatten_array_ctors_without_strlen (e->value.op.op2);
2010 break;
2012 case EXPR_COMPCALL:
2013 /* TODO: Implement as with EXPR_FUNCTION when needed. */
2014 gcc_unreachable ();
2016 case EXPR_FUNCTION:
2017 for (arg = e->value.function.actual; arg; arg = arg->next)
2018 flatten_array_ctors_without_strlen (arg->expr);
2019 break;
2021 case EXPR_ARRAY:
2023 /* We've found what we're looking for. */
2024 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
2026 gfc_constructor *c;
2027 gfc_expr* new_expr;
2029 gcc_assert (e->value.constructor);
2031 c = gfc_constructor_first (e->value.constructor);
2032 new_expr = c->expr;
2033 c->expr = NULL;
2035 flatten_array_ctors_without_strlen (new_expr);
2036 gfc_replace_expr (e, new_expr);
2037 break;
2040 /* Otherwise, fall through to handle constructor elements. */
2041 case EXPR_STRUCTURE:
2042 for (c = gfc_constructor_first (e->value.constructor);
2043 c; c = gfc_constructor_next (c))
2044 flatten_array_ctors_without_strlen (c->expr);
2045 break;
2047 default:
2048 break;
2054 /* Generate code to initialize a string length variable. Returns the
2055 value. For array constructors, cl->length might be NULL and in this case,
2056 the first element of the constructor is needed. expr is the original
2057 expression so we can access it but can be NULL if this is not needed. */
2059 void
2060 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
2062 gfc_se se;
2064 gfc_init_se (&se, NULL);
2066 if (!cl->length
2067 && cl->backend_decl
2068 && TREE_CODE (cl->backend_decl) == VAR_DECL)
2069 return;
2071 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
2072 "flatten" array constructors by taking their first element; all elements
2073 should be the same length or a cl->length should be present. */
2074 if (!cl->length)
2076 gfc_expr* expr_flat;
2077 gcc_assert (expr);
2078 expr_flat = gfc_copy_expr (expr);
2079 flatten_array_ctors_without_strlen (expr_flat);
2080 gfc_resolve_expr (expr_flat);
2082 gfc_conv_expr (&se, expr_flat);
2083 gfc_add_block_to_block (pblock, &se.pre);
2084 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
2086 gfc_free_expr (expr_flat);
2087 return;
2090 /* Convert cl->length. */
2092 gcc_assert (cl->length);
2094 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
2095 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2096 se.expr, build_int_cst (gfc_charlen_type_node, 0));
2097 gfc_add_block_to_block (pblock, &se.pre);
2099 if (cl->backend_decl)
2100 gfc_add_modify (pblock, cl->backend_decl, se.expr);
2101 else
2102 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
2106 static void
2107 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
2108 const char *name, locus *where)
2110 tree tmp;
2111 tree type;
2112 tree fault;
2113 gfc_se start;
2114 gfc_se end;
2115 char *msg;
2116 mpz_t length;
2118 type = gfc_get_character_type (kind, ref->u.ss.length);
2119 type = build_pointer_type (type);
2121 gfc_init_se (&start, se);
2122 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
2123 gfc_add_block_to_block (&se->pre, &start.pre);
2125 if (integer_onep (start.expr))
2126 gfc_conv_string_parameter (se);
2127 else
2129 tmp = start.expr;
2130 STRIP_NOPS (tmp);
2131 /* Avoid multiple evaluation of substring start. */
2132 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2133 start.expr = gfc_evaluate_now (start.expr, &se->pre);
2135 /* Change the start of the string. */
2136 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
2137 tmp = se->expr;
2138 else
2139 tmp = build_fold_indirect_ref_loc (input_location,
2140 se->expr);
2141 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
2142 se->expr = gfc_build_addr_expr (type, tmp);
2145 /* Length = end + 1 - start. */
2146 gfc_init_se (&end, se);
2147 if (ref->u.ss.end == NULL)
2148 end.expr = se->string_length;
2149 else
2151 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
2152 gfc_add_block_to_block (&se->pre, &end.pre);
2154 tmp = end.expr;
2155 STRIP_NOPS (tmp);
2156 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2157 end.expr = gfc_evaluate_now (end.expr, &se->pre);
2159 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2161 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
2162 boolean_type_node, start.expr,
2163 end.expr);
2165 /* Check lower bound. */
2166 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
2167 start.expr,
2168 build_int_cst (gfc_charlen_type_node, 1));
2169 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2170 boolean_type_node, nonempty, fault);
2171 if (name)
2172 msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
2173 "is less than one", name);
2174 else
2175 msg = xasprintf ("Substring out of bounds: lower bound (%%ld)"
2176 "is less than one");
2177 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2178 fold_convert (long_integer_type_node,
2179 start.expr));
2180 free (msg);
2182 /* Check upper bound. */
2183 fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
2184 end.expr, se->string_length);
2185 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2186 boolean_type_node, nonempty, fault);
2187 if (name)
2188 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
2189 "exceeds string length (%%ld)", name);
2190 else
2191 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
2192 "exceeds string length (%%ld)");
2193 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2194 fold_convert (long_integer_type_node, end.expr),
2195 fold_convert (long_integer_type_node,
2196 se->string_length));
2197 free (msg);
2200 /* Try to calculate the length from the start and end expressions. */
2201 if (ref->u.ss.end
2202 && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
2204 int i_len;
2206 i_len = mpz_get_si (length) + 1;
2207 if (i_len < 0)
2208 i_len = 0;
2210 tmp = build_int_cst (gfc_charlen_type_node, i_len);
2211 mpz_clear (length); /* Was initialized by gfc_dep_difference. */
2213 else
2215 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
2216 end.expr, start.expr);
2217 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
2218 build_int_cst (gfc_charlen_type_node, 1), tmp);
2219 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2220 tmp, build_int_cst (gfc_charlen_type_node, 0));
2223 se->string_length = tmp;
2227 /* Convert a derived type component reference. */
2229 static void
2230 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
2232 gfc_component *c;
2233 tree tmp;
2234 tree decl;
2235 tree field;
2237 c = ref->u.c.component;
2239 if (c->backend_decl == NULL_TREE
2240 && ref->u.c.sym != NULL)
2241 gfc_get_derived_type (ref->u.c.sym);
2243 field = c->backend_decl;
2244 gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
2245 decl = se->expr;
2247 /* Components can correspond to fields of different containing
2248 types, as components are created without context, whereas
2249 a concrete use of a component has the type of decl as context.
2250 So, if the type doesn't match, we search the corresponding
2251 FIELD_DECL in the parent type. To not waste too much time
2252 we cache this result in norestrict_decl. */
2254 if (DECL_FIELD_CONTEXT (field) != TREE_TYPE (decl))
2256 tree f2 = c->norestrict_decl;
2257 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
2258 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
2259 if (TREE_CODE (f2) == FIELD_DECL
2260 && DECL_NAME (f2) == DECL_NAME (field))
2261 break;
2262 gcc_assert (f2);
2263 c->norestrict_decl = f2;
2264 field = f2;
2267 if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
2268 && strcmp ("_data", c->name) == 0)
2270 /* Found a ref to the _data component. Store the associated ref to
2271 the vptr in se->class_vptr. */
2272 se->class_vptr = gfc_class_vptr_get (decl);
2274 else
2275 se->class_vptr = NULL_TREE;
2277 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
2278 decl, field, NULL_TREE);
2280 se->expr = tmp;
2282 /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
2283 strlen () conditional below. */
2284 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
2285 && !(c->attr.allocatable && c->ts.deferred))
2287 tmp = c->ts.u.cl->backend_decl;
2288 /* Components must always be constant length. */
2289 gcc_assert (tmp && INTEGER_CST_P (tmp));
2290 se->string_length = tmp;
2293 if (gfc_deferred_strlen (c, &field))
2295 tmp = fold_build3_loc (input_location, COMPONENT_REF,
2296 TREE_TYPE (field),
2297 decl, field, NULL_TREE);
2298 se->string_length = tmp;
2301 if (((c->attr.pointer || c->attr.allocatable)
2302 && (!c->attr.dimension && !c->attr.codimension)
2303 && c->ts.type != BT_CHARACTER)
2304 || c->attr.proc_pointer)
2305 se->expr = build_fold_indirect_ref_loc (input_location,
2306 se->expr);
2310 /* This function deals with component references to components of the
2311 parent type for derived type extensions. */
2312 static void
2313 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
2315 gfc_component *c;
2316 gfc_component *cmp;
2317 gfc_symbol *dt;
2318 gfc_ref parent;
2320 dt = ref->u.c.sym;
2321 c = ref->u.c.component;
2323 /* Return if the component is in the parent type. */
2324 for (cmp = dt->components; cmp; cmp = cmp->next)
2325 if (strcmp (c->name, cmp->name) == 0)
2326 return;
2328 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
2329 parent.type = REF_COMPONENT;
2330 parent.next = NULL;
2331 parent.u.c.sym = dt;
2332 parent.u.c.component = dt->components;
2334 if (dt->backend_decl == NULL)
2335 gfc_get_derived_type (dt);
2337 /* Build the reference and call self. */
2338 gfc_conv_component_ref (se, &parent);
2339 parent.u.c.sym = dt->components->ts.u.derived;
2340 parent.u.c.component = c;
2341 conv_parent_component_references (se, &parent);
2344 /* Return the contents of a variable. Also handles reference/pointer
2345 variables (all Fortran pointer references are implicit). */
2347 static void
2348 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
2350 gfc_ss *ss;
2351 gfc_ref *ref;
2352 gfc_symbol *sym;
2353 tree parent_decl = NULL_TREE;
2354 int parent_flag;
2355 bool return_value;
2356 bool alternate_entry;
2357 bool entry_master;
2358 bool is_classarray;
2359 bool first_time = true;
2361 sym = expr->symtree->n.sym;
2362 is_classarray = IS_CLASS_ARRAY (sym);
2363 ss = se->ss;
2364 if (ss != NULL)
2366 gfc_ss_info *ss_info = ss->info;
2368 /* Check that something hasn't gone horribly wrong. */
2369 gcc_assert (ss != gfc_ss_terminator);
2370 gcc_assert (ss_info->expr == expr);
2372 /* A scalarized term. We already know the descriptor. */
2373 se->expr = ss_info->data.array.descriptor;
2374 se->string_length = ss_info->string_length;
2375 ref = ss_info->data.array.ref;
2376 if (ref)
2377 gcc_assert (ref->type == REF_ARRAY
2378 && ref->u.ar.type != AR_ELEMENT);
2379 else
2380 gfc_conv_tmp_array_ref (se);
2382 else
2384 tree se_expr = NULL_TREE;
2386 se->expr = gfc_get_symbol_decl (sym);
2388 /* Deal with references to a parent results or entries by storing
2389 the current_function_decl and moving to the parent_decl. */
2390 return_value = sym->attr.function && sym->result == sym;
2391 alternate_entry = sym->attr.function && sym->attr.entry
2392 && sym->result == sym;
2393 entry_master = sym->attr.result
2394 && sym->ns->proc_name->attr.entry_master
2395 && !gfc_return_by_reference (sym->ns->proc_name);
2396 if (current_function_decl)
2397 parent_decl = DECL_CONTEXT (current_function_decl);
2399 if ((se->expr == parent_decl && return_value)
2400 || (sym->ns && sym->ns->proc_name
2401 && parent_decl
2402 && sym->ns->proc_name->backend_decl == parent_decl
2403 && (alternate_entry || entry_master)))
2404 parent_flag = 1;
2405 else
2406 parent_flag = 0;
2408 /* Special case for assigning the return value of a function.
2409 Self recursive functions must have an explicit return value. */
2410 if (return_value && (se->expr == current_function_decl || parent_flag))
2411 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2413 /* Similarly for alternate entry points. */
2414 else if (alternate_entry
2415 && (sym->ns->proc_name->backend_decl == current_function_decl
2416 || parent_flag))
2418 gfc_entry_list *el = NULL;
2420 for (el = sym->ns->entries; el; el = el->next)
2421 if (sym == el->sym)
2423 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2424 break;
2428 else if (entry_master
2429 && (sym->ns->proc_name->backend_decl == current_function_decl
2430 || parent_flag))
2431 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2433 if (se_expr)
2434 se->expr = se_expr;
2436 /* Procedure actual arguments. */
2437 else if (sym->attr.flavor == FL_PROCEDURE
2438 && se->expr != current_function_decl)
2440 if (!sym->attr.dummy && !sym->attr.proc_pointer)
2442 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
2443 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
2445 return;
2449 /* Dereference the expression, where needed. Since characters
2450 are entirely different from other types, they are treated
2451 separately. */
2452 if (sym->ts.type == BT_CHARACTER)
2454 /* Dereference character pointer dummy arguments
2455 or results. */
2456 if ((sym->attr.pointer || sym->attr.allocatable)
2457 && (sym->attr.dummy
2458 || sym->attr.function
2459 || sym->attr.result))
2460 se->expr = build_fold_indirect_ref_loc (input_location,
2461 se->expr);
2464 else if (!sym->attr.value)
2466 /* Dereference temporaries for class array dummy arguments. */
2467 if (sym->attr.dummy && is_classarray
2468 && GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr)))
2470 if (!se->descriptor_only)
2471 se->expr = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
2473 se->expr = build_fold_indirect_ref_loc (input_location,
2474 se->expr);
2477 /* Dereference non-character scalar dummy arguments. */
2478 if (sym->attr.dummy && !sym->attr.dimension
2479 && !(sym->attr.codimension && sym->attr.allocatable)
2480 && (sym->ts.type != BT_CLASS
2481 || (!CLASS_DATA (sym)->attr.dimension
2482 && !(CLASS_DATA (sym)->attr.codimension
2483 && CLASS_DATA (sym)->attr.allocatable))))
2484 se->expr = build_fold_indirect_ref_loc (input_location,
2485 se->expr);
2487 /* Dereference scalar hidden result. */
2488 if (flag_f2c && sym->ts.type == BT_COMPLEX
2489 && (sym->attr.function || sym->attr.result)
2490 && !sym->attr.dimension && !sym->attr.pointer
2491 && !sym->attr.always_explicit)
2492 se->expr = build_fold_indirect_ref_loc (input_location,
2493 se->expr);
2495 /* Dereference non-character, non-class pointer variables.
2496 These must be dummies, results, or scalars. */
2497 if (!is_classarray
2498 && (sym->attr.pointer || sym->attr.allocatable
2499 || gfc_is_associate_pointer (sym)
2500 || (sym->as && sym->as->type == AS_ASSUMED_RANK))
2501 && (sym->attr.dummy
2502 || sym->attr.function
2503 || sym->attr.result
2504 || (!sym->attr.dimension
2505 && (!sym->attr.codimension || !sym->attr.allocatable))))
2506 se->expr = build_fold_indirect_ref_loc (input_location,
2507 se->expr);
2508 /* Now treat the class array pointer variables accordingly. */
2509 else if (sym->ts.type == BT_CLASS
2510 && sym->attr.dummy
2511 && (CLASS_DATA (sym)->attr.dimension
2512 || CLASS_DATA (sym)->attr.codimension)
2513 && ((CLASS_DATA (sym)->as
2514 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
2515 || CLASS_DATA (sym)->attr.allocatable
2516 || CLASS_DATA (sym)->attr.class_pointer))
2517 se->expr = build_fold_indirect_ref_loc (input_location,
2518 se->expr);
2519 /* And the case where a non-dummy, non-result, non-function,
2520 non-allotable and non-pointer classarray is present. This case was
2521 previously covered by the first if, but with introducing the
2522 condition !is_classarray there, that case has to be covered
2523 explicitly. */
2524 else if (sym->ts.type == BT_CLASS
2525 && !sym->attr.dummy
2526 && !sym->attr.function
2527 && !sym->attr.result
2528 && (CLASS_DATA (sym)->attr.dimension
2529 || CLASS_DATA (sym)->attr.codimension)
2530 && (sym->assoc
2531 || !CLASS_DATA (sym)->attr.allocatable)
2532 && !CLASS_DATA (sym)->attr.class_pointer)
2533 se->expr = build_fold_indirect_ref_loc (input_location,
2534 se->expr);
2537 ref = expr->ref;
2540 /* For character variables, also get the length. */
2541 if (sym->ts.type == BT_CHARACTER)
2543 /* If the character length of an entry isn't set, get the length from
2544 the master function instead. */
2545 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
2546 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
2547 else
2548 se->string_length = sym->ts.u.cl->backend_decl;
2549 gcc_assert (se->string_length);
2552 while (ref)
2554 switch (ref->type)
2556 case REF_ARRAY:
2557 /* Return the descriptor if that's what we want and this is an array
2558 section reference. */
2559 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
2560 return;
2561 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
2562 /* Return the descriptor for array pointers and allocations. */
2563 if (se->want_pointer
2564 && ref->next == NULL && (se->descriptor_only))
2565 return;
2567 gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
2568 /* Return a pointer to an element. */
2569 break;
2571 case REF_COMPONENT:
2572 if (first_time && is_classarray && sym->attr.dummy
2573 && se->descriptor_only
2574 && !CLASS_DATA (sym)->attr.allocatable
2575 && !CLASS_DATA (sym)->attr.class_pointer
2576 && CLASS_DATA (sym)->as
2577 && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
2578 && strcmp ("_data", ref->u.c.component->name) == 0)
2579 /* Skip the first ref of a _data component, because for class
2580 arrays that one is already done by introducing a temporary
2581 array descriptor. */
2582 break;
2584 if (ref->u.c.sym->attr.extension)
2585 conv_parent_component_references (se, ref);
2587 gfc_conv_component_ref (se, ref);
2588 if (!ref->next && ref->u.c.sym->attr.codimension
2589 && se->want_pointer && se->descriptor_only)
2590 return;
2592 break;
2594 case REF_SUBSTRING:
2595 gfc_conv_substring (se, ref, expr->ts.kind,
2596 expr->symtree->name, &expr->where);
2597 break;
2599 default:
2600 gcc_unreachable ();
2601 break;
2603 first_time = false;
2604 ref = ref->next;
2606 /* Pointer assignment, allocation or pass by reference. Arrays are handled
2607 separately. */
2608 if (se->want_pointer)
2610 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
2611 gfc_conv_string_parameter (se);
2612 else
2613 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
2618 /* Unary ops are easy... Or they would be if ! was a valid op. */
2620 static void
2621 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
2623 gfc_se operand;
2624 tree type;
2626 gcc_assert (expr->ts.type != BT_CHARACTER);
2627 /* Initialize the operand. */
2628 gfc_init_se (&operand, se);
2629 gfc_conv_expr_val (&operand, expr->value.op.op1);
2630 gfc_add_block_to_block (&se->pre, &operand.pre);
2632 type = gfc_typenode_for_spec (&expr->ts);
2634 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
2635 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
2636 All other unary operators have an equivalent GIMPLE unary operator. */
2637 if (code == TRUTH_NOT_EXPR)
2638 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
2639 build_int_cst (type, 0));
2640 else
2641 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
2645 /* Expand power operator to optimal multiplications when a value is raised
2646 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
2647 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
2648 Programming", 3rd Edition, 1998. */
2650 /* This code is mostly duplicated from expand_powi in the backend.
2651 We establish the "optimal power tree" lookup table with the defined size.
2652 The items in the table are the exponents used to calculate the index
2653 exponents. Any integer n less than the value can get an "addition chain",
2654 with the first node being one. */
2655 #define POWI_TABLE_SIZE 256
2657 /* The table is from builtins.c. */
2658 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2660 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2661 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2662 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2663 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2664 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2665 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2666 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2667 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2668 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2669 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2670 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2671 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2672 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2673 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2674 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2675 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2676 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2677 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2678 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2679 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2680 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2681 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2682 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2683 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2684 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2685 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2686 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2687 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2688 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2689 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2690 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2691 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2694 /* If n is larger than lookup table's max index, we use the "window
2695 method". */
2696 #define POWI_WINDOW_SIZE 3
2698 /* Recursive function to expand the power operator. The temporary
2699 values are put in tmpvar. The function returns tmpvar[1] ** n. */
2700 static tree
2701 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
2703 tree op0;
2704 tree op1;
2705 tree tmp;
2706 int digit;
2708 if (n < POWI_TABLE_SIZE)
2710 if (tmpvar[n])
2711 return tmpvar[n];
2713 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
2714 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
2716 else if (n & 1)
2718 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2719 op0 = gfc_conv_powi (se, n - digit, tmpvar);
2720 op1 = gfc_conv_powi (se, digit, tmpvar);
2722 else
2724 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
2725 op1 = op0;
2728 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
2729 tmp = gfc_evaluate_now (tmp, &se->pre);
2731 if (n < POWI_TABLE_SIZE)
2732 tmpvar[n] = tmp;
2734 return tmp;
2738 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
2739 return 1. Else return 0 and a call to runtime library functions
2740 will have to be built. */
2741 static int
2742 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
2744 tree cond;
2745 tree tmp;
2746 tree type;
2747 tree vartmp[POWI_TABLE_SIZE];
2748 HOST_WIDE_INT m;
2749 unsigned HOST_WIDE_INT n;
2750 int sgn;
2751 wide_int wrhs = rhs;
2753 /* If exponent is too large, we won't expand it anyway, so don't bother
2754 with large integer values. */
2755 if (!wi::fits_shwi_p (wrhs))
2756 return 0;
2758 m = wrhs.to_shwi ();
2759 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
2760 of the asymmetric range of the integer type. */
2761 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
2763 type = TREE_TYPE (lhs);
2764 sgn = tree_int_cst_sgn (rhs);
2766 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
2767 || optimize_size) && (m > 2 || m < -1))
2768 return 0;
2770 /* rhs == 0 */
2771 if (sgn == 0)
2773 se->expr = gfc_build_const (type, integer_one_node);
2774 return 1;
2777 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
2778 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
2780 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2781 lhs, build_int_cst (TREE_TYPE (lhs), -1));
2782 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2783 lhs, build_int_cst (TREE_TYPE (lhs), 1));
2785 /* If rhs is even,
2786 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
2787 if ((n & 1) == 0)
2789 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2790 boolean_type_node, tmp, cond);
2791 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2792 tmp, build_int_cst (type, 1),
2793 build_int_cst (type, 0));
2794 return 1;
2796 /* If rhs is odd,
2797 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
2798 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
2799 build_int_cst (type, -1),
2800 build_int_cst (type, 0));
2801 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2802 cond, build_int_cst (type, 1), tmp);
2803 return 1;
2806 memset (vartmp, 0, sizeof (vartmp));
2807 vartmp[1] = lhs;
2808 if (sgn == -1)
2810 tmp = gfc_build_const (type, integer_one_node);
2811 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
2812 vartmp[1]);
2815 se->expr = gfc_conv_powi (se, n, vartmp);
2817 return 1;
2821 /* Power op (**). Constant integer exponent has special handling. */
2823 static void
2824 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
2826 tree gfc_int4_type_node;
2827 int kind;
2828 int ikind;
2829 int res_ikind_1, res_ikind_2;
2830 gfc_se lse;
2831 gfc_se rse;
2832 tree fndecl = NULL;
2834 gfc_init_se (&lse, se);
2835 gfc_conv_expr_val (&lse, expr->value.op.op1);
2836 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
2837 gfc_add_block_to_block (&se->pre, &lse.pre);
2839 gfc_init_se (&rse, se);
2840 gfc_conv_expr_val (&rse, expr->value.op.op2);
2841 gfc_add_block_to_block (&se->pre, &rse.pre);
2843 if (expr->value.op.op2->ts.type == BT_INTEGER
2844 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
2845 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
2846 return;
2848 gfc_int4_type_node = gfc_get_int_type (4);
2850 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
2851 library routine. But in the end, we have to convert the result back
2852 if this case applies -- with res_ikind_K, we keep track whether operand K
2853 falls into this case. */
2854 res_ikind_1 = -1;
2855 res_ikind_2 = -1;
2857 kind = expr->value.op.op1->ts.kind;
2858 switch (expr->value.op.op2->ts.type)
2860 case BT_INTEGER:
2861 ikind = expr->value.op.op2->ts.kind;
2862 switch (ikind)
2864 case 1:
2865 case 2:
2866 rse.expr = convert (gfc_int4_type_node, rse.expr);
2867 res_ikind_2 = ikind;
2868 /* Fall through. */
2870 case 4:
2871 ikind = 0;
2872 break;
2874 case 8:
2875 ikind = 1;
2876 break;
2878 case 16:
2879 ikind = 2;
2880 break;
2882 default:
2883 gcc_unreachable ();
2885 switch (kind)
2887 case 1:
2888 case 2:
2889 if (expr->value.op.op1->ts.type == BT_INTEGER)
2891 lse.expr = convert (gfc_int4_type_node, lse.expr);
2892 res_ikind_1 = kind;
2894 else
2895 gcc_unreachable ();
2896 /* Fall through. */
2898 case 4:
2899 kind = 0;
2900 break;
2902 case 8:
2903 kind = 1;
2904 break;
2906 case 10:
2907 kind = 2;
2908 break;
2910 case 16:
2911 kind = 3;
2912 break;
2914 default:
2915 gcc_unreachable ();
2918 switch (expr->value.op.op1->ts.type)
2920 case BT_INTEGER:
2921 if (kind == 3) /* Case 16 was not handled properly above. */
2922 kind = 2;
2923 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
2924 break;
2926 case BT_REAL:
2927 /* Use builtins for real ** int4. */
2928 if (ikind == 0)
2930 switch (kind)
2932 case 0:
2933 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
2934 break;
2936 case 1:
2937 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
2938 break;
2940 case 2:
2941 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
2942 break;
2944 case 3:
2945 /* Use the __builtin_powil() only if real(kind=16) is
2946 actually the C long double type. */
2947 if (!gfc_real16_is_float128)
2948 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
2949 break;
2951 default:
2952 gcc_unreachable ();
2956 /* If we don't have a good builtin for this, go for the
2957 library function. */
2958 if (!fndecl)
2959 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
2960 break;
2962 case BT_COMPLEX:
2963 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
2964 break;
2966 default:
2967 gcc_unreachable ();
2969 break;
2971 case BT_REAL:
2972 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
2973 break;
2975 case BT_COMPLEX:
2976 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
2977 break;
2979 default:
2980 gcc_unreachable ();
2981 break;
2984 se->expr = build_call_expr_loc (input_location,
2985 fndecl, 2, lse.expr, rse.expr);
2987 /* Convert the result back if it is of wrong integer kind. */
2988 if (res_ikind_1 != -1 && res_ikind_2 != -1)
2990 /* We want the maximum of both operand kinds as result. */
2991 if (res_ikind_1 < res_ikind_2)
2992 res_ikind_1 = res_ikind_2;
2993 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
2998 /* Generate code to allocate a string temporary. */
3000 tree
3001 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
3003 tree var;
3004 tree tmp;
3006 if (gfc_can_put_var_on_stack (len))
3008 /* Create a temporary variable to hold the result. */
3009 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3010 gfc_charlen_type_node, len,
3011 build_int_cst (gfc_charlen_type_node, 1));
3012 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
3014 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
3015 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
3016 else
3017 tmp = build_array_type (TREE_TYPE (type), tmp);
3019 var = gfc_create_var (tmp, "str");
3020 var = gfc_build_addr_expr (type, var);
3022 else
3024 /* Allocate a temporary to hold the result. */
3025 var = gfc_create_var (type, "pstr");
3026 gcc_assert (POINTER_TYPE_P (type));
3027 tmp = TREE_TYPE (type);
3028 if (TREE_CODE (tmp) == ARRAY_TYPE)
3029 tmp = TREE_TYPE (tmp);
3030 tmp = TYPE_SIZE_UNIT (tmp);
3031 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
3032 fold_convert (size_type_node, len),
3033 fold_convert (size_type_node, tmp));
3034 tmp = gfc_call_malloc (&se->pre, type, tmp);
3035 gfc_add_modify (&se->pre, var, tmp);
3037 /* Free the temporary afterwards. */
3038 tmp = gfc_call_free (var);
3039 gfc_add_expr_to_block (&se->post, tmp);
3042 return var;
3046 /* Handle a string concatenation operation. A temporary will be allocated to
3047 hold the result. */
3049 static void
3050 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
3052 gfc_se lse, rse;
3053 tree len, type, var, tmp, fndecl;
3055 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
3056 && expr->value.op.op2->ts.type == BT_CHARACTER);
3057 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
3059 gfc_init_se (&lse, se);
3060 gfc_conv_expr (&lse, expr->value.op.op1);
3061 gfc_conv_string_parameter (&lse);
3062 gfc_init_se (&rse, se);
3063 gfc_conv_expr (&rse, expr->value.op.op2);
3064 gfc_conv_string_parameter (&rse);
3066 gfc_add_block_to_block (&se->pre, &lse.pre);
3067 gfc_add_block_to_block (&se->pre, &rse.pre);
3069 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
3070 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3071 if (len == NULL_TREE)
3073 len = fold_build2_loc (input_location, PLUS_EXPR,
3074 TREE_TYPE (lse.string_length),
3075 lse.string_length, rse.string_length);
3078 type = build_pointer_type (type);
3080 var = gfc_conv_string_tmp (se, type, len);
3082 /* Do the actual concatenation. */
3083 if (expr->ts.kind == 1)
3084 fndecl = gfor_fndecl_concat_string;
3085 else if (expr->ts.kind == 4)
3086 fndecl = gfor_fndecl_concat_string_char4;
3087 else
3088 gcc_unreachable ();
3090 tmp = build_call_expr_loc (input_location,
3091 fndecl, 6, len, var, lse.string_length, lse.expr,
3092 rse.string_length, rse.expr);
3093 gfc_add_expr_to_block (&se->pre, tmp);
3095 /* Add the cleanup for the operands. */
3096 gfc_add_block_to_block (&se->pre, &rse.post);
3097 gfc_add_block_to_block (&se->pre, &lse.post);
3099 se->expr = var;
3100 se->string_length = len;
3103 /* Translates an op expression. Common (binary) cases are handled by this
3104 function, others are passed on. Recursion is used in either case.
3105 We use the fact that (op1.ts == op2.ts) (except for the power
3106 operator **).
3107 Operators need no special handling for scalarized expressions as long as
3108 they call gfc_conv_simple_val to get their operands.
3109 Character strings get special handling. */
3111 static void
3112 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
3114 enum tree_code code;
3115 gfc_se lse;
3116 gfc_se rse;
3117 tree tmp, type;
3118 int lop;
3119 int checkstring;
3121 checkstring = 0;
3122 lop = 0;
3123 switch (expr->value.op.op)
3125 case INTRINSIC_PARENTHESES:
3126 if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
3127 && flag_protect_parens)
3129 gfc_conv_unary_op (PAREN_EXPR, se, expr);
3130 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
3131 return;
3134 /* Fallthrough. */
3135 case INTRINSIC_UPLUS:
3136 gfc_conv_expr (se, expr->value.op.op1);
3137 return;
3139 case INTRINSIC_UMINUS:
3140 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
3141 return;
3143 case INTRINSIC_NOT:
3144 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
3145 return;
3147 case INTRINSIC_PLUS:
3148 code = PLUS_EXPR;
3149 break;
3151 case INTRINSIC_MINUS:
3152 code = MINUS_EXPR;
3153 break;
3155 case INTRINSIC_TIMES:
3156 code = MULT_EXPR;
3157 break;
3159 case INTRINSIC_DIVIDE:
3160 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
3161 an integer, we must round towards zero, so we use a
3162 TRUNC_DIV_EXPR. */
3163 if (expr->ts.type == BT_INTEGER)
3164 code = TRUNC_DIV_EXPR;
3165 else
3166 code = RDIV_EXPR;
3167 break;
3169 case INTRINSIC_POWER:
3170 gfc_conv_power_op (se, expr);
3171 return;
3173 case INTRINSIC_CONCAT:
3174 gfc_conv_concat_op (se, expr);
3175 return;
3177 case INTRINSIC_AND:
3178 code = TRUTH_ANDIF_EXPR;
3179 lop = 1;
3180 break;
3182 case INTRINSIC_OR:
3183 code = TRUTH_ORIF_EXPR;
3184 lop = 1;
3185 break;
3187 /* EQV and NEQV only work on logicals, but since we represent them
3188 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
3189 case INTRINSIC_EQ:
3190 case INTRINSIC_EQ_OS:
3191 case INTRINSIC_EQV:
3192 code = EQ_EXPR;
3193 checkstring = 1;
3194 lop = 1;
3195 break;
3197 case INTRINSIC_NE:
3198 case INTRINSIC_NE_OS:
3199 case INTRINSIC_NEQV:
3200 code = NE_EXPR;
3201 checkstring = 1;
3202 lop = 1;
3203 break;
3205 case INTRINSIC_GT:
3206 case INTRINSIC_GT_OS:
3207 code = GT_EXPR;
3208 checkstring = 1;
3209 lop = 1;
3210 break;
3212 case INTRINSIC_GE:
3213 case INTRINSIC_GE_OS:
3214 code = GE_EXPR;
3215 checkstring = 1;
3216 lop = 1;
3217 break;
3219 case INTRINSIC_LT:
3220 case INTRINSIC_LT_OS:
3221 code = LT_EXPR;
3222 checkstring = 1;
3223 lop = 1;
3224 break;
3226 case INTRINSIC_LE:
3227 case INTRINSIC_LE_OS:
3228 code = LE_EXPR;
3229 checkstring = 1;
3230 lop = 1;
3231 break;
3233 case INTRINSIC_USER:
3234 case INTRINSIC_ASSIGN:
3235 /* These should be converted into function calls by the frontend. */
3236 gcc_unreachable ();
3238 default:
3239 fatal_error (input_location, "Unknown intrinsic op");
3240 return;
3243 /* The only exception to this is **, which is handled separately anyway. */
3244 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
3246 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
3247 checkstring = 0;
3249 /* lhs */
3250 gfc_init_se (&lse, se);
3251 gfc_conv_expr (&lse, expr->value.op.op1);
3252 gfc_add_block_to_block (&se->pre, &lse.pre);
3254 /* rhs */
3255 gfc_init_se (&rse, se);
3256 gfc_conv_expr (&rse, expr->value.op.op2);
3257 gfc_add_block_to_block (&se->pre, &rse.pre);
3259 if (checkstring)
3261 gfc_conv_string_parameter (&lse);
3262 gfc_conv_string_parameter (&rse);
3264 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
3265 rse.string_length, rse.expr,
3266 expr->value.op.op1->ts.kind,
3267 code);
3268 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
3269 gfc_add_block_to_block (&lse.post, &rse.post);
3272 type = gfc_typenode_for_spec (&expr->ts);
3274 if (lop)
3276 /* The result of logical ops is always boolean_type_node. */
3277 tmp = fold_build2_loc (input_location, code, boolean_type_node,
3278 lse.expr, rse.expr);
3279 se->expr = convert (type, tmp);
3281 else
3282 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
3284 /* Add the post blocks. */
3285 gfc_add_block_to_block (&se->post, &rse.post);
3286 gfc_add_block_to_block (&se->post, &lse.post);
3289 /* If a string's length is one, we convert it to a single character. */
3291 tree
3292 gfc_string_to_single_character (tree len, tree str, int kind)
3295 if (len == NULL
3296 || !tree_fits_uhwi_p (len)
3297 || !POINTER_TYPE_P (TREE_TYPE (str)))
3298 return NULL_TREE;
3300 if (TREE_INT_CST_LOW (len) == 1)
3302 str = fold_convert (gfc_get_pchar_type (kind), str);
3303 return build_fold_indirect_ref_loc (input_location, str);
3306 if (kind == 1
3307 && TREE_CODE (str) == ADDR_EXPR
3308 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
3309 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
3310 && array_ref_low_bound (TREE_OPERAND (str, 0))
3311 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
3312 && TREE_INT_CST_LOW (len) > 1
3313 && TREE_INT_CST_LOW (len)
3314 == (unsigned HOST_WIDE_INT)
3315 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
3317 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
3318 ret = build_fold_indirect_ref_loc (input_location, ret);
3319 if (TREE_CODE (ret) == INTEGER_CST)
3321 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
3322 int i, length = TREE_STRING_LENGTH (string_cst);
3323 const char *ptr = TREE_STRING_POINTER (string_cst);
3325 for (i = 1; i < length; i++)
3326 if (ptr[i] != ' ')
3327 return NULL_TREE;
3329 return ret;
3333 return NULL_TREE;
3337 void
3338 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
3341 if (sym->backend_decl)
3343 /* This becomes the nominal_type in
3344 function.c:assign_parm_find_data_types. */
3345 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
3346 /* This becomes the passed_type in
3347 function.c:assign_parm_find_data_types. C promotes char to
3348 integer for argument passing. */
3349 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
3351 DECL_BY_REFERENCE (sym->backend_decl) = 0;
3354 if (expr != NULL)
3356 /* If we have a constant character expression, make it into an
3357 integer. */
3358 if ((*expr)->expr_type == EXPR_CONSTANT)
3360 gfc_typespec ts;
3361 gfc_clear_ts (&ts);
3363 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
3364 (int)(*expr)->value.character.string[0]);
3365 if ((*expr)->ts.kind != gfc_c_int_kind)
3367 /* The expr needs to be compatible with a C int. If the
3368 conversion fails, then the 2 causes an ICE. */
3369 ts.type = BT_INTEGER;
3370 ts.kind = gfc_c_int_kind;
3371 gfc_convert_type (*expr, &ts, 2);
3374 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
3376 if ((*expr)->ref == NULL)
3378 se->expr = gfc_string_to_single_character
3379 (build_int_cst (integer_type_node, 1),
3380 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
3381 gfc_get_symbol_decl
3382 ((*expr)->symtree->n.sym)),
3383 (*expr)->ts.kind);
3385 else
3387 gfc_conv_variable (se, *expr);
3388 se->expr = gfc_string_to_single_character
3389 (build_int_cst (integer_type_node, 1),
3390 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
3391 se->expr),
3392 (*expr)->ts.kind);
3398 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
3399 if STR is a string literal, otherwise return -1. */
3401 static int
3402 gfc_optimize_len_trim (tree len, tree str, int kind)
3404 if (kind == 1
3405 && TREE_CODE (str) == ADDR_EXPR
3406 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
3407 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
3408 && array_ref_low_bound (TREE_OPERAND (str, 0))
3409 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
3410 && tree_fits_uhwi_p (len)
3411 && tree_to_uhwi (len) >= 1
3412 && tree_to_uhwi (len)
3413 == (unsigned HOST_WIDE_INT)
3414 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
3416 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
3417 folded = build_fold_indirect_ref_loc (input_location, folded);
3418 if (TREE_CODE (folded) == INTEGER_CST)
3420 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
3421 int length = TREE_STRING_LENGTH (string_cst);
3422 const char *ptr = TREE_STRING_POINTER (string_cst);
3424 for (; length > 0; length--)
3425 if (ptr[length - 1] != ' ')
3426 break;
3428 return length;
3431 return -1;
3434 /* Helper to build a call to memcmp. */
3436 static tree
3437 build_memcmp_call (tree s1, tree s2, tree n)
3439 tree tmp;
3441 if (!POINTER_TYPE_P (TREE_TYPE (s1)))
3442 s1 = gfc_build_addr_expr (pvoid_type_node, s1);
3443 else
3444 s1 = fold_convert (pvoid_type_node, s1);
3446 if (!POINTER_TYPE_P (TREE_TYPE (s2)))
3447 s2 = gfc_build_addr_expr (pvoid_type_node, s2);
3448 else
3449 s2 = fold_convert (pvoid_type_node, s2);
3451 n = fold_convert (size_type_node, n);
3453 tmp = build_call_expr_loc (input_location,
3454 builtin_decl_explicit (BUILT_IN_MEMCMP),
3455 3, s1, s2, n);
3457 return fold_convert (integer_type_node, tmp);
3460 /* Compare two strings. If they are all single characters, the result is the
3461 subtraction of them. Otherwise, we build a library call. */
3463 tree
3464 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
3465 enum tree_code code)
3467 tree sc1;
3468 tree sc2;
3469 tree fndecl;
3471 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
3472 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
3474 sc1 = gfc_string_to_single_character (len1, str1, kind);
3475 sc2 = gfc_string_to_single_character (len2, str2, kind);
3477 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
3479 /* Deal with single character specially. */
3480 sc1 = fold_convert (integer_type_node, sc1);
3481 sc2 = fold_convert (integer_type_node, sc2);
3482 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
3483 sc1, sc2);
3486 if ((code == EQ_EXPR || code == NE_EXPR)
3487 && optimize
3488 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
3490 /* If one string is a string literal with LEN_TRIM longer
3491 than the length of the second string, the strings
3492 compare unequal. */
3493 int len = gfc_optimize_len_trim (len1, str1, kind);
3494 if (len > 0 && compare_tree_int (len2, len) < 0)
3495 return integer_one_node;
3496 len = gfc_optimize_len_trim (len2, str2, kind);
3497 if (len > 0 && compare_tree_int (len1, len) < 0)
3498 return integer_one_node;
3501 /* We can compare via memcpy if the strings are known to be equal
3502 in length and they are
3503 - kind=1
3504 - kind=4 and the comparison is for (in)equality. */
3506 if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
3507 && tree_int_cst_equal (len1, len2)
3508 && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
3510 tree tmp;
3511 tree chartype;
3513 chartype = gfc_get_char_type (kind);
3514 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
3515 fold_convert (TREE_TYPE(len1),
3516 TYPE_SIZE_UNIT(chartype)),
3517 len1);
3518 return build_memcmp_call (str1, str2, tmp);
3521 /* Build a call for the comparison. */
3522 if (kind == 1)
3523 fndecl = gfor_fndecl_compare_string;
3524 else if (kind == 4)
3525 fndecl = gfor_fndecl_compare_string_char4;
3526 else
3527 gcc_unreachable ();
3529 return build_call_expr_loc (input_location, fndecl, 4,
3530 len1, str1, len2, str2);
3534 /* Return the backend_decl for a procedure pointer component. */
3536 static tree
3537 get_proc_ptr_comp (gfc_expr *e)
3539 gfc_se comp_se;
3540 gfc_expr *e2;
3541 expr_t old_type;
3543 gfc_init_se (&comp_se, NULL);
3544 e2 = gfc_copy_expr (e);
3545 /* We have to restore the expr type later so that gfc_free_expr frees
3546 the exact same thing that was allocated.
3547 TODO: This is ugly. */
3548 old_type = e2->expr_type;
3549 e2->expr_type = EXPR_VARIABLE;
3550 gfc_conv_expr (&comp_se, e2);
3551 e2->expr_type = old_type;
3552 gfc_free_expr (e2);
3553 return build_fold_addr_expr_loc (input_location, comp_se.expr);
3557 /* Convert a typebound function reference from a class object. */
3558 static void
3559 conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
3561 gfc_ref *ref;
3562 tree var;
3564 if (TREE_CODE (base_object) != VAR_DECL)
3566 var = gfc_create_var (TREE_TYPE (base_object), NULL);
3567 gfc_add_modify (&se->pre, var, base_object);
3569 se->expr = gfc_class_vptr_get (base_object);
3570 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
3571 ref = expr->ref;
3572 while (ref && ref->next)
3573 ref = ref->next;
3574 gcc_assert (ref && ref->type == REF_COMPONENT);
3575 if (ref->u.c.sym->attr.extension)
3576 conv_parent_component_references (se, ref);
3577 gfc_conv_component_ref (se, ref);
3578 se->expr = build_fold_addr_expr_loc (input_location, se->expr);
3582 static void
3583 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
3585 tree tmp;
3587 if (gfc_is_proc_ptr_comp (expr))
3588 tmp = get_proc_ptr_comp (expr);
3589 else if (sym->attr.dummy)
3591 tmp = gfc_get_symbol_decl (sym);
3592 if (sym->attr.proc_pointer)
3593 tmp = build_fold_indirect_ref_loc (input_location,
3594 tmp);
3595 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
3596 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
3598 else
3600 if (!sym->backend_decl)
3601 sym->backend_decl = gfc_get_extern_function_decl (sym);
3603 TREE_USED (sym->backend_decl) = 1;
3605 tmp = sym->backend_decl;
3607 if (sym->attr.cray_pointee)
3609 /* TODO - make the cray pointee a pointer to a procedure,
3610 assign the pointer to it and use it for the call. This
3611 will do for now! */
3612 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
3613 gfc_get_symbol_decl (sym->cp_pointer));
3614 tmp = gfc_evaluate_now (tmp, &se->pre);
3617 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
3619 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
3620 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
3623 se->expr = tmp;
3627 /* Initialize MAPPING. */
3629 void
3630 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
3632 mapping->syms = NULL;
3633 mapping->charlens = NULL;
3637 /* Free all memory held by MAPPING (but not MAPPING itself). */
3639 void
3640 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
3642 gfc_interface_sym_mapping *sym;
3643 gfc_interface_sym_mapping *nextsym;
3644 gfc_charlen *cl;
3645 gfc_charlen *nextcl;
3647 for (sym = mapping->syms; sym; sym = nextsym)
3649 nextsym = sym->next;
3650 sym->new_sym->n.sym->formal = NULL;
3651 gfc_free_symbol (sym->new_sym->n.sym);
3652 gfc_free_expr (sym->expr);
3653 free (sym->new_sym);
3654 free (sym);
3656 for (cl = mapping->charlens; cl; cl = nextcl)
3658 nextcl = cl->next;
3659 gfc_free_expr (cl->length);
3660 free (cl);
3665 /* Return a copy of gfc_charlen CL. Add the returned structure to
3666 MAPPING so that it will be freed by gfc_free_interface_mapping. */
3668 static gfc_charlen *
3669 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
3670 gfc_charlen * cl)
3672 gfc_charlen *new_charlen;
3674 new_charlen = gfc_get_charlen ();
3675 new_charlen->next = mapping->charlens;
3676 new_charlen->length = gfc_copy_expr (cl->length);
3678 mapping->charlens = new_charlen;
3679 return new_charlen;
3683 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
3684 array variable that can be used as the actual argument for dummy
3685 argument SYM. Add any initialization code to BLOCK. PACKED is as
3686 for gfc_get_nodesc_array_type and DATA points to the first element
3687 in the passed array. */
3689 static tree
3690 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
3691 gfc_packed packed, tree data)
3693 tree type;
3694 tree var;
3696 type = gfc_typenode_for_spec (&sym->ts);
3697 type = gfc_get_nodesc_array_type (type, sym->as, packed,
3698 !sym->attr.target && !sym->attr.pointer
3699 && !sym->attr.proc_pointer);
3701 var = gfc_create_var (type, "ifm");
3702 gfc_add_modify (block, var, fold_convert (type, data));
3704 return var;
3708 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
3709 and offset of descriptorless array type TYPE given that it has the same
3710 size as DESC. Add any set-up code to BLOCK. */
3712 static void
3713 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
3715 int n;
3716 tree dim;
3717 tree offset;
3718 tree tmp;
3720 offset = gfc_index_zero_node;
3721 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
3723 dim = gfc_rank_cst[n];
3724 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
3725 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
3727 GFC_TYPE_ARRAY_LBOUND (type, n)
3728 = gfc_conv_descriptor_lbound_get (desc, dim);
3729 GFC_TYPE_ARRAY_UBOUND (type, n)
3730 = gfc_conv_descriptor_ubound_get (desc, dim);
3732 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
3734 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3735 gfc_array_index_type,
3736 gfc_conv_descriptor_ubound_get (desc, dim),
3737 gfc_conv_descriptor_lbound_get (desc, dim));
3738 tmp = fold_build2_loc (input_location, PLUS_EXPR,
3739 gfc_array_index_type,
3740 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
3741 tmp = gfc_evaluate_now (tmp, block);
3742 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
3744 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
3745 GFC_TYPE_ARRAY_LBOUND (type, n),
3746 GFC_TYPE_ARRAY_STRIDE (type, n));
3747 offset = fold_build2_loc (input_location, MINUS_EXPR,
3748 gfc_array_index_type, offset, tmp);
3750 offset = gfc_evaluate_now (offset, block);
3751 GFC_TYPE_ARRAY_OFFSET (type) = offset;
3755 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
3756 in SE. The caller may still use se->expr and se->string_length after
3757 calling this function. */
3759 void
3760 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
3761 gfc_symbol * sym, gfc_se * se,
3762 gfc_expr *expr)
3764 gfc_interface_sym_mapping *sm;
3765 tree desc;
3766 tree tmp;
3767 tree value;
3768 gfc_symbol *new_sym;
3769 gfc_symtree *root;
3770 gfc_symtree *new_symtree;
3772 /* Create a new symbol to represent the actual argument. */
3773 new_sym = gfc_new_symbol (sym->name, NULL);
3774 new_sym->ts = sym->ts;
3775 new_sym->as = gfc_copy_array_spec (sym->as);
3776 new_sym->attr.referenced = 1;
3777 new_sym->attr.dimension = sym->attr.dimension;
3778 new_sym->attr.contiguous = sym->attr.contiguous;
3779 new_sym->attr.codimension = sym->attr.codimension;
3780 new_sym->attr.pointer = sym->attr.pointer;
3781 new_sym->attr.allocatable = sym->attr.allocatable;
3782 new_sym->attr.flavor = sym->attr.flavor;
3783 new_sym->attr.function = sym->attr.function;
3785 /* Ensure that the interface is available and that
3786 descriptors are passed for array actual arguments. */
3787 if (sym->attr.flavor == FL_PROCEDURE)
3789 new_sym->formal = expr->symtree->n.sym->formal;
3790 new_sym->attr.always_explicit
3791 = expr->symtree->n.sym->attr.always_explicit;
3794 /* Create a fake symtree for it. */
3795 root = NULL;
3796 new_symtree = gfc_new_symtree (&root, sym->name);
3797 new_symtree->n.sym = new_sym;
3798 gcc_assert (new_symtree == root);
3800 /* Create a dummy->actual mapping. */
3801 sm = XCNEW (gfc_interface_sym_mapping);
3802 sm->next = mapping->syms;
3803 sm->old = sym;
3804 sm->new_sym = new_symtree;
3805 sm->expr = gfc_copy_expr (expr);
3806 mapping->syms = sm;
3808 /* Stabilize the argument's value. */
3809 if (!sym->attr.function && se)
3810 se->expr = gfc_evaluate_now (se->expr, &se->pre);
3812 if (sym->ts.type == BT_CHARACTER)
3814 /* Create a copy of the dummy argument's length. */
3815 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
3816 sm->expr->ts.u.cl = new_sym->ts.u.cl;
3818 /* If the length is specified as "*", record the length that
3819 the caller is passing. We should use the callee's length
3820 in all other cases. */
3821 if (!new_sym->ts.u.cl->length && se)
3823 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
3824 new_sym->ts.u.cl->backend_decl = se->string_length;
3828 if (!se)
3829 return;
3831 /* Use the passed value as-is if the argument is a function. */
3832 if (sym->attr.flavor == FL_PROCEDURE)
3833 value = se->expr;
3835 /* If the argument is either a string or a pointer to a string,
3836 convert it to a boundless character type. */
3837 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
3839 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
3840 tmp = build_pointer_type (tmp);
3841 if (sym->attr.pointer)
3842 value = build_fold_indirect_ref_loc (input_location,
3843 se->expr);
3844 else
3845 value = se->expr;
3846 value = fold_convert (tmp, value);
3849 /* If the argument is a scalar, a pointer to an array or an allocatable,
3850 dereference it. */
3851 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
3852 value = build_fold_indirect_ref_loc (input_location,
3853 se->expr);
3855 /* For character(*), use the actual argument's descriptor. */
3856 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
3857 value = build_fold_indirect_ref_loc (input_location,
3858 se->expr);
3860 /* If the argument is an array descriptor, use it to determine
3861 information about the actual argument's shape. */
3862 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
3863 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
3865 /* Get the actual argument's descriptor. */
3866 desc = build_fold_indirect_ref_loc (input_location,
3867 se->expr);
3869 /* Create the replacement variable. */
3870 tmp = gfc_conv_descriptor_data_get (desc);
3871 value = gfc_get_interface_mapping_array (&se->pre, sym,
3872 PACKED_NO, tmp);
3874 /* Use DESC to work out the upper bounds, strides and offset. */
3875 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
3877 else
3878 /* Otherwise we have a packed array. */
3879 value = gfc_get_interface_mapping_array (&se->pre, sym,
3880 PACKED_FULL, se->expr);
3882 new_sym->backend_decl = value;
3886 /* Called once all dummy argument mappings have been added to MAPPING,
3887 but before the mapping is used to evaluate expressions. Pre-evaluate
3888 the length of each argument, adding any initialization code to PRE and
3889 any finalization code to POST. */
3891 void
3892 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
3893 stmtblock_t * pre, stmtblock_t * post)
3895 gfc_interface_sym_mapping *sym;
3896 gfc_expr *expr;
3897 gfc_se se;
3899 for (sym = mapping->syms; sym; sym = sym->next)
3900 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
3901 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
3903 expr = sym->new_sym->n.sym->ts.u.cl->length;
3904 gfc_apply_interface_mapping_to_expr (mapping, expr);
3905 gfc_init_se (&se, NULL);
3906 gfc_conv_expr (&se, expr);
3907 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
3908 se.expr = gfc_evaluate_now (se.expr, &se.pre);
3909 gfc_add_block_to_block (pre, &se.pre);
3910 gfc_add_block_to_block (post, &se.post);
3912 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
3917 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3918 constructor C. */
3920 static void
3921 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
3922 gfc_constructor_base base)
3924 gfc_constructor *c;
3925 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
3927 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
3928 if (c->iterator)
3930 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
3931 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
3932 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
3938 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3939 reference REF. */
3941 static void
3942 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
3943 gfc_ref * ref)
3945 int n;
3947 for (; ref; ref = ref->next)
3948 switch (ref->type)
3950 case REF_ARRAY:
3951 for (n = 0; n < ref->u.ar.dimen; n++)
3953 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
3954 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
3955 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
3957 break;
3959 case REF_COMPONENT:
3960 break;
3962 case REF_SUBSTRING:
3963 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
3964 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
3965 break;
3970 /* Convert intrinsic function calls into result expressions. */
3972 static bool
3973 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
3975 gfc_symbol *sym;
3976 gfc_expr *new_expr;
3977 gfc_expr *arg1;
3978 gfc_expr *arg2;
3979 int d, dup;
3981 arg1 = expr->value.function.actual->expr;
3982 if (expr->value.function.actual->next)
3983 arg2 = expr->value.function.actual->next->expr;
3984 else
3985 arg2 = NULL;
3987 sym = arg1->symtree->n.sym;
3989 if (sym->attr.dummy)
3990 return false;
3992 new_expr = NULL;
3994 switch (expr->value.function.isym->id)
3996 case GFC_ISYM_LEN:
3997 /* TODO figure out why this condition is necessary. */
3998 if (sym->attr.function
3999 && (arg1->ts.u.cl->length == NULL
4000 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
4001 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
4002 return false;
4004 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
4005 break;
4007 case GFC_ISYM_SIZE:
4008 if (!sym->as || sym->as->rank == 0)
4009 return false;
4011 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
4013 dup = mpz_get_si (arg2->value.integer);
4014 d = dup - 1;
4016 else
4018 dup = sym->as->rank;
4019 d = 0;
4022 for (; d < dup; d++)
4024 gfc_expr *tmp;
4026 if (!sym->as->upper[d] || !sym->as->lower[d])
4028 gfc_free_expr (new_expr);
4029 return false;
4032 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
4033 gfc_get_int_expr (gfc_default_integer_kind,
4034 NULL, 1));
4035 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
4036 if (new_expr)
4037 new_expr = gfc_multiply (new_expr, tmp);
4038 else
4039 new_expr = tmp;
4041 break;
4043 case GFC_ISYM_LBOUND:
4044 case GFC_ISYM_UBOUND:
4045 /* TODO These implementations of lbound and ubound do not limit if
4046 the size < 0, according to F95's 13.14.53 and 13.14.113. */
4048 if (!sym->as || sym->as->rank == 0)
4049 return false;
4051 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
4052 d = mpz_get_si (arg2->value.integer) - 1;
4053 else
4054 /* TODO: If the need arises, this could produce an array of
4055 ubound/lbounds. */
4056 gcc_unreachable ();
4058 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
4060 if (sym->as->lower[d])
4061 new_expr = gfc_copy_expr (sym->as->lower[d]);
4063 else
4065 if (sym->as->upper[d])
4066 new_expr = gfc_copy_expr (sym->as->upper[d]);
4068 break;
4070 default:
4071 break;
4074 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
4075 if (!new_expr)
4076 return false;
4078 gfc_replace_expr (expr, new_expr);
4079 return true;
4083 static void
4084 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
4085 gfc_interface_mapping * mapping)
4087 gfc_formal_arglist *f;
4088 gfc_actual_arglist *actual;
4090 actual = expr->value.function.actual;
4091 f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
4093 for (; f && actual; f = f->next, actual = actual->next)
4095 if (!actual->expr)
4096 continue;
4098 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
4101 if (map_expr->symtree->n.sym->attr.dimension)
4103 int d;
4104 gfc_array_spec *as;
4106 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
4108 for (d = 0; d < as->rank; d++)
4110 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
4111 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
4114 expr->value.function.esym->as = as;
4117 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
4119 expr->value.function.esym->ts.u.cl->length
4120 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
4122 gfc_apply_interface_mapping_to_expr (mapping,
4123 expr->value.function.esym->ts.u.cl->length);
4128 /* EXPR is a copy of an expression that appeared in the interface
4129 associated with MAPPING. Walk it recursively looking for references to
4130 dummy arguments that MAPPING maps to actual arguments. Replace each such
4131 reference with a reference to the associated actual argument. */
4133 static void
4134 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
4135 gfc_expr * expr)
4137 gfc_interface_sym_mapping *sym;
4138 gfc_actual_arglist *actual;
4140 if (!expr)
4141 return;
4143 /* Copying an expression does not copy its length, so do that here. */
4144 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
4146 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
4147 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
4150 /* Apply the mapping to any references. */
4151 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
4153 /* ...and to the expression's symbol, if it has one. */
4154 /* TODO Find out why the condition on expr->symtree had to be moved into
4155 the loop rather than being outside it, as originally. */
4156 for (sym = mapping->syms; sym; sym = sym->next)
4157 if (expr->symtree && sym->old == expr->symtree->n.sym)
4159 if (sym->new_sym->n.sym->backend_decl)
4160 expr->symtree = sym->new_sym;
4161 else if (sym->expr)
4162 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
4165 /* ...and to subexpressions in expr->value. */
4166 switch (expr->expr_type)
4168 case EXPR_VARIABLE:
4169 case EXPR_CONSTANT:
4170 case EXPR_NULL:
4171 case EXPR_SUBSTRING:
4172 break;
4174 case EXPR_OP:
4175 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
4176 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
4177 break;
4179 case EXPR_FUNCTION:
4180 for (actual = expr->value.function.actual; actual; actual = actual->next)
4181 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
4183 if (expr->value.function.esym == NULL
4184 && expr->value.function.isym != NULL
4185 && expr->value.function.actual->expr->symtree
4186 && gfc_map_intrinsic_function (expr, mapping))
4187 break;
4189 for (sym = mapping->syms; sym; sym = sym->next)
4190 if (sym->old == expr->value.function.esym)
4192 expr->value.function.esym = sym->new_sym->n.sym;
4193 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
4194 expr->value.function.esym->result = sym->new_sym->n.sym;
4196 break;
4198 case EXPR_ARRAY:
4199 case EXPR_STRUCTURE:
4200 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
4201 break;
4203 case EXPR_COMPCALL:
4204 case EXPR_PPC:
4205 gcc_unreachable ();
4206 break;
4209 return;
4213 /* Evaluate interface expression EXPR using MAPPING. Store the result
4214 in SE. */
4216 void
4217 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
4218 gfc_se * se, gfc_expr * expr)
4220 expr = gfc_copy_expr (expr);
4221 gfc_apply_interface_mapping_to_expr (mapping, expr);
4222 gfc_conv_expr (se, expr);
4223 se->expr = gfc_evaluate_now (se->expr, &se->pre);
4224 gfc_free_expr (expr);
4228 /* Returns a reference to a temporary array into which a component of
4229 an actual argument derived type array is copied and then returned
4230 after the function call. */
4231 void
4232 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
4233 sym_intent intent, bool formal_ptr)
4235 gfc_se lse;
4236 gfc_se rse;
4237 gfc_ss *lss;
4238 gfc_ss *rss;
4239 gfc_loopinfo loop;
4240 gfc_loopinfo loop2;
4241 gfc_array_info *info;
4242 tree offset;
4243 tree tmp_index;
4244 tree tmp;
4245 tree base_type;
4246 tree size;
4247 stmtblock_t body;
4248 int n;
4249 int dimen;
4251 gfc_init_se (&lse, NULL);
4252 gfc_init_se (&rse, NULL);
4254 /* Walk the argument expression. */
4255 rss = gfc_walk_expr (expr);
4257 gcc_assert (rss != gfc_ss_terminator);
4259 /* Initialize the scalarizer. */
4260 gfc_init_loopinfo (&loop);
4261 gfc_add_ss_to_loop (&loop, rss);
4263 /* Calculate the bounds of the scalarization. */
4264 gfc_conv_ss_startstride (&loop);
4266 /* Build an ss for the temporary. */
4267 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
4268 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
4270 base_type = gfc_typenode_for_spec (&expr->ts);
4271 if (GFC_ARRAY_TYPE_P (base_type)
4272 || GFC_DESCRIPTOR_TYPE_P (base_type))
4273 base_type = gfc_get_element_type (base_type);
4275 if (expr->ts.type == BT_CLASS)
4276 base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
4278 loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
4279 ? expr->ts.u.cl->backend_decl
4280 : NULL),
4281 loop.dimen);
4283 parmse->string_length = loop.temp_ss->info->string_length;
4285 /* Associate the SS with the loop. */
4286 gfc_add_ss_to_loop (&loop, loop.temp_ss);
4288 /* Setup the scalarizing loops. */
4289 gfc_conv_loop_setup (&loop, &expr->where);
4291 /* Pass the temporary descriptor back to the caller. */
4292 info = &loop.temp_ss->info->data.array;
4293 parmse->expr = info->descriptor;
4295 /* Setup the gfc_se structures. */
4296 gfc_copy_loopinfo_to_se (&lse, &loop);
4297 gfc_copy_loopinfo_to_se (&rse, &loop);
4299 rse.ss = rss;
4300 lse.ss = loop.temp_ss;
4301 gfc_mark_ss_chain_used (rss, 1);
4302 gfc_mark_ss_chain_used (loop.temp_ss, 1);
4304 /* Start the scalarized loop body. */
4305 gfc_start_scalarized_body (&loop, &body);
4307 /* Translate the expression. */
4308 gfc_conv_expr (&rse, expr);
4310 /* Reset the offset for the function call since the loop
4311 is zero based on the data pointer. Note that the temp
4312 comes first in the loop chain since it is added second. */
4313 if (gfc_is_alloc_class_array_function (expr))
4315 tmp = loop.ss->loop_chain->info->data.array.descriptor;
4316 gfc_conv_descriptor_offset_set (&loop.pre, tmp,
4317 gfc_index_zero_node);
4320 gfc_conv_tmp_array_ref (&lse);
4322 if (intent != INTENT_OUT)
4324 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
4325 gfc_add_expr_to_block (&body, tmp);
4326 gcc_assert (rse.ss == gfc_ss_terminator);
4327 gfc_trans_scalarizing_loops (&loop, &body);
4329 else
4331 /* Make sure that the temporary declaration survives by merging
4332 all the loop declarations into the current context. */
4333 for (n = 0; n < loop.dimen; n++)
4335 gfc_merge_block_scope (&body);
4336 body = loop.code[loop.order[n]];
4338 gfc_merge_block_scope (&body);
4341 /* Add the post block after the second loop, so that any
4342 freeing of allocated memory is done at the right time. */
4343 gfc_add_block_to_block (&parmse->pre, &loop.pre);
4345 /**********Copy the temporary back again.*********/
4347 gfc_init_se (&lse, NULL);
4348 gfc_init_se (&rse, NULL);
4350 /* Walk the argument expression. */
4351 lss = gfc_walk_expr (expr);
4352 rse.ss = loop.temp_ss;
4353 lse.ss = lss;
4355 /* Initialize the scalarizer. */
4356 gfc_init_loopinfo (&loop2);
4357 gfc_add_ss_to_loop (&loop2, lss);
4359 dimen = rse.ss->dimen;
4361 /* Skip the write-out loop for this case. */
4362 if (gfc_is_alloc_class_array_function (expr))
4363 goto class_array_fcn;
4365 /* Calculate the bounds of the scalarization. */
4366 gfc_conv_ss_startstride (&loop2);
4368 /* Setup the scalarizing loops. */
4369 gfc_conv_loop_setup (&loop2, &expr->where);
4371 gfc_copy_loopinfo_to_se (&lse, &loop2);
4372 gfc_copy_loopinfo_to_se (&rse, &loop2);
4374 gfc_mark_ss_chain_used (lss, 1);
4375 gfc_mark_ss_chain_used (loop.temp_ss, 1);
4377 /* Declare the variable to hold the temporary offset and start the
4378 scalarized loop body. */
4379 offset = gfc_create_var (gfc_array_index_type, NULL);
4380 gfc_start_scalarized_body (&loop2, &body);
4382 /* Build the offsets for the temporary from the loop variables. The
4383 temporary array has lbounds of zero and strides of one in all
4384 dimensions, so this is very simple. The offset is only computed
4385 outside the innermost loop, so the overall transfer could be
4386 optimized further. */
4387 info = &rse.ss->info->data.array;
4389 tmp_index = gfc_index_zero_node;
4390 for (n = dimen - 1; n > 0; n--)
4392 tree tmp_str;
4393 tmp = rse.loop->loopvar[n];
4394 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4395 tmp, rse.loop->from[n]);
4396 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4397 tmp, tmp_index);
4399 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
4400 gfc_array_index_type,
4401 rse.loop->to[n-1], rse.loop->from[n-1]);
4402 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
4403 gfc_array_index_type,
4404 tmp_str, gfc_index_one_node);
4406 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
4407 gfc_array_index_type, tmp, tmp_str);
4410 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
4411 gfc_array_index_type,
4412 tmp_index, rse.loop->from[0]);
4413 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
4415 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
4416 gfc_array_index_type,
4417 rse.loop->loopvar[0], offset);
4419 /* Now use the offset for the reference. */
4420 tmp = build_fold_indirect_ref_loc (input_location,
4421 info->data);
4422 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
4424 if (expr->ts.type == BT_CHARACTER)
4425 rse.string_length = expr->ts.u.cl->backend_decl;
4427 gfc_conv_expr (&lse, expr);
4429 gcc_assert (lse.ss == gfc_ss_terminator);
4431 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, true);
4432 gfc_add_expr_to_block (&body, tmp);
4434 /* Generate the copying loops. */
4435 gfc_trans_scalarizing_loops (&loop2, &body);
4437 /* Wrap the whole thing up by adding the second loop to the post-block
4438 and following it by the post-block of the first loop. In this way,
4439 if the temporary needs freeing, it is done after use! */
4440 if (intent != INTENT_IN)
4442 gfc_add_block_to_block (&parmse->post, &loop2.pre);
4443 gfc_add_block_to_block (&parmse->post, &loop2.post);
4446 class_array_fcn:
4448 gfc_add_block_to_block (&parmse->post, &loop.post);
4450 gfc_cleanup_loop (&loop);
4451 gfc_cleanup_loop (&loop2);
4453 /* Pass the string length to the argument expression. */
4454 if (expr->ts.type == BT_CHARACTER)
4455 parmse->string_length = expr->ts.u.cl->backend_decl;
4457 /* Determine the offset for pointer formal arguments and set the
4458 lbounds to one. */
4459 if (formal_ptr)
4461 size = gfc_index_one_node;
4462 offset = gfc_index_zero_node;
4463 for (n = 0; n < dimen; n++)
4465 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
4466 gfc_rank_cst[n]);
4467 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4468 gfc_array_index_type, tmp,
4469 gfc_index_one_node);
4470 gfc_conv_descriptor_ubound_set (&parmse->pre,
4471 parmse->expr,
4472 gfc_rank_cst[n],
4473 tmp);
4474 gfc_conv_descriptor_lbound_set (&parmse->pre,
4475 parmse->expr,
4476 gfc_rank_cst[n],
4477 gfc_index_one_node);
4478 size = gfc_evaluate_now (size, &parmse->pre);
4479 offset = fold_build2_loc (input_location, MINUS_EXPR,
4480 gfc_array_index_type,
4481 offset, size);
4482 offset = gfc_evaluate_now (offset, &parmse->pre);
4483 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4484 gfc_array_index_type,
4485 rse.loop->to[n], rse.loop->from[n]);
4486 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4487 gfc_array_index_type,
4488 tmp, gfc_index_one_node);
4489 size = fold_build2_loc (input_location, MULT_EXPR,
4490 gfc_array_index_type, size, tmp);
4493 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
4494 offset);
4497 /* We want either the address for the data or the address of the descriptor,
4498 depending on the mode of passing array arguments. */
4499 if (g77)
4500 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
4501 else
4502 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
4504 return;
4508 /* Generate the code for argument list functions. */
4510 static void
4511 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
4513 /* Pass by value for g77 %VAL(arg), pass the address
4514 indirectly for %LOC, else by reference. Thus %REF
4515 is a "do-nothing" and %LOC is the same as an F95
4516 pointer. */
4517 if (strncmp (name, "%VAL", 4) == 0)
4518 gfc_conv_expr (se, expr);
4519 else if (strncmp (name, "%LOC", 4) == 0)
4521 gfc_conv_expr_reference (se, expr);
4522 se->expr = gfc_build_addr_expr (NULL, se->expr);
4524 else if (strncmp (name, "%REF", 4) == 0)
4525 gfc_conv_expr_reference (se, expr);
4526 else
4527 gfc_error ("Unknown argument list function at %L", &expr->where);
4531 /* This function tells whether the middle-end representation of the expression
4532 E given as input may point to data otherwise accessible through a variable
4533 (sub-)reference.
4534 It is assumed that the only expressions that may alias are variables,
4535 and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
4536 may alias.
4537 This function is used to decide whether freeing an expression's allocatable
4538 components is safe or should be avoided.
4540 If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
4541 its elements are copied from a variable. This ARRAY_MAY_ALIAS trick
4542 is necessary because for array constructors, aliasing depends on how
4543 the array is used:
4544 - If E is an array constructor used as argument to an elemental procedure,
4545 the array, which is generated through shallow copy by the scalarizer,
4546 is used directly and can alias the expressions it was copied from.
4547 - If E is an array constructor used as argument to a non-elemental
4548 procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
4549 the array as in the previous case, but then that array is used
4550 to initialize a new descriptor through deep copy. There is no alias
4551 possible in that case.
4552 Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
4553 above. */
4555 static bool
4556 expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
4558 gfc_constructor *c;
4560 if (e->expr_type == EXPR_VARIABLE)
4561 return true;
4562 else if (e->expr_type == EXPR_FUNCTION)
4564 gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
4566 if ((proc_ifc->result->ts.type == BT_CLASS
4567 && proc_ifc->result->ts.u.derived->attr.is_class
4568 && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
4569 || proc_ifc->result->attr.pointer)
4570 return true;
4571 else
4572 return false;
4574 else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
4575 return false;
4577 for (c = gfc_constructor_first (e->value.constructor);
4578 c; c = gfc_constructor_next (c))
4579 if (c->expr
4580 && expr_may_alias_variables (c->expr, array_may_alias))
4581 return true;
4583 return false;
4587 /* Generate code for a procedure call. Note can return se->post != NULL.
4588 If se->direct_byref is set then se->expr contains the return parameter.
4589 Return nonzero, if the call has alternate specifiers.
4590 'expr' is only needed for procedure pointer components. */
4593 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
4594 gfc_actual_arglist * args, gfc_expr * expr,
4595 vec<tree, va_gc> *append_args)
4597 gfc_interface_mapping mapping;
4598 vec<tree, va_gc> *arglist;
4599 vec<tree, va_gc> *retargs;
4600 tree tmp;
4601 tree fntype;
4602 gfc_se parmse;
4603 gfc_array_info *info;
4604 int byref;
4605 int parm_kind;
4606 tree type;
4607 tree var;
4608 tree len;
4609 tree base_object;
4610 vec<tree, va_gc> *stringargs;
4611 vec<tree, va_gc> *optionalargs;
4612 tree result = NULL;
4613 gfc_formal_arglist *formal;
4614 gfc_actual_arglist *arg;
4615 int has_alternate_specifier = 0;
4616 bool need_interface_mapping;
4617 bool callee_alloc;
4618 bool ulim_copy;
4619 gfc_typespec ts;
4620 gfc_charlen cl;
4621 gfc_expr *e;
4622 gfc_symbol *fsym;
4623 stmtblock_t post;
4624 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
4625 gfc_component *comp = NULL;
4626 int arglen;
4627 unsigned int argc;
4629 arglist = NULL;
4630 retargs = NULL;
4631 stringargs = NULL;
4632 optionalargs = NULL;
4633 var = NULL_TREE;
4634 len = NULL_TREE;
4635 gfc_clear_ts (&ts);
4637 comp = gfc_get_proc_ptr_comp (expr);
4639 bool elemental_proc = (comp
4640 && comp->ts.interface
4641 && comp->ts.interface->attr.elemental)
4642 || (comp && comp->attr.elemental)
4643 || sym->attr.elemental;
4645 if (se->ss != NULL)
4647 if (!elemental_proc)
4649 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
4650 if (se->ss->info->useflags)
4652 gcc_assert ((!comp && gfc_return_by_reference (sym)
4653 && sym->result->attr.dimension)
4654 || (comp && comp->attr.dimension)
4655 || gfc_is_alloc_class_array_function (expr));
4656 gcc_assert (se->loop != NULL);
4657 /* Access the previously obtained result. */
4658 gfc_conv_tmp_array_ref (se);
4659 return 0;
4662 info = &se->ss->info->data.array;
4664 else
4665 info = NULL;
4667 gfc_init_block (&post);
4668 gfc_init_interface_mapping (&mapping);
4669 if (!comp)
4671 formal = gfc_sym_get_dummy_args (sym);
4672 need_interface_mapping = sym->attr.dimension ||
4673 (sym->ts.type == BT_CHARACTER
4674 && sym->ts.u.cl->length
4675 && sym->ts.u.cl->length->expr_type
4676 != EXPR_CONSTANT);
4678 else
4680 formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
4681 need_interface_mapping = comp->attr.dimension ||
4682 (comp->ts.type == BT_CHARACTER
4683 && comp->ts.u.cl->length
4684 && comp->ts.u.cl->length->expr_type
4685 != EXPR_CONSTANT);
4688 base_object = NULL_TREE;
4689 /* For _vprt->_copy () routines no formal symbol is present. Nevertheless
4690 is the third and fourth argument to such a function call a value
4691 denoting the number of elements to copy (i.e., most of the time the
4692 length of a deferred length string). */
4693 ulim_copy = formal == NULL && UNLIMITED_POLY (sym)
4694 && strcmp ("_copy", comp->name) == 0;
4696 /* Evaluate the arguments. */
4697 for (arg = args, argc = 0; arg != NULL;
4698 arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
4700 e = arg->expr;
4701 fsym = formal ? formal->sym : NULL;
4702 parm_kind = MISSING;
4704 /* If the procedure requires an explicit interface, the actual
4705 argument is passed according to the corresponding formal
4706 argument. If the corresponding formal argument is a POINTER,
4707 ALLOCATABLE or assumed shape, we do not use g77's calling
4708 convention, and pass the address of the array descriptor
4709 instead. Otherwise we use g77's calling convention, in other words
4710 pass the array data pointer without descriptor. */
4711 bool nodesc_arg = fsym != NULL
4712 && !(fsym->attr.pointer || fsym->attr.allocatable)
4713 && fsym->as
4714 && fsym->as->type != AS_ASSUMED_SHAPE
4715 && fsym->as->type != AS_ASSUMED_RANK;
4716 if (comp)
4717 nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
4718 else
4719 nodesc_arg = nodesc_arg || !sym->attr.always_explicit;
4721 /* Class array expressions are sometimes coming completely unadorned
4722 with either arrayspec or _data component. Correct that here.
4723 OOP-TODO: Move this to the frontend. */
4724 if (e && e->expr_type == EXPR_VARIABLE
4725 && !e->ref
4726 && e->ts.type == BT_CLASS
4727 && (CLASS_DATA (e)->attr.codimension
4728 || CLASS_DATA (e)->attr.dimension))
4730 gfc_typespec temp_ts = e->ts;
4731 gfc_add_class_array_ref (e);
4732 e->ts = temp_ts;
4735 if (e == NULL)
4737 if (se->ignore_optional)
4739 /* Some intrinsics have already been resolved to the correct
4740 parameters. */
4741 continue;
4743 else if (arg->label)
4745 has_alternate_specifier = 1;
4746 continue;
4748 else
4750 gfc_init_se (&parmse, NULL);
4752 /* For scalar arguments with VALUE attribute which are passed by
4753 value, pass "0" and a hidden argument gives the optional
4754 status. */
4755 if (fsym && fsym->attr.optional && fsym->attr.value
4756 && !fsym->attr.dimension && fsym->ts.type != BT_CHARACTER
4757 && fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED)
4759 parmse.expr = fold_convert (gfc_sym_type (fsym),
4760 integer_zero_node);
4761 vec_safe_push (optionalargs, boolean_false_node);
4763 else
4765 /* Pass a NULL pointer for an absent arg. */
4766 parmse.expr = null_pointer_node;
4767 if (arg->missing_arg_type == BT_CHARACTER)
4768 parmse.string_length = build_int_cst (gfc_charlen_type_node,
4773 else if (arg->expr->expr_type == EXPR_NULL
4774 && fsym && !fsym->attr.pointer
4775 && (fsym->ts.type != BT_CLASS
4776 || !CLASS_DATA (fsym)->attr.class_pointer))
4778 /* Pass a NULL pointer to denote an absent arg. */
4779 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
4780 && (fsym->ts.type != BT_CLASS
4781 || !CLASS_DATA (fsym)->attr.allocatable));
4782 gfc_init_se (&parmse, NULL);
4783 parmse.expr = null_pointer_node;
4784 if (arg->missing_arg_type == BT_CHARACTER)
4785 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
4787 else if (fsym && fsym->ts.type == BT_CLASS
4788 && e->ts.type == BT_DERIVED)
4790 /* The derived type needs to be converted to a temporary
4791 CLASS object. */
4792 gfc_init_se (&parmse, se);
4793 gfc_conv_derived_to_class (&parmse, e, fsym->ts, NULL,
4794 fsym->attr.optional
4795 && e->expr_type == EXPR_VARIABLE
4796 && e->symtree->n.sym->attr.optional,
4797 CLASS_DATA (fsym)->attr.class_pointer
4798 || CLASS_DATA (fsym)->attr.allocatable);
4800 else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS)
4802 /* The intrinsic type needs to be converted to a temporary
4803 CLASS object for the unlimited polymorphic formal. */
4804 gfc_init_se (&parmse, se);
4805 gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
4807 else if (se->ss && se->ss->info->useflags)
4809 gfc_ss *ss;
4811 ss = se->ss;
4813 /* An elemental function inside a scalarized loop. */
4814 gfc_init_se (&parmse, se);
4815 parm_kind = ELEMENTAL;
4817 /* When no fsym is present, ulim_copy is set and this is a third or
4818 fourth argument, use call-by-value instead of by reference to
4819 hand the length properties to the copy routine (i.e., most of the
4820 time this will be a call to a __copy_character_* routine where the
4821 third and fourth arguments are the lengths of a deferred length
4822 char array). */
4823 if ((fsym && fsym->attr.value)
4824 || (ulim_copy && (argc == 2 || argc == 3)))
4825 gfc_conv_expr (&parmse, e);
4826 else
4827 gfc_conv_expr_reference (&parmse, e);
4829 if (e->ts.type == BT_CHARACTER && !e->rank
4830 && e->expr_type == EXPR_FUNCTION)
4831 parmse.expr = build_fold_indirect_ref_loc (input_location,
4832 parmse.expr);
4834 if (fsym && fsym->ts.type == BT_DERIVED
4835 && gfc_is_class_container_ref (e))
4837 parmse.expr = gfc_class_data_get (parmse.expr);
4839 if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
4840 && e->symtree->n.sym->attr.optional)
4842 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
4843 parmse.expr = build3_loc (input_location, COND_EXPR,
4844 TREE_TYPE (parmse.expr),
4845 cond, parmse.expr,
4846 fold_convert (TREE_TYPE (parmse.expr),
4847 null_pointer_node));
4851 /* If we are passing an absent array as optional dummy to an
4852 elemental procedure, make sure that we pass NULL when the data
4853 pointer is NULL. We need this extra conditional because of
4854 scalarization which passes arrays elements to the procedure,
4855 ignoring the fact that the array can be absent/unallocated/... */
4856 if (ss->info->can_be_null_ref && ss->info->type != GFC_SS_REFERENCE)
4858 tree descriptor_data;
4860 descriptor_data = ss->info->data.array.data;
4861 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
4862 descriptor_data,
4863 fold_convert (TREE_TYPE (descriptor_data),
4864 null_pointer_node));
4865 parmse.expr
4866 = fold_build3_loc (input_location, COND_EXPR,
4867 TREE_TYPE (parmse.expr),
4868 gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
4869 fold_convert (TREE_TYPE (parmse.expr),
4870 null_pointer_node),
4871 parmse.expr);
4874 /* The scalarizer does not repackage the reference to a class
4875 array - instead it returns a pointer to the data element. */
4876 if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
4877 gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
4878 fsym->attr.intent != INTENT_IN
4879 && (CLASS_DATA (fsym)->attr.class_pointer
4880 || CLASS_DATA (fsym)->attr.allocatable),
4881 fsym->attr.optional
4882 && e->expr_type == EXPR_VARIABLE
4883 && e->symtree->n.sym->attr.optional,
4884 CLASS_DATA (fsym)->attr.class_pointer
4885 || CLASS_DATA (fsym)->attr.allocatable);
4887 else
4889 bool scalar;
4890 gfc_ss *argss;
4892 gfc_init_se (&parmse, NULL);
4894 /* Check whether the expression is a scalar or not; we cannot use
4895 e->rank as it can be nonzero for functions arguments. */
4896 argss = gfc_walk_expr (e);
4897 scalar = argss == gfc_ss_terminator;
4898 if (!scalar)
4899 gfc_free_ss_chain (argss);
4901 /* Special handling for passing scalar polymorphic coarrays;
4902 otherwise one passes "class->_data.data" instead of "&class". */
4903 if (e->rank == 0 && e->ts.type == BT_CLASS
4904 && fsym && fsym->ts.type == BT_CLASS
4905 && CLASS_DATA (fsym)->attr.codimension
4906 && !CLASS_DATA (fsym)->attr.dimension)
4908 gfc_add_class_array_ref (e);
4909 parmse.want_coarray = 1;
4910 scalar = false;
4913 /* A scalar or transformational function. */
4914 if (scalar)
4916 if (e->expr_type == EXPR_VARIABLE
4917 && e->symtree->n.sym->attr.cray_pointee
4918 && fsym && fsym->attr.flavor == FL_PROCEDURE)
4920 /* The Cray pointer needs to be converted to a pointer to
4921 a type given by the expression. */
4922 gfc_conv_expr (&parmse, e);
4923 type = build_pointer_type (TREE_TYPE (parmse.expr));
4924 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
4925 parmse.expr = convert (type, tmp);
4927 else if (fsym && fsym->attr.value)
4929 if (fsym->ts.type == BT_CHARACTER
4930 && fsym->ts.is_c_interop
4931 && fsym->ns->proc_name != NULL
4932 && fsym->ns->proc_name->attr.is_bind_c)
4934 parmse.expr = NULL;
4935 gfc_conv_scalar_char_value (fsym, &parmse, &e);
4936 if (parmse.expr == NULL)
4937 gfc_conv_expr (&parmse, e);
4939 else
4941 gfc_conv_expr (&parmse, e);
4942 if (fsym->attr.optional
4943 && fsym->ts.type != BT_CLASS
4944 && fsym->ts.type != BT_DERIVED)
4946 if (e->expr_type != EXPR_VARIABLE
4947 || !e->symtree->n.sym->attr.optional
4948 || e->ref != NULL)
4949 vec_safe_push (optionalargs, boolean_true_node);
4950 else
4952 tmp = gfc_conv_expr_present (e->symtree->n.sym);
4953 if (!e->symtree->n.sym->attr.value)
4954 parmse.expr
4955 = fold_build3_loc (input_location, COND_EXPR,
4956 TREE_TYPE (parmse.expr),
4957 tmp, parmse.expr,
4958 fold_convert (TREE_TYPE (parmse.expr),
4959 integer_zero_node));
4961 vec_safe_push (optionalargs, tmp);
4966 else if (arg->name && arg->name[0] == '%')
4967 /* Argument list functions %VAL, %LOC and %REF are signalled
4968 through arg->name. */
4969 conv_arglist_function (&parmse, arg->expr, arg->name);
4970 else if ((e->expr_type == EXPR_FUNCTION)
4971 && ((e->value.function.esym
4972 && e->value.function.esym->result->attr.pointer)
4973 || (!e->value.function.esym
4974 && e->symtree->n.sym->attr.pointer))
4975 && fsym && fsym->attr.target)
4977 gfc_conv_expr (&parmse, e);
4978 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4980 else if (e->expr_type == EXPR_FUNCTION
4981 && e->symtree->n.sym->result
4982 && e->symtree->n.sym->result != e->symtree->n.sym
4983 && e->symtree->n.sym->result->attr.proc_pointer)
4985 /* Functions returning procedure pointers. */
4986 gfc_conv_expr (&parmse, e);
4987 if (fsym && fsym->attr.proc_pointer)
4988 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4990 else
4992 if (e->ts.type == BT_CLASS && fsym
4993 && fsym->ts.type == BT_CLASS
4994 && (!CLASS_DATA (fsym)->as
4995 || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
4996 && CLASS_DATA (e)->attr.codimension)
4998 gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
4999 gcc_assert (!CLASS_DATA (fsym)->as);
5000 gfc_add_class_array_ref (e);
5001 parmse.want_coarray = 1;
5002 gfc_conv_expr_reference (&parmse, e);
5003 class_scalar_coarray_to_class (&parmse, e, fsym->ts,
5004 fsym->attr.optional
5005 && e->expr_type == EXPR_VARIABLE);
5007 else if (e->ts.type == BT_CLASS && fsym
5008 && fsym->ts.type == BT_CLASS
5009 && !CLASS_DATA (fsym)->as
5010 && !CLASS_DATA (e)->as
5011 && strcmp (fsym->ts.u.derived->name,
5012 e->ts.u.derived->name))
5014 type = gfc_typenode_for_spec (&fsym->ts);
5015 var = gfc_create_var (type, fsym->name);
5016 gfc_conv_expr (&parmse, e);
5017 if (fsym->attr.optional
5018 && e->expr_type == EXPR_VARIABLE
5019 && e->symtree->n.sym->attr.optional)
5021 stmtblock_t block;
5022 tree cond;
5023 tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5024 cond = fold_build2_loc (input_location, NE_EXPR,
5025 boolean_type_node, tmp,
5026 fold_convert (TREE_TYPE (tmp),
5027 null_pointer_node));
5028 gfc_start_block (&block);
5029 gfc_add_modify (&block, var,
5030 fold_build1_loc (input_location,
5031 VIEW_CONVERT_EXPR,
5032 type, parmse.expr));
5033 gfc_add_expr_to_block (&parmse.pre,
5034 fold_build3_loc (input_location,
5035 COND_EXPR, void_type_node,
5036 cond, gfc_finish_block (&block),
5037 build_empty_stmt (input_location)));
5038 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
5039 parmse.expr = build3_loc (input_location, COND_EXPR,
5040 TREE_TYPE (parmse.expr),
5041 cond, parmse.expr,
5042 fold_convert (TREE_TYPE (parmse.expr),
5043 null_pointer_node));
5045 else
5047 gfc_add_modify (&parmse.pre, var,
5048 fold_build1_loc (input_location,
5049 VIEW_CONVERT_EXPR,
5050 type, parmse.expr));
5051 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
5054 else
5055 gfc_conv_expr_reference (&parmse, e);
5057 /* Catch base objects that are not variables. */
5058 if (e->ts.type == BT_CLASS
5059 && e->expr_type != EXPR_VARIABLE
5060 && expr && e == expr->base_expr)
5061 base_object = build_fold_indirect_ref_loc (input_location,
5062 parmse.expr);
5064 /* A class array element needs converting back to be a
5065 class object, if the formal argument is a class object. */
5066 if (fsym && fsym->ts.type == BT_CLASS
5067 && e->ts.type == BT_CLASS
5068 && ((CLASS_DATA (fsym)->as
5069 && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
5070 || CLASS_DATA (e)->attr.dimension))
5071 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
5072 fsym->attr.intent != INTENT_IN
5073 && (CLASS_DATA (fsym)->attr.class_pointer
5074 || CLASS_DATA (fsym)->attr.allocatable),
5075 fsym->attr.optional
5076 && e->expr_type == EXPR_VARIABLE
5077 && e->symtree->n.sym->attr.optional,
5078 CLASS_DATA (fsym)->attr.class_pointer
5079 || CLASS_DATA (fsym)->attr.allocatable);
5081 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5082 allocated on entry, it must be deallocated. */
5083 if (fsym && fsym->attr.intent == INTENT_OUT
5084 && (fsym->attr.allocatable
5085 || (fsym->ts.type == BT_CLASS
5086 && CLASS_DATA (fsym)->attr.allocatable)))
5088 stmtblock_t block;
5089 tree ptr;
5091 gfc_init_block (&block);
5092 ptr = parmse.expr;
5093 if (e->ts.type == BT_CLASS)
5094 ptr = gfc_class_data_get (ptr);
5096 tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
5097 true, e, e->ts);
5098 gfc_add_expr_to_block (&block, tmp);
5099 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5100 void_type_node, ptr,
5101 null_pointer_node);
5102 gfc_add_expr_to_block (&block, tmp);
5104 if (fsym->ts.type == BT_CLASS && UNLIMITED_POLY (fsym))
5106 gfc_add_modify (&block, ptr,
5107 fold_convert (TREE_TYPE (ptr),
5108 null_pointer_node));
5109 gfc_add_expr_to_block (&block, tmp);
5111 else if (fsym->ts.type == BT_CLASS)
5113 gfc_symbol *vtab;
5114 vtab = gfc_find_derived_vtab (fsym->ts.u.derived);
5115 tmp = gfc_get_symbol_decl (vtab);
5116 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5117 ptr = gfc_class_vptr_get (parmse.expr);
5118 gfc_add_modify (&block, ptr,
5119 fold_convert (TREE_TYPE (ptr), tmp));
5120 gfc_add_expr_to_block (&block, tmp);
5123 if (fsym->attr.optional
5124 && e->expr_type == EXPR_VARIABLE
5125 && e->symtree->n.sym->attr.optional)
5127 tmp = fold_build3_loc (input_location, COND_EXPR,
5128 void_type_node,
5129 gfc_conv_expr_present (e->symtree->n.sym),
5130 gfc_finish_block (&block),
5131 build_empty_stmt (input_location));
5133 else
5134 tmp = gfc_finish_block (&block);
5136 gfc_add_expr_to_block (&se->pre, tmp);
5139 if (fsym && (fsym->ts.type == BT_DERIVED
5140 || fsym->ts.type == BT_ASSUMED)
5141 && e->ts.type == BT_CLASS
5142 && !CLASS_DATA (e)->attr.dimension
5143 && !CLASS_DATA (e)->attr.codimension)
5144 parmse.expr = gfc_class_data_get (parmse.expr);
5146 /* Wrap scalar variable in a descriptor. We need to convert
5147 the address of a pointer back to the pointer itself before,
5148 we can assign it to the data field. */
5150 if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
5151 && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
5153 tmp = parmse.expr;
5154 if (TREE_CODE (tmp) == ADDR_EXPR
5155 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp, 0))))
5156 tmp = TREE_OPERAND (tmp, 0);
5157 parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
5158 fsym->attr);
5159 parmse.expr = gfc_build_addr_expr (NULL_TREE,
5160 parmse.expr);
5162 else if (fsym && e->expr_type != EXPR_NULL
5163 && ((fsym->attr.pointer
5164 && fsym->attr.flavor != FL_PROCEDURE)
5165 || (fsym->attr.proc_pointer
5166 && !(e->expr_type == EXPR_VARIABLE
5167 && e->symtree->n.sym->attr.dummy))
5168 || (fsym->attr.proc_pointer
5169 && e->expr_type == EXPR_VARIABLE
5170 && gfc_is_proc_ptr_comp (e))
5171 || (fsym->attr.allocatable
5172 && fsym->attr.flavor != FL_PROCEDURE)))
5174 /* Scalar pointer dummy args require an extra level of
5175 indirection. The null pointer already contains
5176 this level of indirection. */
5177 parm_kind = SCALAR_POINTER;
5178 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5182 else if (e->ts.type == BT_CLASS
5183 && fsym && fsym->ts.type == BT_CLASS
5184 && (CLASS_DATA (fsym)->attr.dimension
5185 || CLASS_DATA (fsym)->attr.codimension))
5187 /* Pass a class array. */
5188 parmse.use_offset = 1;
5189 gfc_conv_expr_descriptor (&parmse, e);
5191 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5192 allocated on entry, it must be deallocated. */
5193 if (fsym->attr.intent == INTENT_OUT
5194 && CLASS_DATA (fsym)->attr.allocatable)
5196 stmtblock_t block;
5197 tree ptr;
5199 gfc_init_block (&block);
5200 ptr = parmse.expr;
5201 ptr = gfc_class_data_get (ptr);
5203 tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
5204 NULL_TREE, NULL_TREE,
5205 NULL_TREE, true, e,
5206 false);
5207 gfc_add_expr_to_block (&block, tmp);
5208 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5209 void_type_node, ptr,
5210 null_pointer_node);
5211 gfc_add_expr_to_block (&block, tmp);
5212 gfc_reset_vptr (&block, e);
5214 if (fsym->attr.optional
5215 && e->expr_type == EXPR_VARIABLE
5216 && (!e->ref
5217 || (e->ref->type == REF_ARRAY
5218 && e->ref->u.ar.type != AR_FULL))
5219 && e->symtree->n.sym->attr.optional)
5221 tmp = fold_build3_loc (input_location, COND_EXPR,
5222 void_type_node,
5223 gfc_conv_expr_present (e->symtree->n.sym),
5224 gfc_finish_block (&block),
5225 build_empty_stmt (input_location));
5227 else
5228 tmp = gfc_finish_block (&block);
5230 gfc_add_expr_to_block (&se->pre, tmp);
5233 /* The conversion does not repackage the reference to a class
5234 array - _data descriptor. */
5235 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
5236 fsym->attr.intent != INTENT_IN
5237 && (CLASS_DATA (fsym)->attr.class_pointer
5238 || CLASS_DATA (fsym)->attr.allocatable),
5239 fsym->attr.optional
5240 && e->expr_type == EXPR_VARIABLE
5241 && e->symtree->n.sym->attr.optional,
5242 CLASS_DATA (fsym)->attr.class_pointer
5243 || CLASS_DATA (fsym)->attr.allocatable);
5245 else
5247 /* If the argument is a function call that may not create
5248 a temporary for the result, we have to check that we
5249 can do it, i.e. that there is no alias between this
5250 argument and another one. */
5251 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
5253 gfc_expr *iarg;
5254 sym_intent intent;
5256 if (fsym != NULL)
5257 intent = fsym->attr.intent;
5258 else
5259 intent = INTENT_UNKNOWN;
5261 if (gfc_check_fncall_dependency (e, intent, sym, args,
5262 NOT_ELEMENTAL))
5263 parmse.force_tmp = 1;
5265 iarg = e->value.function.actual->expr;
5267 /* Temporary needed if aliasing due to host association. */
5268 if (sym->attr.contained
5269 && !sym->attr.pure
5270 && !sym->attr.implicit_pure
5271 && !sym->attr.use_assoc
5272 && iarg->expr_type == EXPR_VARIABLE
5273 && sym->ns == iarg->symtree->n.sym->ns)
5274 parmse.force_tmp = 1;
5276 /* Ditto within module. */
5277 if (sym->attr.use_assoc
5278 && !sym->attr.pure
5279 && !sym->attr.implicit_pure
5280 && iarg->expr_type == EXPR_VARIABLE
5281 && sym->module == iarg->symtree->n.sym->module)
5282 parmse.force_tmp = 1;
5285 if (e->expr_type == EXPR_VARIABLE
5286 && is_subref_array (e))
5287 /* The actual argument is a component reference to an
5288 array of derived types. In this case, the argument
5289 is converted to a temporary, which is passed and then
5290 written back after the procedure call. */
5291 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
5292 fsym ? fsym->attr.intent : INTENT_INOUT,
5293 fsym && fsym->attr.pointer);
5294 else if (gfc_is_class_array_ref (e, NULL)
5295 && fsym && fsym->ts.type == BT_DERIVED)
5296 /* The actual argument is a component reference to an
5297 array of derived types. In this case, the argument
5298 is converted to a temporary, which is passed and then
5299 written back after the procedure call.
5300 OOP-TODO: Insert code so that if the dynamic type is
5301 the same as the declared type, copy-in/copy-out does
5302 not occur. */
5303 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
5304 fsym ? fsym->attr.intent : INTENT_INOUT,
5305 fsym && fsym->attr.pointer);
5307 else if (gfc_is_alloc_class_array_function (e)
5308 && fsym && fsym->ts.type == BT_DERIVED)
5309 /* See previous comment. For function actual argument,
5310 the write out is not needed so the intent is set as
5311 intent in. */
5313 e->must_finalize = 1;
5314 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
5315 INTENT_IN,
5316 fsym && fsym->attr.pointer);
5318 else
5319 gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
5320 sym->name, NULL);
5322 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5323 allocated on entry, it must be deallocated. */
5324 if (fsym && fsym->attr.allocatable
5325 && fsym->attr.intent == INTENT_OUT)
5327 tmp = build_fold_indirect_ref_loc (input_location,
5328 parmse.expr);
5329 tmp = gfc_trans_dealloc_allocated (tmp, false, e);
5330 if (fsym->attr.optional
5331 && e->expr_type == EXPR_VARIABLE
5332 && e->symtree->n.sym->attr.optional)
5333 tmp = fold_build3_loc (input_location, COND_EXPR,
5334 void_type_node,
5335 gfc_conv_expr_present (e->symtree->n.sym),
5336 tmp, build_empty_stmt (input_location));
5337 gfc_add_expr_to_block (&se->pre, tmp);
5342 /* The case with fsym->attr.optional is that of a user subroutine
5343 with an interface indicating an optional argument. When we call
5344 an intrinsic subroutine, however, fsym is NULL, but we might still
5345 have an optional argument, so we proceed to the substitution
5346 just in case. */
5347 if (e && (fsym == NULL || fsym->attr.optional))
5349 /* If an optional argument is itself an optional dummy argument,
5350 check its presence and substitute a null if absent. This is
5351 only needed when passing an array to an elemental procedure
5352 as then array elements are accessed - or no NULL pointer is
5353 allowed and a "1" or "0" should be passed if not present.
5354 When passing a non-array-descriptor full array to a
5355 non-array-descriptor dummy, no check is needed. For
5356 array-descriptor actual to array-descriptor dummy, see
5357 PR 41911 for why a check has to be inserted.
5358 fsym == NULL is checked as intrinsics required the descriptor
5359 but do not always set fsym. */
5360 if (e->expr_type == EXPR_VARIABLE
5361 && e->symtree->n.sym->attr.optional
5362 && ((e->rank != 0 && elemental_proc)
5363 || e->representation.length || e->ts.type == BT_CHARACTER
5364 || (e->rank != 0
5365 && (fsym == NULL
5366 || (fsym-> as
5367 && (fsym->as->type == AS_ASSUMED_SHAPE
5368 || fsym->as->type == AS_ASSUMED_RANK
5369 || fsym->as->type == AS_DEFERRED))))))
5370 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
5371 e->representation.length);
5374 if (fsym && e)
5376 /* Obtain the character length of an assumed character length
5377 length procedure from the typespec. */
5378 if (fsym->ts.type == BT_CHARACTER
5379 && parmse.string_length == NULL_TREE
5380 && e->ts.type == BT_PROCEDURE
5381 && e->symtree->n.sym->ts.type == BT_CHARACTER
5382 && e->symtree->n.sym->ts.u.cl->length != NULL
5383 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
5385 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
5386 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
5390 if (fsym && need_interface_mapping && e)
5391 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
5393 gfc_add_block_to_block (&se->pre, &parmse.pre);
5394 gfc_add_block_to_block (&post, &parmse.post);
5396 /* Allocated allocatable components of derived types must be
5397 deallocated for non-variable scalars, array arguments to elemental
5398 procedures, and array arguments with descriptor to non-elemental
5399 procedures. As bounds information for descriptorless arrays is no
5400 longer available here, they are dealt with in trans-array.c
5401 (gfc_conv_array_parameter). */
5402 if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
5403 && e->ts.u.derived->attr.alloc_comp
5404 && (e->rank == 0 || elemental_proc || !nodesc_arg)
5405 && !expr_may_alias_variables (e, elemental_proc))
5407 int parm_rank;
5408 /* It is known the e returns a structure type with at least one
5409 allocatable component. When e is a function, ensure that the
5410 function is called once only by using a temporary variable. */
5411 if (!DECL_P (parmse.expr))
5412 parmse.expr = gfc_evaluate_now_loc (input_location,
5413 parmse.expr, &se->pre);
5415 if (fsym && fsym->attr.value)
5416 tmp = parmse.expr;
5417 else
5418 tmp = build_fold_indirect_ref_loc (input_location,
5419 parmse.expr);
5421 parm_rank = e->rank;
5422 switch (parm_kind)
5424 case (ELEMENTAL):
5425 case (SCALAR):
5426 parm_rank = 0;
5427 break;
5429 case (SCALAR_POINTER):
5430 tmp = build_fold_indirect_ref_loc (input_location,
5431 tmp);
5432 break;
5435 if (e->expr_type == EXPR_OP
5436 && e->value.op.op == INTRINSIC_PARENTHESES
5437 && e->value.op.op1->expr_type == EXPR_VARIABLE)
5439 tree local_tmp;
5440 local_tmp = gfc_evaluate_now (tmp, &se->pre);
5441 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
5442 gfc_add_expr_to_block (&se->post, local_tmp);
5445 if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
5447 /* The derived type is passed to gfc_deallocate_alloc_comp.
5448 Therefore, class actuals can handled correctly but derived
5449 types passed to class formals need the _data component. */
5450 tmp = gfc_class_data_get (tmp);
5451 if (!CLASS_DATA (fsym)->attr.dimension)
5452 tmp = build_fold_indirect_ref_loc (input_location, tmp);
5455 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
5457 gfc_add_expr_to_block (&se->post, tmp);
5460 /* Add argument checking of passing an unallocated/NULL actual to
5461 a nonallocatable/nonpointer dummy. */
5463 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
5465 symbol_attribute attr;
5466 char *msg;
5467 tree cond;
5469 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
5470 attr = gfc_expr_attr (e);
5471 else
5472 goto end_pointer_check;
5474 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
5475 allocatable to an optional dummy, cf. 12.5.2.12. */
5476 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
5477 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5478 goto end_pointer_check;
5480 if (attr.optional)
5482 /* If the actual argument is an optional pointer/allocatable and
5483 the formal argument takes an nonpointer optional value,
5484 it is invalid to pass a non-present argument on, even
5485 though there is no technical reason for this in gfortran.
5486 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
5487 tree present, null_ptr, type;
5489 if (attr.allocatable
5490 && (fsym == NULL || !fsym->attr.allocatable))
5491 msg = xasprintf ("Allocatable actual argument '%s' is not "
5492 "allocated or not present",
5493 e->symtree->n.sym->name);
5494 else if (attr.pointer
5495 && (fsym == NULL || !fsym->attr.pointer))
5496 msg = xasprintf ("Pointer actual argument '%s' is not "
5497 "associated or not present",
5498 e->symtree->n.sym->name);
5499 else if (attr.proc_pointer
5500 && (fsym == NULL || !fsym->attr.proc_pointer))
5501 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
5502 "associated or not present",
5503 e->symtree->n.sym->name);
5504 else
5505 goto end_pointer_check;
5507 present = gfc_conv_expr_present (e->symtree->n.sym);
5508 type = TREE_TYPE (present);
5509 present = fold_build2_loc (input_location, EQ_EXPR,
5510 boolean_type_node, present,
5511 fold_convert (type,
5512 null_pointer_node));
5513 type = TREE_TYPE (parmse.expr);
5514 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
5515 boolean_type_node, parmse.expr,
5516 fold_convert (type,
5517 null_pointer_node));
5518 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
5519 boolean_type_node, present, null_ptr);
5521 else
5523 if (attr.allocatable
5524 && (fsym == NULL || !fsym->attr.allocatable))
5525 msg = xasprintf ("Allocatable actual argument '%s' is not "
5526 "allocated", e->symtree->n.sym->name);
5527 else if (attr.pointer
5528 && (fsym == NULL || !fsym->attr.pointer))
5529 msg = xasprintf ("Pointer actual argument '%s' is not "
5530 "associated", e->symtree->n.sym->name);
5531 else if (attr.proc_pointer
5532 && (fsym == NULL || !fsym->attr.proc_pointer))
5533 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
5534 "associated", e->symtree->n.sym->name);
5535 else
5536 goto end_pointer_check;
5538 tmp = parmse.expr;
5540 /* If the argument is passed by value, we need to strip the
5541 INDIRECT_REF. */
5542 if (!POINTER_TYPE_P (TREE_TYPE (parmse.expr)))
5543 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5545 cond = fold_build2_loc (input_location, EQ_EXPR,
5546 boolean_type_node, tmp,
5547 fold_convert (TREE_TYPE (tmp),
5548 null_pointer_node));
5551 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
5552 msg);
5553 free (msg);
5555 end_pointer_check:
5557 /* Deferred length dummies pass the character length by reference
5558 so that the value can be returned. */
5559 if (parmse.string_length && fsym && fsym->ts.deferred)
5561 if (INDIRECT_REF_P (parmse.string_length))
5562 /* In chains of functions/procedure calls the string_length already
5563 is a pointer to the variable holding the length. Therefore
5564 remove the deref on call. */
5565 parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
5566 else
5568 tmp = parmse.string_length;
5569 if (TREE_CODE (tmp) != VAR_DECL)
5570 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
5571 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
5575 /* Character strings are passed as two parameters, a length and a
5576 pointer - except for Bind(c) which only passes the pointer.
5577 An unlimited polymorphic formal argument likewise does not
5578 need the length. */
5579 if (parmse.string_length != NULL_TREE
5580 && !sym->attr.is_bind_c
5581 && !(fsym && UNLIMITED_POLY (fsym)))
5582 vec_safe_push (stringargs, parmse.string_length);
5584 /* When calling __copy for character expressions to unlimited
5585 polymorphic entities, the dst argument needs a string length. */
5586 if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
5587 && strncmp (sym->name, "__vtab_CHARACTER", 16) == 0
5588 && arg->next && arg->next->expr
5589 && arg->next->expr->ts.type == BT_DERIVED
5590 && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
5591 vec_safe_push (stringargs, parmse.string_length);
5593 /* For descriptorless coarrays and assumed-shape coarray dummies, we
5594 pass the token and the offset as additional arguments. */
5595 if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
5596 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
5597 && !fsym->attr.allocatable)
5598 || (fsym->ts.type == BT_CLASS
5599 && CLASS_DATA (fsym)->attr.codimension
5600 && !CLASS_DATA (fsym)->attr.allocatable)))
5602 /* Token and offset. */
5603 vec_safe_push (stringargs, null_pointer_node);
5604 vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
5605 gcc_assert (fsym->attr.optional);
5607 else if (fsym && flag_coarray == GFC_FCOARRAY_LIB
5608 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
5609 && !fsym->attr.allocatable)
5610 || (fsym->ts.type == BT_CLASS
5611 && CLASS_DATA (fsym)->attr.codimension
5612 && !CLASS_DATA (fsym)->attr.allocatable)))
5614 tree caf_decl, caf_type;
5615 tree offset, tmp2;
5617 caf_decl = gfc_get_tree_for_caf_expr (e);
5618 caf_type = TREE_TYPE (caf_decl);
5620 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
5621 && (GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE
5622 || GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_POINTER))
5623 tmp = gfc_conv_descriptor_token (caf_decl);
5624 else if (DECL_LANG_SPECIFIC (caf_decl)
5625 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
5626 tmp = GFC_DECL_TOKEN (caf_decl);
5627 else
5629 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
5630 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
5631 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
5634 vec_safe_push (stringargs, tmp);
5636 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
5637 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
5638 offset = build_int_cst (gfc_array_index_type, 0);
5639 else if (DECL_LANG_SPECIFIC (caf_decl)
5640 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
5641 offset = GFC_DECL_CAF_OFFSET (caf_decl);
5642 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
5643 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
5644 else
5645 offset = build_int_cst (gfc_array_index_type, 0);
5647 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
5648 tmp = gfc_conv_descriptor_data_get (caf_decl);
5649 else
5651 gcc_assert (POINTER_TYPE_P (caf_type));
5652 tmp = caf_decl;
5655 tmp2 = fsym->ts.type == BT_CLASS
5656 ? gfc_class_data_get (parmse.expr) : parmse.expr;
5657 if ((fsym->ts.type != BT_CLASS
5658 && (fsym->as->type == AS_ASSUMED_SHAPE
5659 || fsym->as->type == AS_ASSUMED_RANK))
5660 || (fsym->ts.type == BT_CLASS
5661 && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
5662 || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
5664 if (fsym->ts.type == BT_CLASS)
5665 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
5666 else
5668 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
5669 tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
5671 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
5672 tmp2 = gfc_conv_descriptor_data_get (tmp2);
5674 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
5675 tmp2 = gfc_conv_descriptor_data_get (tmp2);
5676 else
5678 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
5681 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5682 gfc_array_index_type,
5683 fold_convert (gfc_array_index_type, tmp2),
5684 fold_convert (gfc_array_index_type, tmp));
5685 offset = fold_build2_loc (input_location, PLUS_EXPR,
5686 gfc_array_index_type, offset, tmp);
5688 vec_safe_push (stringargs, offset);
5691 vec_safe_push (arglist, parmse.expr);
5693 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
5695 if (comp)
5696 ts = comp->ts;
5697 else
5698 ts = sym->ts;
5700 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
5701 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
5702 else if (ts.type == BT_CHARACTER)
5704 if (ts.u.cl->length == NULL)
5706 /* Assumed character length results are not allowed by 5.1.1.5 of the
5707 standard and are trapped in resolve.c; except in the case of SPREAD
5708 (and other intrinsics?) and dummy functions. In the case of SPREAD,
5709 we take the character length of the first argument for the result.
5710 For dummies, we have to look through the formal argument list for
5711 this function and use the character length found there.*/
5712 if (ts.deferred)
5713 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
5714 else if (!sym->attr.dummy)
5715 cl.backend_decl = (*stringargs)[0];
5716 else
5718 formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
5719 for (; formal; formal = formal->next)
5720 if (strcmp (formal->sym->name, sym->name) == 0)
5721 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
5723 len = cl.backend_decl;
5725 else
5727 tree tmp;
5729 /* Calculate the length of the returned string. */
5730 gfc_init_se (&parmse, NULL);
5731 if (need_interface_mapping)
5732 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
5733 else
5734 gfc_conv_expr (&parmse, ts.u.cl->length);
5735 gfc_add_block_to_block (&se->pre, &parmse.pre);
5736 gfc_add_block_to_block (&se->post, &parmse.post);
5738 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
5739 tmp = fold_build2_loc (input_location, MAX_EXPR,
5740 gfc_charlen_type_node, tmp,
5741 build_int_cst (gfc_charlen_type_node, 0));
5742 cl.backend_decl = tmp;
5745 /* Set up a charlen structure for it. */
5746 cl.next = NULL;
5747 cl.length = NULL;
5748 ts.u.cl = &cl;
5750 len = cl.backend_decl;
5753 byref = (comp && (comp->attr.dimension || comp->ts.type == BT_CHARACTER))
5754 || (!comp && gfc_return_by_reference (sym));
5755 if (byref)
5757 if (se->direct_byref)
5759 /* Sometimes, too much indirection can be applied; e.g. for
5760 function_result = array_valued_recursive_function. */
5761 if (TREE_TYPE (TREE_TYPE (se->expr))
5762 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
5763 && GFC_DESCRIPTOR_TYPE_P
5764 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
5765 se->expr = build_fold_indirect_ref_loc (input_location,
5766 se->expr);
5768 /* If the lhs of an assignment x = f(..) is allocatable and
5769 f2003 is allowed, we must do the automatic reallocation.
5770 TODO - deal with intrinsics, without using a temporary. */
5771 if (flag_realloc_lhs
5772 && se->ss && se->ss->loop_chain
5773 && se->ss->loop_chain->is_alloc_lhs
5774 && !expr->value.function.isym
5775 && sym->result->as != NULL)
5777 /* Evaluate the bounds of the result, if known. */
5778 gfc_set_loop_bounds_from_array_spec (&mapping, se,
5779 sym->result->as);
5781 /* Perform the automatic reallocation. */
5782 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
5783 expr, NULL);
5784 gfc_add_expr_to_block (&se->pre, tmp);
5786 /* Pass the temporary as the first argument. */
5787 result = info->descriptor;
5789 else
5790 result = build_fold_indirect_ref_loc (input_location,
5791 se->expr);
5792 vec_safe_push (retargs, se->expr);
5794 else if (comp && comp->attr.dimension)
5796 gcc_assert (se->loop && info);
5798 /* Set the type of the array. */
5799 tmp = gfc_typenode_for_spec (&comp->ts);
5800 gcc_assert (se->ss->dimen == se->loop->dimen);
5802 /* Evaluate the bounds of the result, if known. */
5803 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
5805 /* If the lhs of an assignment x = f(..) is allocatable and
5806 f2003 is allowed, we must not generate the function call
5807 here but should just send back the results of the mapping.
5808 This is signalled by the function ss being flagged. */
5809 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
5811 gfc_free_interface_mapping (&mapping);
5812 return has_alternate_specifier;
5815 /* Create a temporary to store the result. In case the function
5816 returns a pointer, the temporary will be a shallow copy and
5817 mustn't be deallocated. */
5818 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
5819 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5820 tmp, NULL_TREE, false,
5821 !comp->attr.pointer, callee_alloc,
5822 &se->ss->info->expr->where);
5824 /* Pass the temporary as the first argument. */
5825 result = info->descriptor;
5826 tmp = gfc_build_addr_expr (NULL_TREE, result);
5827 vec_safe_push (retargs, tmp);
5829 else if (!comp && sym->result->attr.dimension)
5831 gcc_assert (se->loop && info);
5833 /* Set the type of the array. */
5834 tmp = gfc_typenode_for_spec (&ts);
5835 gcc_assert (se->ss->dimen == se->loop->dimen);
5837 /* Evaluate the bounds of the result, if known. */
5838 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
5840 /* If the lhs of an assignment x = f(..) is allocatable and
5841 f2003 is allowed, we must not generate the function call
5842 here but should just send back the results of the mapping.
5843 This is signalled by the function ss being flagged. */
5844 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
5846 gfc_free_interface_mapping (&mapping);
5847 return has_alternate_specifier;
5850 /* Create a temporary to store the result. In case the function
5851 returns a pointer, the temporary will be a shallow copy and
5852 mustn't be deallocated. */
5853 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
5854 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5855 tmp, NULL_TREE, false,
5856 !sym->attr.pointer, callee_alloc,
5857 &se->ss->info->expr->where);
5859 /* Pass the temporary as the first argument. */
5860 result = info->descriptor;
5861 tmp = gfc_build_addr_expr (NULL_TREE, result);
5862 vec_safe_push (retargs, tmp);
5864 else if (ts.type == BT_CHARACTER)
5866 /* Pass the string length. */
5867 type = gfc_get_character_type (ts.kind, ts.u.cl);
5868 type = build_pointer_type (type);
5870 /* Return an address to a char[0:len-1]* temporary for
5871 character pointers. */
5872 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5873 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
5875 var = gfc_create_var (type, "pstr");
5877 if ((!comp && sym->attr.allocatable)
5878 || (comp && comp->attr.allocatable))
5880 gfc_add_modify (&se->pre, var,
5881 fold_convert (TREE_TYPE (var),
5882 null_pointer_node));
5883 tmp = gfc_call_free (var);
5884 gfc_add_expr_to_block (&se->post, tmp);
5887 /* Provide an address expression for the function arguments. */
5888 var = gfc_build_addr_expr (NULL_TREE, var);
5890 else
5891 var = gfc_conv_string_tmp (se, type, len);
5893 vec_safe_push (retargs, var);
5895 else
5897 gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
5899 type = gfc_get_complex_type (ts.kind);
5900 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
5901 vec_safe_push (retargs, var);
5904 /* Add the string length to the argument list. */
5905 if (ts.type == BT_CHARACTER && ts.deferred)
5907 tmp = len;
5908 if (TREE_CODE (tmp) != VAR_DECL)
5909 tmp = gfc_evaluate_now (len, &se->pre);
5910 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5911 vec_safe_push (retargs, tmp);
5913 else if (ts.type == BT_CHARACTER)
5914 vec_safe_push (retargs, len);
5916 gfc_free_interface_mapping (&mapping);
5918 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
5919 arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
5920 + vec_safe_length (stringargs) + vec_safe_length (append_args));
5921 vec_safe_reserve (retargs, arglen);
5923 /* Add the return arguments. */
5924 vec_safe_splice (retargs, arglist);
5926 /* Add the hidden present status for optional+value to the arguments. */
5927 vec_safe_splice (retargs, optionalargs);
5929 /* Add the hidden string length parameters to the arguments. */
5930 vec_safe_splice (retargs, stringargs);
5932 /* We may want to append extra arguments here. This is used e.g. for
5933 calls to libgfortran_matmul_??, which need extra information. */
5934 vec_safe_splice (retargs, append_args);
5936 arglist = retargs;
5938 /* Generate the actual call. */
5939 if (base_object == NULL_TREE)
5940 conv_function_val (se, sym, expr);
5941 else
5942 conv_base_obj_fcn_val (se, base_object, expr);
5944 /* If there are alternate return labels, function type should be
5945 integer. Can't modify the type in place though, since it can be shared
5946 with other functions. For dummy arguments, the typing is done to
5947 this result, even if it has to be repeated for each call. */
5948 if (has_alternate_specifier
5949 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
5951 if (!sym->attr.dummy)
5953 TREE_TYPE (sym->backend_decl)
5954 = build_function_type (integer_type_node,
5955 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
5956 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
5958 else
5959 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
5962 fntype = TREE_TYPE (TREE_TYPE (se->expr));
5963 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
5965 /* Allocatable scalar function results must be freed and nullified
5966 after use. This necessitates the creation of a temporary to
5967 hold the result to prevent duplicate calls. */
5968 if (!byref && sym->ts.type != BT_CHARACTER
5969 && sym->attr.allocatable && !sym->attr.dimension)
5971 tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
5972 gfc_add_modify (&se->pre, tmp, se->expr);
5973 se->expr = tmp;
5974 tmp = gfc_call_free (tmp);
5975 gfc_add_expr_to_block (&post, tmp);
5976 gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
5979 /* If we have a pointer function, but we don't want a pointer, e.g.
5980 something like
5981 x = f()
5982 where f is pointer valued, we have to dereference the result. */
5983 if (!se->want_pointer && !byref
5984 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5985 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
5986 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
5988 /* f2c calling conventions require a scalar default real function to
5989 return a double precision result. Convert this back to default
5990 real. We only care about the cases that can happen in Fortran 77.
5992 if (flag_f2c && sym->ts.type == BT_REAL
5993 && sym->ts.kind == gfc_default_real_kind
5994 && !sym->attr.always_explicit)
5995 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
5997 /* A pure function may still have side-effects - it may modify its
5998 parameters. */
5999 TREE_SIDE_EFFECTS (se->expr) = 1;
6000 #if 0
6001 if (!sym->attr.pure)
6002 TREE_SIDE_EFFECTS (se->expr) = 1;
6003 #endif
6005 if (byref)
6007 /* Add the function call to the pre chain. There is no expression. */
6008 gfc_add_expr_to_block (&se->pre, se->expr);
6009 se->expr = NULL_TREE;
6011 if (!se->direct_byref)
6013 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
6015 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
6017 /* Check the data pointer hasn't been modified. This would
6018 happen in a function returning a pointer. */
6019 tmp = gfc_conv_descriptor_data_get (info->descriptor);
6020 tmp = fold_build2_loc (input_location, NE_EXPR,
6021 boolean_type_node,
6022 tmp, info->data);
6023 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
6024 gfc_msg_fault);
6026 se->expr = info->descriptor;
6027 /* Bundle in the string length. */
6028 se->string_length = len;
6030 else if (ts.type == BT_CHARACTER)
6032 /* Dereference for character pointer results. */
6033 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
6034 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
6035 se->expr = build_fold_indirect_ref_loc (input_location, var);
6036 else
6037 se->expr = var;
6039 se->string_length = len;
6041 else
6043 gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
6044 se->expr = build_fold_indirect_ref_loc (input_location, var);
6049 /* Follow the function call with the argument post block. */
6050 if (byref)
6052 gfc_add_block_to_block (&se->pre, &post);
6054 /* Transformational functions of derived types with allocatable
6055 components must have the result allocatable components copied. */
6056 arg = expr->value.function.actual;
6057 if (result && arg && expr->rank
6058 && expr->value.function.isym
6059 && expr->value.function.isym->transformational
6060 && arg->expr->ts.type == BT_DERIVED
6061 && arg->expr->ts.u.derived->attr.alloc_comp)
6063 tree tmp2;
6064 /* Copy the allocatable components. We have to use a
6065 temporary here to prevent source allocatable components
6066 from being corrupted. */
6067 tmp2 = gfc_evaluate_now (result, &se->pre);
6068 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
6069 result, tmp2, expr->rank);
6070 gfc_add_expr_to_block (&se->pre, tmp);
6071 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
6072 expr->rank);
6073 gfc_add_expr_to_block (&se->pre, tmp);
6075 /* Finally free the temporary's data field. */
6076 tmp = gfc_conv_descriptor_data_get (tmp2);
6077 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
6078 NULL_TREE, NULL_TREE, true,
6079 NULL, false);
6080 gfc_add_expr_to_block (&se->pre, tmp);
6083 else
6085 /* For a function with a class array result, save the result as
6086 a temporary, set the info fields needed by the scalarizer and
6087 call the finalization function of the temporary. Note that the
6088 nullification of allocatable components needed by the result
6089 is done in gfc_trans_assignment_1. */
6090 if (expr && ((gfc_is_alloc_class_array_function (expr)
6091 && se->ss && se->ss->loop)
6092 || gfc_is_alloc_class_scalar_function (expr))
6093 && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
6094 && expr->must_finalize)
6096 tree final_fndecl;
6097 tree is_final;
6098 int n;
6099 if (se->ss && se->ss->loop)
6101 se->expr = gfc_evaluate_now (se->expr, &se->ss->loop->pre);
6102 tmp = gfc_class_data_get (se->expr);
6103 info->descriptor = tmp;
6104 info->data = gfc_conv_descriptor_data_get (tmp);
6105 info->offset = gfc_conv_descriptor_offset_get (tmp);
6106 for (n = 0; n < se->ss->loop->dimen; n++)
6108 tree dim = gfc_rank_cst[n];
6109 se->ss->loop->to[n] = gfc_conv_descriptor_ubound_get (tmp, dim);
6110 se->ss->loop->from[n] = gfc_conv_descriptor_lbound_get (tmp, dim);
6113 else
6115 /* TODO Eliminate the doubling of temporaries. This
6116 one is necessary to ensure no memory leakage. */
6117 se->expr = gfc_evaluate_now (se->expr, &se->pre);
6118 tmp = gfc_class_data_get (se->expr);
6119 tmp = gfc_conv_scalar_to_descriptor (se, tmp,
6120 CLASS_DATA (expr->value.function.esym->result)->attr);
6123 final_fndecl = gfc_class_vtab_final_get (se->expr);
6124 is_final = fold_build2_loc (input_location, NE_EXPR,
6125 boolean_type_node,
6126 final_fndecl,
6127 fold_convert (TREE_TYPE (final_fndecl),
6128 null_pointer_node));
6129 final_fndecl = build_fold_indirect_ref_loc (input_location,
6130 final_fndecl);
6131 tmp = build_call_expr_loc (input_location,
6132 final_fndecl, 3,
6133 gfc_build_addr_expr (NULL, tmp),
6134 gfc_class_vtab_size_get (se->expr),
6135 boolean_false_node);
6136 tmp = fold_build3_loc (input_location, COND_EXPR,
6137 void_type_node, is_final, tmp,
6138 build_empty_stmt (input_location));
6140 if (se->ss && se->ss->loop)
6142 gfc_add_expr_to_block (&se->ss->loop->post, tmp);
6143 tmp = gfc_call_free (info->data);
6144 gfc_add_expr_to_block (&se->ss->loop->post, tmp);
6146 else
6148 gfc_add_expr_to_block (&se->post, tmp);
6149 tmp = gfc_class_data_get (se->expr);
6150 tmp = gfc_call_free (tmp);
6151 gfc_add_expr_to_block (&se->post, tmp);
6153 expr->must_finalize = 0;
6156 gfc_add_block_to_block (&se->post, &post);
6159 return has_alternate_specifier;
6163 /* Fill a character string with spaces. */
6165 static tree
6166 fill_with_spaces (tree start, tree type, tree size)
6168 stmtblock_t block, loop;
6169 tree i, el, exit_label, cond, tmp;
6171 /* For a simple char type, we can call memset(). */
6172 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
6173 return build_call_expr_loc (input_location,
6174 builtin_decl_explicit (BUILT_IN_MEMSET),
6175 3, start,
6176 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
6177 lang_hooks.to_target_charset (' ')),
6178 size);
6180 /* Otherwise, we use a loop:
6181 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
6182 *el = (type) ' ';
6185 /* Initialize variables. */
6186 gfc_init_block (&block);
6187 i = gfc_create_var (sizetype, "i");
6188 gfc_add_modify (&block, i, fold_convert (sizetype, size));
6189 el = gfc_create_var (build_pointer_type (type), "el");
6190 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
6191 exit_label = gfc_build_label_decl (NULL_TREE);
6192 TREE_USED (exit_label) = 1;
6195 /* Loop body. */
6196 gfc_init_block (&loop);
6198 /* Exit condition. */
6199 cond = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, i,
6200 build_zero_cst (sizetype));
6201 tmp = build1_v (GOTO_EXPR, exit_label);
6202 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
6203 build_empty_stmt (input_location));
6204 gfc_add_expr_to_block (&loop, tmp);
6206 /* Assignment. */
6207 gfc_add_modify (&loop,
6208 fold_build1_loc (input_location, INDIRECT_REF, type, el),
6209 build_int_cst (type, lang_hooks.to_target_charset (' ')));
6211 /* Increment loop variables. */
6212 gfc_add_modify (&loop, i,
6213 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
6214 TYPE_SIZE_UNIT (type)));
6215 gfc_add_modify (&loop, el,
6216 fold_build_pointer_plus_loc (input_location,
6217 el, TYPE_SIZE_UNIT (type)));
6219 /* Making the loop... actually loop! */
6220 tmp = gfc_finish_block (&loop);
6221 tmp = build1_v (LOOP_EXPR, tmp);
6222 gfc_add_expr_to_block (&block, tmp);
6224 /* The exit label. */
6225 tmp = build1_v (LABEL_EXPR, exit_label);
6226 gfc_add_expr_to_block (&block, tmp);
6229 return gfc_finish_block (&block);
6233 /* Generate code to copy a string. */
6235 void
6236 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
6237 int dkind, tree slength, tree src, int skind)
6239 tree tmp, dlen, slen;
6240 tree dsc;
6241 tree ssc;
6242 tree cond;
6243 tree cond2;
6244 tree tmp2;
6245 tree tmp3;
6246 tree tmp4;
6247 tree chartype;
6248 stmtblock_t tempblock;
6250 gcc_assert (dkind == skind);
6252 if (slength != NULL_TREE)
6254 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
6255 ssc = gfc_string_to_single_character (slen, src, skind);
6257 else
6259 slen = build_int_cst (size_type_node, 1);
6260 ssc = src;
6263 if (dlength != NULL_TREE)
6265 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
6266 dsc = gfc_string_to_single_character (dlen, dest, dkind);
6268 else
6270 dlen = build_int_cst (size_type_node, 1);
6271 dsc = dest;
6274 /* Assign directly if the types are compatible. */
6275 if (dsc != NULL_TREE && ssc != NULL_TREE
6276 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
6278 gfc_add_modify (block, dsc, ssc);
6279 return;
6282 /* Do nothing if the destination length is zero. */
6283 cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, dlen,
6284 build_int_cst (size_type_node, 0));
6286 /* The following code was previously in _gfortran_copy_string:
6288 // The two strings may overlap so we use memmove.
6289 void
6290 copy_string (GFC_INTEGER_4 destlen, char * dest,
6291 GFC_INTEGER_4 srclen, const char * src)
6293 if (srclen >= destlen)
6295 // This will truncate if too long.
6296 memmove (dest, src, destlen);
6298 else
6300 memmove (dest, src, srclen);
6301 // Pad with spaces.
6302 memset (&dest[srclen], ' ', destlen - srclen);
6306 We're now doing it here for better optimization, but the logic
6307 is the same. */
6309 /* For non-default character kinds, we have to multiply the string
6310 length by the base type size. */
6311 chartype = gfc_get_char_type (dkind);
6312 slen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6313 fold_convert (size_type_node, slen),
6314 fold_convert (size_type_node,
6315 TYPE_SIZE_UNIT (chartype)));
6316 dlen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6317 fold_convert (size_type_node, dlen),
6318 fold_convert (size_type_node,
6319 TYPE_SIZE_UNIT (chartype)));
6321 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
6322 dest = fold_convert (pvoid_type_node, dest);
6323 else
6324 dest = gfc_build_addr_expr (pvoid_type_node, dest);
6326 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
6327 src = fold_convert (pvoid_type_node, src);
6328 else
6329 src = gfc_build_addr_expr (pvoid_type_node, src);
6331 /* Truncate string if source is too long. */
6332 cond2 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, slen,
6333 dlen);
6334 tmp2 = build_call_expr_loc (input_location,
6335 builtin_decl_explicit (BUILT_IN_MEMMOVE),
6336 3, dest, src, dlen);
6338 /* Else copy and pad with spaces. */
6339 tmp3 = build_call_expr_loc (input_location,
6340 builtin_decl_explicit (BUILT_IN_MEMMOVE),
6341 3, dest, src, slen);
6343 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
6344 tmp4 = fill_with_spaces (tmp4, chartype,
6345 fold_build2_loc (input_location, MINUS_EXPR,
6346 TREE_TYPE(dlen), dlen, slen));
6348 gfc_init_block (&tempblock);
6349 gfc_add_expr_to_block (&tempblock, tmp3);
6350 gfc_add_expr_to_block (&tempblock, tmp4);
6351 tmp3 = gfc_finish_block (&tempblock);
6353 /* The whole copy_string function is there. */
6354 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
6355 tmp2, tmp3);
6356 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
6357 build_empty_stmt (input_location));
6358 gfc_add_expr_to_block (block, tmp);
6362 /* Translate a statement function.
6363 The value of a statement function reference is obtained by evaluating the
6364 expression using the values of the actual arguments for the values of the
6365 corresponding dummy arguments. */
6367 static void
6368 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
6370 gfc_symbol *sym;
6371 gfc_symbol *fsym;
6372 gfc_formal_arglist *fargs;
6373 gfc_actual_arglist *args;
6374 gfc_se lse;
6375 gfc_se rse;
6376 gfc_saved_var *saved_vars;
6377 tree *temp_vars;
6378 tree type;
6379 tree tmp;
6380 int n;
6382 sym = expr->symtree->n.sym;
6383 args = expr->value.function.actual;
6384 gfc_init_se (&lse, NULL);
6385 gfc_init_se (&rse, NULL);
6387 n = 0;
6388 for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
6389 n++;
6390 saved_vars = XCNEWVEC (gfc_saved_var, n);
6391 temp_vars = XCNEWVEC (tree, n);
6393 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6394 fargs = fargs->next, n++)
6396 /* Each dummy shall be specified, explicitly or implicitly, to be
6397 scalar. */
6398 gcc_assert (fargs->sym->attr.dimension == 0);
6399 fsym = fargs->sym;
6401 if (fsym->ts.type == BT_CHARACTER)
6403 /* Copy string arguments. */
6404 tree arglen;
6406 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
6407 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
6409 /* Create a temporary to hold the value. */
6410 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
6411 fsym->ts.u.cl->backend_decl
6412 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
6414 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
6415 temp_vars[n] = gfc_create_var (type, fsym->name);
6417 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
6419 gfc_conv_expr (&rse, args->expr);
6420 gfc_conv_string_parameter (&rse);
6421 gfc_add_block_to_block (&se->pre, &lse.pre);
6422 gfc_add_block_to_block (&se->pre, &rse.pre);
6424 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
6425 rse.string_length, rse.expr, fsym->ts.kind);
6426 gfc_add_block_to_block (&se->pre, &lse.post);
6427 gfc_add_block_to_block (&se->pre, &rse.post);
6429 else
6431 /* For everything else, just evaluate the expression. */
6433 /* Create a temporary to hold the value. */
6434 type = gfc_typenode_for_spec (&fsym->ts);
6435 temp_vars[n] = gfc_create_var (type, fsym->name);
6437 gfc_conv_expr (&lse, args->expr);
6439 gfc_add_block_to_block (&se->pre, &lse.pre);
6440 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
6441 gfc_add_block_to_block (&se->pre, &lse.post);
6444 args = args->next;
6447 /* Use the temporary variables in place of the real ones. */
6448 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6449 fargs = fargs->next, n++)
6450 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
6452 gfc_conv_expr (se, sym->value);
6454 if (sym->ts.type == BT_CHARACTER)
6456 gfc_conv_const_charlen (sym->ts.u.cl);
6458 /* Force the expression to the correct length. */
6459 if (!INTEGER_CST_P (se->string_length)
6460 || tree_int_cst_lt (se->string_length,
6461 sym->ts.u.cl->backend_decl))
6463 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
6464 tmp = gfc_create_var (type, sym->name);
6465 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
6466 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
6467 sym->ts.kind, se->string_length, se->expr,
6468 sym->ts.kind);
6469 se->expr = tmp;
6471 se->string_length = sym->ts.u.cl->backend_decl;
6474 /* Restore the original variables. */
6475 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6476 fargs = fargs->next, n++)
6477 gfc_restore_sym (fargs->sym, &saved_vars[n]);
6478 free (temp_vars);
6479 free (saved_vars);
6483 /* Translate a function expression. */
6485 static void
6486 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
6488 gfc_symbol *sym;
6490 if (expr->value.function.isym)
6492 gfc_conv_intrinsic_function (se, expr);
6493 return;
6496 /* expr.value.function.esym is the resolved (specific) function symbol for
6497 most functions. However this isn't set for dummy procedures. */
6498 sym = expr->value.function.esym;
6499 if (!sym)
6500 sym = expr->symtree->n.sym;
6502 /* The IEEE_ARITHMETIC functions are caught here. */
6503 if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
6504 if (gfc_conv_ieee_arithmetic_function (se, expr))
6505 return;
6507 /* We distinguish statement functions from general functions to improve
6508 runtime performance. */
6509 if (sym->attr.proc == PROC_ST_FUNCTION)
6511 gfc_conv_statement_function (se, expr);
6512 return;
6515 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
6516 NULL);
6520 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
6522 static bool
6523 is_zero_initializer_p (gfc_expr * expr)
6525 if (expr->expr_type != EXPR_CONSTANT)
6526 return false;
6528 /* We ignore constants with prescribed memory representations for now. */
6529 if (expr->representation.string)
6530 return false;
6532 switch (expr->ts.type)
6534 case BT_INTEGER:
6535 return mpz_cmp_si (expr->value.integer, 0) == 0;
6537 case BT_REAL:
6538 return mpfr_zero_p (expr->value.real)
6539 && MPFR_SIGN (expr->value.real) >= 0;
6541 case BT_LOGICAL:
6542 return expr->value.logical == 0;
6544 case BT_COMPLEX:
6545 return mpfr_zero_p (mpc_realref (expr->value.complex))
6546 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
6547 && mpfr_zero_p (mpc_imagref (expr->value.complex))
6548 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
6550 default:
6551 break;
6553 return false;
6557 static void
6558 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
6560 gfc_ss *ss;
6562 ss = se->ss;
6563 gcc_assert (ss != NULL && ss != gfc_ss_terminator);
6564 gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
6566 gfc_conv_tmp_array_ref (se);
6570 /* Build a static initializer. EXPR is the expression for the initial value.
6571 The other parameters describe the variable of the component being
6572 initialized. EXPR may be null. */
6574 tree
6575 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
6576 bool array, bool pointer, bool procptr)
6578 gfc_se se;
6580 if (!(expr || pointer || procptr))
6581 return NULL_TREE;
6583 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
6584 (these are the only two iso_c_binding derived types that can be
6585 used as initialization expressions). If so, we need to modify
6586 the 'expr' to be that for a (void *). */
6587 if (expr != NULL && expr->ts.type == BT_DERIVED
6588 && expr->ts.is_iso_c && expr->ts.u.derived)
6590 gfc_symbol *derived = expr->ts.u.derived;
6592 /* The derived symbol has already been converted to a (void *). Use
6593 its kind. */
6594 expr = gfc_get_int_expr (derived->ts.kind, NULL, 0);
6595 expr->ts.f90_type = derived->ts.f90_type;
6597 gfc_init_se (&se, NULL);
6598 gfc_conv_constant (&se, expr);
6599 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6600 return se.expr;
6603 if (array && !procptr)
6605 tree ctor;
6606 /* Arrays need special handling. */
6607 if (pointer)
6608 ctor = gfc_build_null_descriptor (type);
6609 /* Special case assigning an array to zero. */
6610 else if (is_zero_initializer_p (expr))
6611 ctor = build_constructor (type, NULL);
6612 else
6613 ctor = gfc_conv_array_initializer (type, expr);
6614 TREE_STATIC (ctor) = 1;
6615 return ctor;
6617 else if (pointer || procptr)
6619 if (ts->type == BT_CLASS && !procptr)
6621 gfc_init_se (&se, NULL);
6622 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
6623 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
6624 TREE_STATIC (se.expr) = 1;
6625 return se.expr;
6627 else if (!expr || expr->expr_type == EXPR_NULL)
6628 return fold_convert (type, null_pointer_node);
6629 else
6631 gfc_init_se (&se, NULL);
6632 se.want_pointer = 1;
6633 gfc_conv_expr (&se, expr);
6634 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6635 return se.expr;
6638 else
6640 switch (ts->type)
6642 case BT_DERIVED:
6643 case BT_CLASS:
6644 gfc_init_se (&se, NULL);
6645 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
6646 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
6647 else
6648 gfc_conv_structure (&se, expr, 1);
6649 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
6650 TREE_STATIC (se.expr) = 1;
6651 return se.expr;
6653 case BT_CHARACTER:
6655 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
6656 TREE_STATIC (ctor) = 1;
6657 return ctor;
6660 default:
6661 gfc_init_se (&se, NULL);
6662 gfc_conv_constant (&se, expr);
6663 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6664 return se.expr;
6669 static tree
6670 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
6672 gfc_se rse;
6673 gfc_se lse;
6674 gfc_ss *rss;
6675 gfc_ss *lss;
6676 gfc_array_info *lss_array;
6677 stmtblock_t body;
6678 stmtblock_t block;
6679 gfc_loopinfo loop;
6680 int n;
6681 tree tmp;
6683 gfc_start_block (&block);
6685 /* Initialize the scalarizer. */
6686 gfc_init_loopinfo (&loop);
6688 gfc_init_se (&lse, NULL);
6689 gfc_init_se (&rse, NULL);
6691 /* Walk the rhs. */
6692 rss = gfc_walk_expr (expr);
6693 if (rss == gfc_ss_terminator)
6694 /* The rhs is scalar. Add a ss for the expression. */
6695 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
6697 /* Create a SS for the destination. */
6698 lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
6699 GFC_SS_COMPONENT);
6700 lss_array = &lss->info->data.array;
6701 lss_array->shape = gfc_get_shape (cm->as->rank);
6702 lss_array->descriptor = dest;
6703 lss_array->data = gfc_conv_array_data (dest);
6704 lss_array->offset = gfc_conv_array_offset (dest);
6705 for (n = 0; n < cm->as->rank; n++)
6707 lss_array->start[n] = gfc_conv_array_lbound (dest, n);
6708 lss_array->stride[n] = gfc_index_one_node;
6710 mpz_init (lss_array->shape[n]);
6711 mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
6712 cm->as->lower[n]->value.integer);
6713 mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
6716 /* Associate the SS with the loop. */
6717 gfc_add_ss_to_loop (&loop, lss);
6718 gfc_add_ss_to_loop (&loop, rss);
6720 /* Calculate the bounds of the scalarization. */
6721 gfc_conv_ss_startstride (&loop);
6723 /* Setup the scalarizing loops. */
6724 gfc_conv_loop_setup (&loop, &expr->where);
6726 /* Setup the gfc_se structures. */
6727 gfc_copy_loopinfo_to_se (&lse, &loop);
6728 gfc_copy_loopinfo_to_se (&rse, &loop);
6730 rse.ss = rss;
6731 gfc_mark_ss_chain_used (rss, 1);
6732 lse.ss = lss;
6733 gfc_mark_ss_chain_used (lss, 1);
6735 /* Start the scalarized loop body. */
6736 gfc_start_scalarized_body (&loop, &body);
6738 gfc_conv_tmp_array_ref (&lse);
6739 if (cm->ts.type == BT_CHARACTER)
6740 lse.string_length = cm->ts.u.cl->backend_decl;
6742 gfc_conv_expr (&rse, expr);
6744 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
6745 gfc_add_expr_to_block (&body, tmp);
6747 gcc_assert (rse.ss == gfc_ss_terminator);
6749 /* Generate the copying loops. */
6750 gfc_trans_scalarizing_loops (&loop, &body);
6752 /* Wrap the whole thing up. */
6753 gfc_add_block_to_block (&block, &loop.pre);
6754 gfc_add_block_to_block (&block, &loop.post);
6756 gcc_assert (lss_array->shape != NULL);
6757 gfc_free_shape (&lss_array->shape, cm->as->rank);
6758 gfc_cleanup_loop (&loop);
6760 return gfc_finish_block (&block);
6764 static tree
6765 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
6766 gfc_expr * expr)
6768 gfc_se se;
6769 stmtblock_t block;
6770 tree offset;
6771 int n;
6772 tree tmp;
6773 tree tmp2;
6774 gfc_array_spec *as;
6775 gfc_expr *arg = NULL;
6777 gfc_start_block (&block);
6778 gfc_init_se (&se, NULL);
6780 /* Get the descriptor for the expressions. */
6781 se.want_pointer = 0;
6782 gfc_conv_expr_descriptor (&se, expr);
6783 gfc_add_block_to_block (&block, &se.pre);
6784 gfc_add_modify (&block, dest, se.expr);
6786 /* Deal with arrays of derived types with allocatable components. */
6787 if (cm->ts.type == BT_DERIVED
6788 && cm->ts.u.derived->attr.alloc_comp)
6789 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
6790 se.expr, dest,
6791 cm->as->rank);
6792 else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
6793 && CLASS_DATA(cm)->attr.allocatable)
6795 if (cm->ts.u.derived->attr.alloc_comp)
6796 tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
6797 se.expr, dest,
6798 expr->rank);
6799 else
6801 tmp = TREE_TYPE (dest);
6802 tmp = gfc_duplicate_allocatable (dest, se.expr,
6803 tmp, expr->rank, NULL_TREE);
6806 else
6807 tmp = gfc_duplicate_allocatable (dest, se.expr,
6808 TREE_TYPE(cm->backend_decl),
6809 cm->as->rank, NULL_TREE);
6811 gfc_add_expr_to_block (&block, tmp);
6812 gfc_add_block_to_block (&block, &se.post);
6814 if (expr->expr_type != EXPR_VARIABLE)
6815 gfc_conv_descriptor_data_set (&block, se.expr,
6816 null_pointer_node);
6818 /* We need to know if the argument of a conversion function is a
6819 variable, so that the correct lower bound can be used. */
6820 if (expr->expr_type == EXPR_FUNCTION
6821 && expr->value.function.isym
6822 && expr->value.function.isym->conversion
6823 && expr->value.function.actual->expr
6824 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
6825 arg = expr->value.function.actual->expr;
6827 /* Obtain the array spec of full array references. */
6828 if (arg)
6829 as = gfc_get_full_arrayspec_from_expr (arg);
6830 else
6831 as = gfc_get_full_arrayspec_from_expr (expr);
6833 /* Shift the lbound and ubound of temporaries to being unity,
6834 rather than zero, based. Always calculate the offset. */
6835 offset = gfc_conv_descriptor_offset_get (dest);
6836 gfc_add_modify (&block, offset, gfc_index_zero_node);
6837 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
6839 for (n = 0; n < expr->rank; n++)
6841 tree span;
6842 tree lbound;
6844 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
6845 TODO It looks as if gfc_conv_expr_descriptor should return
6846 the correct bounds and that the following should not be
6847 necessary. This would simplify gfc_conv_intrinsic_bound
6848 as well. */
6849 if (as && as->lower[n])
6851 gfc_se lbse;
6852 gfc_init_se (&lbse, NULL);
6853 gfc_conv_expr (&lbse, as->lower[n]);
6854 gfc_add_block_to_block (&block, &lbse.pre);
6855 lbound = gfc_evaluate_now (lbse.expr, &block);
6857 else if (as && arg)
6859 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
6860 lbound = gfc_conv_descriptor_lbound_get (tmp,
6861 gfc_rank_cst[n]);
6863 else if (as)
6864 lbound = gfc_conv_descriptor_lbound_get (dest,
6865 gfc_rank_cst[n]);
6866 else
6867 lbound = gfc_index_one_node;
6869 lbound = fold_convert (gfc_array_index_type, lbound);
6871 /* Shift the bounds and set the offset accordingly. */
6872 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
6873 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6874 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
6875 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6876 span, lbound);
6877 gfc_conv_descriptor_ubound_set (&block, dest,
6878 gfc_rank_cst[n], tmp);
6879 gfc_conv_descriptor_lbound_set (&block, dest,
6880 gfc_rank_cst[n], lbound);
6882 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6883 gfc_conv_descriptor_lbound_get (dest,
6884 gfc_rank_cst[n]),
6885 gfc_conv_descriptor_stride_get (dest,
6886 gfc_rank_cst[n]));
6887 gfc_add_modify (&block, tmp2, tmp);
6888 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6889 offset, tmp2);
6890 gfc_conv_descriptor_offset_set (&block, dest, tmp);
6893 if (arg)
6895 /* If a conversion expression has a null data pointer
6896 argument, nullify the allocatable component. */
6897 tree non_null_expr;
6898 tree null_expr;
6900 if (arg->symtree->n.sym->attr.allocatable
6901 || arg->symtree->n.sym->attr.pointer)
6903 non_null_expr = gfc_finish_block (&block);
6904 gfc_start_block (&block);
6905 gfc_conv_descriptor_data_set (&block, dest,
6906 null_pointer_node);
6907 null_expr = gfc_finish_block (&block);
6908 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
6909 tmp = build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
6910 fold_convert (TREE_TYPE (tmp), null_pointer_node));
6911 return build3_v (COND_EXPR, tmp,
6912 null_expr, non_null_expr);
6916 return gfc_finish_block (&block);
6920 /* Allocate or reallocate scalar component, as necessary. */
6922 static void
6923 alloc_scalar_allocatable_for_subcomponent_assignment (stmtblock_t *block,
6924 tree comp,
6925 gfc_component *cm,
6926 gfc_expr *expr2,
6927 gfc_symbol *sym)
6929 tree tmp;
6930 tree ptr;
6931 tree size;
6932 tree size_in_bytes;
6933 tree lhs_cl_size = NULL_TREE;
6935 if (!comp)
6936 return;
6938 if (!expr2 || expr2->rank)
6939 return;
6941 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
6943 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
6945 char name[GFC_MAX_SYMBOL_LEN+9];
6946 gfc_component *strlen;
6947 /* Use the rhs string length and the lhs element size. */
6948 gcc_assert (expr2->ts.type == BT_CHARACTER);
6949 if (!expr2->ts.u.cl->backend_decl)
6951 gfc_conv_string_length (expr2->ts.u.cl, expr2, block);
6952 gcc_assert (expr2->ts.u.cl->backend_decl);
6955 size = expr2->ts.u.cl->backend_decl;
6957 /* Ensure that cm->ts.u.cl->backend_decl is a componentref to _%s_length
6958 component. */
6959 sprintf (name, "_%s_length", cm->name);
6960 strlen = gfc_find_component (sym, name, true, true);
6961 lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
6962 gfc_charlen_type_node,
6963 TREE_OPERAND (comp, 0),
6964 strlen->backend_decl, NULL_TREE);
6966 tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
6967 tmp = TYPE_SIZE_UNIT (tmp);
6968 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
6969 TREE_TYPE (tmp), tmp,
6970 fold_convert (TREE_TYPE (tmp), size));
6972 else if (cm->ts.type == BT_CLASS)
6974 gcc_assert (expr2->ts.type == BT_CLASS || expr2->ts.type == BT_DERIVED);
6975 if (expr2->ts.type == BT_DERIVED)
6977 tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
6978 size = TYPE_SIZE_UNIT (tmp);
6980 else
6982 gfc_expr *e2vtab;
6983 gfc_se se;
6984 e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
6985 gfc_add_vptr_component (e2vtab);
6986 gfc_add_size_component (e2vtab);
6987 gfc_init_se (&se, NULL);
6988 gfc_conv_expr (&se, e2vtab);
6989 gfc_add_block_to_block (block, &se.pre);
6990 size = fold_convert (size_type_node, se.expr);
6991 gfc_free_expr (e2vtab);
6993 size_in_bytes = size;
6995 else
6997 /* Otherwise use the length in bytes of the rhs. */
6998 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
6999 size_in_bytes = size;
7002 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
7003 size_in_bytes, size_one_node);
7005 if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
7007 tmp = build_call_expr_loc (input_location,
7008 builtin_decl_explicit (BUILT_IN_CALLOC),
7009 2, build_one_cst (size_type_node),
7010 size_in_bytes);
7011 tmp = fold_convert (TREE_TYPE (comp), tmp);
7012 gfc_add_modify (block, comp, tmp);
7014 else
7016 tmp = build_call_expr_loc (input_location,
7017 builtin_decl_explicit (BUILT_IN_MALLOC),
7018 1, size_in_bytes);
7019 if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
7020 ptr = gfc_class_data_get (comp);
7021 else
7022 ptr = comp;
7023 tmp = fold_convert (TREE_TYPE (ptr), tmp);
7024 gfc_add_modify (block, ptr, tmp);
7027 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
7028 /* Update the lhs character length. */
7029 gfc_add_modify (block, lhs_cl_size, size);
7033 /* Assign a single component of a derived type constructor. */
7035 static tree
7036 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr,
7037 gfc_symbol *sym, bool init)
7039 gfc_se se;
7040 gfc_se lse;
7041 stmtblock_t block;
7042 tree tmp;
7043 tree vtab;
7045 gfc_start_block (&block);
7047 if (cm->attr.pointer || cm->attr.proc_pointer)
7049 /* Only care about pointers here, not about allocatables. */
7050 gfc_init_se (&se, NULL);
7051 /* Pointer component. */
7052 if ((cm->attr.dimension || cm->attr.codimension)
7053 && !cm->attr.proc_pointer)
7055 /* Array pointer. */
7056 if (expr->expr_type == EXPR_NULL)
7057 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
7058 else
7060 se.direct_byref = 1;
7061 se.expr = dest;
7062 gfc_conv_expr_descriptor (&se, expr);
7063 gfc_add_block_to_block (&block, &se.pre);
7064 gfc_add_block_to_block (&block, &se.post);
7067 else
7069 /* Scalar pointers. */
7070 se.want_pointer = 1;
7071 gfc_conv_expr (&se, expr);
7072 gfc_add_block_to_block (&block, &se.pre);
7074 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
7075 && expr->symtree->n.sym->attr.dummy)
7076 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
7078 gfc_add_modify (&block, dest,
7079 fold_convert (TREE_TYPE (dest), se.expr));
7080 gfc_add_block_to_block (&block, &se.post);
7083 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
7085 /* NULL initialization for CLASS components. */
7086 tmp = gfc_trans_structure_assign (dest,
7087 gfc_class_initializer (&cm->ts, expr),
7088 false);
7089 gfc_add_expr_to_block (&block, tmp);
7091 else if ((cm->attr.dimension || cm->attr.codimension)
7092 && !cm->attr.proc_pointer)
7094 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
7095 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
7096 else if (cm->attr.allocatable)
7098 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
7099 gfc_add_expr_to_block (&block, tmp);
7101 else
7103 tmp = gfc_trans_subarray_assign (dest, cm, expr);
7104 gfc_add_expr_to_block (&block, tmp);
7107 else if (cm->ts.type == BT_CLASS
7108 && CLASS_DATA (cm)->attr.dimension
7109 && CLASS_DATA (cm)->attr.allocatable
7110 && expr->ts.type == BT_DERIVED)
7112 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
7113 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
7114 tmp = gfc_class_vptr_get (dest);
7115 gfc_add_modify (&block, tmp,
7116 fold_convert (TREE_TYPE (tmp), vtab));
7117 tmp = gfc_class_data_get (dest);
7118 tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
7119 gfc_add_expr_to_block (&block, tmp);
7121 else if (init && (cm->attr.allocatable
7122 || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
7123 && expr->ts.type != BT_CLASS)))
7125 /* Take care about non-array allocatable components here. The alloc_*
7126 routine below is motivated by the alloc_scalar_allocatable_for_
7127 assignment() routine, but with the realloc portions removed and
7128 different input. */
7129 alloc_scalar_allocatable_for_subcomponent_assignment (&block,
7130 dest,
7132 expr,
7133 sym);
7134 /* The remainder of these instructions follow the if (cm->attr.pointer)
7135 if (!cm->attr.dimension) part above. */
7136 gfc_init_se (&se, NULL);
7137 gfc_conv_expr (&se, expr);
7138 gfc_add_block_to_block (&block, &se.pre);
7140 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
7141 && expr->symtree->n.sym->attr.dummy)
7142 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
7144 if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED)
7146 tmp = gfc_class_data_get (dest);
7147 tmp = build_fold_indirect_ref_loc (input_location, tmp);
7148 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
7149 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
7150 gfc_add_modify (&block, gfc_class_vptr_get (dest),
7151 fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
7153 else
7154 tmp = build_fold_indirect_ref_loc (input_location, dest);
7156 /* For deferred strings insert a memcpy. */
7157 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
7159 tree size;
7160 gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
7161 size = size_of_string_in_bytes (cm->ts.kind, se.string_length
7162 ? se.string_length
7163 : expr->ts.u.cl->backend_decl);
7164 tmp = gfc_build_memcpy_call (tmp, se.expr, size);
7165 gfc_add_expr_to_block (&block, tmp);
7167 else
7168 gfc_add_modify (&block, tmp,
7169 fold_convert (TREE_TYPE (tmp), se.expr));
7170 gfc_add_block_to_block (&block, &se.post);
7172 else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
7174 if (expr->expr_type != EXPR_STRUCTURE)
7176 tree dealloc = NULL_TREE;
7177 gfc_init_se (&se, NULL);
7178 gfc_conv_expr (&se, expr);
7179 gfc_add_block_to_block (&block, &se.pre);
7180 /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
7181 expression in a temporary variable and deallocate the allocatable
7182 components. Then we can the copy the expression to the result. */
7183 if (cm->ts.u.derived->attr.alloc_comp
7184 && expr->expr_type != EXPR_VARIABLE)
7186 se.expr = gfc_evaluate_now (se.expr, &block);
7187 dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
7188 expr->rank);
7190 gfc_add_modify (&block, dest,
7191 fold_convert (TREE_TYPE (dest), se.expr));
7192 if (cm->ts.u.derived->attr.alloc_comp
7193 && expr->expr_type != EXPR_NULL)
7195 tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
7196 dest, expr->rank);
7197 gfc_add_expr_to_block (&block, tmp);
7198 if (dealloc != NULL_TREE)
7199 gfc_add_expr_to_block (&block, dealloc);
7201 gfc_add_block_to_block (&block, &se.post);
7203 else
7205 /* Nested constructors. */
7206 tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
7207 gfc_add_expr_to_block (&block, tmp);
7210 else if (gfc_deferred_strlen (cm, &tmp))
7212 tree strlen;
7213 strlen = tmp;
7214 gcc_assert (strlen);
7215 strlen = fold_build3_loc (input_location, COMPONENT_REF,
7216 TREE_TYPE (strlen),
7217 TREE_OPERAND (dest, 0),
7218 strlen, NULL_TREE);
7220 if (expr->expr_type == EXPR_NULL)
7222 tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
7223 gfc_add_modify (&block, dest, tmp);
7224 tmp = build_int_cst (TREE_TYPE (strlen), 0);
7225 gfc_add_modify (&block, strlen, tmp);
7227 else
7229 tree size;
7230 gfc_init_se (&se, NULL);
7231 gfc_conv_expr (&se, expr);
7232 size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
7233 tmp = build_call_expr_loc (input_location,
7234 builtin_decl_explicit (BUILT_IN_MALLOC),
7235 1, size);
7236 gfc_add_modify (&block, dest,
7237 fold_convert (TREE_TYPE (dest), tmp));
7238 gfc_add_modify (&block, strlen, se.string_length);
7239 tmp = gfc_build_memcpy_call (dest, se.expr, size);
7240 gfc_add_expr_to_block (&block, tmp);
7243 else if (!cm->attr.artificial)
7245 /* Scalar component (excluding deferred parameters). */
7246 gfc_init_se (&se, NULL);
7247 gfc_init_se (&lse, NULL);
7249 gfc_conv_expr (&se, expr);
7250 if (cm->ts.type == BT_CHARACTER)
7251 lse.string_length = cm->ts.u.cl->backend_decl;
7252 lse.expr = dest;
7253 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
7254 gfc_add_expr_to_block (&block, tmp);
7256 return gfc_finish_block (&block);
7259 /* Assign a derived type constructor to a variable. */
7261 tree
7262 gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init)
7264 gfc_constructor *c;
7265 gfc_component *cm;
7266 stmtblock_t block;
7267 tree field;
7268 tree tmp;
7270 gfc_start_block (&block);
7271 cm = expr->ts.u.derived->components;
7273 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
7274 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
7275 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
7277 gfc_se se, lse;
7279 gcc_assert (cm->backend_decl == NULL);
7280 gfc_init_se (&se, NULL);
7281 gfc_init_se (&lse, NULL);
7282 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
7283 lse.expr = dest;
7284 gfc_add_modify (&block, lse.expr,
7285 fold_convert (TREE_TYPE (lse.expr), se.expr));
7287 return gfc_finish_block (&block);
7290 for (c = gfc_constructor_first (expr->value.constructor);
7291 c; c = gfc_constructor_next (c), cm = cm->next)
7293 /* Skip absent members in default initializers. */
7294 if (!c->expr && !cm->attr.allocatable)
7295 continue;
7297 field = cm->backend_decl;
7298 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
7299 dest, field, NULL_TREE);
7300 if (!c->expr)
7302 gfc_expr *e = gfc_get_null_expr (NULL);
7303 tmp = gfc_trans_subcomponent_assign (tmp, cm, e, expr->ts.u.derived,
7304 init);
7305 gfc_free_expr (e);
7307 else
7308 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr,
7309 expr->ts.u.derived, init);
7310 gfc_add_expr_to_block (&block, tmp);
7312 return gfc_finish_block (&block);
7315 /* Build an expression for a constructor. If init is nonzero then
7316 this is part of a static variable initializer. */
7318 void
7319 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
7321 gfc_constructor *c;
7322 gfc_component *cm;
7323 tree val;
7324 tree type;
7325 tree tmp;
7326 vec<constructor_elt, va_gc> *v = NULL;
7328 gcc_assert (se->ss == NULL);
7329 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
7330 type = gfc_typenode_for_spec (&expr->ts);
7332 if (!init)
7334 /* Create a temporary variable and fill it in. */
7335 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
7336 /* The symtree in expr is NULL, if the code to generate is for
7337 initializing the static members only. */
7338 tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL);
7339 gfc_add_expr_to_block (&se->pre, tmp);
7340 return;
7343 cm = expr->ts.u.derived->components;
7345 for (c = gfc_constructor_first (expr->value.constructor);
7346 c; c = gfc_constructor_next (c), cm = cm->next)
7348 /* Skip absent members in default initializers and allocatable
7349 components. Although the latter have a default initializer
7350 of EXPR_NULL,... by default, the static nullify is not needed
7351 since this is done every time we come into scope. */
7352 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
7353 continue;
7355 if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
7356 && strcmp (cm->name, "_extends") == 0
7357 && cm->initializer->symtree)
7359 tree vtab;
7360 gfc_symbol *vtabs;
7361 vtabs = cm->initializer->symtree->n.sym;
7362 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
7363 vtab = unshare_expr_without_location (vtab);
7364 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
7366 else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
7368 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
7369 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
7370 fold_convert (TREE_TYPE (cm->backend_decl),
7371 val));
7373 else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
7374 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
7375 fold_convert (TREE_TYPE (cm->backend_decl),
7376 integer_zero_node));
7377 else
7379 val = gfc_conv_initializer (c->expr, &cm->ts,
7380 TREE_TYPE (cm->backend_decl),
7381 cm->attr.dimension, cm->attr.pointer,
7382 cm->attr.proc_pointer);
7383 val = unshare_expr_without_location (val);
7385 /* Append it to the constructor list. */
7386 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
7389 se->expr = build_constructor (type, v);
7390 if (init)
7391 TREE_CONSTANT (se->expr) = 1;
7395 /* Translate a substring expression. */
7397 static void
7398 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
7400 gfc_ref *ref;
7402 ref = expr->ref;
7404 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
7406 se->expr = gfc_build_wide_string_const (expr->ts.kind,
7407 expr->value.character.length,
7408 expr->value.character.string);
7410 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
7411 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
7413 if (ref)
7414 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
7418 /* Entry point for expression translation. Evaluates a scalar quantity.
7419 EXPR is the expression to be translated, and SE is the state structure if
7420 called from within the scalarized. */
7422 void
7423 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
7425 gfc_ss *ss;
7427 ss = se->ss;
7428 if (ss && ss->info->expr == expr
7429 && (ss->info->type == GFC_SS_SCALAR
7430 || ss->info->type == GFC_SS_REFERENCE))
7432 gfc_ss_info *ss_info;
7434 ss_info = ss->info;
7435 /* Substitute a scalar expression evaluated outside the scalarization
7436 loop. */
7437 se->expr = ss_info->data.scalar.value;
7438 if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
7439 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
7441 se->string_length = ss_info->string_length;
7442 gfc_advance_se_ss_chain (se);
7443 return;
7446 /* We need to convert the expressions for the iso_c_binding derived types.
7447 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
7448 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
7449 typespec for the C_PTR and C_FUNPTR symbols, which has already been
7450 updated to be an integer with a kind equal to the size of a (void *). */
7451 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
7452 && expr->ts.u.derived->attr.is_bind_c)
7454 if (expr->expr_type == EXPR_VARIABLE
7455 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
7456 || expr->symtree->n.sym->intmod_sym_id
7457 == ISOCBINDING_NULL_FUNPTR))
7459 /* Set expr_type to EXPR_NULL, which will result in
7460 null_pointer_node being used below. */
7461 expr->expr_type = EXPR_NULL;
7463 else
7465 /* Update the type/kind of the expression to be what the new
7466 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
7467 expr->ts.type = BT_INTEGER;
7468 expr->ts.f90_type = BT_VOID;
7469 expr->ts.kind = gfc_index_integer_kind;
7473 gfc_fix_class_refs (expr);
7475 switch (expr->expr_type)
7477 case EXPR_OP:
7478 gfc_conv_expr_op (se, expr);
7479 break;
7481 case EXPR_FUNCTION:
7482 gfc_conv_function_expr (se, expr);
7483 break;
7485 case EXPR_CONSTANT:
7486 gfc_conv_constant (se, expr);
7487 break;
7489 case EXPR_VARIABLE:
7490 gfc_conv_variable (se, expr);
7491 break;
7493 case EXPR_NULL:
7494 se->expr = null_pointer_node;
7495 break;
7497 case EXPR_SUBSTRING:
7498 gfc_conv_substring_expr (se, expr);
7499 break;
7501 case EXPR_STRUCTURE:
7502 gfc_conv_structure (se, expr, 0);
7503 break;
7505 case EXPR_ARRAY:
7506 gfc_conv_array_constructor_expr (se, expr);
7507 break;
7509 default:
7510 gcc_unreachable ();
7511 break;
7515 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
7516 of an assignment. */
7517 void
7518 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
7520 gfc_conv_expr (se, expr);
7521 /* All numeric lvalues should have empty post chains. If not we need to
7522 figure out a way of rewriting an lvalue so that it has no post chain. */
7523 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
7526 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
7527 numeric expressions. Used for scalar values where inserting cleanup code
7528 is inconvenient. */
7529 void
7530 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
7532 tree val;
7534 gcc_assert (expr->ts.type != BT_CHARACTER);
7535 gfc_conv_expr (se, expr);
7536 if (se->post.head)
7538 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
7539 gfc_add_modify (&se->pre, val, se->expr);
7540 se->expr = val;
7541 gfc_add_block_to_block (&se->pre, &se->post);
7545 /* Helper to translate an expression and convert it to a particular type. */
7546 void
7547 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
7549 gfc_conv_expr_val (se, expr);
7550 se->expr = convert (type, se->expr);
7554 /* Converts an expression so that it can be passed by reference. Scalar
7555 values only. */
7557 void
7558 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
7560 gfc_ss *ss;
7561 tree var;
7563 ss = se->ss;
7564 if (ss && ss->info->expr == expr
7565 && ss->info->type == GFC_SS_REFERENCE)
7567 /* Returns a reference to the scalar evaluated outside the loop
7568 for this case. */
7569 gfc_conv_expr (se, expr);
7571 if (expr->ts.type == BT_CHARACTER
7572 && expr->expr_type != EXPR_FUNCTION)
7573 gfc_conv_string_parameter (se);
7574 else
7575 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
7577 return;
7580 if (expr->ts.type == BT_CHARACTER)
7582 gfc_conv_expr (se, expr);
7583 gfc_conv_string_parameter (se);
7584 return;
7587 if (expr->expr_type == EXPR_VARIABLE)
7589 se->want_pointer = 1;
7590 gfc_conv_expr (se, expr);
7591 if (se->post.head)
7593 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7594 gfc_add_modify (&se->pre, var, se->expr);
7595 gfc_add_block_to_block (&se->pre, &se->post);
7596 se->expr = var;
7598 return;
7601 if (expr->expr_type == EXPR_FUNCTION
7602 && ((expr->value.function.esym
7603 && expr->value.function.esym->result->attr.pointer
7604 && !expr->value.function.esym->result->attr.dimension)
7605 || (!expr->value.function.esym && !expr->ref
7606 && expr->symtree->n.sym->attr.pointer
7607 && !expr->symtree->n.sym->attr.dimension)))
7609 se->want_pointer = 1;
7610 gfc_conv_expr (se, expr);
7611 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7612 gfc_add_modify (&se->pre, var, se->expr);
7613 se->expr = var;
7614 return;
7617 gfc_conv_expr (se, expr);
7619 /* Create a temporary var to hold the value. */
7620 if (TREE_CONSTANT (se->expr))
7622 tree tmp = se->expr;
7623 STRIP_TYPE_NOPS (tmp);
7624 var = build_decl (input_location,
7625 CONST_DECL, NULL, TREE_TYPE (tmp));
7626 DECL_INITIAL (var) = tmp;
7627 TREE_STATIC (var) = 1;
7628 pushdecl (var);
7630 else
7632 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7633 gfc_add_modify (&se->pre, var, se->expr);
7635 gfc_add_block_to_block (&se->pre, &se->post);
7637 /* Take the address of that value. */
7638 se->expr = gfc_build_addr_expr (NULL_TREE, var);
7642 tree
7643 gfc_trans_pointer_assign (gfc_code * code)
7645 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
7649 /* Generate code for a pointer assignment. */
7651 tree
7652 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
7654 gfc_expr *expr1_vptr = NULL;
7655 gfc_se lse;
7656 gfc_se rse;
7657 stmtblock_t block;
7658 tree desc;
7659 tree tmp;
7660 tree decl;
7661 bool scalar;
7662 gfc_ss *ss;
7664 gfc_start_block (&block);
7666 gfc_init_se (&lse, NULL);
7668 /* Check whether the expression is a scalar or not; we cannot use
7669 expr1->rank as it can be nonzero for proc pointers. */
7670 ss = gfc_walk_expr (expr1);
7671 scalar = ss == gfc_ss_terminator;
7672 if (!scalar)
7673 gfc_free_ss_chain (ss);
7675 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
7676 && expr2->expr_type != EXPR_FUNCTION)
7678 gfc_add_data_component (expr2);
7679 /* The following is required as gfc_add_data_component doesn't
7680 update ts.type if there is a tailing REF_ARRAY. */
7681 expr2->ts.type = BT_DERIVED;
7684 if (scalar)
7686 /* Scalar pointers. */
7687 lse.want_pointer = 1;
7688 gfc_conv_expr (&lse, expr1);
7689 gfc_init_se (&rse, NULL);
7690 rse.want_pointer = 1;
7691 gfc_conv_expr (&rse, expr2);
7693 if (expr1->symtree->n.sym->attr.proc_pointer
7694 && expr1->symtree->n.sym->attr.dummy)
7695 lse.expr = build_fold_indirect_ref_loc (input_location,
7696 lse.expr);
7698 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
7699 && expr2->symtree->n.sym->attr.dummy)
7700 rse.expr = build_fold_indirect_ref_loc (input_location,
7701 rse.expr);
7703 gfc_add_block_to_block (&block, &lse.pre);
7704 gfc_add_block_to_block (&block, &rse.pre);
7706 /* For string assignments to unlimited polymorphic pointers add an
7707 assignment of the string_length to the _len component of the
7708 pointer. */
7709 if ((expr1->ts.type == BT_CLASS || expr1->ts.type == BT_DERIVED)
7710 && expr1->ts.u.derived->attr.unlimited_polymorphic
7711 && (expr2->ts.type == BT_CHARACTER ||
7712 ((expr2->ts.type == BT_DERIVED || expr2->ts.type == BT_CLASS)
7713 && expr2->ts.u.derived->attr.unlimited_polymorphic)))
7715 gfc_expr *len_comp;
7716 gfc_se se;
7717 len_comp = gfc_get_len_component (expr1);
7718 gfc_init_se (&se, NULL);
7719 gfc_conv_expr (&se, len_comp);
7721 /* ptr % _len = len (str) */
7722 gfc_add_modify (&block, se.expr, rse.string_length);
7723 lse.string_length = se.expr;
7724 gfc_free_expr (len_comp);
7727 /* Check character lengths if character expression. The test is only
7728 really added if -fbounds-check is enabled. Exclude deferred
7729 character length lefthand sides. */
7730 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
7731 && !expr1->ts.deferred
7732 && !expr1->symtree->n.sym->attr.proc_pointer
7733 && !gfc_is_proc_ptr_comp (expr1))
7735 gcc_assert (expr2->ts.type == BT_CHARACTER);
7736 gcc_assert (lse.string_length && rse.string_length);
7737 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
7738 lse.string_length, rse.string_length,
7739 &block);
7742 /* The assignment to an deferred character length sets the string
7743 length to that of the rhs. */
7744 if (expr1->ts.deferred)
7746 if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
7747 gfc_add_modify (&block, lse.string_length, rse.string_length);
7748 else if (lse.string_length != NULL)
7749 gfc_add_modify (&block, lse.string_length,
7750 build_int_cst (gfc_charlen_type_node, 0));
7753 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS)
7754 rse.expr = gfc_class_data_get (rse.expr);
7756 gfc_add_modify (&block, lse.expr,
7757 fold_convert (TREE_TYPE (lse.expr), rse.expr));
7759 gfc_add_block_to_block (&block, &rse.post);
7760 gfc_add_block_to_block (&block, &lse.post);
7762 else
7764 gfc_ref* remap;
7765 bool rank_remap;
7766 tree strlen_lhs;
7767 tree strlen_rhs = NULL_TREE;
7769 /* Array pointer. Find the last reference on the LHS and if it is an
7770 array section ref, we're dealing with bounds remapping. In this case,
7771 set it to AR_FULL so that gfc_conv_expr_descriptor does
7772 not see it and process the bounds remapping afterwards explicitly. */
7773 for (remap = expr1->ref; remap; remap = remap->next)
7774 if (!remap->next && remap->type == REF_ARRAY
7775 && remap->u.ar.type == AR_SECTION)
7776 break;
7777 rank_remap = (remap && remap->u.ar.end[0]);
7779 gfc_init_se (&lse, NULL);
7780 if (remap)
7781 lse.descriptor_only = 1;
7782 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS
7783 && expr1->ts.type == BT_CLASS)
7784 expr1_vptr = gfc_copy_expr (expr1);
7785 gfc_conv_expr_descriptor (&lse, expr1);
7786 strlen_lhs = lse.string_length;
7787 desc = lse.expr;
7789 if (expr2->expr_type == EXPR_NULL)
7791 /* Just set the data pointer to null. */
7792 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
7794 else if (rank_remap)
7796 /* If we are rank-remapping, just get the RHS's descriptor and
7797 process this later on. */
7798 gfc_init_se (&rse, NULL);
7799 rse.direct_byref = 1;
7800 rse.byref_noassign = 1;
7802 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
7804 gfc_conv_function_expr (&rse, expr2);
7806 if (expr1->ts.type != BT_CLASS)
7807 rse.expr = gfc_class_data_get (rse.expr);
7808 else
7810 gfc_add_block_to_block (&block, &rse.pre);
7811 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
7812 gfc_add_modify (&lse.pre, tmp, rse.expr);
7814 gfc_add_vptr_component (expr1_vptr);
7815 gfc_init_se (&rse, NULL);
7816 rse.want_pointer = 1;
7817 gfc_conv_expr (&rse, expr1_vptr);
7818 gfc_add_modify (&lse.pre, rse.expr,
7819 fold_convert (TREE_TYPE (rse.expr),
7820 gfc_class_vptr_get (tmp)));
7821 rse.expr = gfc_class_data_get (tmp);
7824 else if (expr2->expr_type == EXPR_FUNCTION)
7826 tree bound[GFC_MAX_DIMENSIONS];
7827 int i;
7829 for (i = 0; i < expr2->rank; i++)
7830 bound[i] = NULL_TREE;
7831 tmp = gfc_typenode_for_spec (&expr2->ts);
7832 tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
7833 bound, bound, 0,
7834 GFC_ARRAY_POINTER_CONT, false);
7835 tmp = gfc_create_var (tmp, "ptrtemp");
7836 lse.descriptor_only = 0;
7837 lse.expr = tmp;
7838 lse.direct_byref = 1;
7839 gfc_conv_expr_descriptor (&lse, expr2);
7840 strlen_rhs = lse.string_length;
7841 rse.expr = tmp;
7843 else
7845 gfc_conv_expr_descriptor (&rse, expr2);
7846 strlen_rhs = rse.string_length;
7849 else if (expr2->expr_type == EXPR_VARIABLE)
7851 /* Assign directly to the LHS's descriptor. */
7852 lse.descriptor_only = 0;
7853 lse.direct_byref = 1;
7854 gfc_conv_expr_descriptor (&lse, expr2);
7855 strlen_rhs = lse.string_length;
7857 /* If this is a subreference array pointer assignment, use the rhs
7858 descriptor element size for the lhs span. */
7859 if (expr1->symtree->n.sym->attr.subref_array_pointer)
7861 decl = expr1->symtree->n.sym->backend_decl;
7862 gfc_init_se (&rse, NULL);
7863 rse.descriptor_only = 1;
7864 gfc_conv_expr (&rse, expr2);
7865 tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
7866 tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
7867 if (!INTEGER_CST_P (tmp))
7868 gfc_add_block_to_block (&lse.post, &rse.pre);
7869 gfc_add_modify (&lse.post, GFC_DECL_SPAN(decl), tmp);
7872 else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
7874 gfc_init_se (&rse, NULL);
7875 rse.want_pointer = 1;
7876 gfc_conv_function_expr (&rse, expr2);
7877 if (expr1->ts.type != BT_CLASS)
7879 rse.expr = gfc_class_data_get (rse.expr);
7880 gfc_add_modify (&lse.pre, desc, rse.expr);
7882 else
7884 gfc_add_block_to_block (&block, &rse.pre);
7885 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
7886 gfc_add_modify (&lse.pre, tmp, rse.expr);
7888 gfc_add_vptr_component (expr1_vptr);
7889 gfc_init_se (&rse, NULL);
7890 rse.want_pointer = 1;
7891 gfc_conv_expr (&rse, expr1_vptr);
7892 gfc_add_modify (&lse.pre, rse.expr,
7893 fold_convert (TREE_TYPE (rse.expr),
7894 gfc_class_vptr_get (tmp)));
7895 rse.expr = gfc_class_data_get (tmp);
7896 gfc_add_modify (&lse.pre, desc, rse.expr);
7899 else
7901 /* Assign to a temporary descriptor and then copy that
7902 temporary to the pointer. */
7903 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
7904 lse.descriptor_only = 0;
7905 lse.expr = tmp;
7906 lse.direct_byref = 1;
7907 gfc_conv_expr_descriptor (&lse, expr2);
7908 strlen_rhs = lse.string_length;
7909 gfc_add_modify (&lse.pre, desc, tmp);
7912 if (expr1_vptr)
7913 gfc_free_expr (expr1_vptr);
7915 gfc_add_block_to_block (&block, &lse.pre);
7916 if (rank_remap)
7917 gfc_add_block_to_block (&block, &rse.pre);
7919 /* If we do bounds remapping, update LHS descriptor accordingly. */
7920 if (remap)
7922 int dim;
7923 gcc_assert (remap->u.ar.dimen == expr1->rank);
7925 if (rank_remap)
7927 /* Do rank remapping. We already have the RHS's descriptor
7928 converted in rse and now have to build the correct LHS
7929 descriptor for it. */
7931 tree dtype, data;
7932 tree offs, stride;
7933 tree lbound, ubound;
7935 /* Set dtype. */
7936 dtype = gfc_conv_descriptor_dtype (desc);
7937 tmp = gfc_get_dtype (TREE_TYPE (desc));
7938 gfc_add_modify (&block, dtype, tmp);
7940 /* Copy data pointer. */
7941 data = gfc_conv_descriptor_data_get (rse.expr);
7942 gfc_conv_descriptor_data_set (&block, desc, data);
7944 /* Copy offset but adjust it such that it would correspond
7945 to a lbound of zero. */
7946 offs = gfc_conv_descriptor_offset_get (rse.expr);
7947 for (dim = 0; dim < expr2->rank; ++dim)
7949 stride = gfc_conv_descriptor_stride_get (rse.expr,
7950 gfc_rank_cst[dim]);
7951 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
7952 gfc_rank_cst[dim]);
7953 tmp = fold_build2_loc (input_location, MULT_EXPR,
7954 gfc_array_index_type, stride, lbound);
7955 offs = fold_build2_loc (input_location, PLUS_EXPR,
7956 gfc_array_index_type, offs, tmp);
7958 gfc_conv_descriptor_offset_set (&block, desc, offs);
7960 /* Set the bounds as declared for the LHS and calculate strides as
7961 well as another offset update accordingly. */
7962 stride = gfc_conv_descriptor_stride_get (rse.expr,
7963 gfc_rank_cst[0]);
7964 for (dim = 0; dim < expr1->rank; ++dim)
7966 gfc_se lower_se;
7967 gfc_se upper_se;
7969 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
7971 /* Convert declared bounds. */
7972 gfc_init_se (&lower_se, NULL);
7973 gfc_init_se (&upper_se, NULL);
7974 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
7975 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
7977 gfc_add_block_to_block (&block, &lower_se.pre);
7978 gfc_add_block_to_block (&block, &upper_se.pre);
7980 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
7981 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
7983 lbound = gfc_evaluate_now (lbound, &block);
7984 ubound = gfc_evaluate_now (ubound, &block);
7986 gfc_add_block_to_block (&block, &lower_se.post);
7987 gfc_add_block_to_block (&block, &upper_se.post);
7989 /* Set bounds in descriptor. */
7990 gfc_conv_descriptor_lbound_set (&block, desc,
7991 gfc_rank_cst[dim], lbound);
7992 gfc_conv_descriptor_ubound_set (&block, desc,
7993 gfc_rank_cst[dim], ubound);
7995 /* Set stride. */
7996 stride = gfc_evaluate_now (stride, &block);
7997 gfc_conv_descriptor_stride_set (&block, desc,
7998 gfc_rank_cst[dim], stride);
8000 /* Update offset. */
8001 offs = gfc_conv_descriptor_offset_get (desc);
8002 tmp = fold_build2_loc (input_location, MULT_EXPR,
8003 gfc_array_index_type, lbound, stride);
8004 offs = fold_build2_loc (input_location, MINUS_EXPR,
8005 gfc_array_index_type, offs, tmp);
8006 offs = gfc_evaluate_now (offs, &block);
8007 gfc_conv_descriptor_offset_set (&block, desc, offs);
8009 /* Update stride. */
8010 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
8011 stride = fold_build2_loc (input_location, MULT_EXPR,
8012 gfc_array_index_type, stride, tmp);
8015 else
8017 /* Bounds remapping. Just shift the lower bounds. */
8019 gcc_assert (expr1->rank == expr2->rank);
8021 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
8023 gfc_se lbound_se;
8025 gcc_assert (remap->u.ar.start[dim]);
8026 gcc_assert (!remap->u.ar.end[dim]);
8027 gfc_init_se (&lbound_se, NULL);
8028 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
8030 gfc_add_block_to_block (&block, &lbound_se.pre);
8031 gfc_conv_shift_descriptor_lbound (&block, desc,
8032 dim, lbound_se.expr);
8033 gfc_add_block_to_block (&block, &lbound_se.post);
8038 /* Check string lengths if applicable. The check is only really added
8039 to the output code if -fbounds-check is enabled. */
8040 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
8042 gcc_assert (expr2->ts.type == BT_CHARACTER);
8043 gcc_assert (strlen_lhs && strlen_rhs);
8044 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
8045 strlen_lhs, strlen_rhs, &block);
8048 /* If rank remapping was done, check with -fcheck=bounds that
8049 the target is at least as large as the pointer. */
8050 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
8052 tree lsize, rsize;
8053 tree fault;
8054 const char* msg;
8056 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
8057 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
8059 lsize = gfc_evaluate_now (lsize, &block);
8060 rsize = gfc_evaluate_now (rsize, &block);
8061 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
8062 rsize, lsize);
8064 msg = _("Target of rank remapping is too small (%ld < %ld)");
8065 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
8066 msg, rsize, lsize);
8069 gfc_add_block_to_block (&block, &lse.post);
8070 if (rank_remap)
8071 gfc_add_block_to_block (&block, &rse.post);
8074 return gfc_finish_block (&block);
8078 /* Makes sure se is suitable for passing as a function string parameter. */
8079 /* TODO: Need to check all callers of this function. It may be abused. */
8081 void
8082 gfc_conv_string_parameter (gfc_se * se)
8084 tree type;
8086 if (TREE_CODE (se->expr) == STRING_CST)
8088 type = TREE_TYPE (TREE_TYPE (se->expr));
8089 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
8090 return;
8093 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
8095 if (TREE_CODE (se->expr) != INDIRECT_REF)
8097 type = TREE_TYPE (se->expr);
8098 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
8100 else
8102 type = gfc_get_character_type_len (gfc_default_character_kind,
8103 se->string_length);
8104 type = build_pointer_type (type);
8105 se->expr = gfc_build_addr_expr (type, se->expr);
8109 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
8113 /* Generate code for assignment of scalar variables. Includes character
8114 strings and derived types with allocatable components.
8115 If you know that the LHS has no allocations, set dealloc to false.
8117 DEEP_COPY has no effect if the typespec TS is not a derived type with
8118 allocatable components. Otherwise, if it is set, an explicit copy of each
8119 allocatable component is made. This is necessary as a simple copy of the
8120 whole object would copy array descriptors as is, so that the lhs's
8121 allocatable components would point to the rhs's after the assignment.
8122 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
8123 necessary if the rhs is a non-pointer function, as the allocatable components
8124 are not accessible by other means than the function's result after the
8125 function has returned. It is even more subtle when temporaries are involved,
8126 as the two following examples show:
8127 1. When we evaluate an array constructor, a temporary is created. Thus
8128 there is theoretically no alias possible. However, no deep copy is
8129 made for this temporary, so that if the constructor is made of one or
8130 more variable with allocatable components, those components still point
8131 to the variable's: DEEP_COPY should be set for the assignment from the
8132 temporary to the lhs in that case.
8133 2. When assigning a scalar to an array, we evaluate the scalar value out
8134 of the loop, store it into a temporary variable, and assign from that.
8135 In that case, deep copying when assigning to the temporary would be a
8136 waste of resources; however deep copies should happen when assigning from
8137 the temporary to each array element: again DEEP_COPY should be set for
8138 the assignment from the temporary to the lhs. */
8140 tree
8141 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
8142 bool deep_copy, bool dealloc)
8144 stmtblock_t block;
8145 tree tmp;
8146 tree cond;
8148 gfc_init_block (&block);
8150 if (ts.type == BT_CHARACTER)
8152 tree rlen = NULL;
8153 tree llen = NULL;
8155 if (lse->string_length != NULL_TREE)
8157 gfc_conv_string_parameter (lse);
8158 gfc_add_block_to_block (&block, &lse->pre);
8159 llen = lse->string_length;
8162 if (rse->string_length != NULL_TREE)
8164 gcc_assert (rse->string_length != NULL_TREE);
8165 gfc_conv_string_parameter (rse);
8166 gfc_add_block_to_block (&block, &rse->pre);
8167 rlen = rse->string_length;
8170 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
8171 rse->expr, ts.kind);
8173 else if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
8175 tree tmp_var = NULL_TREE;
8176 cond = NULL_TREE;
8178 /* Are the rhs and the lhs the same? */
8179 if (deep_copy)
8181 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8182 gfc_build_addr_expr (NULL_TREE, lse->expr),
8183 gfc_build_addr_expr (NULL_TREE, rse->expr));
8184 cond = gfc_evaluate_now (cond, &lse->pre);
8187 /* Deallocate the lhs allocated components as long as it is not
8188 the same as the rhs. This must be done following the assignment
8189 to prevent deallocating data that could be used in the rhs
8190 expression. */
8191 if (dealloc)
8193 tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
8194 tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var, 0);
8195 if (deep_copy)
8196 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8197 tmp);
8198 gfc_add_expr_to_block (&lse->post, tmp);
8201 gfc_add_block_to_block (&block, &rse->pre);
8202 gfc_add_block_to_block (&block, &lse->pre);
8204 gfc_add_modify (&block, lse->expr,
8205 fold_convert (TREE_TYPE (lse->expr), rse->expr));
8207 /* Restore pointer address of coarray components. */
8208 if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
8210 tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
8211 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8212 tmp);
8213 gfc_add_expr_to_block (&block, tmp);
8216 /* Do a deep copy if the rhs is a variable, if it is not the
8217 same as the lhs. */
8218 if (deep_copy)
8220 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0);
8221 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8222 tmp);
8223 gfc_add_expr_to_block (&block, tmp);
8226 else if (ts.type == BT_DERIVED || ts.type == BT_CLASS)
8228 gfc_add_block_to_block (&block, &lse->pre);
8229 gfc_add_block_to_block (&block, &rse->pre);
8230 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
8231 TREE_TYPE (lse->expr), rse->expr);
8232 gfc_add_modify (&block, lse->expr, tmp);
8234 else
8236 gfc_add_block_to_block (&block, &lse->pre);
8237 gfc_add_block_to_block (&block, &rse->pre);
8239 gfc_add_modify (&block, lse->expr,
8240 fold_convert (TREE_TYPE (lse->expr), rse->expr));
8243 gfc_add_block_to_block (&block, &lse->post);
8244 gfc_add_block_to_block (&block, &rse->post);
8246 return gfc_finish_block (&block);
8250 /* There are quite a lot of restrictions on the optimisation in using an
8251 array function assign without a temporary. */
8253 static bool
8254 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
8256 gfc_ref * ref;
8257 bool seen_array_ref;
8258 bool c = false;
8259 gfc_symbol *sym = expr1->symtree->n.sym;
8261 /* Play it safe with class functions assigned to a derived type. */
8262 if (gfc_is_alloc_class_array_function (expr2)
8263 && expr1->ts.type == BT_DERIVED)
8264 return true;
8266 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
8267 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
8268 return true;
8270 /* Elemental functions are scalarized so that they don't need a
8271 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
8272 they would need special treatment in gfc_trans_arrayfunc_assign. */
8273 if (expr2->value.function.esym != NULL
8274 && expr2->value.function.esym->attr.elemental)
8275 return true;
8277 /* Need a temporary if rhs is not FULL or a contiguous section. */
8278 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
8279 return true;
8281 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
8282 if (gfc_ref_needs_temporary_p (expr1->ref))
8283 return true;
8285 /* Functions returning pointers or allocatables need temporaries. */
8286 c = expr2->value.function.esym
8287 ? (expr2->value.function.esym->attr.pointer
8288 || expr2->value.function.esym->attr.allocatable)
8289 : (expr2->symtree->n.sym->attr.pointer
8290 || expr2->symtree->n.sym->attr.allocatable);
8291 if (c)
8292 return true;
8294 /* Character array functions need temporaries unless the
8295 character lengths are the same. */
8296 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
8298 if (expr1->ts.u.cl->length == NULL
8299 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8300 return true;
8302 if (expr2->ts.u.cl->length == NULL
8303 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8304 return true;
8306 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
8307 expr2->ts.u.cl->length->value.integer) != 0)
8308 return true;
8311 /* Check that no LHS component references appear during an array
8312 reference. This is needed because we do not have the means to
8313 span any arbitrary stride with an array descriptor. This check
8314 is not needed for the rhs because the function result has to be
8315 a complete type. */
8316 seen_array_ref = false;
8317 for (ref = expr1->ref; ref; ref = ref->next)
8319 if (ref->type == REF_ARRAY)
8320 seen_array_ref= true;
8321 else if (ref->type == REF_COMPONENT && seen_array_ref)
8322 return true;
8325 /* Check for a dependency. */
8326 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
8327 expr2->value.function.esym,
8328 expr2->value.function.actual,
8329 NOT_ELEMENTAL))
8330 return true;
8332 /* If we have reached here with an intrinsic function, we do not
8333 need a temporary except in the particular case that reallocation
8334 on assignment is active and the lhs is allocatable and a target. */
8335 if (expr2->value.function.isym)
8336 return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target);
8338 /* If the LHS is a dummy, we need a temporary if it is not
8339 INTENT(OUT). */
8340 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
8341 return true;
8343 /* If the lhs has been host_associated, is in common, a pointer or is
8344 a target and the function is not using a RESULT variable, aliasing
8345 can occur and a temporary is needed. */
8346 if ((sym->attr.host_assoc
8347 || sym->attr.in_common
8348 || sym->attr.pointer
8349 || sym->attr.cray_pointee
8350 || sym->attr.target)
8351 && expr2->symtree != NULL
8352 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
8353 return true;
8355 /* A PURE function can unconditionally be called without a temporary. */
8356 if (expr2->value.function.esym != NULL
8357 && expr2->value.function.esym->attr.pure)
8358 return false;
8360 /* Implicit_pure functions are those which could legally be declared
8361 to be PURE. */
8362 if (expr2->value.function.esym != NULL
8363 && expr2->value.function.esym->attr.implicit_pure)
8364 return false;
8366 if (!sym->attr.use_assoc
8367 && !sym->attr.in_common
8368 && !sym->attr.pointer
8369 && !sym->attr.target
8370 && !sym->attr.cray_pointee
8371 && expr2->value.function.esym)
8373 /* A temporary is not needed if the function is not contained and
8374 the variable is local or host associated and not a pointer or
8375 a target. */
8376 if (!expr2->value.function.esym->attr.contained)
8377 return false;
8379 /* A temporary is not needed if the lhs has never been host
8380 associated and the procedure is contained. */
8381 else if (!sym->attr.host_assoc)
8382 return false;
8384 /* A temporary is not needed if the variable is local and not
8385 a pointer, a target or a result. */
8386 if (sym->ns->parent
8387 && expr2->value.function.esym->ns == sym->ns->parent)
8388 return false;
8391 /* Default to temporary use. */
8392 return true;
8396 /* Provide the loop info so that the lhs descriptor can be built for
8397 reallocatable assignments from extrinsic function calls. */
8399 static void
8400 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
8401 gfc_loopinfo *loop)
8403 /* Signal that the function call should not be made by
8404 gfc_conv_loop_setup. */
8405 se->ss->is_alloc_lhs = 1;
8406 gfc_init_loopinfo (loop);
8407 gfc_add_ss_to_loop (loop, *ss);
8408 gfc_add_ss_to_loop (loop, se->ss);
8409 gfc_conv_ss_startstride (loop);
8410 gfc_conv_loop_setup (loop, where);
8411 gfc_copy_loopinfo_to_se (se, loop);
8412 gfc_add_block_to_block (&se->pre, &loop->pre);
8413 gfc_add_block_to_block (&se->pre, &loop->post);
8414 se->ss->is_alloc_lhs = 0;
8418 /* For assignment to a reallocatable lhs from intrinsic functions,
8419 replace the se.expr (ie. the result) with a temporary descriptor.
8420 Null the data field so that the library allocates space for the
8421 result. Free the data of the original descriptor after the function,
8422 in case it appears in an argument expression and transfer the
8423 result to the original descriptor. */
8425 static void
8426 fcncall_realloc_result (gfc_se *se, int rank)
8428 tree desc;
8429 tree res_desc;
8430 tree tmp;
8431 tree offset;
8432 tree zero_cond;
8433 int n;
8435 /* Use the allocation done by the library. Substitute the lhs
8436 descriptor with a copy, whose data field is nulled.*/
8437 desc = build_fold_indirect_ref_loc (input_location, se->expr);
8438 if (POINTER_TYPE_P (TREE_TYPE (desc)))
8439 desc = build_fold_indirect_ref_loc (input_location, desc);
8441 /* Unallocated, the descriptor does not have a dtype. */
8442 tmp = gfc_conv_descriptor_dtype (desc);
8443 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
8445 res_desc = gfc_evaluate_now (desc, &se->pre);
8446 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
8447 se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
8449 /* Free the lhs after the function call and copy the result data to
8450 the lhs descriptor. */
8451 tmp = gfc_conv_descriptor_data_get (desc);
8452 zero_cond = fold_build2_loc (input_location, EQ_EXPR,
8453 boolean_type_node, tmp,
8454 build_int_cst (TREE_TYPE (tmp), 0));
8455 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
8456 tmp = gfc_call_free (tmp);
8457 gfc_add_expr_to_block (&se->post, tmp);
8459 tmp = gfc_conv_descriptor_data_get (res_desc);
8460 gfc_conv_descriptor_data_set (&se->post, desc, tmp);
8462 /* Check that the shapes are the same between lhs and expression. */
8463 for (n = 0 ; n < rank; n++)
8465 tree tmp1;
8466 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
8467 tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
8468 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8469 gfc_array_index_type, tmp, tmp1);
8470 tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
8471 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8472 gfc_array_index_type, tmp, tmp1);
8473 tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
8474 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8475 gfc_array_index_type, tmp, tmp1);
8476 tmp = fold_build2_loc (input_location, NE_EXPR,
8477 boolean_type_node, tmp,
8478 gfc_index_zero_node);
8479 tmp = gfc_evaluate_now (tmp, &se->post);
8480 zero_cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
8481 boolean_type_node, tmp,
8482 zero_cond);
8485 /* 'zero_cond' being true is equal to lhs not being allocated or the
8486 shapes being different. */
8487 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
8489 /* Now reset the bounds returned from the function call to bounds based
8490 on the lhs lbounds, except where the lhs is not allocated or the shapes
8491 of 'variable and 'expr' are different. Set the offset accordingly. */
8492 offset = gfc_index_zero_node;
8493 for (n = 0 ; n < rank; n++)
8495 tree lbound;
8497 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
8498 lbound = fold_build3_loc (input_location, COND_EXPR,
8499 gfc_array_index_type, zero_cond,
8500 gfc_index_one_node, lbound);
8501 lbound = gfc_evaluate_now (lbound, &se->post);
8503 tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
8504 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8505 gfc_array_index_type, tmp, lbound);
8506 gfc_conv_descriptor_lbound_set (&se->post, desc,
8507 gfc_rank_cst[n], lbound);
8508 gfc_conv_descriptor_ubound_set (&se->post, desc,
8509 gfc_rank_cst[n], tmp);
8511 /* Set stride and accumulate the offset. */
8512 tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
8513 gfc_conv_descriptor_stride_set (&se->post, desc,
8514 gfc_rank_cst[n], tmp);
8515 tmp = fold_build2_loc (input_location, MULT_EXPR,
8516 gfc_array_index_type, lbound, tmp);
8517 offset = fold_build2_loc (input_location, MINUS_EXPR,
8518 gfc_array_index_type, offset, tmp);
8519 offset = gfc_evaluate_now (offset, &se->post);
8522 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
8527 /* Try to translate array(:) = func (...), where func is a transformational
8528 array function, without using a temporary. Returns NULL if this isn't the
8529 case. */
8531 static tree
8532 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
8534 gfc_se se;
8535 gfc_ss *ss = NULL;
8536 gfc_component *comp = NULL;
8537 gfc_loopinfo loop;
8539 if (arrayfunc_assign_needs_temporary (expr1, expr2))
8540 return NULL;
8542 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
8543 functions. */
8544 comp = gfc_get_proc_ptr_comp (expr2);
8545 gcc_assert (expr2->value.function.isym
8546 || (comp && comp->attr.dimension)
8547 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
8548 && expr2->value.function.esym->result->attr.dimension));
8550 gfc_init_se (&se, NULL);
8551 gfc_start_block (&se.pre);
8552 se.want_pointer = 1;
8554 gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
8556 if (expr1->ts.type == BT_DERIVED
8557 && expr1->ts.u.derived->attr.alloc_comp)
8559 tree tmp;
8560 tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, se.expr,
8561 expr1->rank);
8562 gfc_add_expr_to_block (&se.pre, tmp);
8565 se.direct_byref = 1;
8566 se.ss = gfc_walk_expr (expr2);
8567 gcc_assert (se.ss != gfc_ss_terminator);
8569 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
8570 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
8571 Clearly, this cannot be done for an allocatable function result, since
8572 the shape of the result is unknown and, in any case, the function must
8573 correctly take care of the reallocation internally. For intrinsic
8574 calls, the array data is freed and the library takes care of allocation.
8575 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
8576 to the library. */
8577 if (flag_realloc_lhs
8578 && gfc_is_reallocatable_lhs (expr1)
8579 && !gfc_expr_attr (expr1).codimension
8580 && !gfc_is_coindexed (expr1)
8581 && !(expr2->value.function.esym
8582 && expr2->value.function.esym->result->attr.allocatable))
8584 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
8586 if (!expr2->value.function.isym)
8588 ss = gfc_walk_expr (expr1);
8589 gcc_assert (ss != gfc_ss_terminator);
8591 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
8592 ss->is_alloc_lhs = 1;
8594 else
8595 fcncall_realloc_result (&se, expr1->rank);
8598 gfc_conv_function_expr (&se, expr2);
8599 gfc_add_block_to_block (&se.pre, &se.post);
8601 if (ss)
8602 gfc_cleanup_loop (&loop);
8603 else
8604 gfc_free_ss_chain (se.ss);
8606 return gfc_finish_block (&se.pre);
8610 /* Try to efficiently translate array(:) = 0. Return NULL if this
8611 can't be done. */
8613 static tree
8614 gfc_trans_zero_assign (gfc_expr * expr)
8616 tree dest, len, type;
8617 tree tmp;
8618 gfc_symbol *sym;
8620 sym = expr->symtree->n.sym;
8621 dest = gfc_get_symbol_decl (sym);
8623 type = TREE_TYPE (dest);
8624 if (POINTER_TYPE_P (type))
8625 type = TREE_TYPE (type);
8626 if (!GFC_ARRAY_TYPE_P (type))
8627 return NULL_TREE;
8629 /* Determine the length of the array. */
8630 len = GFC_TYPE_ARRAY_SIZE (type);
8631 if (!len || TREE_CODE (len) != INTEGER_CST)
8632 return NULL_TREE;
8634 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
8635 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
8636 fold_convert (gfc_array_index_type, tmp));
8638 /* If we are zeroing a local array avoid taking its address by emitting
8639 a = {} instead. */
8640 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
8641 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
8642 dest, build_constructor (TREE_TYPE (dest),
8643 NULL));
8645 /* Convert arguments to the correct types. */
8646 dest = fold_convert (pvoid_type_node, dest);
8647 len = fold_convert (size_type_node, len);
8649 /* Construct call to __builtin_memset. */
8650 tmp = build_call_expr_loc (input_location,
8651 builtin_decl_explicit (BUILT_IN_MEMSET),
8652 3, dest, integer_zero_node, len);
8653 return fold_convert (void_type_node, tmp);
8657 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
8658 that constructs the call to __builtin_memcpy. */
8660 tree
8661 gfc_build_memcpy_call (tree dst, tree src, tree len)
8663 tree tmp;
8665 /* Convert arguments to the correct types. */
8666 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
8667 dst = gfc_build_addr_expr (pvoid_type_node, dst);
8668 else
8669 dst = fold_convert (pvoid_type_node, dst);
8671 if (!POINTER_TYPE_P (TREE_TYPE (src)))
8672 src = gfc_build_addr_expr (pvoid_type_node, src);
8673 else
8674 src = fold_convert (pvoid_type_node, src);
8676 len = fold_convert (size_type_node, len);
8678 /* Construct call to __builtin_memcpy. */
8679 tmp = build_call_expr_loc (input_location,
8680 builtin_decl_explicit (BUILT_IN_MEMCPY),
8681 3, dst, src, len);
8682 return fold_convert (void_type_node, tmp);
8686 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
8687 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
8688 source/rhs, both are gfc_full_array_ref_p which have been checked for
8689 dependencies. */
8691 static tree
8692 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
8694 tree dst, dlen, dtype;
8695 tree src, slen, stype;
8696 tree tmp;
8698 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
8699 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
8701 dtype = TREE_TYPE (dst);
8702 if (POINTER_TYPE_P (dtype))
8703 dtype = TREE_TYPE (dtype);
8704 stype = TREE_TYPE (src);
8705 if (POINTER_TYPE_P (stype))
8706 stype = TREE_TYPE (stype);
8708 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
8709 return NULL_TREE;
8711 /* Determine the lengths of the arrays. */
8712 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
8713 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
8714 return NULL_TREE;
8715 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
8716 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8717 dlen, fold_convert (gfc_array_index_type, tmp));
8719 slen = GFC_TYPE_ARRAY_SIZE (stype);
8720 if (!slen || TREE_CODE (slen) != INTEGER_CST)
8721 return NULL_TREE;
8722 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
8723 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8724 slen, fold_convert (gfc_array_index_type, tmp));
8726 /* Sanity check that they are the same. This should always be
8727 the case, as we should already have checked for conformance. */
8728 if (!tree_int_cst_equal (slen, dlen))
8729 return NULL_TREE;
8731 return gfc_build_memcpy_call (dst, src, dlen);
8735 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
8736 this can't be done. EXPR1 is the destination/lhs for which
8737 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
8739 static tree
8740 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
8742 unsigned HOST_WIDE_INT nelem;
8743 tree dst, dtype;
8744 tree src, stype;
8745 tree len;
8746 tree tmp;
8748 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
8749 if (nelem == 0)
8750 return NULL_TREE;
8752 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
8753 dtype = TREE_TYPE (dst);
8754 if (POINTER_TYPE_P (dtype))
8755 dtype = TREE_TYPE (dtype);
8756 if (!GFC_ARRAY_TYPE_P (dtype))
8757 return NULL_TREE;
8759 /* Determine the lengths of the array. */
8760 len = GFC_TYPE_ARRAY_SIZE (dtype);
8761 if (!len || TREE_CODE (len) != INTEGER_CST)
8762 return NULL_TREE;
8764 /* Confirm that the constructor is the same size. */
8765 if (compare_tree_int (len, nelem) != 0)
8766 return NULL_TREE;
8768 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
8769 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
8770 fold_convert (gfc_array_index_type, tmp));
8772 stype = gfc_typenode_for_spec (&expr2->ts);
8773 src = gfc_build_constant_array_constructor (expr2, stype);
8775 stype = TREE_TYPE (src);
8776 if (POINTER_TYPE_P (stype))
8777 stype = TREE_TYPE (stype);
8779 return gfc_build_memcpy_call (dst, src, len);
8783 /* Tells whether the expression is to be treated as a variable reference. */
8785 static bool
8786 expr_is_variable (gfc_expr *expr)
8788 gfc_expr *arg;
8789 gfc_component *comp;
8790 gfc_symbol *func_ifc;
8792 if (expr->expr_type == EXPR_VARIABLE)
8793 return true;
8795 arg = gfc_get_noncopying_intrinsic_argument (expr);
8796 if (arg)
8798 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
8799 return expr_is_variable (arg);
8802 /* A data-pointer-returning function should be considered as a variable
8803 too. */
8804 if (expr->expr_type == EXPR_FUNCTION
8805 && expr->ref == NULL)
8807 if (expr->value.function.isym != NULL)
8808 return false;
8810 if (expr->value.function.esym != NULL)
8812 func_ifc = expr->value.function.esym;
8813 goto found_ifc;
8815 else
8817 gcc_assert (expr->symtree);
8818 func_ifc = expr->symtree->n.sym;
8819 goto found_ifc;
8822 gcc_unreachable ();
8825 comp = gfc_get_proc_ptr_comp (expr);
8826 if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
8827 && comp)
8829 func_ifc = comp->ts.interface;
8830 goto found_ifc;
8833 if (expr->expr_type == EXPR_COMPCALL)
8835 gcc_assert (!expr->value.compcall.tbp->is_generic);
8836 func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
8837 goto found_ifc;
8840 return false;
8842 found_ifc:
8843 gcc_assert (func_ifc->attr.function
8844 && func_ifc->result != NULL);
8845 return func_ifc->result->attr.pointer;
8849 /* Is the lhs OK for automatic reallocation? */
8851 static bool
8852 is_scalar_reallocatable_lhs (gfc_expr *expr)
8854 gfc_ref * ref;
8856 /* An allocatable variable with no reference. */
8857 if (expr->symtree->n.sym->attr.allocatable
8858 && !expr->ref)
8859 return true;
8861 /* All that can be left are allocatable components. */
8862 if ((expr->symtree->n.sym->ts.type != BT_DERIVED
8863 && expr->symtree->n.sym->ts.type != BT_CLASS)
8864 || !expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
8865 return false;
8867 /* Find an allocatable component ref last. */
8868 for (ref = expr->ref; ref; ref = ref->next)
8869 if (ref->type == REF_COMPONENT
8870 && !ref->next
8871 && ref->u.c.component->attr.allocatable)
8872 return true;
8874 return false;
8878 /* Allocate or reallocate scalar lhs, as necessary. */
8880 static void
8881 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
8882 tree string_length,
8883 gfc_expr *expr1,
8884 gfc_expr *expr2)
8887 tree cond;
8888 tree tmp;
8889 tree size;
8890 tree size_in_bytes;
8891 tree jump_label1;
8892 tree jump_label2;
8893 gfc_se lse;
8894 gfc_ref *ref;
8896 if (!expr1 || expr1->rank)
8897 return;
8899 if (!expr2 || expr2->rank)
8900 return;
8902 for (ref = expr1->ref; ref; ref = ref->next)
8903 if (ref->type == REF_SUBSTRING)
8904 return;
8906 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
8908 /* Since this is a scalar lhs, we can afford to do this. That is,
8909 there is no risk of side effects being repeated. */
8910 gfc_init_se (&lse, NULL);
8911 lse.want_pointer = 1;
8912 gfc_conv_expr (&lse, expr1);
8914 jump_label1 = gfc_build_label_decl (NULL_TREE);
8915 jump_label2 = gfc_build_label_decl (NULL_TREE);
8917 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
8918 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
8919 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
8920 lse.expr, tmp);
8921 tmp = build3_v (COND_EXPR, cond,
8922 build1_v (GOTO_EXPR, jump_label1),
8923 build_empty_stmt (input_location));
8924 gfc_add_expr_to_block (block, tmp);
8926 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
8928 /* Use the rhs string length and the lhs element size. */
8929 size = string_length;
8930 tmp = TREE_TYPE (gfc_typenode_for_spec (&expr1->ts));
8931 tmp = TYPE_SIZE_UNIT (tmp);
8932 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
8933 TREE_TYPE (tmp), tmp,
8934 fold_convert (TREE_TYPE (tmp), size));
8936 else
8938 /* Otherwise use the length in bytes of the rhs. */
8939 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
8940 size_in_bytes = size;
8943 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
8944 size_in_bytes, size_one_node);
8946 if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
8948 tmp = build_call_expr_loc (input_location,
8949 builtin_decl_explicit (BUILT_IN_CALLOC),
8950 2, build_one_cst (size_type_node),
8951 size_in_bytes);
8952 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
8953 gfc_add_modify (block, lse.expr, tmp);
8955 else
8957 tmp = build_call_expr_loc (input_location,
8958 builtin_decl_explicit (BUILT_IN_MALLOC),
8959 1, size_in_bytes);
8960 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
8961 gfc_add_modify (block, lse.expr, tmp);
8964 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
8966 /* Deferred characters need checking for lhs and rhs string
8967 length. Other deferred parameter variables will have to
8968 come here too. */
8969 tmp = build1_v (GOTO_EXPR, jump_label2);
8970 gfc_add_expr_to_block (block, tmp);
8972 tmp = build1_v (LABEL_EXPR, jump_label1);
8973 gfc_add_expr_to_block (block, tmp);
8975 /* For a deferred length character, reallocate if lengths of lhs and
8976 rhs are different. */
8977 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
8979 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8980 lse.string_length, size);
8981 /* Jump past the realloc if the lengths are the same. */
8982 tmp = build3_v (COND_EXPR, cond,
8983 build1_v (GOTO_EXPR, jump_label2),
8984 build_empty_stmt (input_location));
8985 gfc_add_expr_to_block (block, tmp);
8986 tmp = build_call_expr_loc (input_location,
8987 builtin_decl_explicit (BUILT_IN_REALLOC),
8988 2, fold_convert (pvoid_type_node, lse.expr),
8989 size_in_bytes);
8990 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
8991 gfc_add_modify (block, lse.expr, tmp);
8992 tmp = build1_v (LABEL_EXPR, jump_label2);
8993 gfc_add_expr_to_block (block, tmp);
8995 /* Update the lhs character length. */
8996 size = string_length;
8997 gfc_add_modify (block, lse.string_length, size);
9001 /* Check for assignments of the type
9003 a = a + 4
9005 to make sure we do not check for reallocation unneccessarily. */
9008 static bool
9009 is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
9011 gfc_actual_arglist *a;
9012 gfc_expr *e1, *e2;
9014 switch (expr2->expr_type)
9016 case EXPR_VARIABLE:
9017 return gfc_dep_compare_expr (expr1, expr2) == 0;
9019 case EXPR_FUNCTION:
9020 if (expr2->value.function.esym
9021 && expr2->value.function.esym->attr.elemental)
9023 for (a = expr2->value.function.actual; a != NULL; a = a->next)
9025 e1 = a->expr;
9026 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
9027 return false;
9029 return true;
9031 else if (expr2->value.function.isym
9032 && expr2->value.function.isym->elemental)
9034 for (a = expr2->value.function.actual; a != NULL; a = a->next)
9036 e1 = a->expr;
9037 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
9038 return false;
9040 return true;
9043 break;
9045 case EXPR_OP:
9046 switch (expr2->value.op.op)
9048 case INTRINSIC_NOT:
9049 case INTRINSIC_UPLUS:
9050 case INTRINSIC_UMINUS:
9051 case INTRINSIC_PARENTHESES:
9052 return is_runtime_conformable (expr1, expr2->value.op.op1);
9054 case INTRINSIC_PLUS:
9055 case INTRINSIC_MINUS:
9056 case INTRINSIC_TIMES:
9057 case INTRINSIC_DIVIDE:
9058 case INTRINSIC_POWER:
9059 case INTRINSIC_AND:
9060 case INTRINSIC_OR:
9061 case INTRINSIC_EQV:
9062 case INTRINSIC_NEQV:
9063 case INTRINSIC_EQ:
9064 case INTRINSIC_NE:
9065 case INTRINSIC_GT:
9066 case INTRINSIC_GE:
9067 case INTRINSIC_LT:
9068 case INTRINSIC_LE:
9069 case INTRINSIC_EQ_OS:
9070 case INTRINSIC_NE_OS:
9071 case INTRINSIC_GT_OS:
9072 case INTRINSIC_GE_OS:
9073 case INTRINSIC_LT_OS:
9074 case INTRINSIC_LE_OS:
9076 e1 = expr2->value.op.op1;
9077 e2 = expr2->value.op.op2;
9079 if (e1->rank == 0 && e2->rank > 0)
9080 return is_runtime_conformable (expr1, e2);
9081 else if (e1->rank > 0 && e2->rank == 0)
9082 return is_runtime_conformable (expr1, e1);
9083 else if (e1->rank > 0 && e2->rank > 0)
9084 return is_runtime_conformable (expr1, e1)
9085 && is_runtime_conformable (expr1, e2);
9086 break;
9088 default:
9089 break;
9093 break;
9095 default:
9096 break;
9098 return false;
9101 /* Subroutine of gfc_trans_assignment that actually scalarizes the
9102 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
9103 init_flag indicates initialization expressions and dealloc that no
9104 deallocate prior assignment is needed (if in doubt, set true). */
9106 static tree
9107 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
9108 bool dealloc)
9110 gfc_se lse;
9111 gfc_se rse;
9112 gfc_ss *lss;
9113 gfc_ss *lss_section;
9114 gfc_ss *rss;
9115 gfc_loopinfo loop;
9116 tree tmp;
9117 stmtblock_t block;
9118 stmtblock_t body;
9119 bool l_is_temp;
9120 bool scalar_to_array;
9121 tree string_length;
9122 int n;
9124 /* Assignment of the form lhs = rhs. */
9125 gfc_start_block (&block);
9127 gfc_init_se (&lse, NULL);
9128 gfc_init_se (&rse, NULL);
9130 /* Walk the lhs. */
9131 lss = gfc_walk_expr (expr1);
9132 if (gfc_is_reallocatable_lhs (expr1)
9133 && !(expr2->expr_type == EXPR_FUNCTION
9134 && expr2->value.function.isym != NULL))
9135 lss->is_alloc_lhs = 1;
9136 rss = NULL;
9138 if ((expr1->ts.type == BT_DERIVED)
9139 && (gfc_is_alloc_class_array_function (expr2)
9140 || gfc_is_alloc_class_scalar_function (expr2)))
9141 expr2->must_finalize = 1;
9143 if (lss != gfc_ss_terminator)
9145 /* The assignment needs scalarization. */
9146 lss_section = lss;
9148 /* Find a non-scalar SS from the lhs. */
9149 while (lss_section != gfc_ss_terminator
9150 && lss_section->info->type != GFC_SS_SECTION)
9151 lss_section = lss_section->next;
9153 gcc_assert (lss_section != gfc_ss_terminator);
9155 /* Initialize the scalarizer. */
9156 gfc_init_loopinfo (&loop);
9158 /* Walk the rhs. */
9159 rss = gfc_walk_expr (expr2);
9160 if (rss == gfc_ss_terminator)
9161 /* The rhs is scalar. Add a ss for the expression. */
9162 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
9164 /* Associate the SS with the loop. */
9165 gfc_add_ss_to_loop (&loop, lss);
9166 gfc_add_ss_to_loop (&loop, rss);
9168 /* Calculate the bounds of the scalarization. */
9169 gfc_conv_ss_startstride (&loop);
9170 /* Enable loop reversal. */
9171 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
9172 loop.reverse[n] = GFC_ENABLE_REVERSE;
9173 /* Resolve any data dependencies in the statement. */
9174 gfc_conv_resolve_dependencies (&loop, lss, rss);
9175 /* Setup the scalarizing loops. */
9176 gfc_conv_loop_setup (&loop, &expr2->where);
9178 /* Setup the gfc_se structures. */
9179 gfc_copy_loopinfo_to_se (&lse, &loop);
9180 gfc_copy_loopinfo_to_se (&rse, &loop);
9182 rse.ss = rss;
9183 gfc_mark_ss_chain_used (rss, 1);
9184 if (loop.temp_ss == NULL)
9186 lse.ss = lss;
9187 gfc_mark_ss_chain_used (lss, 1);
9189 else
9191 lse.ss = loop.temp_ss;
9192 gfc_mark_ss_chain_used (lss, 3);
9193 gfc_mark_ss_chain_used (loop.temp_ss, 3);
9196 /* Allow the scalarizer to workshare array assignments. */
9197 if ((ompws_flags & OMPWS_WORKSHARE_FLAG) && loop.temp_ss == NULL)
9198 ompws_flags |= OMPWS_SCALARIZER_WS;
9200 /* Start the scalarized loop body. */
9201 gfc_start_scalarized_body (&loop, &body);
9203 else
9204 gfc_init_block (&body);
9206 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
9208 /* Translate the expression. */
9209 gfc_conv_expr (&rse, expr2);
9211 /* Deal with the case of a scalar class function assigned to a derived type. */
9212 if (gfc_is_alloc_class_scalar_function (expr2)
9213 && expr1->ts.type == BT_DERIVED)
9215 rse.expr = gfc_class_data_get (rse.expr);
9216 rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
9219 /* Stabilize a string length for temporaries. */
9220 if (expr2->ts.type == BT_CHARACTER)
9221 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
9222 else
9223 string_length = NULL_TREE;
9225 if (l_is_temp)
9227 gfc_conv_tmp_array_ref (&lse);
9228 if (expr2->ts.type == BT_CHARACTER)
9229 lse.string_length = string_length;
9231 else
9232 gfc_conv_expr (&lse, expr1);
9234 /* Assignments of scalar derived types with allocatable components
9235 to arrays must be done with a deep copy and the rhs temporary
9236 must have its components deallocated afterwards. */
9237 scalar_to_array = (expr2->ts.type == BT_DERIVED
9238 && expr2->ts.u.derived->attr.alloc_comp
9239 && !expr_is_variable (expr2)
9240 && expr1->rank && !expr2->rank);
9241 scalar_to_array |= (expr1->ts.type == BT_DERIVED
9242 && expr1->rank
9243 && expr1->ts.u.derived->attr.alloc_comp
9244 && gfc_is_alloc_class_scalar_function (expr2));
9245 if (scalar_to_array && dealloc)
9247 tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
9248 gfc_prepend_expr_to_block (&loop.post, tmp);
9251 /* When assigning a character function result to a deferred-length variable,
9252 the function call must happen before the (re)allocation of the lhs -
9253 otherwise the character length of the result is not known.
9254 NOTE: This relies on having the exact dependence of the length type
9255 parameter available to the caller; gfortran saves it in the .mod files. */
9256 if (flag_realloc_lhs && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred)
9257 gfc_add_block_to_block (&block, &rse.pre);
9259 /* Nullify the allocatable components corresponding to those of the lhs
9260 derived type, so that the finalization of the function result does not
9261 affect the lhs of the assignment. Prepend is used to ensure that the
9262 nullification occurs before the call to the finalizer. In the case of
9263 a scalar to array assignment, this is done in gfc_trans_scalar_assign
9264 as part of the deep copy. */
9265 if (!scalar_to_array && (expr1->ts.type == BT_DERIVED)
9266 && (gfc_is_alloc_class_array_function (expr2)
9267 || gfc_is_alloc_class_scalar_function (expr2)))
9269 tmp = rse.expr;
9270 tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
9271 gfc_prepend_expr_to_block (&rse.post, tmp);
9272 if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
9273 gfc_add_block_to_block (&loop.post, &rse.post);
9276 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
9277 expr_is_variable (expr2) || scalar_to_array
9278 || expr2->expr_type == EXPR_ARRAY,
9279 !(l_is_temp || init_flag) && dealloc);
9280 gfc_add_expr_to_block (&body, tmp);
9282 if (lss == gfc_ss_terminator)
9284 /* F2003: Add the code for reallocation on assignment. */
9285 if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1))
9286 alloc_scalar_allocatable_for_assignment (&block, string_length,
9287 expr1, expr2);
9289 /* Use the scalar assignment as is. */
9290 gfc_add_block_to_block (&block, &body);
9292 else
9294 gcc_assert (lse.ss == gfc_ss_terminator
9295 && rse.ss == gfc_ss_terminator);
9297 if (l_is_temp)
9299 gfc_trans_scalarized_loop_boundary (&loop, &body);
9301 /* We need to copy the temporary to the actual lhs. */
9302 gfc_init_se (&lse, NULL);
9303 gfc_init_se (&rse, NULL);
9304 gfc_copy_loopinfo_to_se (&lse, &loop);
9305 gfc_copy_loopinfo_to_se (&rse, &loop);
9307 rse.ss = loop.temp_ss;
9308 lse.ss = lss;
9310 gfc_conv_tmp_array_ref (&rse);
9311 gfc_conv_expr (&lse, expr1);
9313 gcc_assert (lse.ss == gfc_ss_terminator
9314 && rse.ss == gfc_ss_terminator);
9316 if (expr2->ts.type == BT_CHARACTER)
9317 rse.string_length = string_length;
9319 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
9320 false, dealloc);
9321 gfc_add_expr_to_block (&body, tmp);
9324 /* F2003: Allocate or reallocate lhs of allocatable array. */
9325 if (flag_realloc_lhs
9326 && gfc_is_reallocatable_lhs (expr1)
9327 && !gfc_expr_attr (expr1).codimension
9328 && !gfc_is_coindexed (expr1)
9329 && expr2->rank
9330 && !is_runtime_conformable (expr1, expr2))
9332 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
9333 ompws_flags &= ~OMPWS_SCALARIZER_WS;
9334 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
9335 if (tmp != NULL_TREE)
9336 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
9339 /* Generate the copying loops. */
9340 gfc_trans_scalarizing_loops (&loop, &body);
9342 /* Wrap the whole thing up. */
9343 gfc_add_block_to_block (&block, &loop.pre);
9344 gfc_add_block_to_block (&block, &loop.post);
9346 gfc_cleanup_loop (&loop);
9349 return gfc_finish_block (&block);
9353 /* Check whether EXPR is a copyable array. */
9355 static bool
9356 copyable_array_p (gfc_expr * expr)
9358 if (expr->expr_type != EXPR_VARIABLE)
9359 return false;
9361 /* First check it's an array. */
9362 if (expr->rank < 1 || !expr->ref || expr->ref->next)
9363 return false;
9365 if (!gfc_full_array_ref_p (expr->ref, NULL))
9366 return false;
9368 /* Next check that it's of a simple enough type. */
9369 switch (expr->ts.type)
9371 case BT_INTEGER:
9372 case BT_REAL:
9373 case BT_COMPLEX:
9374 case BT_LOGICAL:
9375 return true;
9377 case BT_CHARACTER:
9378 return false;
9380 case BT_DERIVED:
9381 return !expr->ts.u.derived->attr.alloc_comp;
9383 default:
9384 break;
9387 return false;
9390 /* Translate an assignment. */
9392 tree
9393 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
9394 bool dealloc)
9396 tree tmp;
9398 /* Special case a single function returning an array. */
9399 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
9401 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
9402 if (tmp)
9403 return tmp;
9406 /* Special case assigning an array to zero. */
9407 if (copyable_array_p (expr1)
9408 && is_zero_initializer_p (expr2))
9410 tmp = gfc_trans_zero_assign (expr1);
9411 if (tmp)
9412 return tmp;
9415 /* Special case copying one array to another. */
9416 if (copyable_array_p (expr1)
9417 && copyable_array_p (expr2)
9418 && gfc_compare_types (&expr1->ts, &expr2->ts)
9419 && !gfc_check_dependency (expr1, expr2, 0))
9421 tmp = gfc_trans_array_copy (expr1, expr2);
9422 if (tmp)
9423 return tmp;
9426 /* Special case initializing an array from a constant array constructor. */
9427 if (copyable_array_p (expr1)
9428 && expr2->expr_type == EXPR_ARRAY
9429 && gfc_compare_types (&expr1->ts, &expr2->ts))
9431 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
9432 if (tmp)
9433 return tmp;
9436 /* Fallback to the scalarizer to generate explicit loops. */
9437 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc);
9440 tree
9441 gfc_trans_init_assign (gfc_code * code)
9443 return gfc_trans_assignment (code->expr1, code->expr2, true, false);
9446 tree
9447 gfc_trans_assign (gfc_code * code)
9449 return gfc_trans_assignment (code->expr1, code->expr2, false, true);