2014-07-12 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / trans-expr.c
blob81f213711775d83a57812c3581b7e8f35e96090b
1 /* Expression translation
2 Copyright (C) 2002-2014 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 "stringpool.h"
29 #include "diagnostic-core.h" /* For fatal_error. */
30 #include "langhooks.h"
31 #include "flags.h"
32 #include "gfortran.h"
33 #include "arith.h"
34 #include "constructor.h"
35 #include "trans.h"
36 #include "trans-const.h"
37 #include "trans-types.h"
38 #include "trans-array.h"
39 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
40 #include "trans-stmt.h"
41 #include "dependency.h"
42 #include "gimplify.h"
43 #include "wide-int.h"
45 /* Convert a scalar to an array descriptor. To be used for assumed-rank
46 arrays. */
48 static tree
49 get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
51 enum gfc_array_kind akind;
53 if (attr.pointer)
54 akind = GFC_ARRAY_POINTER_CONT;
55 else if (attr.allocatable)
56 akind = GFC_ARRAY_ALLOCATABLE;
57 else
58 akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
60 if (POINTER_TYPE_P (TREE_TYPE (scalar)))
61 scalar = TREE_TYPE (scalar);
62 return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, 0, NULL, NULL, 1,
63 akind, !(attr.pointer || attr.target));
66 tree
67 gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
69 tree desc, type;
71 type = get_scalar_to_descriptor_type (scalar, attr);
72 desc = gfc_create_var (type, "desc");
73 DECL_ARTIFICIAL (desc) = 1;
75 if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
76 scalar = gfc_build_addr_expr (NULL_TREE, scalar);
77 gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
78 gfc_get_dtype (type));
79 gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
81 /* Copy pointer address back - but only if it could have changed and
82 if the actual argument is a pointer and not, e.g., NULL(). */
83 if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
84 gfc_add_modify (&se->post, scalar,
85 fold_convert (TREE_TYPE (scalar),
86 gfc_conv_descriptor_data_get (desc)));
87 return desc;
91 /* This is the seed for an eventual trans-class.c
93 The following parameters should not be used directly since they might
94 in future implementations. Use the corresponding APIs. */
95 #define CLASS_DATA_FIELD 0
96 #define CLASS_VPTR_FIELD 1
97 #define VTABLE_HASH_FIELD 0
98 #define VTABLE_SIZE_FIELD 1
99 #define VTABLE_EXTENDS_FIELD 2
100 #define VTABLE_DEF_INIT_FIELD 3
101 #define VTABLE_COPY_FIELD 4
102 #define VTABLE_FINAL_FIELD 5
105 tree
106 gfc_class_set_static_fields (tree decl, tree vptr, tree data)
108 tree tmp;
109 tree field;
110 vec<constructor_elt, va_gc> *init = NULL;
112 field = TYPE_FIELDS (TREE_TYPE (decl));
113 tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
114 CONSTRUCTOR_APPEND_ELT (init, tmp, data);
116 tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
117 CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
119 return build_constructor (TREE_TYPE (decl), init);
123 tree
124 gfc_class_data_get (tree decl)
126 tree data;
127 if (POINTER_TYPE_P (TREE_TYPE (decl)))
128 decl = build_fold_indirect_ref_loc (input_location, decl);
129 data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
130 CLASS_DATA_FIELD);
131 return fold_build3_loc (input_location, COMPONENT_REF,
132 TREE_TYPE (data), decl, data,
133 NULL_TREE);
137 tree
138 gfc_class_vptr_get (tree decl)
140 tree vptr;
141 if (POINTER_TYPE_P (TREE_TYPE (decl)))
142 decl = build_fold_indirect_ref_loc (input_location, decl);
143 vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
144 CLASS_VPTR_FIELD);
145 return fold_build3_loc (input_location, COMPONENT_REF,
146 TREE_TYPE (vptr), decl, vptr,
147 NULL_TREE);
151 static tree
152 gfc_vtable_field_get (tree decl, int field)
154 tree size;
155 tree vptr;
156 vptr = gfc_class_vptr_get (decl);
157 vptr = build_fold_indirect_ref_loc (input_location, vptr);
158 size = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
159 field);
160 size = fold_build3_loc (input_location, COMPONENT_REF,
161 TREE_TYPE (size), vptr, size,
162 NULL_TREE);
163 /* Always return size as an array index type. */
164 if (field == VTABLE_SIZE_FIELD)
165 size = fold_convert (gfc_array_index_type, size);
166 gcc_assert (size);
167 return size;
171 tree
172 gfc_vtable_hash_get (tree decl)
174 return gfc_vtable_field_get (decl, VTABLE_HASH_FIELD);
178 tree
179 gfc_vtable_size_get (tree decl)
181 return gfc_vtable_field_get (decl, VTABLE_SIZE_FIELD);
185 tree
186 gfc_vtable_extends_get (tree decl)
188 return gfc_vtable_field_get (decl, VTABLE_EXTENDS_FIELD);
192 tree
193 gfc_vtable_def_init_get (tree decl)
195 return gfc_vtable_field_get (decl, VTABLE_DEF_INIT_FIELD);
199 tree
200 gfc_vtable_copy_get (tree decl)
202 return gfc_vtable_field_get (decl, VTABLE_COPY_FIELD);
206 tree
207 gfc_vtable_final_get (tree decl)
209 return gfc_vtable_field_get (decl, VTABLE_FINAL_FIELD);
213 #undef CLASS_DATA_FIELD
214 #undef CLASS_VPTR_FIELD
215 #undef VTABLE_HASH_FIELD
216 #undef VTABLE_SIZE_FIELD
217 #undef VTABLE_EXTENDS_FIELD
218 #undef VTABLE_DEF_INIT_FIELD
219 #undef VTABLE_COPY_FIELD
220 #undef VTABLE_FINAL_FIELD
223 /* Reset the vptr to the declared type, e.g. after deallocation. */
225 void
226 gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
228 gfc_expr *rhs, *lhs = gfc_copy_expr (e);
229 gfc_symbol *vtab;
230 tree tmp;
231 gfc_ref *ref;
233 /* If we have a class array, we need go back to the class
234 container. */
235 if (lhs->ref && lhs->ref->next && !lhs->ref->next->next
236 && lhs->ref->next->type == REF_ARRAY
237 && lhs->ref->next->u.ar.type == AR_FULL
238 && lhs->ref->type == REF_COMPONENT
239 && strcmp (lhs->ref->u.c.component->name, "_data") == 0)
241 gfc_free_ref_list (lhs->ref);
242 lhs->ref = NULL;
244 else
245 for (ref = lhs->ref; ref; ref = ref->next)
246 if (ref->next && ref->next->next && !ref->next->next->next
247 && ref->next->next->type == REF_ARRAY
248 && ref->next->next->u.ar.type == AR_FULL
249 && ref->next->type == REF_COMPONENT
250 && strcmp (ref->next->u.c.component->name, "_data") == 0)
252 gfc_free_ref_list (ref->next);
253 ref->next = NULL;
256 gfc_add_vptr_component (lhs);
258 if (UNLIMITED_POLY (e))
259 rhs = gfc_get_null_expr (NULL);
260 else
262 vtab = gfc_find_derived_vtab (e->ts.u.derived);
263 rhs = gfc_lval_expr_from_sym (vtab);
265 tmp = gfc_trans_pointer_assignment (lhs, rhs);
266 gfc_add_expr_to_block (block, tmp);
267 gfc_free_expr (lhs);
268 gfc_free_expr (rhs);
272 /* Obtain the vptr of the last class reference in an expression.
273 Return NULL_TREE if no class reference is found. */
275 tree
276 gfc_get_vptr_from_expr (tree expr)
278 tree tmp;
279 tree type;
281 for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
283 type = TREE_TYPE (tmp);
284 while (type)
286 if (GFC_CLASS_TYPE_P (type))
287 return gfc_class_vptr_get (tmp);
288 if (type != TYPE_CANONICAL (type))
289 type = TYPE_CANONICAL (type);
290 else
291 type = NULL_TREE;
293 if (TREE_CODE (tmp) == VAR_DECL)
294 break;
296 return NULL_TREE;
300 static void
301 class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
302 bool lhs_type)
304 tree tmp, tmp2, type;
306 gfc_conv_descriptor_data_set (block, lhs_desc,
307 gfc_conv_descriptor_data_get (rhs_desc));
308 gfc_conv_descriptor_offset_set (block, lhs_desc,
309 gfc_conv_descriptor_offset_get (rhs_desc));
311 gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
312 gfc_conv_descriptor_dtype (rhs_desc));
314 /* Assign the dimension as range-ref. */
315 tmp = gfc_get_descriptor_dimension (lhs_desc);
316 tmp2 = gfc_get_descriptor_dimension (rhs_desc);
318 type = lhs_type ? TREE_TYPE (tmp) : TREE_TYPE (tmp2);
319 tmp = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp,
320 gfc_index_zero_node, NULL_TREE, NULL_TREE);
321 tmp2 = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp2,
322 gfc_index_zero_node, NULL_TREE, NULL_TREE);
323 gfc_add_modify (block, tmp, tmp2);
327 /* Takes a derived type expression and returns the address of a temporary
328 class object of the 'declared' type. If vptr is not NULL, this is
329 used for the temporary class object.
330 optional_alloc_ptr is false when the dummy is neither allocatable
331 nor a pointer; that's only relevant for the optional handling. */
332 void
333 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
334 gfc_typespec class_ts, tree vptr, bool optional,
335 bool optional_alloc_ptr)
337 gfc_symbol *vtab;
338 tree cond_optional = NULL_TREE;
339 gfc_ss *ss;
340 tree ctree;
341 tree var;
342 tree tmp;
344 /* The derived type needs to be converted to a temporary
345 CLASS object. */
346 tmp = gfc_typenode_for_spec (&class_ts);
347 var = gfc_create_var (tmp, "class");
349 /* Set the vptr. */
350 ctree = gfc_class_vptr_get (var);
352 if (vptr != NULL_TREE)
354 /* Use the dynamic vptr. */
355 tmp = vptr;
357 else
359 /* In this case the vtab corresponds to the derived type and the
360 vptr must point to it. */
361 vtab = gfc_find_derived_vtab (e->ts.u.derived);
362 gcc_assert (vtab);
363 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
365 gfc_add_modify (&parmse->pre, ctree,
366 fold_convert (TREE_TYPE (ctree), tmp));
368 /* Now set the data field. */
369 ctree = gfc_class_data_get (var);
371 if (optional)
372 cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
374 if (parmse->ss && parmse->ss->info->useflags)
376 /* For an array reference in an elemental procedure call we need
377 to retain the ss to provide the scalarized array reference. */
378 gfc_conv_expr_reference (parmse, e);
379 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
380 if (optional)
381 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
382 cond_optional, tmp,
383 fold_convert (TREE_TYPE (tmp), null_pointer_node));
384 gfc_add_modify (&parmse->pre, ctree, tmp);
387 else
389 ss = gfc_walk_expr (e);
390 if (ss == gfc_ss_terminator)
392 parmse->ss = NULL;
393 gfc_conv_expr_reference (parmse, e);
395 /* Scalar to an assumed-rank array. */
396 if (class_ts.u.derived->components->as)
398 tree type;
399 type = get_scalar_to_descriptor_type (parmse->expr,
400 gfc_expr_attr (e));
401 gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
402 gfc_get_dtype (type));
403 if (optional)
404 parmse->expr = build3_loc (input_location, COND_EXPR,
405 TREE_TYPE (parmse->expr),
406 cond_optional, parmse->expr,
407 fold_convert (TREE_TYPE (parmse->expr),
408 null_pointer_node));
409 gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
411 else
413 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
414 if (optional)
415 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
416 cond_optional, tmp,
417 fold_convert (TREE_TYPE (tmp),
418 null_pointer_node));
419 gfc_add_modify (&parmse->pre, ctree, tmp);
422 else
424 stmtblock_t block;
425 gfc_init_block (&block);
427 parmse->ss = ss;
428 gfc_conv_expr_descriptor (parmse, e);
430 if (e->rank != class_ts.u.derived->components->as->rank)
432 gcc_assert (class_ts.u.derived->components->as->type
433 == AS_ASSUMED_RANK);
434 class_array_data_assign (&block, ctree, parmse->expr, false);
436 else
438 if (gfc_expr_attr (e).codimension)
439 parmse->expr = fold_build1_loc (input_location,
440 VIEW_CONVERT_EXPR,
441 TREE_TYPE (ctree),
442 parmse->expr);
443 gfc_add_modify (&block, ctree, parmse->expr);
446 if (optional)
448 tmp = gfc_finish_block (&block);
450 gfc_init_block (&block);
451 gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
453 tmp = build3_v (COND_EXPR, cond_optional, tmp,
454 gfc_finish_block (&block));
455 gfc_add_expr_to_block (&parmse->pre, tmp);
457 else
458 gfc_add_block_to_block (&parmse->pre, &block);
462 /* Pass the address of the class object. */
463 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
465 if (optional && optional_alloc_ptr)
466 parmse->expr = build3_loc (input_location, COND_EXPR,
467 TREE_TYPE (parmse->expr),
468 cond_optional, parmse->expr,
469 fold_convert (TREE_TYPE (parmse->expr),
470 null_pointer_node));
474 /* Create a new class container, which is required as scalar coarrays
475 have an array descriptor while normal scalars haven't. Optionally,
476 NULL pointer checks are added if the argument is OPTIONAL. */
478 static void
479 class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
480 gfc_typespec class_ts, bool optional)
482 tree var, ctree, tmp;
483 stmtblock_t block;
484 gfc_ref *ref;
485 gfc_ref *class_ref;
487 gfc_init_block (&block);
489 class_ref = NULL;
490 for (ref = e->ref; ref; ref = ref->next)
492 if (ref->type == REF_COMPONENT
493 && ref->u.c.component->ts.type == BT_CLASS)
494 class_ref = ref;
497 if (class_ref == NULL
498 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
499 tmp = e->symtree->n.sym->backend_decl;
500 else
502 /* Remove everything after the last class reference, convert the
503 expression and then recover its tailend once more. */
504 gfc_se tmpse;
505 ref = class_ref->next;
506 class_ref->next = NULL;
507 gfc_init_se (&tmpse, NULL);
508 gfc_conv_expr (&tmpse, e);
509 class_ref->next = ref;
510 tmp = tmpse.expr;
513 var = gfc_typenode_for_spec (&class_ts);
514 var = gfc_create_var (var, "class");
516 ctree = gfc_class_vptr_get (var);
517 gfc_add_modify (&block, ctree,
518 fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
520 ctree = gfc_class_data_get (var);
521 tmp = gfc_conv_descriptor_data_get (gfc_class_data_get (tmp));
522 gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
524 /* Pass the address of the class object. */
525 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
527 if (optional)
529 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
530 tree tmp2;
532 tmp = gfc_finish_block (&block);
534 gfc_init_block (&block);
535 tmp2 = gfc_class_data_get (var);
536 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
537 null_pointer_node));
538 tmp2 = gfc_finish_block (&block);
540 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
541 cond, tmp, tmp2);
542 gfc_add_expr_to_block (&parmse->pre, tmp);
544 else
545 gfc_add_block_to_block (&parmse->pre, &block);
549 /* Takes an intrinsic type expression and returns the address of a temporary
550 class object of the 'declared' type. */
551 void
552 gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
553 gfc_typespec class_ts)
555 gfc_symbol *vtab;
556 gfc_ss *ss;
557 tree ctree;
558 tree var;
559 tree tmp;
561 /* The intrinsic type needs to be converted to a temporary
562 CLASS object. */
563 tmp = gfc_typenode_for_spec (&class_ts);
564 var = gfc_create_var (tmp, "class");
566 /* Set the vptr. */
567 ctree = gfc_class_vptr_get (var);
569 vtab = gfc_find_vtab (&e->ts);
570 gcc_assert (vtab);
571 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
572 gfc_add_modify (&parmse->pre, ctree,
573 fold_convert (TREE_TYPE (ctree), tmp));
575 /* Now set the data field. */
576 ctree = gfc_class_data_get (var);
577 if (parmse->ss && parmse->ss->info->useflags)
579 /* For an array reference in an elemental procedure call we need
580 to retain the ss to provide the scalarized array reference. */
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 ss = gfc_walk_expr (e);
588 if (ss == gfc_ss_terminator)
590 parmse->ss = NULL;
591 gfc_conv_expr_reference (parmse, e);
592 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
593 gfc_add_modify (&parmse->pre, ctree, tmp);
595 else
597 parmse->ss = ss;
598 parmse->use_offset = 1;
599 gfc_conv_expr_descriptor (parmse, e);
600 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
604 /* Pass the address of the class object. */
605 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
609 /* Takes a scalarized class array expression and returns the
610 address of a temporary scalar class object of the 'declared'
611 type.
612 OOP-TODO: This could be improved by adding code that branched on
613 the dynamic type being the same as the declared type. In this case
614 the original class expression can be passed directly.
615 optional_alloc_ptr is false when the dummy is neither allocatable
616 nor a pointer; that's relevant for the optional handling.
617 Set copyback to true if class container's _data and _vtab pointers
618 might get modified. */
620 void
621 gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
622 bool elemental, bool copyback, bool optional,
623 bool optional_alloc_ptr)
625 tree ctree;
626 tree var;
627 tree tmp;
628 tree vptr;
629 tree cond = NULL_TREE;
630 gfc_ref *ref;
631 gfc_ref *class_ref;
632 stmtblock_t block;
633 bool full_array = false;
635 gfc_init_block (&block);
637 class_ref = NULL;
638 for (ref = e->ref; ref; ref = ref->next)
640 if (ref->type == REF_COMPONENT
641 && ref->u.c.component->ts.type == BT_CLASS)
642 class_ref = ref;
644 if (ref->next == NULL)
645 break;
648 if ((ref == NULL || class_ref == ref)
649 && (!class_ts.u.derived->components->as
650 || class_ts.u.derived->components->as->rank != -1))
651 return;
653 /* Test for FULL_ARRAY. */
654 if (e->rank == 0 && gfc_expr_attr (e).codimension
655 && gfc_expr_attr (e).dimension)
656 full_array = true;
657 else
658 gfc_is_class_array_ref (e, &full_array);
660 /* The derived type needs to be converted to a temporary
661 CLASS object. */
662 tmp = gfc_typenode_for_spec (&class_ts);
663 var = gfc_create_var (tmp, "class");
665 /* Set the data. */
666 ctree = gfc_class_data_get (var);
667 if (class_ts.u.derived->components->as
668 && e->rank != class_ts.u.derived->components->as->rank)
670 if (e->rank == 0)
672 tree type = get_scalar_to_descriptor_type (parmse->expr,
673 gfc_expr_attr (e));
674 gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
675 gfc_get_dtype (type));
677 tmp = gfc_class_data_get (parmse->expr);
678 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
679 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
681 gfc_conv_descriptor_data_set (&block, ctree, tmp);
683 else
684 class_array_data_assign (&block, ctree, parmse->expr, false);
686 else
688 if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
689 parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
690 TREE_TYPE (ctree), parmse->expr);
691 gfc_add_modify (&block, ctree, parmse->expr);
694 /* Return the data component, except in the case of scalarized array
695 references, where nullification of the cannot occur and so there
696 is no need. */
697 if (!elemental && full_array && copyback)
699 if (class_ts.u.derived->components->as
700 && e->rank != class_ts.u.derived->components->as->rank)
702 if (e->rank == 0)
703 gfc_add_modify (&parmse->post, gfc_class_data_get (parmse->expr),
704 gfc_conv_descriptor_data_get (ctree));
705 else
706 class_array_data_assign (&parmse->post, parmse->expr, ctree, true);
708 else
709 gfc_add_modify (&parmse->post, parmse->expr, ctree);
712 /* Set the vptr. */
713 ctree = gfc_class_vptr_get (var);
715 /* The vptr is the second field of the actual argument.
716 First we have to find the corresponding class reference. */
718 tmp = NULL_TREE;
719 if (class_ref == NULL
720 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
721 tmp = e->symtree->n.sym->backend_decl;
722 else
724 /* Remove everything after the last class reference, convert the
725 expression and then recover its tailend once more. */
726 gfc_se tmpse;
727 ref = class_ref->next;
728 class_ref->next = NULL;
729 gfc_init_se (&tmpse, NULL);
730 gfc_conv_expr (&tmpse, e);
731 class_ref->next = ref;
732 tmp = tmpse.expr;
735 gcc_assert (tmp != NULL_TREE);
737 /* Dereference if needs be. */
738 if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
739 tmp = build_fold_indirect_ref_loc (input_location, tmp);
741 vptr = gfc_class_vptr_get (tmp);
742 gfc_add_modify (&block, ctree,
743 fold_convert (TREE_TYPE (ctree), vptr));
745 /* Return the vptr component, except in the case of scalarized array
746 references, where the dynamic type cannot change. */
747 if (!elemental && full_array && copyback)
748 gfc_add_modify (&parmse->post, vptr,
749 fold_convert (TREE_TYPE (vptr), ctree));
751 if (optional)
753 tree tmp2;
755 cond = gfc_conv_expr_present (e->symtree->n.sym);
756 tmp = gfc_finish_block (&block);
758 if (optional_alloc_ptr)
759 tmp2 = build_empty_stmt (input_location);
760 else
762 gfc_init_block (&block);
764 tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
765 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
766 null_pointer_node));
767 tmp2 = gfc_finish_block (&block);
770 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
771 cond, tmp, tmp2);
772 gfc_add_expr_to_block (&parmse->pre, tmp);
774 else
775 gfc_add_block_to_block (&parmse->pre, &block);
777 /* Pass the address of the class object. */
778 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
780 if (optional && optional_alloc_ptr)
781 parmse->expr = build3_loc (input_location, COND_EXPR,
782 TREE_TYPE (parmse->expr),
783 cond, parmse->expr,
784 fold_convert (TREE_TYPE (parmse->expr),
785 null_pointer_node));
789 /* Given a class array declaration and an index, returns the address
790 of the referenced element. */
792 tree
793 gfc_get_class_array_ref (tree index, tree class_decl)
795 tree data = gfc_class_data_get (class_decl);
796 tree size = gfc_vtable_size_get (class_decl);
797 tree offset = fold_build2_loc (input_location, MULT_EXPR,
798 gfc_array_index_type,
799 index, size);
800 tree ptr;
801 data = gfc_conv_descriptor_data_get (data);
802 ptr = fold_convert (pvoid_type_node, data);
803 ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
804 return fold_convert (TREE_TYPE (data), ptr);
808 /* Copies one class expression to another, assuming that if either
809 'to' or 'from' are arrays they are packed. Should 'from' be
810 NULL_TREE, the initialization expression for 'to' is used, assuming
811 that the _vptr is set. */
813 tree
814 gfc_copy_class_to_class (tree from, tree to, tree nelems)
816 tree fcn;
817 tree fcn_type;
818 tree from_data;
819 tree to_data;
820 tree to_ref;
821 tree from_ref;
822 vec<tree, va_gc> *args;
823 tree tmp;
824 tree index;
825 stmtblock_t loopbody;
826 stmtblock_t body;
827 gfc_loopinfo loop;
829 args = NULL;
831 if (from != NULL_TREE)
832 fcn = gfc_vtable_copy_get (from);
833 else
834 fcn = gfc_vtable_copy_get (to);
836 fcn_type = TREE_TYPE (TREE_TYPE (fcn));
838 if (from != NULL_TREE)
839 from_data = gfc_class_data_get (from);
840 else
841 from_data = gfc_vtable_def_init_get (to);
843 to_data = gfc_class_data_get (to);
845 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
847 gfc_init_block (&body);
848 tmp = fold_build2_loc (input_location, MINUS_EXPR,
849 gfc_array_index_type, nelems,
850 gfc_index_one_node);
851 nelems = gfc_evaluate_now (tmp, &body);
852 index = gfc_create_var (gfc_array_index_type, "S");
854 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data)))
856 from_ref = gfc_get_class_array_ref (index, from);
857 vec_safe_push (args, from_ref);
859 else
860 vec_safe_push (args, from_data);
862 to_ref = gfc_get_class_array_ref (index, to);
863 vec_safe_push (args, to_ref);
865 tmp = build_call_vec (fcn_type, fcn, args);
867 /* Build the body of the loop. */
868 gfc_init_block (&loopbody);
869 gfc_add_expr_to_block (&loopbody, tmp);
871 /* Build the loop and return. */
872 gfc_init_loopinfo (&loop);
873 loop.dimen = 1;
874 loop.from[0] = gfc_index_zero_node;
875 loop.loopvar[0] = index;
876 loop.to[0] = nelems;
877 gfc_trans_scalarizing_loops (&loop, &loopbody);
878 gfc_add_block_to_block (&body, &loop.pre);
879 tmp = gfc_finish_block (&body);
880 gfc_cleanup_loop (&loop);
882 else
884 gcc_assert (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data)));
885 vec_safe_push (args, from_data);
886 vec_safe_push (args, to_data);
887 tmp = build_call_vec (fcn_type, fcn, args);
890 return tmp;
893 static tree
894 gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
896 gfc_actual_arglist *actual;
897 gfc_expr *ppc;
898 gfc_code *ppc_code;
899 tree res;
901 actual = gfc_get_actual_arglist ();
902 actual->expr = gfc_copy_expr (rhs);
903 actual->next = gfc_get_actual_arglist ();
904 actual->next->expr = gfc_copy_expr (lhs);
905 ppc = gfc_copy_expr (obj);
906 gfc_add_vptr_component (ppc);
907 gfc_add_component_ref (ppc, "_copy");
908 ppc_code = gfc_get_code (EXEC_CALL);
909 ppc_code->resolved_sym = ppc->symtree->n.sym;
910 /* Although '_copy' is set to be elemental in class.c, it is
911 not staying that way. Find out why, sometime.... */
912 ppc_code->resolved_sym->attr.elemental = 1;
913 ppc_code->ext.actual = actual;
914 ppc_code->expr1 = ppc;
915 /* Since '_copy' is elemental, the scalarizer will take care
916 of arrays in gfc_trans_call. */
917 res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
918 gfc_free_statements (ppc_code);
919 return res;
922 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
923 A MEMCPY is needed to copy the full data from the default initializer
924 of the dynamic type. */
926 tree
927 gfc_trans_class_init_assign (gfc_code *code)
929 stmtblock_t block;
930 tree tmp;
931 gfc_se dst,src,memsz;
932 gfc_expr *lhs, *rhs, *sz;
934 gfc_start_block (&block);
936 lhs = gfc_copy_expr (code->expr1);
937 gfc_add_data_component (lhs);
939 rhs = gfc_copy_expr (code->expr1);
940 gfc_add_vptr_component (rhs);
942 /* Make sure that the component backend_decls have been built, which
943 will not have happened if the derived types concerned have not
944 been referenced. */
945 gfc_get_derived_type (rhs->ts.u.derived);
946 gfc_add_def_init_component (rhs);
948 if (code->expr1->ts.type == BT_CLASS
949 && CLASS_DATA (code->expr1)->attr.dimension)
950 tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
951 else
953 sz = gfc_copy_expr (code->expr1);
954 gfc_add_vptr_component (sz);
955 gfc_add_size_component (sz);
957 gfc_init_se (&dst, NULL);
958 gfc_init_se (&src, NULL);
959 gfc_init_se (&memsz, NULL);
960 gfc_conv_expr (&dst, lhs);
961 gfc_conv_expr (&src, rhs);
962 gfc_conv_expr (&memsz, sz);
963 gfc_add_block_to_block (&block, &src.pre);
964 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
966 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
969 if (code->expr1->symtree->n.sym->attr.optional
970 || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master)
972 tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
973 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
974 present, tmp,
975 build_empty_stmt (input_location));
978 gfc_add_expr_to_block (&block, tmp);
980 return gfc_finish_block (&block);
984 /* Translate an assignment to a CLASS object
985 (pointer or ordinary assignment). */
987 tree
988 gfc_trans_class_assign (gfc_expr *expr1, gfc_expr *expr2, gfc_exec_op op)
990 stmtblock_t block;
991 tree tmp;
992 gfc_expr *lhs;
993 gfc_expr *rhs;
994 gfc_ref *ref;
996 gfc_start_block (&block);
998 ref = expr1->ref;
999 while (ref && ref->next)
1000 ref = ref->next;
1002 /* Class valued proc_pointer assignments do not need any further
1003 preparation. */
1004 if (ref && ref->type == REF_COMPONENT
1005 && ref->u.c.component->attr.proc_pointer
1006 && expr2->expr_type == EXPR_VARIABLE
1007 && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE
1008 && op == EXEC_POINTER_ASSIGN)
1009 goto assign;
1011 if (expr2->ts.type != BT_CLASS)
1013 /* Insert an additional assignment which sets the '_vptr' field. */
1014 gfc_symbol *vtab = NULL;
1015 gfc_symtree *st;
1017 lhs = gfc_copy_expr (expr1);
1018 gfc_add_vptr_component (lhs);
1020 if (UNLIMITED_POLY (expr1)
1021 && expr2->expr_type == EXPR_NULL && expr2->ts.type == BT_UNKNOWN)
1023 rhs = gfc_get_null_expr (&expr2->where);
1024 goto assign_vptr;
1027 if (expr2->expr_type == EXPR_NULL)
1028 vtab = gfc_find_vtab (&expr1->ts);
1029 else
1030 vtab = gfc_find_vtab (&expr2->ts);
1031 gcc_assert (vtab);
1033 rhs = gfc_get_expr ();
1034 rhs->expr_type = EXPR_VARIABLE;
1035 gfc_find_sym_tree (vtab->name, vtab->ns, 1, &st);
1036 rhs->symtree = st;
1037 rhs->ts = vtab->ts;
1038 assign_vptr:
1039 tmp = gfc_trans_pointer_assignment (lhs, rhs);
1040 gfc_add_expr_to_block (&block, tmp);
1042 gfc_free_expr (lhs);
1043 gfc_free_expr (rhs);
1045 else if (expr1->ts.type == BT_DERIVED && UNLIMITED_POLY (expr2))
1047 /* F2003:C717 only sequence and bind-C types can come here. */
1048 gcc_assert (expr1->ts.u.derived->attr.sequence
1049 || expr1->ts.u.derived->attr.is_bind_c);
1050 gfc_add_data_component (expr2);
1051 goto assign;
1053 else if (CLASS_DATA (expr2)->attr.dimension && expr2->expr_type != EXPR_FUNCTION)
1055 /* Insert an additional assignment which sets the '_vptr' field. */
1056 lhs = gfc_copy_expr (expr1);
1057 gfc_add_vptr_component (lhs);
1059 rhs = gfc_copy_expr (expr2);
1060 gfc_add_vptr_component (rhs);
1062 tmp = gfc_trans_pointer_assignment (lhs, rhs);
1063 gfc_add_expr_to_block (&block, tmp);
1065 gfc_free_expr (lhs);
1066 gfc_free_expr (rhs);
1069 /* Do the actual CLASS assignment. */
1070 if (expr2->ts.type == BT_CLASS
1071 && !CLASS_DATA (expr2)->attr.dimension)
1072 op = EXEC_ASSIGN;
1073 else if (expr2->expr_type != EXPR_FUNCTION || expr2->ts.type != BT_CLASS
1074 || !CLASS_DATA (expr2)->attr.dimension)
1075 gfc_add_data_component (expr1);
1077 assign:
1079 if (op == EXEC_ASSIGN)
1080 tmp = gfc_trans_assignment (expr1, expr2, false, true);
1081 else if (op == EXEC_POINTER_ASSIGN)
1082 tmp = gfc_trans_pointer_assignment (expr1, expr2);
1083 else
1084 gcc_unreachable();
1086 gfc_add_expr_to_block (&block, tmp);
1088 return gfc_finish_block (&block);
1092 /* End of prototype trans-class.c */
1095 static void
1096 realloc_lhs_warning (bt type, bool array, locus *where)
1098 if (array && type != BT_CLASS && type != BT_DERIVED
1099 && gfc_option.warn_realloc_lhs)
1100 gfc_warning ("Code for reallocating the allocatable array at %L will "
1101 "be added", where);
1102 else if (gfc_option.warn_realloc_lhs_all)
1103 gfc_warning ("Code for reallocating the allocatable variable at %L "
1104 "will be added", where);
1108 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
1109 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
1110 gfc_expr *);
1112 /* Copy the scalarization loop variables. */
1114 static void
1115 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
1117 dest->ss = src->ss;
1118 dest->loop = src->loop;
1122 /* Initialize a simple expression holder.
1124 Care must be taken when multiple se are created with the same parent.
1125 The child se must be kept in sync. The easiest way is to delay creation
1126 of a child se until after after the previous se has been translated. */
1128 void
1129 gfc_init_se (gfc_se * se, gfc_se * parent)
1131 memset (se, 0, sizeof (gfc_se));
1132 gfc_init_block (&se->pre);
1133 gfc_init_block (&se->post);
1135 se->parent = parent;
1137 if (parent)
1138 gfc_copy_se_loopvars (se, parent);
1142 /* Advances to the next SS in the chain. Use this rather than setting
1143 se->ss = se->ss->next because all the parents needs to be kept in sync.
1144 See gfc_init_se. */
1146 void
1147 gfc_advance_se_ss_chain (gfc_se * se)
1149 gfc_se *p;
1150 gfc_ss *ss;
1152 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
1154 p = se;
1155 /* Walk down the parent chain. */
1156 while (p != NULL)
1158 /* Simple consistency check. */
1159 gcc_assert (p->parent == NULL || p->parent->ss == p->ss
1160 || p->parent->ss->nested_ss == p->ss);
1162 /* If we were in a nested loop, the next scalarized expression can be
1163 on the parent ss' next pointer. Thus we should not take the next
1164 pointer blindly, but rather go up one nest level as long as next
1165 is the end of chain. */
1166 ss = p->ss;
1167 while (ss->next == gfc_ss_terminator && ss->parent != NULL)
1168 ss = ss->parent;
1170 p->ss = ss->next;
1172 p = p->parent;
1177 /* Ensures the result of the expression as either a temporary variable
1178 or a constant so that it can be used repeatedly. */
1180 void
1181 gfc_make_safe_expr (gfc_se * se)
1183 tree var;
1185 if (CONSTANT_CLASS_P (se->expr))
1186 return;
1188 /* We need a temporary for this result. */
1189 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
1190 gfc_add_modify (&se->pre, var, se->expr);
1191 se->expr = var;
1195 /* Return an expression which determines if a dummy parameter is present.
1196 Also used for arguments to procedures with multiple entry points. */
1198 tree
1199 gfc_conv_expr_present (gfc_symbol * sym)
1201 tree decl, cond;
1203 gcc_assert (sym->attr.dummy);
1204 decl = gfc_get_symbol_decl (sym);
1206 /* Intrinsic scalars with VALUE attribute which are passed by value
1207 use a hidden argument to denote the present status. */
1208 if (sym->attr.value && sym->ts.type != BT_CHARACTER
1209 && sym->ts.type != BT_CLASS && sym->ts.type != BT_DERIVED
1210 && !sym->attr.dimension)
1212 char name[GFC_MAX_SYMBOL_LEN + 2];
1213 tree tree_name;
1215 gcc_assert (TREE_CODE (decl) == PARM_DECL);
1216 name[0] = '_';
1217 strcpy (&name[1], sym->name);
1218 tree_name = get_identifier (name);
1220 /* Walk function argument list to find hidden arg. */
1221 cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
1222 for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
1223 if (DECL_NAME (cond) == tree_name)
1224 break;
1226 gcc_assert (cond);
1227 return cond;
1230 if (TREE_CODE (decl) != PARM_DECL)
1232 /* Array parameters use a temporary descriptor, we want the real
1233 parameter. */
1234 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
1235 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
1236 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
1239 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
1240 fold_convert (TREE_TYPE (decl), null_pointer_node));
1242 /* Fortran 2008 allows to pass null pointers and non-associated pointers
1243 as actual argument to denote absent dummies. For array descriptors,
1244 we thus also need to check the array descriptor. For BT_CLASS, it
1245 can also occur for scalars and F2003 due to type->class wrapping and
1246 class->class wrapping. Note further that BT_CLASS always uses an
1247 array descriptor for arrays, also for explicit-shape/assumed-size. */
1249 if (!sym->attr.allocatable
1250 && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
1251 || (sym->ts.type == BT_CLASS
1252 && !CLASS_DATA (sym)->attr.allocatable
1253 && !CLASS_DATA (sym)->attr.class_pointer))
1254 && ((gfc_option.allow_std & GFC_STD_F2008) != 0
1255 || sym->ts.type == BT_CLASS))
1257 tree tmp;
1259 if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
1260 || sym->as->type == AS_ASSUMED_RANK
1261 || sym->attr.codimension))
1262 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
1264 tmp = build_fold_indirect_ref_loc (input_location, decl);
1265 if (sym->ts.type == BT_CLASS)
1266 tmp = gfc_class_data_get (tmp);
1267 tmp = gfc_conv_array_data (tmp);
1269 else if (sym->ts.type == BT_CLASS)
1270 tmp = gfc_class_data_get (decl);
1271 else
1272 tmp = NULL_TREE;
1274 if (tmp != NULL_TREE)
1276 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
1277 fold_convert (TREE_TYPE (tmp), null_pointer_node));
1278 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1279 boolean_type_node, cond, tmp);
1283 return cond;
1287 /* Converts a missing, dummy argument into a null or zero. */
1289 void
1290 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
1292 tree present;
1293 tree tmp;
1295 present = gfc_conv_expr_present (arg->symtree->n.sym);
1297 if (kind > 0)
1299 /* Create a temporary and convert it to the correct type. */
1300 tmp = gfc_get_int_type (kind);
1301 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
1302 se->expr));
1304 /* Test for a NULL value. */
1305 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
1306 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
1307 tmp = gfc_evaluate_now (tmp, &se->pre);
1308 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
1310 else
1312 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
1313 present, se->expr,
1314 build_zero_cst (TREE_TYPE (se->expr)));
1315 tmp = gfc_evaluate_now (tmp, &se->pre);
1316 se->expr = tmp;
1319 if (ts.type == BT_CHARACTER)
1321 tmp = build_int_cst (gfc_charlen_type_node, 0);
1322 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
1323 present, se->string_length, tmp);
1324 tmp = gfc_evaluate_now (tmp, &se->pre);
1325 se->string_length = tmp;
1327 return;
1331 /* Get the character length of an expression, looking through gfc_refs
1332 if necessary. */
1334 tree
1335 gfc_get_expr_charlen (gfc_expr *e)
1337 gfc_ref *r;
1338 tree length;
1340 gcc_assert (e->expr_type == EXPR_VARIABLE
1341 && e->ts.type == BT_CHARACTER);
1343 length = NULL; /* To silence compiler warning. */
1345 if (is_subref_array (e) && e->ts.u.cl->length)
1347 gfc_se tmpse;
1348 gfc_init_se (&tmpse, NULL);
1349 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
1350 e->ts.u.cl->backend_decl = tmpse.expr;
1351 return tmpse.expr;
1354 /* First candidate: if the variable is of type CHARACTER, the
1355 expression's length could be the length of the character
1356 variable. */
1357 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
1358 length = e->symtree->n.sym->ts.u.cl->backend_decl;
1360 /* Look through the reference chain for component references. */
1361 for (r = e->ref; r; r = r->next)
1363 switch (r->type)
1365 case REF_COMPONENT:
1366 if (r->u.c.component->ts.type == BT_CHARACTER)
1367 length = r->u.c.component->ts.u.cl->backend_decl;
1368 break;
1370 case REF_ARRAY:
1371 /* Do nothing. */
1372 break;
1374 default:
1375 /* We should never got substring references here. These will be
1376 broken down by the scalarizer. */
1377 gcc_unreachable ();
1378 break;
1382 gcc_assert (length != NULL);
1383 return length;
1387 /* Return for an expression the backend decl of the coarray. */
1389 tree
1390 gfc_get_tree_for_caf_expr (gfc_expr *expr)
1392 tree caf_decl;
1393 bool found;
1394 gfc_ref *ref;
1396 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
1398 caf_decl = expr->symtree->n.sym->backend_decl;
1399 gcc_assert (caf_decl);
1400 if (expr->symtree->n.sym->ts.type == BT_CLASS)
1401 caf_decl = gfc_class_data_get (caf_decl);
1402 if (expr->symtree->n.sym->attr.codimension)
1403 return caf_decl;
1405 /* The following code assumes that the coarray is a component reachable via
1406 only scalar components/variables; the Fortran standard guarantees this. */
1408 for (ref = expr->ref; ref; ref = ref->next)
1409 if (ref->type == REF_COMPONENT)
1411 gfc_component *comp = ref->u.c.component;
1413 if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
1414 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1415 caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
1416 TREE_TYPE (comp->backend_decl), caf_decl,
1417 comp->backend_decl, NULL_TREE);
1418 if (comp->ts.type == BT_CLASS)
1419 caf_decl = gfc_class_data_get (caf_decl);
1420 if (comp->attr.codimension)
1422 found = true;
1423 break;
1426 gcc_assert (found && caf_decl);
1427 return caf_decl;
1431 /* For each character array constructor subexpression without a ts.u.cl->length,
1432 replace it by its first element (if there aren't any elements, the length
1433 should already be set to zero). */
1435 static void
1436 flatten_array_ctors_without_strlen (gfc_expr* e)
1438 gfc_actual_arglist* arg;
1439 gfc_constructor* c;
1441 if (!e)
1442 return;
1444 switch (e->expr_type)
1447 case EXPR_OP:
1448 flatten_array_ctors_without_strlen (e->value.op.op1);
1449 flatten_array_ctors_without_strlen (e->value.op.op2);
1450 break;
1452 case EXPR_COMPCALL:
1453 /* TODO: Implement as with EXPR_FUNCTION when needed. */
1454 gcc_unreachable ();
1456 case EXPR_FUNCTION:
1457 for (arg = e->value.function.actual; arg; arg = arg->next)
1458 flatten_array_ctors_without_strlen (arg->expr);
1459 break;
1461 case EXPR_ARRAY:
1463 /* We've found what we're looking for. */
1464 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
1466 gfc_constructor *c;
1467 gfc_expr* new_expr;
1469 gcc_assert (e->value.constructor);
1471 c = gfc_constructor_first (e->value.constructor);
1472 new_expr = c->expr;
1473 c->expr = NULL;
1475 flatten_array_ctors_without_strlen (new_expr);
1476 gfc_replace_expr (e, new_expr);
1477 break;
1480 /* Otherwise, fall through to handle constructor elements. */
1481 case EXPR_STRUCTURE:
1482 for (c = gfc_constructor_first (e->value.constructor);
1483 c; c = gfc_constructor_next (c))
1484 flatten_array_ctors_without_strlen (c->expr);
1485 break;
1487 default:
1488 break;
1494 /* Generate code to initialize a string length variable. Returns the
1495 value. For array constructors, cl->length might be NULL and in this case,
1496 the first element of the constructor is needed. expr is the original
1497 expression so we can access it but can be NULL if this is not needed. */
1499 void
1500 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
1502 gfc_se se;
1504 gfc_init_se (&se, NULL);
1506 if (!cl->length
1507 && cl->backend_decl
1508 && TREE_CODE (cl->backend_decl) == VAR_DECL)
1509 return;
1511 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
1512 "flatten" array constructors by taking their first element; all elements
1513 should be the same length or a cl->length should be present. */
1514 if (!cl->length)
1516 gfc_expr* expr_flat;
1517 gcc_assert (expr);
1518 expr_flat = gfc_copy_expr (expr);
1519 flatten_array_ctors_without_strlen (expr_flat);
1520 gfc_resolve_expr (expr_flat);
1522 gfc_conv_expr (&se, expr_flat);
1523 gfc_add_block_to_block (pblock, &se.pre);
1524 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
1526 gfc_free_expr (expr_flat);
1527 return;
1530 /* Convert cl->length. */
1532 gcc_assert (cl->length);
1534 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
1535 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
1536 se.expr, build_int_cst (gfc_charlen_type_node, 0));
1537 gfc_add_block_to_block (pblock, &se.pre);
1539 if (cl->backend_decl)
1540 gfc_add_modify (pblock, cl->backend_decl, se.expr);
1541 else
1542 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
1546 static void
1547 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
1548 const char *name, locus *where)
1550 tree tmp;
1551 tree type;
1552 tree fault;
1553 gfc_se start;
1554 gfc_se end;
1555 char *msg;
1556 mpz_t length;
1558 type = gfc_get_character_type (kind, ref->u.ss.length);
1559 type = build_pointer_type (type);
1561 gfc_init_se (&start, se);
1562 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
1563 gfc_add_block_to_block (&se->pre, &start.pre);
1565 if (integer_onep (start.expr))
1566 gfc_conv_string_parameter (se);
1567 else
1569 tmp = start.expr;
1570 STRIP_NOPS (tmp);
1571 /* Avoid multiple evaluation of substring start. */
1572 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
1573 start.expr = gfc_evaluate_now (start.expr, &se->pre);
1575 /* Change the start of the string. */
1576 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
1577 tmp = se->expr;
1578 else
1579 tmp = build_fold_indirect_ref_loc (input_location,
1580 se->expr);
1581 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
1582 se->expr = gfc_build_addr_expr (type, tmp);
1585 /* Length = end + 1 - start. */
1586 gfc_init_se (&end, se);
1587 if (ref->u.ss.end == NULL)
1588 end.expr = se->string_length;
1589 else
1591 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
1592 gfc_add_block_to_block (&se->pre, &end.pre);
1594 tmp = end.expr;
1595 STRIP_NOPS (tmp);
1596 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
1597 end.expr = gfc_evaluate_now (end.expr, &se->pre);
1599 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1601 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
1602 boolean_type_node, start.expr,
1603 end.expr);
1605 /* Check lower bound. */
1606 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
1607 start.expr,
1608 build_int_cst (gfc_charlen_type_node, 1));
1609 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1610 boolean_type_node, nonempty, fault);
1611 if (name)
1612 asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
1613 "is less than one", name);
1614 else
1615 asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
1616 "is less than one");
1617 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
1618 fold_convert (long_integer_type_node,
1619 start.expr));
1620 free (msg);
1622 /* Check upper bound. */
1623 fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
1624 end.expr, se->string_length);
1625 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1626 boolean_type_node, nonempty, fault);
1627 if (name)
1628 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
1629 "exceeds string length (%%ld)", name);
1630 else
1631 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
1632 "exceeds string length (%%ld)");
1633 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
1634 fold_convert (long_integer_type_node, end.expr),
1635 fold_convert (long_integer_type_node,
1636 se->string_length));
1637 free (msg);
1640 /* Try to calculate the length from the start and end expressions. */
1641 if (ref->u.ss.end
1642 && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
1644 int i_len;
1646 i_len = mpz_get_si (length) + 1;
1647 if (i_len < 0)
1648 i_len = 0;
1650 tmp = build_int_cst (gfc_charlen_type_node, i_len);
1651 mpz_clear (length); /* Was initialized by gfc_dep_difference. */
1653 else
1655 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
1656 end.expr, start.expr);
1657 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
1658 build_int_cst (gfc_charlen_type_node, 1), tmp);
1659 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
1660 tmp, build_int_cst (gfc_charlen_type_node, 0));
1663 se->string_length = tmp;
1667 /* Convert a derived type component reference. */
1669 static void
1670 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
1672 gfc_component *c;
1673 tree tmp;
1674 tree decl;
1675 tree field;
1677 c = ref->u.c.component;
1679 gcc_assert (c->backend_decl);
1681 field = c->backend_decl;
1682 gcc_assert (TREE_CODE (field) == FIELD_DECL);
1683 decl = se->expr;
1685 /* Components can correspond to fields of different containing
1686 types, as components are created without context, whereas
1687 a concrete use of a component has the type of decl as context.
1688 So, if the type doesn't match, we search the corresponding
1689 FIELD_DECL in the parent type. To not waste too much time
1690 we cache this result in norestrict_decl. */
1692 if (DECL_FIELD_CONTEXT (field) != TREE_TYPE (decl))
1694 tree f2 = c->norestrict_decl;
1695 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
1696 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
1697 if (TREE_CODE (f2) == FIELD_DECL
1698 && DECL_NAME (f2) == DECL_NAME (field))
1699 break;
1700 gcc_assert (f2);
1701 c->norestrict_decl = f2;
1702 field = f2;
1705 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
1706 decl, field, NULL_TREE);
1708 se->expr = tmp;
1710 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
1712 tmp = c->ts.u.cl->backend_decl;
1713 /* Components must always be constant length. */
1714 gcc_assert (tmp && INTEGER_CST_P (tmp));
1715 se->string_length = tmp;
1718 if (gfc_deferred_strlen (c, &field))
1720 tmp = fold_build3_loc (input_location, COMPONENT_REF,
1721 TREE_TYPE (field),
1722 decl, field, NULL_TREE);
1723 se->string_length = tmp;
1726 if (((c->attr.pointer || c->attr.allocatable)
1727 && (!c->attr.dimension && !c->attr.codimension)
1728 && c->ts.type != BT_CHARACTER)
1729 || c->attr.proc_pointer)
1730 se->expr = build_fold_indirect_ref_loc (input_location,
1731 se->expr);
1735 /* This function deals with component references to components of the
1736 parent type for derived type extensions. */
1737 static void
1738 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
1740 gfc_component *c;
1741 gfc_component *cmp;
1742 gfc_symbol *dt;
1743 gfc_ref parent;
1745 dt = ref->u.c.sym;
1746 c = ref->u.c.component;
1748 /* Return if the component is in the parent type. */
1749 for (cmp = dt->components; cmp; cmp = cmp->next)
1750 if (strcmp (c->name, cmp->name) == 0)
1751 return;
1753 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
1754 parent.type = REF_COMPONENT;
1755 parent.next = NULL;
1756 parent.u.c.sym = dt;
1757 parent.u.c.component = dt->components;
1759 if (dt->backend_decl == NULL)
1760 gfc_get_derived_type (dt);
1762 /* Build the reference and call self. */
1763 gfc_conv_component_ref (se, &parent);
1764 parent.u.c.sym = dt->components->ts.u.derived;
1765 parent.u.c.component = c;
1766 conv_parent_component_references (se, &parent);
1769 /* Return the contents of a variable. Also handles reference/pointer
1770 variables (all Fortran pointer references are implicit). */
1772 static void
1773 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
1775 gfc_ss *ss;
1776 gfc_ref *ref;
1777 gfc_symbol *sym;
1778 tree parent_decl = NULL_TREE;
1779 int parent_flag;
1780 bool return_value;
1781 bool alternate_entry;
1782 bool entry_master;
1784 sym = expr->symtree->n.sym;
1785 ss = se->ss;
1786 if (ss != NULL)
1788 gfc_ss_info *ss_info = ss->info;
1790 /* Check that something hasn't gone horribly wrong. */
1791 gcc_assert (ss != gfc_ss_terminator);
1792 gcc_assert (ss_info->expr == expr);
1794 /* A scalarized term. We already know the descriptor. */
1795 se->expr = ss_info->data.array.descriptor;
1796 se->string_length = ss_info->string_length;
1797 ref = ss_info->data.array.ref;
1798 if (ref)
1799 gcc_assert (ref->type == REF_ARRAY
1800 && ref->u.ar.type != AR_ELEMENT);
1801 else
1802 gfc_conv_tmp_array_ref (se);
1804 else
1806 tree se_expr = NULL_TREE;
1808 se->expr = gfc_get_symbol_decl (sym);
1810 /* Deal with references to a parent results or entries by storing
1811 the current_function_decl and moving to the parent_decl. */
1812 return_value = sym->attr.function && sym->result == sym;
1813 alternate_entry = sym->attr.function && sym->attr.entry
1814 && sym->result == sym;
1815 entry_master = sym->attr.result
1816 && sym->ns->proc_name->attr.entry_master
1817 && !gfc_return_by_reference (sym->ns->proc_name);
1818 if (current_function_decl)
1819 parent_decl = DECL_CONTEXT (current_function_decl);
1821 if ((se->expr == parent_decl && return_value)
1822 || (sym->ns && sym->ns->proc_name
1823 && parent_decl
1824 && sym->ns->proc_name->backend_decl == parent_decl
1825 && (alternate_entry || entry_master)))
1826 parent_flag = 1;
1827 else
1828 parent_flag = 0;
1830 /* Special case for assigning the return value of a function.
1831 Self recursive functions must have an explicit return value. */
1832 if (return_value && (se->expr == current_function_decl || parent_flag))
1833 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
1835 /* Similarly for alternate entry points. */
1836 else if (alternate_entry
1837 && (sym->ns->proc_name->backend_decl == current_function_decl
1838 || parent_flag))
1840 gfc_entry_list *el = NULL;
1842 for (el = sym->ns->entries; el; el = el->next)
1843 if (sym == el->sym)
1845 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
1846 break;
1850 else if (entry_master
1851 && (sym->ns->proc_name->backend_decl == current_function_decl
1852 || parent_flag))
1853 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
1855 if (se_expr)
1856 se->expr = se_expr;
1858 /* Procedure actual arguments. */
1859 else if (sym->attr.flavor == FL_PROCEDURE
1860 && se->expr != current_function_decl)
1862 if (!sym->attr.dummy && !sym->attr.proc_pointer)
1864 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
1865 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
1867 return;
1871 /* Dereference the expression, where needed. Since characters
1872 are entirely different from other types, they are treated
1873 separately. */
1874 if (sym->ts.type == BT_CHARACTER)
1876 /* Dereference character pointer dummy arguments
1877 or results. */
1878 if ((sym->attr.pointer || sym->attr.allocatable)
1879 && (sym->attr.dummy
1880 || sym->attr.function
1881 || sym->attr.result))
1882 se->expr = build_fold_indirect_ref_loc (input_location,
1883 se->expr);
1886 else if (!sym->attr.value)
1888 /* Dereference non-character scalar dummy arguments. */
1889 if (sym->attr.dummy && !sym->attr.dimension
1890 && !(sym->attr.codimension && sym->attr.allocatable))
1891 se->expr = build_fold_indirect_ref_loc (input_location,
1892 se->expr);
1894 /* Dereference scalar hidden result. */
1895 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
1896 && (sym->attr.function || sym->attr.result)
1897 && !sym->attr.dimension && !sym->attr.pointer
1898 && !sym->attr.always_explicit)
1899 se->expr = build_fold_indirect_ref_loc (input_location,
1900 se->expr);
1902 /* Dereference non-character pointer variables.
1903 These must be dummies, results, or scalars. */
1904 if ((sym->attr.pointer || sym->attr.allocatable
1905 || gfc_is_associate_pointer (sym)
1906 || (sym->as && sym->as->type == AS_ASSUMED_RANK))
1907 && (sym->attr.dummy
1908 || sym->attr.function
1909 || sym->attr.result
1910 || (!sym->attr.dimension
1911 && (!sym->attr.codimension || !sym->attr.allocatable))))
1912 se->expr = build_fold_indirect_ref_loc (input_location,
1913 se->expr);
1916 ref = expr->ref;
1919 /* For character variables, also get the length. */
1920 if (sym->ts.type == BT_CHARACTER)
1922 /* If the character length of an entry isn't set, get the length from
1923 the master function instead. */
1924 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
1925 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
1926 else
1927 se->string_length = sym->ts.u.cl->backend_decl;
1928 gcc_assert (se->string_length);
1931 while (ref)
1933 switch (ref->type)
1935 case REF_ARRAY:
1936 /* Return the descriptor if that's what we want and this is an array
1937 section reference. */
1938 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
1939 return;
1940 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
1941 /* Return the descriptor for array pointers and allocations. */
1942 if (se->want_pointer
1943 && ref->next == NULL && (se->descriptor_only))
1944 return;
1946 gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
1947 /* Return a pointer to an element. */
1948 break;
1950 case REF_COMPONENT:
1951 if (ref->u.c.sym->attr.extension)
1952 conv_parent_component_references (se, ref);
1954 gfc_conv_component_ref (se, ref);
1955 if (!ref->next && ref->u.c.sym->attr.codimension
1956 && se->want_pointer && se->descriptor_only)
1957 return;
1959 break;
1961 case REF_SUBSTRING:
1962 gfc_conv_substring (se, ref, expr->ts.kind,
1963 expr->symtree->name, &expr->where);
1964 break;
1966 default:
1967 gcc_unreachable ();
1968 break;
1970 ref = ref->next;
1972 /* Pointer assignment, allocation or pass by reference. Arrays are handled
1973 separately. */
1974 if (se->want_pointer)
1976 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
1977 gfc_conv_string_parameter (se);
1978 else
1979 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
1984 /* Unary ops are easy... Or they would be if ! was a valid op. */
1986 static void
1987 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
1989 gfc_se operand;
1990 tree type;
1992 gcc_assert (expr->ts.type != BT_CHARACTER);
1993 /* Initialize the operand. */
1994 gfc_init_se (&operand, se);
1995 gfc_conv_expr_val (&operand, expr->value.op.op1);
1996 gfc_add_block_to_block (&se->pre, &operand.pre);
1998 type = gfc_typenode_for_spec (&expr->ts);
2000 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
2001 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
2002 All other unary operators have an equivalent GIMPLE unary operator. */
2003 if (code == TRUTH_NOT_EXPR)
2004 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
2005 build_int_cst (type, 0));
2006 else
2007 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
2011 /* Expand power operator to optimal multiplications when a value is raised
2012 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
2013 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
2014 Programming", 3rd Edition, 1998. */
2016 /* This code is mostly duplicated from expand_powi in the backend.
2017 We establish the "optimal power tree" lookup table with the defined size.
2018 The items in the table are the exponents used to calculate the index
2019 exponents. Any integer n less than the value can get an "addition chain",
2020 with the first node being one. */
2021 #define POWI_TABLE_SIZE 256
2023 /* The table is from builtins.c. */
2024 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2026 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2027 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2028 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2029 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2030 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2031 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2032 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2033 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2034 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2035 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2036 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2037 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2038 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2039 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2040 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2041 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2042 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2043 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2044 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2045 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2046 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2047 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2048 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2049 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2050 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2051 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2052 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2053 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2054 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2055 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2056 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2057 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2060 /* If n is larger than lookup table's max index, we use the "window
2061 method". */
2062 #define POWI_WINDOW_SIZE 3
2064 /* Recursive function to expand the power operator. The temporary
2065 values are put in tmpvar. The function returns tmpvar[1] ** n. */
2066 static tree
2067 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
2069 tree op0;
2070 tree op1;
2071 tree tmp;
2072 int digit;
2074 if (n < POWI_TABLE_SIZE)
2076 if (tmpvar[n])
2077 return tmpvar[n];
2079 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
2080 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
2082 else if (n & 1)
2084 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2085 op0 = gfc_conv_powi (se, n - digit, tmpvar);
2086 op1 = gfc_conv_powi (se, digit, tmpvar);
2088 else
2090 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
2091 op1 = op0;
2094 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
2095 tmp = gfc_evaluate_now (tmp, &se->pre);
2097 if (n < POWI_TABLE_SIZE)
2098 tmpvar[n] = tmp;
2100 return tmp;
2104 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
2105 return 1. Else return 0 and a call to runtime library functions
2106 will have to be built. */
2107 static int
2108 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
2110 tree cond;
2111 tree tmp;
2112 tree type;
2113 tree vartmp[POWI_TABLE_SIZE];
2114 HOST_WIDE_INT m;
2115 unsigned HOST_WIDE_INT n;
2116 int sgn;
2117 wide_int wrhs = rhs;
2119 /* If exponent is too large, we won't expand it anyway, so don't bother
2120 with large integer values. */
2121 if (!wi::fits_shwi_p (wrhs))
2122 return 0;
2124 m = wrhs.to_shwi ();
2125 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
2126 of the asymmetric range of the integer type. */
2127 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
2129 type = TREE_TYPE (lhs);
2130 sgn = tree_int_cst_sgn (rhs);
2132 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
2133 || optimize_size) && (m > 2 || m < -1))
2134 return 0;
2136 /* rhs == 0 */
2137 if (sgn == 0)
2139 se->expr = gfc_build_const (type, integer_one_node);
2140 return 1;
2143 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
2144 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
2146 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2147 lhs, build_int_cst (TREE_TYPE (lhs), -1));
2148 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2149 lhs, build_int_cst (TREE_TYPE (lhs), 1));
2151 /* If rhs is even,
2152 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
2153 if ((n & 1) == 0)
2155 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2156 boolean_type_node, tmp, cond);
2157 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2158 tmp, build_int_cst (type, 1),
2159 build_int_cst (type, 0));
2160 return 1;
2162 /* If rhs is odd,
2163 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
2164 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
2165 build_int_cst (type, -1),
2166 build_int_cst (type, 0));
2167 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2168 cond, build_int_cst (type, 1), tmp);
2169 return 1;
2172 memset (vartmp, 0, sizeof (vartmp));
2173 vartmp[1] = lhs;
2174 if (sgn == -1)
2176 tmp = gfc_build_const (type, integer_one_node);
2177 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
2178 vartmp[1]);
2181 se->expr = gfc_conv_powi (se, n, vartmp);
2183 return 1;
2187 /* Power op (**). Constant integer exponent has special handling. */
2189 static void
2190 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
2192 tree gfc_int4_type_node;
2193 int kind;
2194 int ikind;
2195 int res_ikind_1, res_ikind_2;
2196 gfc_se lse;
2197 gfc_se rse;
2198 tree fndecl = NULL;
2200 gfc_init_se (&lse, se);
2201 gfc_conv_expr_val (&lse, expr->value.op.op1);
2202 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
2203 gfc_add_block_to_block (&se->pre, &lse.pre);
2205 gfc_init_se (&rse, se);
2206 gfc_conv_expr_val (&rse, expr->value.op.op2);
2207 gfc_add_block_to_block (&se->pre, &rse.pre);
2209 if (expr->value.op.op2->ts.type == BT_INTEGER
2210 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
2211 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
2212 return;
2214 gfc_int4_type_node = gfc_get_int_type (4);
2216 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
2217 library routine. But in the end, we have to convert the result back
2218 if this case applies -- with res_ikind_K, we keep track whether operand K
2219 falls into this case. */
2220 res_ikind_1 = -1;
2221 res_ikind_2 = -1;
2223 kind = expr->value.op.op1->ts.kind;
2224 switch (expr->value.op.op2->ts.type)
2226 case BT_INTEGER:
2227 ikind = expr->value.op.op2->ts.kind;
2228 switch (ikind)
2230 case 1:
2231 case 2:
2232 rse.expr = convert (gfc_int4_type_node, rse.expr);
2233 res_ikind_2 = ikind;
2234 /* Fall through. */
2236 case 4:
2237 ikind = 0;
2238 break;
2240 case 8:
2241 ikind = 1;
2242 break;
2244 case 16:
2245 ikind = 2;
2246 break;
2248 default:
2249 gcc_unreachable ();
2251 switch (kind)
2253 case 1:
2254 case 2:
2255 if (expr->value.op.op1->ts.type == BT_INTEGER)
2257 lse.expr = convert (gfc_int4_type_node, lse.expr);
2258 res_ikind_1 = kind;
2260 else
2261 gcc_unreachable ();
2262 /* Fall through. */
2264 case 4:
2265 kind = 0;
2266 break;
2268 case 8:
2269 kind = 1;
2270 break;
2272 case 10:
2273 kind = 2;
2274 break;
2276 case 16:
2277 kind = 3;
2278 break;
2280 default:
2281 gcc_unreachable ();
2284 switch (expr->value.op.op1->ts.type)
2286 case BT_INTEGER:
2287 if (kind == 3) /* Case 16 was not handled properly above. */
2288 kind = 2;
2289 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
2290 break;
2292 case BT_REAL:
2293 /* Use builtins for real ** int4. */
2294 if (ikind == 0)
2296 switch (kind)
2298 case 0:
2299 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
2300 break;
2302 case 1:
2303 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
2304 break;
2306 case 2:
2307 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
2308 break;
2310 case 3:
2311 /* Use the __builtin_powil() only if real(kind=16) is
2312 actually the C long double type. */
2313 if (!gfc_real16_is_float128)
2314 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
2315 break;
2317 default:
2318 gcc_unreachable ();
2322 /* If we don't have a good builtin for this, go for the
2323 library function. */
2324 if (!fndecl)
2325 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
2326 break;
2328 case BT_COMPLEX:
2329 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
2330 break;
2332 default:
2333 gcc_unreachable ();
2335 break;
2337 case BT_REAL:
2338 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
2339 break;
2341 case BT_COMPLEX:
2342 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
2343 break;
2345 default:
2346 gcc_unreachable ();
2347 break;
2350 se->expr = build_call_expr_loc (input_location,
2351 fndecl, 2, lse.expr, rse.expr);
2353 /* Convert the result back if it is of wrong integer kind. */
2354 if (res_ikind_1 != -1 && res_ikind_2 != -1)
2356 /* We want the maximum of both operand kinds as result. */
2357 if (res_ikind_1 < res_ikind_2)
2358 res_ikind_1 = res_ikind_2;
2359 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
2364 /* Generate code to allocate a string temporary. */
2366 tree
2367 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
2369 tree var;
2370 tree tmp;
2372 if (gfc_can_put_var_on_stack (len))
2374 /* Create a temporary variable to hold the result. */
2375 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2376 gfc_charlen_type_node, len,
2377 build_int_cst (gfc_charlen_type_node, 1));
2378 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
2380 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
2381 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
2382 else
2383 tmp = build_array_type (TREE_TYPE (type), tmp);
2385 var = gfc_create_var (tmp, "str");
2386 var = gfc_build_addr_expr (type, var);
2388 else
2390 /* Allocate a temporary to hold the result. */
2391 var = gfc_create_var (type, "pstr");
2392 gcc_assert (POINTER_TYPE_P (type));
2393 tmp = TREE_TYPE (type);
2394 if (TREE_CODE (tmp) == ARRAY_TYPE)
2395 tmp = TREE_TYPE (tmp);
2396 tmp = TYPE_SIZE_UNIT (tmp);
2397 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
2398 fold_convert (size_type_node, len),
2399 fold_convert (size_type_node, tmp));
2400 tmp = gfc_call_malloc (&se->pre, type, tmp);
2401 gfc_add_modify (&se->pre, var, tmp);
2403 /* Free the temporary afterwards. */
2404 tmp = gfc_call_free (convert (pvoid_type_node, var));
2405 gfc_add_expr_to_block (&se->post, tmp);
2408 return var;
2412 /* Handle a string concatenation operation. A temporary will be allocated to
2413 hold the result. */
2415 static void
2416 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
2418 gfc_se lse, rse;
2419 tree len, type, var, tmp, fndecl;
2421 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
2422 && expr->value.op.op2->ts.type == BT_CHARACTER);
2423 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
2425 gfc_init_se (&lse, se);
2426 gfc_conv_expr (&lse, expr->value.op.op1);
2427 gfc_conv_string_parameter (&lse);
2428 gfc_init_se (&rse, se);
2429 gfc_conv_expr (&rse, expr->value.op.op2);
2430 gfc_conv_string_parameter (&rse);
2432 gfc_add_block_to_block (&se->pre, &lse.pre);
2433 gfc_add_block_to_block (&se->pre, &rse.pre);
2435 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
2436 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
2437 if (len == NULL_TREE)
2439 len = fold_build2_loc (input_location, PLUS_EXPR,
2440 TREE_TYPE (lse.string_length),
2441 lse.string_length, rse.string_length);
2444 type = build_pointer_type (type);
2446 var = gfc_conv_string_tmp (se, type, len);
2448 /* Do the actual concatenation. */
2449 if (expr->ts.kind == 1)
2450 fndecl = gfor_fndecl_concat_string;
2451 else if (expr->ts.kind == 4)
2452 fndecl = gfor_fndecl_concat_string_char4;
2453 else
2454 gcc_unreachable ();
2456 tmp = build_call_expr_loc (input_location,
2457 fndecl, 6, len, var, lse.string_length, lse.expr,
2458 rse.string_length, rse.expr);
2459 gfc_add_expr_to_block (&se->pre, tmp);
2461 /* Add the cleanup for the operands. */
2462 gfc_add_block_to_block (&se->pre, &rse.post);
2463 gfc_add_block_to_block (&se->pre, &lse.post);
2465 se->expr = var;
2466 se->string_length = len;
2469 /* Translates an op expression. Common (binary) cases are handled by this
2470 function, others are passed on. Recursion is used in either case.
2471 We use the fact that (op1.ts == op2.ts) (except for the power
2472 operator **).
2473 Operators need no special handling for scalarized expressions as long as
2474 they call gfc_conv_simple_val to get their operands.
2475 Character strings get special handling. */
2477 static void
2478 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
2480 enum tree_code code;
2481 gfc_se lse;
2482 gfc_se rse;
2483 tree tmp, type;
2484 int lop;
2485 int checkstring;
2487 checkstring = 0;
2488 lop = 0;
2489 switch (expr->value.op.op)
2491 case INTRINSIC_PARENTHESES:
2492 if ((expr->ts.type == BT_REAL
2493 || expr->ts.type == BT_COMPLEX)
2494 && gfc_option.flag_protect_parens)
2496 gfc_conv_unary_op (PAREN_EXPR, se, expr);
2497 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
2498 return;
2501 /* Fallthrough. */
2502 case INTRINSIC_UPLUS:
2503 gfc_conv_expr (se, expr->value.op.op1);
2504 return;
2506 case INTRINSIC_UMINUS:
2507 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
2508 return;
2510 case INTRINSIC_NOT:
2511 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
2512 return;
2514 case INTRINSIC_PLUS:
2515 code = PLUS_EXPR;
2516 break;
2518 case INTRINSIC_MINUS:
2519 code = MINUS_EXPR;
2520 break;
2522 case INTRINSIC_TIMES:
2523 code = MULT_EXPR;
2524 break;
2526 case INTRINSIC_DIVIDE:
2527 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
2528 an integer, we must round towards zero, so we use a
2529 TRUNC_DIV_EXPR. */
2530 if (expr->ts.type == BT_INTEGER)
2531 code = TRUNC_DIV_EXPR;
2532 else
2533 code = RDIV_EXPR;
2534 break;
2536 case INTRINSIC_POWER:
2537 gfc_conv_power_op (se, expr);
2538 return;
2540 case INTRINSIC_CONCAT:
2541 gfc_conv_concat_op (se, expr);
2542 return;
2544 case INTRINSIC_AND:
2545 code = TRUTH_ANDIF_EXPR;
2546 lop = 1;
2547 break;
2549 case INTRINSIC_OR:
2550 code = TRUTH_ORIF_EXPR;
2551 lop = 1;
2552 break;
2554 /* EQV and NEQV only work on logicals, but since we represent them
2555 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
2556 case INTRINSIC_EQ:
2557 case INTRINSIC_EQ_OS:
2558 case INTRINSIC_EQV:
2559 code = EQ_EXPR;
2560 checkstring = 1;
2561 lop = 1;
2562 break;
2564 case INTRINSIC_NE:
2565 case INTRINSIC_NE_OS:
2566 case INTRINSIC_NEQV:
2567 code = NE_EXPR;
2568 checkstring = 1;
2569 lop = 1;
2570 break;
2572 case INTRINSIC_GT:
2573 case INTRINSIC_GT_OS:
2574 code = GT_EXPR;
2575 checkstring = 1;
2576 lop = 1;
2577 break;
2579 case INTRINSIC_GE:
2580 case INTRINSIC_GE_OS:
2581 code = GE_EXPR;
2582 checkstring = 1;
2583 lop = 1;
2584 break;
2586 case INTRINSIC_LT:
2587 case INTRINSIC_LT_OS:
2588 code = LT_EXPR;
2589 checkstring = 1;
2590 lop = 1;
2591 break;
2593 case INTRINSIC_LE:
2594 case INTRINSIC_LE_OS:
2595 code = LE_EXPR;
2596 checkstring = 1;
2597 lop = 1;
2598 break;
2600 case INTRINSIC_USER:
2601 case INTRINSIC_ASSIGN:
2602 /* These should be converted into function calls by the frontend. */
2603 gcc_unreachable ();
2605 default:
2606 fatal_error ("Unknown intrinsic op");
2607 return;
2610 /* The only exception to this is **, which is handled separately anyway. */
2611 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
2613 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
2614 checkstring = 0;
2616 /* lhs */
2617 gfc_init_se (&lse, se);
2618 gfc_conv_expr (&lse, expr->value.op.op1);
2619 gfc_add_block_to_block (&se->pre, &lse.pre);
2621 /* rhs */
2622 gfc_init_se (&rse, se);
2623 gfc_conv_expr (&rse, expr->value.op.op2);
2624 gfc_add_block_to_block (&se->pre, &rse.pre);
2626 if (checkstring)
2628 gfc_conv_string_parameter (&lse);
2629 gfc_conv_string_parameter (&rse);
2631 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
2632 rse.string_length, rse.expr,
2633 expr->value.op.op1->ts.kind,
2634 code);
2635 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
2636 gfc_add_block_to_block (&lse.post, &rse.post);
2639 type = gfc_typenode_for_spec (&expr->ts);
2641 if (lop)
2643 /* The result of logical ops is always boolean_type_node. */
2644 tmp = fold_build2_loc (input_location, code, boolean_type_node,
2645 lse.expr, rse.expr);
2646 se->expr = convert (type, tmp);
2648 else
2649 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
2651 /* Add the post blocks. */
2652 gfc_add_block_to_block (&se->post, &rse.post);
2653 gfc_add_block_to_block (&se->post, &lse.post);
2656 /* If a string's length is one, we convert it to a single character. */
2658 tree
2659 gfc_string_to_single_character (tree len, tree str, int kind)
2662 if (len == NULL
2663 || !tree_fits_uhwi_p (len)
2664 || !POINTER_TYPE_P (TREE_TYPE (str)))
2665 return NULL_TREE;
2667 if (TREE_INT_CST_LOW (len) == 1)
2669 str = fold_convert (gfc_get_pchar_type (kind), str);
2670 return build_fold_indirect_ref_loc (input_location, str);
2673 if (kind == 1
2674 && TREE_CODE (str) == ADDR_EXPR
2675 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
2676 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
2677 && array_ref_low_bound (TREE_OPERAND (str, 0))
2678 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
2679 && TREE_INT_CST_LOW (len) > 1
2680 && TREE_INT_CST_LOW (len)
2681 == (unsigned HOST_WIDE_INT)
2682 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
2684 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
2685 ret = build_fold_indirect_ref_loc (input_location, ret);
2686 if (TREE_CODE (ret) == INTEGER_CST)
2688 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
2689 int i, length = TREE_STRING_LENGTH (string_cst);
2690 const char *ptr = TREE_STRING_POINTER (string_cst);
2692 for (i = 1; i < length; i++)
2693 if (ptr[i] != ' ')
2694 return NULL_TREE;
2696 return ret;
2700 return NULL_TREE;
2704 void
2705 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
2708 if (sym->backend_decl)
2710 /* This becomes the nominal_type in
2711 function.c:assign_parm_find_data_types. */
2712 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
2713 /* This becomes the passed_type in
2714 function.c:assign_parm_find_data_types. C promotes char to
2715 integer for argument passing. */
2716 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
2718 DECL_BY_REFERENCE (sym->backend_decl) = 0;
2721 if (expr != NULL)
2723 /* If we have a constant character expression, make it into an
2724 integer. */
2725 if ((*expr)->expr_type == EXPR_CONSTANT)
2727 gfc_typespec ts;
2728 gfc_clear_ts (&ts);
2730 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
2731 (int)(*expr)->value.character.string[0]);
2732 if ((*expr)->ts.kind != gfc_c_int_kind)
2734 /* The expr needs to be compatible with a C int. If the
2735 conversion fails, then the 2 causes an ICE. */
2736 ts.type = BT_INTEGER;
2737 ts.kind = gfc_c_int_kind;
2738 gfc_convert_type (*expr, &ts, 2);
2741 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
2743 if ((*expr)->ref == NULL)
2745 se->expr = gfc_string_to_single_character
2746 (build_int_cst (integer_type_node, 1),
2747 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
2748 gfc_get_symbol_decl
2749 ((*expr)->symtree->n.sym)),
2750 (*expr)->ts.kind);
2752 else
2754 gfc_conv_variable (se, *expr);
2755 se->expr = gfc_string_to_single_character
2756 (build_int_cst (integer_type_node, 1),
2757 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
2758 se->expr),
2759 (*expr)->ts.kind);
2765 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
2766 if STR is a string literal, otherwise return -1. */
2768 static int
2769 gfc_optimize_len_trim (tree len, tree str, int kind)
2771 if (kind == 1
2772 && TREE_CODE (str) == ADDR_EXPR
2773 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
2774 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
2775 && array_ref_low_bound (TREE_OPERAND (str, 0))
2776 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
2777 && tree_fits_uhwi_p (len)
2778 && tree_to_uhwi (len) >= 1
2779 && tree_to_uhwi (len)
2780 == (unsigned HOST_WIDE_INT)
2781 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
2783 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
2784 folded = build_fold_indirect_ref_loc (input_location, folded);
2785 if (TREE_CODE (folded) == INTEGER_CST)
2787 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
2788 int length = TREE_STRING_LENGTH (string_cst);
2789 const char *ptr = TREE_STRING_POINTER (string_cst);
2791 for (; length > 0; length--)
2792 if (ptr[length - 1] != ' ')
2793 break;
2795 return length;
2798 return -1;
2801 /* Helper to build a call to memcmp. */
2803 static tree
2804 build_memcmp_call (tree s1, tree s2, tree n)
2806 tree tmp;
2808 if (!POINTER_TYPE_P (TREE_TYPE (s1)))
2809 s1 = gfc_build_addr_expr (pvoid_type_node, s1);
2810 else
2811 s1 = fold_convert (pvoid_type_node, s1);
2813 if (!POINTER_TYPE_P (TREE_TYPE (s2)))
2814 s2 = gfc_build_addr_expr (pvoid_type_node, s2);
2815 else
2816 s2 = fold_convert (pvoid_type_node, s2);
2818 n = fold_convert (size_type_node, n);
2820 tmp = build_call_expr_loc (input_location,
2821 builtin_decl_explicit (BUILT_IN_MEMCMP),
2822 3, s1, s2, n);
2824 return fold_convert (integer_type_node, tmp);
2827 /* Compare two strings. If they are all single characters, the result is the
2828 subtraction of them. Otherwise, we build a library call. */
2830 tree
2831 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
2832 enum tree_code code)
2834 tree sc1;
2835 tree sc2;
2836 tree fndecl;
2838 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
2839 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
2841 sc1 = gfc_string_to_single_character (len1, str1, kind);
2842 sc2 = gfc_string_to_single_character (len2, str2, kind);
2844 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
2846 /* Deal with single character specially. */
2847 sc1 = fold_convert (integer_type_node, sc1);
2848 sc2 = fold_convert (integer_type_node, sc2);
2849 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
2850 sc1, sc2);
2853 if ((code == EQ_EXPR || code == NE_EXPR)
2854 && optimize
2855 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
2857 /* If one string is a string literal with LEN_TRIM longer
2858 than the length of the second string, the strings
2859 compare unequal. */
2860 int len = gfc_optimize_len_trim (len1, str1, kind);
2861 if (len > 0 && compare_tree_int (len2, len) < 0)
2862 return integer_one_node;
2863 len = gfc_optimize_len_trim (len2, str2, kind);
2864 if (len > 0 && compare_tree_int (len1, len) < 0)
2865 return integer_one_node;
2868 /* We can compare via memcpy if the strings are known to be equal
2869 in length and they are
2870 - kind=1
2871 - kind=4 and the comparison is for (in)equality. */
2873 if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
2874 && tree_int_cst_equal (len1, len2)
2875 && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
2877 tree tmp;
2878 tree chartype;
2880 chartype = gfc_get_char_type (kind);
2881 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
2882 fold_convert (TREE_TYPE(len1),
2883 TYPE_SIZE_UNIT(chartype)),
2884 len1);
2885 return build_memcmp_call (str1, str2, tmp);
2888 /* Build a call for the comparison. */
2889 if (kind == 1)
2890 fndecl = gfor_fndecl_compare_string;
2891 else if (kind == 4)
2892 fndecl = gfor_fndecl_compare_string_char4;
2893 else
2894 gcc_unreachable ();
2896 return build_call_expr_loc (input_location, fndecl, 4,
2897 len1, str1, len2, str2);
2901 /* Return the backend_decl for a procedure pointer component. */
2903 static tree
2904 get_proc_ptr_comp (gfc_expr *e)
2906 gfc_se comp_se;
2907 gfc_expr *e2;
2908 expr_t old_type;
2910 gfc_init_se (&comp_se, NULL);
2911 e2 = gfc_copy_expr (e);
2912 /* We have to restore the expr type later so that gfc_free_expr frees
2913 the exact same thing that was allocated.
2914 TODO: This is ugly. */
2915 old_type = e2->expr_type;
2916 e2->expr_type = EXPR_VARIABLE;
2917 gfc_conv_expr (&comp_se, e2);
2918 e2->expr_type = old_type;
2919 gfc_free_expr (e2);
2920 return build_fold_addr_expr_loc (input_location, comp_se.expr);
2924 /* Convert a typebound function reference from a class object. */
2925 static void
2926 conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
2928 gfc_ref *ref;
2929 tree var;
2931 if (TREE_CODE (base_object) != VAR_DECL)
2933 var = gfc_create_var (TREE_TYPE (base_object), NULL);
2934 gfc_add_modify (&se->pre, var, base_object);
2936 se->expr = gfc_class_vptr_get (base_object);
2937 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
2938 ref = expr->ref;
2939 while (ref && ref->next)
2940 ref = ref->next;
2941 gcc_assert (ref && ref->type == REF_COMPONENT);
2942 if (ref->u.c.sym->attr.extension)
2943 conv_parent_component_references (se, ref);
2944 gfc_conv_component_ref (se, ref);
2945 se->expr = build_fold_addr_expr_loc (input_location, se->expr);
2949 static void
2950 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
2952 tree tmp;
2954 if (gfc_is_proc_ptr_comp (expr))
2955 tmp = get_proc_ptr_comp (expr);
2956 else if (sym->attr.dummy)
2958 tmp = gfc_get_symbol_decl (sym);
2959 if (sym->attr.proc_pointer)
2960 tmp = build_fold_indirect_ref_loc (input_location,
2961 tmp);
2962 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
2963 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
2965 else
2967 if (!sym->backend_decl)
2968 sym->backend_decl = gfc_get_extern_function_decl (sym);
2970 TREE_USED (sym->backend_decl) = 1;
2972 tmp = sym->backend_decl;
2974 if (sym->attr.cray_pointee)
2976 /* TODO - make the cray pointee a pointer to a procedure,
2977 assign the pointer to it and use it for the call. This
2978 will do for now! */
2979 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
2980 gfc_get_symbol_decl (sym->cp_pointer));
2981 tmp = gfc_evaluate_now (tmp, &se->pre);
2984 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
2986 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
2987 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
2990 se->expr = tmp;
2994 /* Initialize MAPPING. */
2996 void
2997 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
2999 mapping->syms = NULL;
3000 mapping->charlens = NULL;
3004 /* Free all memory held by MAPPING (but not MAPPING itself). */
3006 void
3007 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
3009 gfc_interface_sym_mapping *sym;
3010 gfc_interface_sym_mapping *nextsym;
3011 gfc_charlen *cl;
3012 gfc_charlen *nextcl;
3014 for (sym = mapping->syms; sym; sym = nextsym)
3016 nextsym = sym->next;
3017 sym->new_sym->n.sym->formal = NULL;
3018 gfc_free_symbol (sym->new_sym->n.sym);
3019 gfc_free_expr (sym->expr);
3020 free (sym->new_sym);
3021 free (sym);
3023 for (cl = mapping->charlens; cl; cl = nextcl)
3025 nextcl = cl->next;
3026 gfc_free_expr (cl->length);
3027 free (cl);
3032 /* Return a copy of gfc_charlen CL. Add the returned structure to
3033 MAPPING so that it will be freed by gfc_free_interface_mapping. */
3035 static gfc_charlen *
3036 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
3037 gfc_charlen * cl)
3039 gfc_charlen *new_charlen;
3041 new_charlen = gfc_get_charlen ();
3042 new_charlen->next = mapping->charlens;
3043 new_charlen->length = gfc_copy_expr (cl->length);
3045 mapping->charlens = new_charlen;
3046 return new_charlen;
3050 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
3051 array variable that can be used as the actual argument for dummy
3052 argument SYM. Add any initialization code to BLOCK. PACKED is as
3053 for gfc_get_nodesc_array_type and DATA points to the first element
3054 in the passed array. */
3056 static tree
3057 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
3058 gfc_packed packed, tree data)
3060 tree type;
3061 tree var;
3063 type = gfc_typenode_for_spec (&sym->ts);
3064 type = gfc_get_nodesc_array_type (type, sym->as, packed,
3065 !sym->attr.target && !sym->attr.pointer
3066 && !sym->attr.proc_pointer);
3068 var = gfc_create_var (type, "ifm");
3069 gfc_add_modify (block, var, fold_convert (type, data));
3071 return var;
3075 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
3076 and offset of descriptorless array type TYPE given that it has the same
3077 size as DESC. Add any set-up code to BLOCK. */
3079 static void
3080 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
3082 int n;
3083 tree dim;
3084 tree offset;
3085 tree tmp;
3087 offset = gfc_index_zero_node;
3088 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
3090 dim = gfc_rank_cst[n];
3091 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
3092 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
3094 GFC_TYPE_ARRAY_LBOUND (type, n)
3095 = gfc_conv_descriptor_lbound_get (desc, dim);
3096 GFC_TYPE_ARRAY_UBOUND (type, n)
3097 = gfc_conv_descriptor_ubound_get (desc, dim);
3099 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
3101 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3102 gfc_array_index_type,
3103 gfc_conv_descriptor_ubound_get (desc, dim),
3104 gfc_conv_descriptor_lbound_get (desc, dim));
3105 tmp = fold_build2_loc (input_location, PLUS_EXPR,
3106 gfc_array_index_type,
3107 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
3108 tmp = gfc_evaluate_now (tmp, block);
3109 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
3111 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
3112 GFC_TYPE_ARRAY_LBOUND (type, n),
3113 GFC_TYPE_ARRAY_STRIDE (type, n));
3114 offset = fold_build2_loc (input_location, MINUS_EXPR,
3115 gfc_array_index_type, offset, tmp);
3117 offset = gfc_evaluate_now (offset, block);
3118 GFC_TYPE_ARRAY_OFFSET (type) = offset;
3122 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
3123 in SE. The caller may still use se->expr and se->string_length after
3124 calling this function. */
3126 void
3127 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
3128 gfc_symbol * sym, gfc_se * se,
3129 gfc_expr *expr)
3131 gfc_interface_sym_mapping *sm;
3132 tree desc;
3133 tree tmp;
3134 tree value;
3135 gfc_symbol *new_sym;
3136 gfc_symtree *root;
3137 gfc_symtree *new_symtree;
3139 /* Create a new symbol to represent the actual argument. */
3140 new_sym = gfc_new_symbol (sym->name, NULL);
3141 new_sym->ts = sym->ts;
3142 new_sym->as = gfc_copy_array_spec (sym->as);
3143 new_sym->attr.referenced = 1;
3144 new_sym->attr.dimension = sym->attr.dimension;
3145 new_sym->attr.contiguous = sym->attr.contiguous;
3146 new_sym->attr.codimension = sym->attr.codimension;
3147 new_sym->attr.pointer = sym->attr.pointer;
3148 new_sym->attr.allocatable = sym->attr.allocatable;
3149 new_sym->attr.flavor = sym->attr.flavor;
3150 new_sym->attr.function = sym->attr.function;
3152 /* Ensure that the interface is available and that
3153 descriptors are passed for array actual arguments. */
3154 if (sym->attr.flavor == FL_PROCEDURE)
3156 new_sym->formal = expr->symtree->n.sym->formal;
3157 new_sym->attr.always_explicit
3158 = expr->symtree->n.sym->attr.always_explicit;
3161 /* Create a fake symtree for it. */
3162 root = NULL;
3163 new_symtree = gfc_new_symtree (&root, sym->name);
3164 new_symtree->n.sym = new_sym;
3165 gcc_assert (new_symtree == root);
3167 /* Create a dummy->actual mapping. */
3168 sm = XCNEW (gfc_interface_sym_mapping);
3169 sm->next = mapping->syms;
3170 sm->old = sym;
3171 sm->new_sym = new_symtree;
3172 sm->expr = gfc_copy_expr (expr);
3173 mapping->syms = sm;
3175 /* Stabilize the argument's value. */
3176 if (!sym->attr.function && se)
3177 se->expr = gfc_evaluate_now (se->expr, &se->pre);
3179 if (sym->ts.type == BT_CHARACTER)
3181 /* Create a copy of the dummy argument's length. */
3182 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
3183 sm->expr->ts.u.cl = new_sym->ts.u.cl;
3185 /* If the length is specified as "*", record the length that
3186 the caller is passing. We should use the callee's length
3187 in all other cases. */
3188 if (!new_sym->ts.u.cl->length && se)
3190 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
3191 new_sym->ts.u.cl->backend_decl = se->string_length;
3195 if (!se)
3196 return;
3198 /* Use the passed value as-is if the argument is a function. */
3199 if (sym->attr.flavor == FL_PROCEDURE)
3200 value = se->expr;
3202 /* If the argument is either a string or a pointer to a string,
3203 convert it to a boundless character type. */
3204 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
3206 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
3207 tmp = build_pointer_type (tmp);
3208 if (sym->attr.pointer)
3209 value = build_fold_indirect_ref_loc (input_location,
3210 se->expr);
3211 else
3212 value = se->expr;
3213 value = fold_convert (tmp, value);
3216 /* If the argument is a scalar, a pointer to an array or an allocatable,
3217 dereference it. */
3218 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
3219 value = build_fold_indirect_ref_loc (input_location,
3220 se->expr);
3222 /* For character(*), use the actual argument's descriptor. */
3223 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
3224 value = build_fold_indirect_ref_loc (input_location,
3225 se->expr);
3227 /* If the argument is an array descriptor, use it to determine
3228 information about the actual argument's shape. */
3229 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
3230 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
3232 /* Get the actual argument's descriptor. */
3233 desc = build_fold_indirect_ref_loc (input_location,
3234 se->expr);
3236 /* Create the replacement variable. */
3237 tmp = gfc_conv_descriptor_data_get (desc);
3238 value = gfc_get_interface_mapping_array (&se->pre, sym,
3239 PACKED_NO, tmp);
3241 /* Use DESC to work out the upper bounds, strides and offset. */
3242 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
3244 else
3245 /* Otherwise we have a packed array. */
3246 value = gfc_get_interface_mapping_array (&se->pre, sym,
3247 PACKED_FULL, se->expr);
3249 new_sym->backend_decl = value;
3253 /* Called once all dummy argument mappings have been added to MAPPING,
3254 but before the mapping is used to evaluate expressions. Pre-evaluate
3255 the length of each argument, adding any initialization code to PRE and
3256 any finalization code to POST. */
3258 void
3259 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
3260 stmtblock_t * pre, stmtblock_t * post)
3262 gfc_interface_sym_mapping *sym;
3263 gfc_expr *expr;
3264 gfc_se se;
3266 for (sym = mapping->syms; sym; sym = sym->next)
3267 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
3268 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
3270 expr = sym->new_sym->n.sym->ts.u.cl->length;
3271 gfc_apply_interface_mapping_to_expr (mapping, expr);
3272 gfc_init_se (&se, NULL);
3273 gfc_conv_expr (&se, expr);
3274 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
3275 se.expr = gfc_evaluate_now (se.expr, &se.pre);
3276 gfc_add_block_to_block (pre, &se.pre);
3277 gfc_add_block_to_block (post, &se.post);
3279 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
3284 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3285 constructor C. */
3287 static void
3288 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
3289 gfc_constructor_base base)
3291 gfc_constructor *c;
3292 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
3294 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
3295 if (c->iterator)
3297 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
3298 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
3299 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
3305 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3306 reference REF. */
3308 static void
3309 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
3310 gfc_ref * ref)
3312 int n;
3314 for (; ref; ref = ref->next)
3315 switch (ref->type)
3317 case REF_ARRAY:
3318 for (n = 0; n < ref->u.ar.dimen; n++)
3320 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
3321 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
3322 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
3324 break;
3326 case REF_COMPONENT:
3327 break;
3329 case REF_SUBSTRING:
3330 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
3331 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
3332 break;
3337 /* Convert intrinsic function calls into result expressions. */
3339 static bool
3340 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
3342 gfc_symbol *sym;
3343 gfc_expr *new_expr;
3344 gfc_expr *arg1;
3345 gfc_expr *arg2;
3346 int d, dup;
3348 arg1 = expr->value.function.actual->expr;
3349 if (expr->value.function.actual->next)
3350 arg2 = expr->value.function.actual->next->expr;
3351 else
3352 arg2 = NULL;
3354 sym = arg1->symtree->n.sym;
3356 if (sym->attr.dummy)
3357 return false;
3359 new_expr = NULL;
3361 switch (expr->value.function.isym->id)
3363 case GFC_ISYM_LEN:
3364 /* TODO figure out why this condition is necessary. */
3365 if (sym->attr.function
3366 && (arg1->ts.u.cl->length == NULL
3367 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
3368 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
3369 return false;
3371 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
3372 break;
3374 case GFC_ISYM_SIZE:
3375 if (!sym->as || sym->as->rank == 0)
3376 return false;
3378 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
3380 dup = mpz_get_si (arg2->value.integer);
3381 d = dup - 1;
3383 else
3385 dup = sym->as->rank;
3386 d = 0;
3389 for (; d < dup; d++)
3391 gfc_expr *tmp;
3393 if (!sym->as->upper[d] || !sym->as->lower[d])
3395 gfc_free_expr (new_expr);
3396 return false;
3399 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
3400 gfc_get_int_expr (gfc_default_integer_kind,
3401 NULL, 1));
3402 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
3403 if (new_expr)
3404 new_expr = gfc_multiply (new_expr, tmp);
3405 else
3406 new_expr = tmp;
3408 break;
3410 case GFC_ISYM_LBOUND:
3411 case GFC_ISYM_UBOUND:
3412 /* TODO These implementations of lbound and ubound do not limit if
3413 the size < 0, according to F95's 13.14.53 and 13.14.113. */
3415 if (!sym->as || sym->as->rank == 0)
3416 return false;
3418 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
3419 d = mpz_get_si (arg2->value.integer) - 1;
3420 else
3421 /* TODO: If the need arises, this could produce an array of
3422 ubound/lbounds. */
3423 gcc_unreachable ();
3425 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
3427 if (sym->as->lower[d])
3428 new_expr = gfc_copy_expr (sym->as->lower[d]);
3430 else
3432 if (sym->as->upper[d])
3433 new_expr = gfc_copy_expr (sym->as->upper[d]);
3435 break;
3437 default:
3438 break;
3441 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
3442 if (!new_expr)
3443 return false;
3445 gfc_replace_expr (expr, new_expr);
3446 return true;
3450 static void
3451 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
3452 gfc_interface_mapping * mapping)
3454 gfc_formal_arglist *f;
3455 gfc_actual_arglist *actual;
3457 actual = expr->value.function.actual;
3458 f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
3460 for (; f && actual; f = f->next, actual = actual->next)
3462 if (!actual->expr)
3463 continue;
3465 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
3468 if (map_expr->symtree->n.sym->attr.dimension)
3470 int d;
3471 gfc_array_spec *as;
3473 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
3475 for (d = 0; d < as->rank; d++)
3477 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
3478 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
3481 expr->value.function.esym->as = as;
3484 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
3486 expr->value.function.esym->ts.u.cl->length
3487 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
3489 gfc_apply_interface_mapping_to_expr (mapping,
3490 expr->value.function.esym->ts.u.cl->length);
3495 /* EXPR is a copy of an expression that appeared in the interface
3496 associated with MAPPING. Walk it recursively looking for references to
3497 dummy arguments that MAPPING maps to actual arguments. Replace each such
3498 reference with a reference to the associated actual argument. */
3500 static void
3501 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
3502 gfc_expr * expr)
3504 gfc_interface_sym_mapping *sym;
3505 gfc_actual_arglist *actual;
3507 if (!expr)
3508 return;
3510 /* Copying an expression does not copy its length, so do that here. */
3511 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
3513 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
3514 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
3517 /* Apply the mapping to any references. */
3518 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
3520 /* ...and to the expression's symbol, if it has one. */
3521 /* TODO Find out why the condition on expr->symtree had to be moved into
3522 the loop rather than being outside it, as originally. */
3523 for (sym = mapping->syms; sym; sym = sym->next)
3524 if (expr->symtree && sym->old == expr->symtree->n.sym)
3526 if (sym->new_sym->n.sym->backend_decl)
3527 expr->symtree = sym->new_sym;
3528 else if (sym->expr)
3529 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
3530 /* Replace base type for polymorphic arguments. */
3531 if (expr->ref && expr->ref->type == REF_COMPONENT
3532 && sym->expr && sym->expr->ts.type == BT_CLASS)
3533 expr->ref->u.c.sym = sym->expr->ts.u.derived;
3536 /* ...and to subexpressions in expr->value. */
3537 switch (expr->expr_type)
3539 case EXPR_VARIABLE:
3540 case EXPR_CONSTANT:
3541 case EXPR_NULL:
3542 case EXPR_SUBSTRING:
3543 break;
3545 case EXPR_OP:
3546 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
3547 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
3548 break;
3550 case EXPR_FUNCTION:
3551 for (actual = expr->value.function.actual; actual; actual = actual->next)
3552 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
3554 if (expr->value.function.esym == NULL
3555 && expr->value.function.isym != NULL
3556 && expr->value.function.actual->expr->symtree
3557 && gfc_map_intrinsic_function (expr, mapping))
3558 break;
3560 for (sym = mapping->syms; sym; sym = sym->next)
3561 if (sym->old == expr->value.function.esym)
3563 expr->value.function.esym = sym->new_sym->n.sym;
3564 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
3565 expr->value.function.esym->result = sym->new_sym->n.sym;
3567 break;
3569 case EXPR_ARRAY:
3570 case EXPR_STRUCTURE:
3571 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
3572 break;
3574 case EXPR_COMPCALL:
3575 case EXPR_PPC:
3576 gcc_unreachable ();
3577 break;
3580 return;
3584 /* Evaluate interface expression EXPR using MAPPING. Store the result
3585 in SE. */
3587 void
3588 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
3589 gfc_se * se, gfc_expr * expr)
3591 expr = gfc_copy_expr (expr);
3592 gfc_apply_interface_mapping_to_expr (mapping, expr);
3593 gfc_conv_expr (se, expr);
3594 se->expr = gfc_evaluate_now (se->expr, &se->pre);
3595 gfc_free_expr (expr);
3599 /* Returns a reference to a temporary array into which a component of
3600 an actual argument derived type array is copied and then returned
3601 after the function call. */
3602 void
3603 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
3604 sym_intent intent, bool formal_ptr)
3606 gfc_se lse;
3607 gfc_se rse;
3608 gfc_ss *lss;
3609 gfc_ss *rss;
3610 gfc_loopinfo loop;
3611 gfc_loopinfo loop2;
3612 gfc_array_info *info;
3613 tree offset;
3614 tree tmp_index;
3615 tree tmp;
3616 tree base_type;
3617 tree size;
3618 stmtblock_t body;
3619 int n;
3620 int dimen;
3622 gcc_assert (expr->expr_type == EXPR_VARIABLE);
3624 gfc_init_se (&lse, NULL);
3625 gfc_init_se (&rse, NULL);
3627 /* Walk the argument expression. */
3628 rss = gfc_walk_expr (expr);
3630 gcc_assert (rss != gfc_ss_terminator);
3632 /* Initialize the scalarizer. */
3633 gfc_init_loopinfo (&loop);
3634 gfc_add_ss_to_loop (&loop, rss);
3636 /* Calculate the bounds of the scalarization. */
3637 gfc_conv_ss_startstride (&loop);
3639 /* Build an ss for the temporary. */
3640 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
3641 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
3643 base_type = gfc_typenode_for_spec (&expr->ts);
3644 if (GFC_ARRAY_TYPE_P (base_type)
3645 || GFC_DESCRIPTOR_TYPE_P (base_type))
3646 base_type = gfc_get_element_type (base_type);
3648 if (expr->ts.type == BT_CLASS)
3649 base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
3651 loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
3652 ? expr->ts.u.cl->backend_decl
3653 : NULL),
3654 loop.dimen);
3656 parmse->string_length = loop.temp_ss->info->string_length;
3658 /* Associate the SS with the loop. */
3659 gfc_add_ss_to_loop (&loop, loop.temp_ss);
3661 /* Setup the scalarizing loops. */
3662 gfc_conv_loop_setup (&loop, &expr->where);
3664 /* Pass the temporary descriptor back to the caller. */
3665 info = &loop.temp_ss->info->data.array;
3666 parmse->expr = info->descriptor;
3668 /* Setup the gfc_se structures. */
3669 gfc_copy_loopinfo_to_se (&lse, &loop);
3670 gfc_copy_loopinfo_to_se (&rse, &loop);
3672 rse.ss = rss;
3673 lse.ss = loop.temp_ss;
3674 gfc_mark_ss_chain_used (rss, 1);
3675 gfc_mark_ss_chain_used (loop.temp_ss, 1);
3677 /* Start the scalarized loop body. */
3678 gfc_start_scalarized_body (&loop, &body);
3680 /* Translate the expression. */
3681 gfc_conv_expr (&rse, expr);
3683 gfc_conv_tmp_array_ref (&lse);
3685 if (intent != INTENT_OUT)
3687 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false, true);
3688 gfc_add_expr_to_block (&body, tmp);
3689 gcc_assert (rse.ss == gfc_ss_terminator);
3690 gfc_trans_scalarizing_loops (&loop, &body);
3692 else
3694 /* Make sure that the temporary declaration survives by merging
3695 all the loop declarations into the current context. */
3696 for (n = 0; n < loop.dimen; n++)
3698 gfc_merge_block_scope (&body);
3699 body = loop.code[loop.order[n]];
3701 gfc_merge_block_scope (&body);
3704 /* Add the post block after the second loop, so that any
3705 freeing of allocated memory is done at the right time. */
3706 gfc_add_block_to_block (&parmse->pre, &loop.pre);
3708 /**********Copy the temporary back again.*********/
3710 gfc_init_se (&lse, NULL);
3711 gfc_init_se (&rse, NULL);
3713 /* Walk the argument expression. */
3714 lss = gfc_walk_expr (expr);
3715 rse.ss = loop.temp_ss;
3716 lse.ss = lss;
3718 /* Initialize the scalarizer. */
3719 gfc_init_loopinfo (&loop2);
3720 gfc_add_ss_to_loop (&loop2, lss);
3722 /* Calculate the bounds of the scalarization. */
3723 gfc_conv_ss_startstride (&loop2);
3725 /* Setup the scalarizing loops. */
3726 gfc_conv_loop_setup (&loop2, &expr->where);
3728 gfc_copy_loopinfo_to_se (&lse, &loop2);
3729 gfc_copy_loopinfo_to_se (&rse, &loop2);
3731 gfc_mark_ss_chain_used (lss, 1);
3732 gfc_mark_ss_chain_used (loop.temp_ss, 1);
3734 /* Declare the variable to hold the temporary offset and start the
3735 scalarized loop body. */
3736 offset = gfc_create_var (gfc_array_index_type, NULL);
3737 gfc_start_scalarized_body (&loop2, &body);
3739 /* Build the offsets for the temporary from the loop variables. The
3740 temporary array has lbounds of zero and strides of one in all
3741 dimensions, so this is very simple. The offset is only computed
3742 outside the innermost loop, so the overall transfer could be
3743 optimized further. */
3744 info = &rse.ss->info->data.array;
3745 dimen = rse.ss->dimen;
3747 tmp_index = gfc_index_zero_node;
3748 for (n = dimen - 1; n > 0; n--)
3750 tree tmp_str;
3751 tmp = rse.loop->loopvar[n];
3752 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
3753 tmp, rse.loop->from[n]);
3754 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
3755 tmp, tmp_index);
3757 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
3758 gfc_array_index_type,
3759 rse.loop->to[n-1], rse.loop->from[n-1]);
3760 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
3761 gfc_array_index_type,
3762 tmp_str, gfc_index_one_node);
3764 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
3765 gfc_array_index_type, tmp, tmp_str);
3768 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
3769 gfc_array_index_type,
3770 tmp_index, rse.loop->from[0]);
3771 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
3773 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
3774 gfc_array_index_type,
3775 rse.loop->loopvar[0], offset);
3777 /* Now use the offset for the reference. */
3778 tmp = build_fold_indirect_ref_loc (input_location,
3779 info->data);
3780 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
3782 if (expr->ts.type == BT_CHARACTER)
3783 rse.string_length = expr->ts.u.cl->backend_decl;
3785 gfc_conv_expr (&lse, expr);
3787 gcc_assert (lse.ss == gfc_ss_terminator);
3789 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false, true);
3790 gfc_add_expr_to_block (&body, tmp);
3792 /* Generate the copying loops. */
3793 gfc_trans_scalarizing_loops (&loop2, &body);
3795 /* Wrap the whole thing up by adding the second loop to the post-block
3796 and following it by the post-block of the first loop. In this way,
3797 if the temporary needs freeing, it is done after use! */
3798 if (intent != INTENT_IN)
3800 gfc_add_block_to_block (&parmse->post, &loop2.pre);
3801 gfc_add_block_to_block (&parmse->post, &loop2.post);
3804 gfc_add_block_to_block (&parmse->post, &loop.post);
3806 gfc_cleanup_loop (&loop);
3807 gfc_cleanup_loop (&loop2);
3809 /* Pass the string length to the argument expression. */
3810 if (expr->ts.type == BT_CHARACTER)
3811 parmse->string_length = expr->ts.u.cl->backend_decl;
3813 /* Determine the offset for pointer formal arguments and set the
3814 lbounds to one. */
3815 if (formal_ptr)
3817 size = gfc_index_one_node;
3818 offset = gfc_index_zero_node;
3819 for (n = 0; n < dimen; n++)
3821 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
3822 gfc_rank_cst[n]);
3823 tmp = fold_build2_loc (input_location, PLUS_EXPR,
3824 gfc_array_index_type, tmp,
3825 gfc_index_one_node);
3826 gfc_conv_descriptor_ubound_set (&parmse->pre,
3827 parmse->expr,
3828 gfc_rank_cst[n],
3829 tmp);
3830 gfc_conv_descriptor_lbound_set (&parmse->pre,
3831 parmse->expr,
3832 gfc_rank_cst[n],
3833 gfc_index_one_node);
3834 size = gfc_evaluate_now (size, &parmse->pre);
3835 offset = fold_build2_loc (input_location, MINUS_EXPR,
3836 gfc_array_index_type,
3837 offset, size);
3838 offset = gfc_evaluate_now (offset, &parmse->pre);
3839 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3840 gfc_array_index_type,
3841 rse.loop->to[n], rse.loop->from[n]);
3842 tmp = fold_build2_loc (input_location, PLUS_EXPR,
3843 gfc_array_index_type,
3844 tmp, gfc_index_one_node);
3845 size = fold_build2_loc (input_location, MULT_EXPR,
3846 gfc_array_index_type, size, tmp);
3849 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
3850 offset);
3853 /* We want either the address for the data or the address of the descriptor,
3854 depending on the mode of passing array arguments. */
3855 if (g77)
3856 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
3857 else
3858 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
3860 return;
3864 /* Generate the code for argument list functions. */
3866 static void
3867 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
3869 /* Pass by value for g77 %VAL(arg), pass the address
3870 indirectly for %LOC, else by reference. Thus %REF
3871 is a "do-nothing" and %LOC is the same as an F95
3872 pointer. */
3873 if (strncmp (name, "%VAL", 4) == 0)
3874 gfc_conv_expr (se, expr);
3875 else if (strncmp (name, "%LOC", 4) == 0)
3877 gfc_conv_expr_reference (se, expr);
3878 se->expr = gfc_build_addr_expr (NULL, se->expr);
3880 else if (strncmp (name, "%REF", 4) == 0)
3881 gfc_conv_expr_reference (se, expr);
3882 else
3883 gfc_error ("Unknown argument list function at %L", &expr->where);
3887 /* Generate code for a procedure call. Note can return se->post != NULL.
3888 If se->direct_byref is set then se->expr contains the return parameter.
3889 Return nonzero, if the call has alternate specifiers.
3890 'expr' is only needed for procedure pointer components. */
3893 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
3894 gfc_actual_arglist * args, gfc_expr * expr,
3895 vec<tree, va_gc> *append_args)
3897 gfc_interface_mapping mapping;
3898 vec<tree, va_gc> *arglist;
3899 vec<tree, va_gc> *retargs;
3900 tree tmp;
3901 tree fntype;
3902 gfc_se parmse;
3903 gfc_array_info *info;
3904 int byref;
3905 int parm_kind;
3906 tree type;
3907 tree var;
3908 tree len;
3909 tree base_object;
3910 vec<tree, va_gc> *stringargs;
3911 vec<tree, va_gc> *optionalargs;
3912 tree result = NULL;
3913 gfc_formal_arglist *formal;
3914 gfc_actual_arglist *arg;
3915 int has_alternate_specifier = 0;
3916 bool need_interface_mapping;
3917 bool callee_alloc;
3918 gfc_typespec ts;
3919 gfc_charlen cl;
3920 gfc_expr *e;
3921 gfc_symbol *fsym;
3922 stmtblock_t post;
3923 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
3924 gfc_component *comp = NULL;
3925 int arglen;
3927 arglist = NULL;
3928 retargs = NULL;
3929 stringargs = NULL;
3930 optionalargs = NULL;
3931 var = NULL_TREE;
3932 len = NULL_TREE;
3933 gfc_clear_ts (&ts);
3935 comp = gfc_get_proc_ptr_comp (expr);
3937 if (se->ss != NULL)
3939 if (!sym->attr.elemental && !(comp && comp->attr.elemental))
3941 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
3942 if (se->ss->info->useflags)
3944 gcc_assert ((!comp && gfc_return_by_reference (sym)
3945 && sym->result->attr.dimension)
3946 || (comp && comp->attr.dimension));
3947 gcc_assert (se->loop != NULL);
3949 /* Access the previously obtained result. */
3950 gfc_conv_tmp_array_ref (se);
3951 return 0;
3954 info = &se->ss->info->data.array;
3956 else
3957 info = NULL;
3959 gfc_init_block (&post);
3960 gfc_init_interface_mapping (&mapping);
3961 if (!comp)
3963 formal = gfc_sym_get_dummy_args (sym);
3964 need_interface_mapping = sym->attr.dimension ||
3965 (sym->ts.type == BT_CHARACTER
3966 && sym->ts.u.cl->length
3967 && sym->ts.u.cl->length->expr_type
3968 != EXPR_CONSTANT);
3970 else
3972 formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
3973 need_interface_mapping = comp->attr.dimension ||
3974 (comp->ts.type == BT_CHARACTER
3975 && comp->ts.u.cl->length
3976 && comp->ts.u.cl->length->expr_type
3977 != EXPR_CONSTANT);
3980 base_object = NULL_TREE;
3982 /* Evaluate the arguments. */
3983 for (arg = args; arg != NULL;
3984 arg = arg->next, formal = formal ? formal->next : NULL)
3986 e = arg->expr;
3987 fsym = formal ? formal->sym : NULL;
3988 parm_kind = MISSING;
3990 /* Class array expressions are sometimes coming completely unadorned
3991 with either arrayspec or _data component. Correct that here.
3992 OOP-TODO: Move this to the frontend. */
3993 if (e && e->expr_type == EXPR_VARIABLE
3994 && !e->ref
3995 && e->ts.type == BT_CLASS
3996 && (CLASS_DATA (e)->attr.codimension
3997 || CLASS_DATA (e)->attr.dimension))
3999 gfc_typespec temp_ts = e->ts;
4000 gfc_add_class_array_ref (e);
4001 e->ts = temp_ts;
4004 if (e == NULL)
4006 if (se->ignore_optional)
4008 /* Some intrinsics have already been resolved to the correct
4009 parameters. */
4010 continue;
4012 else if (arg->label)
4014 has_alternate_specifier = 1;
4015 continue;
4017 else
4019 gfc_init_se (&parmse, NULL);
4021 /* For scalar arguments with VALUE attribute which are passed by
4022 value, pass "0" and a hidden argument gives the optional
4023 status. */
4024 if (fsym && fsym->attr.optional && fsym->attr.value
4025 && !fsym->attr.dimension && fsym->ts.type != BT_CHARACTER
4026 && fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED)
4028 parmse.expr = fold_convert (gfc_sym_type (fsym),
4029 integer_zero_node);
4030 vec_safe_push (optionalargs, boolean_false_node);
4032 else
4034 /* Pass a NULL pointer for an absent arg. */
4035 parmse.expr = null_pointer_node;
4036 if (arg->missing_arg_type == BT_CHARACTER)
4037 parmse.string_length = build_int_cst (gfc_charlen_type_node,
4042 else if (arg->expr->expr_type == EXPR_NULL
4043 && fsym && !fsym->attr.pointer
4044 && (fsym->ts.type != BT_CLASS
4045 || !CLASS_DATA (fsym)->attr.class_pointer))
4047 /* Pass a NULL pointer to denote an absent arg. */
4048 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
4049 && (fsym->ts.type != BT_CLASS
4050 || !CLASS_DATA (fsym)->attr.allocatable));
4051 gfc_init_se (&parmse, NULL);
4052 parmse.expr = null_pointer_node;
4053 if (arg->missing_arg_type == BT_CHARACTER)
4054 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
4056 else if (fsym && fsym->ts.type == BT_CLASS
4057 && e->ts.type == BT_DERIVED)
4059 /* The derived type needs to be converted to a temporary
4060 CLASS object. */
4061 gfc_init_se (&parmse, se);
4062 gfc_conv_derived_to_class (&parmse, e, fsym->ts, NULL,
4063 fsym->attr.optional
4064 && e->expr_type == EXPR_VARIABLE
4065 && e->symtree->n.sym->attr.optional,
4066 CLASS_DATA (fsym)->attr.class_pointer
4067 || CLASS_DATA (fsym)->attr.allocatable);
4069 else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS)
4071 /* The intrinsic type needs to be converted to a temporary
4072 CLASS object for the unlimited polymorphic formal. */
4073 gfc_init_se (&parmse, se);
4074 gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
4076 else if (se->ss && se->ss->info->useflags)
4078 gfc_ss *ss;
4080 ss = se->ss;
4082 /* An elemental function inside a scalarized loop. */
4083 gfc_init_se (&parmse, se);
4084 parm_kind = ELEMENTAL;
4086 if (fsym && fsym->attr.value)
4087 gfc_conv_expr (&parmse, e);
4088 else
4089 gfc_conv_expr_reference (&parmse, e);
4091 if (e->ts.type == BT_CHARACTER && !e->rank
4092 && e->expr_type == EXPR_FUNCTION)
4093 parmse.expr = build_fold_indirect_ref_loc (input_location,
4094 parmse.expr);
4096 if (fsym && fsym->ts.type == BT_DERIVED
4097 && gfc_is_class_container_ref (e))
4099 parmse.expr = gfc_class_data_get (parmse.expr);
4101 if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
4102 && e->symtree->n.sym->attr.optional)
4104 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
4105 parmse.expr = build3_loc (input_location, COND_EXPR,
4106 TREE_TYPE (parmse.expr),
4107 cond, parmse.expr,
4108 fold_convert (TREE_TYPE (parmse.expr),
4109 null_pointer_node));
4113 /* If we are passing an absent array as optional dummy to an
4114 elemental procedure, make sure that we pass NULL when the data
4115 pointer is NULL. We need this extra conditional because of
4116 scalarization which passes arrays elements to the procedure,
4117 ignoring the fact that the array can be absent/unallocated/... */
4118 if (ss->info->can_be_null_ref && ss->info->type != GFC_SS_REFERENCE)
4120 tree descriptor_data;
4122 descriptor_data = ss->info->data.array.data;
4123 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
4124 descriptor_data,
4125 fold_convert (TREE_TYPE (descriptor_data),
4126 null_pointer_node));
4127 parmse.expr
4128 = fold_build3_loc (input_location, COND_EXPR,
4129 TREE_TYPE (parmse.expr),
4130 gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
4131 fold_convert (TREE_TYPE (parmse.expr),
4132 null_pointer_node),
4133 parmse.expr);
4136 /* The scalarizer does not repackage the reference to a class
4137 array - instead it returns a pointer to the data element. */
4138 if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
4139 gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
4140 fsym->attr.intent != INTENT_IN
4141 && (CLASS_DATA (fsym)->attr.class_pointer
4142 || CLASS_DATA (fsym)->attr.allocatable),
4143 fsym->attr.optional
4144 && e->expr_type == EXPR_VARIABLE
4145 && e->symtree->n.sym->attr.optional,
4146 CLASS_DATA (fsym)->attr.class_pointer
4147 || CLASS_DATA (fsym)->attr.allocatable);
4149 else
4151 bool scalar;
4152 gfc_ss *argss;
4154 gfc_init_se (&parmse, NULL);
4156 /* Check whether the expression is a scalar or not; we cannot use
4157 e->rank as it can be nonzero for functions arguments. */
4158 argss = gfc_walk_expr (e);
4159 scalar = argss == gfc_ss_terminator;
4160 if (!scalar)
4161 gfc_free_ss_chain (argss);
4163 /* Special handling for passing scalar polymorphic coarrays;
4164 otherwise one passes "class->_data.data" instead of "&class". */
4165 if (e->rank == 0 && e->ts.type == BT_CLASS
4166 && fsym && fsym->ts.type == BT_CLASS
4167 && CLASS_DATA (fsym)->attr.codimension
4168 && !CLASS_DATA (fsym)->attr.dimension)
4170 gfc_add_class_array_ref (e);
4171 parmse.want_coarray = 1;
4172 scalar = false;
4175 /* A scalar or transformational function. */
4176 if (scalar)
4178 if (e->expr_type == EXPR_VARIABLE
4179 && e->symtree->n.sym->attr.cray_pointee
4180 && fsym && fsym->attr.flavor == FL_PROCEDURE)
4182 /* The Cray pointer needs to be converted to a pointer to
4183 a type given by the expression. */
4184 gfc_conv_expr (&parmse, e);
4185 type = build_pointer_type (TREE_TYPE (parmse.expr));
4186 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
4187 parmse.expr = convert (type, tmp);
4189 else if (fsym && fsym->attr.value)
4191 if (fsym->ts.type == BT_CHARACTER
4192 && fsym->ts.is_c_interop
4193 && fsym->ns->proc_name != NULL
4194 && fsym->ns->proc_name->attr.is_bind_c)
4196 parmse.expr = NULL;
4197 gfc_conv_scalar_char_value (fsym, &parmse, &e);
4198 if (parmse.expr == NULL)
4199 gfc_conv_expr (&parmse, e);
4201 else
4203 gfc_conv_expr (&parmse, e);
4204 if (fsym->attr.optional
4205 && fsym->ts.type != BT_CLASS
4206 && fsym->ts.type != BT_DERIVED)
4208 if (e->expr_type != EXPR_VARIABLE
4209 || !e->symtree->n.sym->attr.optional
4210 || e->ref != NULL)
4211 vec_safe_push (optionalargs, boolean_true_node);
4212 else
4214 tmp = gfc_conv_expr_present (e->symtree->n.sym);
4215 if (!e->symtree->n.sym->attr.value)
4216 parmse.expr
4217 = fold_build3_loc (input_location, COND_EXPR,
4218 TREE_TYPE (parmse.expr),
4219 tmp, parmse.expr,
4220 fold_convert (TREE_TYPE (parmse.expr),
4221 integer_zero_node));
4223 vec_safe_push (optionalargs, tmp);
4228 else if (arg->name && arg->name[0] == '%')
4229 /* Argument list functions %VAL, %LOC and %REF are signalled
4230 through arg->name. */
4231 conv_arglist_function (&parmse, arg->expr, arg->name);
4232 else if ((e->expr_type == EXPR_FUNCTION)
4233 && ((e->value.function.esym
4234 && e->value.function.esym->result->attr.pointer)
4235 || (!e->value.function.esym
4236 && e->symtree->n.sym->attr.pointer))
4237 && fsym && fsym->attr.target)
4239 gfc_conv_expr (&parmse, e);
4240 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4242 else if (e->expr_type == EXPR_FUNCTION
4243 && e->symtree->n.sym->result
4244 && e->symtree->n.sym->result != e->symtree->n.sym
4245 && e->symtree->n.sym->result->attr.proc_pointer)
4247 /* Functions returning procedure pointers. */
4248 gfc_conv_expr (&parmse, e);
4249 if (fsym && fsym->attr.proc_pointer)
4250 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4252 else
4254 if (e->ts.type == BT_CLASS && fsym
4255 && fsym->ts.type == BT_CLASS
4256 && (!CLASS_DATA (fsym)->as
4257 || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
4258 && CLASS_DATA (e)->attr.codimension)
4260 gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
4261 gcc_assert (!CLASS_DATA (fsym)->as);
4262 gfc_add_class_array_ref (e);
4263 parmse.want_coarray = 1;
4264 gfc_conv_expr_reference (&parmse, e);
4265 class_scalar_coarray_to_class (&parmse, e, fsym->ts,
4266 fsym->attr.optional
4267 && e->expr_type == EXPR_VARIABLE);
4269 else
4270 gfc_conv_expr_reference (&parmse, e);
4272 /* Catch base objects that are not variables. */
4273 if (e->ts.type == BT_CLASS
4274 && e->expr_type != EXPR_VARIABLE
4275 && expr && e == expr->base_expr)
4276 base_object = build_fold_indirect_ref_loc (input_location,
4277 parmse.expr);
4279 /* A class array element needs converting back to be a
4280 class object, if the formal argument is a class object. */
4281 if (fsym && fsym->ts.type == BT_CLASS
4282 && e->ts.type == BT_CLASS
4283 && ((CLASS_DATA (fsym)->as
4284 && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
4285 || CLASS_DATA (e)->attr.dimension))
4286 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
4287 fsym->attr.intent != INTENT_IN
4288 && (CLASS_DATA (fsym)->attr.class_pointer
4289 || CLASS_DATA (fsym)->attr.allocatable),
4290 fsym->attr.optional
4291 && e->expr_type == EXPR_VARIABLE
4292 && e->symtree->n.sym->attr.optional,
4293 CLASS_DATA (fsym)->attr.class_pointer
4294 || CLASS_DATA (fsym)->attr.allocatable);
4296 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4297 allocated on entry, it must be deallocated. */
4298 if (fsym && fsym->attr.intent == INTENT_OUT
4299 && (fsym->attr.allocatable
4300 || (fsym->ts.type == BT_CLASS
4301 && CLASS_DATA (fsym)->attr.allocatable)))
4303 stmtblock_t block;
4304 tree ptr;
4306 gfc_init_block (&block);
4307 ptr = parmse.expr;
4308 if (e->ts.type == BT_CLASS)
4309 ptr = gfc_class_data_get (ptr);
4311 tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
4312 true, e, e->ts);
4313 gfc_add_expr_to_block (&block, tmp);
4314 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4315 void_type_node, ptr,
4316 null_pointer_node);
4317 gfc_add_expr_to_block (&block, tmp);
4319 if (fsym->ts.type == BT_CLASS && UNLIMITED_POLY (fsym))
4321 gfc_add_modify (&block, ptr,
4322 fold_convert (TREE_TYPE (ptr),
4323 null_pointer_node));
4324 gfc_add_expr_to_block (&block, tmp);
4326 else if (fsym->ts.type == BT_CLASS)
4328 gfc_symbol *vtab;
4329 vtab = gfc_find_derived_vtab (fsym->ts.u.derived);
4330 tmp = gfc_get_symbol_decl (vtab);
4331 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
4332 ptr = gfc_class_vptr_get (parmse.expr);
4333 gfc_add_modify (&block, ptr,
4334 fold_convert (TREE_TYPE (ptr), tmp));
4335 gfc_add_expr_to_block (&block, tmp);
4338 if (fsym->attr.optional
4339 && e->expr_type == EXPR_VARIABLE
4340 && e->symtree->n.sym->attr.optional)
4342 tmp = fold_build3_loc (input_location, COND_EXPR,
4343 void_type_node,
4344 gfc_conv_expr_present (e->symtree->n.sym),
4345 gfc_finish_block (&block),
4346 build_empty_stmt (input_location));
4348 else
4349 tmp = gfc_finish_block (&block);
4351 gfc_add_expr_to_block (&se->pre, tmp);
4354 if (fsym && (fsym->ts.type == BT_DERIVED
4355 || fsym->ts.type == BT_ASSUMED)
4356 && e->ts.type == BT_CLASS
4357 && !CLASS_DATA (e)->attr.dimension
4358 && !CLASS_DATA (e)->attr.codimension)
4359 parmse.expr = gfc_class_data_get (parmse.expr);
4361 /* Wrap scalar variable in a descriptor. We need to convert
4362 the address of a pointer back to the pointer itself before,
4363 we can assign it to the data field. */
4365 if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
4366 && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
4368 tmp = parmse.expr;
4369 if (TREE_CODE (tmp) == ADDR_EXPR
4370 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp, 0))))
4371 tmp = TREE_OPERAND (tmp, 0);
4372 parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
4373 fsym->attr);
4374 parmse.expr = gfc_build_addr_expr (NULL_TREE,
4375 parmse.expr);
4377 else if (fsym && e->expr_type != EXPR_NULL
4378 && ((fsym->attr.pointer
4379 && fsym->attr.flavor != FL_PROCEDURE)
4380 || (fsym->attr.proc_pointer
4381 && !(e->expr_type == EXPR_VARIABLE
4382 && e->symtree->n.sym->attr.dummy))
4383 || (fsym->attr.proc_pointer
4384 && e->expr_type == EXPR_VARIABLE
4385 && gfc_is_proc_ptr_comp (e))
4386 || (fsym->attr.allocatable
4387 && fsym->attr.flavor != FL_PROCEDURE)))
4389 /* Scalar pointer dummy args require an extra level of
4390 indirection. The null pointer already contains
4391 this level of indirection. */
4392 parm_kind = SCALAR_POINTER;
4393 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4397 else if (e->ts.type == BT_CLASS
4398 && fsym && fsym->ts.type == BT_CLASS
4399 && (CLASS_DATA (fsym)->attr.dimension
4400 || CLASS_DATA (fsym)->attr.codimension))
4402 /* Pass a class array. */
4403 parmse.use_offset = 1;
4404 gfc_conv_expr_descriptor (&parmse, e);
4406 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4407 allocated on entry, it must be deallocated. */
4408 if (fsym->attr.intent == INTENT_OUT
4409 && CLASS_DATA (fsym)->attr.allocatable)
4411 stmtblock_t block;
4412 tree ptr;
4414 gfc_init_block (&block);
4415 ptr = parmse.expr;
4416 ptr = gfc_class_data_get (ptr);
4418 tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
4419 NULL_TREE, NULL_TREE,
4420 NULL_TREE, true, e,
4421 false);
4422 gfc_add_expr_to_block (&block, tmp);
4423 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
4424 void_type_node, ptr,
4425 null_pointer_node);
4426 gfc_add_expr_to_block (&block, tmp);
4427 gfc_reset_vptr (&block, e);
4429 if (fsym->attr.optional
4430 && e->expr_type == EXPR_VARIABLE
4431 && (!e->ref
4432 || (e->ref->type == REF_ARRAY
4433 && !e->ref->u.ar.type != AR_FULL))
4434 && e->symtree->n.sym->attr.optional)
4436 tmp = fold_build3_loc (input_location, COND_EXPR,
4437 void_type_node,
4438 gfc_conv_expr_present (e->symtree->n.sym),
4439 gfc_finish_block (&block),
4440 build_empty_stmt (input_location));
4442 else
4443 tmp = gfc_finish_block (&block);
4445 gfc_add_expr_to_block (&se->pre, tmp);
4448 /* The conversion does not repackage the reference to a class
4449 array - _data descriptor. */
4450 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
4451 fsym->attr.intent != INTENT_IN
4452 && (CLASS_DATA (fsym)->attr.class_pointer
4453 || CLASS_DATA (fsym)->attr.allocatable),
4454 fsym->attr.optional
4455 && e->expr_type == EXPR_VARIABLE
4456 && e->symtree->n.sym->attr.optional,
4457 CLASS_DATA (fsym)->attr.class_pointer
4458 || CLASS_DATA (fsym)->attr.allocatable);
4460 else
4462 /* If the procedure requires an explicit interface, the actual
4463 argument is passed according to the corresponding formal
4464 argument. If the corresponding formal argument is a POINTER,
4465 ALLOCATABLE or assumed shape, we do not use g77's calling
4466 convention, and pass the address of the array descriptor
4467 instead. Otherwise we use g77's calling convention. */
4468 bool f;
4469 f = (fsym != NULL)
4470 && !(fsym->attr.pointer || fsym->attr.allocatable)
4471 && fsym->as && fsym->as->type != AS_ASSUMED_SHAPE
4472 && fsym->as->type != AS_ASSUMED_RANK;
4473 if (comp)
4474 f = f || !comp->attr.always_explicit;
4475 else
4476 f = f || !sym->attr.always_explicit;
4478 /* If the argument is a function call that may not create
4479 a temporary for the result, we have to check that we
4480 can do it, i.e. that there is no alias between this
4481 argument and another one. */
4482 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
4484 gfc_expr *iarg;
4485 sym_intent intent;
4487 if (fsym != NULL)
4488 intent = fsym->attr.intent;
4489 else
4490 intent = INTENT_UNKNOWN;
4492 if (gfc_check_fncall_dependency (e, intent, sym, args,
4493 NOT_ELEMENTAL))
4494 parmse.force_tmp = 1;
4496 iarg = e->value.function.actual->expr;
4498 /* Temporary needed if aliasing due to host association. */
4499 if (sym->attr.contained
4500 && !sym->attr.pure
4501 && !sym->attr.implicit_pure
4502 && !sym->attr.use_assoc
4503 && iarg->expr_type == EXPR_VARIABLE
4504 && sym->ns == iarg->symtree->n.sym->ns)
4505 parmse.force_tmp = 1;
4507 /* Ditto within module. */
4508 if (sym->attr.use_assoc
4509 && !sym->attr.pure
4510 && !sym->attr.implicit_pure
4511 && iarg->expr_type == EXPR_VARIABLE
4512 && sym->module == iarg->symtree->n.sym->module)
4513 parmse.force_tmp = 1;
4516 if (e->expr_type == EXPR_VARIABLE
4517 && is_subref_array (e))
4518 /* The actual argument is a component reference to an
4519 array of derived types. In this case, the argument
4520 is converted to a temporary, which is passed and then
4521 written back after the procedure call. */
4522 gfc_conv_subref_array_arg (&parmse, e, f,
4523 fsym ? fsym->attr.intent : INTENT_INOUT,
4524 fsym && fsym->attr.pointer);
4525 else if (gfc_is_class_array_ref (e, NULL)
4526 && fsym && fsym->ts.type == BT_DERIVED)
4527 /* The actual argument is a component reference to an
4528 array of derived types. In this case, the argument
4529 is converted to a temporary, which is passed and then
4530 written back after the procedure call.
4531 OOP-TODO: Insert code so that if the dynamic type is
4532 the same as the declared type, copy-in/copy-out does
4533 not occur. */
4534 gfc_conv_subref_array_arg (&parmse, e, f,
4535 fsym ? fsym->attr.intent : INTENT_INOUT,
4536 fsym && fsym->attr.pointer);
4537 else
4538 gfc_conv_array_parameter (&parmse, e, f, fsym, sym->name, NULL);
4540 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4541 allocated on entry, it must be deallocated. */
4542 if (fsym && fsym->attr.allocatable
4543 && fsym->attr.intent == INTENT_OUT)
4545 tmp = build_fold_indirect_ref_loc (input_location,
4546 parmse.expr);
4547 tmp = gfc_trans_dealloc_allocated (tmp, false, e);
4548 if (fsym->attr.optional
4549 && e->expr_type == EXPR_VARIABLE
4550 && e->symtree->n.sym->attr.optional)
4551 tmp = fold_build3_loc (input_location, COND_EXPR,
4552 void_type_node,
4553 gfc_conv_expr_present (e->symtree->n.sym),
4554 tmp, build_empty_stmt (input_location));
4555 gfc_add_expr_to_block (&se->pre, tmp);
4560 /* The case with fsym->attr.optional is that of a user subroutine
4561 with an interface indicating an optional argument. When we call
4562 an intrinsic subroutine, however, fsym is NULL, but we might still
4563 have an optional argument, so we proceed to the substitution
4564 just in case. */
4565 if (e && (fsym == NULL || fsym->attr.optional))
4567 /* If an optional argument is itself an optional dummy argument,
4568 check its presence and substitute a null if absent. This is
4569 only needed when passing an array to an elemental procedure
4570 as then array elements are accessed - or no NULL pointer is
4571 allowed and a "1" or "0" should be passed if not present.
4572 When passing a non-array-descriptor full array to a
4573 non-array-descriptor dummy, no check is needed. For
4574 array-descriptor actual to array-descriptor dummy, see
4575 PR 41911 for why a check has to be inserted.
4576 fsym == NULL is checked as intrinsics required the descriptor
4577 but do not always set fsym. */
4578 if (e->expr_type == EXPR_VARIABLE
4579 && e->symtree->n.sym->attr.optional
4580 && ((e->rank != 0 && sym->attr.elemental)
4581 || e->representation.length || e->ts.type == BT_CHARACTER
4582 || (e->rank != 0
4583 && (fsym == NULL
4584 || (fsym-> as
4585 && (fsym->as->type == AS_ASSUMED_SHAPE
4586 || fsym->as->type == AS_ASSUMED_RANK
4587 || fsym->as->type == AS_DEFERRED))))))
4588 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
4589 e->representation.length);
4592 if (fsym && e)
4594 /* Obtain the character length of an assumed character length
4595 length procedure from the typespec. */
4596 if (fsym->ts.type == BT_CHARACTER
4597 && parmse.string_length == NULL_TREE
4598 && e->ts.type == BT_PROCEDURE
4599 && e->symtree->n.sym->ts.type == BT_CHARACTER
4600 && e->symtree->n.sym->ts.u.cl->length != NULL
4601 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
4603 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
4604 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
4608 if (fsym && need_interface_mapping && e)
4609 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
4611 gfc_add_block_to_block (&se->pre, &parmse.pre);
4612 gfc_add_block_to_block (&post, &parmse.post);
4614 /* Allocated allocatable components of derived types must be
4615 deallocated for non-variable scalars. Non-variable arrays are
4616 dealt with in trans-array.c(gfc_conv_array_parameter). */
4617 if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
4618 && e->ts.u.derived->attr.alloc_comp
4619 && !(e->symtree && e->symtree->n.sym->attr.pointer)
4620 && (e->expr_type != EXPR_VARIABLE && !e->rank))
4622 int parm_rank;
4623 tmp = build_fold_indirect_ref_loc (input_location,
4624 parmse.expr);
4625 parm_rank = e->rank;
4626 switch (parm_kind)
4628 case (ELEMENTAL):
4629 case (SCALAR):
4630 parm_rank = 0;
4631 break;
4633 case (SCALAR_POINTER):
4634 tmp = build_fold_indirect_ref_loc (input_location,
4635 tmp);
4636 break;
4639 if (e->expr_type == EXPR_OP
4640 && e->value.op.op == INTRINSIC_PARENTHESES
4641 && e->value.op.op1->expr_type == EXPR_VARIABLE)
4643 tree local_tmp;
4644 local_tmp = gfc_evaluate_now (tmp, &se->pre);
4645 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
4646 gfc_add_expr_to_block (&se->post, local_tmp);
4649 if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
4651 /* The derived type is passed to gfc_deallocate_alloc_comp.
4652 Therefore, class actuals can handled correctly but derived
4653 types passed to class formals need the _data component. */
4654 tmp = gfc_class_data_get (tmp);
4655 if (!CLASS_DATA (fsym)->attr.dimension)
4656 tmp = build_fold_indirect_ref_loc (input_location, tmp);
4659 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
4661 gfc_add_expr_to_block (&se->post, tmp);
4664 /* Add argument checking of passing an unallocated/NULL actual to
4665 a nonallocatable/nonpointer dummy. */
4667 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
4669 symbol_attribute attr;
4670 char *msg;
4671 tree cond;
4673 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
4674 attr = gfc_expr_attr (e);
4675 else
4676 goto end_pointer_check;
4678 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
4679 allocatable to an optional dummy, cf. 12.5.2.12. */
4680 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
4681 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
4682 goto end_pointer_check;
4684 if (attr.optional)
4686 /* If the actual argument is an optional pointer/allocatable and
4687 the formal argument takes an nonpointer optional value,
4688 it is invalid to pass a non-present argument on, even
4689 though there is no technical reason for this in gfortran.
4690 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
4691 tree present, null_ptr, type;
4693 if (attr.allocatable
4694 && (fsym == NULL || !fsym->attr.allocatable))
4695 asprintf (&msg, "Allocatable actual argument '%s' is not "
4696 "allocated or not present", e->symtree->n.sym->name);
4697 else if (attr.pointer
4698 && (fsym == NULL || !fsym->attr.pointer))
4699 asprintf (&msg, "Pointer actual argument '%s' is not "
4700 "associated or not present",
4701 e->symtree->n.sym->name);
4702 else if (attr.proc_pointer
4703 && (fsym == NULL || !fsym->attr.proc_pointer))
4704 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
4705 "associated or not present",
4706 e->symtree->n.sym->name);
4707 else
4708 goto end_pointer_check;
4710 present = gfc_conv_expr_present (e->symtree->n.sym);
4711 type = TREE_TYPE (present);
4712 present = fold_build2_loc (input_location, EQ_EXPR,
4713 boolean_type_node, present,
4714 fold_convert (type,
4715 null_pointer_node));
4716 type = TREE_TYPE (parmse.expr);
4717 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
4718 boolean_type_node, parmse.expr,
4719 fold_convert (type,
4720 null_pointer_node));
4721 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
4722 boolean_type_node, present, null_ptr);
4724 else
4726 if (attr.allocatable
4727 && (fsym == NULL || !fsym->attr.allocatable))
4728 asprintf (&msg, "Allocatable actual argument '%s' is not "
4729 "allocated", e->symtree->n.sym->name);
4730 else if (attr.pointer
4731 && (fsym == NULL || !fsym->attr.pointer))
4732 asprintf (&msg, "Pointer actual argument '%s' is not "
4733 "associated", e->symtree->n.sym->name);
4734 else if (attr.proc_pointer
4735 && (fsym == NULL || !fsym->attr.proc_pointer))
4736 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
4737 "associated", e->symtree->n.sym->name);
4738 else
4739 goto end_pointer_check;
4741 tmp = parmse.expr;
4743 /* If the argument is passed by value, we need to strip the
4744 INDIRECT_REF. */
4745 if (!POINTER_TYPE_P (TREE_TYPE (parmse.expr)))
4746 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
4748 cond = fold_build2_loc (input_location, EQ_EXPR,
4749 boolean_type_node, tmp,
4750 fold_convert (TREE_TYPE (tmp),
4751 null_pointer_node));
4754 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
4755 msg);
4756 free (msg);
4758 end_pointer_check:
4760 /* Deferred length dummies pass the character length by reference
4761 so that the value can be returned. */
4762 if (parmse.string_length && fsym && fsym->ts.deferred)
4764 tmp = parmse.string_length;
4765 if (TREE_CODE (tmp) != VAR_DECL)
4766 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
4767 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
4770 /* Character strings are passed as two parameters, a length and a
4771 pointer - except for Bind(c) which only passes the pointer.
4772 An unlimited polymorphic formal argument likewise does not
4773 need the length. */
4774 if (parmse.string_length != NULL_TREE
4775 && !sym->attr.is_bind_c
4776 && !(fsym && UNLIMITED_POLY (fsym)))
4777 vec_safe_push (stringargs, parmse.string_length);
4779 /* When calling __copy for character expressions to unlimited
4780 polymorphic entities, the dst argument needs a string length. */
4781 if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
4782 && strncmp (sym->name, "__vtab_CHARACTER", 16) == 0
4783 && arg->next && arg->next->expr
4784 && arg->next->expr->ts.type == BT_DERIVED
4785 && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
4786 vec_safe_push (stringargs, parmse.string_length);
4788 /* For descriptorless coarrays and assumed-shape coarray dummies, we
4789 pass the token and the offset as additional arguments. */
4790 if (fsym && e == NULL && gfc_option.coarray == GFC_FCOARRAY_LIB
4791 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
4792 && !fsym->attr.allocatable)
4793 || (fsym->ts.type == BT_CLASS
4794 && CLASS_DATA (fsym)->attr.codimension
4795 && !CLASS_DATA (fsym)->attr.allocatable)))
4797 /* Token and offset. */
4798 vec_safe_push (stringargs, null_pointer_node);
4799 vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
4800 gcc_assert (fsym->attr.optional);
4802 else if (fsym && gfc_option.coarray == GFC_FCOARRAY_LIB
4803 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
4804 && !fsym->attr.allocatable)
4805 || (fsym->ts.type == BT_CLASS
4806 && CLASS_DATA (fsym)->attr.codimension
4807 && !CLASS_DATA (fsym)->attr.allocatable)))
4809 tree caf_decl, caf_type;
4810 tree offset, tmp2;
4812 caf_decl = gfc_get_tree_for_caf_expr (e);
4813 caf_type = TREE_TYPE (caf_decl);
4815 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
4816 && (GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE
4817 || GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_POINTER))
4818 tmp = gfc_conv_descriptor_token (caf_decl);
4819 else if (DECL_LANG_SPECIFIC (caf_decl)
4820 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
4821 tmp = GFC_DECL_TOKEN (caf_decl);
4822 else
4824 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
4825 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
4826 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
4829 vec_safe_push (stringargs, tmp);
4831 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
4832 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
4833 offset = build_int_cst (gfc_array_index_type, 0);
4834 else if (DECL_LANG_SPECIFIC (caf_decl)
4835 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
4836 offset = GFC_DECL_CAF_OFFSET (caf_decl);
4837 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
4838 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
4839 else
4840 offset = build_int_cst (gfc_array_index_type, 0);
4842 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
4843 tmp = gfc_conv_descriptor_data_get (caf_decl);
4844 else
4846 gcc_assert (POINTER_TYPE_P (caf_type));
4847 tmp = caf_decl;
4850 tmp2 = fsym->ts.type == BT_CLASS
4851 ? gfc_class_data_get (parmse.expr) : parmse.expr;
4852 if ((fsym->ts.type != BT_CLASS
4853 && (fsym->as->type == AS_ASSUMED_SHAPE
4854 || fsym->as->type == AS_ASSUMED_RANK))
4855 || (fsym->ts.type == BT_CLASS
4856 && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
4857 || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
4859 if (fsym->ts.type == BT_CLASS)
4860 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
4861 else
4863 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
4864 tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
4866 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
4867 tmp2 = gfc_conv_descriptor_data_get (tmp2);
4869 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
4870 tmp2 = gfc_conv_descriptor_data_get (tmp2);
4871 else
4873 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
4876 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4877 gfc_array_index_type,
4878 fold_convert (gfc_array_index_type, tmp2),
4879 fold_convert (gfc_array_index_type, tmp));
4880 offset = fold_build2_loc (input_location, PLUS_EXPR,
4881 gfc_array_index_type, offset, tmp);
4883 vec_safe_push (stringargs, offset);
4886 vec_safe_push (arglist, parmse.expr);
4888 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
4890 if (comp)
4891 ts = comp->ts;
4892 else
4893 ts = sym->ts;
4895 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
4896 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
4897 else if (ts.type == BT_CHARACTER)
4899 if (ts.u.cl->length == NULL)
4901 /* Assumed character length results are not allowed by 5.1.1.5 of the
4902 standard and are trapped in resolve.c; except in the case of SPREAD
4903 (and other intrinsics?) and dummy functions. In the case of SPREAD,
4904 we take the character length of the first argument for the result.
4905 For dummies, we have to look through the formal argument list for
4906 this function and use the character length found there.*/
4907 if (ts.deferred)
4908 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
4909 else if (!sym->attr.dummy)
4910 cl.backend_decl = (*stringargs)[0];
4911 else
4913 formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
4914 for (; formal; formal = formal->next)
4915 if (strcmp (formal->sym->name, sym->name) == 0)
4916 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
4918 len = cl.backend_decl;
4920 else
4922 tree tmp;
4924 /* Calculate the length of the returned string. */
4925 gfc_init_se (&parmse, NULL);
4926 if (need_interface_mapping)
4927 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
4928 else
4929 gfc_conv_expr (&parmse, ts.u.cl->length);
4930 gfc_add_block_to_block (&se->pre, &parmse.pre);
4931 gfc_add_block_to_block (&se->post, &parmse.post);
4933 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
4934 tmp = fold_build2_loc (input_location, MAX_EXPR,
4935 gfc_charlen_type_node, tmp,
4936 build_int_cst (gfc_charlen_type_node, 0));
4937 cl.backend_decl = tmp;
4940 /* Set up a charlen structure for it. */
4941 cl.next = NULL;
4942 cl.length = NULL;
4943 ts.u.cl = &cl;
4945 len = cl.backend_decl;
4948 byref = (comp && (comp->attr.dimension || comp->ts.type == BT_CHARACTER))
4949 || (!comp && gfc_return_by_reference (sym));
4950 if (byref)
4952 if (se->direct_byref)
4954 /* Sometimes, too much indirection can be applied; e.g. for
4955 function_result = array_valued_recursive_function. */
4956 if (TREE_TYPE (TREE_TYPE (se->expr))
4957 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
4958 && GFC_DESCRIPTOR_TYPE_P
4959 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
4960 se->expr = build_fold_indirect_ref_loc (input_location,
4961 se->expr);
4963 /* If the lhs of an assignment x = f(..) is allocatable and
4964 f2003 is allowed, we must do the automatic reallocation.
4965 TODO - deal with intrinsics, without using a temporary. */
4966 if (gfc_option.flag_realloc_lhs
4967 && se->ss && se->ss->loop_chain
4968 && se->ss->loop_chain->is_alloc_lhs
4969 && !expr->value.function.isym
4970 && sym->result->as != NULL)
4972 /* Evaluate the bounds of the result, if known. */
4973 gfc_set_loop_bounds_from_array_spec (&mapping, se,
4974 sym->result->as);
4976 /* Perform the automatic reallocation. */
4977 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
4978 expr, NULL);
4979 gfc_add_expr_to_block (&se->pre, tmp);
4981 /* Pass the temporary as the first argument. */
4982 result = info->descriptor;
4984 else
4985 result = build_fold_indirect_ref_loc (input_location,
4986 se->expr);
4987 vec_safe_push (retargs, se->expr);
4989 else if (comp && comp->attr.dimension)
4991 gcc_assert (se->loop && info);
4993 /* Set the type of the array. */
4994 tmp = gfc_typenode_for_spec (&comp->ts);
4995 gcc_assert (se->ss->dimen == se->loop->dimen);
4997 /* Evaluate the bounds of the result, if known. */
4998 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
5000 /* If the lhs of an assignment x = f(..) is allocatable and
5001 f2003 is allowed, we must not generate the function call
5002 here but should just send back the results of the mapping.
5003 This is signalled by the function ss being flagged. */
5004 if (gfc_option.flag_realloc_lhs
5005 && se->ss && se->ss->is_alloc_lhs)
5007 gfc_free_interface_mapping (&mapping);
5008 return has_alternate_specifier;
5011 /* Create a temporary to store the result. In case the function
5012 returns a pointer, the temporary will be a shallow copy and
5013 mustn't be deallocated. */
5014 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
5015 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5016 tmp, NULL_TREE, false,
5017 !comp->attr.pointer, callee_alloc,
5018 &se->ss->info->expr->where);
5020 /* Pass the temporary as the first argument. */
5021 result = info->descriptor;
5022 tmp = gfc_build_addr_expr (NULL_TREE, result);
5023 vec_safe_push (retargs, tmp);
5025 else if (!comp && sym->result->attr.dimension)
5027 gcc_assert (se->loop && info);
5029 /* Set the type of the array. */
5030 tmp = gfc_typenode_for_spec (&ts);
5031 gcc_assert (se->ss->dimen == se->loop->dimen);
5033 /* Evaluate the bounds of the result, if known. */
5034 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
5036 /* If the lhs of an assignment x = f(..) is allocatable and
5037 f2003 is allowed, we must not generate the function call
5038 here but should just send back the results of the mapping.
5039 This is signalled by the function ss being flagged. */
5040 if (gfc_option.flag_realloc_lhs
5041 && se->ss && se->ss->is_alloc_lhs)
5043 gfc_free_interface_mapping (&mapping);
5044 return has_alternate_specifier;
5047 /* Create a temporary to store the result. In case the function
5048 returns a pointer, the temporary will be a shallow copy and
5049 mustn't be deallocated. */
5050 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
5051 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5052 tmp, NULL_TREE, false,
5053 !sym->attr.pointer, callee_alloc,
5054 &se->ss->info->expr->where);
5056 /* Pass the temporary as the first argument. */
5057 result = info->descriptor;
5058 tmp = gfc_build_addr_expr (NULL_TREE, result);
5059 vec_safe_push (retargs, tmp);
5061 else if (ts.type == BT_CHARACTER)
5063 /* Pass the string length. */
5064 type = gfc_get_character_type (ts.kind, ts.u.cl);
5065 type = build_pointer_type (type);
5067 /* Return an address to a char[0:len-1]* temporary for
5068 character pointers. */
5069 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5070 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
5072 var = gfc_create_var (type, "pstr");
5074 if ((!comp && sym->attr.allocatable)
5075 || (comp && comp->attr.allocatable))
5077 gfc_add_modify (&se->pre, var,
5078 fold_convert (TREE_TYPE (var),
5079 null_pointer_node));
5080 tmp = gfc_call_free (convert (pvoid_type_node, var));
5081 gfc_add_expr_to_block (&se->post, tmp);
5084 /* Provide an address expression for the function arguments. */
5085 var = gfc_build_addr_expr (NULL_TREE, var);
5087 else
5088 var = gfc_conv_string_tmp (se, type, len);
5090 vec_safe_push (retargs, var);
5092 else
5094 gcc_assert (gfc_option.flag_f2c && ts.type == BT_COMPLEX);
5096 type = gfc_get_complex_type (ts.kind);
5097 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
5098 vec_safe_push (retargs, var);
5101 /* Add the string length to the argument list. */
5102 if (ts.type == BT_CHARACTER && ts.deferred)
5104 tmp = len;
5105 if (TREE_CODE (tmp) != VAR_DECL)
5106 tmp = gfc_evaluate_now (len, &se->pre);
5107 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5108 vec_safe_push (retargs, tmp);
5110 else if (ts.type == BT_CHARACTER)
5111 vec_safe_push (retargs, len);
5113 gfc_free_interface_mapping (&mapping);
5115 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
5116 arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
5117 + vec_safe_length (stringargs) + vec_safe_length (append_args));
5118 vec_safe_reserve (retargs, arglen);
5120 /* Add the return arguments. */
5121 retargs->splice (arglist);
5123 /* Add the hidden present status for optional+value to the arguments. */
5124 retargs->splice (optionalargs);
5126 /* Add the hidden string length parameters to the arguments. */
5127 retargs->splice (stringargs);
5129 /* We may want to append extra arguments here. This is used e.g. for
5130 calls to libgfortran_matmul_??, which need extra information. */
5131 if (!vec_safe_is_empty (append_args))
5132 retargs->splice (append_args);
5133 arglist = retargs;
5135 /* Generate the actual call. */
5136 if (base_object == NULL_TREE)
5137 conv_function_val (se, sym, expr);
5138 else
5139 conv_base_obj_fcn_val (se, base_object, expr);
5141 /* If there are alternate return labels, function type should be
5142 integer. Can't modify the type in place though, since it can be shared
5143 with other functions. For dummy arguments, the typing is done to
5144 this result, even if it has to be repeated for each call. */
5145 if (has_alternate_specifier
5146 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
5148 if (!sym->attr.dummy)
5150 TREE_TYPE (sym->backend_decl)
5151 = build_function_type (integer_type_node,
5152 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
5153 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
5155 else
5156 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
5159 fntype = TREE_TYPE (TREE_TYPE (se->expr));
5160 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
5162 /* If we have a pointer function, but we don't want a pointer, e.g.
5163 something like
5164 x = f()
5165 where f is pointer valued, we have to dereference the result. */
5166 if (!se->want_pointer && !byref
5167 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5168 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
5169 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
5171 /* f2c calling conventions require a scalar default real function to
5172 return a double precision result. Convert this back to default
5173 real. We only care about the cases that can happen in Fortran 77.
5175 if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
5176 && sym->ts.kind == gfc_default_real_kind
5177 && !sym->attr.always_explicit)
5178 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
5180 /* A pure function may still have side-effects - it may modify its
5181 parameters. */
5182 TREE_SIDE_EFFECTS (se->expr) = 1;
5183 #if 0
5184 if (!sym->attr.pure)
5185 TREE_SIDE_EFFECTS (se->expr) = 1;
5186 #endif
5188 if (byref)
5190 /* Add the function call to the pre chain. There is no expression. */
5191 gfc_add_expr_to_block (&se->pre, se->expr);
5192 se->expr = NULL_TREE;
5194 if (!se->direct_byref)
5196 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
5198 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
5200 /* Check the data pointer hasn't been modified. This would
5201 happen in a function returning a pointer. */
5202 tmp = gfc_conv_descriptor_data_get (info->descriptor);
5203 tmp = fold_build2_loc (input_location, NE_EXPR,
5204 boolean_type_node,
5205 tmp, info->data);
5206 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
5207 gfc_msg_fault);
5209 se->expr = info->descriptor;
5210 /* Bundle in the string length. */
5211 se->string_length = len;
5213 else if (ts.type == BT_CHARACTER)
5215 /* Dereference for character pointer results. */
5216 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5217 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
5218 se->expr = build_fold_indirect_ref_loc (input_location, var);
5219 else
5220 se->expr = var;
5222 se->string_length = len;
5224 else
5226 gcc_assert (ts.type == BT_COMPLEX && gfc_option.flag_f2c);
5227 se->expr = build_fold_indirect_ref_loc (input_location, var);
5232 /* Follow the function call with the argument post block. */
5233 if (byref)
5235 gfc_add_block_to_block (&se->pre, &post);
5237 /* Transformational functions of derived types with allocatable
5238 components must have the result allocatable components copied. */
5239 arg = expr->value.function.actual;
5240 if (result && arg && expr->rank
5241 && expr->value.function.isym
5242 && expr->value.function.isym->transformational
5243 && arg->expr->ts.type == BT_DERIVED
5244 && arg->expr->ts.u.derived->attr.alloc_comp)
5246 tree tmp2;
5247 /* Copy the allocatable components. We have to use a
5248 temporary here to prevent source allocatable components
5249 from being corrupted. */
5250 tmp2 = gfc_evaluate_now (result, &se->pre);
5251 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
5252 result, tmp2, expr->rank);
5253 gfc_add_expr_to_block (&se->pre, tmp);
5254 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
5255 expr->rank);
5256 gfc_add_expr_to_block (&se->pre, tmp);
5258 /* Finally free the temporary's data field. */
5259 tmp = gfc_conv_descriptor_data_get (tmp2);
5260 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
5261 NULL_TREE, NULL_TREE, true,
5262 NULL, false);
5263 gfc_add_expr_to_block (&se->pre, tmp);
5266 else
5267 gfc_add_block_to_block (&se->post, &post);
5269 return has_alternate_specifier;
5273 /* Fill a character string with spaces. */
5275 static tree
5276 fill_with_spaces (tree start, tree type, tree size)
5278 stmtblock_t block, loop;
5279 tree i, el, exit_label, cond, tmp;
5281 /* For a simple char type, we can call memset(). */
5282 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
5283 return build_call_expr_loc (input_location,
5284 builtin_decl_explicit (BUILT_IN_MEMSET),
5285 3, start,
5286 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
5287 lang_hooks.to_target_charset (' ')),
5288 size);
5290 /* Otherwise, we use a loop:
5291 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
5292 *el = (type) ' ';
5295 /* Initialize variables. */
5296 gfc_init_block (&block);
5297 i = gfc_create_var (sizetype, "i");
5298 gfc_add_modify (&block, i, fold_convert (sizetype, size));
5299 el = gfc_create_var (build_pointer_type (type), "el");
5300 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
5301 exit_label = gfc_build_label_decl (NULL_TREE);
5302 TREE_USED (exit_label) = 1;
5305 /* Loop body. */
5306 gfc_init_block (&loop);
5308 /* Exit condition. */
5309 cond = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, i,
5310 build_zero_cst (sizetype));
5311 tmp = build1_v (GOTO_EXPR, exit_label);
5312 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
5313 build_empty_stmt (input_location));
5314 gfc_add_expr_to_block (&loop, tmp);
5316 /* Assignment. */
5317 gfc_add_modify (&loop,
5318 fold_build1_loc (input_location, INDIRECT_REF, type, el),
5319 build_int_cst (type, lang_hooks.to_target_charset (' ')));
5321 /* Increment loop variables. */
5322 gfc_add_modify (&loop, i,
5323 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
5324 TYPE_SIZE_UNIT (type)));
5325 gfc_add_modify (&loop, el,
5326 fold_build_pointer_plus_loc (input_location,
5327 el, TYPE_SIZE_UNIT (type)));
5329 /* Making the loop... actually loop! */
5330 tmp = gfc_finish_block (&loop);
5331 tmp = build1_v (LOOP_EXPR, tmp);
5332 gfc_add_expr_to_block (&block, tmp);
5334 /* The exit label. */
5335 tmp = build1_v (LABEL_EXPR, exit_label);
5336 gfc_add_expr_to_block (&block, tmp);
5339 return gfc_finish_block (&block);
5343 /* Generate code to copy a string. */
5345 void
5346 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
5347 int dkind, tree slength, tree src, int skind)
5349 tree tmp, dlen, slen;
5350 tree dsc;
5351 tree ssc;
5352 tree cond;
5353 tree cond2;
5354 tree tmp2;
5355 tree tmp3;
5356 tree tmp4;
5357 tree chartype;
5358 stmtblock_t tempblock;
5360 gcc_assert (dkind == skind);
5362 if (slength != NULL_TREE)
5364 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
5365 ssc = gfc_string_to_single_character (slen, src, skind);
5367 else
5369 slen = build_int_cst (size_type_node, 1);
5370 ssc = src;
5373 if (dlength != NULL_TREE)
5375 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
5376 dsc = gfc_string_to_single_character (dlen, dest, dkind);
5378 else
5380 dlen = build_int_cst (size_type_node, 1);
5381 dsc = dest;
5384 /* Assign directly if the types are compatible. */
5385 if (dsc != NULL_TREE && ssc != NULL_TREE
5386 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
5388 gfc_add_modify (block, dsc, ssc);
5389 return;
5392 /* Do nothing if the destination length is zero. */
5393 cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, dlen,
5394 build_int_cst (size_type_node, 0));
5396 /* The following code was previously in _gfortran_copy_string:
5398 // The two strings may overlap so we use memmove.
5399 void
5400 copy_string (GFC_INTEGER_4 destlen, char * dest,
5401 GFC_INTEGER_4 srclen, const char * src)
5403 if (srclen >= destlen)
5405 // This will truncate if too long.
5406 memmove (dest, src, destlen);
5408 else
5410 memmove (dest, src, srclen);
5411 // Pad with spaces.
5412 memset (&dest[srclen], ' ', destlen - srclen);
5416 We're now doing it here for better optimization, but the logic
5417 is the same. */
5419 /* For non-default character kinds, we have to multiply the string
5420 length by the base type size. */
5421 chartype = gfc_get_char_type (dkind);
5422 slen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
5423 fold_convert (size_type_node, slen),
5424 fold_convert (size_type_node,
5425 TYPE_SIZE_UNIT (chartype)));
5426 dlen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
5427 fold_convert (size_type_node, dlen),
5428 fold_convert (size_type_node,
5429 TYPE_SIZE_UNIT (chartype)));
5431 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
5432 dest = fold_convert (pvoid_type_node, dest);
5433 else
5434 dest = gfc_build_addr_expr (pvoid_type_node, dest);
5436 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
5437 src = fold_convert (pvoid_type_node, src);
5438 else
5439 src = gfc_build_addr_expr (pvoid_type_node, src);
5441 /* Truncate string if source is too long. */
5442 cond2 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, slen,
5443 dlen);
5444 tmp2 = build_call_expr_loc (input_location,
5445 builtin_decl_explicit (BUILT_IN_MEMMOVE),
5446 3, dest, src, dlen);
5448 /* Else copy and pad with spaces. */
5449 tmp3 = build_call_expr_loc (input_location,
5450 builtin_decl_explicit (BUILT_IN_MEMMOVE),
5451 3, dest, src, slen);
5453 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
5454 tmp4 = fill_with_spaces (tmp4, chartype,
5455 fold_build2_loc (input_location, MINUS_EXPR,
5456 TREE_TYPE(dlen), dlen, slen));
5458 gfc_init_block (&tempblock);
5459 gfc_add_expr_to_block (&tempblock, tmp3);
5460 gfc_add_expr_to_block (&tempblock, tmp4);
5461 tmp3 = gfc_finish_block (&tempblock);
5463 /* The whole copy_string function is there. */
5464 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
5465 tmp2, tmp3);
5466 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
5467 build_empty_stmt (input_location));
5468 gfc_add_expr_to_block (block, tmp);
5472 /* Translate a statement function.
5473 The value of a statement function reference is obtained by evaluating the
5474 expression using the values of the actual arguments for the values of the
5475 corresponding dummy arguments. */
5477 static void
5478 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
5480 gfc_symbol *sym;
5481 gfc_symbol *fsym;
5482 gfc_formal_arglist *fargs;
5483 gfc_actual_arglist *args;
5484 gfc_se lse;
5485 gfc_se rse;
5486 gfc_saved_var *saved_vars;
5487 tree *temp_vars;
5488 tree type;
5489 tree tmp;
5490 int n;
5492 sym = expr->symtree->n.sym;
5493 args = expr->value.function.actual;
5494 gfc_init_se (&lse, NULL);
5495 gfc_init_se (&rse, NULL);
5497 n = 0;
5498 for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
5499 n++;
5500 saved_vars = XCNEWVEC (gfc_saved_var, n);
5501 temp_vars = XCNEWVEC (tree, n);
5503 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
5504 fargs = fargs->next, n++)
5506 /* Each dummy shall be specified, explicitly or implicitly, to be
5507 scalar. */
5508 gcc_assert (fargs->sym->attr.dimension == 0);
5509 fsym = fargs->sym;
5511 if (fsym->ts.type == BT_CHARACTER)
5513 /* Copy string arguments. */
5514 tree arglen;
5516 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
5517 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
5519 /* Create a temporary to hold the value. */
5520 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
5521 fsym->ts.u.cl->backend_decl
5522 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
5524 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
5525 temp_vars[n] = gfc_create_var (type, fsym->name);
5527 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
5529 gfc_conv_expr (&rse, args->expr);
5530 gfc_conv_string_parameter (&rse);
5531 gfc_add_block_to_block (&se->pre, &lse.pre);
5532 gfc_add_block_to_block (&se->pre, &rse.pre);
5534 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
5535 rse.string_length, rse.expr, fsym->ts.kind);
5536 gfc_add_block_to_block (&se->pre, &lse.post);
5537 gfc_add_block_to_block (&se->pre, &rse.post);
5539 else
5541 /* For everything else, just evaluate the expression. */
5543 /* Create a temporary to hold the value. */
5544 type = gfc_typenode_for_spec (&fsym->ts);
5545 temp_vars[n] = gfc_create_var (type, fsym->name);
5547 gfc_conv_expr (&lse, args->expr);
5549 gfc_add_block_to_block (&se->pre, &lse.pre);
5550 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
5551 gfc_add_block_to_block (&se->pre, &lse.post);
5554 args = args->next;
5557 /* Use the temporary variables in place of the real ones. */
5558 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
5559 fargs = fargs->next, n++)
5560 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
5562 gfc_conv_expr (se, sym->value);
5564 if (sym->ts.type == BT_CHARACTER)
5566 gfc_conv_const_charlen (sym->ts.u.cl);
5568 /* Force the expression to the correct length. */
5569 if (!INTEGER_CST_P (se->string_length)
5570 || tree_int_cst_lt (se->string_length,
5571 sym->ts.u.cl->backend_decl))
5573 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
5574 tmp = gfc_create_var (type, sym->name);
5575 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
5576 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
5577 sym->ts.kind, se->string_length, se->expr,
5578 sym->ts.kind);
5579 se->expr = tmp;
5581 se->string_length = sym->ts.u.cl->backend_decl;
5584 /* Restore the original variables. */
5585 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
5586 fargs = fargs->next, n++)
5587 gfc_restore_sym (fargs->sym, &saved_vars[n]);
5588 free (temp_vars);
5589 free (saved_vars);
5593 /* Translate a function expression. */
5595 static void
5596 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
5598 gfc_symbol *sym;
5600 if (expr->value.function.isym)
5602 gfc_conv_intrinsic_function (se, expr);
5603 return;
5606 /* expr.value.function.esym is the resolved (specific) function symbol for
5607 most functions. However this isn't set for dummy procedures. */
5608 sym = expr->value.function.esym;
5609 if (!sym)
5610 sym = expr->symtree->n.sym;
5612 /* We distinguish statement functions from general functions to improve
5613 runtime performance. */
5614 if (sym->attr.proc == PROC_ST_FUNCTION)
5616 gfc_conv_statement_function (se, expr);
5617 return;
5620 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
5621 NULL);
5625 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
5627 static bool
5628 is_zero_initializer_p (gfc_expr * expr)
5630 if (expr->expr_type != EXPR_CONSTANT)
5631 return false;
5633 /* We ignore constants with prescribed memory representations for now. */
5634 if (expr->representation.string)
5635 return false;
5637 switch (expr->ts.type)
5639 case BT_INTEGER:
5640 return mpz_cmp_si (expr->value.integer, 0) == 0;
5642 case BT_REAL:
5643 return mpfr_zero_p (expr->value.real)
5644 && MPFR_SIGN (expr->value.real) >= 0;
5646 case BT_LOGICAL:
5647 return expr->value.logical == 0;
5649 case BT_COMPLEX:
5650 return mpfr_zero_p (mpc_realref (expr->value.complex))
5651 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
5652 && mpfr_zero_p (mpc_imagref (expr->value.complex))
5653 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
5655 default:
5656 break;
5658 return false;
5662 static void
5663 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
5665 gfc_ss *ss;
5667 ss = se->ss;
5668 gcc_assert (ss != NULL && ss != gfc_ss_terminator);
5669 gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
5671 gfc_conv_tmp_array_ref (se);
5675 /* Build a static initializer. EXPR is the expression for the initial value.
5676 The other parameters describe the variable of the component being
5677 initialized. EXPR may be null. */
5679 tree
5680 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
5681 bool array, bool pointer, bool procptr)
5683 gfc_se se;
5685 if (!(expr || pointer || procptr))
5686 return NULL_TREE;
5688 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
5689 (these are the only two iso_c_binding derived types that can be
5690 used as initialization expressions). If so, we need to modify
5691 the 'expr' to be that for a (void *). */
5692 if (expr != NULL && expr->ts.type == BT_DERIVED
5693 && expr->ts.is_iso_c && expr->ts.u.derived)
5695 gfc_symbol *derived = expr->ts.u.derived;
5697 /* The derived symbol has already been converted to a (void *). Use
5698 its kind. */
5699 expr = gfc_get_int_expr (derived->ts.kind, NULL, 0);
5700 expr->ts.f90_type = derived->ts.f90_type;
5702 gfc_init_se (&se, NULL);
5703 gfc_conv_constant (&se, expr);
5704 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
5705 return se.expr;
5708 if (array && !procptr)
5710 tree ctor;
5711 /* Arrays need special handling. */
5712 if (pointer)
5713 ctor = gfc_build_null_descriptor (type);
5714 /* Special case assigning an array to zero. */
5715 else if (is_zero_initializer_p (expr))
5716 ctor = build_constructor (type, NULL);
5717 else
5718 ctor = gfc_conv_array_initializer (type, expr);
5719 TREE_STATIC (ctor) = 1;
5720 return ctor;
5722 else if (pointer || procptr)
5724 if (ts->type == BT_CLASS && !procptr)
5726 gfc_init_se (&se, NULL);
5727 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
5728 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
5729 TREE_STATIC (se.expr) = 1;
5730 return se.expr;
5732 else if (!expr || expr->expr_type == EXPR_NULL)
5733 return fold_convert (type, null_pointer_node);
5734 else
5736 gfc_init_se (&se, NULL);
5737 se.want_pointer = 1;
5738 gfc_conv_expr (&se, expr);
5739 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
5740 return se.expr;
5743 else
5745 switch (ts->type)
5747 case BT_DERIVED:
5748 case BT_CLASS:
5749 gfc_init_se (&se, NULL);
5750 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
5751 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
5752 else
5753 gfc_conv_structure (&se, expr, 1);
5754 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
5755 TREE_STATIC (se.expr) = 1;
5756 return se.expr;
5758 case BT_CHARACTER:
5760 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
5761 TREE_STATIC (ctor) = 1;
5762 return ctor;
5765 default:
5766 gfc_init_se (&se, NULL);
5767 gfc_conv_constant (&se, expr);
5768 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
5769 return se.expr;
5774 static tree
5775 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
5777 gfc_se rse;
5778 gfc_se lse;
5779 gfc_ss *rss;
5780 gfc_ss *lss;
5781 gfc_array_info *lss_array;
5782 stmtblock_t body;
5783 stmtblock_t block;
5784 gfc_loopinfo loop;
5785 int n;
5786 tree tmp;
5788 gfc_start_block (&block);
5790 /* Initialize the scalarizer. */
5791 gfc_init_loopinfo (&loop);
5793 gfc_init_se (&lse, NULL);
5794 gfc_init_se (&rse, NULL);
5796 /* Walk the rhs. */
5797 rss = gfc_walk_expr (expr);
5798 if (rss == gfc_ss_terminator)
5799 /* The rhs is scalar. Add a ss for the expression. */
5800 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
5802 /* Create a SS for the destination. */
5803 lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
5804 GFC_SS_COMPONENT);
5805 lss_array = &lss->info->data.array;
5806 lss_array->shape = gfc_get_shape (cm->as->rank);
5807 lss_array->descriptor = dest;
5808 lss_array->data = gfc_conv_array_data (dest);
5809 lss_array->offset = gfc_conv_array_offset (dest);
5810 for (n = 0; n < cm->as->rank; n++)
5812 lss_array->start[n] = gfc_conv_array_lbound (dest, n);
5813 lss_array->stride[n] = gfc_index_one_node;
5815 mpz_init (lss_array->shape[n]);
5816 mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
5817 cm->as->lower[n]->value.integer);
5818 mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
5821 /* Associate the SS with the loop. */
5822 gfc_add_ss_to_loop (&loop, lss);
5823 gfc_add_ss_to_loop (&loop, rss);
5825 /* Calculate the bounds of the scalarization. */
5826 gfc_conv_ss_startstride (&loop);
5828 /* Setup the scalarizing loops. */
5829 gfc_conv_loop_setup (&loop, &expr->where);
5831 /* Setup the gfc_se structures. */
5832 gfc_copy_loopinfo_to_se (&lse, &loop);
5833 gfc_copy_loopinfo_to_se (&rse, &loop);
5835 rse.ss = rss;
5836 gfc_mark_ss_chain_used (rss, 1);
5837 lse.ss = lss;
5838 gfc_mark_ss_chain_used (lss, 1);
5840 /* Start the scalarized loop body. */
5841 gfc_start_scalarized_body (&loop, &body);
5843 gfc_conv_tmp_array_ref (&lse);
5844 if (cm->ts.type == BT_CHARACTER)
5845 lse.string_length = cm->ts.u.cl->backend_decl;
5847 gfc_conv_expr (&rse, expr);
5849 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false, true);
5850 gfc_add_expr_to_block (&body, tmp);
5852 gcc_assert (rse.ss == gfc_ss_terminator);
5854 /* Generate the copying loops. */
5855 gfc_trans_scalarizing_loops (&loop, &body);
5857 /* Wrap the whole thing up. */
5858 gfc_add_block_to_block (&block, &loop.pre);
5859 gfc_add_block_to_block (&block, &loop.post);
5861 gcc_assert (lss_array->shape != NULL);
5862 gfc_free_shape (&lss_array->shape, cm->as->rank);
5863 gfc_cleanup_loop (&loop);
5865 return gfc_finish_block (&block);
5869 static tree
5870 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
5871 gfc_expr * expr)
5873 gfc_se se;
5874 stmtblock_t block;
5875 tree offset;
5876 int n;
5877 tree tmp;
5878 tree tmp2;
5879 gfc_array_spec *as;
5880 gfc_expr *arg = NULL;
5882 gfc_start_block (&block);
5883 gfc_init_se (&se, NULL);
5885 /* Get the descriptor for the expressions. */
5886 se.want_pointer = 0;
5887 gfc_conv_expr_descriptor (&se, expr);
5888 gfc_add_block_to_block (&block, &se.pre);
5889 gfc_add_modify (&block, dest, se.expr);
5891 /* Deal with arrays of derived types with allocatable components. */
5892 if (cm->ts.type == BT_DERIVED
5893 && cm->ts.u.derived->attr.alloc_comp)
5894 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
5895 se.expr, dest,
5896 cm->as->rank);
5897 else
5898 tmp = gfc_duplicate_allocatable (dest, se.expr,
5899 TREE_TYPE(cm->backend_decl),
5900 cm->as->rank);
5902 gfc_add_expr_to_block (&block, tmp);
5903 gfc_add_block_to_block (&block, &se.post);
5905 if (expr->expr_type != EXPR_VARIABLE)
5906 gfc_conv_descriptor_data_set (&block, se.expr,
5907 null_pointer_node);
5909 /* We need to know if the argument of a conversion function is a
5910 variable, so that the correct lower bound can be used. */
5911 if (expr->expr_type == EXPR_FUNCTION
5912 && expr->value.function.isym
5913 && expr->value.function.isym->conversion
5914 && expr->value.function.actual->expr
5915 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
5916 arg = expr->value.function.actual->expr;
5918 /* Obtain the array spec of full array references. */
5919 if (arg)
5920 as = gfc_get_full_arrayspec_from_expr (arg);
5921 else
5922 as = gfc_get_full_arrayspec_from_expr (expr);
5924 /* Shift the lbound and ubound of temporaries to being unity,
5925 rather than zero, based. Always calculate the offset. */
5926 offset = gfc_conv_descriptor_offset_get (dest);
5927 gfc_add_modify (&block, offset, gfc_index_zero_node);
5928 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
5930 for (n = 0; n < expr->rank; n++)
5932 tree span;
5933 tree lbound;
5935 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
5936 TODO It looks as if gfc_conv_expr_descriptor should return
5937 the correct bounds and that the following should not be
5938 necessary. This would simplify gfc_conv_intrinsic_bound
5939 as well. */
5940 if (as && as->lower[n])
5942 gfc_se lbse;
5943 gfc_init_se (&lbse, NULL);
5944 gfc_conv_expr (&lbse, as->lower[n]);
5945 gfc_add_block_to_block (&block, &lbse.pre);
5946 lbound = gfc_evaluate_now (lbse.expr, &block);
5948 else if (as && arg)
5950 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
5951 lbound = gfc_conv_descriptor_lbound_get (tmp,
5952 gfc_rank_cst[n]);
5954 else if (as)
5955 lbound = gfc_conv_descriptor_lbound_get (dest,
5956 gfc_rank_cst[n]);
5957 else
5958 lbound = gfc_index_one_node;
5960 lbound = fold_convert (gfc_array_index_type, lbound);
5962 /* Shift the bounds and set the offset accordingly. */
5963 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
5964 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5965 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
5966 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5967 span, lbound);
5968 gfc_conv_descriptor_ubound_set (&block, dest,
5969 gfc_rank_cst[n], tmp);
5970 gfc_conv_descriptor_lbound_set (&block, dest,
5971 gfc_rank_cst[n], lbound);
5973 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5974 gfc_conv_descriptor_lbound_get (dest,
5975 gfc_rank_cst[n]),
5976 gfc_conv_descriptor_stride_get (dest,
5977 gfc_rank_cst[n]));
5978 gfc_add_modify (&block, tmp2, tmp);
5979 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5980 offset, tmp2);
5981 gfc_conv_descriptor_offset_set (&block, dest, tmp);
5984 if (arg)
5986 /* If a conversion expression has a null data pointer
5987 argument, nullify the allocatable component. */
5988 tree non_null_expr;
5989 tree null_expr;
5991 if (arg->symtree->n.sym->attr.allocatable
5992 || arg->symtree->n.sym->attr.pointer)
5994 non_null_expr = gfc_finish_block (&block);
5995 gfc_start_block (&block);
5996 gfc_conv_descriptor_data_set (&block, dest,
5997 null_pointer_node);
5998 null_expr = gfc_finish_block (&block);
5999 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
6000 tmp = build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
6001 fold_convert (TREE_TYPE (tmp), null_pointer_node));
6002 return build3_v (COND_EXPR, tmp,
6003 null_expr, non_null_expr);
6007 return gfc_finish_block (&block);
6011 /* Assign a single component of a derived type constructor. */
6013 static tree
6014 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr)
6016 gfc_se se;
6017 gfc_se lse;
6018 stmtblock_t block;
6019 tree tmp;
6021 gfc_start_block (&block);
6023 if (cm->attr.pointer || cm->attr.proc_pointer)
6025 gfc_init_se (&se, NULL);
6026 /* Pointer component. */
6027 if ((cm->attr.dimension || cm->attr.codimension)
6028 && !cm->attr.proc_pointer)
6030 /* Array pointer. */
6031 if (expr->expr_type == EXPR_NULL)
6032 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
6033 else
6035 se.direct_byref = 1;
6036 se.expr = dest;
6037 gfc_conv_expr_descriptor (&se, expr);
6038 gfc_add_block_to_block (&block, &se.pre);
6039 gfc_add_block_to_block (&block, &se.post);
6042 else
6044 /* Scalar pointers. */
6045 se.want_pointer = 1;
6046 gfc_conv_expr (&se, expr);
6047 gfc_add_block_to_block (&block, &se.pre);
6049 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
6050 && expr->symtree->n.sym->attr.dummy)
6051 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
6053 gfc_add_modify (&block, dest,
6054 fold_convert (TREE_TYPE (dest), se.expr));
6055 gfc_add_block_to_block (&block, &se.post);
6058 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
6060 /* NULL initialization for CLASS components. */
6061 tmp = gfc_trans_structure_assign (dest,
6062 gfc_class_initializer (&cm->ts, expr));
6063 gfc_add_expr_to_block (&block, tmp);
6065 else if ((cm->attr.dimension || cm->attr.codimension)
6066 && !cm->attr.proc_pointer)
6068 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
6069 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
6070 else if (cm->attr.allocatable)
6072 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
6073 gfc_add_expr_to_block (&block, tmp);
6075 else
6077 tmp = gfc_trans_subarray_assign (dest, cm, expr);
6078 gfc_add_expr_to_block (&block, tmp);
6081 else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
6083 if (expr->expr_type != EXPR_STRUCTURE)
6085 gfc_init_se (&se, NULL);
6086 gfc_conv_expr (&se, expr);
6087 gfc_add_block_to_block (&block, &se.pre);
6088 gfc_add_modify (&block, dest,
6089 fold_convert (TREE_TYPE (dest), se.expr));
6090 gfc_add_block_to_block (&block, &se.post);
6092 else
6094 /* Nested constructors. */
6095 tmp = gfc_trans_structure_assign (dest, expr);
6096 gfc_add_expr_to_block (&block, tmp);
6099 else if (gfc_deferred_strlen (cm, &tmp))
6101 tree strlen;
6102 strlen = tmp;
6103 gcc_assert (strlen);
6104 strlen = fold_build3_loc (input_location, COMPONENT_REF,
6105 TREE_TYPE (strlen),
6106 TREE_OPERAND (dest, 0),
6107 strlen, NULL_TREE);
6109 if (expr->expr_type == EXPR_NULL)
6111 tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
6112 gfc_add_modify (&block, dest, tmp);
6113 tmp = build_int_cst (TREE_TYPE (strlen), 0);
6114 gfc_add_modify (&block, strlen, tmp);
6116 else
6118 tree size;
6119 gfc_init_se (&se, NULL);
6120 gfc_conv_expr (&se, expr);
6121 size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
6122 tmp = build_call_expr_loc (input_location,
6123 builtin_decl_explicit (BUILT_IN_MALLOC),
6124 1, size);
6125 gfc_add_modify (&block, dest,
6126 fold_convert (TREE_TYPE (dest), tmp));
6127 gfc_add_modify (&block, strlen, se.string_length);
6128 tmp = gfc_build_memcpy_call (dest, se.expr, size);
6129 gfc_add_expr_to_block (&block, tmp);
6132 else if (!cm->attr.deferred_parameter)
6134 /* Scalar component (excluding deferred parameters). */
6135 gfc_init_se (&se, NULL);
6136 gfc_init_se (&lse, NULL);
6138 gfc_conv_expr (&se, expr);
6139 if (cm->ts.type == BT_CHARACTER)
6140 lse.string_length = cm->ts.u.cl->backend_decl;
6141 lse.expr = dest;
6142 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, true, false, true);
6143 gfc_add_expr_to_block (&block, tmp);
6145 return gfc_finish_block (&block);
6148 /* Assign a derived type constructor to a variable. */
6150 static tree
6151 gfc_trans_structure_assign (tree dest, gfc_expr * expr)
6153 gfc_constructor *c;
6154 gfc_component *cm;
6155 stmtblock_t block;
6156 tree field;
6157 tree tmp;
6159 gfc_start_block (&block);
6160 cm = expr->ts.u.derived->components;
6162 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
6163 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
6164 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
6166 gfc_se se, lse;
6168 gcc_assert (cm->backend_decl == NULL);
6169 gfc_init_se (&se, NULL);
6170 gfc_init_se (&lse, NULL);
6171 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
6172 lse.expr = dest;
6173 gfc_add_modify (&block, lse.expr,
6174 fold_convert (TREE_TYPE (lse.expr), se.expr));
6176 return gfc_finish_block (&block);
6179 for (c = gfc_constructor_first (expr->value.constructor);
6180 c; c = gfc_constructor_next (c), cm = cm->next)
6182 /* Skip absent members in default initializers. */
6183 if (!c->expr)
6184 continue;
6186 field = cm->backend_decl;
6187 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
6188 dest, field, NULL_TREE);
6189 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr);
6190 gfc_add_expr_to_block (&block, tmp);
6192 return gfc_finish_block (&block);
6195 /* Build an expression for a constructor. If init is nonzero then
6196 this is part of a static variable initializer. */
6198 void
6199 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
6201 gfc_constructor *c;
6202 gfc_component *cm;
6203 tree val;
6204 tree type;
6205 tree tmp;
6206 vec<constructor_elt, va_gc> *v = NULL;
6208 gcc_assert (se->ss == NULL);
6209 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
6210 type = gfc_typenode_for_spec (&expr->ts);
6212 if (!init)
6214 /* Create a temporary variable and fill it in. */
6215 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
6216 tmp = gfc_trans_structure_assign (se->expr, expr);
6217 gfc_add_expr_to_block (&se->pre, tmp);
6218 return;
6221 cm = expr->ts.u.derived->components;
6223 for (c = gfc_constructor_first (expr->value.constructor);
6224 c; c = gfc_constructor_next (c), cm = cm->next)
6226 /* Skip absent members in default initializers and allocatable
6227 components. Although the latter have a default initializer
6228 of EXPR_NULL,... by default, the static nullify is not needed
6229 since this is done every time we come into scope. */
6230 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
6231 continue;
6233 if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
6234 && strcmp (cm->name, "_extends") == 0
6235 && cm->initializer->symtree)
6237 tree vtab;
6238 gfc_symbol *vtabs;
6239 vtabs = cm->initializer->symtree->n.sym;
6240 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
6241 vtab = unshare_expr_without_location (vtab);
6242 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
6244 else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
6246 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
6247 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
6249 else
6251 val = gfc_conv_initializer (c->expr, &cm->ts,
6252 TREE_TYPE (cm->backend_decl),
6253 cm->attr.dimension, cm->attr.pointer,
6254 cm->attr.proc_pointer);
6255 val = unshare_expr_without_location (val);
6257 /* Append it to the constructor list. */
6258 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
6261 se->expr = build_constructor (type, v);
6262 if (init)
6263 TREE_CONSTANT (se->expr) = 1;
6267 /* Translate a substring expression. */
6269 static void
6270 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
6272 gfc_ref *ref;
6274 ref = expr->ref;
6276 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
6278 se->expr = gfc_build_wide_string_const (expr->ts.kind,
6279 expr->value.character.length,
6280 expr->value.character.string);
6282 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
6283 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
6285 if (ref)
6286 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
6290 /* Entry point for expression translation. Evaluates a scalar quantity.
6291 EXPR is the expression to be translated, and SE is the state structure if
6292 called from within the scalarized. */
6294 void
6295 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
6297 gfc_ss *ss;
6299 ss = se->ss;
6300 if (ss && ss->info->expr == expr
6301 && (ss->info->type == GFC_SS_SCALAR
6302 || ss->info->type == GFC_SS_REFERENCE))
6304 gfc_ss_info *ss_info;
6306 ss_info = ss->info;
6307 /* Substitute a scalar expression evaluated outside the scalarization
6308 loop. */
6309 se->expr = ss_info->data.scalar.value;
6310 /* If the reference can be NULL, the value field contains the reference,
6311 not the value the reference points to (see gfc_add_loop_ss_code). */
6312 if (ss_info->can_be_null_ref)
6313 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
6315 se->string_length = ss_info->string_length;
6316 gfc_advance_se_ss_chain (se);
6317 return;
6320 /* We need to convert the expressions for the iso_c_binding derived types.
6321 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
6322 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
6323 typespec for the C_PTR and C_FUNPTR symbols, which has already been
6324 updated to be an integer with a kind equal to the size of a (void *). */
6325 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID)
6327 if (expr->expr_type == EXPR_VARIABLE
6328 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
6329 || expr->symtree->n.sym->intmod_sym_id
6330 == ISOCBINDING_NULL_FUNPTR))
6332 /* Set expr_type to EXPR_NULL, which will result in
6333 null_pointer_node being used below. */
6334 expr->expr_type = EXPR_NULL;
6336 else
6338 /* Update the type/kind of the expression to be what the new
6339 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
6340 expr->ts.type = BT_INTEGER;
6341 expr->ts.f90_type = BT_VOID;
6342 expr->ts.kind = gfc_index_integer_kind;
6346 gfc_fix_class_refs (expr);
6348 switch (expr->expr_type)
6350 case EXPR_OP:
6351 gfc_conv_expr_op (se, expr);
6352 break;
6354 case EXPR_FUNCTION:
6355 gfc_conv_function_expr (se, expr);
6356 break;
6358 case EXPR_CONSTANT:
6359 gfc_conv_constant (se, expr);
6360 break;
6362 case EXPR_VARIABLE:
6363 gfc_conv_variable (se, expr);
6364 break;
6366 case EXPR_NULL:
6367 se->expr = null_pointer_node;
6368 break;
6370 case EXPR_SUBSTRING:
6371 gfc_conv_substring_expr (se, expr);
6372 break;
6374 case EXPR_STRUCTURE:
6375 gfc_conv_structure (se, expr, 0);
6376 break;
6378 case EXPR_ARRAY:
6379 gfc_conv_array_constructor_expr (se, expr);
6380 break;
6382 default:
6383 gcc_unreachable ();
6384 break;
6388 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
6389 of an assignment. */
6390 void
6391 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
6393 gfc_conv_expr (se, expr);
6394 /* All numeric lvalues should have empty post chains. If not we need to
6395 figure out a way of rewriting an lvalue so that it has no post chain. */
6396 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
6399 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
6400 numeric expressions. Used for scalar values where inserting cleanup code
6401 is inconvenient. */
6402 void
6403 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
6405 tree val;
6407 gcc_assert (expr->ts.type != BT_CHARACTER);
6408 gfc_conv_expr (se, expr);
6409 if (se->post.head)
6411 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
6412 gfc_add_modify (&se->pre, val, se->expr);
6413 se->expr = val;
6414 gfc_add_block_to_block (&se->pre, &se->post);
6418 /* Helper to translate an expression and convert it to a particular type. */
6419 void
6420 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
6422 gfc_conv_expr_val (se, expr);
6423 se->expr = convert (type, se->expr);
6427 /* Converts an expression so that it can be passed by reference. Scalar
6428 values only. */
6430 void
6431 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
6433 gfc_ss *ss;
6434 tree var;
6436 ss = se->ss;
6437 if (ss && ss->info->expr == expr
6438 && ss->info->type == GFC_SS_REFERENCE)
6440 /* Returns a reference to the scalar evaluated outside the loop
6441 for this case. */
6442 gfc_conv_expr (se, expr);
6444 if (expr->ts.type == BT_CHARACTER
6445 && expr->expr_type != EXPR_FUNCTION)
6446 gfc_conv_string_parameter (se);
6447 else
6448 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
6450 return;
6453 if (expr->ts.type == BT_CHARACTER)
6455 gfc_conv_expr (se, expr);
6456 gfc_conv_string_parameter (se);
6457 return;
6460 if (expr->expr_type == EXPR_VARIABLE)
6462 se->want_pointer = 1;
6463 gfc_conv_expr (se, expr);
6464 if (se->post.head)
6466 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
6467 gfc_add_modify (&se->pre, var, se->expr);
6468 gfc_add_block_to_block (&se->pre, &se->post);
6469 se->expr = var;
6471 return;
6474 if (expr->expr_type == EXPR_FUNCTION
6475 && ((expr->value.function.esym
6476 && expr->value.function.esym->result->attr.pointer
6477 && !expr->value.function.esym->result->attr.dimension)
6478 || (!expr->value.function.esym && !expr->ref
6479 && expr->symtree->n.sym->attr.pointer
6480 && !expr->symtree->n.sym->attr.dimension)))
6482 se->want_pointer = 1;
6483 gfc_conv_expr (se, expr);
6484 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
6485 gfc_add_modify (&se->pre, var, se->expr);
6486 se->expr = var;
6487 return;
6490 gfc_conv_expr (se, expr);
6492 /* Create a temporary var to hold the value. */
6493 if (TREE_CONSTANT (se->expr))
6495 tree tmp = se->expr;
6496 STRIP_TYPE_NOPS (tmp);
6497 var = build_decl (input_location,
6498 CONST_DECL, NULL, TREE_TYPE (tmp));
6499 DECL_INITIAL (var) = tmp;
6500 TREE_STATIC (var) = 1;
6501 pushdecl (var);
6503 else
6505 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
6506 gfc_add_modify (&se->pre, var, se->expr);
6508 gfc_add_block_to_block (&se->pre, &se->post);
6510 /* Take the address of that value. */
6511 se->expr = gfc_build_addr_expr (NULL_TREE, var);
6512 if (expr->ts.type == BT_DERIVED && expr->rank
6513 && !gfc_is_finalizable (expr->ts.u.derived, NULL)
6514 && expr->ts.u.derived->attr.alloc_comp
6515 && expr->expr_type != EXPR_VARIABLE)
6517 tree tmp;
6519 tmp = build_fold_indirect_ref_loc (input_location, se->expr);
6520 tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
6522 /* The components shall be deallocated before
6523 their containing entity. */
6524 gfc_prepend_expr_to_block (&se->post, tmp);
6529 tree
6530 gfc_trans_pointer_assign (gfc_code * code)
6532 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
6536 /* Generate code for a pointer assignment. */
6538 tree
6539 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
6541 gfc_expr *expr1_vptr = NULL;
6542 gfc_se lse;
6543 gfc_se rse;
6544 stmtblock_t block;
6545 tree desc;
6546 tree tmp;
6547 tree decl;
6548 bool scalar;
6549 gfc_ss *ss;
6551 gfc_start_block (&block);
6553 gfc_init_se (&lse, NULL);
6555 /* Check whether the expression is a scalar or not; we cannot use
6556 expr1->rank as it can be nonzero for proc pointers. */
6557 ss = gfc_walk_expr (expr1);
6558 scalar = ss == gfc_ss_terminator;
6559 if (!scalar)
6560 gfc_free_ss_chain (ss);
6562 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
6563 && expr2->expr_type != EXPR_FUNCTION)
6565 gfc_add_data_component (expr2);
6566 /* The following is required as gfc_add_data_component doesn't
6567 update ts.type if there is a tailing REF_ARRAY. */
6568 expr2->ts.type = BT_DERIVED;
6571 if (scalar)
6573 /* Scalar pointers. */
6574 lse.want_pointer = 1;
6575 gfc_conv_expr (&lse, expr1);
6576 gfc_init_se (&rse, NULL);
6577 rse.want_pointer = 1;
6578 gfc_conv_expr (&rse, expr2);
6580 if (expr1->symtree->n.sym->attr.proc_pointer
6581 && expr1->symtree->n.sym->attr.dummy)
6582 lse.expr = build_fold_indirect_ref_loc (input_location,
6583 lse.expr);
6585 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
6586 && expr2->symtree->n.sym->attr.dummy)
6587 rse.expr = build_fold_indirect_ref_loc (input_location,
6588 rse.expr);
6590 gfc_add_block_to_block (&block, &lse.pre);
6591 gfc_add_block_to_block (&block, &rse.pre);
6593 /* Check character lengths if character expression. The test is only
6594 really added if -fbounds-check is enabled. Exclude deferred
6595 character length lefthand sides. */
6596 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
6597 && !expr1->ts.deferred
6598 && !expr1->symtree->n.sym->attr.proc_pointer
6599 && !gfc_is_proc_ptr_comp (expr1))
6601 gcc_assert (expr2->ts.type == BT_CHARACTER);
6602 gcc_assert (lse.string_length && rse.string_length);
6603 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
6604 lse.string_length, rse.string_length,
6605 &block);
6608 /* The assignment to an deferred character length sets the string
6609 length to that of the rhs. */
6610 if (expr1->ts.deferred)
6612 if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
6613 gfc_add_modify (&block, lse.string_length, rse.string_length);
6614 else if (lse.string_length != NULL)
6615 gfc_add_modify (&block, lse.string_length,
6616 build_int_cst (gfc_charlen_type_node, 0));
6619 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS)
6620 rse.expr = gfc_class_data_get (rse.expr);
6622 gfc_add_modify (&block, lse.expr,
6623 fold_convert (TREE_TYPE (lse.expr), rse.expr));
6625 gfc_add_block_to_block (&block, &rse.post);
6626 gfc_add_block_to_block (&block, &lse.post);
6628 else
6630 gfc_ref* remap;
6631 bool rank_remap;
6632 tree strlen_lhs;
6633 tree strlen_rhs = NULL_TREE;
6635 /* Array pointer. Find the last reference on the LHS and if it is an
6636 array section ref, we're dealing with bounds remapping. In this case,
6637 set it to AR_FULL so that gfc_conv_expr_descriptor does
6638 not see it and process the bounds remapping afterwards explicitly. */
6639 for (remap = expr1->ref; remap; remap = remap->next)
6640 if (!remap->next && remap->type == REF_ARRAY
6641 && remap->u.ar.type == AR_SECTION)
6642 break;
6643 rank_remap = (remap && remap->u.ar.end[0]);
6645 gfc_init_se (&lse, NULL);
6646 if (remap)
6647 lse.descriptor_only = 1;
6648 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS
6649 && expr1->ts.type == BT_CLASS)
6650 expr1_vptr = gfc_copy_expr (expr1);
6651 gfc_conv_expr_descriptor (&lse, expr1);
6652 strlen_lhs = lse.string_length;
6653 desc = lse.expr;
6655 if (expr2->expr_type == EXPR_NULL)
6657 /* Just set the data pointer to null. */
6658 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
6660 else if (rank_remap)
6662 /* If we are rank-remapping, just get the RHS's descriptor and
6663 process this later on. */
6664 gfc_init_se (&rse, NULL);
6665 rse.direct_byref = 1;
6666 rse.byref_noassign = 1;
6668 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
6670 gfc_conv_function_expr (&rse, expr2);
6672 if (expr1->ts.type != BT_CLASS)
6673 rse.expr = gfc_class_data_get (rse.expr);
6674 else
6676 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
6677 gfc_add_modify (&lse.pre, tmp, rse.expr);
6679 gfc_add_vptr_component (expr1_vptr);
6680 gfc_init_se (&rse, NULL);
6681 rse.want_pointer = 1;
6682 gfc_conv_expr (&rse, expr1_vptr);
6683 gfc_add_modify (&lse.pre, rse.expr,
6684 fold_convert (TREE_TYPE (rse.expr),
6685 gfc_class_vptr_get (tmp)));
6686 rse.expr = gfc_class_data_get (tmp);
6689 else if (expr2->expr_type == EXPR_FUNCTION)
6691 tree bound[GFC_MAX_DIMENSIONS];
6692 int i;
6694 for (i = 0; i < expr2->rank; i++)
6695 bound[i] = NULL_TREE;
6696 tmp = gfc_typenode_for_spec (&expr2->ts);
6697 tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
6698 bound, bound, 0,
6699 GFC_ARRAY_POINTER_CONT, false);
6700 tmp = gfc_create_var (tmp, "ptrtemp");
6701 lse.expr = tmp;
6702 lse.direct_byref = 1;
6703 gfc_conv_expr_descriptor (&lse, expr2);
6704 strlen_rhs = lse.string_length;
6705 rse.expr = tmp;
6707 else
6709 gfc_conv_expr_descriptor (&rse, expr2);
6710 strlen_rhs = rse.string_length;
6713 else if (expr2->expr_type == EXPR_VARIABLE)
6715 /* Assign directly to the LHS's descriptor. */
6716 lse.direct_byref = 1;
6717 gfc_conv_expr_descriptor (&lse, expr2);
6718 strlen_rhs = lse.string_length;
6720 /* If this is a subreference array pointer assignment, use the rhs
6721 descriptor element size for the lhs span. */
6722 if (expr1->symtree->n.sym->attr.subref_array_pointer)
6724 decl = expr1->symtree->n.sym->backend_decl;
6725 gfc_init_se (&rse, NULL);
6726 rse.descriptor_only = 1;
6727 gfc_conv_expr (&rse, expr2);
6728 tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
6729 tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
6730 if (!INTEGER_CST_P (tmp))
6731 gfc_add_block_to_block (&lse.post, &rse.pre);
6732 gfc_add_modify (&lse.post, GFC_DECL_SPAN(decl), tmp);
6735 else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
6737 gfc_init_se (&rse, NULL);
6738 rse.want_pointer = 1;
6739 gfc_conv_function_expr (&rse, expr2);
6740 if (expr1->ts.type != BT_CLASS)
6742 rse.expr = gfc_class_data_get (rse.expr);
6743 gfc_add_modify (&lse.pre, desc, rse.expr);
6745 else
6747 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
6748 gfc_add_modify (&lse.pre, tmp, rse.expr);
6750 gfc_add_vptr_component (expr1_vptr);
6751 gfc_init_se (&rse, NULL);
6752 rse.want_pointer = 1;
6753 gfc_conv_expr (&rse, expr1_vptr);
6754 gfc_add_modify (&lse.pre, rse.expr,
6755 fold_convert (TREE_TYPE (rse.expr),
6756 gfc_class_vptr_get (tmp)));
6757 rse.expr = gfc_class_data_get (tmp);
6758 gfc_add_modify (&lse.pre, desc, rse.expr);
6761 else
6763 /* Assign to a temporary descriptor and then copy that
6764 temporary to the pointer. */
6765 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
6766 lse.expr = tmp;
6767 lse.direct_byref = 1;
6768 gfc_conv_expr_descriptor (&lse, expr2);
6769 strlen_rhs = lse.string_length;
6770 gfc_add_modify (&lse.pre, desc, tmp);
6773 if (expr1_vptr)
6774 gfc_free_expr (expr1_vptr);
6776 gfc_add_block_to_block (&block, &lse.pre);
6777 if (rank_remap)
6778 gfc_add_block_to_block (&block, &rse.pre);
6780 /* If we do bounds remapping, update LHS descriptor accordingly. */
6781 if (remap)
6783 int dim;
6784 gcc_assert (remap->u.ar.dimen == expr1->rank);
6786 if (rank_remap)
6788 /* Do rank remapping. We already have the RHS's descriptor
6789 converted in rse and now have to build the correct LHS
6790 descriptor for it. */
6792 tree dtype, data;
6793 tree offs, stride;
6794 tree lbound, ubound;
6796 /* Set dtype. */
6797 dtype = gfc_conv_descriptor_dtype (desc);
6798 tmp = gfc_get_dtype (TREE_TYPE (desc));
6799 gfc_add_modify (&block, dtype, tmp);
6801 /* Copy data pointer. */
6802 data = gfc_conv_descriptor_data_get (rse.expr);
6803 gfc_conv_descriptor_data_set (&block, desc, data);
6805 /* Copy offset but adjust it such that it would correspond
6806 to a lbound of zero. */
6807 offs = gfc_conv_descriptor_offset_get (rse.expr);
6808 for (dim = 0; dim < expr2->rank; ++dim)
6810 stride = gfc_conv_descriptor_stride_get (rse.expr,
6811 gfc_rank_cst[dim]);
6812 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
6813 gfc_rank_cst[dim]);
6814 tmp = fold_build2_loc (input_location, MULT_EXPR,
6815 gfc_array_index_type, stride, lbound);
6816 offs = fold_build2_loc (input_location, PLUS_EXPR,
6817 gfc_array_index_type, offs, tmp);
6819 gfc_conv_descriptor_offset_set (&block, desc, offs);
6821 /* Set the bounds as declared for the LHS and calculate strides as
6822 well as another offset update accordingly. */
6823 stride = gfc_conv_descriptor_stride_get (rse.expr,
6824 gfc_rank_cst[0]);
6825 for (dim = 0; dim < expr1->rank; ++dim)
6827 gfc_se lower_se;
6828 gfc_se upper_se;
6830 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
6832 /* Convert declared bounds. */
6833 gfc_init_se (&lower_se, NULL);
6834 gfc_init_se (&upper_se, NULL);
6835 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
6836 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
6838 gfc_add_block_to_block (&block, &lower_se.pre);
6839 gfc_add_block_to_block (&block, &upper_se.pre);
6841 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
6842 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
6844 lbound = gfc_evaluate_now (lbound, &block);
6845 ubound = gfc_evaluate_now (ubound, &block);
6847 gfc_add_block_to_block (&block, &lower_se.post);
6848 gfc_add_block_to_block (&block, &upper_se.post);
6850 /* Set bounds in descriptor. */
6851 gfc_conv_descriptor_lbound_set (&block, desc,
6852 gfc_rank_cst[dim], lbound);
6853 gfc_conv_descriptor_ubound_set (&block, desc,
6854 gfc_rank_cst[dim], ubound);
6856 /* Set stride. */
6857 stride = gfc_evaluate_now (stride, &block);
6858 gfc_conv_descriptor_stride_set (&block, desc,
6859 gfc_rank_cst[dim], stride);
6861 /* Update offset. */
6862 offs = gfc_conv_descriptor_offset_get (desc);
6863 tmp = fold_build2_loc (input_location, MULT_EXPR,
6864 gfc_array_index_type, lbound, stride);
6865 offs = fold_build2_loc (input_location, MINUS_EXPR,
6866 gfc_array_index_type, offs, tmp);
6867 offs = gfc_evaluate_now (offs, &block);
6868 gfc_conv_descriptor_offset_set (&block, desc, offs);
6870 /* Update stride. */
6871 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
6872 stride = fold_build2_loc (input_location, MULT_EXPR,
6873 gfc_array_index_type, stride, tmp);
6876 else
6878 /* Bounds remapping. Just shift the lower bounds. */
6880 gcc_assert (expr1->rank == expr2->rank);
6882 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
6884 gfc_se lbound_se;
6886 gcc_assert (remap->u.ar.start[dim]);
6887 gcc_assert (!remap->u.ar.end[dim]);
6888 gfc_init_se (&lbound_se, NULL);
6889 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
6891 gfc_add_block_to_block (&block, &lbound_se.pre);
6892 gfc_conv_shift_descriptor_lbound (&block, desc,
6893 dim, lbound_se.expr);
6894 gfc_add_block_to_block (&block, &lbound_se.post);
6899 /* Check string lengths if applicable. The check is only really added
6900 to the output code if -fbounds-check is enabled. */
6901 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
6903 gcc_assert (expr2->ts.type == BT_CHARACTER);
6904 gcc_assert (strlen_lhs && strlen_rhs);
6905 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
6906 strlen_lhs, strlen_rhs, &block);
6909 /* If rank remapping was done, check with -fcheck=bounds that
6910 the target is at least as large as the pointer. */
6911 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
6913 tree lsize, rsize;
6914 tree fault;
6915 const char* msg;
6917 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
6918 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
6920 lsize = gfc_evaluate_now (lsize, &block);
6921 rsize = gfc_evaluate_now (rsize, &block);
6922 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
6923 rsize, lsize);
6925 msg = _("Target of rank remapping is too small (%ld < %ld)");
6926 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
6927 msg, rsize, lsize);
6930 gfc_add_block_to_block (&block, &lse.post);
6931 if (rank_remap)
6932 gfc_add_block_to_block (&block, &rse.post);
6935 return gfc_finish_block (&block);
6939 /* Makes sure se is suitable for passing as a function string parameter. */
6940 /* TODO: Need to check all callers of this function. It may be abused. */
6942 void
6943 gfc_conv_string_parameter (gfc_se * se)
6945 tree type;
6947 if (TREE_CODE (se->expr) == STRING_CST)
6949 type = TREE_TYPE (TREE_TYPE (se->expr));
6950 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
6951 return;
6954 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
6956 if (TREE_CODE (se->expr) != INDIRECT_REF)
6958 type = TREE_TYPE (se->expr);
6959 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
6961 else
6963 type = gfc_get_character_type_len (gfc_default_character_kind,
6964 se->string_length);
6965 type = build_pointer_type (type);
6966 se->expr = gfc_build_addr_expr (type, se->expr);
6970 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
6974 /* Generate code for assignment of scalar variables. Includes character
6975 strings and derived types with allocatable components.
6976 If you know that the LHS has no allocations, set dealloc to false.
6978 DEEP_COPY has no effect if the typespec TS is not a derived type with
6979 allocatable components. Otherwise, if it is set, an explicit copy of each
6980 allocatable component is made. This is necessary as a simple copy of the
6981 whole object would copy array descriptors as is, so that the lhs's
6982 allocatable components would point to the rhs's after the assignment.
6983 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
6984 necessary if the rhs is a non-pointer function, as the allocatable components
6985 are not accessible by other means than the function's result after the
6986 function has returned. It is even more subtle when temporaries are involved,
6987 as the two following examples show:
6988 1. When we evaluate an array constructor, a temporary is created. Thus
6989 there is theoretically no alias possible. However, no deep copy is
6990 made for this temporary, so that if the constructor is made of one or
6991 more variable with allocatable components, those components still point
6992 to the variable's: DEEP_COPY should be set for the assignment from the
6993 temporary to the lhs in that case.
6994 2. When assigning a scalar to an array, we evaluate the scalar value out
6995 of the loop, store it into a temporary variable, and assign from that.
6996 In that case, deep copying when assigning to the temporary would be a
6997 waste of resources; however deep copies should happen when assigning from
6998 the temporary to each array element: again DEEP_COPY should be set for
6999 the assignment from the temporary to the lhs. */
7001 tree
7002 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
7003 bool l_is_temp, bool deep_copy, bool dealloc)
7005 stmtblock_t block;
7006 tree tmp;
7007 tree cond;
7009 gfc_init_block (&block);
7011 if (ts.type == BT_CHARACTER)
7013 tree rlen = NULL;
7014 tree llen = NULL;
7016 if (lse->string_length != NULL_TREE)
7018 gfc_conv_string_parameter (lse);
7019 gfc_add_block_to_block (&block, &lse->pre);
7020 llen = lse->string_length;
7023 if (rse->string_length != NULL_TREE)
7025 gcc_assert (rse->string_length != NULL_TREE);
7026 gfc_conv_string_parameter (rse);
7027 gfc_add_block_to_block (&block, &rse->pre);
7028 rlen = rse->string_length;
7031 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
7032 rse->expr, ts.kind);
7034 else if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
7036 tree tmp_var = NULL_TREE;
7037 cond = NULL_TREE;
7039 /* Are the rhs and the lhs the same? */
7040 if (deep_copy)
7042 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
7043 gfc_build_addr_expr (NULL_TREE, lse->expr),
7044 gfc_build_addr_expr (NULL_TREE, rse->expr));
7045 cond = gfc_evaluate_now (cond, &lse->pre);
7048 /* Deallocate the lhs allocated components as long as it is not
7049 the same as the rhs. This must be done following the assignment
7050 to prevent deallocating data that could be used in the rhs
7051 expression. */
7052 if (!l_is_temp && dealloc)
7054 tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
7055 tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var, 0);
7056 if (deep_copy)
7057 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
7058 tmp);
7059 gfc_add_expr_to_block (&lse->post, tmp);
7062 gfc_add_block_to_block (&block, &rse->pre);
7063 gfc_add_block_to_block (&block, &lse->pre);
7065 gfc_add_modify (&block, lse->expr,
7066 fold_convert (TREE_TYPE (lse->expr), rse->expr));
7068 /* Restore pointer address of coarray components. */
7069 if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
7071 tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
7072 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
7073 tmp);
7074 gfc_add_expr_to_block (&block, tmp);
7077 /* Do a deep copy if the rhs is a variable, if it is not the
7078 same as the lhs. */
7079 if (deep_copy)
7081 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0);
7082 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
7083 tmp);
7084 gfc_add_expr_to_block (&block, tmp);
7087 else if (ts.type == BT_DERIVED || ts.type == BT_CLASS)
7089 gfc_add_block_to_block (&block, &lse->pre);
7090 gfc_add_block_to_block (&block, &rse->pre);
7091 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
7092 TREE_TYPE (lse->expr), rse->expr);
7093 gfc_add_modify (&block, lse->expr, tmp);
7095 else
7097 gfc_add_block_to_block (&block, &lse->pre);
7098 gfc_add_block_to_block (&block, &rse->pre);
7100 gfc_add_modify (&block, lse->expr,
7101 fold_convert (TREE_TYPE (lse->expr), rse->expr));
7104 gfc_add_block_to_block (&block, &lse->post);
7105 gfc_add_block_to_block (&block, &rse->post);
7107 return gfc_finish_block (&block);
7111 /* There are quite a lot of restrictions on the optimisation in using an
7112 array function assign without a temporary. */
7114 static bool
7115 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
7117 gfc_ref * ref;
7118 bool seen_array_ref;
7119 bool c = false;
7120 gfc_symbol *sym = expr1->symtree->n.sym;
7122 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
7123 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
7124 return true;
7126 /* Elemental functions are scalarized so that they don't need a
7127 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
7128 they would need special treatment in gfc_trans_arrayfunc_assign. */
7129 if (expr2->value.function.esym != NULL
7130 && expr2->value.function.esym->attr.elemental)
7131 return true;
7133 /* Need a temporary if rhs is not FULL or a contiguous section. */
7134 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
7135 return true;
7137 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
7138 if (gfc_ref_needs_temporary_p (expr1->ref))
7139 return true;
7141 /* Functions returning pointers or allocatables need temporaries. */
7142 c = expr2->value.function.esym
7143 ? (expr2->value.function.esym->attr.pointer
7144 || expr2->value.function.esym->attr.allocatable)
7145 : (expr2->symtree->n.sym->attr.pointer
7146 || expr2->symtree->n.sym->attr.allocatable);
7147 if (c)
7148 return true;
7150 /* Character array functions need temporaries unless the
7151 character lengths are the same. */
7152 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
7154 if (expr1->ts.u.cl->length == NULL
7155 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
7156 return true;
7158 if (expr2->ts.u.cl->length == NULL
7159 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
7160 return true;
7162 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
7163 expr2->ts.u.cl->length->value.integer) != 0)
7164 return true;
7167 /* Check that no LHS component references appear during an array
7168 reference. This is needed because we do not have the means to
7169 span any arbitrary stride with an array descriptor. This check
7170 is not needed for the rhs because the function result has to be
7171 a complete type. */
7172 seen_array_ref = false;
7173 for (ref = expr1->ref; ref; ref = ref->next)
7175 if (ref->type == REF_ARRAY)
7176 seen_array_ref= true;
7177 else if (ref->type == REF_COMPONENT && seen_array_ref)
7178 return true;
7181 /* Check for a dependency. */
7182 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
7183 expr2->value.function.esym,
7184 expr2->value.function.actual,
7185 NOT_ELEMENTAL))
7186 return true;
7188 /* If we have reached here with an intrinsic function, we do not
7189 need a temporary except in the particular case that reallocation
7190 on assignment is active and the lhs is allocatable and a target. */
7191 if (expr2->value.function.isym)
7192 return (gfc_option.flag_realloc_lhs
7193 && sym->attr.allocatable
7194 && sym->attr.target);
7196 /* If the LHS is a dummy, we need a temporary if it is not
7197 INTENT(OUT). */
7198 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
7199 return true;
7201 /* If the lhs has been host_associated, is in common, a pointer or is
7202 a target and the function is not using a RESULT variable, aliasing
7203 can occur and a temporary is needed. */
7204 if ((sym->attr.host_assoc
7205 || sym->attr.in_common
7206 || sym->attr.pointer
7207 || sym->attr.cray_pointee
7208 || sym->attr.target)
7209 && expr2->symtree != NULL
7210 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
7211 return true;
7213 /* A PURE function can unconditionally be called without a temporary. */
7214 if (expr2->value.function.esym != NULL
7215 && expr2->value.function.esym->attr.pure)
7216 return false;
7218 /* Implicit_pure functions are those which could legally be declared
7219 to be PURE. */
7220 if (expr2->value.function.esym != NULL
7221 && expr2->value.function.esym->attr.implicit_pure)
7222 return false;
7224 if (!sym->attr.use_assoc
7225 && !sym->attr.in_common
7226 && !sym->attr.pointer
7227 && !sym->attr.target
7228 && !sym->attr.cray_pointee
7229 && expr2->value.function.esym)
7231 /* A temporary is not needed if the function is not contained and
7232 the variable is local or host associated and not a pointer or
7233 a target. */
7234 if (!expr2->value.function.esym->attr.contained)
7235 return false;
7237 /* A temporary is not needed if the lhs has never been host
7238 associated and the procedure is contained. */
7239 else if (!sym->attr.host_assoc)
7240 return false;
7242 /* A temporary is not needed if the variable is local and not
7243 a pointer, a target or a result. */
7244 if (sym->ns->parent
7245 && expr2->value.function.esym->ns == sym->ns->parent)
7246 return false;
7249 /* Default to temporary use. */
7250 return true;
7254 /* Provide the loop info so that the lhs descriptor can be built for
7255 reallocatable assignments from extrinsic function calls. */
7257 static void
7258 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
7259 gfc_loopinfo *loop)
7261 /* Signal that the function call should not be made by
7262 gfc_conv_loop_setup. */
7263 se->ss->is_alloc_lhs = 1;
7264 gfc_init_loopinfo (loop);
7265 gfc_add_ss_to_loop (loop, *ss);
7266 gfc_add_ss_to_loop (loop, se->ss);
7267 gfc_conv_ss_startstride (loop);
7268 gfc_conv_loop_setup (loop, where);
7269 gfc_copy_loopinfo_to_se (se, loop);
7270 gfc_add_block_to_block (&se->pre, &loop->pre);
7271 gfc_add_block_to_block (&se->pre, &loop->post);
7272 se->ss->is_alloc_lhs = 0;
7276 /* For assignment to a reallocatable lhs from intrinsic functions,
7277 replace the se.expr (ie. the result) with a temporary descriptor.
7278 Null the data field so that the library allocates space for the
7279 result. Free the data of the original descriptor after the function,
7280 in case it appears in an argument expression and transfer the
7281 result to the original descriptor. */
7283 static void
7284 fcncall_realloc_result (gfc_se *se, int rank)
7286 tree desc;
7287 tree res_desc;
7288 tree tmp;
7289 tree offset;
7290 tree zero_cond;
7291 int n;
7293 /* Use the allocation done by the library. Substitute the lhs
7294 descriptor with a copy, whose data field is nulled.*/
7295 desc = build_fold_indirect_ref_loc (input_location, se->expr);
7296 if (POINTER_TYPE_P (TREE_TYPE (desc)))
7297 desc = build_fold_indirect_ref_loc (input_location, desc);
7299 /* Unallocated, the descriptor does not have a dtype. */
7300 tmp = gfc_conv_descriptor_dtype (desc);
7301 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
7303 res_desc = gfc_evaluate_now (desc, &se->pre);
7304 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
7305 se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
7307 /* Free the lhs after the function call and copy the result data to
7308 the lhs descriptor. */
7309 tmp = gfc_conv_descriptor_data_get (desc);
7310 zero_cond = fold_build2_loc (input_location, EQ_EXPR,
7311 boolean_type_node, tmp,
7312 build_int_cst (TREE_TYPE (tmp), 0));
7313 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
7314 tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
7315 gfc_add_expr_to_block (&se->post, tmp);
7317 tmp = gfc_conv_descriptor_data_get (res_desc);
7318 gfc_conv_descriptor_data_set (&se->post, desc, tmp);
7320 /* Check that the shapes are the same between lhs and expression. */
7321 for (n = 0 ; n < rank; n++)
7323 tree tmp1;
7324 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
7325 tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
7326 tmp = fold_build2_loc (input_location, MINUS_EXPR,
7327 gfc_array_index_type, tmp, tmp1);
7328 tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
7329 tmp = fold_build2_loc (input_location, MINUS_EXPR,
7330 gfc_array_index_type, tmp, tmp1);
7331 tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
7332 tmp = fold_build2_loc (input_location, PLUS_EXPR,
7333 gfc_array_index_type, tmp, tmp1);
7334 tmp = fold_build2_loc (input_location, NE_EXPR,
7335 boolean_type_node, tmp,
7336 gfc_index_zero_node);
7337 tmp = gfc_evaluate_now (tmp, &se->post);
7338 zero_cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
7339 boolean_type_node, tmp,
7340 zero_cond);
7343 /* 'zero_cond' being true is equal to lhs not being allocated or the
7344 shapes being different. */
7345 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
7347 /* Now reset the bounds returned from the function call to bounds based
7348 on the lhs lbounds, except where the lhs is not allocated or the shapes
7349 of 'variable and 'expr' are different. Set the offset accordingly. */
7350 offset = gfc_index_zero_node;
7351 for (n = 0 ; n < rank; n++)
7353 tree lbound;
7355 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
7356 lbound = fold_build3_loc (input_location, COND_EXPR,
7357 gfc_array_index_type, zero_cond,
7358 gfc_index_one_node, lbound);
7359 lbound = gfc_evaluate_now (lbound, &se->post);
7361 tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
7362 tmp = fold_build2_loc (input_location, PLUS_EXPR,
7363 gfc_array_index_type, tmp, lbound);
7364 gfc_conv_descriptor_lbound_set (&se->post, desc,
7365 gfc_rank_cst[n], lbound);
7366 gfc_conv_descriptor_ubound_set (&se->post, desc,
7367 gfc_rank_cst[n], tmp);
7369 /* Set stride and accumulate the offset. */
7370 tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
7371 gfc_conv_descriptor_stride_set (&se->post, desc,
7372 gfc_rank_cst[n], tmp);
7373 tmp = fold_build2_loc (input_location, MULT_EXPR,
7374 gfc_array_index_type, lbound, tmp);
7375 offset = fold_build2_loc (input_location, MINUS_EXPR,
7376 gfc_array_index_type, offset, tmp);
7377 offset = gfc_evaluate_now (offset, &se->post);
7380 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
7385 /* Try to translate array(:) = func (...), where func is a transformational
7386 array function, without using a temporary. Returns NULL if this isn't the
7387 case. */
7389 static tree
7390 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
7392 gfc_se se;
7393 gfc_ss *ss = NULL;
7394 gfc_component *comp = NULL;
7395 gfc_loopinfo loop;
7397 if (arrayfunc_assign_needs_temporary (expr1, expr2))
7398 return NULL;
7400 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
7401 functions. */
7402 comp = gfc_get_proc_ptr_comp (expr2);
7403 gcc_assert (expr2->value.function.isym
7404 || (comp && comp->attr.dimension)
7405 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
7406 && expr2->value.function.esym->result->attr.dimension));
7408 gfc_init_se (&se, NULL);
7409 gfc_start_block (&se.pre);
7410 se.want_pointer = 1;
7412 gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
7414 if (expr1->ts.type == BT_DERIVED
7415 && expr1->ts.u.derived->attr.alloc_comp)
7417 tree tmp;
7418 tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, se.expr,
7419 expr1->rank);
7420 gfc_add_expr_to_block (&se.pre, tmp);
7423 se.direct_byref = 1;
7424 se.ss = gfc_walk_expr (expr2);
7425 gcc_assert (se.ss != gfc_ss_terminator);
7427 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
7428 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
7429 Clearly, this cannot be done for an allocatable function result, since
7430 the shape of the result is unknown and, in any case, the function must
7431 correctly take care of the reallocation internally. For intrinsic
7432 calls, the array data is freed and the library takes care of allocation.
7433 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
7434 to the library. */
7435 if (gfc_option.flag_realloc_lhs
7436 && gfc_is_reallocatable_lhs (expr1)
7437 && !gfc_expr_attr (expr1).codimension
7438 && !gfc_is_coindexed (expr1)
7439 && !(expr2->value.function.esym
7440 && expr2->value.function.esym->result->attr.allocatable))
7442 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
7444 if (!expr2->value.function.isym)
7446 ss = gfc_walk_expr (expr1);
7447 gcc_assert (ss != gfc_ss_terminator);
7449 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
7450 ss->is_alloc_lhs = 1;
7452 else
7453 fcncall_realloc_result (&se, expr1->rank);
7456 gfc_conv_function_expr (&se, expr2);
7457 gfc_add_block_to_block (&se.pre, &se.post);
7459 if (ss)
7460 gfc_cleanup_loop (&loop);
7461 else
7462 gfc_free_ss_chain (se.ss);
7464 return gfc_finish_block (&se.pre);
7468 /* Try to efficiently translate array(:) = 0. Return NULL if this
7469 can't be done. */
7471 static tree
7472 gfc_trans_zero_assign (gfc_expr * expr)
7474 tree dest, len, type;
7475 tree tmp;
7476 gfc_symbol *sym;
7478 sym = expr->symtree->n.sym;
7479 dest = gfc_get_symbol_decl (sym);
7481 type = TREE_TYPE (dest);
7482 if (POINTER_TYPE_P (type))
7483 type = TREE_TYPE (type);
7484 if (!GFC_ARRAY_TYPE_P (type))
7485 return NULL_TREE;
7487 /* Determine the length of the array. */
7488 len = GFC_TYPE_ARRAY_SIZE (type);
7489 if (!len || TREE_CODE (len) != INTEGER_CST)
7490 return NULL_TREE;
7492 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
7493 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
7494 fold_convert (gfc_array_index_type, tmp));
7496 /* If we are zeroing a local array avoid taking its address by emitting
7497 a = {} instead. */
7498 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
7499 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
7500 dest, build_constructor (TREE_TYPE (dest),
7501 NULL));
7503 /* Convert arguments to the correct types. */
7504 dest = fold_convert (pvoid_type_node, dest);
7505 len = fold_convert (size_type_node, len);
7507 /* Construct call to __builtin_memset. */
7508 tmp = build_call_expr_loc (input_location,
7509 builtin_decl_explicit (BUILT_IN_MEMSET),
7510 3, dest, integer_zero_node, len);
7511 return fold_convert (void_type_node, tmp);
7515 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
7516 that constructs the call to __builtin_memcpy. */
7518 tree
7519 gfc_build_memcpy_call (tree dst, tree src, tree len)
7521 tree tmp;
7523 /* Convert arguments to the correct types. */
7524 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
7525 dst = gfc_build_addr_expr (pvoid_type_node, dst);
7526 else
7527 dst = fold_convert (pvoid_type_node, dst);
7529 if (!POINTER_TYPE_P (TREE_TYPE (src)))
7530 src = gfc_build_addr_expr (pvoid_type_node, src);
7531 else
7532 src = fold_convert (pvoid_type_node, src);
7534 len = fold_convert (size_type_node, len);
7536 /* Construct call to __builtin_memcpy. */
7537 tmp = build_call_expr_loc (input_location,
7538 builtin_decl_explicit (BUILT_IN_MEMCPY),
7539 3, dst, src, len);
7540 return fold_convert (void_type_node, tmp);
7544 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
7545 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
7546 source/rhs, both are gfc_full_array_ref_p which have been checked for
7547 dependencies. */
7549 static tree
7550 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
7552 tree dst, dlen, dtype;
7553 tree src, slen, stype;
7554 tree tmp;
7556 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
7557 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
7559 dtype = TREE_TYPE (dst);
7560 if (POINTER_TYPE_P (dtype))
7561 dtype = TREE_TYPE (dtype);
7562 stype = TREE_TYPE (src);
7563 if (POINTER_TYPE_P (stype))
7564 stype = TREE_TYPE (stype);
7566 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
7567 return NULL_TREE;
7569 /* Determine the lengths of the arrays. */
7570 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
7571 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
7572 return NULL_TREE;
7573 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
7574 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7575 dlen, fold_convert (gfc_array_index_type, tmp));
7577 slen = GFC_TYPE_ARRAY_SIZE (stype);
7578 if (!slen || TREE_CODE (slen) != INTEGER_CST)
7579 return NULL_TREE;
7580 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
7581 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7582 slen, fold_convert (gfc_array_index_type, tmp));
7584 /* Sanity check that they are the same. This should always be
7585 the case, as we should already have checked for conformance. */
7586 if (!tree_int_cst_equal (slen, dlen))
7587 return NULL_TREE;
7589 return gfc_build_memcpy_call (dst, src, dlen);
7593 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
7594 this can't be done. EXPR1 is the destination/lhs for which
7595 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
7597 static tree
7598 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
7600 unsigned HOST_WIDE_INT nelem;
7601 tree dst, dtype;
7602 tree src, stype;
7603 tree len;
7604 tree tmp;
7606 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
7607 if (nelem == 0)
7608 return NULL_TREE;
7610 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
7611 dtype = TREE_TYPE (dst);
7612 if (POINTER_TYPE_P (dtype))
7613 dtype = TREE_TYPE (dtype);
7614 if (!GFC_ARRAY_TYPE_P (dtype))
7615 return NULL_TREE;
7617 /* Determine the lengths of the array. */
7618 len = GFC_TYPE_ARRAY_SIZE (dtype);
7619 if (!len || TREE_CODE (len) != INTEGER_CST)
7620 return NULL_TREE;
7622 /* Confirm that the constructor is the same size. */
7623 if (compare_tree_int (len, nelem) != 0)
7624 return NULL_TREE;
7626 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
7627 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
7628 fold_convert (gfc_array_index_type, tmp));
7630 stype = gfc_typenode_for_spec (&expr2->ts);
7631 src = gfc_build_constant_array_constructor (expr2, stype);
7633 stype = TREE_TYPE (src);
7634 if (POINTER_TYPE_P (stype))
7635 stype = TREE_TYPE (stype);
7637 return gfc_build_memcpy_call (dst, src, len);
7641 /* Tells whether the expression is to be treated as a variable reference. */
7643 static bool
7644 expr_is_variable (gfc_expr *expr)
7646 gfc_expr *arg;
7647 gfc_component *comp;
7648 gfc_symbol *func_ifc;
7650 if (expr->expr_type == EXPR_VARIABLE)
7651 return true;
7653 arg = gfc_get_noncopying_intrinsic_argument (expr);
7654 if (arg)
7656 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
7657 return expr_is_variable (arg);
7660 /* A data-pointer-returning function should be considered as a variable
7661 too. */
7662 if (expr->expr_type == EXPR_FUNCTION
7663 && expr->ref == NULL)
7665 if (expr->value.function.isym != NULL)
7666 return false;
7668 if (expr->value.function.esym != NULL)
7670 func_ifc = expr->value.function.esym;
7671 goto found_ifc;
7673 else
7675 gcc_assert (expr->symtree);
7676 func_ifc = expr->symtree->n.sym;
7677 goto found_ifc;
7680 gcc_unreachable ();
7683 comp = gfc_get_proc_ptr_comp (expr);
7684 if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
7685 && comp)
7687 func_ifc = comp->ts.interface;
7688 goto found_ifc;
7691 if (expr->expr_type == EXPR_COMPCALL)
7693 gcc_assert (!expr->value.compcall.tbp->is_generic);
7694 func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
7695 goto found_ifc;
7698 return false;
7700 found_ifc:
7701 gcc_assert (func_ifc->attr.function
7702 && func_ifc->result != NULL);
7703 return func_ifc->result->attr.pointer;
7707 /* Is the lhs OK for automatic reallocation? */
7709 static bool
7710 is_scalar_reallocatable_lhs (gfc_expr *expr)
7712 gfc_ref * ref;
7714 /* An allocatable variable with no reference. */
7715 if (expr->symtree->n.sym->attr.allocatable
7716 && !expr->ref)
7717 return true;
7719 /* All that can be left are allocatable components. */
7720 if ((expr->symtree->n.sym->ts.type != BT_DERIVED
7721 && expr->symtree->n.sym->ts.type != BT_CLASS)
7722 || !expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
7723 return false;
7725 /* Find an allocatable component ref last. */
7726 for (ref = expr->ref; ref; ref = ref->next)
7727 if (ref->type == REF_COMPONENT
7728 && !ref->next
7729 && ref->u.c.component->attr.allocatable)
7730 return true;
7732 return false;
7736 /* Allocate or reallocate scalar lhs, as necessary. */
7738 static void
7739 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
7740 tree string_length,
7741 gfc_expr *expr1,
7742 gfc_expr *expr2)
7745 tree cond;
7746 tree tmp;
7747 tree size;
7748 tree size_in_bytes;
7749 tree jump_label1;
7750 tree jump_label2;
7751 gfc_se lse;
7753 if (!expr1 || expr1->rank)
7754 return;
7756 if (!expr2 || expr2->rank)
7757 return;
7759 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
7761 /* Since this is a scalar lhs, we can afford to do this. That is,
7762 there is no risk of side effects being repeated. */
7763 gfc_init_se (&lse, NULL);
7764 lse.want_pointer = 1;
7765 gfc_conv_expr (&lse, expr1);
7767 jump_label1 = gfc_build_label_decl (NULL_TREE);
7768 jump_label2 = gfc_build_label_decl (NULL_TREE);
7770 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
7771 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
7772 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
7773 lse.expr, tmp);
7774 tmp = build3_v (COND_EXPR, cond,
7775 build1_v (GOTO_EXPR, jump_label1),
7776 build_empty_stmt (input_location));
7777 gfc_add_expr_to_block (block, tmp);
7779 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
7781 /* Use the rhs string length and the lhs element size. */
7782 size = string_length;
7783 tmp = TREE_TYPE (gfc_typenode_for_spec (&expr1->ts));
7784 tmp = TYPE_SIZE_UNIT (tmp);
7785 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
7786 TREE_TYPE (tmp), tmp,
7787 fold_convert (TREE_TYPE (tmp), size));
7789 else
7791 /* Otherwise use the length in bytes of the rhs. */
7792 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
7793 size_in_bytes = size;
7796 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
7797 size_in_bytes, size_one_node);
7799 if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
7801 tmp = build_call_expr_loc (input_location,
7802 builtin_decl_explicit (BUILT_IN_CALLOC),
7803 2, build_one_cst (size_type_node),
7804 size_in_bytes);
7805 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
7806 gfc_add_modify (block, lse.expr, tmp);
7808 else
7810 tmp = build_call_expr_loc (input_location,
7811 builtin_decl_explicit (BUILT_IN_MALLOC),
7812 1, size_in_bytes);
7813 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
7814 gfc_add_modify (block, lse.expr, tmp);
7817 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
7819 /* Deferred characters need checking for lhs and rhs string
7820 length. Other deferred parameter variables will have to
7821 come here too. */
7822 tmp = build1_v (GOTO_EXPR, jump_label2);
7823 gfc_add_expr_to_block (block, tmp);
7825 tmp = build1_v (LABEL_EXPR, jump_label1);
7826 gfc_add_expr_to_block (block, tmp);
7828 /* For a deferred length character, reallocate if lengths of lhs and
7829 rhs are different. */
7830 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
7832 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
7833 expr1->ts.u.cl->backend_decl, size);
7834 /* Jump past the realloc if the lengths are the same. */
7835 tmp = build3_v (COND_EXPR, cond,
7836 build1_v (GOTO_EXPR, jump_label2),
7837 build_empty_stmt (input_location));
7838 gfc_add_expr_to_block (block, tmp);
7839 tmp = build_call_expr_loc (input_location,
7840 builtin_decl_explicit (BUILT_IN_REALLOC),
7841 2, fold_convert (pvoid_type_node, lse.expr),
7842 size_in_bytes);
7843 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
7844 gfc_add_modify (block, lse.expr, tmp);
7845 tmp = build1_v (LABEL_EXPR, jump_label2);
7846 gfc_add_expr_to_block (block, tmp);
7848 /* Update the lhs character length. */
7849 size = string_length;
7850 if (TREE_CODE (expr1->ts.u.cl->backend_decl) == VAR_DECL)
7851 gfc_add_modify (block, expr1->ts.u.cl->backend_decl, size);
7852 else
7853 gfc_add_modify (block, lse.string_length, size);
7857 /* Check for assignments of the type
7859 a = a + 4
7861 to make sure we do not check for reallocation unneccessarily. */
7864 static bool
7865 is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
7867 gfc_actual_arglist *a;
7868 gfc_expr *e1, *e2;
7870 switch (expr2->expr_type)
7872 case EXPR_VARIABLE:
7873 return gfc_dep_compare_expr (expr1, expr2) == 0;
7875 case EXPR_FUNCTION:
7876 if (expr2->value.function.esym
7877 && expr2->value.function.esym->attr.elemental)
7879 for (a = expr2->value.function.actual; a != NULL; a = a->next)
7881 e1 = a->expr;
7882 if (e1->rank > 0 && !is_runtime_conformable (expr1, e1))
7883 return false;
7885 return true;
7887 else if (expr2->value.function.isym
7888 && expr2->value.function.isym->elemental)
7890 for (a = expr2->value.function.actual; a != NULL; a = a->next)
7892 e1 = a->expr;
7893 if (e1->rank > 0 && !is_runtime_conformable (expr1, e1))
7894 return false;
7896 return true;
7899 break;
7901 case EXPR_OP:
7902 switch (expr2->value.op.op)
7904 case INTRINSIC_NOT:
7905 case INTRINSIC_UPLUS:
7906 case INTRINSIC_UMINUS:
7907 case INTRINSIC_PARENTHESES:
7908 return is_runtime_conformable (expr1, expr2->value.op.op1);
7910 case INTRINSIC_PLUS:
7911 case INTRINSIC_MINUS:
7912 case INTRINSIC_TIMES:
7913 case INTRINSIC_DIVIDE:
7914 case INTRINSIC_POWER:
7915 case INTRINSIC_AND:
7916 case INTRINSIC_OR:
7917 case INTRINSIC_EQV:
7918 case INTRINSIC_NEQV:
7919 case INTRINSIC_EQ:
7920 case INTRINSIC_NE:
7921 case INTRINSIC_GT:
7922 case INTRINSIC_GE:
7923 case INTRINSIC_LT:
7924 case INTRINSIC_LE:
7925 case INTRINSIC_EQ_OS:
7926 case INTRINSIC_NE_OS:
7927 case INTRINSIC_GT_OS:
7928 case INTRINSIC_GE_OS:
7929 case INTRINSIC_LT_OS:
7930 case INTRINSIC_LE_OS:
7932 e1 = expr2->value.op.op1;
7933 e2 = expr2->value.op.op2;
7935 if (e1->rank == 0 && e2->rank > 0)
7936 return is_runtime_conformable (expr1, e2);
7937 else if (e1->rank > 0 && e2->rank == 0)
7938 return is_runtime_conformable (expr1, e1);
7939 else if (e1->rank > 0 && e2->rank > 0)
7940 return is_runtime_conformable (expr1, e1)
7941 && is_runtime_conformable (expr1, e2);
7942 break;
7944 default:
7945 break;
7949 break;
7951 default:
7952 break;
7954 return false;
7957 /* Subroutine of gfc_trans_assignment that actually scalarizes the
7958 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
7959 init_flag indicates initialization expressions and dealloc that no
7960 deallocate prior assignment is needed (if in doubt, set true). */
7962 static tree
7963 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
7964 bool dealloc)
7966 gfc_se lse;
7967 gfc_se rse;
7968 gfc_ss *lss;
7969 gfc_ss *lss_section;
7970 gfc_ss *rss;
7971 gfc_loopinfo loop;
7972 tree tmp;
7973 stmtblock_t block;
7974 stmtblock_t body;
7975 bool l_is_temp;
7976 bool scalar_to_array;
7977 tree string_length;
7978 int n;
7980 /* Assignment of the form lhs = rhs. */
7981 gfc_start_block (&block);
7983 gfc_init_se (&lse, NULL);
7984 gfc_init_se (&rse, NULL);
7986 /* Walk the lhs. */
7987 lss = gfc_walk_expr (expr1);
7988 if (gfc_is_reallocatable_lhs (expr1)
7989 && !(expr2->expr_type == EXPR_FUNCTION
7990 && expr2->value.function.isym != NULL))
7991 lss->is_alloc_lhs = 1;
7992 rss = NULL;
7993 if (lss != gfc_ss_terminator)
7995 /* The assignment needs scalarization. */
7996 lss_section = lss;
7998 /* Find a non-scalar SS from the lhs. */
7999 while (lss_section != gfc_ss_terminator
8000 && lss_section->info->type != GFC_SS_SECTION)
8001 lss_section = lss_section->next;
8003 gcc_assert (lss_section != gfc_ss_terminator);
8005 /* Initialize the scalarizer. */
8006 gfc_init_loopinfo (&loop);
8008 /* Walk the rhs. */
8009 rss = gfc_walk_expr (expr2);
8010 if (rss == gfc_ss_terminator)
8011 /* The rhs is scalar. Add a ss for the expression. */
8012 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
8014 /* Associate the SS with the loop. */
8015 gfc_add_ss_to_loop (&loop, lss);
8016 gfc_add_ss_to_loop (&loop, rss);
8018 /* Calculate the bounds of the scalarization. */
8019 gfc_conv_ss_startstride (&loop);
8020 /* Enable loop reversal. */
8021 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
8022 loop.reverse[n] = GFC_ENABLE_REVERSE;
8023 /* Resolve any data dependencies in the statement. */
8024 gfc_conv_resolve_dependencies (&loop, lss, rss);
8025 /* Setup the scalarizing loops. */
8026 gfc_conv_loop_setup (&loop, &expr2->where);
8028 /* Setup the gfc_se structures. */
8029 gfc_copy_loopinfo_to_se (&lse, &loop);
8030 gfc_copy_loopinfo_to_se (&rse, &loop);
8032 rse.ss = rss;
8033 gfc_mark_ss_chain_used (rss, 1);
8034 if (loop.temp_ss == NULL)
8036 lse.ss = lss;
8037 gfc_mark_ss_chain_used (lss, 1);
8039 else
8041 lse.ss = loop.temp_ss;
8042 gfc_mark_ss_chain_used (lss, 3);
8043 gfc_mark_ss_chain_used (loop.temp_ss, 3);
8046 /* Allow the scalarizer to workshare array assignments. */
8047 if ((ompws_flags & OMPWS_WORKSHARE_FLAG) && loop.temp_ss == NULL)
8048 ompws_flags |= OMPWS_SCALARIZER_WS;
8050 /* Start the scalarized loop body. */
8051 gfc_start_scalarized_body (&loop, &body);
8053 else
8054 gfc_init_block (&body);
8056 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
8058 /* Translate the expression. */
8059 gfc_conv_expr (&rse, expr2);
8061 /* Stabilize a string length for temporaries. */
8062 if (expr2->ts.type == BT_CHARACTER)
8063 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
8064 else
8065 string_length = NULL_TREE;
8067 if (l_is_temp)
8069 gfc_conv_tmp_array_ref (&lse);
8070 if (expr2->ts.type == BT_CHARACTER)
8071 lse.string_length = string_length;
8073 else
8074 gfc_conv_expr (&lse, expr1);
8076 /* Assignments of scalar derived types with allocatable components
8077 to arrays must be done with a deep copy and the rhs temporary
8078 must have its components deallocated afterwards. */
8079 scalar_to_array = (expr2->ts.type == BT_DERIVED
8080 && expr2->ts.u.derived->attr.alloc_comp
8081 && !expr_is_variable (expr2)
8082 && !gfc_is_constant_expr (expr2)
8083 && expr1->rank && !expr2->rank);
8084 if (scalar_to_array && dealloc)
8086 tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
8087 gfc_add_expr_to_block (&loop.post, tmp);
8090 /* When assigning a character function result to a deferred-length variable,
8091 the function call must happen before the (re)allocation of the lhs -
8092 otherwise the character length of the result is not known.
8093 NOTE: This relies on having the exact dependence of the length type
8094 parameter available to the caller; gfortran saves it in the .mod files. */
8095 if (gfc_option.flag_realloc_lhs && expr2->ts.type == BT_CHARACTER
8096 && expr1->ts.deferred)
8097 gfc_add_block_to_block (&block, &rse.pre);
8099 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
8100 l_is_temp || init_flag,
8101 expr_is_variable (expr2) || scalar_to_array
8102 || expr2->expr_type == EXPR_ARRAY, dealloc);
8103 gfc_add_expr_to_block (&body, tmp);
8105 if (lss == gfc_ss_terminator)
8107 /* F2003: Add the code for reallocation on assignment. */
8108 if (gfc_option.flag_realloc_lhs
8109 && is_scalar_reallocatable_lhs (expr1))
8110 alloc_scalar_allocatable_for_assignment (&block, rse.string_length,
8111 expr1, expr2);
8113 /* Use the scalar assignment as is. */
8114 gfc_add_block_to_block (&block, &body);
8116 else
8118 gcc_assert (lse.ss == gfc_ss_terminator
8119 && rse.ss == gfc_ss_terminator);
8121 if (l_is_temp)
8123 gfc_trans_scalarized_loop_boundary (&loop, &body);
8125 /* We need to copy the temporary to the actual lhs. */
8126 gfc_init_se (&lse, NULL);
8127 gfc_init_se (&rse, NULL);
8128 gfc_copy_loopinfo_to_se (&lse, &loop);
8129 gfc_copy_loopinfo_to_se (&rse, &loop);
8131 rse.ss = loop.temp_ss;
8132 lse.ss = lss;
8134 gfc_conv_tmp_array_ref (&rse);
8135 gfc_conv_expr (&lse, expr1);
8137 gcc_assert (lse.ss == gfc_ss_terminator
8138 && rse.ss == gfc_ss_terminator);
8140 if (expr2->ts.type == BT_CHARACTER)
8141 rse.string_length = string_length;
8143 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
8144 false, false, dealloc);
8145 gfc_add_expr_to_block (&body, tmp);
8148 /* F2003: Allocate or reallocate lhs of allocatable array. */
8149 if (gfc_option.flag_realloc_lhs
8150 && gfc_is_reallocatable_lhs (expr1)
8151 && !gfc_expr_attr (expr1).codimension
8152 && !gfc_is_coindexed (expr1)
8153 && expr2->rank
8154 && !is_runtime_conformable (expr1, expr2))
8156 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
8157 ompws_flags &= ~OMPWS_SCALARIZER_WS;
8158 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
8159 if (tmp != NULL_TREE)
8160 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
8163 /* Generate the copying loops. */
8164 gfc_trans_scalarizing_loops (&loop, &body);
8166 /* Wrap the whole thing up. */
8167 gfc_add_block_to_block (&block, &loop.pre);
8168 gfc_add_block_to_block (&block, &loop.post);
8170 gfc_cleanup_loop (&loop);
8173 return gfc_finish_block (&block);
8177 /* Check whether EXPR is a copyable array. */
8179 static bool
8180 copyable_array_p (gfc_expr * expr)
8182 if (expr->expr_type != EXPR_VARIABLE)
8183 return false;
8185 /* First check it's an array. */
8186 if (expr->rank < 1 || !expr->ref || expr->ref->next)
8187 return false;
8189 if (!gfc_full_array_ref_p (expr->ref, NULL))
8190 return false;
8192 /* Next check that it's of a simple enough type. */
8193 switch (expr->ts.type)
8195 case BT_INTEGER:
8196 case BT_REAL:
8197 case BT_COMPLEX:
8198 case BT_LOGICAL:
8199 return true;
8201 case BT_CHARACTER:
8202 return false;
8204 case BT_DERIVED:
8205 return !expr->ts.u.derived->attr.alloc_comp;
8207 default:
8208 break;
8211 return false;
8214 /* Translate an assignment. */
8216 tree
8217 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
8218 bool dealloc)
8220 tree tmp;
8222 /* Special case a single function returning an array. */
8223 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
8225 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
8226 if (tmp)
8227 return tmp;
8230 /* Special case assigning an array to zero. */
8231 if (copyable_array_p (expr1)
8232 && is_zero_initializer_p (expr2))
8234 tmp = gfc_trans_zero_assign (expr1);
8235 if (tmp)
8236 return tmp;
8239 /* Special case copying one array to another. */
8240 if (copyable_array_p (expr1)
8241 && copyable_array_p (expr2)
8242 && gfc_compare_types (&expr1->ts, &expr2->ts)
8243 && !gfc_check_dependency (expr1, expr2, 0))
8245 tmp = gfc_trans_array_copy (expr1, expr2);
8246 if (tmp)
8247 return tmp;
8250 /* Special case initializing an array from a constant array constructor. */
8251 if (copyable_array_p (expr1)
8252 && expr2->expr_type == EXPR_ARRAY
8253 && gfc_compare_types (&expr1->ts, &expr2->ts))
8255 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
8256 if (tmp)
8257 return tmp;
8260 /* Fallback to the scalarizer to generate explicit loops. */
8261 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc);
8264 tree
8265 gfc_trans_init_assign (gfc_code * code)
8267 return gfc_trans_assignment (code->expr1, code->expr2, true, false);
8270 tree
8271 gfc_trans_assign (gfc_code * code)
8273 return gfc_trans_assignment (code->expr1, code->expr2, false, true);