2013-09-04 Teresa Johnson <tejohnson@google.com>
[official-gcc.git] / gcc / fortran / trans-expr.c
blob0ecfdfce46901f5005e69285bc59c2ff77b8cc8e
1 /* Expression translation
2 Copyright (C) 2002-2013 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 "tree.h"
28 #include "diagnostic-core.h" /* For fatal_error. */
29 #include "langhooks.h"
30 #include "flags.h"
31 #include "gfortran.h"
32 #include "arith.h"
33 #include "constructor.h"
34 #include "trans.h"
35 #include "trans-const.h"
36 #include "trans-types.h"
37 #include "trans-array.h"
38 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
39 #include "trans-stmt.h"
40 #include "dependency.h"
43 /* Convert a scalar to an array descriptor. To be used for assumed-rank
44 arrays. */
46 static tree
47 get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
49 enum gfc_array_kind akind;
51 if (attr.pointer)
52 akind = GFC_ARRAY_POINTER_CONT;
53 else if (attr.allocatable)
54 akind = GFC_ARRAY_ALLOCATABLE;
55 else
56 akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
58 return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, 0, NULL, NULL, 1,
59 akind, !(attr.pointer || attr.target));
62 tree
63 gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
65 tree desc, type;
67 type = get_scalar_to_descriptor_type (scalar, attr);
68 desc = gfc_create_var (type, "desc");
69 DECL_ARTIFICIAL (desc) = 1;
70 gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
71 gfc_get_dtype (type));
72 gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
74 /* Copy pointer address back - but only if it could have changed and
75 if the actual argument is a pointer and not, e.g., NULL(). */
76 if ((attr.pointer || attr.allocatable)
77 && attr.intent != INTENT_IN && POINTER_TYPE_P (TREE_TYPE (scalar)))
78 gfc_add_modify (&se->post, scalar,
79 fold_convert (TREE_TYPE (scalar),
80 gfc_conv_descriptor_data_get (desc)));
81 return desc;
85 /* This is the seed for an eventual trans-class.c
87 The following parameters should not be used directly since they might
88 in future implementations. Use the corresponding APIs. */
89 #define CLASS_DATA_FIELD 0
90 #define CLASS_VPTR_FIELD 1
91 #define VTABLE_HASH_FIELD 0
92 #define VTABLE_SIZE_FIELD 1
93 #define VTABLE_EXTENDS_FIELD 2
94 #define VTABLE_DEF_INIT_FIELD 3
95 #define VTABLE_COPY_FIELD 4
96 #define VTABLE_FINAL_FIELD 5
99 tree
100 gfc_class_set_static_fields (tree decl, tree vptr, tree data)
102 tree tmp;
103 tree field;
104 vec<constructor_elt, va_gc> *init = NULL;
106 field = TYPE_FIELDS (TREE_TYPE (decl));
107 tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
108 CONSTRUCTOR_APPEND_ELT (init, tmp, data);
110 tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
111 CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
113 return build_constructor (TREE_TYPE (decl), init);
117 tree
118 gfc_class_data_get (tree decl)
120 tree data;
121 if (POINTER_TYPE_P (TREE_TYPE (decl)))
122 decl = build_fold_indirect_ref_loc (input_location, decl);
123 data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
124 CLASS_DATA_FIELD);
125 return fold_build3_loc (input_location, COMPONENT_REF,
126 TREE_TYPE (data), decl, data,
127 NULL_TREE);
131 tree
132 gfc_class_vptr_get (tree decl)
134 tree vptr;
135 if (POINTER_TYPE_P (TREE_TYPE (decl)))
136 decl = build_fold_indirect_ref_loc (input_location, decl);
137 vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
138 CLASS_VPTR_FIELD);
139 return fold_build3_loc (input_location, COMPONENT_REF,
140 TREE_TYPE (vptr), decl, vptr,
141 NULL_TREE);
145 static tree
146 gfc_vtable_field_get (tree decl, int field)
148 tree size;
149 tree vptr;
150 vptr = gfc_class_vptr_get (decl);
151 vptr = build_fold_indirect_ref_loc (input_location, vptr);
152 size = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
153 field);
154 size = fold_build3_loc (input_location, COMPONENT_REF,
155 TREE_TYPE (size), vptr, size,
156 NULL_TREE);
157 /* Always return size as an array index type. */
158 if (field == VTABLE_SIZE_FIELD)
159 size = fold_convert (gfc_array_index_type, size);
160 gcc_assert (size);
161 return size;
165 tree
166 gfc_vtable_hash_get (tree decl)
168 return gfc_vtable_field_get (decl, VTABLE_HASH_FIELD);
172 tree
173 gfc_vtable_size_get (tree decl)
175 return gfc_vtable_field_get (decl, VTABLE_SIZE_FIELD);
179 tree
180 gfc_vtable_extends_get (tree decl)
182 return gfc_vtable_field_get (decl, VTABLE_EXTENDS_FIELD);
186 tree
187 gfc_vtable_def_init_get (tree decl)
189 return gfc_vtable_field_get (decl, VTABLE_DEF_INIT_FIELD);
193 tree
194 gfc_vtable_copy_get (tree decl)
196 return gfc_vtable_field_get (decl, VTABLE_COPY_FIELD);
200 tree
201 gfc_vtable_final_get (tree decl)
203 return gfc_vtable_field_get (decl, VTABLE_FINAL_FIELD);
207 #undef CLASS_DATA_FIELD
208 #undef CLASS_VPTR_FIELD
209 #undef VTABLE_HASH_FIELD
210 #undef VTABLE_SIZE_FIELD
211 #undef VTABLE_EXTENDS_FIELD
212 #undef VTABLE_DEF_INIT_FIELD
213 #undef VTABLE_COPY_FIELD
214 #undef VTABLE_FINAL_FIELD
217 /* Reset the vptr to the declared type, e.g. after deallocation. */
219 void
220 gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
222 gfc_expr *rhs, *lhs = gfc_copy_expr (e);
223 gfc_symbol *vtab;
224 tree tmp;
225 gfc_ref *ref;
227 /* If we have a class array, we need go back to the class
228 container. */
229 if (lhs->ref && lhs->ref->next && !lhs->ref->next->next
230 && lhs->ref->next->type == REF_ARRAY
231 && lhs->ref->next->u.ar.type == AR_FULL
232 && lhs->ref->type == REF_COMPONENT
233 && strcmp (lhs->ref->u.c.component->name, "_data") == 0)
235 gfc_free_ref_list (lhs->ref);
236 lhs->ref = NULL;
238 else
239 for (ref = lhs->ref; ref; ref = ref->next)
240 if (ref->next && ref->next->next && !ref->next->next->next
241 && ref->next->next->type == REF_ARRAY
242 && ref->next->next->u.ar.type == AR_FULL
243 && ref->next->type == REF_COMPONENT
244 && strcmp (ref->next->u.c.component->name, "_data") == 0)
246 gfc_free_ref_list (ref->next);
247 ref->next = NULL;
250 gfc_add_vptr_component (lhs);
252 if (UNLIMITED_POLY (e))
253 rhs = gfc_get_null_expr (NULL);
254 else
256 vtab = gfc_find_derived_vtab (e->ts.u.derived);
257 rhs = gfc_lval_expr_from_sym (vtab);
259 tmp = gfc_trans_pointer_assignment (lhs, rhs);
260 gfc_add_expr_to_block (block, tmp);
261 gfc_free_expr (lhs);
262 gfc_free_expr (rhs);
266 /* Obtain the vptr of the last class reference in an expression.
267 Return NULL_TREE if no class reference is found. */
269 tree
270 gfc_get_vptr_from_expr (tree expr)
272 tree tmp;
273 tree type;
275 for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
277 type = TREE_TYPE (tmp);
278 while (type)
280 if (GFC_CLASS_TYPE_P (type))
281 return gfc_class_vptr_get (tmp);
282 if (type != TYPE_CANONICAL (type))
283 type = TYPE_CANONICAL (type);
284 else
285 type = NULL_TREE;
287 if (TREE_CODE (tmp) == VAR_DECL)
288 break;
290 return NULL_TREE;
294 static void
295 class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
296 bool lhs_type)
298 tree tmp, tmp2, type;
300 gfc_conv_descriptor_data_set (block, lhs_desc,
301 gfc_conv_descriptor_data_get (rhs_desc));
302 gfc_conv_descriptor_offset_set (block, lhs_desc,
303 gfc_conv_descriptor_offset_get (rhs_desc));
305 gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
306 gfc_conv_descriptor_dtype (rhs_desc));
308 /* Assign the dimension as range-ref. */
309 tmp = gfc_get_descriptor_dimension (lhs_desc);
310 tmp2 = gfc_get_descriptor_dimension (rhs_desc);
312 type = lhs_type ? TREE_TYPE (tmp) : TREE_TYPE (tmp2);
313 tmp = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp,
314 gfc_index_zero_node, NULL_TREE, NULL_TREE);
315 tmp2 = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp2,
316 gfc_index_zero_node, NULL_TREE, NULL_TREE);
317 gfc_add_modify (block, tmp, tmp2);
321 /* Takes a derived type expression and returns the address of a temporary
322 class object of the 'declared' type. If vptr is not NULL, this is
323 used for the temporary class object.
324 optional_alloc_ptr is false when the dummy is neither allocatable
325 nor a pointer; that's only relevant for the optional handling. */
326 void
327 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
328 gfc_typespec class_ts, tree vptr, bool optional,
329 bool optional_alloc_ptr)
331 gfc_symbol *vtab;
332 tree cond_optional = NULL_TREE;
333 gfc_ss *ss;
334 tree ctree;
335 tree var;
336 tree tmp;
338 /* The derived type needs to be converted to a temporary
339 CLASS object. */
340 tmp = gfc_typenode_for_spec (&class_ts);
341 var = gfc_create_var (tmp, "class");
343 /* Set the vptr. */
344 ctree = gfc_class_vptr_get (var);
346 if (vptr != NULL_TREE)
348 /* Use the dynamic vptr. */
349 tmp = vptr;
351 else
353 /* In this case the vtab corresponds to the derived type and the
354 vptr must point to it. */
355 vtab = gfc_find_derived_vtab (e->ts.u.derived);
356 gcc_assert (vtab);
357 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
359 gfc_add_modify (&parmse->pre, ctree,
360 fold_convert (TREE_TYPE (ctree), tmp));
362 /* Now set the data field. */
363 ctree = gfc_class_data_get (var);
365 if (optional)
366 cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
368 if (parmse->ss && parmse->ss->info->useflags)
370 /* For an array reference in an elemental procedure call we need
371 to retain the ss to provide the scalarized array reference. */
372 gfc_conv_expr_reference (parmse, e);
373 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
374 if (optional)
375 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
376 cond_optional, tmp,
377 fold_convert (TREE_TYPE (tmp), null_pointer_node));
378 gfc_add_modify (&parmse->pre, ctree, tmp);
381 else
383 ss = gfc_walk_expr (e);
384 if (ss == gfc_ss_terminator)
386 parmse->ss = NULL;
387 gfc_conv_expr_reference (parmse, e);
389 /* Scalar to an assumed-rank array. */
390 if (class_ts.u.derived->components->as)
392 tree type;
393 type = get_scalar_to_descriptor_type (parmse->expr,
394 gfc_expr_attr (e));
395 gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
396 gfc_get_dtype (type));
397 if (optional)
398 parmse->expr = build3_loc (input_location, COND_EXPR,
399 TREE_TYPE (parmse->expr),
400 cond_optional, parmse->expr,
401 fold_convert (TREE_TYPE (parmse->expr),
402 null_pointer_node));
403 gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
405 else
407 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
408 if (optional)
409 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
410 cond_optional, tmp,
411 fold_convert (TREE_TYPE (tmp),
412 null_pointer_node));
413 gfc_add_modify (&parmse->pre, ctree, tmp);
416 else
418 stmtblock_t block;
419 gfc_init_block (&block);
421 parmse->ss = ss;
422 gfc_conv_expr_descriptor (parmse, e);
424 if (e->rank != class_ts.u.derived->components->as->rank)
425 class_array_data_assign (&block, ctree, parmse->expr, true);
426 else
428 if (gfc_expr_attr (e).codimension)
429 parmse->expr = fold_build1_loc (input_location,
430 VIEW_CONVERT_EXPR,
431 TREE_TYPE (ctree),
432 parmse->expr);
433 gfc_add_modify (&block, ctree, parmse->expr);
436 if (optional)
438 tmp = gfc_finish_block (&block);
440 gfc_init_block (&block);
441 gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
443 tmp = build3_v (COND_EXPR, cond_optional, tmp,
444 gfc_finish_block (&block));
445 gfc_add_expr_to_block (&parmse->pre, tmp);
447 else
448 gfc_add_block_to_block (&parmse->pre, &block);
452 /* Pass the address of the class object. */
453 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
455 if (optional && optional_alloc_ptr)
456 parmse->expr = build3_loc (input_location, COND_EXPR,
457 TREE_TYPE (parmse->expr),
458 cond_optional, parmse->expr,
459 fold_convert (TREE_TYPE (parmse->expr),
460 null_pointer_node));
464 /* Create a new class container, which is required as scalar coarrays
465 have an array descriptor while normal scalars haven't. Optionally,
466 NULL pointer checks are added if the argument is OPTIONAL. */
468 static void
469 class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
470 gfc_typespec class_ts, bool optional)
472 tree var, ctree, tmp;
473 stmtblock_t block;
474 gfc_ref *ref;
475 gfc_ref *class_ref;
477 gfc_init_block (&block);
479 class_ref = NULL;
480 for (ref = e->ref; ref; ref = ref->next)
482 if (ref->type == REF_COMPONENT
483 && ref->u.c.component->ts.type == BT_CLASS)
484 class_ref = ref;
487 if (class_ref == NULL
488 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
489 tmp = e->symtree->n.sym->backend_decl;
490 else
492 /* Remove everything after the last class reference, convert the
493 expression and then recover its tailend once more. */
494 gfc_se tmpse;
495 ref = class_ref->next;
496 class_ref->next = NULL;
497 gfc_init_se (&tmpse, NULL);
498 gfc_conv_expr (&tmpse, e);
499 class_ref->next = ref;
500 tmp = tmpse.expr;
503 var = gfc_typenode_for_spec (&class_ts);
504 var = gfc_create_var (var, "class");
506 ctree = gfc_class_vptr_get (var);
507 gfc_add_modify (&block, ctree,
508 fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
510 ctree = gfc_class_data_get (var);
511 tmp = gfc_conv_descriptor_data_get (gfc_class_data_get (tmp));
512 gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
514 /* Pass the address of the class object. */
515 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
517 if (optional)
519 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
520 tree tmp2;
522 tmp = gfc_finish_block (&block);
524 gfc_init_block (&block);
525 tmp2 = gfc_class_data_get (var);
526 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
527 null_pointer_node));
528 tmp2 = gfc_finish_block (&block);
530 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
531 cond, tmp, tmp2);
532 gfc_add_expr_to_block (&parmse->pre, tmp);
534 else
535 gfc_add_block_to_block (&parmse->pre, &block);
539 /* Takes an intrinsic type expression and returns the address of a temporary
540 class object of the 'declared' type. */
541 void
542 gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
543 gfc_typespec class_ts)
545 gfc_symbol *vtab;
546 gfc_ss *ss;
547 tree ctree;
548 tree var;
549 tree tmp;
551 /* The intrinsic type needs to be converted to a temporary
552 CLASS object. */
553 tmp = gfc_typenode_for_spec (&class_ts);
554 var = gfc_create_var (tmp, "class");
556 /* Set the vptr. */
557 ctree = gfc_class_vptr_get (var);
559 vtab = gfc_find_intrinsic_vtab (&e->ts);
560 gcc_assert (vtab);
561 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
562 gfc_add_modify (&parmse->pre, ctree,
563 fold_convert (TREE_TYPE (ctree), tmp));
565 /* Now set the data field. */
566 ctree = gfc_class_data_get (var);
567 if (parmse->ss && parmse->ss->info->useflags)
569 /* For an array reference in an elemental procedure call we need
570 to retain the ss to provide the scalarized array reference. */
571 gfc_conv_expr_reference (parmse, e);
572 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
573 gfc_add_modify (&parmse->pre, ctree, tmp);
575 else
577 ss = gfc_walk_expr (e);
578 if (ss == gfc_ss_terminator)
580 parmse->ss = NULL;
581 gfc_conv_expr_reference (parmse, e);
582 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
583 gfc_add_modify (&parmse->pre, ctree, tmp);
585 else
587 parmse->ss = ss;
588 gfc_conv_expr_descriptor (parmse, e);
589 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
593 /* Pass the address of the class object. */
594 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
598 /* Takes a scalarized class array expression and returns the
599 address of a temporary scalar class object of the 'declared'
600 type.
601 OOP-TODO: This could be improved by adding code that branched on
602 the dynamic type being the same as the declared type. In this case
603 the original class expression can be passed directly.
604 optional_alloc_ptr is false when the dummy is neither allocatable
605 nor a pointer; that's relevant for the optional handling.
606 Set copyback to true if class container's _data and _vtab pointers
607 might get modified. */
609 void
610 gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
611 bool elemental, bool copyback, bool optional,
612 bool optional_alloc_ptr)
614 tree ctree;
615 tree var;
616 tree tmp;
617 tree vptr;
618 tree cond = NULL_TREE;
619 gfc_ref *ref;
620 gfc_ref *class_ref;
621 stmtblock_t block;
622 bool full_array = false;
624 gfc_init_block (&block);
626 class_ref = NULL;
627 for (ref = e->ref; ref; ref = ref->next)
629 if (ref->type == REF_COMPONENT
630 && ref->u.c.component->ts.type == BT_CLASS)
631 class_ref = ref;
633 if (ref->next == NULL)
634 break;
637 if ((ref == NULL || class_ref == ref)
638 && (!class_ts.u.derived->components->as
639 || class_ts.u.derived->components->as->rank != -1))
640 return;
642 /* Test for FULL_ARRAY. */
643 if (e->rank == 0 && gfc_expr_attr (e).codimension
644 && gfc_expr_attr (e).dimension)
645 full_array = true;
646 else
647 gfc_is_class_array_ref (e, &full_array);
649 /* The derived type needs to be converted to a temporary
650 CLASS object. */
651 tmp = gfc_typenode_for_spec (&class_ts);
652 var = gfc_create_var (tmp, "class");
654 /* Set the data. */
655 ctree = gfc_class_data_get (var);
656 if (class_ts.u.derived->components->as
657 && e->rank != class_ts.u.derived->components->as->rank)
659 if (e->rank == 0)
661 tree type = get_scalar_to_descriptor_type (parmse->expr,
662 gfc_expr_attr (e));
663 gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
664 gfc_get_dtype (type));
666 tmp = gfc_class_data_get (parmse->expr);
667 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
668 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
670 gfc_conv_descriptor_data_set (&block, ctree, tmp);
672 else
673 class_array_data_assign (&block, ctree, parmse->expr, false);
675 else
677 if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
678 parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
679 TREE_TYPE (ctree), parmse->expr);
680 gfc_add_modify (&block, ctree, parmse->expr);
683 /* Return the data component, except in the case of scalarized array
684 references, where nullification of the cannot occur and so there
685 is no need. */
686 if (!elemental && full_array && copyback)
688 if (class_ts.u.derived->components->as
689 && e->rank != class_ts.u.derived->components->as->rank)
691 if (e->rank == 0)
692 gfc_add_modify (&parmse->post, gfc_class_data_get (parmse->expr),
693 gfc_conv_descriptor_data_get (ctree));
694 else
695 class_array_data_assign (&parmse->post, parmse->expr, ctree, true);
697 else
698 gfc_add_modify (&parmse->post, parmse->expr, ctree);
701 /* Set the vptr. */
702 ctree = gfc_class_vptr_get (var);
704 /* The vptr is the second field of the actual argument.
705 First we have to find the corresponding class reference. */
707 tmp = NULL_TREE;
708 if (class_ref == NULL
709 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
710 tmp = e->symtree->n.sym->backend_decl;
711 else
713 /* Remove everything after the last class reference, convert the
714 expression and then recover its tailend once more. */
715 gfc_se tmpse;
716 ref = class_ref->next;
717 class_ref->next = NULL;
718 gfc_init_se (&tmpse, NULL);
719 gfc_conv_expr (&tmpse, e);
720 class_ref->next = ref;
721 tmp = tmpse.expr;
724 gcc_assert (tmp != NULL_TREE);
726 /* Dereference if needs be. */
727 if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
728 tmp = build_fold_indirect_ref_loc (input_location, tmp);
730 vptr = gfc_class_vptr_get (tmp);
731 gfc_add_modify (&block, ctree,
732 fold_convert (TREE_TYPE (ctree), vptr));
734 /* Return the vptr component, except in the case of scalarized array
735 references, where the dynamic type cannot change. */
736 if (!elemental && full_array && copyback)
737 gfc_add_modify (&parmse->post, vptr,
738 fold_convert (TREE_TYPE (vptr), ctree));
740 gcc_assert (!optional || (optional && !copyback));
741 if (optional)
743 tree tmp2;
745 cond = gfc_conv_expr_present (e->symtree->n.sym);
746 tmp = gfc_finish_block (&block);
748 if (optional_alloc_ptr)
749 tmp2 = build_empty_stmt (input_location);
750 else
752 gfc_init_block (&block);
754 tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
755 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
756 null_pointer_node));
757 tmp2 = gfc_finish_block (&block);
760 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
761 cond, tmp, tmp2);
762 gfc_add_expr_to_block (&parmse->pre, tmp);
764 else
765 gfc_add_block_to_block (&parmse->pre, &block);
767 /* Pass the address of the class object. */
768 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
770 if (optional && optional_alloc_ptr)
771 parmse->expr = build3_loc (input_location, COND_EXPR,
772 TREE_TYPE (parmse->expr),
773 cond, parmse->expr,
774 fold_convert (TREE_TYPE (parmse->expr),
775 null_pointer_node));
779 /* Given a class array declaration and an index, returns the address
780 of the referenced element. */
782 tree
783 gfc_get_class_array_ref (tree index, tree class_decl)
785 tree data = gfc_class_data_get (class_decl);
786 tree size = gfc_vtable_size_get (class_decl);
787 tree offset = fold_build2_loc (input_location, MULT_EXPR,
788 gfc_array_index_type,
789 index, size);
790 tree ptr;
791 data = gfc_conv_descriptor_data_get (data);
792 ptr = fold_convert (pvoid_type_node, data);
793 ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
794 return fold_convert (TREE_TYPE (data), ptr);
798 /* Copies one class expression to another, assuming that if either
799 'to' or 'from' are arrays they are packed. Should 'from' be
800 NULL_TREE, the initialization expression for 'to' is used, assuming
801 that the _vptr is set. */
803 tree
804 gfc_copy_class_to_class (tree from, tree to, tree nelems)
806 tree fcn;
807 tree fcn_type;
808 tree from_data;
809 tree to_data;
810 tree to_ref;
811 tree from_ref;
812 vec<tree, va_gc> *args;
813 tree tmp;
814 tree index;
815 stmtblock_t loopbody;
816 stmtblock_t body;
817 gfc_loopinfo loop;
819 args = NULL;
821 if (from != NULL_TREE)
822 fcn = gfc_vtable_copy_get (from);
823 else
824 fcn = gfc_vtable_copy_get (to);
826 fcn_type = TREE_TYPE (TREE_TYPE (fcn));
828 if (from != NULL_TREE)
829 from_data = gfc_class_data_get (from);
830 else
831 from_data = gfc_vtable_def_init_get (to);
833 to_data = gfc_class_data_get (to);
835 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
837 gfc_init_block (&body);
838 tmp = fold_build2_loc (input_location, MINUS_EXPR,
839 gfc_array_index_type, nelems,
840 gfc_index_one_node);
841 nelems = gfc_evaluate_now (tmp, &body);
842 index = gfc_create_var (gfc_array_index_type, "S");
844 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data)))
846 from_ref = gfc_get_class_array_ref (index, from);
847 vec_safe_push (args, from_ref);
849 else
850 vec_safe_push (args, from_data);
852 to_ref = gfc_get_class_array_ref (index, to);
853 vec_safe_push (args, to_ref);
855 tmp = build_call_vec (fcn_type, fcn, args);
857 /* Build the body of the loop. */
858 gfc_init_block (&loopbody);
859 gfc_add_expr_to_block (&loopbody, tmp);
861 /* Build the loop and return. */
862 gfc_init_loopinfo (&loop);
863 loop.dimen = 1;
864 loop.from[0] = gfc_index_zero_node;
865 loop.loopvar[0] = index;
866 loop.to[0] = nelems;
867 gfc_trans_scalarizing_loops (&loop, &loopbody);
868 gfc_add_block_to_block (&body, &loop.pre);
869 tmp = gfc_finish_block (&body);
870 gfc_cleanup_loop (&loop);
872 else
874 gcc_assert (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data)));
875 vec_safe_push (args, from_data);
876 vec_safe_push (args, to_data);
877 tmp = build_call_vec (fcn_type, fcn, args);
880 return tmp;
883 static tree
884 gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
886 gfc_actual_arglist *actual;
887 gfc_expr *ppc;
888 gfc_code *ppc_code;
889 tree res;
891 actual = gfc_get_actual_arglist ();
892 actual->expr = gfc_copy_expr (rhs);
893 actual->next = gfc_get_actual_arglist ();
894 actual->next->expr = gfc_copy_expr (lhs);
895 ppc = gfc_copy_expr (obj);
896 gfc_add_vptr_component (ppc);
897 gfc_add_component_ref (ppc, "_copy");
898 ppc_code = gfc_get_code (EXEC_CALL);
899 ppc_code->resolved_sym = ppc->symtree->n.sym;
900 /* Although '_copy' is set to be elemental in class.c, it is
901 not staying that way. Find out why, sometime.... */
902 ppc_code->resolved_sym->attr.elemental = 1;
903 ppc_code->ext.actual = actual;
904 ppc_code->expr1 = ppc;
905 /* Since '_copy' is elemental, the scalarizer will take care
906 of arrays in gfc_trans_call. */
907 res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
908 gfc_free_statements (ppc_code);
909 return res;
912 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
913 A MEMCPY is needed to copy the full data from the default initializer
914 of the dynamic type. */
916 tree
917 gfc_trans_class_init_assign (gfc_code *code)
919 stmtblock_t block;
920 tree tmp;
921 gfc_se dst,src,memsz;
922 gfc_expr *lhs, *rhs, *sz;
924 gfc_start_block (&block);
926 lhs = gfc_copy_expr (code->expr1);
927 gfc_add_data_component (lhs);
929 rhs = gfc_copy_expr (code->expr1);
930 gfc_add_vptr_component (rhs);
932 /* Make sure that the component backend_decls have been built, which
933 will not have happened if the derived types concerned have not
934 been referenced. */
935 gfc_get_derived_type (rhs->ts.u.derived);
936 gfc_add_def_init_component (rhs);
938 if (code->expr1->ts.type == BT_CLASS
939 && CLASS_DATA (code->expr1)->attr.dimension)
940 tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
941 else
943 sz = gfc_copy_expr (code->expr1);
944 gfc_add_vptr_component (sz);
945 gfc_add_size_component (sz);
947 gfc_init_se (&dst, NULL);
948 gfc_init_se (&src, NULL);
949 gfc_init_se (&memsz, NULL);
950 gfc_conv_expr (&dst, lhs);
951 gfc_conv_expr (&src, rhs);
952 gfc_conv_expr (&memsz, sz);
953 gfc_add_block_to_block (&block, &src.pre);
954 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
956 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
959 if (code->expr1->symtree->n.sym->attr.optional
960 || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master)
962 tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
963 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
964 present, tmp,
965 build_empty_stmt (input_location));
968 gfc_add_expr_to_block (&block, tmp);
970 return gfc_finish_block (&block);
974 /* Translate an assignment to a CLASS object
975 (pointer or ordinary assignment). */
977 tree
978 gfc_trans_class_assign (gfc_expr *expr1, gfc_expr *expr2, gfc_exec_op op)
980 stmtblock_t block;
981 tree tmp;
982 gfc_expr *lhs;
983 gfc_expr *rhs;
984 gfc_ref *ref;
986 gfc_start_block (&block);
988 ref = expr1->ref;
989 while (ref && ref->next)
990 ref = ref->next;
992 /* Class valued proc_pointer assignments do not need any further
993 preparation. */
994 if (ref && ref->type == REF_COMPONENT
995 && ref->u.c.component->attr.proc_pointer
996 && expr2->expr_type == EXPR_VARIABLE
997 && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE
998 && op == EXEC_POINTER_ASSIGN)
999 goto assign;
1001 if (expr2->ts.type != BT_CLASS)
1003 /* Insert an additional assignment which sets the '_vptr' field. */
1004 gfc_symbol *vtab = NULL;
1005 gfc_symtree *st;
1007 lhs = gfc_copy_expr (expr1);
1008 gfc_add_vptr_component (lhs);
1010 if (UNLIMITED_POLY (expr1)
1011 && expr2->expr_type == EXPR_NULL && expr2->ts.type == BT_UNKNOWN)
1013 rhs = gfc_get_null_expr (&expr2->where);
1014 goto assign_vptr;
1017 if (expr2->ts.type == BT_DERIVED)
1018 vtab = gfc_find_derived_vtab (expr2->ts.u.derived);
1019 else if (expr2->expr_type == EXPR_NULL)
1020 vtab = gfc_find_derived_vtab (expr1->ts.u.derived);
1021 else
1022 vtab = gfc_find_intrinsic_vtab (&expr2->ts);
1023 gcc_assert (vtab);
1025 rhs = gfc_get_expr ();
1026 rhs->expr_type = EXPR_VARIABLE;
1027 gfc_find_sym_tree (vtab->name, vtab->ns, 1, &st);
1028 rhs->symtree = st;
1029 rhs->ts = vtab->ts;
1030 assign_vptr:
1031 tmp = gfc_trans_pointer_assignment (lhs, rhs);
1032 gfc_add_expr_to_block (&block, tmp);
1034 gfc_free_expr (lhs);
1035 gfc_free_expr (rhs);
1037 else if (expr1->ts.type == BT_DERIVED && UNLIMITED_POLY (expr2))
1039 /* F2003:C717 only sequence and bind-C types can come here. */
1040 gcc_assert (expr1->ts.u.derived->attr.sequence
1041 || expr1->ts.u.derived->attr.is_bind_c);
1042 gfc_add_data_component (expr2);
1043 goto assign;
1045 else if (CLASS_DATA (expr2)->attr.dimension && expr2->expr_type != EXPR_FUNCTION)
1047 /* Insert an additional assignment which sets the '_vptr' field. */
1048 lhs = gfc_copy_expr (expr1);
1049 gfc_add_vptr_component (lhs);
1051 rhs = gfc_copy_expr (expr2);
1052 gfc_add_vptr_component (rhs);
1054 tmp = gfc_trans_pointer_assignment (lhs, rhs);
1055 gfc_add_expr_to_block (&block, tmp);
1057 gfc_free_expr (lhs);
1058 gfc_free_expr (rhs);
1061 /* Do the actual CLASS assignment. */
1062 if (expr2->ts.type == BT_CLASS
1063 && !CLASS_DATA (expr2)->attr.dimension)
1064 op = EXEC_ASSIGN;
1065 else if (expr2->expr_type != EXPR_FUNCTION || expr2->ts.type != BT_CLASS
1066 || !CLASS_DATA (expr2)->attr.dimension)
1067 gfc_add_data_component (expr1);
1069 assign:
1071 if (op == EXEC_ASSIGN)
1072 tmp = gfc_trans_assignment (expr1, expr2, false, true);
1073 else if (op == EXEC_POINTER_ASSIGN)
1074 tmp = gfc_trans_pointer_assignment (expr1, expr2);
1075 else
1076 gcc_unreachable();
1078 gfc_add_expr_to_block (&block, tmp);
1080 return gfc_finish_block (&block);
1084 /* End of prototype trans-class.c */
1087 static void
1088 realloc_lhs_warning (bt type, bool array, locus *where)
1090 if (array && type != BT_CLASS && type != BT_DERIVED
1091 && gfc_option.warn_realloc_lhs)
1092 gfc_warning ("Code for reallocating the allocatable array at %L will "
1093 "be added", where);
1094 else if (gfc_option.warn_realloc_lhs_all)
1095 gfc_warning ("Code for reallocating the allocatable variable at %L "
1096 "will be added", where);
1100 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
1101 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
1102 gfc_expr *);
1104 /* Copy the scalarization loop variables. */
1106 static void
1107 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
1109 dest->ss = src->ss;
1110 dest->loop = src->loop;
1114 /* Initialize a simple expression holder.
1116 Care must be taken when multiple se are created with the same parent.
1117 The child se must be kept in sync. The easiest way is to delay creation
1118 of a child se until after after the previous se has been translated. */
1120 void
1121 gfc_init_se (gfc_se * se, gfc_se * parent)
1123 memset (se, 0, sizeof (gfc_se));
1124 gfc_init_block (&se->pre);
1125 gfc_init_block (&se->post);
1127 se->parent = parent;
1129 if (parent)
1130 gfc_copy_se_loopvars (se, parent);
1134 /* Advances to the next SS in the chain. Use this rather than setting
1135 se->ss = se->ss->next because all the parents needs to be kept in sync.
1136 See gfc_init_se. */
1138 void
1139 gfc_advance_se_ss_chain (gfc_se * se)
1141 gfc_se *p;
1142 gfc_ss *ss;
1144 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
1146 p = se;
1147 /* Walk down the parent chain. */
1148 while (p != NULL)
1150 /* Simple consistency check. */
1151 gcc_assert (p->parent == NULL || p->parent->ss == p->ss
1152 || p->parent->ss->nested_ss == p->ss);
1154 /* If we were in a nested loop, the next scalarized expression can be
1155 on the parent ss' next pointer. Thus we should not take the next
1156 pointer blindly, but rather go up one nest level as long as next
1157 is the end of chain. */
1158 ss = p->ss;
1159 while (ss->next == gfc_ss_terminator && ss->parent != NULL)
1160 ss = ss->parent;
1162 p->ss = ss->next;
1164 p = p->parent;
1169 /* Ensures the result of the expression as either a temporary variable
1170 or a constant so that it can be used repeatedly. */
1172 void
1173 gfc_make_safe_expr (gfc_se * se)
1175 tree var;
1177 if (CONSTANT_CLASS_P (se->expr))
1178 return;
1180 /* We need a temporary for this result. */
1181 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
1182 gfc_add_modify (&se->pre, var, se->expr);
1183 se->expr = var;
1187 /* Return an expression which determines if a dummy parameter is present.
1188 Also used for arguments to procedures with multiple entry points. */
1190 tree
1191 gfc_conv_expr_present (gfc_symbol * sym)
1193 tree decl, cond;
1195 gcc_assert (sym->attr.dummy);
1196 decl = gfc_get_symbol_decl (sym);
1198 /* Intrinsic scalars with VALUE attribute which are passed by value
1199 use a hidden argument to denote the present status. */
1200 if (sym->attr.value && sym->ts.type != BT_CHARACTER
1201 && sym->ts.type != BT_CLASS && sym->ts.type != BT_DERIVED
1202 && !sym->attr.dimension)
1204 char name[GFC_MAX_SYMBOL_LEN + 2];
1205 tree tree_name;
1207 gcc_assert (TREE_CODE (decl) == PARM_DECL);
1208 name[0] = '_';
1209 strcpy (&name[1], sym->name);
1210 tree_name = get_identifier (name);
1212 /* Walk function argument list to find hidden arg. */
1213 cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
1214 for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
1215 if (DECL_NAME (cond) == tree_name)
1216 break;
1218 gcc_assert (cond);
1219 return cond;
1222 if (TREE_CODE (decl) != PARM_DECL)
1224 /* Array parameters use a temporary descriptor, we want the real
1225 parameter. */
1226 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
1227 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
1228 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
1231 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
1232 fold_convert (TREE_TYPE (decl), null_pointer_node));
1234 /* Fortran 2008 allows to pass null pointers and non-associated pointers
1235 as actual argument to denote absent dummies. For array descriptors,
1236 we thus also need to check the array descriptor. For BT_CLASS, it
1237 can also occur for scalars and F2003 due to type->class wrapping and
1238 class->class wrapping. Note further that BT_CLASS always uses an
1239 array descriptor for arrays, also for explicit-shape/assumed-size. */
1241 if (!sym->attr.allocatable
1242 && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
1243 || (sym->ts.type == BT_CLASS
1244 && !CLASS_DATA (sym)->attr.allocatable
1245 && !CLASS_DATA (sym)->attr.class_pointer))
1246 && ((gfc_option.allow_std & GFC_STD_F2008) != 0
1247 || sym->ts.type == BT_CLASS))
1249 tree tmp;
1251 if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
1252 || sym->as->type == AS_ASSUMED_RANK
1253 || sym->attr.codimension))
1254 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
1256 tmp = build_fold_indirect_ref_loc (input_location, decl);
1257 if (sym->ts.type == BT_CLASS)
1258 tmp = gfc_class_data_get (tmp);
1259 tmp = gfc_conv_array_data (tmp);
1261 else if (sym->ts.type == BT_CLASS)
1262 tmp = gfc_class_data_get (decl);
1263 else
1264 tmp = NULL_TREE;
1266 if (tmp != NULL_TREE)
1268 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
1269 fold_convert (TREE_TYPE (tmp), null_pointer_node));
1270 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1271 boolean_type_node, cond, tmp);
1275 return cond;
1279 /* Converts a missing, dummy argument into a null or zero. */
1281 void
1282 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
1284 tree present;
1285 tree tmp;
1287 present = gfc_conv_expr_present (arg->symtree->n.sym);
1289 if (kind > 0)
1291 /* Create a temporary and convert it to the correct type. */
1292 tmp = gfc_get_int_type (kind);
1293 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
1294 se->expr));
1296 /* Test for a NULL value. */
1297 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
1298 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
1299 tmp = gfc_evaluate_now (tmp, &se->pre);
1300 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
1302 else
1304 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
1305 present, se->expr,
1306 build_zero_cst (TREE_TYPE (se->expr)));
1307 tmp = gfc_evaluate_now (tmp, &se->pre);
1308 se->expr = tmp;
1311 if (ts.type == BT_CHARACTER)
1313 tmp = build_int_cst (gfc_charlen_type_node, 0);
1314 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
1315 present, se->string_length, tmp);
1316 tmp = gfc_evaluate_now (tmp, &se->pre);
1317 se->string_length = tmp;
1319 return;
1323 /* Get the character length of an expression, looking through gfc_refs
1324 if necessary. */
1326 tree
1327 gfc_get_expr_charlen (gfc_expr *e)
1329 gfc_ref *r;
1330 tree length;
1332 gcc_assert (e->expr_type == EXPR_VARIABLE
1333 && e->ts.type == BT_CHARACTER);
1335 length = NULL; /* To silence compiler warning. */
1337 if (is_subref_array (e) && e->ts.u.cl->length)
1339 gfc_se tmpse;
1340 gfc_init_se (&tmpse, NULL);
1341 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
1342 e->ts.u.cl->backend_decl = tmpse.expr;
1343 return tmpse.expr;
1346 /* First candidate: if the variable is of type CHARACTER, the
1347 expression's length could be the length of the character
1348 variable. */
1349 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
1350 length = e->symtree->n.sym->ts.u.cl->backend_decl;
1352 /* Look through the reference chain for component references. */
1353 for (r = e->ref; r; r = r->next)
1355 switch (r->type)
1357 case REF_COMPONENT:
1358 if (r->u.c.component->ts.type == BT_CHARACTER)
1359 length = r->u.c.component->ts.u.cl->backend_decl;
1360 break;
1362 case REF_ARRAY:
1363 /* Do nothing. */
1364 break;
1366 default:
1367 /* We should never got substring references here. These will be
1368 broken down by the scalarizer. */
1369 gcc_unreachable ();
1370 break;
1374 gcc_assert (length != NULL);
1375 return length;
1379 /* Return for an expression the backend decl of the coarray. */
1381 static tree
1382 get_tree_for_caf_expr (gfc_expr *expr)
1384 tree caf_decl = NULL_TREE;
1385 gfc_ref *ref;
1387 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
1388 if (expr->symtree->n.sym->attr.codimension)
1389 caf_decl = expr->symtree->n.sym->backend_decl;
1391 for (ref = expr->ref; ref; ref = ref->next)
1392 if (ref->type == REF_COMPONENT)
1394 gfc_component *comp = ref->u.c.component;
1395 if (comp->attr.pointer || comp->attr.allocatable)
1396 caf_decl = NULL_TREE;
1397 if (comp->attr.codimension)
1398 caf_decl = comp->backend_decl;
1401 gcc_assert (caf_decl != NULL_TREE);
1402 return caf_decl;
1406 /* For each character array constructor subexpression without a ts.u.cl->length,
1407 replace it by its first element (if there aren't any elements, the length
1408 should already be set to zero). */
1410 static void
1411 flatten_array_ctors_without_strlen (gfc_expr* e)
1413 gfc_actual_arglist* arg;
1414 gfc_constructor* c;
1416 if (!e)
1417 return;
1419 switch (e->expr_type)
1422 case EXPR_OP:
1423 flatten_array_ctors_without_strlen (e->value.op.op1);
1424 flatten_array_ctors_without_strlen (e->value.op.op2);
1425 break;
1427 case EXPR_COMPCALL:
1428 /* TODO: Implement as with EXPR_FUNCTION when needed. */
1429 gcc_unreachable ();
1431 case EXPR_FUNCTION:
1432 for (arg = e->value.function.actual; arg; arg = arg->next)
1433 flatten_array_ctors_without_strlen (arg->expr);
1434 break;
1436 case EXPR_ARRAY:
1438 /* We've found what we're looking for. */
1439 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
1441 gfc_constructor *c;
1442 gfc_expr* new_expr;
1444 gcc_assert (e->value.constructor);
1446 c = gfc_constructor_first (e->value.constructor);
1447 new_expr = c->expr;
1448 c->expr = NULL;
1450 flatten_array_ctors_without_strlen (new_expr);
1451 gfc_replace_expr (e, new_expr);
1452 break;
1455 /* Otherwise, fall through to handle constructor elements. */
1456 case EXPR_STRUCTURE:
1457 for (c = gfc_constructor_first (e->value.constructor);
1458 c; c = gfc_constructor_next (c))
1459 flatten_array_ctors_without_strlen (c->expr);
1460 break;
1462 default:
1463 break;
1469 /* Generate code to initialize a string length variable. Returns the
1470 value. For array constructors, cl->length might be NULL and in this case,
1471 the first element of the constructor is needed. expr is the original
1472 expression so we can access it but can be NULL if this is not needed. */
1474 void
1475 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
1477 gfc_se se;
1479 gfc_init_se (&se, NULL);
1481 if (!cl->length
1482 && cl->backend_decl
1483 && TREE_CODE (cl->backend_decl) == VAR_DECL)
1484 return;
1486 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
1487 "flatten" array constructors by taking their first element; all elements
1488 should be the same length or a cl->length should be present. */
1489 if (!cl->length)
1491 gfc_expr* expr_flat;
1492 gcc_assert (expr);
1493 expr_flat = gfc_copy_expr (expr);
1494 flatten_array_ctors_without_strlen (expr_flat);
1495 gfc_resolve_expr (expr_flat);
1497 gfc_conv_expr (&se, expr_flat);
1498 gfc_add_block_to_block (pblock, &se.pre);
1499 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
1501 gfc_free_expr (expr_flat);
1502 return;
1505 /* Convert cl->length. */
1507 gcc_assert (cl->length);
1509 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
1510 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
1511 se.expr, build_int_cst (gfc_charlen_type_node, 0));
1512 gfc_add_block_to_block (pblock, &se.pre);
1514 if (cl->backend_decl)
1515 gfc_add_modify (pblock, cl->backend_decl, se.expr);
1516 else
1517 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
1521 static void
1522 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
1523 const char *name, locus *where)
1525 tree tmp;
1526 tree type;
1527 tree fault;
1528 gfc_se start;
1529 gfc_se end;
1530 char *msg;
1531 mpz_t length;
1533 type = gfc_get_character_type (kind, ref->u.ss.length);
1534 type = build_pointer_type (type);
1536 gfc_init_se (&start, se);
1537 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
1538 gfc_add_block_to_block (&se->pre, &start.pre);
1540 if (integer_onep (start.expr))
1541 gfc_conv_string_parameter (se);
1542 else
1544 tmp = start.expr;
1545 STRIP_NOPS (tmp);
1546 /* Avoid multiple evaluation of substring start. */
1547 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
1548 start.expr = gfc_evaluate_now (start.expr, &se->pre);
1550 /* Change the start of the string. */
1551 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
1552 tmp = se->expr;
1553 else
1554 tmp = build_fold_indirect_ref_loc (input_location,
1555 se->expr);
1556 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
1557 se->expr = gfc_build_addr_expr (type, tmp);
1560 /* Length = end + 1 - start. */
1561 gfc_init_se (&end, se);
1562 if (ref->u.ss.end == NULL)
1563 end.expr = se->string_length;
1564 else
1566 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
1567 gfc_add_block_to_block (&se->pre, &end.pre);
1569 tmp = end.expr;
1570 STRIP_NOPS (tmp);
1571 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
1572 end.expr = gfc_evaluate_now (end.expr, &se->pre);
1574 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1576 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
1577 boolean_type_node, start.expr,
1578 end.expr);
1580 /* Check lower bound. */
1581 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
1582 start.expr,
1583 build_int_cst (gfc_charlen_type_node, 1));
1584 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1585 boolean_type_node, nonempty, fault);
1586 if (name)
1587 asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
1588 "is less than one", name);
1589 else
1590 asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
1591 "is less than one");
1592 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
1593 fold_convert (long_integer_type_node,
1594 start.expr));
1595 free (msg);
1597 /* Check upper bound. */
1598 fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
1599 end.expr, se->string_length);
1600 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1601 boolean_type_node, nonempty, fault);
1602 if (name)
1603 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
1604 "exceeds string length (%%ld)", name);
1605 else
1606 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
1607 "exceeds string length (%%ld)");
1608 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
1609 fold_convert (long_integer_type_node, end.expr),
1610 fold_convert (long_integer_type_node,
1611 se->string_length));
1612 free (msg);
1615 /* Try to calculate the length from the start and end expressions. */
1616 if (ref->u.ss.end
1617 && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
1619 int i_len;
1621 i_len = mpz_get_si (length) + 1;
1622 if (i_len < 0)
1623 i_len = 0;
1625 tmp = build_int_cst (gfc_charlen_type_node, i_len);
1626 mpz_clear (length); /* Was initialized by gfc_dep_difference. */
1628 else
1630 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
1631 end.expr, start.expr);
1632 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
1633 build_int_cst (gfc_charlen_type_node, 1), tmp);
1634 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
1635 tmp, build_int_cst (gfc_charlen_type_node, 0));
1638 se->string_length = tmp;
1642 /* Convert a derived type component reference. */
1644 static void
1645 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
1647 gfc_component *c;
1648 tree tmp;
1649 tree decl;
1650 tree field;
1652 c = ref->u.c.component;
1654 gcc_assert (c->backend_decl);
1656 field = c->backend_decl;
1657 gcc_assert (TREE_CODE (field) == FIELD_DECL);
1658 decl = se->expr;
1660 /* Components can correspond to fields of different containing
1661 types, as components are created without context, whereas
1662 a concrete use of a component has the type of decl as context.
1663 So, if the type doesn't match, we search the corresponding
1664 FIELD_DECL in the parent type. To not waste too much time
1665 we cache this result in norestrict_decl. */
1667 if (DECL_FIELD_CONTEXT (field) != TREE_TYPE (decl))
1669 tree f2 = c->norestrict_decl;
1670 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
1671 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
1672 if (TREE_CODE (f2) == FIELD_DECL
1673 && DECL_NAME (f2) == DECL_NAME (field))
1674 break;
1675 gcc_assert (f2);
1676 c->norestrict_decl = f2;
1677 field = f2;
1680 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
1681 decl, field, NULL_TREE);
1683 se->expr = tmp;
1685 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
1687 tmp = c->ts.u.cl->backend_decl;
1688 /* Components must always be constant length. */
1689 gcc_assert (tmp && INTEGER_CST_P (tmp));
1690 se->string_length = tmp;
1693 if (((c->attr.pointer || c->attr.allocatable)
1694 && (!c->attr.dimension && !c->attr.codimension)
1695 && c->ts.type != BT_CHARACTER)
1696 || c->attr.proc_pointer)
1697 se->expr = build_fold_indirect_ref_loc (input_location,
1698 se->expr);
1702 /* This function deals with component references to components of the
1703 parent type for derived type extensions. */
1704 static void
1705 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
1707 gfc_component *c;
1708 gfc_component *cmp;
1709 gfc_symbol *dt;
1710 gfc_ref parent;
1712 dt = ref->u.c.sym;
1713 c = ref->u.c.component;
1715 /* Return if the component is in the parent type. */
1716 for (cmp = dt->components; cmp; cmp = cmp->next)
1717 if (strcmp (c->name, cmp->name) == 0)
1718 return;
1720 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
1721 parent.type = REF_COMPONENT;
1722 parent.next = NULL;
1723 parent.u.c.sym = dt;
1724 parent.u.c.component = dt->components;
1726 if (dt->backend_decl == NULL)
1727 gfc_get_derived_type (dt);
1729 /* Build the reference and call self. */
1730 gfc_conv_component_ref (se, &parent);
1731 parent.u.c.sym = dt->components->ts.u.derived;
1732 parent.u.c.component = c;
1733 conv_parent_component_references (se, &parent);
1736 /* Return the contents of a variable. Also handles reference/pointer
1737 variables (all Fortran pointer references are implicit). */
1739 static void
1740 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
1742 gfc_ss *ss;
1743 gfc_ref *ref;
1744 gfc_symbol *sym;
1745 tree parent_decl = NULL_TREE;
1746 int parent_flag;
1747 bool return_value;
1748 bool alternate_entry;
1749 bool entry_master;
1751 sym = expr->symtree->n.sym;
1752 ss = se->ss;
1753 if (ss != NULL)
1755 gfc_ss_info *ss_info = ss->info;
1757 /* Check that something hasn't gone horribly wrong. */
1758 gcc_assert (ss != gfc_ss_terminator);
1759 gcc_assert (ss_info->expr == expr);
1761 /* A scalarized term. We already know the descriptor. */
1762 se->expr = ss_info->data.array.descriptor;
1763 se->string_length = ss_info->string_length;
1764 ref = ss_info->data.array.ref;
1765 if (ref)
1766 gcc_assert (ref->type == REF_ARRAY
1767 && ref->u.ar.type != AR_ELEMENT);
1768 else
1769 gfc_conv_tmp_array_ref (se);
1771 else
1773 tree se_expr = NULL_TREE;
1775 se->expr = gfc_get_symbol_decl (sym);
1777 /* Deal with references to a parent results or entries by storing
1778 the current_function_decl and moving to the parent_decl. */
1779 return_value = sym->attr.function && sym->result == sym;
1780 alternate_entry = sym->attr.function && sym->attr.entry
1781 && sym->result == sym;
1782 entry_master = sym->attr.result
1783 && sym->ns->proc_name->attr.entry_master
1784 && !gfc_return_by_reference (sym->ns->proc_name);
1785 if (current_function_decl)
1786 parent_decl = DECL_CONTEXT (current_function_decl);
1788 if ((se->expr == parent_decl && return_value)
1789 || (sym->ns && sym->ns->proc_name
1790 && parent_decl
1791 && sym->ns->proc_name->backend_decl == parent_decl
1792 && (alternate_entry || entry_master)))
1793 parent_flag = 1;
1794 else
1795 parent_flag = 0;
1797 /* Special case for assigning the return value of a function.
1798 Self recursive functions must have an explicit return value. */
1799 if (return_value && (se->expr == current_function_decl || parent_flag))
1800 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
1802 /* Similarly for alternate entry points. */
1803 else if (alternate_entry
1804 && (sym->ns->proc_name->backend_decl == current_function_decl
1805 || parent_flag))
1807 gfc_entry_list *el = NULL;
1809 for (el = sym->ns->entries; el; el = el->next)
1810 if (sym == el->sym)
1812 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
1813 break;
1817 else if (entry_master
1818 && (sym->ns->proc_name->backend_decl == current_function_decl
1819 || parent_flag))
1820 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
1822 if (se_expr)
1823 se->expr = se_expr;
1825 /* Procedure actual arguments. */
1826 else if (sym->attr.flavor == FL_PROCEDURE
1827 && se->expr != current_function_decl)
1829 if (!sym->attr.dummy && !sym->attr.proc_pointer)
1831 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
1832 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
1834 return;
1838 /* Dereference the expression, where needed. Since characters
1839 are entirely different from other types, they are treated
1840 separately. */
1841 if (sym->ts.type == BT_CHARACTER)
1843 /* Dereference character pointer dummy arguments
1844 or results. */
1845 if ((sym->attr.pointer || sym->attr.allocatable)
1846 && (sym->attr.dummy
1847 || sym->attr.function
1848 || sym->attr.result))
1849 se->expr = build_fold_indirect_ref_loc (input_location,
1850 se->expr);
1853 else if (!sym->attr.value)
1855 /* Dereference non-character scalar dummy arguments. */
1856 if (sym->attr.dummy && !sym->attr.dimension
1857 && !(sym->attr.codimension && sym->attr.allocatable))
1858 se->expr = build_fold_indirect_ref_loc (input_location,
1859 se->expr);
1861 /* Dereference scalar hidden result. */
1862 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
1863 && (sym->attr.function || sym->attr.result)
1864 && !sym->attr.dimension && !sym->attr.pointer
1865 && !sym->attr.always_explicit)
1866 se->expr = build_fold_indirect_ref_loc (input_location,
1867 se->expr);
1869 /* Dereference non-character pointer variables.
1870 These must be dummies, results, or scalars. */
1871 if ((sym->attr.pointer || sym->attr.allocatable
1872 || gfc_is_associate_pointer (sym)
1873 || (sym->as && sym->as->type == AS_ASSUMED_RANK))
1874 && (sym->attr.dummy
1875 || sym->attr.function
1876 || sym->attr.result
1877 || (!sym->attr.dimension
1878 && (!sym->attr.codimension || !sym->attr.allocatable))))
1879 se->expr = build_fold_indirect_ref_loc (input_location,
1880 se->expr);
1883 ref = expr->ref;
1886 /* For character variables, also get the length. */
1887 if (sym->ts.type == BT_CHARACTER)
1889 /* If the character length of an entry isn't set, get the length from
1890 the master function instead. */
1891 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
1892 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
1893 else
1894 se->string_length = sym->ts.u.cl->backend_decl;
1895 gcc_assert (se->string_length);
1898 while (ref)
1900 switch (ref->type)
1902 case REF_ARRAY:
1903 /* Return the descriptor if that's what we want and this is an array
1904 section reference. */
1905 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
1906 return;
1907 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
1908 /* Return the descriptor for array pointers and allocations. */
1909 if (se->want_pointer
1910 && ref->next == NULL && (se->descriptor_only))
1911 return;
1913 gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
1914 /* Return a pointer to an element. */
1915 break;
1917 case REF_COMPONENT:
1918 if (ref->u.c.sym->attr.extension)
1919 conv_parent_component_references (se, ref);
1921 gfc_conv_component_ref (se, ref);
1922 if (!ref->next && ref->u.c.sym->attr.codimension
1923 && se->want_pointer && se->descriptor_only)
1924 return;
1926 break;
1928 case REF_SUBSTRING:
1929 gfc_conv_substring (se, ref, expr->ts.kind,
1930 expr->symtree->name, &expr->where);
1931 break;
1933 default:
1934 gcc_unreachable ();
1935 break;
1937 ref = ref->next;
1939 /* Pointer assignment, allocation or pass by reference. Arrays are handled
1940 separately. */
1941 if (se->want_pointer)
1943 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
1944 gfc_conv_string_parameter (se);
1945 else
1946 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
1951 /* Unary ops are easy... Or they would be if ! was a valid op. */
1953 static void
1954 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
1956 gfc_se operand;
1957 tree type;
1959 gcc_assert (expr->ts.type != BT_CHARACTER);
1960 /* Initialize the operand. */
1961 gfc_init_se (&operand, se);
1962 gfc_conv_expr_val (&operand, expr->value.op.op1);
1963 gfc_add_block_to_block (&se->pre, &operand.pre);
1965 type = gfc_typenode_for_spec (&expr->ts);
1967 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
1968 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
1969 All other unary operators have an equivalent GIMPLE unary operator. */
1970 if (code == TRUTH_NOT_EXPR)
1971 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
1972 build_int_cst (type, 0));
1973 else
1974 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
1978 /* Expand power operator to optimal multiplications when a value is raised
1979 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
1980 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
1981 Programming", 3rd Edition, 1998. */
1983 /* This code is mostly duplicated from expand_powi in the backend.
1984 We establish the "optimal power tree" lookup table with the defined size.
1985 The items in the table are the exponents used to calculate the index
1986 exponents. Any integer n less than the value can get an "addition chain",
1987 with the first node being one. */
1988 #define POWI_TABLE_SIZE 256
1990 /* The table is from builtins.c. */
1991 static const unsigned char powi_table[POWI_TABLE_SIZE] =
1993 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
1994 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
1995 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
1996 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
1997 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
1998 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
1999 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2000 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2001 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2002 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2003 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2004 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2005 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2006 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2007 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2008 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2009 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2010 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2011 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2012 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2013 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2014 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2015 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2016 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2017 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2018 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2019 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2020 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2021 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2022 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2023 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2024 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2027 /* If n is larger than lookup table's max index, we use the "window
2028 method". */
2029 #define POWI_WINDOW_SIZE 3
2031 /* Recursive function to expand the power operator. The temporary
2032 values are put in tmpvar. The function returns tmpvar[1] ** n. */
2033 static tree
2034 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
2036 tree op0;
2037 tree op1;
2038 tree tmp;
2039 int digit;
2041 if (n < POWI_TABLE_SIZE)
2043 if (tmpvar[n])
2044 return tmpvar[n];
2046 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
2047 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
2049 else if (n & 1)
2051 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2052 op0 = gfc_conv_powi (se, n - digit, tmpvar);
2053 op1 = gfc_conv_powi (se, digit, tmpvar);
2055 else
2057 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
2058 op1 = op0;
2061 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
2062 tmp = gfc_evaluate_now (tmp, &se->pre);
2064 if (n < POWI_TABLE_SIZE)
2065 tmpvar[n] = tmp;
2067 return tmp;
2071 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
2072 return 1. Else return 0 and a call to runtime library functions
2073 will have to be built. */
2074 static int
2075 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
2077 tree cond;
2078 tree tmp;
2079 tree type;
2080 tree vartmp[POWI_TABLE_SIZE];
2081 HOST_WIDE_INT m;
2082 unsigned HOST_WIDE_INT n;
2083 int sgn;
2085 /* If exponent is too large, we won't expand it anyway, so don't bother
2086 with large integer values. */
2087 if (!TREE_INT_CST (rhs).fits_shwi ())
2088 return 0;
2090 m = TREE_INT_CST (rhs).to_shwi ();
2091 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
2092 of the asymmetric range of the integer type. */
2093 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
2095 type = TREE_TYPE (lhs);
2096 sgn = tree_int_cst_sgn (rhs);
2098 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
2099 || optimize_size) && (m > 2 || m < -1))
2100 return 0;
2102 /* rhs == 0 */
2103 if (sgn == 0)
2105 se->expr = gfc_build_const (type, integer_one_node);
2106 return 1;
2109 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
2110 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
2112 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2113 lhs, build_int_cst (TREE_TYPE (lhs), -1));
2114 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2115 lhs, build_int_cst (TREE_TYPE (lhs), 1));
2117 /* If rhs is even,
2118 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
2119 if ((n & 1) == 0)
2121 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2122 boolean_type_node, tmp, cond);
2123 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2124 tmp, build_int_cst (type, 1),
2125 build_int_cst (type, 0));
2126 return 1;
2128 /* If rhs is odd,
2129 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
2130 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
2131 build_int_cst (type, -1),
2132 build_int_cst (type, 0));
2133 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2134 cond, build_int_cst (type, 1), tmp);
2135 return 1;
2138 memset (vartmp, 0, sizeof (vartmp));
2139 vartmp[1] = lhs;
2140 if (sgn == -1)
2142 tmp = gfc_build_const (type, integer_one_node);
2143 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
2144 vartmp[1]);
2147 se->expr = gfc_conv_powi (se, n, vartmp);
2149 return 1;
2153 /* Power op (**). Constant integer exponent has special handling. */
2155 static void
2156 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
2158 tree gfc_int4_type_node;
2159 int kind;
2160 int ikind;
2161 int res_ikind_1, res_ikind_2;
2162 gfc_se lse;
2163 gfc_se rse;
2164 tree fndecl = NULL;
2166 gfc_init_se (&lse, se);
2167 gfc_conv_expr_val (&lse, expr->value.op.op1);
2168 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
2169 gfc_add_block_to_block (&se->pre, &lse.pre);
2171 gfc_init_se (&rse, se);
2172 gfc_conv_expr_val (&rse, expr->value.op.op2);
2173 gfc_add_block_to_block (&se->pre, &rse.pre);
2175 if (expr->value.op.op2->ts.type == BT_INTEGER
2176 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
2177 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
2178 return;
2180 gfc_int4_type_node = gfc_get_int_type (4);
2182 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
2183 library routine. But in the end, we have to convert the result back
2184 if this case applies -- with res_ikind_K, we keep track whether operand K
2185 falls into this case. */
2186 res_ikind_1 = -1;
2187 res_ikind_2 = -1;
2189 kind = expr->value.op.op1->ts.kind;
2190 switch (expr->value.op.op2->ts.type)
2192 case BT_INTEGER:
2193 ikind = expr->value.op.op2->ts.kind;
2194 switch (ikind)
2196 case 1:
2197 case 2:
2198 rse.expr = convert (gfc_int4_type_node, rse.expr);
2199 res_ikind_2 = ikind;
2200 /* Fall through. */
2202 case 4:
2203 ikind = 0;
2204 break;
2206 case 8:
2207 ikind = 1;
2208 break;
2210 case 16:
2211 ikind = 2;
2212 break;
2214 default:
2215 gcc_unreachable ();
2217 switch (kind)
2219 case 1:
2220 case 2:
2221 if (expr->value.op.op1->ts.type == BT_INTEGER)
2223 lse.expr = convert (gfc_int4_type_node, lse.expr);
2224 res_ikind_1 = kind;
2226 else
2227 gcc_unreachable ();
2228 /* Fall through. */
2230 case 4:
2231 kind = 0;
2232 break;
2234 case 8:
2235 kind = 1;
2236 break;
2238 case 10:
2239 kind = 2;
2240 break;
2242 case 16:
2243 kind = 3;
2244 break;
2246 default:
2247 gcc_unreachable ();
2250 switch (expr->value.op.op1->ts.type)
2252 case BT_INTEGER:
2253 if (kind == 3) /* Case 16 was not handled properly above. */
2254 kind = 2;
2255 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
2256 break;
2258 case BT_REAL:
2259 /* Use builtins for real ** int4. */
2260 if (ikind == 0)
2262 switch (kind)
2264 case 0:
2265 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
2266 break;
2268 case 1:
2269 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
2270 break;
2272 case 2:
2273 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
2274 break;
2276 case 3:
2277 /* Use the __builtin_powil() only if real(kind=16) is
2278 actually the C long double type. */
2279 if (!gfc_real16_is_float128)
2280 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
2281 break;
2283 default:
2284 gcc_unreachable ();
2288 /* If we don't have a good builtin for this, go for the
2289 library function. */
2290 if (!fndecl)
2291 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
2292 break;
2294 case BT_COMPLEX:
2295 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
2296 break;
2298 default:
2299 gcc_unreachable ();
2301 break;
2303 case BT_REAL:
2304 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
2305 break;
2307 case BT_COMPLEX:
2308 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
2309 break;
2311 default:
2312 gcc_unreachable ();
2313 break;
2316 se->expr = build_call_expr_loc (input_location,
2317 fndecl, 2, lse.expr, rse.expr);
2319 /* Convert the result back if it is of wrong integer kind. */
2320 if (res_ikind_1 != -1 && res_ikind_2 != -1)
2322 /* We want the maximum of both operand kinds as result. */
2323 if (res_ikind_1 < res_ikind_2)
2324 res_ikind_1 = res_ikind_2;
2325 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
2330 /* Generate code to allocate a string temporary. */
2332 tree
2333 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
2335 tree var;
2336 tree tmp;
2338 if (gfc_can_put_var_on_stack (len))
2340 /* Create a temporary variable to hold the result. */
2341 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2342 gfc_charlen_type_node, len,
2343 build_int_cst (gfc_charlen_type_node, 1));
2344 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
2346 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
2347 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
2348 else
2349 tmp = build_array_type (TREE_TYPE (type), tmp);
2351 var = gfc_create_var (tmp, "str");
2352 var = gfc_build_addr_expr (type, var);
2354 else
2356 /* Allocate a temporary to hold the result. */
2357 var = gfc_create_var (type, "pstr");
2358 tmp = gfc_call_malloc (&se->pre, type,
2359 fold_build2_loc (input_location, MULT_EXPR,
2360 TREE_TYPE (len), len,
2361 fold_convert (TREE_TYPE (len),
2362 TYPE_SIZE (type))));
2363 gfc_add_modify (&se->pre, var, tmp);
2365 /* Free the temporary afterwards. */
2366 tmp = gfc_call_free (convert (pvoid_type_node, var));
2367 gfc_add_expr_to_block (&se->post, tmp);
2370 return var;
2374 /* Handle a string concatenation operation. A temporary will be allocated to
2375 hold the result. */
2377 static void
2378 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
2380 gfc_se lse, rse;
2381 tree len, type, var, tmp, fndecl;
2383 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
2384 && expr->value.op.op2->ts.type == BT_CHARACTER);
2385 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
2387 gfc_init_se (&lse, se);
2388 gfc_conv_expr (&lse, expr->value.op.op1);
2389 gfc_conv_string_parameter (&lse);
2390 gfc_init_se (&rse, se);
2391 gfc_conv_expr (&rse, expr->value.op.op2);
2392 gfc_conv_string_parameter (&rse);
2394 gfc_add_block_to_block (&se->pre, &lse.pre);
2395 gfc_add_block_to_block (&se->pre, &rse.pre);
2397 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
2398 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
2399 if (len == NULL_TREE)
2401 len = fold_build2_loc (input_location, PLUS_EXPR,
2402 TREE_TYPE (lse.string_length),
2403 lse.string_length, rse.string_length);
2406 type = build_pointer_type (type);
2408 var = gfc_conv_string_tmp (se, type, len);
2410 /* Do the actual concatenation. */
2411 if (expr->ts.kind == 1)
2412 fndecl = gfor_fndecl_concat_string;
2413 else if (expr->ts.kind == 4)
2414 fndecl = gfor_fndecl_concat_string_char4;
2415 else
2416 gcc_unreachable ();
2418 tmp = build_call_expr_loc (input_location,
2419 fndecl, 6, len, var, lse.string_length, lse.expr,
2420 rse.string_length, rse.expr);
2421 gfc_add_expr_to_block (&se->pre, tmp);
2423 /* Add the cleanup for the operands. */
2424 gfc_add_block_to_block (&se->pre, &rse.post);
2425 gfc_add_block_to_block (&se->pre, &lse.post);
2427 se->expr = var;
2428 se->string_length = len;
2431 /* Translates an op expression. Common (binary) cases are handled by this
2432 function, others are passed on. Recursion is used in either case.
2433 We use the fact that (op1.ts == op2.ts) (except for the power
2434 operator **).
2435 Operators need no special handling for scalarized expressions as long as
2436 they call gfc_conv_simple_val to get their operands.
2437 Character strings get special handling. */
2439 static void
2440 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
2442 enum tree_code code;
2443 gfc_se lse;
2444 gfc_se rse;
2445 tree tmp, type;
2446 int lop;
2447 int checkstring;
2449 checkstring = 0;
2450 lop = 0;
2451 switch (expr->value.op.op)
2453 case INTRINSIC_PARENTHESES:
2454 if ((expr->ts.type == BT_REAL
2455 || expr->ts.type == BT_COMPLEX)
2456 && gfc_option.flag_protect_parens)
2458 gfc_conv_unary_op (PAREN_EXPR, se, expr);
2459 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
2460 return;
2463 /* Fallthrough. */
2464 case INTRINSIC_UPLUS:
2465 gfc_conv_expr (se, expr->value.op.op1);
2466 return;
2468 case INTRINSIC_UMINUS:
2469 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
2470 return;
2472 case INTRINSIC_NOT:
2473 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
2474 return;
2476 case INTRINSIC_PLUS:
2477 code = PLUS_EXPR;
2478 break;
2480 case INTRINSIC_MINUS:
2481 code = MINUS_EXPR;
2482 break;
2484 case INTRINSIC_TIMES:
2485 code = MULT_EXPR;
2486 break;
2488 case INTRINSIC_DIVIDE:
2489 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
2490 an integer, we must round towards zero, so we use a
2491 TRUNC_DIV_EXPR. */
2492 if (expr->ts.type == BT_INTEGER)
2493 code = TRUNC_DIV_EXPR;
2494 else
2495 code = RDIV_EXPR;
2496 break;
2498 case INTRINSIC_POWER:
2499 gfc_conv_power_op (se, expr);
2500 return;
2502 case INTRINSIC_CONCAT:
2503 gfc_conv_concat_op (se, expr);
2504 return;
2506 case INTRINSIC_AND:
2507 code = TRUTH_ANDIF_EXPR;
2508 lop = 1;
2509 break;
2511 case INTRINSIC_OR:
2512 code = TRUTH_ORIF_EXPR;
2513 lop = 1;
2514 break;
2516 /* EQV and NEQV only work on logicals, but since we represent them
2517 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
2518 case INTRINSIC_EQ:
2519 case INTRINSIC_EQ_OS:
2520 case INTRINSIC_EQV:
2521 code = EQ_EXPR;
2522 checkstring = 1;
2523 lop = 1;
2524 break;
2526 case INTRINSIC_NE:
2527 case INTRINSIC_NE_OS:
2528 case INTRINSIC_NEQV:
2529 code = NE_EXPR;
2530 checkstring = 1;
2531 lop = 1;
2532 break;
2534 case INTRINSIC_GT:
2535 case INTRINSIC_GT_OS:
2536 code = GT_EXPR;
2537 checkstring = 1;
2538 lop = 1;
2539 break;
2541 case INTRINSIC_GE:
2542 case INTRINSIC_GE_OS:
2543 code = GE_EXPR;
2544 checkstring = 1;
2545 lop = 1;
2546 break;
2548 case INTRINSIC_LT:
2549 case INTRINSIC_LT_OS:
2550 code = LT_EXPR;
2551 checkstring = 1;
2552 lop = 1;
2553 break;
2555 case INTRINSIC_LE:
2556 case INTRINSIC_LE_OS:
2557 code = LE_EXPR;
2558 checkstring = 1;
2559 lop = 1;
2560 break;
2562 case INTRINSIC_USER:
2563 case INTRINSIC_ASSIGN:
2564 /* These should be converted into function calls by the frontend. */
2565 gcc_unreachable ();
2567 default:
2568 fatal_error ("Unknown intrinsic op");
2569 return;
2572 /* The only exception to this is **, which is handled separately anyway. */
2573 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
2575 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
2576 checkstring = 0;
2578 /* lhs */
2579 gfc_init_se (&lse, se);
2580 gfc_conv_expr (&lse, expr->value.op.op1);
2581 gfc_add_block_to_block (&se->pre, &lse.pre);
2583 /* rhs */
2584 gfc_init_se (&rse, se);
2585 gfc_conv_expr (&rse, expr->value.op.op2);
2586 gfc_add_block_to_block (&se->pre, &rse.pre);
2588 if (checkstring)
2590 gfc_conv_string_parameter (&lse);
2591 gfc_conv_string_parameter (&rse);
2593 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
2594 rse.string_length, rse.expr,
2595 expr->value.op.op1->ts.kind,
2596 code);
2597 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
2598 gfc_add_block_to_block (&lse.post, &rse.post);
2601 type = gfc_typenode_for_spec (&expr->ts);
2603 if (lop)
2605 /* The result of logical ops is always boolean_type_node. */
2606 tmp = fold_build2_loc (input_location, code, boolean_type_node,
2607 lse.expr, rse.expr);
2608 se->expr = convert (type, tmp);
2610 else
2611 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
2613 /* Add the post blocks. */
2614 gfc_add_block_to_block (&se->post, &rse.post);
2615 gfc_add_block_to_block (&se->post, &lse.post);
2618 /* If a string's length is one, we convert it to a single character. */
2620 tree
2621 gfc_string_to_single_character (tree len, tree str, int kind)
2624 if (len == NULL
2625 || !INTEGER_CST_P (len) || TREE_INT_CST_HIGH (len) != 0
2626 || !POINTER_TYPE_P (TREE_TYPE (str)))
2627 return NULL_TREE;
2629 if (TREE_INT_CST_LOW (len) == 1)
2631 str = fold_convert (gfc_get_pchar_type (kind), str);
2632 return build_fold_indirect_ref_loc (input_location, str);
2635 if (kind == 1
2636 && TREE_CODE (str) == ADDR_EXPR
2637 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
2638 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
2639 && array_ref_low_bound (TREE_OPERAND (str, 0))
2640 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
2641 && TREE_INT_CST_LOW (len) > 1
2642 && TREE_INT_CST_LOW (len)
2643 == (unsigned HOST_WIDE_INT)
2644 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
2646 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
2647 ret = build_fold_indirect_ref_loc (input_location, ret);
2648 if (TREE_CODE (ret) == INTEGER_CST)
2650 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
2651 int i, length = TREE_STRING_LENGTH (string_cst);
2652 const char *ptr = TREE_STRING_POINTER (string_cst);
2654 for (i = 1; i < length; i++)
2655 if (ptr[i] != ' ')
2656 return NULL_TREE;
2658 return ret;
2662 return NULL_TREE;
2666 void
2667 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
2670 if (sym->backend_decl)
2672 /* This becomes the nominal_type in
2673 function.c:assign_parm_find_data_types. */
2674 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
2675 /* This becomes the passed_type in
2676 function.c:assign_parm_find_data_types. C promotes char to
2677 integer for argument passing. */
2678 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
2680 DECL_BY_REFERENCE (sym->backend_decl) = 0;
2683 if (expr != NULL)
2685 /* If we have a constant character expression, make it into an
2686 integer. */
2687 if ((*expr)->expr_type == EXPR_CONSTANT)
2689 gfc_typespec ts;
2690 gfc_clear_ts (&ts);
2692 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
2693 (int)(*expr)->value.character.string[0]);
2694 if ((*expr)->ts.kind != gfc_c_int_kind)
2696 /* The expr needs to be compatible with a C int. If the
2697 conversion fails, then the 2 causes an ICE. */
2698 ts.type = BT_INTEGER;
2699 ts.kind = gfc_c_int_kind;
2700 gfc_convert_type (*expr, &ts, 2);
2703 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
2705 if ((*expr)->ref == NULL)
2707 se->expr = gfc_string_to_single_character
2708 (build_int_cst (integer_type_node, 1),
2709 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
2710 gfc_get_symbol_decl
2711 ((*expr)->symtree->n.sym)),
2712 (*expr)->ts.kind);
2714 else
2716 gfc_conv_variable (se, *expr);
2717 se->expr = gfc_string_to_single_character
2718 (build_int_cst (integer_type_node, 1),
2719 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
2720 se->expr),
2721 (*expr)->ts.kind);
2727 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
2728 if STR is a string literal, otherwise return -1. */
2730 static int
2731 gfc_optimize_len_trim (tree len, tree str, int kind)
2733 if (kind == 1
2734 && TREE_CODE (str) == ADDR_EXPR
2735 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
2736 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
2737 && array_ref_low_bound (TREE_OPERAND (str, 0))
2738 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
2739 && TREE_INT_CST_LOW (len) >= 1
2740 && TREE_INT_CST_LOW (len)
2741 == (unsigned HOST_WIDE_INT)
2742 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
2744 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
2745 folded = build_fold_indirect_ref_loc (input_location, folded);
2746 if (TREE_CODE (folded) == INTEGER_CST)
2748 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
2749 int length = TREE_STRING_LENGTH (string_cst);
2750 const char *ptr = TREE_STRING_POINTER (string_cst);
2752 for (; length > 0; length--)
2753 if (ptr[length - 1] != ' ')
2754 break;
2756 return length;
2759 return -1;
2762 /* Helper to build a call to memcmp. */
2764 static tree
2765 build_memcmp_call (tree s1, tree s2, tree n)
2767 tree tmp;
2769 if (!POINTER_TYPE_P (TREE_TYPE (s1)))
2770 s1 = gfc_build_addr_expr (pvoid_type_node, s1);
2771 else
2772 s1 = fold_convert (pvoid_type_node, s1);
2774 if (!POINTER_TYPE_P (TREE_TYPE (s2)))
2775 s2 = gfc_build_addr_expr (pvoid_type_node, s2);
2776 else
2777 s2 = fold_convert (pvoid_type_node, s2);
2779 n = fold_convert (size_type_node, n);
2781 tmp = build_call_expr_loc (input_location,
2782 builtin_decl_explicit (BUILT_IN_MEMCMP),
2783 3, s1, s2, n);
2785 return fold_convert (integer_type_node, tmp);
2788 /* Compare two strings. If they are all single characters, the result is the
2789 subtraction of them. Otherwise, we build a library call. */
2791 tree
2792 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
2793 enum tree_code code)
2795 tree sc1;
2796 tree sc2;
2797 tree fndecl;
2799 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
2800 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
2802 sc1 = gfc_string_to_single_character (len1, str1, kind);
2803 sc2 = gfc_string_to_single_character (len2, str2, kind);
2805 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
2807 /* Deal with single character specially. */
2808 sc1 = fold_convert (integer_type_node, sc1);
2809 sc2 = fold_convert (integer_type_node, sc2);
2810 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
2811 sc1, sc2);
2814 if ((code == EQ_EXPR || code == NE_EXPR)
2815 && optimize
2816 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
2818 /* If one string is a string literal with LEN_TRIM longer
2819 than the length of the second string, the strings
2820 compare unequal. */
2821 int len = gfc_optimize_len_trim (len1, str1, kind);
2822 if (len > 0 && compare_tree_int (len2, len) < 0)
2823 return integer_one_node;
2824 len = gfc_optimize_len_trim (len2, str2, kind);
2825 if (len > 0 && compare_tree_int (len1, len) < 0)
2826 return integer_one_node;
2829 /* We can compare via memcpy if the strings are known to be equal
2830 in length and they are
2831 - kind=1
2832 - kind=4 and the comparison is for (in)equality. */
2834 if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
2835 && tree_int_cst_equal (len1, len2)
2836 && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
2838 tree tmp;
2839 tree chartype;
2841 chartype = gfc_get_char_type (kind);
2842 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
2843 fold_convert (TREE_TYPE(len1),
2844 TYPE_SIZE_UNIT(chartype)),
2845 len1);
2846 return build_memcmp_call (str1, str2, tmp);
2849 /* Build a call for the comparison. */
2850 if (kind == 1)
2851 fndecl = gfor_fndecl_compare_string;
2852 else if (kind == 4)
2853 fndecl = gfor_fndecl_compare_string_char4;
2854 else
2855 gcc_unreachable ();
2857 return build_call_expr_loc (input_location, fndecl, 4,
2858 len1, str1, len2, str2);
2862 /* Return the backend_decl for a procedure pointer component. */
2864 static tree
2865 get_proc_ptr_comp (gfc_expr *e)
2867 gfc_se comp_se;
2868 gfc_expr *e2;
2869 expr_t old_type;
2871 gfc_init_se (&comp_se, NULL);
2872 e2 = gfc_copy_expr (e);
2873 /* We have to restore the expr type later so that gfc_free_expr frees
2874 the exact same thing that was allocated.
2875 TODO: This is ugly. */
2876 old_type = e2->expr_type;
2877 e2->expr_type = EXPR_VARIABLE;
2878 gfc_conv_expr (&comp_se, e2);
2879 e2->expr_type = old_type;
2880 gfc_free_expr (e2);
2881 return build_fold_addr_expr_loc (input_location, comp_se.expr);
2885 /* Convert a typebound function reference from a class object. */
2886 static void
2887 conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
2889 gfc_ref *ref;
2890 tree var;
2892 if (TREE_CODE (base_object) != VAR_DECL)
2894 var = gfc_create_var (TREE_TYPE (base_object), NULL);
2895 gfc_add_modify (&se->pre, var, base_object);
2897 se->expr = gfc_class_vptr_get (base_object);
2898 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
2899 ref = expr->ref;
2900 while (ref && ref->next)
2901 ref = ref->next;
2902 gcc_assert (ref && ref->type == REF_COMPONENT);
2903 if (ref->u.c.sym->attr.extension)
2904 conv_parent_component_references (se, ref);
2905 gfc_conv_component_ref (se, ref);
2906 se->expr = build_fold_addr_expr_loc (input_location, se->expr);
2910 static void
2911 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
2913 tree tmp;
2915 if (gfc_is_proc_ptr_comp (expr))
2916 tmp = get_proc_ptr_comp (expr);
2917 else if (sym->attr.dummy)
2919 tmp = gfc_get_symbol_decl (sym);
2920 if (sym->attr.proc_pointer)
2921 tmp = build_fold_indirect_ref_loc (input_location,
2922 tmp);
2923 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
2924 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
2926 else
2928 if (!sym->backend_decl)
2929 sym->backend_decl = gfc_get_extern_function_decl (sym);
2931 TREE_USED (sym->backend_decl) = 1;
2933 tmp = sym->backend_decl;
2935 if (sym->attr.cray_pointee)
2937 /* TODO - make the cray pointee a pointer to a procedure,
2938 assign the pointer to it and use it for the call. This
2939 will do for now! */
2940 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
2941 gfc_get_symbol_decl (sym->cp_pointer));
2942 tmp = gfc_evaluate_now (tmp, &se->pre);
2945 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
2947 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
2948 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
2951 se->expr = tmp;
2955 /* Initialize MAPPING. */
2957 void
2958 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
2960 mapping->syms = NULL;
2961 mapping->charlens = NULL;
2965 /* Free all memory held by MAPPING (but not MAPPING itself). */
2967 void
2968 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
2970 gfc_interface_sym_mapping *sym;
2971 gfc_interface_sym_mapping *nextsym;
2972 gfc_charlen *cl;
2973 gfc_charlen *nextcl;
2975 for (sym = mapping->syms; sym; sym = nextsym)
2977 nextsym = sym->next;
2978 sym->new_sym->n.sym->formal = NULL;
2979 gfc_free_symbol (sym->new_sym->n.sym);
2980 gfc_free_expr (sym->expr);
2981 free (sym->new_sym);
2982 free (sym);
2984 for (cl = mapping->charlens; cl; cl = nextcl)
2986 nextcl = cl->next;
2987 gfc_free_expr (cl->length);
2988 free (cl);
2993 /* Return a copy of gfc_charlen CL. Add the returned structure to
2994 MAPPING so that it will be freed by gfc_free_interface_mapping. */
2996 static gfc_charlen *
2997 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
2998 gfc_charlen * cl)
3000 gfc_charlen *new_charlen;
3002 new_charlen = gfc_get_charlen ();
3003 new_charlen->next = mapping->charlens;
3004 new_charlen->length = gfc_copy_expr (cl->length);
3006 mapping->charlens = new_charlen;
3007 return new_charlen;
3011 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
3012 array variable that can be used as the actual argument for dummy
3013 argument SYM. Add any initialization code to BLOCK. PACKED is as
3014 for gfc_get_nodesc_array_type and DATA points to the first element
3015 in the passed array. */
3017 static tree
3018 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
3019 gfc_packed packed, tree data)
3021 tree type;
3022 tree var;
3024 type = gfc_typenode_for_spec (&sym->ts);
3025 type = gfc_get_nodesc_array_type (type, sym->as, packed,
3026 !sym->attr.target && !sym->attr.pointer
3027 && !sym->attr.proc_pointer);
3029 var = gfc_create_var (type, "ifm");
3030 gfc_add_modify (block, var, fold_convert (type, data));
3032 return var;
3036 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
3037 and offset of descriptorless array type TYPE given that it has the same
3038 size as DESC. Add any set-up code to BLOCK. */
3040 static void
3041 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
3043 int n;
3044 tree dim;
3045 tree offset;
3046 tree tmp;
3048 offset = gfc_index_zero_node;
3049 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
3051 dim = gfc_rank_cst[n];
3052 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
3053 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
3055 GFC_TYPE_ARRAY_LBOUND (type, n)
3056 = gfc_conv_descriptor_lbound_get (desc, dim);
3057 GFC_TYPE_ARRAY_UBOUND (type, n)
3058 = gfc_conv_descriptor_ubound_get (desc, dim);
3060 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
3062 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3063 gfc_array_index_type,
3064 gfc_conv_descriptor_ubound_get (desc, dim),
3065 gfc_conv_descriptor_lbound_get (desc, dim));
3066 tmp = fold_build2_loc (input_location, PLUS_EXPR,
3067 gfc_array_index_type,
3068 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
3069 tmp = gfc_evaluate_now (tmp, block);
3070 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
3072 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
3073 GFC_TYPE_ARRAY_LBOUND (type, n),
3074 GFC_TYPE_ARRAY_STRIDE (type, n));
3075 offset = fold_build2_loc (input_location, MINUS_EXPR,
3076 gfc_array_index_type, offset, tmp);
3078 offset = gfc_evaluate_now (offset, block);
3079 GFC_TYPE_ARRAY_OFFSET (type) = offset;
3083 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
3084 in SE. The caller may still use se->expr and se->string_length after
3085 calling this function. */
3087 void
3088 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
3089 gfc_symbol * sym, gfc_se * se,
3090 gfc_expr *expr)
3092 gfc_interface_sym_mapping *sm;
3093 tree desc;
3094 tree tmp;
3095 tree value;
3096 gfc_symbol *new_sym;
3097 gfc_symtree *root;
3098 gfc_symtree *new_symtree;
3100 /* Create a new symbol to represent the actual argument. */
3101 new_sym = gfc_new_symbol (sym->name, NULL);
3102 new_sym->ts = sym->ts;
3103 new_sym->as = gfc_copy_array_spec (sym->as);
3104 new_sym->attr.referenced = 1;
3105 new_sym->attr.dimension = sym->attr.dimension;
3106 new_sym->attr.contiguous = sym->attr.contiguous;
3107 new_sym->attr.codimension = sym->attr.codimension;
3108 new_sym->attr.pointer = sym->attr.pointer;
3109 new_sym->attr.allocatable = sym->attr.allocatable;
3110 new_sym->attr.flavor = sym->attr.flavor;
3111 new_sym->attr.function = sym->attr.function;
3113 /* Ensure that the interface is available and that
3114 descriptors are passed for array actual arguments. */
3115 if (sym->attr.flavor == FL_PROCEDURE)
3117 new_sym->formal = expr->symtree->n.sym->formal;
3118 new_sym->attr.always_explicit
3119 = expr->symtree->n.sym->attr.always_explicit;
3122 /* Create a fake symtree for it. */
3123 root = NULL;
3124 new_symtree = gfc_new_symtree (&root, sym->name);
3125 new_symtree->n.sym = new_sym;
3126 gcc_assert (new_symtree == root);
3128 /* Create a dummy->actual mapping. */
3129 sm = XCNEW (gfc_interface_sym_mapping);
3130 sm->next = mapping->syms;
3131 sm->old = sym;
3132 sm->new_sym = new_symtree;
3133 sm->expr = gfc_copy_expr (expr);
3134 mapping->syms = sm;
3136 /* Stabilize the argument's value. */
3137 if (!sym->attr.function && se)
3138 se->expr = gfc_evaluate_now (se->expr, &se->pre);
3140 if (sym->ts.type == BT_CHARACTER)
3142 /* Create a copy of the dummy argument's length. */
3143 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
3144 sm->expr->ts.u.cl = new_sym->ts.u.cl;
3146 /* If the length is specified as "*", record the length that
3147 the caller is passing. We should use the callee's length
3148 in all other cases. */
3149 if (!new_sym->ts.u.cl->length && se)
3151 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
3152 new_sym->ts.u.cl->backend_decl = se->string_length;
3156 if (!se)
3157 return;
3159 /* Use the passed value as-is if the argument is a function. */
3160 if (sym->attr.flavor == FL_PROCEDURE)
3161 value = se->expr;
3163 /* If the argument is either a string or a pointer to a string,
3164 convert it to a boundless character type. */
3165 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
3167 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
3168 tmp = build_pointer_type (tmp);
3169 if (sym->attr.pointer)
3170 value = build_fold_indirect_ref_loc (input_location,
3171 se->expr);
3172 else
3173 value = se->expr;
3174 value = fold_convert (tmp, value);
3177 /* If the argument is a scalar, a pointer to an array or an allocatable,
3178 dereference it. */
3179 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
3180 value = build_fold_indirect_ref_loc (input_location,
3181 se->expr);
3183 /* For character(*), use the actual argument's descriptor. */
3184 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
3185 value = build_fold_indirect_ref_loc (input_location,
3186 se->expr);
3188 /* If the argument is an array descriptor, use it to determine
3189 information about the actual argument's shape. */
3190 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
3191 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
3193 /* Get the actual argument's descriptor. */
3194 desc = build_fold_indirect_ref_loc (input_location,
3195 se->expr);
3197 /* Create the replacement variable. */
3198 tmp = gfc_conv_descriptor_data_get (desc);
3199 value = gfc_get_interface_mapping_array (&se->pre, sym,
3200 PACKED_NO, tmp);
3202 /* Use DESC to work out the upper bounds, strides and offset. */
3203 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
3205 else
3206 /* Otherwise we have a packed array. */
3207 value = gfc_get_interface_mapping_array (&se->pre, sym,
3208 PACKED_FULL, se->expr);
3210 new_sym->backend_decl = value;
3214 /* Called once all dummy argument mappings have been added to MAPPING,
3215 but before the mapping is used to evaluate expressions. Pre-evaluate
3216 the length of each argument, adding any initialization code to PRE and
3217 any finalization code to POST. */
3219 void
3220 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
3221 stmtblock_t * pre, stmtblock_t * post)
3223 gfc_interface_sym_mapping *sym;
3224 gfc_expr *expr;
3225 gfc_se se;
3227 for (sym = mapping->syms; sym; sym = sym->next)
3228 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
3229 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
3231 expr = sym->new_sym->n.sym->ts.u.cl->length;
3232 gfc_apply_interface_mapping_to_expr (mapping, expr);
3233 gfc_init_se (&se, NULL);
3234 gfc_conv_expr (&se, expr);
3235 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
3236 se.expr = gfc_evaluate_now (se.expr, &se.pre);
3237 gfc_add_block_to_block (pre, &se.pre);
3238 gfc_add_block_to_block (post, &se.post);
3240 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
3245 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3246 constructor C. */
3248 static void
3249 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
3250 gfc_constructor_base base)
3252 gfc_constructor *c;
3253 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
3255 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
3256 if (c->iterator)
3258 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
3259 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
3260 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
3266 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3267 reference REF. */
3269 static void
3270 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
3271 gfc_ref * ref)
3273 int n;
3275 for (; ref; ref = ref->next)
3276 switch (ref->type)
3278 case REF_ARRAY:
3279 for (n = 0; n < ref->u.ar.dimen; n++)
3281 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
3282 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
3283 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
3285 break;
3287 case REF_COMPONENT:
3288 break;
3290 case REF_SUBSTRING:
3291 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
3292 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
3293 break;
3298 /* Convert intrinsic function calls into result expressions. */
3300 static bool
3301 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
3303 gfc_symbol *sym;
3304 gfc_expr *new_expr;
3305 gfc_expr *arg1;
3306 gfc_expr *arg2;
3307 int d, dup;
3309 arg1 = expr->value.function.actual->expr;
3310 if (expr->value.function.actual->next)
3311 arg2 = expr->value.function.actual->next->expr;
3312 else
3313 arg2 = NULL;
3315 sym = arg1->symtree->n.sym;
3317 if (sym->attr.dummy)
3318 return false;
3320 new_expr = NULL;
3322 switch (expr->value.function.isym->id)
3324 case GFC_ISYM_LEN:
3325 /* TODO figure out why this condition is necessary. */
3326 if (sym->attr.function
3327 && (arg1->ts.u.cl->length == NULL
3328 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
3329 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
3330 return false;
3332 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
3333 break;
3335 case GFC_ISYM_SIZE:
3336 if (!sym->as || sym->as->rank == 0)
3337 return false;
3339 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
3341 dup = mpz_get_si (arg2->value.integer);
3342 d = dup - 1;
3344 else
3346 dup = sym->as->rank;
3347 d = 0;
3350 for (; d < dup; d++)
3352 gfc_expr *tmp;
3354 if (!sym->as->upper[d] || !sym->as->lower[d])
3356 gfc_free_expr (new_expr);
3357 return false;
3360 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
3361 gfc_get_int_expr (gfc_default_integer_kind,
3362 NULL, 1));
3363 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
3364 if (new_expr)
3365 new_expr = gfc_multiply (new_expr, tmp);
3366 else
3367 new_expr = tmp;
3369 break;
3371 case GFC_ISYM_LBOUND:
3372 case GFC_ISYM_UBOUND:
3373 /* TODO These implementations of lbound and ubound do not limit if
3374 the size < 0, according to F95's 13.14.53 and 13.14.113. */
3376 if (!sym->as || sym->as->rank == 0)
3377 return false;
3379 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
3380 d = mpz_get_si (arg2->value.integer) - 1;
3381 else
3382 /* TODO: If the need arises, this could produce an array of
3383 ubound/lbounds. */
3384 gcc_unreachable ();
3386 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
3388 if (sym->as->lower[d])
3389 new_expr = gfc_copy_expr (sym->as->lower[d]);
3391 else
3393 if (sym->as->upper[d])
3394 new_expr = gfc_copy_expr (sym->as->upper[d]);
3396 break;
3398 default:
3399 break;
3402 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
3403 if (!new_expr)
3404 return false;
3406 gfc_replace_expr (expr, new_expr);
3407 return true;
3411 static void
3412 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
3413 gfc_interface_mapping * mapping)
3415 gfc_formal_arglist *f;
3416 gfc_actual_arglist *actual;
3418 actual = expr->value.function.actual;
3419 f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
3421 for (; f && actual; f = f->next, actual = actual->next)
3423 if (!actual->expr)
3424 continue;
3426 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
3429 if (map_expr->symtree->n.sym->attr.dimension)
3431 int d;
3432 gfc_array_spec *as;
3434 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
3436 for (d = 0; d < as->rank; d++)
3438 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
3439 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
3442 expr->value.function.esym->as = as;
3445 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
3447 expr->value.function.esym->ts.u.cl->length
3448 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
3450 gfc_apply_interface_mapping_to_expr (mapping,
3451 expr->value.function.esym->ts.u.cl->length);
3456 /* EXPR is a copy of an expression that appeared in the interface
3457 associated with MAPPING. Walk it recursively looking for references to
3458 dummy arguments that MAPPING maps to actual arguments. Replace each such
3459 reference with a reference to the associated actual argument. */
3461 static void
3462 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
3463 gfc_expr * expr)
3465 gfc_interface_sym_mapping *sym;
3466 gfc_actual_arglist *actual;
3468 if (!expr)
3469 return;
3471 /* Copying an expression does not copy its length, so do that here. */
3472 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
3474 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
3475 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
3478 /* Apply the mapping to any references. */
3479 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
3481 /* ...and to the expression's symbol, if it has one. */
3482 /* TODO Find out why the condition on expr->symtree had to be moved into
3483 the loop rather than being outside it, as originally. */
3484 for (sym = mapping->syms; sym; sym = sym->next)
3485 if (expr->symtree && sym->old == expr->symtree->n.sym)
3487 if (sym->new_sym->n.sym->backend_decl)
3488 expr->symtree = sym->new_sym;
3489 else if (sym->expr)
3490 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
3491 /* Replace base type for polymorphic arguments. */
3492 if (expr->ref && expr->ref->type == REF_COMPONENT
3493 && sym->expr && sym->expr->ts.type == BT_CLASS)
3494 expr->ref->u.c.sym = sym->expr->ts.u.derived;
3497 /* ...and to subexpressions in expr->value. */
3498 switch (expr->expr_type)
3500 case EXPR_VARIABLE:
3501 case EXPR_CONSTANT:
3502 case EXPR_NULL:
3503 case EXPR_SUBSTRING:
3504 break;
3506 case EXPR_OP:
3507 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
3508 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
3509 break;
3511 case EXPR_FUNCTION:
3512 for (actual = expr->value.function.actual; actual; actual = actual->next)
3513 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
3515 if (expr->value.function.esym == NULL
3516 && expr->value.function.isym != NULL
3517 && expr->value.function.actual->expr->symtree
3518 && gfc_map_intrinsic_function (expr, mapping))
3519 break;
3521 for (sym = mapping->syms; sym; sym = sym->next)
3522 if (sym->old == expr->value.function.esym)
3524 expr->value.function.esym = sym->new_sym->n.sym;
3525 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
3526 expr->value.function.esym->result = sym->new_sym->n.sym;
3528 break;
3530 case EXPR_ARRAY:
3531 case EXPR_STRUCTURE:
3532 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
3533 break;
3535 case EXPR_COMPCALL:
3536 case EXPR_PPC:
3537 gcc_unreachable ();
3538 break;
3541 return;
3545 /* Evaluate interface expression EXPR using MAPPING. Store the result
3546 in SE. */
3548 void
3549 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
3550 gfc_se * se, gfc_expr * expr)
3552 expr = gfc_copy_expr (expr);
3553 gfc_apply_interface_mapping_to_expr (mapping, expr);
3554 gfc_conv_expr (se, expr);
3555 se->expr = gfc_evaluate_now (se->expr, &se->pre);
3556 gfc_free_expr (expr);
3560 /* Returns a reference to a temporary array into which a component of
3561 an actual argument derived type array is copied and then returned
3562 after the function call. */
3563 void
3564 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
3565 sym_intent intent, bool formal_ptr)
3567 gfc_se lse;
3568 gfc_se rse;
3569 gfc_ss *lss;
3570 gfc_ss *rss;
3571 gfc_loopinfo loop;
3572 gfc_loopinfo loop2;
3573 gfc_array_info *info;
3574 tree offset;
3575 tree tmp_index;
3576 tree tmp;
3577 tree base_type;
3578 tree size;
3579 stmtblock_t body;
3580 int n;
3581 int dimen;
3583 gcc_assert (expr->expr_type == EXPR_VARIABLE);
3585 gfc_init_se (&lse, NULL);
3586 gfc_init_se (&rse, NULL);
3588 /* Walk the argument expression. */
3589 rss = gfc_walk_expr (expr);
3591 gcc_assert (rss != gfc_ss_terminator);
3593 /* Initialize the scalarizer. */
3594 gfc_init_loopinfo (&loop);
3595 gfc_add_ss_to_loop (&loop, rss);
3597 /* Calculate the bounds of the scalarization. */
3598 gfc_conv_ss_startstride (&loop);
3600 /* Build an ss for the temporary. */
3601 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
3602 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
3604 base_type = gfc_typenode_for_spec (&expr->ts);
3605 if (GFC_ARRAY_TYPE_P (base_type)
3606 || GFC_DESCRIPTOR_TYPE_P (base_type))
3607 base_type = gfc_get_element_type (base_type);
3609 if (expr->ts.type == BT_CLASS)
3610 base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
3612 loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
3613 ? expr->ts.u.cl->backend_decl
3614 : NULL),
3615 loop.dimen);
3617 parmse->string_length = loop.temp_ss->info->string_length;
3619 /* Associate the SS with the loop. */
3620 gfc_add_ss_to_loop (&loop, loop.temp_ss);
3622 /* Setup the scalarizing loops. */
3623 gfc_conv_loop_setup (&loop, &expr->where);
3625 /* Pass the temporary descriptor back to the caller. */
3626 info = &loop.temp_ss->info->data.array;
3627 parmse->expr = info->descriptor;
3629 /* Setup the gfc_se structures. */
3630 gfc_copy_loopinfo_to_se (&lse, &loop);
3631 gfc_copy_loopinfo_to_se (&rse, &loop);
3633 rse.ss = rss;
3634 lse.ss = loop.temp_ss;
3635 gfc_mark_ss_chain_used (rss, 1);
3636 gfc_mark_ss_chain_used (loop.temp_ss, 1);
3638 /* Start the scalarized loop body. */
3639 gfc_start_scalarized_body (&loop, &body);
3641 /* Translate the expression. */
3642 gfc_conv_expr (&rse, expr);
3644 gfc_conv_tmp_array_ref (&lse);
3646 if (intent != INTENT_OUT)
3648 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false, true);
3649 gfc_add_expr_to_block (&body, tmp);
3650 gcc_assert (rse.ss == gfc_ss_terminator);
3651 gfc_trans_scalarizing_loops (&loop, &body);
3653 else
3655 /* Make sure that the temporary declaration survives by merging
3656 all the loop declarations into the current context. */
3657 for (n = 0; n < loop.dimen; n++)
3659 gfc_merge_block_scope (&body);
3660 body = loop.code[loop.order[n]];
3662 gfc_merge_block_scope (&body);
3665 /* Add the post block after the second loop, so that any
3666 freeing of allocated memory is done at the right time. */
3667 gfc_add_block_to_block (&parmse->pre, &loop.pre);
3669 /**********Copy the temporary back again.*********/
3671 gfc_init_se (&lse, NULL);
3672 gfc_init_se (&rse, NULL);
3674 /* Walk the argument expression. */
3675 lss = gfc_walk_expr (expr);
3676 rse.ss = loop.temp_ss;
3677 lse.ss = lss;
3679 /* Initialize the scalarizer. */
3680 gfc_init_loopinfo (&loop2);
3681 gfc_add_ss_to_loop (&loop2, lss);
3683 /* Calculate the bounds of the scalarization. */
3684 gfc_conv_ss_startstride (&loop2);
3686 /* Setup the scalarizing loops. */
3687 gfc_conv_loop_setup (&loop2, &expr->where);
3689 gfc_copy_loopinfo_to_se (&lse, &loop2);
3690 gfc_copy_loopinfo_to_se (&rse, &loop2);
3692 gfc_mark_ss_chain_used (lss, 1);
3693 gfc_mark_ss_chain_used (loop.temp_ss, 1);
3695 /* Declare the variable to hold the temporary offset and start the
3696 scalarized loop body. */
3697 offset = gfc_create_var (gfc_array_index_type, NULL);
3698 gfc_start_scalarized_body (&loop2, &body);
3700 /* Build the offsets for the temporary from the loop variables. The
3701 temporary array has lbounds of zero and strides of one in all
3702 dimensions, so this is very simple. The offset is only computed
3703 outside the innermost loop, so the overall transfer could be
3704 optimized further. */
3705 info = &rse.ss->info->data.array;
3706 dimen = rse.ss->dimen;
3708 tmp_index = gfc_index_zero_node;
3709 for (n = dimen - 1; n > 0; n--)
3711 tree tmp_str;
3712 tmp = rse.loop->loopvar[n];
3713 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
3714 tmp, rse.loop->from[n]);
3715 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
3716 tmp, tmp_index);
3718 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
3719 gfc_array_index_type,
3720 rse.loop->to[n-1], rse.loop->from[n-1]);
3721 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
3722 gfc_array_index_type,
3723 tmp_str, gfc_index_one_node);
3725 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
3726 gfc_array_index_type, tmp, tmp_str);
3729 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
3730 gfc_array_index_type,
3731 tmp_index, rse.loop->from[0]);
3732 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
3734 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
3735 gfc_array_index_type,
3736 rse.loop->loopvar[0], offset);
3738 /* Now use the offset for the reference. */
3739 tmp = build_fold_indirect_ref_loc (input_location,
3740 info->data);
3741 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
3743 if (expr->ts.type == BT_CHARACTER)
3744 rse.string_length = expr->ts.u.cl->backend_decl;
3746 gfc_conv_expr (&lse, expr);
3748 gcc_assert (lse.ss == gfc_ss_terminator);
3750 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false, true);
3751 gfc_add_expr_to_block (&body, tmp);
3753 /* Generate the copying loops. */
3754 gfc_trans_scalarizing_loops (&loop2, &body);
3756 /* Wrap the whole thing up by adding the second loop to the post-block
3757 and following it by the post-block of the first loop. In this way,
3758 if the temporary needs freeing, it is done after use! */
3759 if (intent != INTENT_IN)
3761 gfc_add_block_to_block (&parmse->post, &loop2.pre);
3762 gfc_add_block_to_block (&parmse->post, &loop2.post);
3765 gfc_add_block_to_block (&parmse->post, &loop.post);
3767 gfc_cleanup_loop (&loop);
3768 gfc_cleanup_loop (&loop2);
3770 /* Pass the string length to the argument expression. */
3771 if (expr->ts.type == BT_CHARACTER)
3772 parmse->string_length = expr->ts.u.cl->backend_decl;
3774 /* Determine the offset for pointer formal arguments and set the
3775 lbounds to one. */
3776 if (formal_ptr)
3778 size = gfc_index_one_node;
3779 offset = gfc_index_zero_node;
3780 for (n = 0; n < dimen; n++)
3782 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
3783 gfc_rank_cst[n]);
3784 tmp = fold_build2_loc (input_location, PLUS_EXPR,
3785 gfc_array_index_type, tmp,
3786 gfc_index_one_node);
3787 gfc_conv_descriptor_ubound_set (&parmse->pre,
3788 parmse->expr,
3789 gfc_rank_cst[n],
3790 tmp);
3791 gfc_conv_descriptor_lbound_set (&parmse->pre,
3792 parmse->expr,
3793 gfc_rank_cst[n],
3794 gfc_index_one_node);
3795 size = gfc_evaluate_now (size, &parmse->pre);
3796 offset = fold_build2_loc (input_location, MINUS_EXPR,
3797 gfc_array_index_type,
3798 offset, size);
3799 offset = gfc_evaluate_now (offset, &parmse->pre);
3800 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3801 gfc_array_index_type,
3802 rse.loop->to[n], rse.loop->from[n]);
3803 tmp = fold_build2_loc (input_location, PLUS_EXPR,
3804 gfc_array_index_type,
3805 tmp, gfc_index_one_node);
3806 size = fold_build2_loc (input_location, MULT_EXPR,
3807 gfc_array_index_type, size, tmp);
3810 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
3811 offset);
3814 /* We want either the address for the data or the address of the descriptor,
3815 depending on the mode of passing array arguments. */
3816 if (g77)
3817 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
3818 else
3819 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
3821 return;
3825 /* Generate the code for argument list functions. */
3827 static void
3828 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
3830 /* Pass by value for g77 %VAL(arg), pass the address
3831 indirectly for %LOC, else by reference. Thus %REF
3832 is a "do-nothing" and %LOC is the same as an F95
3833 pointer. */
3834 if (strncmp (name, "%VAL", 4) == 0)
3835 gfc_conv_expr (se, expr);
3836 else if (strncmp (name, "%LOC", 4) == 0)
3838 gfc_conv_expr_reference (se, expr);
3839 se->expr = gfc_build_addr_expr (NULL, se->expr);
3841 else if (strncmp (name, "%REF", 4) == 0)
3842 gfc_conv_expr_reference (se, expr);
3843 else
3844 gfc_error ("Unknown argument list function at %L", &expr->where);
3848 /* Generate code for a procedure call. Note can return se->post != NULL.
3849 If se->direct_byref is set then se->expr contains the return parameter.
3850 Return nonzero, if the call has alternate specifiers.
3851 'expr' is only needed for procedure pointer components. */
3854 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
3855 gfc_actual_arglist * args, gfc_expr * expr,
3856 vec<tree, va_gc> *append_args)
3858 gfc_interface_mapping mapping;
3859 vec<tree, va_gc> *arglist;
3860 vec<tree, va_gc> *retargs;
3861 tree tmp;
3862 tree fntype;
3863 gfc_se parmse;
3864 gfc_array_info *info;
3865 int byref;
3866 int parm_kind;
3867 tree type;
3868 tree var;
3869 tree len;
3870 tree base_object;
3871 vec<tree, va_gc> *stringargs;
3872 vec<tree, va_gc> *optionalargs;
3873 tree result = NULL;
3874 gfc_formal_arglist *formal;
3875 gfc_actual_arglist *arg;
3876 int has_alternate_specifier = 0;
3877 bool need_interface_mapping;
3878 bool callee_alloc;
3879 gfc_typespec ts;
3880 gfc_charlen cl;
3881 gfc_expr *e;
3882 gfc_symbol *fsym;
3883 stmtblock_t post;
3884 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
3885 gfc_component *comp = NULL;
3886 int arglen;
3888 arglist = NULL;
3889 retargs = NULL;
3890 stringargs = NULL;
3891 optionalargs = NULL;
3892 var = NULL_TREE;
3893 len = NULL_TREE;
3894 gfc_clear_ts (&ts);
3896 comp = gfc_get_proc_ptr_comp (expr);
3898 if (se->ss != NULL)
3900 if (!sym->attr.elemental && !(comp && comp->attr.elemental))
3902 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
3903 if (se->ss->info->useflags)
3905 gcc_assert ((!comp && gfc_return_by_reference (sym)
3906 && sym->result->attr.dimension)
3907 || (comp && comp->attr.dimension));
3908 gcc_assert (se->loop != NULL);
3910 /* Access the previously obtained result. */
3911 gfc_conv_tmp_array_ref (se);
3912 return 0;
3915 info = &se->ss->info->data.array;
3917 else
3918 info = NULL;
3920 gfc_init_block (&post);
3921 gfc_init_interface_mapping (&mapping);
3922 if (!comp)
3924 formal = gfc_sym_get_dummy_args (sym);
3925 need_interface_mapping = sym->attr.dimension ||
3926 (sym->ts.type == BT_CHARACTER
3927 && sym->ts.u.cl->length
3928 && sym->ts.u.cl->length->expr_type
3929 != EXPR_CONSTANT);
3931 else
3933 formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
3934 need_interface_mapping = comp->attr.dimension ||
3935 (comp->ts.type == BT_CHARACTER
3936 && comp->ts.u.cl->length
3937 && comp->ts.u.cl->length->expr_type
3938 != EXPR_CONSTANT);
3941 base_object = NULL_TREE;
3943 /* Evaluate the arguments. */
3944 for (arg = args; arg != NULL;
3945 arg = arg->next, formal = formal ? formal->next : NULL)
3947 e = arg->expr;
3948 fsym = formal ? formal->sym : NULL;
3949 parm_kind = MISSING;
3951 /* Class array expressions are sometimes coming completely unadorned
3952 with either arrayspec or _data component. Correct that here.
3953 OOP-TODO: Move this to the frontend. */
3954 if (e && e->expr_type == EXPR_VARIABLE
3955 && !e->ref
3956 && e->ts.type == BT_CLASS
3957 && (CLASS_DATA (e)->attr.codimension
3958 || CLASS_DATA (e)->attr.dimension))
3960 gfc_typespec temp_ts = e->ts;
3961 gfc_add_class_array_ref (e);
3962 e->ts = temp_ts;
3965 if (e == NULL)
3967 if (se->ignore_optional)
3969 /* Some intrinsics have already been resolved to the correct
3970 parameters. */
3971 continue;
3973 else if (arg->label)
3975 has_alternate_specifier = 1;
3976 continue;
3978 else
3980 gfc_init_se (&parmse, NULL);
3982 /* For scalar arguments with VALUE attribute which are passed by
3983 value, pass "0" and a hidden argument gives the optional
3984 status. */
3985 if (fsym && fsym->attr.optional && fsym->attr.value
3986 && !fsym->attr.dimension && fsym->ts.type != BT_CHARACTER
3987 && fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED)
3989 parmse.expr = fold_convert (gfc_sym_type (fsym),
3990 integer_zero_node);
3991 vec_safe_push (optionalargs, boolean_false_node);
3993 else
3995 /* Pass a NULL pointer for an absent arg. */
3996 parmse.expr = null_pointer_node;
3997 if (arg->missing_arg_type == BT_CHARACTER)
3998 parmse.string_length = build_int_cst (gfc_charlen_type_node,
4003 else if (arg->expr->expr_type == EXPR_NULL
4004 && fsym && !fsym->attr.pointer
4005 && (fsym->ts.type != BT_CLASS
4006 || !CLASS_DATA (fsym)->attr.class_pointer))
4008 /* Pass a NULL pointer to denote an absent arg. */
4009 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
4010 && (fsym->ts.type != BT_CLASS
4011 || !CLASS_DATA (fsym)->attr.allocatable));
4012 gfc_init_se (&parmse, NULL);
4013 parmse.expr = null_pointer_node;
4014 if (arg->missing_arg_type == BT_CHARACTER)
4015 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
4017 else if (fsym && fsym->ts.type == BT_CLASS
4018 && e->ts.type == BT_DERIVED)
4020 /* The derived type needs to be converted to a temporary
4021 CLASS object. */
4022 gfc_init_se (&parmse, se);
4023 gfc_conv_derived_to_class (&parmse, e, fsym->ts, NULL,
4024 fsym->attr.optional
4025 && e->expr_type == EXPR_VARIABLE
4026 && e->symtree->n.sym->attr.optional,
4027 CLASS_DATA (fsym)->attr.class_pointer
4028 || CLASS_DATA (fsym)->attr.allocatable);
4030 else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS)
4032 /* The intrinsic type needs to be converted to a temporary
4033 CLASS object for the unlimited polymorphic formal. */
4034 gfc_init_se (&parmse, se);
4035 gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
4037 else if (se->ss && se->ss->info->useflags)
4039 gfc_ss *ss;
4041 ss = se->ss;
4043 /* An elemental function inside a scalarized loop. */
4044 gfc_init_se (&parmse, se);
4045 parm_kind = ELEMENTAL;
4047 gfc_conv_expr_reference (&parmse, e);
4048 if (e->ts.type == BT_CHARACTER && !e->rank
4049 && e->expr_type == EXPR_FUNCTION)
4050 parmse.expr = build_fold_indirect_ref_loc (input_location,
4051 parmse.expr);
4053 if (fsym && fsym->ts.type == BT_DERIVED
4054 && gfc_is_class_container_ref (e))
4056 parmse.expr = gfc_class_data_get (parmse.expr);
4058 if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
4059 && e->symtree->n.sym->attr.optional)
4061 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
4062 parmse.expr = build3_loc (input_location, COND_EXPR,
4063 TREE_TYPE (parmse.expr),
4064 cond, parmse.expr,
4065 fold_convert (TREE_TYPE (parmse.expr),
4066 null_pointer_node));
4070 /* If we are passing an absent array as optional dummy to an
4071 elemental procedure, make sure that we pass NULL when the data
4072 pointer is NULL. We need this extra conditional because of
4073 scalarization which passes arrays elements to the procedure,
4074 ignoring the fact that the array can be absent/unallocated/... */
4075 if (ss->info->can_be_null_ref && ss->info->type != GFC_SS_REFERENCE)
4077 tree descriptor_data;
4079 descriptor_data = ss->info->data.array.data;
4080 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
4081 descriptor_data,
4082 fold_convert (TREE_TYPE (descriptor_data),
4083 null_pointer_node));
4084 parmse.expr
4085 = fold_build3_loc (input_location, COND_EXPR,
4086 TREE_TYPE (parmse.expr),
4087 gfc_unlikely (tmp),
4088 fold_convert (TREE_TYPE (parmse.expr),
4089 null_pointer_node),
4090 parmse.expr);
4093 /* The scalarizer does not repackage the reference to a class
4094 array - instead it returns a pointer to the data element. */
4095 if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
4096 gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
4097 fsym->attr.intent != INTENT_IN
4098 && (CLASS_DATA (fsym)->attr.class_pointer
4099 || CLASS_DATA (fsym)->attr.allocatable),
4100 fsym->attr.optional
4101 && e->expr_type == EXPR_VARIABLE
4102 && e->symtree->n.sym->attr.optional,
4103 CLASS_DATA (fsym)->attr.class_pointer
4104 || CLASS_DATA (fsym)->attr.allocatable);
4106 else
4108 bool scalar;
4109 gfc_ss *argss;
4111 gfc_init_se (&parmse, NULL);
4113 /* Check whether the expression is a scalar or not; we cannot use
4114 e->rank as it can be nonzero for functions arguments. */
4115 argss = gfc_walk_expr (e);
4116 scalar = argss == gfc_ss_terminator;
4117 if (!scalar)
4118 gfc_free_ss_chain (argss);
4120 /* Special handling for passing scalar polymorphic coarrays;
4121 otherwise one passes "class->_data.data" instead of "&class". */
4122 if (e->rank == 0 && e->ts.type == BT_CLASS
4123 && fsym && fsym->ts.type == BT_CLASS
4124 && CLASS_DATA (fsym)->attr.codimension
4125 && !CLASS_DATA (fsym)->attr.dimension)
4127 gfc_add_class_array_ref (e);
4128 parmse.want_coarray = 1;
4129 scalar = false;
4132 /* A scalar or transformational function. */
4133 if (scalar)
4135 if (e->expr_type == EXPR_VARIABLE
4136 && e->symtree->n.sym->attr.cray_pointee
4137 && fsym && fsym->attr.flavor == FL_PROCEDURE)
4139 /* The Cray pointer needs to be converted to a pointer to
4140 a type given by the expression. */
4141 gfc_conv_expr (&parmse, e);
4142 type = build_pointer_type (TREE_TYPE (parmse.expr));
4143 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
4144 parmse.expr = convert (type, tmp);
4146 else if (fsym && fsym->attr.value)
4148 if (fsym->ts.type == BT_CHARACTER
4149 && fsym->ts.is_c_interop
4150 && fsym->ns->proc_name != NULL
4151 && fsym->ns->proc_name->attr.is_bind_c)
4153 parmse.expr = NULL;
4154 gfc_conv_scalar_char_value (fsym, &parmse, &e);
4155 if (parmse.expr == NULL)
4156 gfc_conv_expr (&parmse, e);
4158 else
4160 gfc_conv_expr (&parmse, e);
4161 if (fsym->attr.optional
4162 && fsym->ts.type != BT_CLASS
4163 && fsym->ts.type != BT_DERIVED)
4165 if (e->expr_type != EXPR_VARIABLE
4166 || !e->symtree->n.sym->attr.optional
4167 || e->ref != NULL)
4168 vec_safe_push (optionalargs, boolean_true_node);
4169 else
4171 tmp = gfc_conv_expr_present (e->symtree->n.sym);
4172 if (!e->symtree->n.sym->attr.value)
4173 parmse.expr
4174 = fold_build3_loc (input_location, COND_EXPR,
4175 TREE_TYPE (parmse.expr),
4176 tmp, parmse.expr,
4177 fold_convert (TREE_TYPE (parmse.expr),
4178 integer_zero_node));
4180 vec_safe_push (optionalargs, tmp);
4185 else if (arg->name && arg->name[0] == '%')
4186 /* Argument list functions %VAL, %LOC and %REF are signalled
4187 through arg->name. */
4188 conv_arglist_function (&parmse, arg->expr, arg->name);
4189 else if ((e->expr_type == EXPR_FUNCTION)
4190 && ((e->value.function.esym
4191 && e->value.function.esym->result->attr.pointer)
4192 || (!e->value.function.esym
4193 && e->symtree->n.sym->attr.pointer))
4194 && fsym && fsym->attr.target)
4196 gfc_conv_expr (&parmse, e);
4197 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4199 else if (e->expr_type == EXPR_FUNCTION
4200 && e->symtree->n.sym->result
4201 && e->symtree->n.sym->result != e->symtree->n.sym
4202 && e->symtree->n.sym->result->attr.proc_pointer)
4204 /* Functions returning procedure pointers. */
4205 gfc_conv_expr (&parmse, e);
4206 if (fsym && fsym->attr.proc_pointer)
4207 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4209 else
4211 if (e->ts.type == BT_CLASS && fsym
4212 && fsym->ts.type == BT_CLASS
4213 && (!CLASS_DATA (fsym)->as
4214 || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
4215 && CLASS_DATA (e)->attr.codimension)
4217 gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
4218 gcc_assert (!CLASS_DATA (fsym)->as);
4219 gfc_add_class_array_ref (e);
4220 parmse.want_coarray = 1;
4221 gfc_conv_expr_reference (&parmse, e);
4222 class_scalar_coarray_to_class (&parmse, e, fsym->ts,
4223 fsym->attr.optional
4224 && e->expr_type == EXPR_VARIABLE);
4226 else
4227 gfc_conv_expr_reference (&parmse, e);
4229 /* Catch base objects that are not variables. */
4230 if (e->ts.type == BT_CLASS
4231 && e->expr_type != EXPR_VARIABLE
4232 && expr && e == expr->base_expr)
4233 base_object = build_fold_indirect_ref_loc (input_location,
4234 parmse.expr);
4236 /* A class array element needs converting back to be a
4237 class object, if the formal argument is a class object. */
4238 if (fsym && fsym->ts.type == BT_CLASS
4239 && e->ts.type == BT_CLASS
4240 && ((CLASS_DATA (fsym)->as
4241 && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
4242 || CLASS_DATA (e)->attr.dimension))
4243 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
4244 fsym->attr.intent != INTENT_IN
4245 && (CLASS_DATA (fsym)->attr.class_pointer
4246 || CLASS_DATA (fsym)->attr.allocatable),
4247 fsym->attr.optional
4248 && e->expr_type == EXPR_VARIABLE
4249 && e->symtree->n.sym->attr.optional,
4250 CLASS_DATA (fsym)->attr.class_pointer
4251 || CLASS_DATA (fsym)->attr.allocatable);
4253 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4254 allocated on entry, it must be deallocated. */
4255 if (fsym && fsym->attr.intent == INTENT_OUT
4256 && (fsym->attr.allocatable
4257 || (fsym->ts.type == BT_CLASS
4258 && CLASS_DATA (fsym)->attr.allocatable)))
4260 stmtblock_t block;
4261 tree ptr;
4263 gfc_init_block (&block);
4264 ptr = parmse.expr;
4265 if (e->ts.type == BT_CLASS)
4266 ptr = gfc_class_data_get (ptr);
4268 tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
4269 true, e, e->ts);
4270 gfc_add_expr_to_block (&block, tmp);
4271 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4272 void_type_node, ptr,
4273 null_pointer_node);
4274 gfc_add_expr_to_block (&block, tmp);
4276 if (fsym->ts.type == BT_CLASS && UNLIMITED_POLY (fsym))
4278 gfc_add_modify (&block, ptr,
4279 fold_convert (TREE_TYPE (ptr),
4280 null_pointer_node));
4281 gfc_add_expr_to_block (&block, tmp);
4283 else if (fsym->ts.type == BT_CLASS)
4285 gfc_symbol *vtab;
4286 vtab = gfc_find_derived_vtab (fsym->ts.u.derived);
4287 tmp = gfc_get_symbol_decl (vtab);
4288 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
4289 ptr = gfc_class_vptr_get (parmse.expr);
4290 gfc_add_modify (&block, ptr,
4291 fold_convert (TREE_TYPE (ptr), tmp));
4292 gfc_add_expr_to_block (&block, tmp);
4295 if (fsym->attr.optional
4296 && e->expr_type == EXPR_VARIABLE
4297 && e->symtree->n.sym->attr.optional)
4299 tmp = fold_build3_loc (input_location, COND_EXPR,
4300 void_type_node,
4301 gfc_conv_expr_present (e->symtree->n.sym),
4302 gfc_finish_block (&block),
4303 build_empty_stmt (input_location));
4305 else
4306 tmp = gfc_finish_block (&block);
4308 gfc_add_expr_to_block (&se->pre, tmp);
4311 if (fsym && (fsym->ts.type == BT_DERIVED
4312 || fsym->ts.type == BT_ASSUMED)
4313 && e->ts.type == BT_CLASS
4314 && !CLASS_DATA (e)->attr.dimension
4315 && !CLASS_DATA (e)->attr.codimension)
4316 parmse.expr = gfc_class_data_get (parmse.expr);
4318 /* Wrap scalar variable in a descriptor. We need to convert
4319 the address of a pointer back to the pointer itself before,
4320 we can assign it to the data field. */
4322 if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
4323 && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
4325 tmp = parmse.expr;
4326 if (TREE_CODE (tmp) == ADDR_EXPR
4327 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp, 0))))
4328 tmp = TREE_OPERAND (tmp, 0);
4329 parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
4330 fsym->attr);
4331 parmse.expr = gfc_build_addr_expr (NULL_TREE,
4332 parmse.expr);
4334 else if (fsym && e->expr_type != EXPR_NULL
4335 && ((fsym->attr.pointer
4336 && fsym->attr.flavor != FL_PROCEDURE)
4337 || (fsym->attr.proc_pointer
4338 && !(e->expr_type == EXPR_VARIABLE
4339 && e->symtree->n.sym->attr.dummy))
4340 || (fsym->attr.proc_pointer
4341 && e->expr_type == EXPR_VARIABLE
4342 && gfc_is_proc_ptr_comp (e))
4343 || (fsym->attr.allocatable
4344 && fsym->attr.flavor != FL_PROCEDURE)))
4346 /* Scalar pointer dummy args require an extra level of
4347 indirection. The null pointer already contains
4348 this level of indirection. */
4349 parm_kind = SCALAR_POINTER;
4350 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4354 else if (e->ts.type == BT_CLASS
4355 && fsym && fsym->ts.type == BT_CLASS
4356 && (CLASS_DATA (fsym)->attr.dimension
4357 || CLASS_DATA (fsym)->attr.codimension))
4359 /* Pass a class array. */
4360 gfc_conv_expr_descriptor (&parmse, e);
4362 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4363 allocated on entry, it must be deallocated. */
4364 if (fsym->attr.intent == INTENT_OUT
4365 && CLASS_DATA (fsym)->attr.allocatable)
4367 stmtblock_t block;
4368 tree ptr;
4370 gfc_init_block (&block);
4371 ptr = parmse.expr;
4372 ptr = gfc_class_data_get (ptr);
4374 tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
4375 NULL_TREE, NULL_TREE,
4376 NULL_TREE, true, e,
4377 false);
4378 gfc_add_expr_to_block (&block, tmp);
4379 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4380 void_type_node, ptr,
4381 null_pointer_node);
4382 gfc_add_expr_to_block (&block, tmp);
4383 gfc_reset_vptr (&block, e);
4385 if (fsym->attr.optional
4386 && e->expr_type == EXPR_VARIABLE
4387 && (!e->ref
4388 || (e->ref->type == REF_ARRAY
4389 && !e->ref->u.ar.type != AR_FULL))
4390 && e->symtree->n.sym->attr.optional)
4392 tmp = fold_build3_loc (input_location, COND_EXPR,
4393 void_type_node,
4394 gfc_conv_expr_present (e->symtree->n.sym),
4395 gfc_finish_block (&block),
4396 build_empty_stmt (input_location));
4398 else
4399 tmp = gfc_finish_block (&block);
4401 gfc_add_expr_to_block (&se->pre, tmp);
4404 /* The conversion does not repackage the reference to a class
4405 array - _data descriptor. */
4406 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
4407 fsym->attr.intent != INTENT_IN
4408 && (CLASS_DATA (fsym)->attr.class_pointer
4409 || CLASS_DATA (fsym)->attr.allocatable),
4410 fsym->attr.optional
4411 && e->expr_type == EXPR_VARIABLE
4412 && e->symtree->n.sym->attr.optional,
4413 CLASS_DATA (fsym)->attr.class_pointer
4414 || CLASS_DATA (fsym)->attr.allocatable);
4416 else
4418 /* If the procedure requires an explicit interface, the actual
4419 argument is passed according to the corresponding formal
4420 argument. If the corresponding formal argument is a POINTER,
4421 ALLOCATABLE or assumed shape, we do not use g77's calling
4422 convention, and pass the address of the array descriptor
4423 instead. Otherwise we use g77's calling convention. */
4424 bool f;
4425 f = (fsym != NULL)
4426 && !(fsym->attr.pointer || fsym->attr.allocatable)
4427 && fsym->as && fsym->as->type != AS_ASSUMED_SHAPE
4428 && fsym->as->type != AS_ASSUMED_RANK;
4429 if (comp)
4430 f = f || !comp->attr.always_explicit;
4431 else
4432 f = f || !sym->attr.always_explicit;
4434 /* If the argument is a function call that may not create
4435 a temporary for the result, we have to check that we
4436 can do it, i.e. that there is no alias between this
4437 argument and another one. */
4438 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
4440 gfc_expr *iarg;
4441 sym_intent intent;
4443 if (fsym != NULL)
4444 intent = fsym->attr.intent;
4445 else
4446 intent = INTENT_UNKNOWN;
4448 if (gfc_check_fncall_dependency (e, intent, sym, args,
4449 NOT_ELEMENTAL))
4450 parmse.force_tmp = 1;
4452 iarg = e->value.function.actual->expr;
4454 /* Temporary needed if aliasing due to host association. */
4455 if (sym->attr.contained
4456 && !sym->attr.pure
4457 && !sym->attr.implicit_pure
4458 && !sym->attr.use_assoc
4459 && iarg->expr_type == EXPR_VARIABLE
4460 && sym->ns == iarg->symtree->n.sym->ns)
4461 parmse.force_tmp = 1;
4463 /* Ditto within module. */
4464 if (sym->attr.use_assoc
4465 && !sym->attr.pure
4466 && !sym->attr.implicit_pure
4467 && iarg->expr_type == EXPR_VARIABLE
4468 && sym->module == iarg->symtree->n.sym->module)
4469 parmse.force_tmp = 1;
4472 if (e->expr_type == EXPR_VARIABLE
4473 && is_subref_array (e))
4474 /* The actual argument is a component reference to an
4475 array of derived types. In this case, the argument
4476 is converted to a temporary, which is passed and then
4477 written back after the procedure call. */
4478 gfc_conv_subref_array_arg (&parmse, e, f,
4479 fsym ? fsym->attr.intent : INTENT_INOUT,
4480 fsym && fsym->attr.pointer);
4481 else if (gfc_is_class_array_ref (e, NULL)
4482 && fsym && fsym->ts.type == BT_DERIVED)
4483 /* The actual argument is a component reference to an
4484 array of derived types. In this case, the argument
4485 is converted to a temporary, which is passed and then
4486 written back after the procedure call.
4487 OOP-TODO: Insert code so that if the dynamic type is
4488 the same as the declared type, copy-in/copy-out does
4489 not occur. */
4490 gfc_conv_subref_array_arg (&parmse, e, f,
4491 fsym ? fsym->attr.intent : INTENT_INOUT,
4492 fsym && fsym->attr.pointer);
4493 else
4494 gfc_conv_array_parameter (&parmse, e, f, fsym, sym->name, NULL);
4496 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4497 allocated on entry, it must be deallocated. */
4498 if (fsym && fsym->attr.allocatable
4499 && fsym->attr.intent == INTENT_OUT)
4501 tmp = build_fold_indirect_ref_loc (input_location,
4502 parmse.expr);
4503 tmp = gfc_trans_dealloc_allocated (tmp, false, e);
4504 if (fsym->attr.optional
4505 && e->expr_type == EXPR_VARIABLE
4506 && e->symtree->n.sym->attr.optional)
4507 tmp = fold_build3_loc (input_location, COND_EXPR,
4508 void_type_node,
4509 gfc_conv_expr_present (e->symtree->n.sym),
4510 tmp, build_empty_stmt (input_location));
4511 gfc_add_expr_to_block (&se->pre, tmp);
4516 /* The case with fsym->attr.optional is that of a user subroutine
4517 with an interface indicating an optional argument. When we call
4518 an intrinsic subroutine, however, fsym is NULL, but we might still
4519 have an optional argument, so we proceed to the substitution
4520 just in case. */
4521 if (e && (fsym == NULL || fsym->attr.optional))
4523 /* If an optional argument is itself an optional dummy argument,
4524 check its presence and substitute a null if absent. This is
4525 only needed when passing an array to an elemental procedure
4526 as then array elements are accessed - or no NULL pointer is
4527 allowed and a "1" or "0" should be passed if not present.
4528 When passing a non-array-descriptor full array to a
4529 non-array-descriptor dummy, no check is needed. For
4530 array-descriptor actual to array-descriptor dummy, see
4531 PR 41911 for why a check has to be inserted.
4532 fsym == NULL is checked as intrinsics required the descriptor
4533 but do not always set fsym. */
4534 if (e->expr_type == EXPR_VARIABLE
4535 && e->symtree->n.sym->attr.optional
4536 && ((e->rank != 0 && sym->attr.elemental)
4537 || e->representation.length || e->ts.type == BT_CHARACTER
4538 || (e->rank != 0
4539 && (fsym == NULL
4540 || (fsym-> as
4541 && (fsym->as->type == AS_ASSUMED_SHAPE
4542 || fsym->as->type == AS_ASSUMED_RANK
4543 || fsym->as->type == AS_DEFERRED))))))
4544 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
4545 e->representation.length);
4548 if (fsym && e)
4550 /* Obtain the character length of an assumed character length
4551 length procedure from the typespec. */
4552 if (fsym->ts.type == BT_CHARACTER
4553 && parmse.string_length == NULL_TREE
4554 && e->ts.type == BT_PROCEDURE
4555 && e->symtree->n.sym->ts.type == BT_CHARACTER
4556 && e->symtree->n.sym->ts.u.cl->length != NULL
4557 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
4559 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
4560 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
4564 if (fsym && need_interface_mapping && e)
4565 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
4567 gfc_add_block_to_block (&se->pre, &parmse.pre);
4568 gfc_add_block_to_block (&post, &parmse.post);
4570 /* Allocated allocatable components of derived types must be
4571 deallocated for non-variable scalars. Non-variable arrays are
4572 dealt with in trans-array.c(gfc_conv_array_parameter). */
4573 if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
4574 && e->ts.u.derived->attr.alloc_comp
4575 && !(e->symtree && e->symtree->n.sym->attr.pointer)
4576 && (e->expr_type != EXPR_VARIABLE && !e->rank))
4578 int parm_rank;
4579 tmp = build_fold_indirect_ref_loc (input_location,
4580 parmse.expr);
4581 parm_rank = e->rank;
4582 switch (parm_kind)
4584 case (ELEMENTAL):
4585 case (SCALAR):
4586 parm_rank = 0;
4587 break;
4589 case (SCALAR_POINTER):
4590 tmp = build_fold_indirect_ref_loc (input_location,
4591 tmp);
4592 break;
4595 if (e->expr_type == EXPR_OP
4596 && e->value.op.op == INTRINSIC_PARENTHESES
4597 && e->value.op.op1->expr_type == EXPR_VARIABLE)
4599 tree local_tmp;
4600 local_tmp = gfc_evaluate_now (tmp, &se->pre);
4601 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
4602 gfc_add_expr_to_block (&se->post, local_tmp);
4605 if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
4607 /* The derived type is passed to gfc_deallocate_alloc_comp.
4608 Therefore, class actuals can handled correctly but derived
4609 types passed to class formals need the _data component. */
4610 tmp = gfc_class_data_get (tmp);
4611 if (!CLASS_DATA (fsym)->attr.dimension)
4612 tmp = build_fold_indirect_ref_loc (input_location, tmp);
4615 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
4617 gfc_add_expr_to_block (&se->post, tmp);
4620 /* Add argument checking of passing an unallocated/NULL actual to
4621 a nonallocatable/nonpointer dummy. */
4623 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
4625 symbol_attribute attr;
4626 char *msg;
4627 tree cond;
4629 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
4630 attr = gfc_expr_attr (e);
4631 else
4632 goto end_pointer_check;
4634 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
4635 allocatable to an optional dummy, cf. 12.5.2.12. */
4636 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
4637 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
4638 goto end_pointer_check;
4640 if (attr.optional)
4642 /* If the actual argument is an optional pointer/allocatable and
4643 the formal argument takes an nonpointer optional value,
4644 it is invalid to pass a non-present argument on, even
4645 though there is no technical reason for this in gfortran.
4646 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
4647 tree present, null_ptr, type;
4649 if (attr.allocatable
4650 && (fsym == NULL || !fsym->attr.allocatable))
4651 asprintf (&msg, "Allocatable actual argument '%s' is not "
4652 "allocated or not present", e->symtree->n.sym->name);
4653 else if (attr.pointer
4654 && (fsym == NULL || !fsym->attr.pointer))
4655 asprintf (&msg, "Pointer actual argument '%s' is not "
4656 "associated or not present",
4657 e->symtree->n.sym->name);
4658 else if (attr.proc_pointer
4659 && (fsym == NULL || !fsym->attr.proc_pointer))
4660 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
4661 "associated or not present",
4662 e->symtree->n.sym->name);
4663 else
4664 goto end_pointer_check;
4666 present = gfc_conv_expr_present (e->symtree->n.sym);
4667 type = TREE_TYPE (present);
4668 present = fold_build2_loc (input_location, EQ_EXPR,
4669 boolean_type_node, present,
4670 fold_convert (type,
4671 null_pointer_node));
4672 type = TREE_TYPE (parmse.expr);
4673 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
4674 boolean_type_node, parmse.expr,
4675 fold_convert (type,
4676 null_pointer_node));
4677 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
4678 boolean_type_node, present, null_ptr);
4680 else
4682 if (attr.allocatable
4683 && (fsym == NULL || !fsym->attr.allocatable))
4684 asprintf (&msg, "Allocatable actual argument '%s' is not "
4685 "allocated", e->symtree->n.sym->name);
4686 else if (attr.pointer
4687 && (fsym == NULL || !fsym->attr.pointer))
4688 asprintf (&msg, "Pointer actual argument '%s' is not "
4689 "associated", e->symtree->n.sym->name);
4690 else if (attr.proc_pointer
4691 && (fsym == NULL || !fsym->attr.proc_pointer))
4692 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
4693 "associated", e->symtree->n.sym->name);
4694 else
4695 goto end_pointer_check;
4697 tmp = parmse.expr;
4699 /* If the argument is passed by value, we need to strip the
4700 INDIRECT_REF. */
4701 if (!POINTER_TYPE_P (TREE_TYPE (parmse.expr)))
4702 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
4704 cond = fold_build2_loc (input_location, EQ_EXPR,
4705 boolean_type_node, tmp,
4706 fold_convert (TREE_TYPE (tmp),
4707 null_pointer_node));
4710 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
4711 msg);
4712 free (msg);
4714 end_pointer_check:
4716 /* Deferred length dummies pass the character length by reference
4717 so that the value can be returned. */
4718 if (parmse.string_length && fsym && fsym->ts.deferred)
4720 tmp = parmse.string_length;
4721 if (TREE_CODE (tmp) != VAR_DECL)
4722 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
4723 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
4726 /* Character strings are passed as two parameters, a length and a
4727 pointer - except for Bind(c) which only passes the pointer.
4728 An unlimited polymorphic formal argument likewise does not
4729 need the length. */
4730 if (parmse.string_length != NULL_TREE
4731 && !sym->attr.is_bind_c
4732 && !(fsym && UNLIMITED_POLY (fsym)))
4733 vec_safe_push (stringargs, parmse.string_length);
4735 /* When calling __copy for character expressions to unlimited
4736 polymorphic entities, the dst argument needs a string length. */
4737 if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
4738 && strncmp (sym->name, "__vtab_CHARACTER", 16) == 0
4739 && arg->next && arg->next->expr
4740 && arg->next->expr->ts.type == BT_DERIVED
4741 && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
4742 vec_safe_push (stringargs, parmse.string_length);
4744 /* For descriptorless coarrays and assumed-shape coarray dummies, we
4745 pass the token and the offset as additional arguments. */
4746 if (fsym && fsym->attr.codimension
4747 && gfc_option.coarray == GFC_FCOARRAY_LIB
4748 && !fsym->attr.allocatable
4749 && e == NULL)
4751 /* Token and offset. */
4752 vec_safe_push (stringargs, null_pointer_node);
4753 vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
4754 gcc_assert (fsym->attr.optional);
4756 else if (fsym && fsym->attr.codimension
4757 && !fsym->attr.allocatable
4758 && gfc_option.coarray == GFC_FCOARRAY_LIB)
4760 tree caf_decl, caf_type;
4761 tree offset, tmp2;
4763 caf_decl = get_tree_for_caf_expr (e);
4764 caf_type = TREE_TYPE (caf_decl);
4766 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
4767 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
4768 tmp = gfc_conv_descriptor_token (caf_decl);
4769 else if (DECL_LANG_SPECIFIC (caf_decl)
4770 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
4771 tmp = GFC_DECL_TOKEN (caf_decl);
4772 else
4774 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
4775 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
4776 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
4779 vec_safe_push (stringargs, tmp);
4781 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
4782 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
4783 offset = build_int_cst (gfc_array_index_type, 0);
4784 else if (DECL_LANG_SPECIFIC (caf_decl)
4785 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
4786 offset = GFC_DECL_CAF_OFFSET (caf_decl);
4787 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
4788 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
4789 else
4790 offset = build_int_cst (gfc_array_index_type, 0);
4792 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
4793 tmp = gfc_conv_descriptor_data_get (caf_decl);
4794 else
4796 gcc_assert (POINTER_TYPE_P (caf_type));
4797 tmp = caf_decl;
4800 if (fsym->as->type == AS_ASSUMED_SHAPE
4801 || (fsym->as->type == AS_ASSUMED_RANK && !fsym->attr.pointer
4802 && !fsym->attr.allocatable))
4804 gcc_assert (POINTER_TYPE_P (TREE_TYPE (parmse.expr)));
4805 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE
4806 (TREE_TYPE (parmse.expr))));
4807 tmp2 = build_fold_indirect_ref_loc (input_location, parmse.expr);
4808 tmp2 = gfc_conv_descriptor_data_get (tmp2);
4810 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse.expr)))
4811 tmp2 = gfc_conv_descriptor_data_get (parmse.expr);
4812 else
4814 gcc_assert (POINTER_TYPE_P (TREE_TYPE (parmse.expr)));
4815 tmp2 = parmse.expr;
4818 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4819 gfc_array_index_type,
4820 fold_convert (gfc_array_index_type, tmp2),
4821 fold_convert (gfc_array_index_type, tmp));
4822 offset = fold_build2_loc (input_location, PLUS_EXPR,
4823 gfc_array_index_type, offset, tmp);
4825 vec_safe_push (stringargs, offset);
4828 vec_safe_push (arglist, parmse.expr);
4830 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
4832 if (comp)
4833 ts = comp->ts;
4834 else
4835 ts = sym->ts;
4837 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
4838 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
4839 else if (ts.type == BT_CHARACTER)
4841 if (ts.u.cl->length == NULL)
4843 /* Assumed character length results are not allowed by 5.1.1.5 of the
4844 standard and are trapped in resolve.c; except in the case of SPREAD
4845 (and other intrinsics?) and dummy functions. In the case of SPREAD,
4846 we take the character length of the first argument for the result.
4847 For dummies, we have to look through the formal argument list for
4848 this function and use the character length found there.*/
4849 if (ts.deferred)
4850 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
4851 else if (!sym->attr.dummy)
4852 cl.backend_decl = (*stringargs)[0];
4853 else
4855 formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
4856 for (; formal; formal = formal->next)
4857 if (strcmp (formal->sym->name, sym->name) == 0)
4858 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
4860 len = cl.backend_decl;
4862 else
4864 tree tmp;
4866 /* Calculate the length of the returned string. */
4867 gfc_init_se (&parmse, NULL);
4868 if (need_interface_mapping)
4869 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
4870 else
4871 gfc_conv_expr (&parmse, ts.u.cl->length);
4872 gfc_add_block_to_block (&se->pre, &parmse.pre);
4873 gfc_add_block_to_block (&se->post, &parmse.post);
4875 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
4876 tmp = fold_build2_loc (input_location, MAX_EXPR,
4877 gfc_charlen_type_node, tmp,
4878 build_int_cst (gfc_charlen_type_node, 0));
4879 cl.backend_decl = tmp;
4882 /* Set up a charlen structure for it. */
4883 cl.next = NULL;
4884 cl.length = NULL;
4885 ts.u.cl = &cl;
4887 len = cl.backend_decl;
4890 byref = (comp && (comp->attr.dimension || comp->ts.type == BT_CHARACTER))
4891 || (!comp && gfc_return_by_reference (sym));
4892 if (byref)
4894 if (se->direct_byref)
4896 /* Sometimes, too much indirection can be applied; e.g. for
4897 function_result = array_valued_recursive_function. */
4898 if (TREE_TYPE (TREE_TYPE (se->expr))
4899 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
4900 && GFC_DESCRIPTOR_TYPE_P
4901 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
4902 se->expr = build_fold_indirect_ref_loc (input_location,
4903 se->expr);
4905 /* If the lhs of an assignment x = f(..) is allocatable and
4906 f2003 is allowed, we must do the automatic reallocation.
4907 TODO - deal with intrinsics, without using a temporary. */
4908 if (gfc_option.flag_realloc_lhs
4909 && se->ss && se->ss->loop_chain
4910 && se->ss->loop_chain->is_alloc_lhs
4911 && !expr->value.function.isym
4912 && sym->result->as != NULL)
4914 /* Evaluate the bounds of the result, if known. */
4915 gfc_set_loop_bounds_from_array_spec (&mapping, se,
4916 sym->result->as);
4918 /* Perform the automatic reallocation. */
4919 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
4920 expr, NULL);
4921 gfc_add_expr_to_block (&se->pre, tmp);
4923 /* Pass the temporary as the first argument. */
4924 result = info->descriptor;
4926 else
4927 result = build_fold_indirect_ref_loc (input_location,
4928 se->expr);
4929 vec_safe_push (retargs, se->expr);
4931 else if (comp && comp->attr.dimension)
4933 gcc_assert (se->loop && info);
4935 /* Set the type of the array. */
4936 tmp = gfc_typenode_for_spec (&comp->ts);
4937 gcc_assert (se->ss->dimen == se->loop->dimen);
4939 /* Evaluate the bounds of the result, if known. */
4940 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
4942 /* If the lhs of an assignment x = f(..) is allocatable and
4943 f2003 is allowed, we must not generate the function call
4944 here but should just send back the results of the mapping.
4945 This is signalled by the function ss being flagged. */
4946 if (gfc_option.flag_realloc_lhs
4947 && se->ss && se->ss->is_alloc_lhs)
4949 gfc_free_interface_mapping (&mapping);
4950 return has_alternate_specifier;
4953 /* Create a temporary to store the result. In case the function
4954 returns a pointer, the temporary will be a shallow copy and
4955 mustn't be deallocated. */
4956 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
4957 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
4958 tmp, NULL_TREE, false,
4959 !comp->attr.pointer, callee_alloc,
4960 &se->ss->info->expr->where);
4962 /* Pass the temporary as the first argument. */
4963 result = info->descriptor;
4964 tmp = gfc_build_addr_expr (NULL_TREE, result);
4965 vec_safe_push (retargs, tmp);
4967 else if (!comp && sym->result->attr.dimension)
4969 gcc_assert (se->loop && info);
4971 /* Set the type of the array. */
4972 tmp = gfc_typenode_for_spec (&ts);
4973 gcc_assert (se->ss->dimen == se->loop->dimen);
4975 /* Evaluate the bounds of the result, if known. */
4976 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
4978 /* If the lhs of an assignment x = f(..) is allocatable and
4979 f2003 is allowed, we must not generate the function call
4980 here but should just send back the results of the mapping.
4981 This is signalled by the function ss being flagged. */
4982 if (gfc_option.flag_realloc_lhs
4983 && se->ss && se->ss->is_alloc_lhs)
4985 gfc_free_interface_mapping (&mapping);
4986 return has_alternate_specifier;
4989 /* Create a temporary to store the result. In case the function
4990 returns a pointer, the temporary will be a shallow copy and
4991 mustn't be deallocated. */
4992 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
4993 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
4994 tmp, NULL_TREE, false,
4995 !sym->attr.pointer, callee_alloc,
4996 &se->ss->info->expr->where);
4998 /* Pass the temporary as the first argument. */
4999 result = info->descriptor;
5000 tmp = gfc_build_addr_expr (NULL_TREE, result);
5001 vec_safe_push (retargs, tmp);
5003 else if (ts.type == BT_CHARACTER)
5005 /* Pass the string length. */
5006 type = gfc_get_character_type (ts.kind, ts.u.cl);
5007 type = build_pointer_type (type);
5009 /* Return an address to a char[0:len-1]* temporary for
5010 character pointers. */
5011 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5012 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
5014 var = gfc_create_var (type, "pstr");
5016 if ((!comp && sym->attr.allocatable)
5017 || (comp && comp->attr.allocatable))
5019 gfc_add_modify (&se->pre, var,
5020 fold_convert (TREE_TYPE (var),
5021 null_pointer_node));
5022 tmp = gfc_call_free (convert (pvoid_type_node, var));
5023 gfc_add_expr_to_block (&se->post, tmp);
5026 /* Provide an address expression for the function arguments. */
5027 var = gfc_build_addr_expr (NULL_TREE, var);
5029 else
5030 var = gfc_conv_string_tmp (se, type, len);
5032 vec_safe_push (retargs, var);
5034 else
5036 gcc_assert (gfc_option.flag_f2c && ts.type == BT_COMPLEX);
5038 type = gfc_get_complex_type (ts.kind);
5039 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
5040 vec_safe_push (retargs, var);
5043 /* Add the string length to the argument list. */
5044 if (ts.type == BT_CHARACTER && ts.deferred)
5046 tmp = len;
5047 if (TREE_CODE (tmp) != VAR_DECL)
5048 tmp = gfc_evaluate_now (len, &se->pre);
5049 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5050 vec_safe_push (retargs, tmp);
5052 else if (ts.type == BT_CHARACTER)
5053 vec_safe_push (retargs, len);
5055 gfc_free_interface_mapping (&mapping);
5057 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
5058 arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
5059 + vec_safe_length (stringargs) + vec_safe_length (append_args));
5060 vec_safe_reserve (retargs, arglen);
5062 /* Add the return arguments. */
5063 retargs->splice (arglist);
5065 /* Add the hidden present status for optional+value to the arguments. */
5066 retargs->splice (optionalargs);
5068 /* Add the hidden string length parameters to the arguments. */
5069 retargs->splice (stringargs);
5071 /* We may want to append extra arguments here. This is used e.g. for
5072 calls to libgfortran_matmul_??, which need extra information. */
5073 if (!vec_safe_is_empty (append_args))
5074 retargs->splice (append_args);
5075 arglist = retargs;
5077 /* Generate the actual call. */
5078 if (base_object == NULL_TREE)
5079 conv_function_val (se, sym, expr);
5080 else
5081 conv_base_obj_fcn_val (se, base_object, expr);
5083 /* If there are alternate return labels, function type should be
5084 integer. Can't modify the type in place though, since it can be shared
5085 with other functions. For dummy arguments, the typing is done to
5086 this result, even if it has to be repeated for each call. */
5087 if (has_alternate_specifier
5088 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
5090 if (!sym->attr.dummy)
5092 TREE_TYPE (sym->backend_decl)
5093 = build_function_type (integer_type_node,
5094 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
5095 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
5097 else
5098 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
5101 fntype = TREE_TYPE (TREE_TYPE (se->expr));
5102 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
5104 /* If we have a pointer function, but we don't want a pointer, e.g.
5105 something like
5106 x = f()
5107 where f is pointer valued, we have to dereference the result. */
5108 if (!se->want_pointer && !byref
5109 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5110 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
5111 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
5113 /* f2c calling conventions require a scalar default real function to
5114 return a double precision result. Convert this back to default
5115 real. We only care about the cases that can happen in Fortran 77.
5117 if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
5118 && sym->ts.kind == gfc_default_real_kind
5119 && !sym->attr.always_explicit)
5120 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
5122 /* A pure function may still have side-effects - it may modify its
5123 parameters. */
5124 TREE_SIDE_EFFECTS (se->expr) = 1;
5125 #if 0
5126 if (!sym->attr.pure)
5127 TREE_SIDE_EFFECTS (se->expr) = 1;
5128 #endif
5130 if (byref)
5132 /* Add the function call to the pre chain. There is no expression. */
5133 gfc_add_expr_to_block (&se->pre, se->expr);
5134 se->expr = NULL_TREE;
5136 if (!se->direct_byref)
5138 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
5140 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
5142 /* Check the data pointer hasn't been modified. This would
5143 happen in a function returning a pointer. */
5144 tmp = gfc_conv_descriptor_data_get (info->descriptor);
5145 tmp = fold_build2_loc (input_location, NE_EXPR,
5146 boolean_type_node,
5147 tmp, info->data);
5148 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
5149 gfc_msg_fault);
5151 se->expr = info->descriptor;
5152 /* Bundle in the string length. */
5153 se->string_length = len;
5155 else if (ts.type == BT_CHARACTER)
5157 /* Dereference for character pointer results. */
5158 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5159 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
5160 se->expr = build_fold_indirect_ref_loc (input_location, var);
5161 else
5162 se->expr = var;
5164 se->string_length = len;
5166 else
5168 gcc_assert (ts.type == BT_COMPLEX && gfc_option.flag_f2c);
5169 se->expr = build_fold_indirect_ref_loc (input_location, var);
5174 /* Follow the function call with the argument post block. */
5175 if (byref)
5177 gfc_add_block_to_block (&se->pre, &post);
5179 /* Transformational functions of derived types with allocatable
5180 components must have the result allocatable components copied. */
5181 arg = expr->value.function.actual;
5182 if (result && arg && expr->rank
5183 && expr->value.function.isym
5184 && expr->value.function.isym->transformational
5185 && arg->expr->ts.type == BT_DERIVED
5186 && arg->expr->ts.u.derived->attr.alloc_comp)
5188 tree tmp2;
5189 /* Copy the allocatable components. We have to use a
5190 temporary here to prevent source allocatable components
5191 from being corrupted. */
5192 tmp2 = gfc_evaluate_now (result, &se->pre);
5193 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
5194 result, tmp2, expr->rank);
5195 gfc_add_expr_to_block (&se->pre, tmp);
5196 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
5197 expr->rank);
5198 gfc_add_expr_to_block (&se->pre, tmp);
5200 /* Finally free the temporary's data field. */
5201 tmp = gfc_conv_descriptor_data_get (tmp2);
5202 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
5203 NULL_TREE, NULL_TREE, true,
5204 NULL, false);
5205 gfc_add_expr_to_block (&se->pre, tmp);
5208 else
5209 gfc_add_block_to_block (&se->post, &post);
5211 return has_alternate_specifier;
5215 /* Fill a character string with spaces. */
5217 static tree
5218 fill_with_spaces (tree start, tree type, tree size)
5220 stmtblock_t block, loop;
5221 tree i, el, exit_label, cond, tmp;
5223 /* For a simple char type, we can call memset(). */
5224 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
5225 return build_call_expr_loc (input_location,
5226 builtin_decl_explicit (BUILT_IN_MEMSET),
5227 3, start,
5228 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
5229 lang_hooks.to_target_charset (' ')),
5230 size);
5232 /* Otherwise, we use a loop:
5233 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
5234 *el = (type) ' ';
5237 /* Initialize variables. */
5238 gfc_init_block (&block);
5239 i = gfc_create_var (sizetype, "i");
5240 gfc_add_modify (&block, i, fold_convert (sizetype, size));
5241 el = gfc_create_var (build_pointer_type (type), "el");
5242 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
5243 exit_label = gfc_build_label_decl (NULL_TREE);
5244 TREE_USED (exit_label) = 1;
5247 /* Loop body. */
5248 gfc_init_block (&loop);
5250 /* Exit condition. */
5251 cond = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, i,
5252 build_zero_cst (sizetype));
5253 tmp = build1_v (GOTO_EXPR, exit_label);
5254 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
5255 build_empty_stmt (input_location));
5256 gfc_add_expr_to_block (&loop, tmp);
5258 /* Assignment. */
5259 gfc_add_modify (&loop,
5260 fold_build1_loc (input_location, INDIRECT_REF, type, el),
5261 build_int_cst (type, lang_hooks.to_target_charset (' ')));
5263 /* Increment loop variables. */
5264 gfc_add_modify (&loop, i,
5265 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
5266 TYPE_SIZE_UNIT (type)));
5267 gfc_add_modify (&loop, el,
5268 fold_build_pointer_plus_loc (input_location,
5269 el, TYPE_SIZE_UNIT (type)));
5271 /* Making the loop... actually loop! */
5272 tmp = gfc_finish_block (&loop);
5273 tmp = build1_v (LOOP_EXPR, tmp);
5274 gfc_add_expr_to_block (&block, tmp);
5276 /* The exit label. */
5277 tmp = build1_v (LABEL_EXPR, exit_label);
5278 gfc_add_expr_to_block (&block, tmp);
5281 return gfc_finish_block (&block);
5285 /* Generate code to copy a string. */
5287 void
5288 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
5289 int dkind, tree slength, tree src, int skind)
5291 tree tmp, dlen, slen;
5292 tree dsc;
5293 tree ssc;
5294 tree cond;
5295 tree cond2;
5296 tree tmp2;
5297 tree tmp3;
5298 tree tmp4;
5299 tree chartype;
5300 stmtblock_t tempblock;
5302 gcc_assert (dkind == skind);
5304 if (slength != NULL_TREE)
5306 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
5307 ssc = gfc_string_to_single_character (slen, src, skind);
5309 else
5311 slen = build_int_cst (size_type_node, 1);
5312 ssc = src;
5315 if (dlength != NULL_TREE)
5317 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
5318 dsc = gfc_string_to_single_character (dlen, dest, dkind);
5320 else
5322 dlen = build_int_cst (size_type_node, 1);
5323 dsc = dest;
5326 /* Assign directly if the types are compatible. */
5327 if (dsc != NULL_TREE && ssc != NULL_TREE
5328 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
5330 gfc_add_modify (block, dsc, ssc);
5331 return;
5334 /* Do nothing if the destination length is zero. */
5335 cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, dlen,
5336 build_int_cst (size_type_node, 0));
5338 /* The following code was previously in _gfortran_copy_string:
5340 // The two strings may overlap so we use memmove.
5341 void
5342 copy_string (GFC_INTEGER_4 destlen, char * dest,
5343 GFC_INTEGER_4 srclen, const char * src)
5345 if (srclen >= destlen)
5347 // This will truncate if too long.
5348 memmove (dest, src, destlen);
5350 else
5352 memmove (dest, src, srclen);
5353 // Pad with spaces.
5354 memset (&dest[srclen], ' ', destlen - srclen);
5358 We're now doing it here for better optimization, but the logic
5359 is the same. */
5361 /* For non-default character kinds, we have to multiply the string
5362 length by the base type size. */
5363 chartype = gfc_get_char_type (dkind);
5364 slen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
5365 fold_convert (size_type_node, slen),
5366 fold_convert (size_type_node,
5367 TYPE_SIZE_UNIT (chartype)));
5368 dlen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
5369 fold_convert (size_type_node, dlen),
5370 fold_convert (size_type_node,
5371 TYPE_SIZE_UNIT (chartype)));
5373 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
5374 dest = fold_convert (pvoid_type_node, dest);
5375 else
5376 dest = gfc_build_addr_expr (pvoid_type_node, dest);
5378 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
5379 src = fold_convert (pvoid_type_node, src);
5380 else
5381 src = gfc_build_addr_expr (pvoid_type_node, src);
5383 /* Truncate string if source is too long. */
5384 cond2 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, slen,
5385 dlen);
5386 tmp2 = build_call_expr_loc (input_location,
5387 builtin_decl_explicit (BUILT_IN_MEMMOVE),
5388 3, dest, src, dlen);
5390 /* Else copy and pad with spaces. */
5391 tmp3 = build_call_expr_loc (input_location,
5392 builtin_decl_explicit (BUILT_IN_MEMMOVE),
5393 3, dest, src, slen);
5395 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
5396 tmp4 = fill_with_spaces (tmp4, chartype,
5397 fold_build2_loc (input_location, MINUS_EXPR,
5398 TREE_TYPE(dlen), dlen, slen));
5400 gfc_init_block (&tempblock);
5401 gfc_add_expr_to_block (&tempblock, tmp3);
5402 gfc_add_expr_to_block (&tempblock, tmp4);
5403 tmp3 = gfc_finish_block (&tempblock);
5405 /* The whole copy_string function is there. */
5406 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
5407 tmp2, tmp3);
5408 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
5409 build_empty_stmt (input_location));
5410 gfc_add_expr_to_block (block, tmp);
5414 /* Translate a statement function.
5415 The value of a statement function reference is obtained by evaluating the
5416 expression using the values of the actual arguments for the values of the
5417 corresponding dummy arguments. */
5419 static void
5420 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
5422 gfc_symbol *sym;
5423 gfc_symbol *fsym;
5424 gfc_formal_arglist *fargs;
5425 gfc_actual_arglist *args;
5426 gfc_se lse;
5427 gfc_se rse;
5428 gfc_saved_var *saved_vars;
5429 tree *temp_vars;
5430 tree type;
5431 tree tmp;
5432 int n;
5434 sym = expr->symtree->n.sym;
5435 args = expr->value.function.actual;
5436 gfc_init_se (&lse, NULL);
5437 gfc_init_se (&rse, NULL);
5439 n = 0;
5440 for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
5441 n++;
5442 saved_vars = XCNEWVEC (gfc_saved_var, n);
5443 temp_vars = XCNEWVEC (tree, n);
5445 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
5446 fargs = fargs->next, n++)
5448 /* Each dummy shall be specified, explicitly or implicitly, to be
5449 scalar. */
5450 gcc_assert (fargs->sym->attr.dimension == 0);
5451 fsym = fargs->sym;
5453 if (fsym->ts.type == BT_CHARACTER)
5455 /* Copy string arguments. */
5456 tree arglen;
5458 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
5459 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
5461 /* Create a temporary to hold the value. */
5462 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
5463 fsym->ts.u.cl->backend_decl
5464 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
5466 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
5467 temp_vars[n] = gfc_create_var (type, fsym->name);
5469 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
5471 gfc_conv_expr (&rse, args->expr);
5472 gfc_conv_string_parameter (&rse);
5473 gfc_add_block_to_block (&se->pre, &lse.pre);
5474 gfc_add_block_to_block (&se->pre, &rse.pre);
5476 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
5477 rse.string_length, rse.expr, fsym->ts.kind);
5478 gfc_add_block_to_block (&se->pre, &lse.post);
5479 gfc_add_block_to_block (&se->pre, &rse.post);
5481 else
5483 /* For everything else, just evaluate the expression. */
5485 /* Create a temporary to hold the value. */
5486 type = gfc_typenode_for_spec (&fsym->ts);
5487 temp_vars[n] = gfc_create_var (type, fsym->name);
5489 gfc_conv_expr (&lse, args->expr);
5491 gfc_add_block_to_block (&se->pre, &lse.pre);
5492 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
5493 gfc_add_block_to_block (&se->pre, &lse.post);
5496 args = args->next;
5499 /* Use the temporary variables in place of the real ones. */
5500 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
5501 fargs = fargs->next, n++)
5502 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
5504 gfc_conv_expr (se, sym->value);
5506 if (sym->ts.type == BT_CHARACTER)
5508 gfc_conv_const_charlen (sym->ts.u.cl);
5510 /* Force the expression to the correct length. */
5511 if (!INTEGER_CST_P (se->string_length)
5512 || tree_int_cst_lt (se->string_length,
5513 sym->ts.u.cl->backend_decl))
5515 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
5516 tmp = gfc_create_var (type, sym->name);
5517 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
5518 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
5519 sym->ts.kind, se->string_length, se->expr,
5520 sym->ts.kind);
5521 se->expr = tmp;
5523 se->string_length = sym->ts.u.cl->backend_decl;
5526 /* Restore the original variables. */
5527 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
5528 fargs = fargs->next, n++)
5529 gfc_restore_sym (fargs->sym, &saved_vars[n]);
5530 free (temp_vars);
5531 free (saved_vars);
5535 /* Translate a function expression. */
5537 static void
5538 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
5540 gfc_symbol *sym;
5542 if (expr->value.function.isym)
5544 gfc_conv_intrinsic_function (se, expr);
5545 return;
5548 /* expr.value.function.esym is the resolved (specific) function symbol for
5549 most functions. However this isn't set for dummy procedures. */
5550 sym = expr->value.function.esym;
5551 if (!sym)
5552 sym = expr->symtree->n.sym;
5554 /* We distinguish statement functions from general functions to improve
5555 runtime performance. */
5556 if (sym->attr.proc == PROC_ST_FUNCTION)
5558 gfc_conv_statement_function (se, expr);
5559 return;
5562 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
5563 NULL);
5567 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
5569 static bool
5570 is_zero_initializer_p (gfc_expr * expr)
5572 if (expr->expr_type != EXPR_CONSTANT)
5573 return false;
5575 /* We ignore constants with prescribed memory representations for now. */
5576 if (expr->representation.string)
5577 return false;
5579 switch (expr->ts.type)
5581 case BT_INTEGER:
5582 return mpz_cmp_si (expr->value.integer, 0) == 0;
5584 case BT_REAL:
5585 return mpfr_zero_p (expr->value.real)
5586 && MPFR_SIGN (expr->value.real) >= 0;
5588 case BT_LOGICAL:
5589 return expr->value.logical == 0;
5591 case BT_COMPLEX:
5592 return mpfr_zero_p (mpc_realref (expr->value.complex))
5593 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
5594 && mpfr_zero_p (mpc_imagref (expr->value.complex))
5595 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
5597 default:
5598 break;
5600 return false;
5604 static void
5605 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
5607 gfc_ss *ss;
5609 ss = se->ss;
5610 gcc_assert (ss != NULL && ss != gfc_ss_terminator);
5611 gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
5613 gfc_conv_tmp_array_ref (se);
5617 /* Build a static initializer. EXPR is the expression for the initial value.
5618 The other parameters describe the variable of the component being
5619 initialized. EXPR may be null. */
5621 tree
5622 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
5623 bool array, bool pointer, bool procptr)
5625 gfc_se se;
5627 if (!(expr || pointer || procptr))
5628 return NULL_TREE;
5630 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
5631 (these are the only two iso_c_binding derived types that can be
5632 used as initialization expressions). If so, we need to modify
5633 the 'expr' to be that for a (void *). */
5634 if (expr != NULL && expr->ts.type == BT_DERIVED
5635 && expr->ts.is_iso_c && expr->ts.u.derived)
5637 gfc_symbol *derived = expr->ts.u.derived;
5639 /* The derived symbol has already been converted to a (void *). Use
5640 its kind. */
5641 expr = gfc_get_int_expr (derived->ts.kind, NULL, 0);
5642 expr->ts.f90_type = derived->ts.f90_type;
5644 gfc_init_se (&se, NULL);
5645 gfc_conv_constant (&se, expr);
5646 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
5647 return se.expr;
5650 if (array && !procptr)
5652 tree ctor;
5653 /* Arrays need special handling. */
5654 if (pointer)
5655 ctor = gfc_build_null_descriptor (type);
5656 /* Special case assigning an array to zero. */
5657 else if (is_zero_initializer_p (expr))
5658 ctor = build_constructor (type, NULL);
5659 else
5660 ctor = gfc_conv_array_initializer (type, expr);
5661 TREE_STATIC (ctor) = 1;
5662 return ctor;
5664 else if (pointer || procptr)
5666 if (ts->type == BT_CLASS && !procptr)
5668 gfc_init_se (&se, NULL);
5669 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
5670 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
5671 TREE_STATIC (se.expr) = 1;
5672 return se.expr;
5674 else if (!expr || expr->expr_type == EXPR_NULL)
5675 return fold_convert (type, null_pointer_node);
5676 else
5678 gfc_init_se (&se, NULL);
5679 se.want_pointer = 1;
5680 gfc_conv_expr (&se, expr);
5681 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
5682 return se.expr;
5685 else
5687 switch (ts->type)
5689 case BT_DERIVED:
5690 case BT_CLASS:
5691 gfc_init_se (&se, NULL);
5692 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
5693 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
5694 else
5695 gfc_conv_structure (&se, expr, 1);
5696 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
5697 TREE_STATIC (se.expr) = 1;
5698 return se.expr;
5700 case BT_CHARACTER:
5702 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
5703 TREE_STATIC (ctor) = 1;
5704 return ctor;
5707 default:
5708 gfc_init_se (&se, NULL);
5709 gfc_conv_constant (&se, expr);
5710 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
5711 return se.expr;
5716 static tree
5717 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
5719 gfc_se rse;
5720 gfc_se lse;
5721 gfc_ss *rss;
5722 gfc_ss *lss;
5723 gfc_array_info *lss_array;
5724 stmtblock_t body;
5725 stmtblock_t block;
5726 gfc_loopinfo loop;
5727 int n;
5728 tree tmp;
5730 gfc_start_block (&block);
5732 /* Initialize the scalarizer. */
5733 gfc_init_loopinfo (&loop);
5735 gfc_init_se (&lse, NULL);
5736 gfc_init_se (&rse, NULL);
5738 /* Walk the rhs. */
5739 rss = gfc_walk_expr (expr);
5740 if (rss == gfc_ss_terminator)
5741 /* The rhs is scalar. Add a ss for the expression. */
5742 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
5744 /* Create a SS for the destination. */
5745 lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
5746 GFC_SS_COMPONENT);
5747 lss_array = &lss->info->data.array;
5748 lss_array->shape = gfc_get_shape (cm->as->rank);
5749 lss_array->descriptor = dest;
5750 lss_array->data = gfc_conv_array_data (dest);
5751 lss_array->offset = gfc_conv_array_offset (dest);
5752 for (n = 0; n < cm->as->rank; n++)
5754 lss_array->start[n] = gfc_conv_array_lbound (dest, n);
5755 lss_array->stride[n] = gfc_index_one_node;
5757 mpz_init (lss_array->shape[n]);
5758 mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
5759 cm->as->lower[n]->value.integer);
5760 mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
5763 /* Associate the SS with the loop. */
5764 gfc_add_ss_to_loop (&loop, lss);
5765 gfc_add_ss_to_loop (&loop, rss);
5767 /* Calculate the bounds of the scalarization. */
5768 gfc_conv_ss_startstride (&loop);
5770 /* Setup the scalarizing loops. */
5771 gfc_conv_loop_setup (&loop, &expr->where);
5773 /* Setup the gfc_se structures. */
5774 gfc_copy_loopinfo_to_se (&lse, &loop);
5775 gfc_copy_loopinfo_to_se (&rse, &loop);
5777 rse.ss = rss;
5778 gfc_mark_ss_chain_used (rss, 1);
5779 lse.ss = lss;
5780 gfc_mark_ss_chain_used (lss, 1);
5782 /* Start the scalarized loop body. */
5783 gfc_start_scalarized_body (&loop, &body);
5785 gfc_conv_tmp_array_ref (&lse);
5786 if (cm->ts.type == BT_CHARACTER)
5787 lse.string_length = cm->ts.u.cl->backend_decl;
5789 gfc_conv_expr (&rse, expr);
5791 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false, true);
5792 gfc_add_expr_to_block (&body, tmp);
5794 gcc_assert (rse.ss == gfc_ss_terminator);
5796 /* Generate the copying loops. */
5797 gfc_trans_scalarizing_loops (&loop, &body);
5799 /* Wrap the whole thing up. */
5800 gfc_add_block_to_block (&block, &loop.pre);
5801 gfc_add_block_to_block (&block, &loop.post);
5803 gcc_assert (lss_array->shape != NULL);
5804 gfc_free_shape (&lss_array->shape, cm->as->rank);
5805 gfc_cleanup_loop (&loop);
5807 return gfc_finish_block (&block);
5811 static tree
5812 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
5813 gfc_expr * expr)
5815 gfc_se se;
5816 stmtblock_t block;
5817 tree offset;
5818 int n;
5819 tree tmp;
5820 tree tmp2;
5821 gfc_array_spec *as;
5822 gfc_expr *arg = NULL;
5824 gfc_start_block (&block);
5825 gfc_init_se (&se, NULL);
5827 /* Get the descriptor for the expressions. */
5828 se.want_pointer = 0;
5829 gfc_conv_expr_descriptor (&se, expr);
5830 gfc_add_block_to_block (&block, &se.pre);
5831 gfc_add_modify (&block, dest, se.expr);
5833 /* Deal with arrays of derived types with allocatable components. */
5834 if (cm->ts.type == BT_DERIVED
5835 && cm->ts.u.derived->attr.alloc_comp)
5836 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
5837 se.expr, dest,
5838 cm->as->rank);
5839 else
5840 tmp = gfc_duplicate_allocatable (dest, se.expr,
5841 TREE_TYPE(cm->backend_decl),
5842 cm->as->rank);
5844 gfc_add_expr_to_block (&block, tmp);
5845 gfc_add_block_to_block (&block, &se.post);
5847 if (expr->expr_type != EXPR_VARIABLE)
5848 gfc_conv_descriptor_data_set (&block, se.expr,
5849 null_pointer_node);
5851 /* We need to know if the argument of a conversion function is a
5852 variable, so that the correct lower bound can be used. */
5853 if (expr->expr_type == EXPR_FUNCTION
5854 && expr->value.function.isym
5855 && expr->value.function.isym->conversion
5856 && expr->value.function.actual->expr
5857 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
5858 arg = expr->value.function.actual->expr;
5860 /* Obtain the array spec of full array references. */
5861 if (arg)
5862 as = gfc_get_full_arrayspec_from_expr (arg);
5863 else
5864 as = gfc_get_full_arrayspec_from_expr (expr);
5866 /* Shift the lbound and ubound of temporaries to being unity,
5867 rather than zero, based. Always calculate the offset. */
5868 offset = gfc_conv_descriptor_offset_get (dest);
5869 gfc_add_modify (&block, offset, gfc_index_zero_node);
5870 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
5872 for (n = 0; n < expr->rank; n++)
5874 tree span;
5875 tree lbound;
5877 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
5878 TODO It looks as if gfc_conv_expr_descriptor should return
5879 the correct bounds and that the following should not be
5880 necessary. This would simplify gfc_conv_intrinsic_bound
5881 as well. */
5882 if (as && as->lower[n])
5884 gfc_se lbse;
5885 gfc_init_se (&lbse, NULL);
5886 gfc_conv_expr (&lbse, as->lower[n]);
5887 gfc_add_block_to_block (&block, &lbse.pre);
5888 lbound = gfc_evaluate_now (lbse.expr, &block);
5890 else if (as && arg)
5892 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
5893 lbound = gfc_conv_descriptor_lbound_get (tmp,
5894 gfc_rank_cst[n]);
5896 else if (as)
5897 lbound = gfc_conv_descriptor_lbound_get (dest,
5898 gfc_rank_cst[n]);
5899 else
5900 lbound = gfc_index_one_node;
5902 lbound = fold_convert (gfc_array_index_type, lbound);
5904 /* Shift the bounds and set the offset accordingly. */
5905 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
5906 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5907 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
5908 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5909 span, lbound);
5910 gfc_conv_descriptor_ubound_set (&block, dest,
5911 gfc_rank_cst[n], tmp);
5912 gfc_conv_descriptor_lbound_set (&block, dest,
5913 gfc_rank_cst[n], lbound);
5915 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5916 gfc_conv_descriptor_lbound_get (dest,
5917 gfc_rank_cst[n]),
5918 gfc_conv_descriptor_stride_get (dest,
5919 gfc_rank_cst[n]));
5920 gfc_add_modify (&block, tmp2, tmp);
5921 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5922 offset, tmp2);
5923 gfc_conv_descriptor_offset_set (&block, dest, tmp);
5926 if (arg)
5928 /* If a conversion expression has a null data pointer
5929 argument, nullify the allocatable component. */
5930 tree non_null_expr;
5931 tree null_expr;
5933 if (arg->symtree->n.sym->attr.allocatable
5934 || arg->symtree->n.sym->attr.pointer)
5936 non_null_expr = gfc_finish_block (&block);
5937 gfc_start_block (&block);
5938 gfc_conv_descriptor_data_set (&block, dest,
5939 null_pointer_node);
5940 null_expr = gfc_finish_block (&block);
5941 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
5942 tmp = build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
5943 fold_convert (TREE_TYPE (tmp), null_pointer_node));
5944 return build3_v (COND_EXPR, tmp,
5945 null_expr, non_null_expr);
5949 return gfc_finish_block (&block);
5953 /* Assign a single component of a derived type constructor. */
5955 static tree
5956 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr)
5958 gfc_se se;
5959 gfc_se lse;
5960 stmtblock_t block;
5961 tree tmp;
5963 gfc_start_block (&block);
5965 if (cm->attr.pointer || cm->attr.proc_pointer)
5967 gfc_init_se (&se, NULL);
5968 /* Pointer component. */
5969 if (cm->attr.dimension && !cm->attr.proc_pointer)
5971 /* Array pointer. */
5972 if (expr->expr_type == EXPR_NULL)
5973 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
5974 else
5976 se.direct_byref = 1;
5977 se.expr = dest;
5978 gfc_conv_expr_descriptor (&se, expr);
5979 gfc_add_block_to_block (&block, &se.pre);
5980 gfc_add_block_to_block (&block, &se.post);
5983 else
5985 /* Scalar pointers. */
5986 se.want_pointer = 1;
5987 gfc_conv_expr (&se, expr);
5988 gfc_add_block_to_block (&block, &se.pre);
5990 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
5991 && expr->symtree->n.sym->attr.dummy)
5992 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
5994 gfc_add_modify (&block, dest,
5995 fold_convert (TREE_TYPE (dest), se.expr));
5996 gfc_add_block_to_block (&block, &se.post);
5999 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
6001 /* NULL initialization for CLASS components. */
6002 tmp = gfc_trans_structure_assign (dest,
6003 gfc_class_initializer (&cm->ts, expr));
6004 gfc_add_expr_to_block (&block, tmp);
6006 else if (cm->attr.dimension && !cm->attr.proc_pointer)
6008 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
6009 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
6010 else if (cm->attr.allocatable)
6012 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
6013 gfc_add_expr_to_block (&block, tmp);
6015 else
6017 tmp = gfc_trans_subarray_assign (dest, cm, expr);
6018 gfc_add_expr_to_block (&block, tmp);
6021 else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
6023 if (expr->expr_type != EXPR_STRUCTURE)
6025 gfc_init_se (&se, NULL);
6026 gfc_conv_expr (&se, expr);
6027 gfc_add_block_to_block (&block, &se.pre);
6028 gfc_add_modify (&block, dest,
6029 fold_convert (TREE_TYPE (dest), se.expr));
6030 gfc_add_block_to_block (&block, &se.post);
6032 else
6034 /* Nested constructors. */
6035 tmp = gfc_trans_structure_assign (dest, expr);
6036 gfc_add_expr_to_block (&block, tmp);
6039 else
6041 /* Scalar component. */
6042 gfc_init_se (&se, NULL);
6043 gfc_init_se (&lse, NULL);
6045 gfc_conv_expr (&se, expr);
6046 if (cm->ts.type == BT_CHARACTER)
6047 lse.string_length = cm->ts.u.cl->backend_decl;
6048 lse.expr = dest;
6049 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, true, false, true);
6050 gfc_add_expr_to_block (&block, tmp);
6052 return gfc_finish_block (&block);
6055 /* Assign a derived type constructor to a variable. */
6057 static tree
6058 gfc_trans_structure_assign (tree dest, gfc_expr * expr)
6060 gfc_constructor *c;
6061 gfc_component *cm;
6062 stmtblock_t block;
6063 tree field;
6064 tree tmp;
6066 gfc_start_block (&block);
6067 cm = expr->ts.u.derived->components;
6069 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
6070 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
6071 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
6073 gfc_se se, lse;
6075 gcc_assert (cm->backend_decl == NULL);
6076 gfc_init_se (&se, NULL);
6077 gfc_init_se (&lse, NULL);
6078 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
6079 lse.expr = dest;
6080 gfc_add_modify (&block, lse.expr,
6081 fold_convert (TREE_TYPE (lse.expr), se.expr));
6083 return gfc_finish_block (&block);
6086 for (c = gfc_constructor_first (expr->value.constructor);
6087 c; c = gfc_constructor_next (c), cm = cm->next)
6089 /* Skip absent members in default initializers. */
6090 if (!c->expr)
6091 continue;
6093 field = cm->backend_decl;
6094 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
6095 dest, field, NULL_TREE);
6096 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr);
6097 gfc_add_expr_to_block (&block, tmp);
6099 return gfc_finish_block (&block);
6102 /* Build an expression for a constructor. If init is nonzero then
6103 this is part of a static variable initializer. */
6105 void
6106 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
6108 gfc_constructor *c;
6109 gfc_component *cm;
6110 tree val;
6111 tree type;
6112 tree tmp;
6113 vec<constructor_elt, va_gc> *v = NULL;
6115 gcc_assert (se->ss == NULL);
6116 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
6117 type = gfc_typenode_for_spec (&expr->ts);
6119 if (!init)
6121 /* Create a temporary variable and fill it in. */
6122 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
6123 tmp = gfc_trans_structure_assign (se->expr, expr);
6124 gfc_add_expr_to_block (&se->pre, tmp);
6125 return;
6128 cm = expr->ts.u.derived->components;
6130 for (c = gfc_constructor_first (expr->value.constructor);
6131 c; c = gfc_constructor_next (c), cm = cm->next)
6133 /* Skip absent members in default initializers and allocatable
6134 components. Although the latter have a default initializer
6135 of EXPR_NULL,... by default, the static nullify is not needed
6136 since this is done every time we come into scope. */
6137 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
6138 continue;
6140 if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
6141 && strcmp (cm->name, "_extends") == 0
6142 && cm->initializer->symtree)
6144 tree vtab;
6145 gfc_symbol *vtabs;
6146 vtabs = cm->initializer->symtree->n.sym;
6147 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
6148 vtab = unshare_expr_without_location (vtab);
6149 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
6151 else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
6153 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
6154 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
6156 else
6158 val = gfc_conv_initializer (c->expr, &cm->ts,
6159 TREE_TYPE (cm->backend_decl),
6160 cm->attr.dimension, cm->attr.pointer,
6161 cm->attr.proc_pointer);
6162 val = unshare_expr_without_location (val);
6164 /* Append it to the constructor list. */
6165 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
6168 se->expr = build_constructor (type, v);
6169 if (init)
6170 TREE_CONSTANT (se->expr) = 1;
6174 /* Translate a substring expression. */
6176 static void
6177 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
6179 gfc_ref *ref;
6181 ref = expr->ref;
6183 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
6185 se->expr = gfc_build_wide_string_const (expr->ts.kind,
6186 expr->value.character.length,
6187 expr->value.character.string);
6189 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
6190 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
6192 if (ref)
6193 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
6197 /* Entry point for expression translation. Evaluates a scalar quantity.
6198 EXPR is the expression to be translated, and SE is the state structure if
6199 called from within the scalarized. */
6201 void
6202 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
6204 gfc_ss *ss;
6206 ss = se->ss;
6207 if (ss && ss->info->expr == expr
6208 && (ss->info->type == GFC_SS_SCALAR
6209 || ss->info->type == GFC_SS_REFERENCE))
6211 gfc_ss_info *ss_info;
6213 ss_info = ss->info;
6214 /* Substitute a scalar expression evaluated outside the scalarization
6215 loop. */
6216 se->expr = ss_info->data.scalar.value;
6217 /* If the reference can be NULL, the value field contains the reference,
6218 not the value the reference points to (see gfc_add_loop_ss_code). */
6219 if (ss_info->can_be_null_ref)
6220 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
6222 se->string_length = ss_info->string_length;
6223 gfc_advance_se_ss_chain (se);
6224 return;
6227 /* We need to convert the expressions for the iso_c_binding derived types.
6228 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
6229 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
6230 typespec for the C_PTR and C_FUNPTR symbols, which has already been
6231 updated to be an integer with a kind equal to the size of a (void *). */
6232 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID)
6234 if (expr->expr_type == EXPR_VARIABLE
6235 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
6236 || expr->symtree->n.sym->intmod_sym_id
6237 == ISOCBINDING_NULL_FUNPTR))
6239 /* Set expr_type to EXPR_NULL, which will result in
6240 null_pointer_node being used below. */
6241 expr->expr_type = EXPR_NULL;
6243 else
6245 /* Update the type/kind of the expression to be what the new
6246 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
6247 expr->ts.type = BT_INTEGER;
6248 expr->ts.f90_type = BT_VOID;
6249 expr->ts.kind = gfc_index_integer_kind;
6253 gfc_fix_class_refs (expr);
6255 switch (expr->expr_type)
6257 case EXPR_OP:
6258 gfc_conv_expr_op (se, expr);
6259 break;
6261 case EXPR_FUNCTION:
6262 gfc_conv_function_expr (se, expr);
6263 break;
6265 case EXPR_CONSTANT:
6266 gfc_conv_constant (se, expr);
6267 break;
6269 case EXPR_VARIABLE:
6270 gfc_conv_variable (se, expr);
6271 break;
6273 case EXPR_NULL:
6274 se->expr = null_pointer_node;
6275 break;
6277 case EXPR_SUBSTRING:
6278 gfc_conv_substring_expr (se, expr);
6279 break;
6281 case EXPR_STRUCTURE:
6282 gfc_conv_structure (se, expr, 0);
6283 break;
6285 case EXPR_ARRAY:
6286 gfc_conv_array_constructor_expr (se, expr);
6287 break;
6289 default:
6290 gcc_unreachable ();
6291 break;
6295 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
6296 of an assignment. */
6297 void
6298 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
6300 gfc_conv_expr (se, expr);
6301 /* All numeric lvalues should have empty post chains. If not we need to
6302 figure out a way of rewriting an lvalue so that it has no post chain. */
6303 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
6306 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
6307 numeric expressions. Used for scalar values where inserting cleanup code
6308 is inconvenient. */
6309 void
6310 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
6312 tree val;
6314 gcc_assert (expr->ts.type != BT_CHARACTER);
6315 gfc_conv_expr (se, expr);
6316 if (se->post.head)
6318 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
6319 gfc_add_modify (&se->pre, val, se->expr);
6320 se->expr = val;
6321 gfc_add_block_to_block (&se->pre, &se->post);
6325 /* Helper to translate an expression and convert it to a particular type. */
6326 void
6327 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
6329 gfc_conv_expr_val (se, expr);
6330 se->expr = convert (type, se->expr);
6334 /* Converts an expression so that it can be passed by reference. Scalar
6335 values only. */
6337 void
6338 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
6340 gfc_ss *ss;
6341 tree var;
6343 ss = se->ss;
6344 if (ss && ss->info->expr == expr
6345 && ss->info->type == GFC_SS_REFERENCE)
6347 /* Returns a reference to the scalar evaluated outside the loop
6348 for this case. */
6349 gfc_conv_expr (se, expr);
6350 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
6351 return;
6354 if (expr->ts.type == BT_CHARACTER)
6356 gfc_conv_expr (se, expr);
6357 gfc_conv_string_parameter (se);
6358 return;
6361 if (expr->expr_type == EXPR_VARIABLE)
6363 se->want_pointer = 1;
6364 gfc_conv_expr (se, expr);
6365 if (se->post.head)
6367 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
6368 gfc_add_modify (&se->pre, var, se->expr);
6369 gfc_add_block_to_block (&se->pre, &se->post);
6370 se->expr = var;
6372 return;
6375 if (expr->expr_type == EXPR_FUNCTION
6376 && ((expr->value.function.esym
6377 && expr->value.function.esym->result->attr.pointer
6378 && !expr->value.function.esym->result->attr.dimension)
6379 || (!expr->value.function.esym && !expr->ref
6380 && expr->symtree->n.sym->attr.pointer
6381 && !expr->symtree->n.sym->attr.dimension)))
6383 se->want_pointer = 1;
6384 gfc_conv_expr (se, expr);
6385 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
6386 gfc_add_modify (&se->pre, var, se->expr);
6387 se->expr = var;
6388 return;
6391 gfc_conv_expr (se, expr);
6393 /* Create a temporary var to hold the value. */
6394 if (TREE_CONSTANT (se->expr))
6396 tree tmp = se->expr;
6397 STRIP_TYPE_NOPS (tmp);
6398 var = build_decl (input_location,
6399 CONST_DECL, NULL, TREE_TYPE (tmp));
6400 DECL_INITIAL (var) = tmp;
6401 TREE_STATIC (var) = 1;
6402 pushdecl (var);
6404 else
6406 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
6407 gfc_add_modify (&se->pre, var, se->expr);
6409 gfc_add_block_to_block (&se->pre, &se->post);
6411 /* Take the address of that value. */
6412 se->expr = gfc_build_addr_expr (NULL_TREE, var);
6416 tree
6417 gfc_trans_pointer_assign (gfc_code * code)
6419 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
6423 /* Generate code for a pointer assignment. */
6425 tree
6426 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
6428 gfc_expr *expr1_vptr = NULL;
6429 gfc_se lse;
6430 gfc_se rse;
6431 stmtblock_t block;
6432 tree desc;
6433 tree tmp;
6434 tree decl;
6435 bool scalar;
6436 gfc_ss *ss;
6438 gfc_start_block (&block);
6440 gfc_init_se (&lse, NULL);
6442 /* Check whether the expression is a scalar or not; we cannot use
6443 expr1->rank as it can be nonzero for proc pointers. */
6444 ss = gfc_walk_expr (expr1);
6445 scalar = ss == gfc_ss_terminator;
6446 if (!scalar)
6447 gfc_free_ss_chain (ss);
6449 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
6450 && expr2->expr_type != EXPR_FUNCTION)
6452 gfc_add_data_component (expr2);
6453 /* The following is required as gfc_add_data_component doesn't
6454 update ts.type if there is a tailing REF_ARRAY. */
6455 expr2->ts.type = BT_DERIVED;
6458 if (scalar)
6460 /* Scalar pointers. */
6461 lse.want_pointer = 1;
6462 gfc_conv_expr (&lse, expr1);
6463 gfc_init_se (&rse, NULL);
6464 rse.want_pointer = 1;
6465 gfc_conv_expr (&rse, expr2);
6467 if (expr1->symtree->n.sym->attr.proc_pointer
6468 && expr1->symtree->n.sym->attr.dummy)
6469 lse.expr = build_fold_indirect_ref_loc (input_location,
6470 lse.expr);
6472 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
6473 && expr2->symtree->n.sym->attr.dummy)
6474 rse.expr = build_fold_indirect_ref_loc (input_location,
6475 rse.expr);
6477 gfc_add_block_to_block (&block, &lse.pre);
6478 gfc_add_block_to_block (&block, &rse.pre);
6480 /* Check character lengths if character expression. The test is only
6481 really added if -fbounds-check is enabled. Exclude deferred
6482 character length lefthand sides. */
6483 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
6484 && !expr1->ts.deferred
6485 && !expr1->symtree->n.sym->attr.proc_pointer
6486 && !gfc_is_proc_ptr_comp (expr1))
6488 gcc_assert (expr2->ts.type == BT_CHARACTER);
6489 gcc_assert (lse.string_length && rse.string_length);
6490 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
6491 lse.string_length, rse.string_length,
6492 &block);
6495 /* The assignment to an deferred character length sets the string
6496 length to that of the rhs. */
6497 if (expr1->ts.deferred)
6499 if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
6500 gfc_add_modify (&block, lse.string_length, rse.string_length);
6501 else if (lse.string_length != NULL)
6502 gfc_add_modify (&block, lse.string_length,
6503 build_int_cst (gfc_charlen_type_node, 0));
6506 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS)
6507 rse.expr = gfc_class_data_get (rse.expr);
6509 gfc_add_modify (&block, lse.expr,
6510 fold_convert (TREE_TYPE (lse.expr), rse.expr));
6512 gfc_add_block_to_block (&block, &rse.post);
6513 gfc_add_block_to_block (&block, &lse.post);
6515 else
6517 gfc_ref* remap;
6518 bool rank_remap;
6519 tree strlen_lhs;
6520 tree strlen_rhs = NULL_TREE;
6522 /* Array pointer. Find the last reference on the LHS and if it is an
6523 array section ref, we're dealing with bounds remapping. In this case,
6524 set it to AR_FULL so that gfc_conv_expr_descriptor does
6525 not see it and process the bounds remapping afterwards explicitly. */
6526 for (remap = expr1->ref; remap; remap = remap->next)
6527 if (!remap->next && remap->type == REF_ARRAY
6528 && remap->u.ar.type == AR_SECTION)
6529 break;
6530 rank_remap = (remap && remap->u.ar.end[0]);
6532 gfc_init_se (&lse, NULL);
6533 if (remap)
6534 lse.descriptor_only = 1;
6535 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS
6536 && expr1->ts.type == BT_CLASS)
6537 expr1_vptr = gfc_copy_expr (expr1);
6538 gfc_conv_expr_descriptor (&lse, expr1);
6539 strlen_lhs = lse.string_length;
6540 desc = lse.expr;
6542 if (expr2->expr_type == EXPR_NULL)
6544 /* Just set the data pointer to null. */
6545 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
6547 else if (rank_remap)
6549 /* If we are rank-remapping, just get the RHS's descriptor and
6550 process this later on. */
6551 gfc_init_se (&rse, NULL);
6552 rse.direct_byref = 1;
6553 rse.byref_noassign = 1;
6555 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
6557 gfc_conv_function_expr (&rse, expr2);
6559 if (expr1->ts.type != BT_CLASS)
6560 rse.expr = gfc_class_data_get (rse.expr);
6561 else
6563 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
6564 gfc_add_modify (&lse.pre, tmp, rse.expr);
6566 gfc_add_vptr_component (expr1_vptr);
6567 gfc_init_se (&rse, NULL);
6568 rse.want_pointer = 1;
6569 gfc_conv_expr (&rse, expr1_vptr);
6570 gfc_add_modify (&lse.pre, rse.expr,
6571 fold_convert (TREE_TYPE (rse.expr),
6572 gfc_class_vptr_get (tmp)));
6573 rse.expr = gfc_class_data_get (tmp);
6576 else if (expr2->expr_type == EXPR_FUNCTION)
6578 tree bound[GFC_MAX_DIMENSIONS];
6579 int i;
6581 for (i = 0; i < expr2->rank; i++)
6582 bound[i] = NULL_TREE;
6583 tmp = gfc_typenode_for_spec (&expr2->ts);
6584 tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
6585 bound, bound, 0,
6586 GFC_ARRAY_POINTER_CONT, false);
6587 tmp = gfc_create_var (tmp, "ptrtemp");
6588 lse.expr = tmp;
6589 lse.direct_byref = 1;
6590 gfc_conv_expr_descriptor (&lse, expr2);
6591 strlen_rhs = lse.string_length;
6592 rse.expr = tmp;
6594 else
6596 gfc_conv_expr_descriptor (&rse, expr2);
6597 strlen_rhs = rse.string_length;
6600 else if (expr2->expr_type == EXPR_VARIABLE)
6602 /* Assign directly to the LHS's descriptor. */
6603 lse.direct_byref = 1;
6604 gfc_conv_expr_descriptor (&lse, expr2);
6605 strlen_rhs = lse.string_length;
6607 /* If this is a subreference array pointer assignment, use the rhs
6608 descriptor element size for the lhs span. */
6609 if (expr1->symtree->n.sym->attr.subref_array_pointer)
6611 decl = expr1->symtree->n.sym->backend_decl;
6612 gfc_init_se (&rse, NULL);
6613 rse.descriptor_only = 1;
6614 gfc_conv_expr (&rse, expr2);
6615 tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
6616 tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
6617 if (!INTEGER_CST_P (tmp))
6618 gfc_add_block_to_block (&lse.post, &rse.pre);
6619 gfc_add_modify (&lse.post, GFC_DECL_SPAN(decl), tmp);
6622 else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
6624 gfc_init_se (&rse, NULL);
6625 rse.want_pointer = 1;
6626 gfc_conv_function_expr (&rse, expr2);
6627 if (expr1->ts.type != BT_CLASS)
6629 rse.expr = gfc_class_data_get (rse.expr);
6630 gfc_add_modify (&lse.pre, desc, rse.expr);
6632 else
6634 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
6635 gfc_add_modify (&lse.pre, tmp, rse.expr);
6637 gfc_add_vptr_component (expr1_vptr);
6638 gfc_init_se (&rse, NULL);
6639 rse.want_pointer = 1;
6640 gfc_conv_expr (&rse, expr1_vptr);
6641 gfc_add_modify (&lse.pre, rse.expr,
6642 fold_convert (TREE_TYPE (rse.expr),
6643 gfc_class_vptr_get (tmp)));
6644 rse.expr = gfc_class_data_get (tmp);
6645 gfc_add_modify (&lse.pre, desc, rse.expr);
6648 else
6650 /* Assign to a temporary descriptor and then copy that
6651 temporary to the pointer. */
6652 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
6653 lse.expr = tmp;
6654 lse.direct_byref = 1;
6655 gfc_conv_expr_descriptor (&lse, expr2);
6656 strlen_rhs = lse.string_length;
6657 gfc_add_modify (&lse.pre, desc, tmp);
6660 if (expr1_vptr)
6661 gfc_free_expr (expr1_vptr);
6663 gfc_add_block_to_block (&block, &lse.pre);
6664 if (rank_remap)
6665 gfc_add_block_to_block (&block, &rse.pre);
6667 /* If we do bounds remapping, update LHS descriptor accordingly. */
6668 if (remap)
6670 int dim;
6671 gcc_assert (remap->u.ar.dimen == expr1->rank);
6673 if (rank_remap)
6675 /* Do rank remapping. We already have the RHS's descriptor
6676 converted in rse and now have to build the correct LHS
6677 descriptor for it. */
6679 tree dtype, data;
6680 tree offs, stride;
6681 tree lbound, ubound;
6683 /* Set dtype. */
6684 dtype = gfc_conv_descriptor_dtype (desc);
6685 tmp = gfc_get_dtype (TREE_TYPE (desc));
6686 gfc_add_modify (&block, dtype, tmp);
6688 /* Copy data pointer. */
6689 data = gfc_conv_descriptor_data_get (rse.expr);
6690 gfc_conv_descriptor_data_set (&block, desc, data);
6692 /* Copy offset but adjust it such that it would correspond
6693 to a lbound of zero. */
6694 offs = gfc_conv_descriptor_offset_get (rse.expr);
6695 for (dim = 0; dim < expr2->rank; ++dim)
6697 stride = gfc_conv_descriptor_stride_get (rse.expr,
6698 gfc_rank_cst[dim]);
6699 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
6700 gfc_rank_cst[dim]);
6701 tmp = fold_build2_loc (input_location, MULT_EXPR,
6702 gfc_array_index_type, stride, lbound);
6703 offs = fold_build2_loc (input_location, PLUS_EXPR,
6704 gfc_array_index_type, offs, tmp);
6706 gfc_conv_descriptor_offset_set (&block, desc, offs);
6708 /* Set the bounds as declared for the LHS and calculate strides as
6709 well as another offset update accordingly. */
6710 stride = gfc_conv_descriptor_stride_get (rse.expr,
6711 gfc_rank_cst[0]);
6712 for (dim = 0; dim < expr1->rank; ++dim)
6714 gfc_se lower_se;
6715 gfc_se upper_se;
6717 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
6719 /* Convert declared bounds. */
6720 gfc_init_se (&lower_se, NULL);
6721 gfc_init_se (&upper_se, NULL);
6722 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
6723 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
6725 gfc_add_block_to_block (&block, &lower_se.pre);
6726 gfc_add_block_to_block (&block, &upper_se.pre);
6728 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
6729 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
6731 lbound = gfc_evaluate_now (lbound, &block);
6732 ubound = gfc_evaluate_now (ubound, &block);
6734 gfc_add_block_to_block (&block, &lower_se.post);
6735 gfc_add_block_to_block (&block, &upper_se.post);
6737 /* Set bounds in descriptor. */
6738 gfc_conv_descriptor_lbound_set (&block, desc,
6739 gfc_rank_cst[dim], lbound);
6740 gfc_conv_descriptor_ubound_set (&block, desc,
6741 gfc_rank_cst[dim], ubound);
6743 /* Set stride. */
6744 stride = gfc_evaluate_now (stride, &block);
6745 gfc_conv_descriptor_stride_set (&block, desc,
6746 gfc_rank_cst[dim], stride);
6748 /* Update offset. */
6749 offs = gfc_conv_descriptor_offset_get (desc);
6750 tmp = fold_build2_loc (input_location, MULT_EXPR,
6751 gfc_array_index_type, lbound, stride);
6752 offs = fold_build2_loc (input_location, MINUS_EXPR,
6753 gfc_array_index_type, offs, tmp);
6754 offs = gfc_evaluate_now (offs, &block);
6755 gfc_conv_descriptor_offset_set (&block, desc, offs);
6757 /* Update stride. */
6758 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
6759 stride = fold_build2_loc (input_location, MULT_EXPR,
6760 gfc_array_index_type, stride, tmp);
6763 else
6765 /* Bounds remapping. Just shift the lower bounds. */
6767 gcc_assert (expr1->rank == expr2->rank);
6769 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
6771 gfc_se lbound_se;
6773 gcc_assert (remap->u.ar.start[dim]);
6774 gcc_assert (!remap->u.ar.end[dim]);
6775 gfc_init_se (&lbound_se, NULL);
6776 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
6778 gfc_add_block_to_block (&block, &lbound_se.pre);
6779 gfc_conv_shift_descriptor_lbound (&block, desc,
6780 dim, lbound_se.expr);
6781 gfc_add_block_to_block (&block, &lbound_se.post);
6786 /* Check string lengths if applicable. The check is only really added
6787 to the output code if -fbounds-check is enabled. */
6788 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
6790 gcc_assert (expr2->ts.type == BT_CHARACTER);
6791 gcc_assert (strlen_lhs && strlen_rhs);
6792 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
6793 strlen_lhs, strlen_rhs, &block);
6796 /* If rank remapping was done, check with -fcheck=bounds that
6797 the target is at least as large as the pointer. */
6798 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
6800 tree lsize, rsize;
6801 tree fault;
6802 const char* msg;
6804 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
6805 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
6807 lsize = gfc_evaluate_now (lsize, &block);
6808 rsize = gfc_evaluate_now (rsize, &block);
6809 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
6810 rsize, lsize);
6812 msg = _("Target of rank remapping is too small (%ld < %ld)");
6813 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
6814 msg, rsize, lsize);
6817 gfc_add_block_to_block (&block, &lse.post);
6818 if (rank_remap)
6819 gfc_add_block_to_block (&block, &rse.post);
6822 return gfc_finish_block (&block);
6826 /* Makes sure se is suitable for passing as a function string parameter. */
6827 /* TODO: Need to check all callers of this function. It may be abused. */
6829 void
6830 gfc_conv_string_parameter (gfc_se * se)
6832 tree type;
6834 if (TREE_CODE (se->expr) == STRING_CST)
6836 type = TREE_TYPE (TREE_TYPE (se->expr));
6837 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
6838 return;
6841 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
6843 if (TREE_CODE (se->expr) != INDIRECT_REF)
6845 type = TREE_TYPE (se->expr);
6846 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
6848 else
6850 type = gfc_get_character_type_len (gfc_default_character_kind,
6851 se->string_length);
6852 type = build_pointer_type (type);
6853 se->expr = gfc_build_addr_expr (type, se->expr);
6857 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
6861 /* Generate code for assignment of scalar variables. Includes character
6862 strings and derived types with allocatable components.
6863 If you know that the LHS has no allocations, set dealloc to false.
6865 DEEP_COPY has no effect if the typespec TS is not a derived type with
6866 allocatable components. Otherwise, if it is set, an explicit copy of each
6867 allocatable component is made. This is necessary as a simple copy of the
6868 whole object would copy array descriptors as is, so that the lhs's
6869 allocatable components would point to the rhs's after the assignment.
6870 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
6871 necessary if the rhs is a non-pointer function, as the allocatable components
6872 are not accessible by other means than the function's result after the
6873 function has returned. It is even more subtle when temporaries are involved,
6874 as the two following examples show:
6875 1. When we evaluate an array constructor, a temporary is created. Thus
6876 there is theoretically no alias possible. However, no deep copy is
6877 made for this temporary, so that if the constructor is made of one or
6878 more variable with allocatable components, those components still point
6879 to the variable's: DEEP_COPY should be set for the assignment from the
6880 temporary to the lhs in that case.
6881 2. When assigning a scalar to an array, we evaluate the scalar value out
6882 of the loop, store it into a temporary variable, and assign from that.
6883 In that case, deep copying when assigning to the temporary would be a
6884 waste of resources; however deep copies should happen when assigning from
6885 the temporary to each array element: again DEEP_COPY should be set for
6886 the assignment from the temporary to the lhs. */
6888 tree
6889 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
6890 bool l_is_temp, bool deep_copy, bool dealloc)
6892 stmtblock_t block;
6893 tree tmp;
6894 tree cond;
6896 gfc_init_block (&block);
6898 if (ts.type == BT_CHARACTER)
6900 tree rlen = NULL;
6901 tree llen = NULL;
6903 if (lse->string_length != NULL_TREE)
6905 gfc_conv_string_parameter (lse);
6906 gfc_add_block_to_block (&block, &lse->pre);
6907 llen = lse->string_length;
6910 if (rse->string_length != NULL_TREE)
6912 gcc_assert (rse->string_length != NULL_TREE);
6913 gfc_conv_string_parameter (rse);
6914 gfc_add_block_to_block (&block, &rse->pre);
6915 rlen = rse->string_length;
6918 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
6919 rse->expr, ts.kind);
6921 else if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
6923 tree tmp_var = NULL_TREE;
6924 cond = NULL_TREE;
6926 /* Are the rhs and the lhs the same? */
6927 if (deep_copy)
6929 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
6930 gfc_build_addr_expr (NULL_TREE, lse->expr),
6931 gfc_build_addr_expr (NULL_TREE, rse->expr));
6932 cond = gfc_evaluate_now (cond, &lse->pre);
6935 /* Deallocate the lhs allocated components as long as it is not
6936 the same as the rhs. This must be done following the assignment
6937 to prevent deallocating data that could be used in the rhs
6938 expression. */
6939 if (!l_is_temp && dealloc)
6941 tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
6942 tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var, 0);
6943 if (deep_copy)
6944 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
6945 tmp);
6946 gfc_add_expr_to_block (&lse->post, tmp);
6949 gfc_add_block_to_block (&block, &rse->pre);
6950 gfc_add_block_to_block (&block, &lse->pre);
6952 gfc_add_modify (&block, lse->expr,
6953 fold_convert (TREE_TYPE (lse->expr), rse->expr));
6955 /* Restore pointer address of coarray components. */
6956 if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
6958 tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
6959 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
6960 tmp);
6961 gfc_add_expr_to_block (&block, tmp);
6964 /* Do a deep copy if the rhs is a variable, if it is not the
6965 same as the lhs. */
6966 if (deep_copy)
6968 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0);
6969 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
6970 tmp);
6971 gfc_add_expr_to_block (&block, tmp);
6974 else if (ts.type == BT_DERIVED || ts.type == BT_CLASS)
6976 gfc_add_block_to_block (&block, &lse->pre);
6977 gfc_add_block_to_block (&block, &rse->pre);
6978 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
6979 TREE_TYPE (lse->expr), rse->expr);
6980 gfc_add_modify (&block, lse->expr, tmp);
6982 else
6984 gfc_add_block_to_block (&block, &lse->pre);
6985 gfc_add_block_to_block (&block, &rse->pre);
6987 gfc_add_modify (&block, lse->expr,
6988 fold_convert (TREE_TYPE (lse->expr), rse->expr));
6991 gfc_add_block_to_block (&block, &lse->post);
6992 gfc_add_block_to_block (&block, &rse->post);
6994 return gfc_finish_block (&block);
6998 /* There are quite a lot of restrictions on the optimisation in using an
6999 array function assign without a temporary. */
7001 static bool
7002 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
7004 gfc_ref * ref;
7005 bool seen_array_ref;
7006 bool c = false;
7007 gfc_symbol *sym = expr1->symtree->n.sym;
7009 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
7010 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
7011 return true;
7013 /* Elemental functions are scalarized so that they don't need a
7014 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
7015 they would need special treatment in gfc_trans_arrayfunc_assign. */
7016 if (expr2->value.function.esym != NULL
7017 && expr2->value.function.esym->attr.elemental)
7018 return true;
7020 /* Need a temporary if rhs is not FULL or a contiguous section. */
7021 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
7022 return true;
7024 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
7025 if (gfc_ref_needs_temporary_p (expr1->ref))
7026 return true;
7028 /* Functions returning pointers or allocatables need temporaries. */
7029 c = expr2->value.function.esym
7030 ? (expr2->value.function.esym->attr.pointer
7031 || expr2->value.function.esym->attr.allocatable)
7032 : (expr2->symtree->n.sym->attr.pointer
7033 || expr2->symtree->n.sym->attr.allocatable);
7034 if (c)
7035 return true;
7037 /* Character array functions need temporaries unless the
7038 character lengths are the same. */
7039 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
7041 if (expr1->ts.u.cl->length == NULL
7042 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
7043 return true;
7045 if (expr2->ts.u.cl->length == NULL
7046 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
7047 return true;
7049 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
7050 expr2->ts.u.cl->length->value.integer) != 0)
7051 return true;
7054 /* Check that no LHS component references appear during an array
7055 reference. This is needed because we do not have the means to
7056 span any arbitrary stride with an array descriptor. This check
7057 is not needed for the rhs because the function result has to be
7058 a complete type. */
7059 seen_array_ref = false;
7060 for (ref = expr1->ref; ref; ref = ref->next)
7062 if (ref->type == REF_ARRAY)
7063 seen_array_ref= true;
7064 else if (ref->type == REF_COMPONENT && seen_array_ref)
7065 return true;
7068 /* Check for a dependency. */
7069 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
7070 expr2->value.function.esym,
7071 expr2->value.function.actual,
7072 NOT_ELEMENTAL))
7073 return true;
7075 /* If we have reached here with an intrinsic function, we do not
7076 need a temporary except in the particular case that reallocation
7077 on assignment is active and the lhs is allocatable and a target. */
7078 if (expr2->value.function.isym)
7079 return (gfc_option.flag_realloc_lhs
7080 && sym->attr.allocatable
7081 && sym->attr.target);
7083 /* If the LHS is a dummy, we need a temporary if it is not
7084 INTENT(OUT). */
7085 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
7086 return true;
7088 /* If the lhs has been host_associated, is in common, a pointer or is
7089 a target and the function is not using a RESULT variable, aliasing
7090 can occur and a temporary is needed. */
7091 if ((sym->attr.host_assoc
7092 || sym->attr.in_common
7093 || sym->attr.pointer
7094 || sym->attr.cray_pointee
7095 || sym->attr.target)
7096 && expr2->symtree != NULL
7097 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
7098 return true;
7100 /* A PURE function can unconditionally be called without a temporary. */
7101 if (expr2->value.function.esym != NULL
7102 && expr2->value.function.esym->attr.pure)
7103 return false;
7105 /* Implicit_pure functions are those which could legally be declared
7106 to be PURE. */
7107 if (expr2->value.function.esym != NULL
7108 && expr2->value.function.esym->attr.implicit_pure)
7109 return false;
7111 if (!sym->attr.use_assoc
7112 && !sym->attr.in_common
7113 && !sym->attr.pointer
7114 && !sym->attr.target
7115 && !sym->attr.cray_pointee
7116 && expr2->value.function.esym)
7118 /* A temporary is not needed if the function is not contained and
7119 the variable is local or host associated and not a pointer or
7120 a target. */
7121 if (!expr2->value.function.esym->attr.contained)
7122 return false;
7124 /* A temporary is not needed if the lhs has never been host
7125 associated and the procedure is contained. */
7126 else if (!sym->attr.host_assoc)
7127 return false;
7129 /* A temporary is not needed if the variable is local and not
7130 a pointer, a target or a result. */
7131 if (sym->ns->parent
7132 && expr2->value.function.esym->ns == sym->ns->parent)
7133 return false;
7136 /* Default to temporary use. */
7137 return true;
7141 /* Provide the loop info so that the lhs descriptor can be built for
7142 reallocatable assignments from extrinsic function calls. */
7144 static void
7145 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
7146 gfc_loopinfo *loop)
7148 /* Signal that the function call should not be made by
7149 gfc_conv_loop_setup. */
7150 se->ss->is_alloc_lhs = 1;
7151 gfc_init_loopinfo (loop);
7152 gfc_add_ss_to_loop (loop, *ss);
7153 gfc_add_ss_to_loop (loop, se->ss);
7154 gfc_conv_ss_startstride (loop);
7155 gfc_conv_loop_setup (loop, where);
7156 gfc_copy_loopinfo_to_se (se, loop);
7157 gfc_add_block_to_block (&se->pre, &loop->pre);
7158 gfc_add_block_to_block (&se->pre, &loop->post);
7159 se->ss->is_alloc_lhs = 0;
7163 /* For assignment to a reallocatable lhs from intrinsic functions,
7164 replace the se.expr (ie. the result) with a temporary descriptor.
7165 Null the data field so that the library allocates space for the
7166 result. Free the data of the original descriptor after the function,
7167 in case it appears in an argument expression and transfer the
7168 result to the original descriptor. */
7170 static void
7171 fcncall_realloc_result (gfc_se *se, int rank)
7173 tree desc;
7174 tree res_desc;
7175 tree tmp;
7176 tree offset;
7177 tree zero_cond;
7178 int n;
7180 /* Use the allocation done by the library. Substitute the lhs
7181 descriptor with a copy, whose data field is nulled.*/
7182 desc = build_fold_indirect_ref_loc (input_location, se->expr);
7183 if (POINTER_TYPE_P (TREE_TYPE (desc)))
7184 desc = build_fold_indirect_ref_loc (input_location, desc);
7186 /* Unallocated, the descriptor does not have a dtype. */
7187 tmp = gfc_conv_descriptor_dtype (desc);
7188 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
7190 res_desc = gfc_evaluate_now (desc, &se->pre);
7191 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
7192 se->expr = gfc_build_addr_expr (TREE_TYPE (se->expr), res_desc);
7194 /* Free the lhs after the function call and copy the result data to
7195 the lhs descriptor. */
7196 tmp = gfc_conv_descriptor_data_get (desc);
7197 zero_cond = fold_build2_loc (input_location, EQ_EXPR,
7198 boolean_type_node, tmp,
7199 build_int_cst (TREE_TYPE (tmp), 0));
7200 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
7201 tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
7202 gfc_add_expr_to_block (&se->post, tmp);
7204 tmp = gfc_conv_descriptor_data_get (res_desc);
7205 gfc_conv_descriptor_data_set (&se->post, desc, tmp);
7207 /* Check that the shapes are the same between lhs and expression. */
7208 for (n = 0 ; n < rank; n++)
7210 tree tmp1;
7211 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
7212 tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
7213 tmp = fold_build2_loc (input_location, MINUS_EXPR,
7214 gfc_array_index_type, tmp, tmp1);
7215 tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
7216 tmp = fold_build2_loc (input_location, MINUS_EXPR,
7217 gfc_array_index_type, tmp, tmp1);
7218 tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
7219 tmp = fold_build2_loc (input_location, PLUS_EXPR,
7220 gfc_array_index_type, tmp, tmp1);
7221 tmp = fold_build2_loc (input_location, NE_EXPR,
7222 boolean_type_node, tmp,
7223 gfc_index_zero_node);
7224 tmp = gfc_evaluate_now (tmp, &se->post);
7225 zero_cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
7226 boolean_type_node, tmp,
7227 zero_cond);
7230 /* 'zero_cond' being true is equal to lhs not being allocated or the
7231 shapes being different. */
7232 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
7234 /* Now reset the bounds returned from the function call to bounds based
7235 on the lhs lbounds, except where the lhs is not allocated or the shapes
7236 of 'variable and 'expr' are different. Set the offset accordingly. */
7237 offset = gfc_index_zero_node;
7238 for (n = 0 ; n < rank; n++)
7240 tree lbound;
7242 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
7243 lbound = fold_build3_loc (input_location, COND_EXPR,
7244 gfc_array_index_type, zero_cond,
7245 gfc_index_one_node, lbound);
7246 lbound = gfc_evaluate_now (lbound, &se->post);
7248 tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
7249 tmp = fold_build2_loc (input_location, PLUS_EXPR,
7250 gfc_array_index_type, tmp, lbound);
7251 gfc_conv_descriptor_lbound_set (&se->post, desc,
7252 gfc_rank_cst[n], lbound);
7253 gfc_conv_descriptor_ubound_set (&se->post, desc,
7254 gfc_rank_cst[n], tmp);
7256 /* Set stride and accumulate the offset. */
7257 tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
7258 gfc_conv_descriptor_stride_set (&se->post, desc,
7259 gfc_rank_cst[n], tmp);
7260 tmp = fold_build2_loc (input_location, MULT_EXPR,
7261 gfc_array_index_type, lbound, tmp);
7262 offset = fold_build2_loc (input_location, MINUS_EXPR,
7263 gfc_array_index_type, offset, tmp);
7264 offset = gfc_evaluate_now (offset, &se->post);
7267 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
7272 /* Try to translate array(:) = func (...), where func is a transformational
7273 array function, without using a temporary. Returns NULL if this isn't the
7274 case. */
7276 static tree
7277 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
7279 gfc_se se;
7280 gfc_ss *ss = NULL;
7281 gfc_component *comp = NULL;
7282 gfc_loopinfo loop;
7284 if (arrayfunc_assign_needs_temporary (expr1, expr2))
7285 return NULL;
7287 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
7288 functions. */
7289 comp = gfc_get_proc_ptr_comp (expr2);
7290 gcc_assert (expr2->value.function.isym
7291 || (comp && comp->attr.dimension)
7292 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
7293 && expr2->value.function.esym->result->attr.dimension));
7295 gfc_init_se (&se, NULL);
7296 gfc_start_block (&se.pre);
7297 se.want_pointer = 1;
7299 gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
7301 if (expr1->ts.type == BT_DERIVED
7302 && expr1->ts.u.derived->attr.alloc_comp)
7304 tree tmp;
7305 tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, se.expr,
7306 expr1->rank);
7307 gfc_add_expr_to_block (&se.pre, tmp);
7310 se.direct_byref = 1;
7311 se.ss = gfc_walk_expr (expr2);
7312 gcc_assert (se.ss != gfc_ss_terminator);
7314 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
7315 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
7316 Clearly, this cannot be done for an allocatable function result, since
7317 the shape of the result is unknown and, in any case, the function must
7318 correctly take care of the reallocation internally. For intrinsic
7319 calls, the array data is freed and the library takes care of allocation.
7320 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
7321 to the library. */
7322 if (gfc_option.flag_realloc_lhs
7323 && gfc_is_reallocatable_lhs (expr1)
7324 && !gfc_expr_attr (expr1).codimension
7325 && !gfc_is_coindexed (expr1)
7326 && !(expr2->value.function.esym
7327 && expr2->value.function.esym->result->attr.allocatable))
7329 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
7331 if (!expr2->value.function.isym)
7333 ss = gfc_walk_expr (expr1);
7334 gcc_assert (ss != gfc_ss_terminator);
7336 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
7337 ss->is_alloc_lhs = 1;
7339 else
7340 fcncall_realloc_result (&se, expr1->rank);
7343 gfc_conv_function_expr (&se, expr2);
7344 gfc_add_block_to_block (&se.pre, &se.post);
7346 if (ss)
7347 gfc_cleanup_loop (&loop);
7348 else
7349 gfc_free_ss_chain (se.ss);
7351 return gfc_finish_block (&se.pre);
7355 /* Try to efficiently translate array(:) = 0. Return NULL if this
7356 can't be done. */
7358 static tree
7359 gfc_trans_zero_assign (gfc_expr * expr)
7361 tree dest, len, type;
7362 tree tmp;
7363 gfc_symbol *sym;
7365 sym = expr->symtree->n.sym;
7366 dest = gfc_get_symbol_decl (sym);
7368 type = TREE_TYPE (dest);
7369 if (POINTER_TYPE_P (type))
7370 type = TREE_TYPE (type);
7371 if (!GFC_ARRAY_TYPE_P (type))
7372 return NULL_TREE;
7374 /* Determine the length of the array. */
7375 len = GFC_TYPE_ARRAY_SIZE (type);
7376 if (!len || TREE_CODE (len) != INTEGER_CST)
7377 return NULL_TREE;
7379 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
7380 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
7381 fold_convert (gfc_array_index_type, tmp));
7383 /* If we are zeroing a local array avoid taking its address by emitting
7384 a = {} instead. */
7385 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
7386 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
7387 dest, build_constructor (TREE_TYPE (dest),
7388 NULL));
7390 /* Convert arguments to the correct types. */
7391 dest = fold_convert (pvoid_type_node, dest);
7392 len = fold_convert (size_type_node, len);
7394 /* Construct call to __builtin_memset. */
7395 tmp = build_call_expr_loc (input_location,
7396 builtin_decl_explicit (BUILT_IN_MEMSET),
7397 3, dest, integer_zero_node, len);
7398 return fold_convert (void_type_node, tmp);
7402 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
7403 that constructs the call to __builtin_memcpy. */
7405 tree
7406 gfc_build_memcpy_call (tree dst, tree src, tree len)
7408 tree tmp;
7410 /* Convert arguments to the correct types. */
7411 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
7412 dst = gfc_build_addr_expr (pvoid_type_node, dst);
7413 else
7414 dst = fold_convert (pvoid_type_node, dst);
7416 if (!POINTER_TYPE_P (TREE_TYPE (src)))
7417 src = gfc_build_addr_expr (pvoid_type_node, src);
7418 else
7419 src = fold_convert (pvoid_type_node, src);
7421 len = fold_convert (size_type_node, len);
7423 /* Construct call to __builtin_memcpy. */
7424 tmp = build_call_expr_loc (input_location,
7425 builtin_decl_explicit (BUILT_IN_MEMCPY),
7426 3, dst, src, len);
7427 return fold_convert (void_type_node, tmp);
7431 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
7432 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
7433 source/rhs, both are gfc_full_array_ref_p which have been checked for
7434 dependencies. */
7436 static tree
7437 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
7439 tree dst, dlen, dtype;
7440 tree src, slen, stype;
7441 tree tmp;
7443 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
7444 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
7446 dtype = TREE_TYPE (dst);
7447 if (POINTER_TYPE_P (dtype))
7448 dtype = TREE_TYPE (dtype);
7449 stype = TREE_TYPE (src);
7450 if (POINTER_TYPE_P (stype))
7451 stype = TREE_TYPE (stype);
7453 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
7454 return NULL_TREE;
7456 /* Determine the lengths of the arrays. */
7457 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
7458 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
7459 return NULL_TREE;
7460 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
7461 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7462 dlen, fold_convert (gfc_array_index_type, tmp));
7464 slen = GFC_TYPE_ARRAY_SIZE (stype);
7465 if (!slen || TREE_CODE (slen) != INTEGER_CST)
7466 return NULL_TREE;
7467 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
7468 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7469 slen, fold_convert (gfc_array_index_type, tmp));
7471 /* Sanity check that they are the same. This should always be
7472 the case, as we should already have checked for conformance. */
7473 if (!tree_int_cst_equal (slen, dlen))
7474 return NULL_TREE;
7476 return gfc_build_memcpy_call (dst, src, dlen);
7480 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
7481 this can't be done. EXPR1 is the destination/lhs for which
7482 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
7484 static tree
7485 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
7487 unsigned HOST_WIDE_INT nelem;
7488 tree dst, dtype;
7489 tree src, stype;
7490 tree len;
7491 tree tmp;
7493 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
7494 if (nelem == 0)
7495 return NULL_TREE;
7497 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
7498 dtype = TREE_TYPE (dst);
7499 if (POINTER_TYPE_P (dtype))
7500 dtype = TREE_TYPE (dtype);
7501 if (!GFC_ARRAY_TYPE_P (dtype))
7502 return NULL_TREE;
7504 /* Determine the lengths of the array. */
7505 len = GFC_TYPE_ARRAY_SIZE (dtype);
7506 if (!len || TREE_CODE (len) != INTEGER_CST)
7507 return NULL_TREE;
7509 /* Confirm that the constructor is the same size. */
7510 if (compare_tree_int (len, nelem) != 0)
7511 return NULL_TREE;
7513 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
7514 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
7515 fold_convert (gfc_array_index_type, tmp));
7517 stype = gfc_typenode_for_spec (&expr2->ts);
7518 src = gfc_build_constant_array_constructor (expr2, stype);
7520 stype = TREE_TYPE (src);
7521 if (POINTER_TYPE_P (stype))
7522 stype = TREE_TYPE (stype);
7524 return gfc_build_memcpy_call (dst, src, len);
7528 /* Tells whether the expression is to be treated as a variable reference. */
7530 static bool
7531 expr_is_variable (gfc_expr *expr)
7533 gfc_expr *arg;
7534 gfc_component *comp;
7535 gfc_symbol *func_ifc;
7537 if (expr->expr_type == EXPR_VARIABLE)
7538 return true;
7540 arg = gfc_get_noncopying_intrinsic_argument (expr);
7541 if (arg)
7543 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
7544 return expr_is_variable (arg);
7547 /* A data-pointer-returning function should be considered as a variable
7548 too. */
7549 if (expr->expr_type == EXPR_FUNCTION
7550 && expr->ref == NULL)
7552 if (expr->value.function.isym != NULL)
7553 return false;
7555 if (expr->value.function.esym != NULL)
7557 func_ifc = expr->value.function.esym;
7558 goto found_ifc;
7560 else
7562 gcc_assert (expr->symtree);
7563 func_ifc = expr->symtree->n.sym;
7564 goto found_ifc;
7567 gcc_unreachable ();
7570 comp = gfc_get_proc_ptr_comp (expr);
7571 if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
7572 && comp)
7574 func_ifc = comp->ts.interface;
7575 goto found_ifc;
7578 if (expr->expr_type == EXPR_COMPCALL)
7580 gcc_assert (!expr->value.compcall.tbp->is_generic);
7581 func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
7582 goto found_ifc;
7585 return false;
7587 found_ifc:
7588 gcc_assert (func_ifc->attr.function
7589 && func_ifc->result != NULL);
7590 return func_ifc->result->attr.pointer;
7594 /* Is the lhs OK for automatic reallocation? */
7596 static bool
7597 is_scalar_reallocatable_lhs (gfc_expr *expr)
7599 gfc_ref * ref;
7601 /* An allocatable variable with no reference. */
7602 if (expr->symtree->n.sym->attr.allocatable
7603 && !expr->ref)
7604 return true;
7606 /* All that can be left are allocatable components. */
7607 if ((expr->symtree->n.sym->ts.type != BT_DERIVED
7608 && expr->symtree->n.sym->ts.type != BT_CLASS)
7609 || !expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
7610 return false;
7612 /* Find an allocatable component ref last. */
7613 for (ref = expr->ref; ref; ref = ref->next)
7614 if (ref->type == REF_COMPONENT
7615 && !ref->next
7616 && ref->u.c.component->attr.allocatable)
7617 return true;
7619 return false;
7623 /* Allocate or reallocate scalar lhs, as necessary. */
7625 static void
7626 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
7627 tree string_length,
7628 gfc_expr *expr1,
7629 gfc_expr *expr2)
7632 tree cond;
7633 tree tmp;
7634 tree size;
7635 tree size_in_bytes;
7636 tree jump_label1;
7637 tree jump_label2;
7638 gfc_se lse;
7640 if (!expr1 || expr1->rank)
7641 return;
7643 if (!expr2 || expr2->rank)
7644 return;
7646 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
7648 /* Since this is a scalar lhs, we can afford to do this. That is,
7649 there is no risk of side effects being repeated. */
7650 gfc_init_se (&lse, NULL);
7651 lse.want_pointer = 1;
7652 gfc_conv_expr (&lse, expr1);
7654 jump_label1 = gfc_build_label_decl (NULL_TREE);
7655 jump_label2 = gfc_build_label_decl (NULL_TREE);
7657 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
7658 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
7659 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7660 lse.expr, tmp);
7661 tmp = build3_v (COND_EXPR, cond,
7662 build1_v (GOTO_EXPR, jump_label1),
7663 build_empty_stmt (input_location));
7664 gfc_add_expr_to_block (block, tmp);
7666 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
7668 /* Use the rhs string length and the lhs element size. */
7669 size = string_length;
7670 tmp = TREE_TYPE (gfc_typenode_for_spec (&expr1->ts));
7671 tmp = TYPE_SIZE_UNIT (tmp);
7672 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
7673 TREE_TYPE (tmp), tmp,
7674 fold_convert (TREE_TYPE (tmp), size));
7676 else
7678 /* Otherwise use the length in bytes of the rhs. */
7679 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
7680 size_in_bytes = size;
7683 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
7684 size_in_bytes, size_one_node);
7686 if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
7688 tmp = build_call_expr_loc (input_location,
7689 builtin_decl_explicit (BUILT_IN_CALLOC),
7690 2, build_one_cst (size_type_node),
7691 size_in_bytes);
7692 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
7693 gfc_add_modify (block, lse.expr, tmp);
7695 else
7697 tmp = build_call_expr_loc (input_location,
7698 builtin_decl_explicit (BUILT_IN_MALLOC),
7699 1, size_in_bytes);
7700 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
7701 gfc_add_modify (block, lse.expr, tmp);
7704 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
7706 /* Deferred characters need checking for lhs and rhs string
7707 length. Other deferred parameter variables will have to
7708 come here too. */
7709 tmp = build1_v (GOTO_EXPR, jump_label2);
7710 gfc_add_expr_to_block (block, tmp);
7712 tmp = build1_v (LABEL_EXPR, jump_label1);
7713 gfc_add_expr_to_block (block, tmp);
7715 /* For a deferred length character, reallocate if lengths of lhs and
7716 rhs are different. */
7717 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
7719 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
7720 expr1->ts.u.cl->backend_decl, size);
7721 /* Jump past the realloc if the lengths are the same. */
7722 tmp = build3_v (COND_EXPR, cond,
7723 build1_v (GOTO_EXPR, jump_label2),
7724 build_empty_stmt (input_location));
7725 gfc_add_expr_to_block (block, tmp);
7726 tmp = build_call_expr_loc (input_location,
7727 builtin_decl_explicit (BUILT_IN_REALLOC),
7728 2, fold_convert (pvoid_type_node, lse.expr),
7729 size_in_bytes);
7730 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
7731 gfc_add_modify (block, lse.expr, tmp);
7732 tmp = build1_v (LABEL_EXPR, jump_label2);
7733 gfc_add_expr_to_block (block, tmp);
7735 /* Update the lhs character length. */
7736 size = string_length;
7737 gfc_add_modify (block, expr1->ts.u.cl->backend_decl, size);
7741 /* Check for assignments of the type
7743 a = a + 4
7745 to make sure we do not check for reallocation unneccessarily. */
7748 static bool
7749 is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
7751 gfc_actual_arglist *a;
7752 gfc_expr *e1, *e2;
7754 switch (expr2->expr_type)
7756 case EXPR_VARIABLE:
7757 return gfc_dep_compare_expr (expr1, expr2) == 0;
7759 case EXPR_FUNCTION:
7760 if (expr2->value.function.esym
7761 && expr2->value.function.esym->attr.elemental)
7763 for (a = expr2->value.function.actual; a != NULL; a = a->next)
7765 e1 = a->expr;
7766 if (e1->rank > 0 && !is_runtime_conformable (expr1, e1))
7767 return false;
7769 return true;
7771 else if (expr2->value.function.isym
7772 && expr2->value.function.isym->elemental)
7774 for (a = expr2->value.function.actual; a != NULL; a = a->next)
7776 e1 = a->expr;
7777 if (e1->rank > 0 && !is_runtime_conformable (expr1, e1))
7778 return false;
7780 return true;
7783 break;
7785 case EXPR_OP:
7786 switch (expr2->value.op.op)
7788 case INTRINSIC_NOT:
7789 case INTRINSIC_UPLUS:
7790 case INTRINSIC_UMINUS:
7791 case INTRINSIC_PARENTHESES:
7792 return is_runtime_conformable (expr1, expr2->value.op.op1);
7794 case INTRINSIC_PLUS:
7795 case INTRINSIC_MINUS:
7796 case INTRINSIC_TIMES:
7797 case INTRINSIC_DIVIDE:
7798 case INTRINSIC_POWER:
7799 case INTRINSIC_AND:
7800 case INTRINSIC_OR:
7801 case INTRINSIC_EQV:
7802 case INTRINSIC_NEQV:
7803 case INTRINSIC_EQ:
7804 case INTRINSIC_NE:
7805 case INTRINSIC_GT:
7806 case INTRINSIC_GE:
7807 case INTRINSIC_LT:
7808 case INTRINSIC_LE:
7809 case INTRINSIC_EQ_OS:
7810 case INTRINSIC_NE_OS:
7811 case INTRINSIC_GT_OS:
7812 case INTRINSIC_GE_OS:
7813 case INTRINSIC_LT_OS:
7814 case INTRINSIC_LE_OS:
7816 e1 = expr2->value.op.op1;
7817 e2 = expr2->value.op.op2;
7819 if (e1->rank == 0 && e2->rank > 0)
7820 return is_runtime_conformable (expr1, e2);
7821 else if (e1->rank > 0 && e2->rank == 0)
7822 return is_runtime_conformable (expr1, e1);
7823 else if (e1->rank > 0 && e2->rank > 0)
7824 return is_runtime_conformable (expr1, e1)
7825 && is_runtime_conformable (expr1, e2);
7826 break;
7828 default:
7829 break;
7833 break;
7835 default:
7836 break;
7838 return false;
7841 /* Subroutine of gfc_trans_assignment that actually scalarizes the
7842 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
7843 init_flag indicates initialization expressions and dealloc that no
7844 deallocate prior assignment is needed (if in doubt, set true). */
7846 static tree
7847 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
7848 bool dealloc)
7850 gfc_se lse;
7851 gfc_se rse;
7852 gfc_ss *lss;
7853 gfc_ss *lss_section;
7854 gfc_ss *rss;
7855 gfc_loopinfo loop;
7856 tree tmp;
7857 stmtblock_t block;
7858 stmtblock_t body;
7859 bool l_is_temp;
7860 bool scalar_to_array;
7861 tree string_length;
7862 int n;
7864 /* Assignment of the form lhs = rhs. */
7865 gfc_start_block (&block);
7867 gfc_init_se (&lse, NULL);
7868 gfc_init_se (&rse, NULL);
7870 /* Walk the lhs. */
7871 lss = gfc_walk_expr (expr1);
7872 if (gfc_is_reallocatable_lhs (expr1)
7873 && !(expr2->expr_type == EXPR_FUNCTION
7874 && expr2->value.function.isym != NULL))
7875 lss->is_alloc_lhs = 1;
7876 rss = NULL;
7877 if (lss != gfc_ss_terminator)
7879 /* The assignment needs scalarization. */
7880 lss_section = lss;
7882 /* Find a non-scalar SS from the lhs. */
7883 while (lss_section != gfc_ss_terminator
7884 && lss_section->info->type != GFC_SS_SECTION)
7885 lss_section = lss_section->next;
7887 gcc_assert (lss_section != gfc_ss_terminator);
7889 /* Initialize the scalarizer. */
7890 gfc_init_loopinfo (&loop);
7892 /* Walk the rhs. */
7893 rss = gfc_walk_expr (expr2);
7894 if (rss == gfc_ss_terminator)
7895 /* The rhs is scalar. Add a ss for the expression. */
7896 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
7898 /* Associate the SS with the loop. */
7899 gfc_add_ss_to_loop (&loop, lss);
7900 gfc_add_ss_to_loop (&loop, rss);
7902 /* Calculate the bounds of the scalarization. */
7903 gfc_conv_ss_startstride (&loop);
7904 /* Enable loop reversal. */
7905 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
7906 loop.reverse[n] = GFC_ENABLE_REVERSE;
7907 /* Resolve any data dependencies in the statement. */
7908 gfc_conv_resolve_dependencies (&loop, lss, rss);
7909 /* Setup the scalarizing loops. */
7910 gfc_conv_loop_setup (&loop, &expr2->where);
7912 /* Setup the gfc_se structures. */
7913 gfc_copy_loopinfo_to_se (&lse, &loop);
7914 gfc_copy_loopinfo_to_se (&rse, &loop);
7916 rse.ss = rss;
7917 gfc_mark_ss_chain_used (rss, 1);
7918 if (loop.temp_ss == NULL)
7920 lse.ss = lss;
7921 gfc_mark_ss_chain_used (lss, 1);
7923 else
7925 lse.ss = loop.temp_ss;
7926 gfc_mark_ss_chain_used (lss, 3);
7927 gfc_mark_ss_chain_used (loop.temp_ss, 3);
7930 /* Allow the scalarizer to workshare array assignments. */
7931 if ((ompws_flags & OMPWS_WORKSHARE_FLAG) && loop.temp_ss == NULL)
7932 ompws_flags |= OMPWS_SCALARIZER_WS;
7934 /* Start the scalarized loop body. */
7935 gfc_start_scalarized_body (&loop, &body);
7937 else
7938 gfc_init_block (&body);
7940 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
7942 /* Translate the expression. */
7943 gfc_conv_expr (&rse, expr2);
7945 /* Stabilize a string length for temporaries. */
7946 if (expr2->ts.type == BT_CHARACTER)
7947 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
7948 else
7949 string_length = NULL_TREE;
7951 if (l_is_temp)
7953 gfc_conv_tmp_array_ref (&lse);
7954 if (expr2->ts.type == BT_CHARACTER)
7955 lse.string_length = string_length;
7957 else
7958 gfc_conv_expr (&lse, expr1);
7960 /* Assignments of scalar derived types with allocatable components
7961 to arrays must be done with a deep copy and the rhs temporary
7962 must have its components deallocated afterwards. */
7963 scalar_to_array = (expr2->ts.type == BT_DERIVED
7964 && expr2->ts.u.derived->attr.alloc_comp
7965 && !expr_is_variable (expr2)
7966 && !gfc_is_constant_expr (expr2)
7967 && expr1->rank && !expr2->rank);
7968 if (scalar_to_array && dealloc)
7970 tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
7971 gfc_add_expr_to_block (&loop.post, tmp);
7974 /* When assigning a character function result to a deferred-length variable,
7975 the function call must happen before the (re)allocation of the lhs -
7976 otherwise the character length of the result is not known.
7977 NOTE: This relies on having the exact dependence of the length type
7978 parameter available to the caller; gfortran saves it in the .mod files. */
7979 if (gfc_option.flag_realloc_lhs && expr2->ts.type == BT_CHARACTER
7980 && expr1->ts.deferred)
7981 gfc_add_block_to_block (&block, &rse.pre);
7983 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
7984 l_is_temp || init_flag,
7985 expr_is_variable (expr2) || scalar_to_array
7986 || expr2->expr_type == EXPR_ARRAY, dealloc);
7987 gfc_add_expr_to_block (&body, tmp);
7989 if (lss == gfc_ss_terminator)
7991 /* F2003: Add the code for reallocation on assignment. */
7992 if (gfc_option.flag_realloc_lhs
7993 && is_scalar_reallocatable_lhs (expr1))
7994 alloc_scalar_allocatable_for_assignment (&block, rse.string_length,
7995 expr1, expr2);
7997 /* Use the scalar assignment as is. */
7998 gfc_add_block_to_block (&block, &body);
8000 else
8002 gcc_assert (lse.ss == gfc_ss_terminator
8003 && rse.ss == gfc_ss_terminator);
8005 if (l_is_temp)
8007 gfc_trans_scalarized_loop_boundary (&loop, &body);
8009 /* We need to copy the temporary to the actual lhs. */
8010 gfc_init_se (&lse, NULL);
8011 gfc_init_se (&rse, NULL);
8012 gfc_copy_loopinfo_to_se (&lse, &loop);
8013 gfc_copy_loopinfo_to_se (&rse, &loop);
8015 rse.ss = loop.temp_ss;
8016 lse.ss = lss;
8018 gfc_conv_tmp_array_ref (&rse);
8019 gfc_conv_expr (&lse, expr1);
8021 gcc_assert (lse.ss == gfc_ss_terminator
8022 && rse.ss == gfc_ss_terminator);
8024 if (expr2->ts.type == BT_CHARACTER)
8025 rse.string_length = string_length;
8027 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
8028 false, false, dealloc);
8029 gfc_add_expr_to_block (&body, tmp);
8032 /* F2003: Allocate or reallocate lhs of allocatable array. */
8033 if (gfc_option.flag_realloc_lhs
8034 && gfc_is_reallocatable_lhs (expr1)
8035 && !gfc_expr_attr (expr1).codimension
8036 && !gfc_is_coindexed (expr1)
8037 && expr2->rank
8038 && !is_runtime_conformable (expr1, expr2))
8040 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
8041 ompws_flags &= ~OMPWS_SCALARIZER_WS;
8042 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
8043 if (tmp != NULL_TREE)
8044 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
8047 /* Generate the copying loops. */
8048 gfc_trans_scalarizing_loops (&loop, &body);
8050 /* Wrap the whole thing up. */
8051 gfc_add_block_to_block (&block, &loop.pre);
8052 gfc_add_block_to_block (&block, &loop.post);
8054 gfc_cleanup_loop (&loop);
8057 return gfc_finish_block (&block);
8061 /* Check whether EXPR is a copyable array. */
8063 static bool
8064 copyable_array_p (gfc_expr * expr)
8066 if (expr->expr_type != EXPR_VARIABLE)
8067 return false;
8069 /* First check it's an array. */
8070 if (expr->rank < 1 || !expr->ref || expr->ref->next)
8071 return false;
8073 if (!gfc_full_array_ref_p (expr->ref, NULL))
8074 return false;
8076 /* Next check that it's of a simple enough type. */
8077 switch (expr->ts.type)
8079 case BT_INTEGER:
8080 case BT_REAL:
8081 case BT_COMPLEX:
8082 case BT_LOGICAL:
8083 return true;
8085 case BT_CHARACTER:
8086 return false;
8088 case BT_DERIVED:
8089 return !expr->ts.u.derived->attr.alloc_comp;
8091 default:
8092 break;
8095 return false;
8098 /* Translate an assignment. */
8100 tree
8101 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
8102 bool dealloc)
8104 tree tmp;
8106 /* Special case a single function returning an array. */
8107 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
8109 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
8110 if (tmp)
8111 return tmp;
8114 /* Special case assigning an array to zero. */
8115 if (copyable_array_p (expr1)
8116 && is_zero_initializer_p (expr2))
8118 tmp = gfc_trans_zero_assign (expr1);
8119 if (tmp)
8120 return tmp;
8123 /* Special case copying one array to another. */
8124 if (copyable_array_p (expr1)
8125 && copyable_array_p (expr2)
8126 && gfc_compare_types (&expr1->ts, &expr2->ts)
8127 && !gfc_check_dependency (expr1, expr2, 0))
8129 tmp = gfc_trans_array_copy (expr1, expr2);
8130 if (tmp)
8131 return tmp;
8134 /* Special case initializing an array from a constant array constructor. */
8135 if (copyable_array_p (expr1)
8136 && expr2->expr_type == EXPR_ARRAY
8137 && gfc_compare_types (&expr1->ts, &expr2->ts))
8139 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
8140 if (tmp)
8141 return tmp;
8144 /* Fallback to the scalarizer to generate explicit loops. */
8145 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc);
8148 tree
8149 gfc_trans_init_assign (gfc_code * code)
8151 return gfc_trans_assignment (code->expr1, code->expr2, true, false);
8154 tree
8155 gfc_trans_assign (gfc_code * code)
8157 return gfc_trans_assignment (code->expr1, code->expr2, false, true);