1 /* Expression translation
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
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
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. */
26 #include "coretypes.h"
28 #include "diagnostic-core.h" /* For fatal_error. */
29 #include "langhooks.h"
33 #include "constructor.h"
35 #include "trans-const.h"
36 #include "trans-types.h"
37 #include "trans-array.h"
38 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
39 #include "trans-stmt.h"
40 #include "dependency.h"
43 /* Convert a scalar to an array descriptor. To be used for assumed-rank
47 get_scalar_to_descriptor_type (tree scalar
, symbol_attribute attr
)
49 enum gfc_array_kind akind
;
52 akind
= GFC_ARRAY_POINTER_CONT
;
53 else if (attr
.allocatable
)
54 akind
= GFC_ARRAY_ALLOCATABLE
;
56 akind
= GFC_ARRAY_ASSUMED_SHAPE_CONT
;
58 return gfc_get_array_type_bounds (TREE_TYPE (scalar
), 0, 0, NULL
, NULL
, 1,
59 akind
, !(attr
.pointer
|| attr
.target
));
63 gfc_conv_scalar_to_descriptor (gfc_se
*se
, tree scalar
, symbol_attribute attr
)
67 type
= get_scalar_to_descriptor_type (scalar
, attr
);
68 desc
= gfc_create_var (type
, "desc");
69 DECL_ARTIFICIAL (desc
) = 1;
70 gfc_add_modify (&se
->pre
, gfc_conv_descriptor_dtype (desc
),
71 gfc_get_dtype (type
));
72 gfc_conv_descriptor_data_set (&se
->pre
, desc
, scalar
);
74 /* Copy pointer address back - but only if it could have changed and
75 if the actual argument is a pointer and not, e.g., NULL(). */
76 if ((attr
.pointer
|| attr
.allocatable
)
77 && attr
.intent
!= INTENT_IN
&& POINTER_TYPE_P (TREE_TYPE (scalar
)))
78 gfc_add_modify (&se
->post
, scalar
,
79 fold_convert (TREE_TYPE (scalar
),
80 gfc_conv_descriptor_data_get (desc
)));
85 /* This is the seed for an eventual trans-class.c
87 The following parameters should not be used directly since they might
88 in future implementations. Use the corresponding APIs. */
89 #define CLASS_DATA_FIELD 0
90 #define CLASS_VPTR_FIELD 1
91 #define VTABLE_HASH_FIELD 0
92 #define VTABLE_SIZE_FIELD 1
93 #define VTABLE_EXTENDS_FIELD 2
94 #define VTABLE_DEF_INIT_FIELD 3
95 #define VTABLE_COPY_FIELD 4
96 #define VTABLE_FINAL_FIELD 5
100 gfc_class_set_static_fields (tree decl
, tree vptr
, tree data
)
104 vec
<constructor_elt
, va_gc
> *init
= NULL
;
106 field
= TYPE_FIELDS (TREE_TYPE (decl
));
107 tmp
= gfc_advance_chain (field
, CLASS_DATA_FIELD
);
108 CONSTRUCTOR_APPEND_ELT (init
, tmp
, data
);
110 tmp
= gfc_advance_chain (field
, CLASS_VPTR_FIELD
);
111 CONSTRUCTOR_APPEND_ELT (init
, tmp
, vptr
);
113 return build_constructor (TREE_TYPE (decl
), init
);
118 gfc_class_data_get (tree decl
)
121 if (POINTER_TYPE_P (TREE_TYPE (decl
)))
122 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
123 data
= gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl
)),
125 return fold_build3_loc (input_location
, COMPONENT_REF
,
126 TREE_TYPE (data
), decl
, data
,
132 gfc_class_vptr_get (tree decl
)
135 if (POINTER_TYPE_P (TREE_TYPE (decl
)))
136 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
137 vptr
= gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl
)),
139 return fold_build3_loc (input_location
, COMPONENT_REF
,
140 TREE_TYPE (vptr
), decl
, vptr
,
146 gfc_vtable_field_get (tree decl
, int field
)
150 vptr
= gfc_class_vptr_get (decl
);
151 vptr
= build_fold_indirect_ref_loc (input_location
, vptr
);
152 size
= gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr
)),
154 size
= fold_build3_loc (input_location
, COMPONENT_REF
,
155 TREE_TYPE (size
), vptr
, size
,
157 /* Always return size as an array index type. */
158 if (field
== VTABLE_SIZE_FIELD
)
159 size
= fold_convert (gfc_array_index_type
, size
);
166 gfc_vtable_hash_get (tree decl
)
168 return gfc_vtable_field_get (decl
, VTABLE_HASH_FIELD
);
173 gfc_vtable_size_get (tree decl
)
175 return gfc_vtable_field_get (decl
, VTABLE_SIZE_FIELD
);
180 gfc_vtable_extends_get (tree decl
)
182 return gfc_vtable_field_get (decl
, VTABLE_EXTENDS_FIELD
);
187 gfc_vtable_def_init_get (tree decl
)
189 return gfc_vtable_field_get (decl
, VTABLE_DEF_INIT_FIELD
);
194 gfc_vtable_copy_get (tree decl
)
196 return gfc_vtable_field_get (decl
, VTABLE_COPY_FIELD
);
201 gfc_vtable_final_get (tree decl
)
203 return gfc_vtable_field_get (decl
, VTABLE_FINAL_FIELD
);
207 #undef CLASS_DATA_FIELD
208 #undef CLASS_VPTR_FIELD
209 #undef VTABLE_HASH_FIELD
210 #undef VTABLE_SIZE_FIELD
211 #undef VTABLE_EXTENDS_FIELD
212 #undef VTABLE_DEF_INIT_FIELD
213 #undef VTABLE_COPY_FIELD
214 #undef VTABLE_FINAL_FIELD
217 /* Reset the vptr to the declared type, e.g. after deallocation. */
220 gfc_reset_vptr (stmtblock_t
*block
, gfc_expr
*e
)
222 gfc_expr
*rhs
, *lhs
= gfc_copy_expr (e
);
227 /* If we have a class array, we need go back to the class
229 if (lhs
->ref
&& lhs
->ref
->next
&& !lhs
->ref
->next
->next
230 && lhs
->ref
->next
->type
== REF_ARRAY
231 && lhs
->ref
->next
->u
.ar
.type
== AR_FULL
232 && lhs
->ref
->type
== REF_COMPONENT
233 && strcmp (lhs
->ref
->u
.c
.component
->name
, "_data") == 0)
235 gfc_free_ref_list (lhs
->ref
);
239 for (ref
= lhs
->ref
; ref
; ref
= ref
->next
)
240 if (ref
->next
&& ref
->next
->next
&& !ref
->next
->next
->next
241 && ref
->next
->next
->type
== REF_ARRAY
242 && ref
->next
->next
->u
.ar
.type
== AR_FULL
243 && ref
->next
->type
== REF_COMPONENT
244 && strcmp (ref
->next
->u
.c
.component
->name
, "_data") == 0)
246 gfc_free_ref_list (ref
->next
);
250 gfc_add_vptr_component (lhs
);
252 if (UNLIMITED_POLY (e
))
253 rhs
= gfc_get_null_expr (NULL
);
256 vtab
= gfc_find_derived_vtab (e
->ts
.u
.derived
);
257 rhs
= gfc_lval_expr_from_sym (vtab
);
259 tmp
= gfc_trans_pointer_assignment (lhs
, rhs
);
260 gfc_add_expr_to_block (block
, tmp
);
266 /* Obtain the vptr of the last class reference in an expression.
267 Return NULL_TREE if no class reference is found. */
270 gfc_get_vptr_from_expr (tree expr
)
275 for (tmp
= expr
; tmp
; tmp
= TREE_OPERAND (tmp
, 0))
277 type
= TREE_TYPE (tmp
);
280 if (GFC_CLASS_TYPE_P (type
))
281 return gfc_class_vptr_get (tmp
);
282 if (type
!= TYPE_CANONICAL (type
))
283 type
= TYPE_CANONICAL (type
);
287 if (TREE_CODE (tmp
) == VAR_DECL
)
295 class_array_data_assign (stmtblock_t
*block
, tree lhs_desc
, tree rhs_desc
,
298 tree tmp
, tmp2
, type
;
300 gfc_conv_descriptor_data_set (block
, lhs_desc
,
301 gfc_conv_descriptor_data_get (rhs_desc
));
302 gfc_conv_descriptor_offset_set (block
, lhs_desc
,
303 gfc_conv_descriptor_offset_get (rhs_desc
));
305 gfc_add_modify (block
, gfc_conv_descriptor_dtype (lhs_desc
),
306 gfc_conv_descriptor_dtype (rhs_desc
));
308 /* Assign the dimension as range-ref. */
309 tmp
= gfc_get_descriptor_dimension (lhs_desc
);
310 tmp2
= gfc_get_descriptor_dimension (rhs_desc
);
312 type
= lhs_type
? TREE_TYPE (tmp
) : TREE_TYPE (tmp2
);
313 tmp
= build4_loc (input_location
, ARRAY_RANGE_REF
, type
, tmp
,
314 gfc_index_zero_node
, NULL_TREE
, NULL_TREE
);
315 tmp2
= build4_loc (input_location
, ARRAY_RANGE_REF
, type
, tmp2
,
316 gfc_index_zero_node
, NULL_TREE
, NULL_TREE
);
317 gfc_add_modify (block
, tmp
, tmp2
);
321 /* Takes a derived type expression and returns the address of a temporary
322 class object of the 'declared' type. If vptr is not NULL, this is
323 used for the temporary class object.
324 optional_alloc_ptr is false when the dummy is neither allocatable
325 nor a pointer; that's only relevant for the optional handling. */
327 gfc_conv_derived_to_class (gfc_se
*parmse
, gfc_expr
*e
,
328 gfc_typespec class_ts
, tree vptr
, bool optional
,
329 bool optional_alloc_ptr
)
332 tree cond_optional
= NULL_TREE
;
338 /* The derived type needs to be converted to a temporary
340 tmp
= gfc_typenode_for_spec (&class_ts
);
341 var
= gfc_create_var (tmp
, "class");
344 ctree
= gfc_class_vptr_get (var
);
346 if (vptr
!= NULL_TREE
)
348 /* Use the dynamic vptr. */
353 /* In this case the vtab corresponds to the derived type and the
354 vptr must point to it. */
355 vtab
= gfc_find_derived_vtab (e
->ts
.u
.derived
);
357 tmp
= gfc_build_addr_expr (NULL_TREE
, gfc_get_symbol_decl (vtab
));
359 gfc_add_modify (&parmse
->pre
, ctree
,
360 fold_convert (TREE_TYPE (ctree
), tmp
));
362 /* Now set the data field. */
363 ctree
= gfc_class_data_get (var
);
366 cond_optional
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
368 if (parmse
->ss
&& parmse
->ss
->info
->useflags
)
370 /* For an array reference in an elemental procedure call we need
371 to retain the ss to provide the scalarized array reference. */
372 gfc_conv_expr_reference (parmse
, e
);
373 tmp
= fold_convert (TREE_TYPE (ctree
), parmse
->expr
);
375 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
377 fold_convert (TREE_TYPE (tmp
), null_pointer_node
));
378 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
383 ss
= gfc_walk_expr (e
);
384 if (ss
== gfc_ss_terminator
)
387 gfc_conv_expr_reference (parmse
, e
);
389 /* Scalar to an assumed-rank array. */
390 if (class_ts
.u
.derived
->components
->as
)
393 type
= get_scalar_to_descriptor_type (parmse
->expr
,
395 gfc_add_modify (&parmse
->pre
, gfc_conv_descriptor_dtype (ctree
),
396 gfc_get_dtype (type
));
398 parmse
->expr
= build3_loc (input_location
, COND_EXPR
,
399 TREE_TYPE (parmse
->expr
),
400 cond_optional
, parmse
->expr
,
401 fold_convert (TREE_TYPE (parmse
->expr
),
403 gfc_conv_descriptor_data_set (&parmse
->pre
, ctree
, parmse
->expr
);
407 tmp
= fold_convert (TREE_TYPE (ctree
), parmse
->expr
);
409 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
411 fold_convert (TREE_TYPE (tmp
),
413 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
419 gfc_init_block (&block
);
422 gfc_conv_expr_descriptor (parmse
, e
);
424 if (e
->rank
!= class_ts
.u
.derived
->components
->as
->rank
)
425 class_array_data_assign (&block
, ctree
, parmse
->expr
, true);
428 if (gfc_expr_attr (e
).codimension
)
429 parmse
->expr
= fold_build1_loc (input_location
,
433 gfc_add_modify (&block
, ctree
, parmse
->expr
);
438 tmp
= gfc_finish_block (&block
);
440 gfc_init_block (&block
);
441 gfc_conv_descriptor_data_set (&block
, ctree
, null_pointer_node
);
443 tmp
= build3_v (COND_EXPR
, cond_optional
, tmp
,
444 gfc_finish_block (&block
));
445 gfc_add_expr_to_block (&parmse
->pre
, tmp
);
448 gfc_add_block_to_block (&parmse
->pre
, &block
);
452 /* Pass the address of the class object. */
453 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
455 if (optional
&& optional_alloc_ptr
)
456 parmse
->expr
= build3_loc (input_location
, COND_EXPR
,
457 TREE_TYPE (parmse
->expr
),
458 cond_optional
, parmse
->expr
,
459 fold_convert (TREE_TYPE (parmse
->expr
),
464 /* Create a new class container, which is required as scalar coarrays
465 have an array descriptor while normal scalars haven't. Optionally,
466 NULL pointer checks are added if the argument is OPTIONAL. */
469 class_scalar_coarray_to_class (gfc_se
*parmse
, gfc_expr
*e
,
470 gfc_typespec class_ts
, bool optional
)
472 tree var
, ctree
, tmp
;
477 gfc_init_block (&block
);
480 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
482 if (ref
->type
== REF_COMPONENT
483 && ref
->u
.c
.component
->ts
.type
== BT_CLASS
)
487 if (class_ref
== NULL
488 && e
->symtree
&& e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
489 tmp
= e
->symtree
->n
.sym
->backend_decl
;
492 /* Remove everything after the last class reference, convert the
493 expression and then recover its tailend once more. */
495 ref
= class_ref
->next
;
496 class_ref
->next
= NULL
;
497 gfc_init_se (&tmpse
, NULL
);
498 gfc_conv_expr (&tmpse
, e
);
499 class_ref
->next
= ref
;
503 var
= gfc_typenode_for_spec (&class_ts
);
504 var
= gfc_create_var (var
, "class");
506 ctree
= gfc_class_vptr_get (var
);
507 gfc_add_modify (&block
, ctree
,
508 fold_convert (TREE_TYPE (ctree
), gfc_class_vptr_get (tmp
)));
510 ctree
= gfc_class_data_get (var
);
511 tmp
= gfc_conv_descriptor_data_get (gfc_class_data_get (tmp
));
512 gfc_add_modify (&block
, ctree
, fold_convert (TREE_TYPE (ctree
), tmp
));
514 /* Pass the address of the class object. */
515 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
519 tree cond
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
522 tmp
= gfc_finish_block (&block
);
524 gfc_init_block (&block
);
525 tmp2
= gfc_class_data_get (var
);
526 gfc_add_modify (&block
, tmp2
, fold_convert (TREE_TYPE (tmp2
),
528 tmp2
= gfc_finish_block (&block
);
530 tmp
= build3_loc (input_location
, COND_EXPR
, void_type_node
,
532 gfc_add_expr_to_block (&parmse
->pre
, tmp
);
535 gfc_add_block_to_block (&parmse
->pre
, &block
);
539 /* Takes an intrinsic type expression and returns the address of a temporary
540 class object of the 'declared' type. */
542 gfc_conv_intrinsic_to_class (gfc_se
*parmse
, gfc_expr
*e
,
543 gfc_typespec class_ts
)
551 /* The intrinsic type needs to be converted to a temporary
553 tmp
= gfc_typenode_for_spec (&class_ts
);
554 var
= gfc_create_var (tmp
, "class");
557 ctree
= gfc_class_vptr_get (var
);
559 vtab
= gfc_find_intrinsic_vtab (&e
->ts
);
561 tmp
= gfc_build_addr_expr (NULL_TREE
, gfc_get_symbol_decl (vtab
));
562 gfc_add_modify (&parmse
->pre
, ctree
,
563 fold_convert (TREE_TYPE (ctree
), tmp
));
565 /* Now set the data field. */
566 ctree
= gfc_class_data_get (var
);
567 if (parmse
->ss
&& parmse
->ss
->info
->useflags
)
569 /* For an array reference in an elemental procedure call we need
570 to retain the ss to provide the scalarized array reference. */
571 gfc_conv_expr_reference (parmse
, e
);
572 tmp
= fold_convert (TREE_TYPE (ctree
), parmse
->expr
);
573 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
577 ss
= gfc_walk_expr (e
);
578 if (ss
== gfc_ss_terminator
)
581 gfc_conv_expr_reference (parmse
, e
);
582 tmp
= fold_convert (TREE_TYPE (ctree
), parmse
->expr
);
583 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
588 gfc_conv_expr_descriptor (parmse
, e
);
589 gfc_add_modify (&parmse
->pre
, ctree
, parmse
->expr
);
593 /* Pass the address of the class object. */
594 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
598 /* Takes a scalarized class array expression and returns the
599 address of a temporary scalar class object of the 'declared'
601 OOP-TODO: This could be improved by adding code that branched on
602 the dynamic type being the same as the declared type. In this case
603 the original class expression can be passed directly.
604 optional_alloc_ptr is false when the dummy is neither allocatable
605 nor a pointer; that's relevant for the optional handling.
606 Set copyback to true if class container's _data and _vtab pointers
607 might get modified. */
610 gfc_conv_class_to_class (gfc_se
*parmse
, gfc_expr
*e
, gfc_typespec class_ts
,
611 bool elemental
, bool copyback
, bool optional
,
612 bool optional_alloc_ptr
)
618 tree cond
= NULL_TREE
;
622 bool full_array
= false;
624 gfc_init_block (&block
);
627 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
629 if (ref
->type
== REF_COMPONENT
630 && ref
->u
.c
.component
->ts
.type
== BT_CLASS
)
633 if (ref
->next
== NULL
)
637 if ((ref
== NULL
|| class_ref
== ref
)
638 && (!class_ts
.u
.derived
->components
->as
639 || class_ts
.u
.derived
->components
->as
->rank
!= -1))
642 /* Test for FULL_ARRAY. */
643 if (e
->rank
== 0 && gfc_expr_attr (e
).codimension
644 && gfc_expr_attr (e
).dimension
)
647 gfc_is_class_array_ref (e
, &full_array
);
649 /* The derived type needs to be converted to a temporary
651 tmp
= gfc_typenode_for_spec (&class_ts
);
652 var
= gfc_create_var (tmp
, "class");
655 ctree
= gfc_class_data_get (var
);
656 if (class_ts
.u
.derived
->components
->as
657 && e
->rank
!= class_ts
.u
.derived
->components
->as
->rank
)
661 tree type
= get_scalar_to_descriptor_type (parmse
->expr
,
663 gfc_add_modify (&block
, gfc_conv_descriptor_dtype (ctree
),
664 gfc_get_dtype (type
));
666 tmp
= gfc_class_data_get (parmse
->expr
);
667 if (!POINTER_TYPE_P (TREE_TYPE (tmp
)))
668 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
670 gfc_conv_descriptor_data_set (&block
, ctree
, tmp
);
673 class_array_data_assign (&block
, ctree
, parmse
->expr
, false);
677 if (TREE_TYPE (parmse
->expr
) != TREE_TYPE (ctree
))
678 parmse
->expr
= fold_build1_loc (input_location
, VIEW_CONVERT_EXPR
,
679 TREE_TYPE (ctree
), parmse
->expr
);
680 gfc_add_modify (&block
, ctree
, parmse
->expr
);
683 /* Return the data component, except in the case of scalarized array
684 references, where nullification of the cannot occur and so there
686 if (!elemental
&& full_array
&& copyback
)
688 if (class_ts
.u
.derived
->components
->as
689 && e
->rank
!= class_ts
.u
.derived
->components
->as
->rank
)
692 gfc_add_modify (&parmse
->post
, gfc_class_data_get (parmse
->expr
),
693 gfc_conv_descriptor_data_get (ctree
));
695 class_array_data_assign (&parmse
->post
, parmse
->expr
, ctree
, true);
698 gfc_add_modify (&parmse
->post
, parmse
->expr
, ctree
);
702 ctree
= gfc_class_vptr_get (var
);
704 /* The vptr is the second field of the actual argument.
705 First we have to find the corresponding class reference. */
708 if (class_ref
== NULL
709 && e
->symtree
&& e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
710 tmp
= e
->symtree
->n
.sym
->backend_decl
;
713 /* Remove everything after the last class reference, convert the
714 expression and then recover its tailend once more. */
716 ref
= class_ref
->next
;
717 class_ref
->next
= NULL
;
718 gfc_init_se (&tmpse
, NULL
);
719 gfc_conv_expr (&tmpse
, e
);
720 class_ref
->next
= ref
;
724 gcc_assert (tmp
!= NULL_TREE
);
726 /* Dereference if needs be. */
727 if (TREE_CODE (TREE_TYPE (tmp
)) == REFERENCE_TYPE
)
728 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
730 vptr
= gfc_class_vptr_get (tmp
);
731 gfc_add_modify (&block
, ctree
,
732 fold_convert (TREE_TYPE (ctree
), vptr
));
734 /* Return the vptr component, except in the case of scalarized array
735 references, where the dynamic type cannot change. */
736 if (!elemental
&& full_array
&& copyback
)
737 gfc_add_modify (&parmse
->post
, vptr
,
738 fold_convert (TREE_TYPE (vptr
), ctree
));
740 gcc_assert (!optional
|| (optional
&& !copyback
));
745 cond
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
746 tmp
= gfc_finish_block (&block
);
748 if (optional_alloc_ptr
)
749 tmp2
= build_empty_stmt (input_location
);
752 gfc_init_block (&block
);
754 tmp2
= gfc_conv_descriptor_data_get (gfc_class_data_get (var
));
755 gfc_add_modify (&block
, tmp2
, fold_convert (TREE_TYPE (tmp2
),
757 tmp2
= gfc_finish_block (&block
);
760 tmp
= build3_loc (input_location
, COND_EXPR
, void_type_node
,
762 gfc_add_expr_to_block (&parmse
->pre
, tmp
);
765 gfc_add_block_to_block (&parmse
->pre
, &block
);
767 /* Pass the address of the class object. */
768 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
770 if (optional
&& optional_alloc_ptr
)
771 parmse
->expr
= build3_loc (input_location
, COND_EXPR
,
772 TREE_TYPE (parmse
->expr
),
774 fold_convert (TREE_TYPE (parmse
->expr
),
779 /* Given a class array declaration and an index, returns the address
780 of the referenced element. */
783 gfc_get_class_array_ref (tree index
, tree class_decl
)
785 tree data
= gfc_class_data_get (class_decl
);
786 tree size
= gfc_vtable_size_get (class_decl
);
787 tree offset
= fold_build2_loc (input_location
, MULT_EXPR
,
788 gfc_array_index_type
,
791 data
= gfc_conv_descriptor_data_get (data
);
792 ptr
= fold_convert (pvoid_type_node
, data
);
793 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, offset
);
794 return fold_convert (TREE_TYPE (data
), ptr
);
798 /* Copies one class expression to another, assuming that if either
799 'to' or 'from' are arrays they are packed. Should 'from' be
800 NULL_TREE, the initialization expression for 'to' is used, assuming
801 that the _vptr is set. */
804 gfc_copy_class_to_class (tree from
, tree to
, tree nelems
)
812 vec
<tree
, va_gc
> *args
;
815 stmtblock_t loopbody
;
821 if (from
!= NULL_TREE
)
822 fcn
= gfc_vtable_copy_get (from
);
824 fcn
= gfc_vtable_copy_get (to
);
826 fcn_type
= TREE_TYPE (TREE_TYPE (fcn
));
828 if (from
!= NULL_TREE
)
829 from_data
= gfc_class_data_get (from
);
831 from_data
= gfc_vtable_def_init_get (to
);
833 to_data
= gfc_class_data_get (to
);
835 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data
)))
837 gfc_init_block (&body
);
838 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
839 gfc_array_index_type
, nelems
,
841 nelems
= gfc_evaluate_now (tmp
, &body
);
842 index
= gfc_create_var (gfc_array_index_type
, "S");
844 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data
)))
846 from_ref
= gfc_get_class_array_ref (index
, from
);
847 vec_safe_push (args
, from_ref
);
850 vec_safe_push (args
, from_data
);
852 to_ref
= gfc_get_class_array_ref (index
, to
);
853 vec_safe_push (args
, to_ref
);
855 tmp
= build_call_vec (fcn_type
, fcn
, args
);
857 /* Build the body of the loop. */
858 gfc_init_block (&loopbody
);
859 gfc_add_expr_to_block (&loopbody
, tmp
);
861 /* Build the loop and return. */
862 gfc_init_loopinfo (&loop
);
864 loop
.from
[0] = gfc_index_zero_node
;
865 loop
.loopvar
[0] = index
;
867 gfc_trans_scalarizing_loops (&loop
, &loopbody
);
868 gfc_add_block_to_block (&body
, &loop
.pre
);
869 tmp
= gfc_finish_block (&body
);
870 gfc_cleanup_loop (&loop
);
874 gcc_assert (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data
)));
875 vec_safe_push (args
, from_data
);
876 vec_safe_push (args
, to_data
);
877 tmp
= build_call_vec (fcn_type
, fcn
, args
);
884 gfc_trans_class_array_init_assign (gfc_expr
*rhs
, gfc_expr
*lhs
, gfc_expr
*obj
)
886 gfc_actual_arglist
*actual
;
891 actual
= gfc_get_actual_arglist ();
892 actual
->expr
= gfc_copy_expr (rhs
);
893 actual
->next
= gfc_get_actual_arglist ();
894 actual
->next
->expr
= gfc_copy_expr (lhs
);
895 ppc
= gfc_copy_expr (obj
);
896 gfc_add_vptr_component (ppc
);
897 gfc_add_component_ref (ppc
, "_copy");
898 ppc_code
= gfc_get_code (EXEC_CALL
);
899 ppc_code
->resolved_sym
= ppc
->symtree
->n
.sym
;
900 /* Although '_copy' is set to be elemental in class.c, it is
901 not staying that way. Find out why, sometime.... */
902 ppc_code
->resolved_sym
->attr
.elemental
= 1;
903 ppc_code
->ext
.actual
= actual
;
904 ppc_code
->expr1
= ppc
;
905 /* Since '_copy' is elemental, the scalarizer will take care
906 of arrays in gfc_trans_call. */
907 res
= gfc_trans_call (ppc_code
, false, NULL
, NULL
, false);
908 gfc_free_statements (ppc_code
);
912 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
913 A MEMCPY is needed to copy the full data from the default initializer
914 of the dynamic type. */
917 gfc_trans_class_init_assign (gfc_code
*code
)
921 gfc_se dst
,src
,memsz
;
922 gfc_expr
*lhs
, *rhs
, *sz
;
924 gfc_start_block (&block
);
926 lhs
= gfc_copy_expr (code
->expr1
);
927 gfc_add_data_component (lhs
);
929 rhs
= gfc_copy_expr (code
->expr1
);
930 gfc_add_vptr_component (rhs
);
932 /* Make sure that the component backend_decls have been built, which
933 will not have happened if the derived types concerned have not
935 gfc_get_derived_type (rhs
->ts
.u
.derived
);
936 gfc_add_def_init_component (rhs
);
938 if (code
->expr1
->ts
.type
== BT_CLASS
939 && CLASS_DATA (code
->expr1
)->attr
.dimension
)
940 tmp
= gfc_trans_class_array_init_assign (rhs
, lhs
, code
->expr1
);
943 sz
= gfc_copy_expr (code
->expr1
);
944 gfc_add_vptr_component (sz
);
945 gfc_add_size_component (sz
);
947 gfc_init_se (&dst
, NULL
);
948 gfc_init_se (&src
, NULL
);
949 gfc_init_se (&memsz
, NULL
);
950 gfc_conv_expr (&dst
, lhs
);
951 gfc_conv_expr (&src
, rhs
);
952 gfc_conv_expr (&memsz
, sz
);
953 gfc_add_block_to_block (&block
, &src
.pre
);
954 src
.expr
= gfc_build_addr_expr (NULL_TREE
, src
.expr
);
956 tmp
= gfc_build_memcpy_call (dst
.expr
, src
.expr
, memsz
.expr
);
959 if (code
->expr1
->symtree
->n
.sym
->attr
.optional
960 || code
->expr1
->symtree
->n
.sym
->ns
->proc_name
->attr
.entry_master
)
962 tree present
= gfc_conv_expr_present (code
->expr1
->symtree
->n
.sym
);
963 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
965 build_empty_stmt (input_location
));
968 gfc_add_expr_to_block (&block
, tmp
);
970 return gfc_finish_block (&block
);
974 /* Translate an assignment to a CLASS object
975 (pointer or ordinary assignment). */
978 gfc_trans_class_assign (gfc_expr
*expr1
, gfc_expr
*expr2
, gfc_exec_op op
)
986 gfc_start_block (&block
);
989 while (ref
&& ref
->next
)
992 /* Class valued proc_pointer assignments do not need any further
994 if (ref
&& ref
->type
== REF_COMPONENT
995 && ref
->u
.c
.component
->attr
.proc_pointer
996 && expr2
->expr_type
== EXPR_VARIABLE
997 && expr2
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
998 && op
== EXEC_POINTER_ASSIGN
)
1001 if (expr2
->ts
.type
!= BT_CLASS
)
1003 /* Insert an additional assignment which sets the '_vptr' field. */
1004 gfc_symbol
*vtab
= NULL
;
1007 lhs
= gfc_copy_expr (expr1
);
1008 gfc_add_vptr_component (lhs
);
1010 if (UNLIMITED_POLY (expr1
)
1011 && expr2
->expr_type
== EXPR_NULL
&& expr2
->ts
.type
== BT_UNKNOWN
)
1013 rhs
= gfc_get_null_expr (&expr2
->where
);
1017 if (expr2
->ts
.type
== BT_DERIVED
)
1018 vtab
= gfc_find_derived_vtab (expr2
->ts
.u
.derived
);
1019 else if (expr2
->expr_type
== EXPR_NULL
)
1020 vtab
= gfc_find_derived_vtab (expr1
->ts
.u
.derived
);
1022 vtab
= gfc_find_intrinsic_vtab (&expr2
->ts
);
1025 rhs
= gfc_get_expr ();
1026 rhs
->expr_type
= EXPR_VARIABLE
;
1027 gfc_find_sym_tree (vtab
->name
, vtab
->ns
, 1, &st
);
1031 tmp
= gfc_trans_pointer_assignment (lhs
, rhs
);
1032 gfc_add_expr_to_block (&block
, tmp
);
1034 gfc_free_expr (lhs
);
1035 gfc_free_expr (rhs
);
1037 else if (expr1
->ts
.type
== BT_DERIVED
&& UNLIMITED_POLY (expr2
))
1039 /* F2003:C717 only sequence and bind-C types can come here. */
1040 gcc_assert (expr1
->ts
.u
.derived
->attr
.sequence
1041 || expr1
->ts
.u
.derived
->attr
.is_bind_c
);
1042 gfc_add_data_component (expr2
);
1045 else if (CLASS_DATA (expr2
)->attr
.dimension
&& expr2
->expr_type
!= EXPR_FUNCTION
)
1047 /* Insert an additional assignment which sets the '_vptr' field. */
1048 lhs
= gfc_copy_expr (expr1
);
1049 gfc_add_vptr_component (lhs
);
1051 rhs
= gfc_copy_expr (expr2
);
1052 gfc_add_vptr_component (rhs
);
1054 tmp
= gfc_trans_pointer_assignment (lhs
, rhs
);
1055 gfc_add_expr_to_block (&block
, tmp
);
1057 gfc_free_expr (lhs
);
1058 gfc_free_expr (rhs
);
1061 /* Do the actual CLASS assignment. */
1062 if (expr2
->ts
.type
== BT_CLASS
1063 && !CLASS_DATA (expr2
)->attr
.dimension
)
1065 else if (expr2
->expr_type
!= EXPR_FUNCTION
|| expr2
->ts
.type
!= BT_CLASS
1066 || !CLASS_DATA (expr2
)->attr
.dimension
)
1067 gfc_add_data_component (expr1
);
1071 if (op
== EXEC_ASSIGN
)
1072 tmp
= gfc_trans_assignment (expr1
, expr2
, false, true);
1073 else if (op
== EXEC_POINTER_ASSIGN
)
1074 tmp
= gfc_trans_pointer_assignment (expr1
, expr2
);
1078 gfc_add_expr_to_block (&block
, tmp
);
1080 return gfc_finish_block (&block
);
1084 /* End of prototype trans-class.c */
1088 realloc_lhs_warning (bt type
, bool array
, locus
*where
)
1090 if (array
&& type
!= BT_CLASS
&& type
!= BT_DERIVED
1091 && gfc_option
.warn_realloc_lhs
)
1092 gfc_warning ("Code for reallocating the allocatable array at %L will "
1094 else if (gfc_option
.warn_realloc_lhs_all
)
1095 gfc_warning ("Code for reallocating the allocatable variable at %L "
1096 "will be added", where
);
1100 static tree
gfc_trans_structure_assign (tree dest
, gfc_expr
* expr
);
1101 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping
*,
1104 /* Copy the scalarization loop variables. */
1107 gfc_copy_se_loopvars (gfc_se
* dest
, gfc_se
* src
)
1110 dest
->loop
= src
->loop
;
1114 /* Initialize a simple expression holder.
1116 Care must be taken when multiple se are created with the same parent.
1117 The child se must be kept in sync. The easiest way is to delay creation
1118 of a child se until after after the previous se has been translated. */
1121 gfc_init_se (gfc_se
* se
, gfc_se
* parent
)
1123 memset (se
, 0, sizeof (gfc_se
));
1124 gfc_init_block (&se
->pre
);
1125 gfc_init_block (&se
->post
);
1127 se
->parent
= parent
;
1130 gfc_copy_se_loopvars (se
, parent
);
1134 /* Advances to the next SS in the chain. Use this rather than setting
1135 se->ss = se->ss->next because all the parents needs to be kept in sync.
1139 gfc_advance_se_ss_chain (gfc_se
* se
)
1144 gcc_assert (se
!= NULL
&& se
->ss
!= NULL
&& se
->ss
!= gfc_ss_terminator
);
1147 /* Walk down the parent chain. */
1150 /* Simple consistency check. */
1151 gcc_assert (p
->parent
== NULL
|| p
->parent
->ss
== p
->ss
1152 || p
->parent
->ss
->nested_ss
== p
->ss
);
1154 /* If we were in a nested loop, the next scalarized expression can be
1155 on the parent ss' next pointer. Thus we should not take the next
1156 pointer blindly, but rather go up one nest level as long as next
1157 is the end of chain. */
1159 while (ss
->next
== gfc_ss_terminator
&& ss
->parent
!= NULL
)
1169 /* Ensures the result of the expression as either a temporary variable
1170 or a constant so that it can be used repeatedly. */
1173 gfc_make_safe_expr (gfc_se
* se
)
1177 if (CONSTANT_CLASS_P (se
->expr
))
1180 /* We need a temporary for this result. */
1181 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
1182 gfc_add_modify (&se
->pre
, var
, se
->expr
);
1187 /* Return an expression which determines if a dummy parameter is present.
1188 Also used for arguments to procedures with multiple entry points. */
1191 gfc_conv_expr_present (gfc_symbol
* sym
)
1195 gcc_assert (sym
->attr
.dummy
);
1196 decl
= gfc_get_symbol_decl (sym
);
1198 /* Intrinsic scalars with VALUE attribute which are passed by value
1199 use a hidden argument to denote the present status. */
1200 if (sym
->attr
.value
&& sym
->ts
.type
!= BT_CHARACTER
1201 && sym
->ts
.type
!= BT_CLASS
&& sym
->ts
.type
!= BT_DERIVED
1202 && !sym
->attr
.dimension
)
1204 char name
[GFC_MAX_SYMBOL_LEN
+ 2];
1207 gcc_assert (TREE_CODE (decl
) == PARM_DECL
);
1209 strcpy (&name
[1], sym
->name
);
1210 tree_name
= get_identifier (name
);
1212 /* Walk function argument list to find hidden arg. */
1213 cond
= DECL_ARGUMENTS (DECL_CONTEXT (decl
));
1214 for ( ; cond
!= NULL_TREE
; cond
= TREE_CHAIN (cond
))
1215 if (DECL_NAME (cond
) == tree_name
)
1222 if (TREE_CODE (decl
) != PARM_DECL
)
1224 /* Array parameters use a temporary descriptor, we want the real
1226 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl
))
1227 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl
)));
1228 decl
= GFC_DECL_SAVED_DESCRIPTOR (decl
);
1231 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
, decl
,
1232 fold_convert (TREE_TYPE (decl
), null_pointer_node
));
1234 /* Fortran 2008 allows to pass null pointers and non-associated pointers
1235 as actual argument to denote absent dummies. For array descriptors,
1236 we thus also need to check the array descriptor. For BT_CLASS, it
1237 can also occur for scalars and F2003 due to type->class wrapping and
1238 class->class wrapping. Note further that BT_CLASS always uses an
1239 array descriptor for arrays, also for explicit-shape/assumed-size. */
1241 if (!sym
->attr
.allocatable
1242 && ((sym
->ts
.type
!= BT_CLASS
&& !sym
->attr
.pointer
)
1243 || (sym
->ts
.type
== BT_CLASS
1244 && !CLASS_DATA (sym
)->attr
.allocatable
1245 && !CLASS_DATA (sym
)->attr
.class_pointer
))
1246 && ((gfc_option
.allow_std
& GFC_STD_F2008
) != 0
1247 || sym
->ts
.type
== BT_CLASS
))
1251 if ((sym
->as
&& (sym
->as
->type
== AS_ASSUMED_SHAPE
1252 || sym
->as
->type
== AS_ASSUMED_RANK
1253 || sym
->attr
.codimension
))
1254 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->as
))
1256 tmp
= build_fold_indirect_ref_loc (input_location
, decl
);
1257 if (sym
->ts
.type
== BT_CLASS
)
1258 tmp
= gfc_class_data_get (tmp
);
1259 tmp
= gfc_conv_array_data (tmp
);
1261 else if (sym
->ts
.type
== BT_CLASS
)
1262 tmp
= gfc_class_data_get (decl
);
1266 if (tmp
!= NULL_TREE
)
1268 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
, tmp
,
1269 fold_convert (TREE_TYPE (tmp
), null_pointer_node
));
1270 cond
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1271 boolean_type_node
, cond
, tmp
);
1279 /* Converts a missing, dummy argument into a null or zero. */
1282 gfc_conv_missing_dummy (gfc_se
* se
, gfc_expr
* arg
, gfc_typespec ts
, int kind
)
1287 present
= gfc_conv_expr_present (arg
->symtree
->n
.sym
);
1291 /* Create a temporary and convert it to the correct type. */
1292 tmp
= gfc_get_int_type (kind
);
1293 tmp
= fold_convert (tmp
, build_fold_indirect_ref_loc (input_location
,
1296 /* Test for a NULL value. */
1297 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
), present
,
1298 tmp
, fold_convert (TREE_TYPE (tmp
), integer_one_node
));
1299 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
1300 se
->expr
= gfc_build_addr_expr (NULL_TREE
, tmp
);
1304 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (se
->expr
),
1306 build_zero_cst (TREE_TYPE (se
->expr
)));
1307 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
1311 if (ts
.type
== BT_CHARACTER
)
1313 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
1314 tmp
= fold_build3_loc (input_location
, COND_EXPR
, gfc_charlen_type_node
,
1315 present
, se
->string_length
, tmp
);
1316 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
1317 se
->string_length
= tmp
;
1323 /* Get the character length of an expression, looking through gfc_refs
1327 gfc_get_expr_charlen (gfc_expr
*e
)
1332 gcc_assert (e
->expr_type
== EXPR_VARIABLE
1333 && e
->ts
.type
== BT_CHARACTER
);
1335 length
= NULL
; /* To silence compiler warning. */
1337 if (is_subref_array (e
) && e
->ts
.u
.cl
->length
)
1340 gfc_init_se (&tmpse
, NULL
);
1341 gfc_conv_expr_type (&tmpse
, e
->ts
.u
.cl
->length
, gfc_charlen_type_node
);
1342 e
->ts
.u
.cl
->backend_decl
= tmpse
.expr
;
1346 /* First candidate: if the variable is of type CHARACTER, the
1347 expression's length could be the length of the character
1349 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
)
1350 length
= e
->symtree
->n
.sym
->ts
.u
.cl
->backend_decl
;
1352 /* Look through the reference chain for component references. */
1353 for (r
= e
->ref
; r
; r
= r
->next
)
1358 if (r
->u
.c
.component
->ts
.type
== BT_CHARACTER
)
1359 length
= r
->u
.c
.component
->ts
.u
.cl
->backend_decl
;
1367 /* We should never got substring references here. These will be
1368 broken down by the scalarizer. */
1374 gcc_assert (length
!= NULL
);
1379 /* Return for an expression the backend decl of the coarray. */
1382 get_tree_for_caf_expr (gfc_expr
*expr
)
1384 tree caf_decl
= NULL_TREE
;
1387 gcc_assert (expr
&& expr
->expr_type
== EXPR_VARIABLE
);
1388 if (expr
->symtree
->n
.sym
->attr
.codimension
)
1389 caf_decl
= expr
->symtree
->n
.sym
->backend_decl
;
1391 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1392 if (ref
->type
== REF_COMPONENT
)
1394 gfc_component
*comp
= ref
->u
.c
.component
;
1395 if (comp
->attr
.pointer
|| comp
->attr
.allocatable
)
1396 caf_decl
= NULL_TREE
;
1397 if (comp
->attr
.codimension
)
1398 caf_decl
= comp
->backend_decl
;
1401 gcc_assert (caf_decl
!= NULL_TREE
);
1406 /* For each character array constructor subexpression without a ts.u.cl->length,
1407 replace it by its first element (if there aren't any elements, the length
1408 should already be set to zero). */
1411 flatten_array_ctors_without_strlen (gfc_expr
* e
)
1413 gfc_actual_arglist
* arg
;
1419 switch (e
->expr_type
)
1423 flatten_array_ctors_without_strlen (e
->value
.op
.op1
);
1424 flatten_array_ctors_without_strlen (e
->value
.op
.op2
);
1428 /* TODO: Implement as with EXPR_FUNCTION when needed. */
1432 for (arg
= e
->value
.function
.actual
; arg
; arg
= arg
->next
)
1433 flatten_array_ctors_without_strlen (arg
->expr
);
1438 /* We've found what we're looking for. */
1439 if (e
->ts
.type
== BT_CHARACTER
&& !e
->ts
.u
.cl
->length
)
1444 gcc_assert (e
->value
.constructor
);
1446 c
= gfc_constructor_first (e
->value
.constructor
);
1450 flatten_array_ctors_without_strlen (new_expr
);
1451 gfc_replace_expr (e
, new_expr
);
1455 /* Otherwise, fall through to handle constructor elements. */
1456 case EXPR_STRUCTURE
:
1457 for (c
= gfc_constructor_first (e
->value
.constructor
);
1458 c
; c
= gfc_constructor_next (c
))
1459 flatten_array_ctors_without_strlen (c
->expr
);
1469 /* Generate code to initialize a string length variable. Returns the
1470 value. For array constructors, cl->length might be NULL and in this case,
1471 the first element of the constructor is needed. expr is the original
1472 expression so we can access it but can be NULL if this is not needed. */
1475 gfc_conv_string_length (gfc_charlen
* cl
, gfc_expr
* expr
, stmtblock_t
* pblock
)
1479 gfc_init_se (&se
, NULL
);
1483 && TREE_CODE (cl
->backend_decl
) == VAR_DECL
)
1486 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
1487 "flatten" array constructors by taking their first element; all elements
1488 should be the same length or a cl->length should be present. */
1491 gfc_expr
* expr_flat
;
1493 expr_flat
= gfc_copy_expr (expr
);
1494 flatten_array_ctors_without_strlen (expr_flat
);
1495 gfc_resolve_expr (expr_flat
);
1497 gfc_conv_expr (&se
, expr_flat
);
1498 gfc_add_block_to_block (pblock
, &se
.pre
);
1499 cl
->backend_decl
= convert (gfc_charlen_type_node
, se
.string_length
);
1501 gfc_free_expr (expr_flat
);
1505 /* Convert cl->length. */
1507 gcc_assert (cl
->length
);
1509 gfc_conv_expr_type (&se
, cl
->length
, gfc_charlen_type_node
);
1510 se
.expr
= fold_build2_loc (input_location
, MAX_EXPR
, gfc_charlen_type_node
,
1511 se
.expr
, build_int_cst (gfc_charlen_type_node
, 0));
1512 gfc_add_block_to_block (pblock
, &se
.pre
);
1514 if (cl
->backend_decl
)
1515 gfc_add_modify (pblock
, cl
->backend_decl
, se
.expr
);
1517 cl
->backend_decl
= gfc_evaluate_now (se
.expr
, pblock
);
1522 gfc_conv_substring (gfc_se
* se
, gfc_ref
* ref
, int kind
,
1523 const char *name
, locus
*where
)
1533 type
= gfc_get_character_type (kind
, ref
->u
.ss
.length
);
1534 type
= build_pointer_type (type
);
1536 gfc_init_se (&start
, se
);
1537 gfc_conv_expr_type (&start
, ref
->u
.ss
.start
, gfc_charlen_type_node
);
1538 gfc_add_block_to_block (&se
->pre
, &start
.pre
);
1540 if (integer_onep (start
.expr
))
1541 gfc_conv_string_parameter (se
);
1546 /* Avoid multiple evaluation of substring start. */
1547 if (!CONSTANT_CLASS_P (tmp
) && !DECL_P (tmp
))
1548 start
.expr
= gfc_evaluate_now (start
.expr
, &se
->pre
);
1550 /* Change the start of the string. */
1551 if (TYPE_STRING_FLAG (TREE_TYPE (se
->expr
)))
1554 tmp
= build_fold_indirect_ref_loc (input_location
,
1556 tmp
= gfc_build_array_ref (tmp
, start
.expr
, NULL
);
1557 se
->expr
= gfc_build_addr_expr (type
, tmp
);
1560 /* Length = end + 1 - start. */
1561 gfc_init_se (&end
, se
);
1562 if (ref
->u
.ss
.end
== NULL
)
1563 end
.expr
= se
->string_length
;
1566 gfc_conv_expr_type (&end
, ref
->u
.ss
.end
, gfc_charlen_type_node
);
1567 gfc_add_block_to_block (&se
->pre
, &end
.pre
);
1571 if (!CONSTANT_CLASS_P (tmp
) && !DECL_P (tmp
))
1572 end
.expr
= gfc_evaluate_now (end
.expr
, &se
->pre
);
1574 if (gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
)
1576 tree nonempty
= fold_build2_loc (input_location
, LE_EXPR
,
1577 boolean_type_node
, start
.expr
,
1580 /* Check lower bound. */
1581 fault
= fold_build2_loc (input_location
, LT_EXPR
, boolean_type_node
,
1583 build_int_cst (gfc_charlen_type_node
, 1));
1584 fault
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1585 boolean_type_node
, nonempty
, fault
);
1587 asprintf (&msg
, "Substring out of bounds: lower bound (%%ld) of '%s' "
1588 "is less than one", name
);
1590 asprintf (&msg
, "Substring out of bounds: lower bound (%%ld)"
1591 "is less than one");
1592 gfc_trans_runtime_check (true, false, fault
, &se
->pre
, where
, msg
,
1593 fold_convert (long_integer_type_node
,
1597 /* Check upper bound. */
1598 fault
= fold_build2_loc (input_location
, GT_EXPR
, boolean_type_node
,
1599 end
.expr
, se
->string_length
);
1600 fault
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1601 boolean_type_node
, nonempty
, fault
);
1603 asprintf (&msg
, "Substring out of bounds: upper bound (%%ld) of '%s' "
1604 "exceeds string length (%%ld)", name
);
1606 asprintf (&msg
, "Substring out of bounds: upper bound (%%ld) "
1607 "exceeds string length (%%ld)");
1608 gfc_trans_runtime_check (true, false, fault
, &se
->pre
, where
, msg
,
1609 fold_convert (long_integer_type_node
, end
.expr
),
1610 fold_convert (long_integer_type_node
,
1611 se
->string_length
));
1615 /* Try to calculate the length from the start and end expressions. */
1617 && gfc_dep_difference (ref
->u
.ss
.end
, ref
->u
.ss
.start
, &length
))
1621 i_len
= mpz_get_si (length
) + 1;
1625 tmp
= build_int_cst (gfc_charlen_type_node
, i_len
);
1626 mpz_clear (length
); /* Was initialized by gfc_dep_difference. */
1630 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_charlen_type_node
,
1631 end
.expr
, start
.expr
);
1632 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
, gfc_charlen_type_node
,
1633 build_int_cst (gfc_charlen_type_node
, 1), tmp
);
1634 tmp
= fold_build2_loc (input_location
, MAX_EXPR
, gfc_charlen_type_node
,
1635 tmp
, build_int_cst (gfc_charlen_type_node
, 0));
1638 se
->string_length
= tmp
;
1642 /* Convert a derived type component reference. */
1645 gfc_conv_component_ref (gfc_se
* se
, gfc_ref
* ref
)
1652 c
= ref
->u
.c
.component
;
1654 gcc_assert (c
->backend_decl
);
1656 field
= c
->backend_decl
;
1657 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
1660 /* Components can correspond to fields of different containing
1661 types, as components are created without context, whereas
1662 a concrete use of a component has the type of decl as context.
1663 So, if the type doesn't match, we search the corresponding
1664 FIELD_DECL in the parent type. To not waste too much time
1665 we cache this result in norestrict_decl. */
1667 if (DECL_FIELD_CONTEXT (field
) != TREE_TYPE (decl
))
1669 tree f2
= c
->norestrict_decl
;
1670 if (!f2
|| DECL_FIELD_CONTEXT (f2
) != TREE_TYPE (decl
))
1671 for (f2
= TYPE_FIELDS (TREE_TYPE (decl
)); f2
; f2
= DECL_CHAIN (f2
))
1672 if (TREE_CODE (f2
) == FIELD_DECL
1673 && DECL_NAME (f2
) == DECL_NAME (field
))
1676 c
->norestrict_decl
= f2
;
1680 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (field
),
1681 decl
, field
, NULL_TREE
);
1685 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.proc_pointer
)
1687 tmp
= c
->ts
.u
.cl
->backend_decl
;
1688 /* Components must always be constant length. */
1689 gcc_assert (tmp
&& INTEGER_CST_P (tmp
));
1690 se
->string_length
= tmp
;
1693 if (((c
->attr
.pointer
|| c
->attr
.allocatable
)
1694 && (!c
->attr
.dimension
&& !c
->attr
.codimension
)
1695 && c
->ts
.type
!= BT_CHARACTER
)
1696 || c
->attr
.proc_pointer
)
1697 se
->expr
= build_fold_indirect_ref_loc (input_location
,
1702 /* This function deals with component references to components of the
1703 parent type for derived type extensions. */
1705 conv_parent_component_references (gfc_se
* se
, gfc_ref
* ref
)
1713 c
= ref
->u
.c
.component
;
1715 /* Return if the component is in the parent type. */
1716 for (cmp
= dt
->components
; cmp
; cmp
= cmp
->next
)
1717 if (strcmp (c
->name
, cmp
->name
) == 0)
1720 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
1721 parent
.type
= REF_COMPONENT
;
1723 parent
.u
.c
.sym
= dt
;
1724 parent
.u
.c
.component
= dt
->components
;
1726 if (dt
->backend_decl
== NULL
)
1727 gfc_get_derived_type (dt
);
1729 /* Build the reference and call self. */
1730 gfc_conv_component_ref (se
, &parent
);
1731 parent
.u
.c
.sym
= dt
->components
->ts
.u
.derived
;
1732 parent
.u
.c
.component
= c
;
1733 conv_parent_component_references (se
, &parent
);
1736 /* Return the contents of a variable. Also handles reference/pointer
1737 variables (all Fortran pointer references are implicit). */
1740 gfc_conv_variable (gfc_se
* se
, gfc_expr
* expr
)
1745 tree parent_decl
= NULL_TREE
;
1748 bool alternate_entry
;
1751 sym
= expr
->symtree
->n
.sym
;
1755 gfc_ss_info
*ss_info
= ss
->info
;
1757 /* Check that something hasn't gone horribly wrong. */
1758 gcc_assert (ss
!= gfc_ss_terminator
);
1759 gcc_assert (ss_info
->expr
== expr
);
1761 /* A scalarized term. We already know the descriptor. */
1762 se
->expr
= ss_info
->data
.array
.descriptor
;
1763 se
->string_length
= ss_info
->string_length
;
1764 ref
= ss_info
->data
.array
.ref
;
1766 gcc_assert (ref
->type
== REF_ARRAY
1767 && ref
->u
.ar
.type
!= AR_ELEMENT
);
1769 gfc_conv_tmp_array_ref (se
);
1773 tree se_expr
= NULL_TREE
;
1775 se
->expr
= gfc_get_symbol_decl (sym
);
1777 /* Deal with references to a parent results or entries by storing
1778 the current_function_decl and moving to the parent_decl. */
1779 return_value
= sym
->attr
.function
&& sym
->result
== sym
;
1780 alternate_entry
= sym
->attr
.function
&& sym
->attr
.entry
1781 && sym
->result
== sym
;
1782 entry_master
= sym
->attr
.result
1783 && sym
->ns
->proc_name
->attr
.entry_master
1784 && !gfc_return_by_reference (sym
->ns
->proc_name
);
1785 if (current_function_decl
)
1786 parent_decl
= DECL_CONTEXT (current_function_decl
);
1788 if ((se
->expr
== parent_decl
&& return_value
)
1789 || (sym
->ns
&& sym
->ns
->proc_name
1791 && sym
->ns
->proc_name
->backend_decl
== parent_decl
1792 && (alternate_entry
|| entry_master
)))
1797 /* Special case for assigning the return value of a function.
1798 Self recursive functions must have an explicit return value. */
1799 if (return_value
&& (se
->expr
== current_function_decl
|| parent_flag
))
1800 se_expr
= gfc_get_fake_result_decl (sym
, parent_flag
);
1802 /* Similarly for alternate entry points. */
1803 else if (alternate_entry
1804 && (sym
->ns
->proc_name
->backend_decl
== current_function_decl
1807 gfc_entry_list
*el
= NULL
;
1809 for (el
= sym
->ns
->entries
; el
; el
= el
->next
)
1812 se_expr
= gfc_get_fake_result_decl (sym
, parent_flag
);
1817 else if (entry_master
1818 && (sym
->ns
->proc_name
->backend_decl
== current_function_decl
1820 se_expr
= gfc_get_fake_result_decl (sym
, parent_flag
);
1825 /* Procedure actual arguments. */
1826 else if (sym
->attr
.flavor
== FL_PROCEDURE
1827 && se
->expr
!= current_function_decl
)
1829 if (!sym
->attr
.dummy
&& !sym
->attr
.proc_pointer
)
1831 gcc_assert (TREE_CODE (se
->expr
) == FUNCTION_DECL
);
1832 se
->expr
= gfc_build_addr_expr (NULL_TREE
, se
->expr
);
1838 /* Dereference the expression, where needed. Since characters
1839 are entirely different from other types, they are treated
1841 if (sym
->ts
.type
== BT_CHARACTER
)
1843 /* Dereference character pointer dummy arguments
1845 if ((sym
->attr
.pointer
|| sym
->attr
.allocatable
)
1847 || sym
->attr
.function
1848 || sym
->attr
.result
))
1849 se
->expr
= build_fold_indirect_ref_loc (input_location
,
1853 else if (!sym
->attr
.value
)
1855 /* Dereference non-character scalar dummy arguments. */
1856 if (sym
->attr
.dummy
&& !sym
->attr
.dimension
1857 && !(sym
->attr
.codimension
&& sym
->attr
.allocatable
))
1858 se
->expr
= build_fold_indirect_ref_loc (input_location
,
1861 /* Dereference scalar hidden result. */
1862 if (gfc_option
.flag_f2c
&& sym
->ts
.type
== BT_COMPLEX
1863 && (sym
->attr
.function
|| sym
->attr
.result
)
1864 && !sym
->attr
.dimension
&& !sym
->attr
.pointer
1865 && !sym
->attr
.always_explicit
)
1866 se
->expr
= build_fold_indirect_ref_loc (input_location
,
1869 /* Dereference non-character pointer variables.
1870 These must be dummies, results, or scalars. */
1871 if ((sym
->attr
.pointer
|| sym
->attr
.allocatable
1872 || gfc_is_associate_pointer (sym
)
1873 || (sym
->as
&& sym
->as
->type
== AS_ASSUMED_RANK
))
1875 || sym
->attr
.function
1877 || (!sym
->attr
.dimension
1878 && (!sym
->attr
.codimension
|| !sym
->attr
.allocatable
))))
1879 se
->expr
= build_fold_indirect_ref_loc (input_location
,
1886 /* For character variables, also get the length. */
1887 if (sym
->ts
.type
== BT_CHARACTER
)
1889 /* If the character length of an entry isn't set, get the length from
1890 the master function instead. */
1891 if (sym
->attr
.entry
&& !sym
->ts
.u
.cl
->backend_decl
)
1892 se
->string_length
= sym
->ns
->proc_name
->ts
.u
.cl
->backend_decl
;
1894 se
->string_length
= sym
->ts
.u
.cl
->backend_decl
;
1895 gcc_assert (se
->string_length
);
1903 /* Return the descriptor if that's what we want and this is an array
1904 section reference. */
1905 if (se
->descriptor_only
&& ref
->u
.ar
.type
!= AR_ELEMENT
)
1907 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
1908 /* Return the descriptor for array pointers and allocations. */
1909 if (se
->want_pointer
1910 && ref
->next
== NULL
&& (se
->descriptor_only
))
1913 gfc_conv_array_ref (se
, &ref
->u
.ar
, expr
, &expr
->where
);
1914 /* Return a pointer to an element. */
1918 if (ref
->u
.c
.sym
->attr
.extension
)
1919 conv_parent_component_references (se
, ref
);
1921 gfc_conv_component_ref (se
, ref
);
1922 if (!ref
->next
&& ref
->u
.c
.sym
->attr
.codimension
1923 && se
->want_pointer
&& se
->descriptor_only
)
1929 gfc_conv_substring (se
, ref
, expr
->ts
.kind
,
1930 expr
->symtree
->name
, &expr
->where
);
1939 /* Pointer assignment, allocation or pass by reference. Arrays are handled
1941 if (se
->want_pointer
)
1943 if (expr
->ts
.type
== BT_CHARACTER
&& !gfc_is_proc_ptr_comp (expr
))
1944 gfc_conv_string_parameter (se
);
1946 se
->expr
= gfc_build_addr_expr (NULL_TREE
, se
->expr
);
1951 /* Unary ops are easy... Or they would be if ! was a valid op. */
1954 gfc_conv_unary_op (enum tree_code code
, gfc_se
* se
, gfc_expr
* expr
)
1959 gcc_assert (expr
->ts
.type
!= BT_CHARACTER
);
1960 /* Initialize the operand. */
1961 gfc_init_se (&operand
, se
);
1962 gfc_conv_expr_val (&operand
, expr
->value
.op
.op1
);
1963 gfc_add_block_to_block (&se
->pre
, &operand
.pre
);
1965 type
= gfc_typenode_for_spec (&expr
->ts
);
1967 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
1968 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
1969 All other unary operators have an equivalent GIMPLE unary operator. */
1970 if (code
== TRUTH_NOT_EXPR
)
1971 se
->expr
= fold_build2_loc (input_location
, EQ_EXPR
, type
, operand
.expr
,
1972 build_int_cst (type
, 0));
1974 se
->expr
= fold_build1_loc (input_location
, code
, type
, operand
.expr
);
1978 /* Expand power operator to optimal multiplications when a value is raised
1979 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
1980 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
1981 Programming", 3rd Edition, 1998. */
1983 /* This code is mostly duplicated from expand_powi in the backend.
1984 We establish the "optimal power tree" lookup table with the defined size.
1985 The items in the table are the exponents used to calculate the index
1986 exponents. Any integer n less than the value can get an "addition chain",
1987 with the first node being one. */
1988 #define POWI_TABLE_SIZE 256
1990 /* The table is from builtins.c. */
1991 static const unsigned char powi_table
[POWI_TABLE_SIZE
] =
1993 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
1994 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
1995 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
1996 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
1997 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
1998 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
1999 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2000 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2001 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2002 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2003 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2004 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2005 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2006 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2007 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2008 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2009 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2010 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2011 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2012 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2013 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2014 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2015 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2016 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2017 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2018 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2019 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2020 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2021 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2022 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2023 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2024 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2027 /* If n is larger than lookup table's max index, we use the "window
2029 #define POWI_WINDOW_SIZE 3
2031 /* Recursive function to expand the power operator. The temporary
2032 values are put in tmpvar. The function returns tmpvar[1] ** n. */
2034 gfc_conv_powi (gfc_se
* se
, unsigned HOST_WIDE_INT n
, tree
* tmpvar
)
2041 if (n
< POWI_TABLE_SIZE
)
2046 op0
= gfc_conv_powi (se
, n
- powi_table
[n
], tmpvar
);
2047 op1
= gfc_conv_powi (se
, powi_table
[n
], tmpvar
);
2051 digit
= n
& ((1 << POWI_WINDOW_SIZE
) - 1);
2052 op0
= gfc_conv_powi (se
, n
- digit
, tmpvar
);
2053 op1
= gfc_conv_powi (se
, digit
, tmpvar
);
2057 op0
= gfc_conv_powi (se
, n
>> 1, tmpvar
);
2061 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, TREE_TYPE (op0
), op0
, op1
);
2062 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
2064 if (n
< POWI_TABLE_SIZE
)
2071 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
2072 return 1. Else return 0 and a call to runtime library functions
2073 will have to be built. */
2075 gfc_conv_cst_int_power (gfc_se
* se
, tree lhs
, tree rhs
)
2080 tree vartmp
[POWI_TABLE_SIZE
];
2082 unsigned HOST_WIDE_INT n
;
2085 /* If exponent is too large, we won't expand it anyway, so don't bother
2086 with large integer values. */
2087 if (!TREE_INT_CST (rhs
).fits_shwi ())
2090 m
= TREE_INT_CST (rhs
).to_shwi ();
2091 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
2092 of the asymmetric range of the integer type. */
2093 n
= (unsigned HOST_WIDE_INT
) (m
< 0 ? -m
: m
);
2095 type
= TREE_TYPE (lhs
);
2096 sgn
= tree_int_cst_sgn (rhs
);
2098 if (((FLOAT_TYPE_P (type
) && !flag_unsafe_math_optimizations
)
2099 || optimize_size
) && (m
> 2 || m
< -1))
2105 se
->expr
= gfc_build_const (type
, integer_one_node
);
2109 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
2110 if ((sgn
== -1) && (TREE_CODE (type
) == INTEGER_TYPE
))
2112 tmp
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
2113 lhs
, build_int_cst (TREE_TYPE (lhs
), -1));
2114 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
2115 lhs
, build_int_cst (TREE_TYPE (lhs
), 1));
2118 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
2121 tmp
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
2122 boolean_type_node
, tmp
, cond
);
2123 se
->expr
= fold_build3_loc (input_location
, COND_EXPR
, type
,
2124 tmp
, build_int_cst (type
, 1),
2125 build_int_cst (type
, 0));
2129 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
2130 tmp
= fold_build3_loc (input_location
, COND_EXPR
, type
, tmp
,
2131 build_int_cst (type
, -1),
2132 build_int_cst (type
, 0));
2133 se
->expr
= fold_build3_loc (input_location
, COND_EXPR
, type
,
2134 cond
, build_int_cst (type
, 1), tmp
);
2138 memset (vartmp
, 0, sizeof (vartmp
));
2142 tmp
= gfc_build_const (type
, integer_one_node
);
2143 vartmp
[1] = fold_build2_loc (input_location
, RDIV_EXPR
, type
, tmp
,
2147 se
->expr
= gfc_conv_powi (se
, n
, vartmp
);
2153 /* Power op (**). Constant integer exponent has special handling. */
2156 gfc_conv_power_op (gfc_se
* se
, gfc_expr
* expr
)
2158 tree gfc_int4_type_node
;
2161 int res_ikind_1
, res_ikind_2
;
2166 gfc_init_se (&lse
, se
);
2167 gfc_conv_expr_val (&lse
, expr
->value
.op
.op1
);
2168 lse
.expr
= gfc_evaluate_now (lse
.expr
, &lse
.pre
);
2169 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
2171 gfc_init_se (&rse
, se
);
2172 gfc_conv_expr_val (&rse
, expr
->value
.op
.op2
);
2173 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
2175 if (expr
->value
.op
.op2
->ts
.type
== BT_INTEGER
2176 && expr
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
)
2177 if (gfc_conv_cst_int_power (se
, lse
.expr
, rse
.expr
))
2180 gfc_int4_type_node
= gfc_get_int_type (4);
2182 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
2183 library routine. But in the end, we have to convert the result back
2184 if this case applies -- with res_ikind_K, we keep track whether operand K
2185 falls into this case. */
2189 kind
= expr
->value
.op
.op1
->ts
.kind
;
2190 switch (expr
->value
.op
.op2
->ts
.type
)
2193 ikind
= expr
->value
.op
.op2
->ts
.kind
;
2198 rse
.expr
= convert (gfc_int4_type_node
, rse
.expr
);
2199 res_ikind_2
= ikind
;
2221 if (expr
->value
.op
.op1
->ts
.type
== BT_INTEGER
)
2223 lse
.expr
= convert (gfc_int4_type_node
, lse
.expr
);
2250 switch (expr
->value
.op
.op1
->ts
.type
)
2253 if (kind
== 3) /* Case 16 was not handled properly above. */
2255 fndecl
= gfor_fndecl_math_powi
[kind
][ikind
].integer
;
2259 /* Use builtins for real ** int4. */
2265 fndecl
= builtin_decl_explicit (BUILT_IN_POWIF
);
2269 fndecl
= builtin_decl_explicit (BUILT_IN_POWI
);
2273 fndecl
= builtin_decl_explicit (BUILT_IN_POWIL
);
2277 /* Use the __builtin_powil() only if real(kind=16) is
2278 actually the C long double type. */
2279 if (!gfc_real16_is_float128
)
2280 fndecl
= builtin_decl_explicit (BUILT_IN_POWIL
);
2288 /* If we don't have a good builtin for this, go for the
2289 library function. */
2291 fndecl
= gfor_fndecl_math_powi
[kind
][ikind
].real
;
2295 fndecl
= gfor_fndecl_math_powi
[kind
][ikind
].cmplx
;
2304 fndecl
= gfc_builtin_decl_for_float_kind (BUILT_IN_POW
, kind
);
2308 fndecl
= gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW
, kind
);
2316 se
->expr
= build_call_expr_loc (input_location
,
2317 fndecl
, 2, lse
.expr
, rse
.expr
);
2319 /* Convert the result back if it is of wrong integer kind. */
2320 if (res_ikind_1
!= -1 && res_ikind_2
!= -1)
2322 /* We want the maximum of both operand kinds as result. */
2323 if (res_ikind_1
< res_ikind_2
)
2324 res_ikind_1
= res_ikind_2
;
2325 se
->expr
= convert (gfc_get_int_type (res_ikind_1
), se
->expr
);
2330 /* Generate code to allocate a string temporary. */
2333 gfc_conv_string_tmp (gfc_se
* se
, tree type
, tree len
)
2338 if (gfc_can_put_var_on_stack (len
))
2340 /* Create a temporary variable to hold the result. */
2341 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
2342 gfc_charlen_type_node
, len
,
2343 build_int_cst (gfc_charlen_type_node
, 1));
2344 tmp
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
, tmp
);
2346 if (TREE_CODE (TREE_TYPE (type
)) == ARRAY_TYPE
)
2347 tmp
= build_array_type (TREE_TYPE (TREE_TYPE (type
)), tmp
);
2349 tmp
= build_array_type (TREE_TYPE (type
), tmp
);
2351 var
= gfc_create_var (tmp
, "str");
2352 var
= gfc_build_addr_expr (type
, var
);
2356 /* Allocate a temporary to hold the result. */
2357 var
= gfc_create_var (type
, "pstr");
2358 tmp
= gfc_call_malloc (&se
->pre
, type
,
2359 fold_build2_loc (input_location
, MULT_EXPR
,
2360 TREE_TYPE (len
), len
,
2361 fold_convert (TREE_TYPE (len
),
2362 TYPE_SIZE (type
))));
2363 gfc_add_modify (&se
->pre
, var
, tmp
);
2365 /* Free the temporary afterwards. */
2366 tmp
= gfc_call_free (convert (pvoid_type_node
, var
));
2367 gfc_add_expr_to_block (&se
->post
, tmp
);
2374 /* Handle a string concatenation operation. A temporary will be allocated to
2378 gfc_conv_concat_op (gfc_se
* se
, gfc_expr
* expr
)
2381 tree len
, type
, var
, tmp
, fndecl
;
2383 gcc_assert (expr
->value
.op
.op1
->ts
.type
== BT_CHARACTER
2384 && expr
->value
.op
.op2
->ts
.type
== BT_CHARACTER
);
2385 gcc_assert (expr
->value
.op
.op1
->ts
.kind
== expr
->value
.op
.op2
->ts
.kind
);
2387 gfc_init_se (&lse
, se
);
2388 gfc_conv_expr (&lse
, expr
->value
.op
.op1
);
2389 gfc_conv_string_parameter (&lse
);
2390 gfc_init_se (&rse
, se
);
2391 gfc_conv_expr (&rse
, expr
->value
.op
.op2
);
2392 gfc_conv_string_parameter (&rse
);
2394 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
2395 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
2397 type
= gfc_get_character_type (expr
->ts
.kind
, expr
->ts
.u
.cl
);
2398 len
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
2399 if (len
== NULL_TREE
)
2401 len
= fold_build2_loc (input_location
, PLUS_EXPR
,
2402 TREE_TYPE (lse
.string_length
),
2403 lse
.string_length
, rse
.string_length
);
2406 type
= build_pointer_type (type
);
2408 var
= gfc_conv_string_tmp (se
, type
, len
);
2410 /* Do the actual concatenation. */
2411 if (expr
->ts
.kind
== 1)
2412 fndecl
= gfor_fndecl_concat_string
;
2413 else if (expr
->ts
.kind
== 4)
2414 fndecl
= gfor_fndecl_concat_string_char4
;
2418 tmp
= build_call_expr_loc (input_location
,
2419 fndecl
, 6, len
, var
, lse
.string_length
, lse
.expr
,
2420 rse
.string_length
, rse
.expr
);
2421 gfc_add_expr_to_block (&se
->pre
, tmp
);
2423 /* Add the cleanup for the operands. */
2424 gfc_add_block_to_block (&se
->pre
, &rse
.post
);
2425 gfc_add_block_to_block (&se
->pre
, &lse
.post
);
2428 se
->string_length
= len
;
2431 /* Translates an op expression. Common (binary) cases are handled by this
2432 function, others are passed on. Recursion is used in either case.
2433 We use the fact that (op1.ts == op2.ts) (except for the power
2435 Operators need no special handling for scalarized expressions as long as
2436 they call gfc_conv_simple_val to get their operands.
2437 Character strings get special handling. */
2440 gfc_conv_expr_op (gfc_se
* se
, gfc_expr
* expr
)
2442 enum tree_code code
;
2451 switch (expr
->value
.op
.op
)
2453 case INTRINSIC_PARENTHESES
:
2454 if ((expr
->ts
.type
== BT_REAL
2455 || expr
->ts
.type
== BT_COMPLEX
)
2456 && gfc_option
.flag_protect_parens
)
2458 gfc_conv_unary_op (PAREN_EXPR
, se
, expr
);
2459 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se
->expr
)));
2464 case INTRINSIC_UPLUS
:
2465 gfc_conv_expr (se
, expr
->value
.op
.op1
);
2468 case INTRINSIC_UMINUS
:
2469 gfc_conv_unary_op (NEGATE_EXPR
, se
, expr
);
2473 gfc_conv_unary_op (TRUTH_NOT_EXPR
, se
, expr
);
2476 case INTRINSIC_PLUS
:
2480 case INTRINSIC_MINUS
:
2484 case INTRINSIC_TIMES
:
2488 case INTRINSIC_DIVIDE
:
2489 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
2490 an integer, we must round towards zero, so we use a
2492 if (expr
->ts
.type
== BT_INTEGER
)
2493 code
= TRUNC_DIV_EXPR
;
2498 case INTRINSIC_POWER
:
2499 gfc_conv_power_op (se
, expr
);
2502 case INTRINSIC_CONCAT
:
2503 gfc_conv_concat_op (se
, expr
);
2507 code
= TRUTH_ANDIF_EXPR
;
2512 code
= TRUTH_ORIF_EXPR
;
2516 /* EQV and NEQV only work on logicals, but since we represent them
2517 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
2519 case INTRINSIC_EQ_OS
:
2527 case INTRINSIC_NE_OS
:
2528 case INTRINSIC_NEQV
:
2535 case INTRINSIC_GT_OS
:
2542 case INTRINSIC_GE_OS
:
2549 case INTRINSIC_LT_OS
:
2556 case INTRINSIC_LE_OS
:
2562 case INTRINSIC_USER
:
2563 case INTRINSIC_ASSIGN
:
2564 /* These should be converted into function calls by the frontend. */
2568 fatal_error ("Unknown intrinsic op");
2572 /* The only exception to this is **, which is handled separately anyway. */
2573 gcc_assert (expr
->value
.op
.op1
->ts
.type
== expr
->value
.op
.op2
->ts
.type
);
2575 if (checkstring
&& expr
->value
.op
.op1
->ts
.type
!= BT_CHARACTER
)
2579 gfc_init_se (&lse
, se
);
2580 gfc_conv_expr (&lse
, expr
->value
.op
.op1
);
2581 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
2584 gfc_init_se (&rse
, se
);
2585 gfc_conv_expr (&rse
, expr
->value
.op
.op2
);
2586 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
2590 gfc_conv_string_parameter (&lse
);
2591 gfc_conv_string_parameter (&rse
);
2593 lse
.expr
= gfc_build_compare_string (lse
.string_length
, lse
.expr
,
2594 rse
.string_length
, rse
.expr
,
2595 expr
->value
.op
.op1
->ts
.kind
,
2597 rse
.expr
= build_int_cst (TREE_TYPE (lse
.expr
), 0);
2598 gfc_add_block_to_block (&lse
.post
, &rse
.post
);
2601 type
= gfc_typenode_for_spec (&expr
->ts
);
2605 /* The result of logical ops is always boolean_type_node. */
2606 tmp
= fold_build2_loc (input_location
, code
, boolean_type_node
,
2607 lse
.expr
, rse
.expr
);
2608 se
->expr
= convert (type
, tmp
);
2611 se
->expr
= fold_build2_loc (input_location
, code
, type
, lse
.expr
, rse
.expr
);
2613 /* Add the post blocks. */
2614 gfc_add_block_to_block (&se
->post
, &rse
.post
);
2615 gfc_add_block_to_block (&se
->post
, &lse
.post
);
2618 /* If a string's length is one, we convert it to a single character. */
2621 gfc_string_to_single_character (tree len
, tree str
, int kind
)
2625 || !INTEGER_CST_P (len
) || TREE_INT_CST_HIGH (len
) != 0
2626 || !POINTER_TYPE_P (TREE_TYPE (str
)))
2629 if (TREE_INT_CST_LOW (len
) == 1)
2631 str
= fold_convert (gfc_get_pchar_type (kind
), str
);
2632 return build_fold_indirect_ref_loc (input_location
, str
);
2636 && TREE_CODE (str
) == ADDR_EXPR
2637 && TREE_CODE (TREE_OPERAND (str
, 0)) == ARRAY_REF
2638 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str
, 0), 0)) == STRING_CST
2639 && array_ref_low_bound (TREE_OPERAND (str
, 0))
2640 == TREE_OPERAND (TREE_OPERAND (str
, 0), 1)
2641 && TREE_INT_CST_LOW (len
) > 1
2642 && TREE_INT_CST_LOW (len
)
2643 == (unsigned HOST_WIDE_INT
)
2644 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str
, 0), 0)))
2646 tree ret
= fold_convert (gfc_get_pchar_type (kind
), str
);
2647 ret
= build_fold_indirect_ref_loc (input_location
, ret
);
2648 if (TREE_CODE (ret
) == INTEGER_CST
)
2650 tree string_cst
= TREE_OPERAND (TREE_OPERAND (str
, 0), 0);
2651 int i
, length
= TREE_STRING_LENGTH (string_cst
);
2652 const char *ptr
= TREE_STRING_POINTER (string_cst
);
2654 for (i
= 1; i
< length
; i
++)
2667 gfc_conv_scalar_char_value (gfc_symbol
*sym
, gfc_se
*se
, gfc_expr
**expr
)
2670 if (sym
->backend_decl
)
2672 /* This becomes the nominal_type in
2673 function.c:assign_parm_find_data_types. */
2674 TREE_TYPE (sym
->backend_decl
) = unsigned_char_type_node
;
2675 /* This becomes the passed_type in
2676 function.c:assign_parm_find_data_types. C promotes char to
2677 integer for argument passing. */
2678 DECL_ARG_TYPE (sym
->backend_decl
) = unsigned_type_node
;
2680 DECL_BY_REFERENCE (sym
->backend_decl
) = 0;
2685 /* If we have a constant character expression, make it into an
2687 if ((*expr
)->expr_type
== EXPR_CONSTANT
)
2692 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
2693 (int)(*expr
)->value
.character
.string
[0]);
2694 if ((*expr
)->ts
.kind
!= gfc_c_int_kind
)
2696 /* The expr needs to be compatible with a C int. If the
2697 conversion fails, then the 2 causes an ICE. */
2698 ts
.type
= BT_INTEGER
;
2699 ts
.kind
= gfc_c_int_kind
;
2700 gfc_convert_type (*expr
, &ts
, 2);
2703 else if (se
!= NULL
&& (*expr
)->expr_type
== EXPR_VARIABLE
)
2705 if ((*expr
)->ref
== NULL
)
2707 se
->expr
= gfc_string_to_single_character
2708 (build_int_cst (integer_type_node
, 1),
2709 gfc_build_addr_expr (gfc_get_pchar_type ((*expr
)->ts
.kind
),
2711 ((*expr
)->symtree
->n
.sym
)),
2716 gfc_conv_variable (se
, *expr
);
2717 se
->expr
= gfc_string_to_single_character
2718 (build_int_cst (integer_type_node
, 1),
2719 gfc_build_addr_expr (gfc_get_pchar_type ((*expr
)->ts
.kind
),
2727 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
2728 if STR is a string literal, otherwise return -1. */
2731 gfc_optimize_len_trim (tree len
, tree str
, int kind
)
2734 && TREE_CODE (str
) == ADDR_EXPR
2735 && TREE_CODE (TREE_OPERAND (str
, 0)) == ARRAY_REF
2736 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str
, 0), 0)) == STRING_CST
2737 && array_ref_low_bound (TREE_OPERAND (str
, 0))
2738 == TREE_OPERAND (TREE_OPERAND (str
, 0), 1)
2739 && TREE_INT_CST_LOW (len
) >= 1
2740 && TREE_INT_CST_LOW (len
)
2741 == (unsigned HOST_WIDE_INT
)
2742 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str
, 0), 0)))
2744 tree folded
= fold_convert (gfc_get_pchar_type (kind
), str
);
2745 folded
= build_fold_indirect_ref_loc (input_location
, folded
);
2746 if (TREE_CODE (folded
) == INTEGER_CST
)
2748 tree string_cst
= TREE_OPERAND (TREE_OPERAND (str
, 0), 0);
2749 int length
= TREE_STRING_LENGTH (string_cst
);
2750 const char *ptr
= TREE_STRING_POINTER (string_cst
);
2752 for (; length
> 0; length
--)
2753 if (ptr
[length
- 1] != ' ')
2762 /* Helper to build a call to memcmp. */
2765 build_memcmp_call (tree s1
, tree s2
, tree n
)
2769 if (!POINTER_TYPE_P (TREE_TYPE (s1
)))
2770 s1
= gfc_build_addr_expr (pvoid_type_node
, s1
);
2772 s1
= fold_convert (pvoid_type_node
, s1
);
2774 if (!POINTER_TYPE_P (TREE_TYPE (s2
)))
2775 s2
= gfc_build_addr_expr (pvoid_type_node
, s2
);
2777 s2
= fold_convert (pvoid_type_node
, s2
);
2779 n
= fold_convert (size_type_node
, n
);
2781 tmp
= build_call_expr_loc (input_location
,
2782 builtin_decl_explicit (BUILT_IN_MEMCMP
),
2785 return fold_convert (integer_type_node
, tmp
);
2788 /* Compare two strings. If they are all single characters, the result is the
2789 subtraction of them. Otherwise, we build a library call. */
2792 gfc_build_compare_string (tree len1
, tree str1
, tree len2
, tree str2
, int kind
,
2793 enum tree_code code
)
2799 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1
)));
2800 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2
)));
2802 sc1
= gfc_string_to_single_character (len1
, str1
, kind
);
2803 sc2
= gfc_string_to_single_character (len2
, str2
, kind
);
2805 if (sc1
!= NULL_TREE
&& sc2
!= NULL_TREE
)
2807 /* Deal with single character specially. */
2808 sc1
= fold_convert (integer_type_node
, sc1
);
2809 sc2
= fold_convert (integer_type_node
, sc2
);
2810 return fold_build2_loc (input_location
, MINUS_EXPR
, integer_type_node
,
2814 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
2816 && INTEGER_CST_P (len1
) && INTEGER_CST_P (len2
))
2818 /* If one string is a string literal with LEN_TRIM longer
2819 than the length of the second string, the strings
2821 int len
= gfc_optimize_len_trim (len1
, str1
, kind
);
2822 if (len
> 0 && compare_tree_int (len2
, len
) < 0)
2823 return integer_one_node
;
2824 len
= gfc_optimize_len_trim (len2
, str2
, kind
);
2825 if (len
> 0 && compare_tree_int (len1
, len
) < 0)
2826 return integer_one_node
;
2829 /* We can compare via memcpy if the strings are known to be equal
2830 in length and they are
2832 - kind=4 and the comparison is for (in)equality. */
2834 if (INTEGER_CST_P (len1
) && INTEGER_CST_P (len2
)
2835 && tree_int_cst_equal (len1
, len2
)
2836 && (kind
== 1 || code
== EQ_EXPR
|| code
== NE_EXPR
))
2841 chartype
= gfc_get_char_type (kind
);
2842 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, TREE_TYPE(len1
),
2843 fold_convert (TREE_TYPE(len1
),
2844 TYPE_SIZE_UNIT(chartype
)),
2846 return build_memcmp_call (str1
, str2
, tmp
);
2849 /* Build a call for the comparison. */
2851 fndecl
= gfor_fndecl_compare_string
;
2853 fndecl
= gfor_fndecl_compare_string_char4
;
2857 return build_call_expr_loc (input_location
, fndecl
, 4,
2858 len1
, str1
, len2
, str2
);
2862 /* Return the backend_decl for a procedure pointer component. */
2865 get_proc_ptr_comp (gfc_expr
*e
)
2871 gfc_init_se (&comp_se
, NULL
);
2872 e2
= gfc_copy_expr (e
);
2873 /* We have to restore the expr type later so that gfc_free_expr frees
2874 the exact same thing that was allocated.
2875 TODO: This is ugly. */
2876 old_type
= e2
->expr_type
;
2877 e2
->expr_type
= EXPR_VARIABLE
;
2878 gfc_conv_expr (&comp_se
, e2
);
2879 e2
->expr_type
= old_type
;
2881 return build_fold_addr_expr_loc (input_location
, comp_se
.expr
);
2885 /* Convert a typebound function reference from a class object. */
2887 conv_base_obj_fcn_val (gfc_se
* se
, tree base_object
, gfc_expr
* expr
)
2892 if (TREE_CODE (base_object
) != VAR_DECL
)
2894 var
= gfc_create_var (TREE_TYPE (base_object
), NULL
);
2895 gfc_add_modify (&se
->pre
, var
, base_object
);
2897 se
->expr
= gfc_class_vptr_get (base_object
);
2898 se
->expr
= build_fold_indirect_ref_loc (input_location
, se
->expr
);
2900 while (ref
&& ref
->next
)
2902 gcc_assert (ref
&& ref
->type
== REF_COMPONENT
);
2903 if (ref
->u
.c
.sym
->attr
.extension
)
2904 conv_parent_component_references (se
, ref
);
2905 gfc_conv_component_ref (se
, ref
);
2906 se
->expr
= build_fold_addr_expr_loc (input_location
, se
->expr
);
2911 conv_function_val (gfc_se
* se
, gfc_symbol
* sym
, gfc_expr
* expr
)
2915 if (gfc_is_proc_ptr_comp (expr
))
2916 tmp
= get_proc_ptr_comp (expr
);
2917 else if (sym
->attr
.dummy
)
2919 tmp
= gfc_get_symbol_decl (sym
);
2920 if (sym
->attr
.proc_pointer
)
2921 tmp
= build_fold_indirect_ref_loc (input_location
,
2923 gcc_assert (TREE_CODE (TREE_TYPE (tmp
)) == POINTER_TYPE
2924 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp
))) == FUNCTION_TYPE
);
2928 if (!sym
->backend_decl
)
2929 sym
->backend_decl
= gfc_get_extern_function_decl (sym
);
2931 TREE_USED (sym
->backend_decl
) = 1;
2933 tmp
= sym
->backend_decl
;
2935 if (sym
->attr
.cray_pointee
)
2937 /* TODO - make the cray pointee a pointer to a procedure,
2938 assign the pointer to it and use it for the call. This
2940 tmp
= convert (build_pointer_type (TREE_TYPE (tmp
)),
2941 gfc_get_symbol_decl (sym
->cp_pointer
));
2942 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
2945 if (!POINTER_TYPE_P (TREE_TYPE (tmp
)))
2947 gcc_assert (TREE_CODE (tmp
) == FUNCTION_DECL
);
2948 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
2955 /* Initialize MAPPING. */
2958 gfc_init_interface_mapping (gfc_interface_mapping
* mapping
)
2960 mapping
->syms
= NULL
;
2961 mapping
->charlens
= NULL
;
2965 /* Free all memory held by MAPPING (but not MAPPING itself). */
2968 gfc_free_interface_mapping (gfc_interface_mapping
* mapping
)
2970 gfc_interface_sym_mapping
*sym
;
2971 gfc_interface_sym_mapping
*nextsym
;
2973 gfc_charlen
*nextcl
;
2975 for (sym
= mapping
->syms
; sym
; sym
= nextsym
)
2977 nextsym
= sym
->next
;
2978 sym
->new_sym
->n
.sym
->formal
= NULL
;
2979 gfc_free_symbol (sym
->new_sym
->n
.sym
);
2980 gfc_free_expr (sym
->expr
);
2981 free (sym
->new_sym
);
2984 for (cl
= mapping
->charlens
; cl
; cl
= nextcl
)
2987 gfc_free_expr (cl
->length
);
2993 /* Return a copy of gfc_charlen CL. Add the returned structure to
2994 MAPPING so that it will be freed by gfc_free_interface_mapping. */
2996 static gfc_charlen
*
2997 gfc_get_interface_mapping_charlen (gfc_interface_mapping
* mapping
,
3000 gfc_charlen
*new_charlen
;
3002 new_charlen
= gfc_get_charlen ();
3003 new_charlen
->next
= mapping
->charlens
;
3004 new_charlen
->length
= gfc_copy_expr (cl
->length
);
3006 mapping
->charlens
= new_charlen
;
3011 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
3012 array variable that can be used as the actual argument for dummy
3013 argument SYM. Add any initialization code to BLOCK. PACKED is as
3014 for gfc_get_nodesc_array_type and DATA points to the first element
3015 in the passed array. */
3018 gfc_get_interface_mapping_array (stmtblock_t
* block
, gfc_symbol
* sym
,
3019 gfc_packed packed
, tree data
)
3024 type
= gfc_typenode_for_spec (&sym
->ts
);
3025 type
= gfc_get_nodesc_array_type (type
, sym
->as
, packed
,
3026 !sym
->attr
.target
&& !sym
->attr
.pointer
3027 && !sym
->attr
.proc_pointer
);
3029 var
= gfc_create_var (type
, "ifm");
3030 gfc_add_modify (block
, var
, fold_convert (type
, data
));
3036 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
3037 and offset of descriptorless array type TYPE given that it has the same
3038 size as DESC. Add any set-up code to BLOCK. */
3041 gfc_set_interface_mapping_bounds (stmtblock_t
* block
, tree type
, tree desc
)
3048 offset
= gfc_index_zero_node
;
3049 for (n
= 0; n
< GFC_TYPE_ARRAY_RANK (type
); n
++)
3051 dim
= gfc_rank_cst
[n
];
3052 GFC_TYPE_ARRAY_STRIDE (type
, n
) = gfc_conv_array_stride (desc
, n
);
3053 if (GFC_TYPE_ARRAY_LBOUND (type
, n
) == NULL_TREE
)
3055 GFC_TYPE_ARRAY_LBOUND (type
, n
)
3056 = gfc_conv_descriptor_lbound_get (desc
, dim
);
3057 GFC_TYPE_ARRAY_UBOUND (type
, n
)
3058 = gfc_conv_descriptor_ubound_get (desc
, dim
);
3060 else if (GFC_TYPE_ARRAY_UBOUND (type
, n
) == NULL_TREE
)
3062 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
3063 gfc_array_index_type
,
3064 gfc_conv_descriptor_ubound_get (desc
, dim
),
3065 gfc_conv_descriptor_lbound_get (desc
, dim
));
3066 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
3067 gfc_array_index_type
,
3068 GFC_TYPE_ARRAY_LBOUND (type
, n
), tmp
);
3069 tmp
= gfc_evaluate_now (tmp
, block
);
3070 GFC_TYPE_ARRAY_UBOUND (type
, n
) = tmp
;
3072 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
3073 GFC_TYPE_ARRAY_LBOUND (type
, n
),
3074 GFC_TYPE_ARRAY_STRIDE (type
, n
));
3075 offset
= fold_build2_loc (input_location
, MINUS_EXPR
,
3076 gfc_array_index_type
, offset
, tmp
);
3078 offset
= gfc_evaluate_now (offset
, block
);
3079 GFC_TYPE_ARRAY_OFFSET (type
) = offset
;
3083 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
3084 in SE. The caller may still use se->expr and se->string_length after
3085 calling this function. */
3088 gfc_add_interface_mapping (gfc_interface_mapping
* mapping
,
3089 gfc_symbol
* sym
, gfc_se
* se
,
3092 gfc_interface_sym_mapping
*sm
;
3096 gfc_symbol
*new_sym
;
3098 gfc_symtree
*new_symtree
;
3100 /* Create a new symbol to represent the actual argument. */
3101 new_sym
= gfc_new_symbol (sym
->name
, NULL
);
3102 new_sym
->ts
= sym
->ts
;
3103 new_sym
->as
= gfc_copy_array_spec (sym
->as
);
3104 new_sym
->attr
.referenced
= 1;
3105 new_sym
->attr
.dimension
= sym
->attr
.dimension
;
3106 new_sym
->attr
.contiguous
= sym
->attr
.contiguous
;
3107 new_sym
->attr
.codimension
= sym
->attr
.codimension
;
3108 new_sym
->attr
.pointer
= sym
->attr
.pointer
;
3109 new_sym
->attr
.allocatable
= sym
->attr
.allocatable
;
3110 new_sym
->attr
.flavor
= sym
->attr
.flavor
;
3111 new_sym
->attr
.function
= sym
->attr
.function
;
3113 /* Ensure that the interface is available and that
3114 descriptors are passed for array actual arguments. */
3115 if (sym
->attr
.flavor
== FL_PROCEDURE
)
3117 new_sym
->formal
= expr
->symtree
->n
.sym
->formal
;
3118 new_sym
->attr
.always_explicit
3119 = expr
->symtree
->n
.sym
->attr
.always_explicit
;
3122 /* Create a fake symtree for it. */
3124 new_symtree
= gfc_new_symtree (&root
, sym
->name
);
3125 new_symtree
->n
.sym
= new_sym
;
3126 gcc_assert (new_symtree
== root
);
3128 /* Create a dummy->actual mapping. */
3129 sm
= XCNEW (gfc_interface_sym_mapping
);
3130 sm
->next
= mapping
->syms
;
3132 sm
->new_sym
= new_symtree
;
3133 sm
->expr
= gfc_copy_expr (expr
);
3136 /* Stabilize the argument's value. */
3137 if (!sym
->attr
.function
&& se
)
3138 se
->expr
= gfc_evaluate_now (se
->expr
, &se
->pre
);
3140 if (sym
->ts
.type
== BT_CHARACTER
)
3142 /* Create a copy of the dummy argument's length. */
3143 new_sym
->ts
.u
.cl
= gfc_get_interface_mapping_charlen (mapping
, sym
->ts
.u
.cl
);
3144 sm
->expr
->ts
.u
.cl
= new_sym
->ts
.u
.cl
;
3146 /* If the length is specified as "*", record the length that
3147 the caller is passing. We should use the callee's length
3148 in all other cases. */
3149 if (!new_sym
->ts
.u
.cl
->length
&& se
)
3151 se
->string_length
= gfc_evaluate_now (se
->string_length
, &se
->pre
);
3152 new_sym
->ts
.u
.cl
->backend_decl
= se
->string_length
;
3159 /* Use the passed value as-is if the argument is a function. */
3160 if (sym
->attr
.flavor
== FL_PROCEDURE
)
3163 /* If the argument is either a string or a pointer to a string,
3164 convert it to a boundless character type. */
3165 else if (!sym
->attr
.dimension
&& sym
->ts
.type
== BT_CHARACTER
)
3167 tmp
= gfc_get_character_type_len (sym
->ts
.kind
, NULL
);
3168 tmp
= build_pointer_type (tmp
);
3169 if (sym
->attr
.pointer
)
3170 value
= build_fold_indirect_ref_loc (input_location
,
3174 value
= fold_convert (tmp
, value
);
3177 /* If the argument is a scalar, a pointer to an array or an allocatable,
3179 else if (!sym
->attr
.dimension
|| sym
->attr
.pointer
|| sym
->attr
.allocatable
)
3180 value
= build_fold_indirect_ref_loc (input_location
,
3183 /* For character(*), use the actual argument's descriptor. */
3184 else if (sym
->ts
.type
== BT_CHARACTER
&& !new_sym
->ts
.u
.cl
->length
)
3185 value
= build_fold_indirect_ref_loc (input_location
,
3188 /* If the argument is an array descriptor, use it to determine
3189 information about the actual argument's shape. */
3190 else if (POINTER_TYPE_P (TREE_TYPE (se
->expr
))
3191 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se
->expr
))))
3193 /* Get the actual argument's descriptor. */
3194 desc
= build_fold_indirect_ref_loc (input_location
,
3197 /* Create the replacement variable. */
3198 tmp
= gfc_conv_descriptor_data_get (desc
);
3199 value
= gfc_get_interface_mapping_array (&se
->pre
, sym
,
3202 /* Use DESC to work out the upper bounds, strides and offset. */
3203 gfc_set_interface_mapping_bounds (&se
->pre
, TREE_TYPE (value
), desc
);
3206 /* Otherwise we have a packed array. */
3207 value
= gfc_get_interface_mapping_array (&se
->pre
, sym
,
3208 PACKED_FULL
, se
->expr
);
3210 new_sym
->backend_decl
= value
;
3214 /* Called once all dummy argument mappings have been added to MAPPING,
3215 but before the mapping is used to evaluate expressions. Pre-evaluate
3216 the length of each argument, adding any initialization code to PRE and
3217 any finalization code to POST. */
3220 gfc_finish_interface_mapping (gfc_interface_mapping
* mapping
,
3221 stmtblock_t
* pre
, stmtblock_t
* post
)
3223 gfc_interface_sym_mapping
*sym
;
3227 for (sym
= mapping
->syms
; sym
; sym
= sym
->next
)
3228 if (sym
->new_sym
->n
.sym
->ts
.type
== BT_CHARACTER
3229 && !sym
->new_sym
->n
.sym
->ts
.u
.cl
->backend_decl
)
3231 expr
= sym
->new_sym
->n
.sym
->ts
.u
.cl
->length
;
3232 gfc_apply_interface_mapping_to_expr (mapping
, expr
);
3233 gfc_init_se (&se
, NULL
);
3234 gfc_conv_expr (&se
, expr
);
3235 se
.expr
= fold_convert (gfc_charlen_type_node
, se
.expr
);
3236 se
.expr
= gfc_evaluate_now (se
.expr
, &se
.pre
);
3237 gfc_add_block_to_block (pre
, &se
.pre
);
3238 gfc_add_block_to_block (post
, &se
.post
);
3240 sym
->new_sym
->n
.sym
->ts
.u
.cl
->backend_decl
= se
.expr
;
3245 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3249 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping
* mapping
,
3250 gfc_constructor_base base
)
3253 for (c
= gfc_constructor_first (base
); c
; c
= gfc_constructor_next (c
))
3255 gfc_apply_interface_mapping_to_expr (mapping
, c
->expr
);
3258 gfc_apply_interface_mapping_to_expr (mapping
, c
->iterator
->start
);
3259 gfc_apply_interface_mapping_to_expr (mapping
, c
->iterator
->end
);
3260 gfc_apply_interface_mapping_to_expr (mapping
, c
->iterator
->step
);
3266 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3270 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping
* mapping
,
3275 for (; ref
; ref
= ref
->next
)
3279 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
3281 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ar
.start
[n
]);
3282 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ar
.end
[n
]);
3283 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ar
.stride
[n
]);
3291 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ss
.start
);
3292 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ss
.end
);
3298 /* Convert intrinsic function calls into result expressions. */
3301 gfc_map_intrinsic_function (gfc_expr
*expr
, gfc_interface_mapping
*mapping
)
3309 arg1
= expr
->value
.function
.actual
->expr
;
3310 if (expr
->value
.function
.actual
->next
)
3311 arg2
= expr
->value
.function
.actual
->next
->expr
;
3315 sym
= arg1
->symtree
->n
.sym
;
3317 if (sym
->attr
.dummy
)
3322 switch (expr
->value
.function
.isym
->id
)
3325 /* TODO figure out why this condition is necessary. */
3326 if (sym
->attr
.function
3327 && (arg1
->ts
.u
.cl
->length
== NULL
3328 || (arg1
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
3329 && arg1
->ts
.u
.cl
->length
->expr_type
!= EXPR_VARIABLE
)))
3332 new_expr
= gfc_copy_expr (arg1
->ts
.u
.cl
->length
);
3336 if (!sym
->as
|| sym
->as
->rank
== 0)
3339 if (arg2
&& arg2
->expr_type
== EXPR_CONSTANT
)
3341 dup
= mpz_get_si (arg2
->value
.integer
);
3346 dup
= sym
->as
->rank
;
3350 for (; d
< dup
; d
++)
3354 if (!sym
->as
->upper
[d
] || !sym
->as
->lower
[d
])
3356 gfc_free_expr (new_expr
);
3360 tmp
= gfc_add (gfc_copy_expr (sym
->as
->upper
[d
]),
3361 gfc_get_int_expr (gfc_default_integer_kind
,
3363 tmp
= gfc_subtract (tmp
, gfc_copy_expr (sym
->as
->lower
[d
]));
3365 new_expr
= gfc_multiply (new_expr
, tmp
);
3371 case GFC_ISYM_LBOUND
:
3372 case GFC_ISYM_UBOUND
:
3373 /* TODO These implementations of lbound and ubound do not limit if
3374 the size < 0, according to F95's 13.14.53 and 13.14.113. */
3376 if (!sym
->as
|| sym
->as
->rank
== 0)
3379 if (arg2
&& arg2
->expr_type
== EXPR_CONSTANT
)
3380 d
= mpz_get_si (arg2
->value
.integer
) - 1;
3382 /* TODO: If the need arises, this could produce an array of
3386 if (expr
->value
.function
.isym
->id
== GFC_ISYM_LBOUND
)
3388 if (sym
->as
->lower
[d
])
3389 new_expr
= gfc_copy_expr (sym
->as
->lower
[d
]);
3393 if (sym
->as
->upper
[d
])
3394 new_expr
= gfc_copy_expr (sym
->as
->upper
[d
]);
3402 gfc_apply_interface_mapping_to_expr (mapping
, new_expr
);
3406 gfc_replace_expr (expr
, new_expr
);
3412 gfc_map_fcn_formal_to_actual (gfc_expr
*expr
, gfc_expr
*map_expr
,
3413 gfc_interface_mapping
* mapping
)
3415 gfc_formal_arglist
*f
;
3416 gfc_actual_arglist
*actual
;
3418 actual
= expr
->value
.function
.actual
;
3419 f
= gfc_sym_get_dummy_args (map_expr
->symtree
->n
.sym
);
3421 for (; f
&& actual
; f
= f
->next
, actual
= actual
->next
)
3426 gfc_add_interface_mapping (mapping
, f
->sym
, NULL
, actual
->expr
);
3429 if (map_expr
->symtree
->n
.sym
->attr
.dimension
)
3434 as
= gfc_copy_array_spec (map_expr
->symtree
->n
.sym
->as
);
3436 for (d
= 0; d
< as
->rank
; d
++)
3438 gfc_apply_interface_mapping_to_expr (mapping
, as
->lower
[d
]);
3439 gfc_apply_interface_mapping_to_expr (mapping
, as
->upper
[d
]);
3442 expr
->value
.function
.esym
->as
= as
;
3445 if (map_expr
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
)
3447 expr
->value
.function
.esym
->ts
.u
.cl
->length
3448 = gfc_copy_expr (map_expr
->symtree
->n
.sym
->ts
.u
.cl
->length
);
3450 gfc_apply_interface_mapping_to_expr (mapping
,
3451 expr
->value
.function
.esym
->ts
.u
.cl
->length
);
3456 /* EXPR is a copy of an expression that appeared in the interface
3457 associated with MAPPING. Walk it recursively looking for references to
3458 dummy arguments that MAPPING maps to actual arguments. Replace each such
3459 reference with a reference to the associated actual argument. */
3462 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping
* mapping
,
3465 gfc_interface_sym_mapping
*sym
;
3466 gfc_actual_arglist
*actual
;
3471 /* Copying an expression does not copy its length, so do that here. */
3472 if (expr
->ts
.type
== BT_CHARACTER
&& expr
->ts
.u
.cl
)
3474 expr
->ts
.u
.cl
= gfc_get_interface_mapping_charlen (mapping
, expr
->ts
.u
.cl
);
3475 gfc_apply_interface_mapping_to_expr (mapping
, expr
->ts
.u
.cl
->length
);
3478 /* Apply the mapping to any references. */
3479 gfc_apply_interface_mapping_to_ref (mapping
, expr
->ref
);
3481 /* ...and to the expression's symbol, if it has one. */
3482 /* TODO Find out why the condition on expr->symtree had to be moved into
3483 the loop rather than being outside it, as originally. */
3484 for (sym
= mapping
->syms
; sym
; sym
= sym
->next
)
3485 if (expr
->symtree
&& sym
->old
== expr
->symtree
->n
.sym
)
3487 if (sym
->new_sym
->n
.sym
->backend_decl
)
3488 expr
->symtree
= sym
->new_sym
;
3490 gfc_replace_expr (expr
, gfc_copy_expr (sym
->expr
));
3491 /* Replace base type for polymorphic arguments. */
3492 if (expr
->ref
&& expr
->ref
->type
== REF_COMPONENT
3493 && sym
->expr
&& sym
->expr
->ts
.type
== BT_CLASS
)
3494 expr
->ref
->u
.c
.sym
= sym
->expr
->ts
.u
.derived
;
3497 /* ...and to subexpressions in expr->value. */
3498 switch (expr
->expr_type
)
3503 case EXPR_SUBSTRING
:
3507 gfc_apply_interface_mapping_to_expr (mapping
, expr
->value
.op
.op1
);
3508 gfc_apply_interface_mapping_to_expr (mapping
, expr
->value
.op
.op2
);
3512 for (actual
= expr
->value
.function
.actual
; actual
; actual
= actual
->next
)
3513 gfc_apply_interface_mapping_to_expr (mapping
, actual
->expr
);
3515 if (expr
->value
.function
.esym
== NULL
3516 && expr
->value
.function
.isym
!= NULL
3517 && expr
->value
.function
.actual
->expr
->symtree
3518 && gfc_map_intrinsic_function (expr
, mapping
))
3521 for (sym
= mapping
->syms
; sym
; sym
= sym
->next
)
3522 if (sym
->old
== expr
->value
.function
.esym
)
3524 expr
->value
.function
.esym
= sym
->new_sym
->n
.sym
;
3525 gfc_map_fcn_formal_to_actual (expr
, sym
->expr
, mapping
);
3526 expr
->value
.function
.esym
->result
= sym
->new_sym
->n
.sym
;
3531 case EXPR_STRUCTURE
:
3532 gfc_apply_interface_mapping_to_cons (mapping
, expr
->value
.constructor
);
3545 /* Evaluate interface expression EXPR using MAPPING. Store the result
3549 gfc_apply_interface_mapping (gfc_interface_mapping
* mapping
,
3550 gfc_se
* se
, gfc_expr
* expr
)
3552 expr
= gfc_copy_expr (expr
);
3553 gfc_apply_interface_mapping_to_expr (mapping
, expr
);
3554 gfc_conv_expr (se
, expr
);
3555 se
->expr
= gfc_evaluate_now (se
->expr
, &se
->pre
);
3556 gfc_free_expr (expr
);
3560 /* Returns a reference to a temporary array into which a component of
3561 an actual argument derived type array is copied and then returned
3562 after the function call. */
3564 gfc_conv_subref_array_arg (gfc_se
* parmse
, gfc_expr
* expr
, int g77
,
3565 sym_intent intent
, bool formal_ptr
)
3573 gfc_array_info
*info
;
3583 gcc_assert (expr
->expr_type
== EXPR_VARIABLE
);
3585 gfc_init_se (&lse
, NULL
);
3586 gfc_init_se (&rse
, NULL
);
3588 /* Walk the argument expression. */
3589 rss
= gfc_walk_expr (expr
);
3591 gcc_assert (rss
!= gfc_ss_terminator
);
3593 /* Initialize the scalarizer. */
3594 gfc_init_loopinfo (&loop
);
3595 gfc_add_ss_to_loop (&loop
, rss
);
3597 /* Calculate the bounds of the scalarization. */
3598 gfc_conv_ss_startstride (&loop
);
3600 /* Build an ss for the temporary. */
3601 if (expr
->ts
.type
== BT_CHARACTER
&& !expr
->ts
.u
.cl
->backend_decl
)
3602 gfc_conv_string_length (expr
->ts
.u
.cl
, expr
, &parmse
->pre
);
3604 base_type
= gfc_typenode_for_spec (&expr
->ts
);
3605 if (GFC_ARRAY_TYPE_P (base_type
)
3606 || GFC_DESCRIPTOR_TYPE_P (base_type
))
3607 base_type
= gfc_get_element_type (base_type
);
3609 if (expr
->ts
.type
== BT_CLASS
)
3610 base_type
= gfc_typenode_for_spec (&CLASS_DATA (expr
)->ts
);
3612 loop
.temp_ss
= gfc_get_temp_ss (base_type
, ((expr
->ts
.type
== BT_CHARACTER
)
3613 ? expr
->ts
.u
.cl
->backend_decl
3617 parmse
->string_length
= loop
.temp_ss
->info
->string_length
;
3619 /* Associate the SS with the loop. */
3620 gfc_add_ss_to_loop (&loop
, loop
.temp_ss
);
3622 /* Setup the scalarizing loops. */
3623 gfc_conv_loop_setup (&loop
, &expr
->where
);
3625 /* Pass the temporary descriptor back to the caller. */
3626 info
= &loop
.temp_ss
->info
->data
.array
;
3627 parmse
->expr
= info
->descriptor
;
3629 /* Setup the gfc_se structures. */
3630 gfc_copy_loopinfo_to_se (&lse
, &loop
);
3631 gfc_copy_loopinfo_to_se (&rse
, &loop
);
3634 lse
.ss
= loop
.temp_ss
;
3635 gfc_mark_ss_chain_used (rss
, 1);
3636 gfc_mark_ss_chain_used (loop
.temp_ss
, 1);
3638 /* Start the scalarized loop body. */
3639 gfc_start_scalarized_body (&loop
, &body
);
3641 /* Translate the expression. */
3642 gfc_conv_expr (&rse
, expr
);
3644 gfc_conv_tmp_array_ref (&lse
);
3646 if (intent
!= INTENT_OUT
)
3648 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr
->ts
, true, false, true);
3649 gfc_add_expr_to_block (&body
, tmp
);
3650 gcc_assert (rse
.ss
== gfc_ss_terminator
);
3651 gfc_trans_scalarizing_loops (&loop
, &body
);
3655 /* Make sure that the temporary declaration survives by merging
3656 all the loop declarations into the current context. */
3657 for (n
= 0; n
< loop
.dimen
; n
++)
3659 gfc_merge_block_scope (&body
);
3660 body
= loop
.code
[loop
.order
[n
]];
3662 gfc_merge_block_scope (&body
);
3665 /* Add the post block after the second loop, so that any
3666 freeing of allocated memory is done at the right time. */
3667 gfc_add_block_to_block (&parmse
->pre
, &loop
.pre
);
3669 /**********Copy the temporary back again.*********/
3671 gfc_init_se (&lse
, NULL
);
3672 gfc_init_se (&rse
, NULL
);
3674 /* Walk the argument expression. */
3675 lss
= gfc_walk_expr (expr
);
3676 rse
.ss
= loop
.temp_ss
;
3679 /* Initialize the scalarizer. */
3680 gfc_init_loopinfo (&loop2
);
3681 gfc_add_ss_to_loop (&loop2
, lss
);
3683 /* Calculate the bounds of the scalarization. */
3684 gfc_conv_ss_startstride (&loop2
);
3686 /* Setup the scalarizing loops. */
3687 gfc_conv_loop_setup (&loop2
, &expr
->where
);
3689 gfc_copy_loopinfo_to_se (&lse
, &loop2
);
3690 gfc_copy_loopinfo_to_se (&rse
, &loop2
);
3692 gfc_mark_ss_chain_used (lss
, 1);
3693 gfc_mark_ss_chain_used (loop
.temp_ss
, 1);
3695 /* Declare the variable to hold the temporary offset and start the
3696 scalarized loop body. */
3697 offset
= gfc_create_var (gfc_array_index_type
, NULL
);
3698 gfc_start_scalarized_body (&loop2
, &body
);
3700 /* Build the offsets for the temporary from the loop variables. The
3701 temporary array has lbounds of zero and strides of one in all
3702 dimensions, so this is very simple. The offset is only computed
3703 outside the innermost loop, so the overall transfer could be
3704 optimized further. */
3705 info
= &rse
.ss
->info
->data
.array
;
3706 dimen
= rse
.ss
->dimen
;
3708 tmp_index
= gfc_index_zero_node
;
3709 for (n
= dimen
- 1; n
> 0; n
--)
3712 tmp
= rse
.loop
->loopvar
[n
];
3713 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
3714 tmp
, rse
.loop
->from
[n
]);
3715 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
, gfc_array_index_type
,
3718 tmp_str
= fold_build2_loc (input_location
, MINUS_EXPR
,
3719 gfc_array_index_type
,
3720 rse
.loop
->to
[n
-1], rse
.loop
->from
[n
-1]);
3721 tmp_str
= fold_build2_loc (input_location
, PLUS_EXPR
,
3722 gfc_array_index_type
,
3723 tmp_str
, gfc_index_one_node
);
3725 tmp_index
= fold_build2_loc (input_location
, MULT_EXPR
,
3726 gfc_array_index_type
, tmp
, tmp_str
);
3729 tmp_index
= fold_build2_loc (input_location
, MINUS_EXPR
,
3730 gfc_array_index_type
,
3731 tmp_index
, rse
.loop
->from
[0]);
3732 gfc_add_modify (&rse
.loop
->code
[0], offset
, tmp_index
);
3734 tmp_index
= fold_build2_loc (input_location
, PLUS_EXPR
,
3735 gfc_array_index_type
,
3736 rse
.loop
->loopvar
[0], offset
);
3738 /* Now use the offset for the reference. */
3739 tmp
= build_fold_indirect_ref_loc (input_location
,
3741 rse
.expr
= gfc_build_array_ref (tmp
, tmp_index
, NULL
);
3743 if (expr
->ts
.type
== BT_CHARACTER
)
3744 rse
.string_length
= expr
->ts
.u
.cl
->backend_decl
;
3746 gfc_conv_expr (&lse
, expr
);
3748 gcc_assert (lse
.ss
== gfc_ss_terminator
);
3750 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr
->ts
, false, false, true);
3751 gfc_add_expr_to_block (&body
, tmp
);
3753 /* Generate the copying loops. */
3754 gfc_trans_scalarizing_loops (&loop2
, &body
);
3756 /* Wrap the whole thing up by adding the second loop to the post-block
3757 and following it by the post-block of the first loop. In this way,
3758 if the temporary needs freeing, it is done after use! */
3759 if (intent
!= INTENT_IN
)
3761 gfc_add_block_to_block (&parmse
->post
, &loop2
.pre
);
3762 gfc_add_block_to_block (&parmse
->post
, &loop2
.post
);
3765 gfc_add_block_to_block (&parmse
->post
, &loop
.post
);
3767 gfc_cleanup_loop (&loop
);
3768 gfc_cleanup_loop (&loop2
);
3770 /* Pass the string length to the argument expression. */
3771 if (expr
->ts
.type
== BT_CHARACTER
)
3772 parmse
->string_length
= expr
->ts
.u
.cl
->backend_decl
;
3774 /* Determine the offset for pointer formal arguments and set the
3778 size
= gfc_index_one_node
;
3779 offset
= gfc_index_zero_node
;
3780 for (n
= 0; n
< dimen
; n
++)
3782 tmp
= gfc_conv_descriptor_ubound_get (parmse
->expr
,
3784 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
3785 gfc_array_index_type
, tmp
,
3786 gfc_index_one_node
);
3787 gfc_conv_descriptor_ubound_set (&parmse
->pre
,
3791 gfc_conv_descriptor_lbound_set (&parmse
->pre
,
3794 gfc_index_one_node
);
3795 size
= gfc_evaluate_now (size
, &parmse
->pre
);
3796 offset
= fold_build2_loc (input_location
, MINUS_EXPR
,
3797 gfc_array_index_type
,
3799 offset
= gfc_evaluate_now (offset
, &parmse
->pre
);
3800 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
3801 gfc_array_index_type
,
3802 rse
.loop
->to
[n
], rse
.loop
->from
[n
]);
3803 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
3804 gfc_array_index_type
,
3805 tmp
, gfc_index_one_node
);
3806 size
= fold_build2_loc (input_location
, MULT_EXPR
,
3807 gfc_array_index_type
, size
, tmp
);
3810 gfc_conv_descriptor_offset_set (&parmse
->pre
, parmse
->expr
,
3814 /* We want either the address for the data or the address of the descriptor,
3815 depending on the mode of passing array arguments. */
3817 parmse
->expr
= gfc_conv_descriptor_data_get (parmse
->expr
);
3819 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, parmse
->expr
);
3825 /* Generate the code for argument list functions. */
3828 conv_arglist_function (gfc_se
*se
, gfc_expr
*expr
, const char *name
)
3830 /* Pass by value for g77 %VAL(arg), pass the address
3831 indirectly for %LOC, else by reference. Thus %REF
3832 is a "do-nothing" and %LOC is the same as an F95
3834 if (strncmp (name
, "%VAL", 4) == 0)
3835 gfc_conv_expr (se
, expr
);
3836 else if (strncmp (name
, "%LOC", 4) == 0)
3838 gfc_conv_expr_reference (se
, expr
);
3839 se
->expr
= gfc_build_addr_expr (NULL
, se
->expr
);
3841 else if (strncmp (name
, "%REF", 4) == 0)
3842 gfc_conv_expr_reference (se
, expr
);
3844 gfc_error ("Unknown argument list function at %L", &expr
->where
);
3848 /* Generate code for a procedure call. Note can return se->post != NULL.
3849 If se->direct_byref is set then se->expr contains the return parameter.
3850 Return nonzero, if the call has alternate specifiers.
3851 'expr' is only needed for procedure pointer components. */
3854 gfc_conv_procedure_call (gfc_se
* se
, gfc_symbol
* sym
,
3855 gfc_actual_arglist
* args
, gfc_expr
* expr
,
3856 vec
<tree
, va_gc
> *append_args
)
3858 gfc_interface_mapping mapping
;
3859 vec
<tree
, va_gc
> *arglist
;
3860 vec
<tree
, va_gc
> *retargs
;
3864 gfc_array_info
*info
;
3871 vec
<tree
, va_gc
> *stringargs
;
3872 vec
<tree
, va_gc
> *optionalargs
;
3874 gfc_formal_arglist
*formal
;
3875 gfc_actual_arglist
*arg
;
3876 int has_alternate_specifier
= 0;
3877 bool need_interface_mapping
;
3884 enum {MISSING
= 0, ELEMENTAL
, SCALAR
, SCALAR_POINTER
, ARRAY
};
3885 gfc_component
*comp
= NULL
;
3891 optionalargs
= NULL
;
3896 comp
= gfc_get_proc_ptr_comp (expr
);
3900 if (!sym
->attr
.elemental
&& !(comp
&& comp
->attr
.elemental
))
3902 gcc_assert (se
->ss
->info
->type
== GFC_SS_FUNCTION
);
3903 if (se
->ss
->info
->useflags
)
3905 gcc_assert ((!comp
&& gfc_return_by_reference (sym
)
3906 && sym
->result
->attr
.dimension
)
3907 || (comp
&& comp
->attr
.dimension
));
3908 gcc_assert (se
->loop
!= NULL
);
3910 /* Access the previously obtained result. */
3911 gfc_conv_tmp_array_ref (se
);
3915 info
= &se
->ss
->info
->data
.array
;
3920 gfc_init_block (&post
);
3921 gfc_init_interface_mapping (&mapping
);
3924 formal
= gfc_sym_get_dummy_args (sym
);
3925 need_interface_mapping
= sym
->attr
.dimension
||
3926 (sym
->ts
.type
== BT_CHARACTER
3927 && sym
->ts
.u
.cl
->length
3928 && sym
->ts
.u
.cl
->length
->expr_type
3933 formal
= comp
->ts
.interface
? comp
->ts
.interface
->formal
: NULL
;
3934 need_interface_mapping
= comp
->attr
.dimension
||
3935 (comp
->ts
.type
== BT_CHARACTER
3936 && comp
->ts
.u
.cl
->length
3937 && comp
->ts
.u
.cl
->length
->expr_type
3941 base_object
= NULL_TREE
;
3943 /* Evaluate the arguments. */
3944 for (arg
= args
; arg
!= NULL
;
3945 arg
= arg
->next
, formal
= formal
? formal
->next
: NULL
)
3948 fsym
= formal
? formal
->sym
: NULL
;
3949 parm_kind
= MISSING
;
3951 /* Class array expressions are sometimes coming completely unadorned
3952 with either arrayspec or _data component. Correct that here.
3953 OOP-TODO: Move this to the frontend. */
3954 if (e
&& e
->expr_type
== EXPR_VARIABLE
3956 && e
->ts
.type
== BT_CLASS
3957 && (CLASS_DATA (e
)->attr
.codimension
3958 || CLASS_DATA (e
)->attr
.dimension
))
3960 gfc_typespec temp_ts
= e
->ts
;
3961 gfc_add_class_array_ref (e
);
3967 if (se
->ignore_optional
)
3969 /* Some intrinsics have already been resolved to the correct
3973 else if (arg
->label
)
3975 has_alternate_specifier
= 1;
3980 gfc_init_se (&parmse
, NULL
);
3982 /* For scalar arguments with VALUE attribute which are passed by
3983 value, pass "0" and a hidden argument gives the optional
3985 if (fsym
&& fsym
->attr
.optional
&& fsym
->attr
.value
3986 && !fsym
->attr
.dimension
&& fsym
->ts
.type
!= BT_CHARACTER
3987 && fsym
->ts
.type
!= BT_CLASS
&& fsym
->ts
.type
!= BT_DERIVED
)
3989 parmse
.expr
= fold_convert (gfc_sym_type (fsym
),
3991 vec_safe_push (optionalargs
, boolean_false_node
);
3995 /* Pass a NULL pointer for an absent arg. */
3996 parmse
.expr
= null_pointer_node
;
3997 if (arg
->missing_arg_type
== BT_CHARACTER
)
3998 parmse
.string_length
= build_int_cst (gfc_charlen_type_node
,
4003 else if (arg
->expr
->expr_type
== EXPR_NULL
4004 && fsym
&& !fsym
->attr
.pointer
4005 && (fsym
->ts
.type
!= BT_CLASS
4006 || !CLASS_DATA (fsym
)->attr
.class_pointer
))
4008 /* Pass a NULL pointer to denote an absent arg. */
4009 gcc_assert (fsym
->attr
.optional
&& !fsym
->attr
.allocatable
4010 && (fsym
->ts
.type
!= BT_CLASS
4011 || !CLASS_DATA (fsym
)->attr
.allocatable
));
4012 gfc_init_se (&parmse
, NULL
);
4013 parmse
.expr
= null_pointer_node
;
4014 if (arg
->missing_arg_type
== BT_CHARACTER
)
4015 parmse
.string_length
= build_int_cst (gfc_charlen_type_node
, 0);
4017 else if (fsym
&& fsym
->ts
.type
== BT_CLASS
4018 && e
->ts
.type
== BT_DERIVED
)
4020 /* The derived type needs to be converted to a temporary
4022 gfc_init_se (&parmse
, se
);
4023 gfc_conv_derived_to_class (&parmse
, e
, fsym
->ts
, NULL
,
4025 && e
->expr_type
== EXPR_VARIABLE
4026 && e
->symtree
->n
.sym
->attr
.optional
,
4027 CLASS_DATA (fsym
)->attr
.class_pointer
4028 || CLASS_DATA (fsym
)->attr
.allocatable
);
4030 else if (UNLIMITED_POLY (fsym
) && e
->ts
.type
!= BT_CLASS
)
4032 /* The intrinsic type needs to be converted to a temporary
4033 CLASS object for the unlimited polymorphic formal. */
4034 gfc_init_se (&parmse
, se
);
4035 gfc_conv_intrinsic_to_class (&parmse
, e
, fsym
->ts
);
4037 else if (se
->ss
&& se
->ss
->info
->useflags
)
4043 /* An elemental function inside a scalarized loop. */
4044 gfc_init_se (&parmse
, se
);
4045 parm_kind
= ELEMENTAL
;
4047 gfc_conv_expr_reference (&parmse
, e
);
4048 if (e
->ts
.type
== BT_CHARACTER
&& !e
->rank
4049 && e
->expr_type
== EXPR_FUNCTION
)
4050 parmse
.expr
= build_fold_indirect_ref_loc (input_location
,
4053 if (fsym
&& fsym
->ts
.type
== BT_DERIVED
4054 && gfc_is_class_container_ref (e
))
4056 parmse
.expr
= gfc_class_data_get (parmse
.expr
);
4058 if (fsym
->attr
.optional
&& e
->expr_type
== EXPR_VARIABLE
4059 && e
->symtree
->n
.sym
->attr
.optional
)
4061 tree cond
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
4062 parmse
.expr
= build3_loc (input_location
, COND_EXPR
,
4063 TREE_TYPE (parmse
.expr
),
4065 fold_convert (TREE_TYPE (parmse
.expr
),
4066 null_pointer_node
));
4070 /* If we are passing an absent array as optional dummy to an
4071 elemental procedure, make sure that we pass NULL when the data
4072 pointer is NULL. We need this extra conditional because of
4073 scalarization which passes arrays elements to the procedure,
4074 ignoring the fact that the array can be absent/unallocated/... */
4075 if (ss
->info
->can_be_null_ref
&& ss
->info
->type
!= GFC_SS_REFERENCE
)
4077 tree descriptor_data
;
4079 descriptor_data
= ss
->info
->data
.array
.data
;
4080 tmp
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
4082 fold_convert (TREE_TYPE (descriptor_data
),
4083 null_pointer_node
));
4085 = fold_build3_loc (input_location
, COND_EXPR
,
4086 TREE_TYPE (parmse
.expr
),
4088 fold_convert (TREE_TYPE (parmse
.expr
),
4093 /* The scalarizer does not repackage the reference to a class
4094 array - instead it returns a pointer to the data element. */
4095 if (fsym
&& fsym
->ts
.type
== BT_CLASS
&& e
->ts
.type
== BT_CLASS
)
4096 gfc_conv_class_to_class (&parmse
, e
, fsym
->ts
, true,
4097 fsym
->attr
.intent
!= INTENT_IN
4098 && (CLASS_DATA (fsym
)->attr
.class_pointer
4099 || CLASS_DATA (fsym
)->attr
.allocatable
),
4101 && e
->expr_type
== EXPR_VARIABLE
4102 && e
->symtree
->n
.sym
->attr
.optional
,
4103 CLASS_DATA (fsym
)->attr
.class_pointer
4104 || CLASS_DATA (fsym
)->attr
.allocatable
);
4111 gfc_init_se (&parmse
, NULL
);
4113 /* Check whether the expression is a scalar or not; we cannot use
4114 e->rank as it can be nonzero for functions arguments. */
4115 argss
= gfc_walk_expr (e
);
4116 scalar
= argss
== gfc_ss_terminator
;
4118 gfc_free_ss_chain (argss
);
4120 /* Special handling for passing scalar polymorphic coarrays;
4121 otherwise one passes "class->_data.data" instead of "&class". */
4122 if (e
->rank
== 0 && e
->ts
.type
== BT_CLASS
4123 && fsym
&& fsym
->ts
.type
== BT_CLASS
4124 && CLASS_DATA (fsym
)->attr
.codimension
4125 && !CLASS_DATA (fsym
)->attr
.dimension
)
4127 gfc_add_class_array_ref (e
);
4128 parmse
.want_coarray
= 1;
4132 /* A scalar or transformational function. */
4135 if (e
->expr_type
== EXPR_VARIABLE
4136 && e
->symtree
->n
.sym
->attr
.cray_pointee
4137 && fsym
&& fsym
->attr
.flavor
== FL_PROCEDURE
)
4139 /* The Cray pointer needs to be converted to a pointer to
4140 a type given by the expression. */
4141 gfc_conv_expr (&parmse
, e
);
4142 type
= build_pointer_type (TREE_TYPE (parmse
.expr
));
4143 tmp
= gfc_get_symbol_decl (e
->symtree
->n
.sym
->cp_pointer
);
4144 parmse
.expr
= convert (type
, tmp
);
4146 else if (fsym
&& fsym
->attr
.value
)
4148 if (fsym
->ts
.type
== BT_CHARACTER
4149 && fsym
->ts
.is_c_interop
4150 && fsym
->ns
->proc_name
!= NULL
4151 && fsym
->ns
->proc_name
->attr
.is_bind_c
)
4154 gfc_conv_scalar_char_value (fsym
, &parmse
, &e
);
4155 if (parmse
.expr
== NULL
)
4156 gfc_conv_expr (&parmse
, e
);
4160 gfc_conv_expr (&parmse
, e
);
4161 if (fsym
->attr
.optional
4162 && fsym
->ts
.type
!= BT_CLASS
4163 && fsym
->ts
.type
!= BT_DERIVED
)
4165 if (e
->expr_type
!= EXPR_VARIABLE
4166 || !e
->symtree
->n
.sym
->attr
.optional
4168 vec_safe_push (optionalargs
, boolean_true_node
);
4171 tmp
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
4172 if (!e
->symtree
->n
.sym
->attr
.value
)
4174 = fold_build3_loc (input_location
, COND_EXPR
,
4175 TREE_TYPE (parmse
.expr
),
4177 fold_convert (TREE_TYPE (parmse
.expr
),
4178 integer_zero_node
));
4180 vec_safe_push (optionalargs
, tmp
);
4185 else if (arg
->name
&& arg
->name
[0] == '%')
4186 /* Argument list functions %VAL, %LOC and %REF are signalled
4187 through arg->name. */
4188 conv_arglist_function (&parmse
, arg
->expr
, arg
->name
);
4189 else if ((e
->expr_type
== EXPR_FUNCTION
)
4190 && ((e
->value
.function
.esym
4191 && e
->value
.function
.esym
->result
->attr
.pointer
)
4192 || (!e
->value
.function
.esym
4193 && e
->symtree
->n
.sym
->attr
.pointer
))
4194 && fsym
&& fsym
->attr
.target
)
4196 gfc_conv_expr (&parmse
, e
);
4197 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
, parmse
.expr
);
4199 else if (e
->expr_type
== EXPR_FUNCTION
4200 && e
->symtree
->n
.sym
->result
4201 && e
->symtree
->n
.sym
->result
!= e
->symtree
->n
.sym
4202 && e
->symtree
->n
.sym
->result
->attr
.proc_pointer
)
4204 /* Functions returning procedure pointers. */
4205 gfc_conv_expr (&parmse
, e
);
4206 if (fsym
&& fsym
->attr
.proc_pointer
)
4207 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
, parmse
.expr
);
4211 if (e
->ts
.type
== BT_CLASS
&& fsym
4212 && fsym
->ts
.type
== BT_CLASS
4213 && (!CLASS_DATA (fsym
)->as
4214 || CLASS_DATA (fsym
)->as
->type
!= AS_ASSUMED_RANK
)
4215 && CLASS_DATA (e
)->attr
.codimension
)
4217 gcc_assert (!CLASS_DATA (fsym
)->attr
.codimension
);
4218 gcc_assert (!CLASS_DATA (fsym
)->as
);
4219 gfc_add_class_array_ref (e
);
4220 parmse
.want_coarray
= 1;
4221 gfc_conv_expr_reference (&parmse
, e
);
4222 class_scalar_coarray_to_class (&parmse
, e
, fsym
->ts
,
4224 && e
->expr_type
== EXPR_VARIABLE
);
4227 gfc_conv_expr_reference (&parmse
, e
);
4229 /* Catch base objects that are not variables. */
4230 if (e
->ts
.type
== BT_CLASS
4231 && e
->expr_type
!= EXPR_VARIABLE
4232 && expr
&& e
== expr
->base_expr
)
4233 base_object
= build_fold_indirect_ref_loc (input_location
,
4236 /* A class array element needs converting back to be a
4237 class object, if the formal argument is a class object. */
4238 if (fsym
&& fsym
->ts
.type
== BT_CLASS
4239 && e
->ts
.type
== BT_CLASS
4240 && ((CLASS_DATA (fsym
)->as
4241 && CLASS_DATA (fsym
)->as
->type
== AS_ASSUMED_RANK
)
4242 || CLASS_DATA (e
)->attr
.dimension
))
4243 gfc_conv_class_to_class (&parmse
, e
, fsym
->ts
, false,
4244 fsym
->attr
.intent
!= INTENT_IN
4245 && (CLASS_DATA (fsym
)->attr
.class_pointer
4246 || CLASS_DATA (fsym
)->attr
.allocatable
),
4248 && e
->expr_type
== EXPR_VARIABLE
4249 && e
->symtree
->n
.sym
->attr
.optional
,
4250 CLASS_DATA (fsym
)->attr
.class_pointer
4251 || CLASS_DATA (fsym
)->attr
.allocatable
);
4253 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4254 allocated on entry, it must be deallocated. */
4255 if (fsym
&& fsym
->attr
.intent
== INTENT_OUT
4256 && (fsym
->attr
.allocatable
4257 || (fsym
->ts
.type
== BT_CLASS
4258 && CLASS_DATA (fsym
)->attr
.allocatable
)))
4263 gfc_init_block (&block
);
4265 if (e
->ts
.type
== BT_CLASS
)
4266 ptr
= gfc_class_data_get (ptr
);
4268 tmp
= gfc_deallocate_scalar_with_status (ptr
, NULL_TREE
,
4270 gfc_add_expr_to_block (&block
, tmp
);
4271 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4272 void_type_node
, ptr
,
4274 gfc_add_expr_to_block (&block
, tmp
);
4276 if (fsym
->ts
.type
== BT_CLASS
&& UNLIMITED_POLY (fsym
))
4278 gfc_add_modify (&block
, ptr
,
4279 fold_convert (TREE_TYPE (ptr
),
4280 null_pointer_node
));
4281 gfc_add_expr_to_block (&block
, tmp
);
4283 else if (fsym
->ts
.type
== BT_CLASS
)
4286 vtab
= gfc_find_derived_vtab (fsym
->ts
.u
.derived
);
4287 tmp
= gfc_get_symbol_decl (vtab
);
4288 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
4289 ptr
= gfc_class_vptr_get (parmse
.expr
);
4290 gfc_add_modify (&block
, ptr
,
4291 fold_convert (TREE_TYPE (ptr
), tmp
));
4292 gfc_add_expr_to_block (&block
, tmp
);
4295 if (fsym
->attr
.optional
4296 && e
->expr_type
== EXPR_VARIABLE
4297 && e
->symtree
->n
.sym
->attr
.optional
)
4299 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
4301 gfc_conv_expr_present (e
->symtree
->n
.sym
),
4302 gfc_finish_block (&block
),
4303 build_empty_stmt (input_location
));
4306 tmp
= gfc_finish_block (&block
);
4308 gfc_add_expr_to_block (&se
->pre
, tmp
);
4311 if (fsym
&& (fsym
->ts
.type
== BT_DERIVED
4312 || fsym
->ts
.type
== BT_ASSUMED
)
4313 && e
->ts
.type
== BT_CLASS
4314 && !CLASS_DATA (e
)->attr
.dimension
4315 && !CLASS_DATA (e
)->attr
.codimension
)
4316 parmse
.expr
= gfc_class_data_get (parmse
.expr
);
4318 /* Wrap scalar variable in a descriptor. We need to convert
4319 the address of a pointer back to the pointer itself before,
4320 we can assign it to the data field. */
4322 if (fsym
&& fsym
->as
&& fsym
->as
->type
== AS_ASSUMED_RANK
4323 && fsym
->ts
.type
!= BT_CLASS
&& e
->expr_type
!= EXPR_NULL
)
4326 if (TREE_CODE (tmp
) == ADDR_EXPR
4327 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp
, 0))))
4328 tmp
= TREE_OPERAND (tmp
, 0);
4329 parmse
.expr
= gfc_conv_scalar_to_descriptor (&parmse
, tmp
,
4331 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
,
4334 else if (fsym
&& e
->expr_type
!= EXPR_NULL
4335 && ((fsym
->attr
.pointer
4336 && fsym
->attr
.flavor
!= FL_PROCEDURE
)
4337 || (fsym
->attr
.proc_pointer
4338 && !(e
->expr_type
== EXPR_VARIABLE
4339 && e
->symtree
->n
.sym
->attr
.dummy
))
4340 || (fsym
->attr
.proc_pointer
4341 && e
->expr_type
== EXPR_VARIABLE
4342 && gfc_is_proc_ptr_comp (e
))
4343 || (fsym
->attr
.allocatable
4344 && fsym
->attr
.flavor
!= FL_PROCEDURE
)))
4346 /* Scalar pointer dummy args require an extra level of
4347 indirection. The null pointer already contains
4348 this level of indirection. */
4349 parm_kind
= SCALAR_POINTER
;
4350 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
, parmse
.expr
);
4354 else if (e
->ts
.type
== BT_CLASS
4355 && fsym
&& fsym
->ts
.type
== BT_CLASS
4356 && (CLASS_DATA (fsym
)->attr
.dimension
4357 || CLASS_DATA (fsym
)->attr
.codimension
))
4359 /* Pass a class array. */
4360 gfc_conv_expr_descriptor (&parmse
, e
);
4362 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4363 allocated on entry, it must be deallocated. */
4364 if (fsym
->attr
.intent
== INTENT_OUT
4365 && CLASS_DATA (fsym
)->attr
.allocatable
)
4370 gfc_init_block (&block
);
4372 ptr
= gfc_class_data_get (ptr
);
4374 tmp
= gfc_deallocate_with_status (ptr
, NULL_TREE
,
4375 NULL_TREE
, NULL_TREE
,
4378 gfc_add_expr_to_block (&block
, tmp
);
4379 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
4380 void_type_node
, ptr
,
4382 gfc_add_expr_to_block (&block
, tmp
);
4383 gfc_reset_vptr (&block
, e
);
4385 if (fsym
->attr
.optional
4386 && e
->expr_type
== EXPR_VARIABLE
4388 || (e
->ref
->type
== REF_ARRAY
4389 && !e
->ref
->u
.ar
.type
!= AR_FULL
))
4390 && e
->symtree
->n
.sym
->attr
.optional
)
4392 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
4394 gfc_conv_expr_present (e
->symtree
->n
.sym
),
4395 gfc_finish_block (&block
),
4396 build_empty_stmt (input_location
));
4399 tmp
= gfc_finish_block (&block
);
4401 gfc_add_expr_to_block (&se
->pre
, tmp
);
4404 /* The conversion does not repackage the reference to a class
4405 array - _data descriptor. */
4406 gfc_conv_class_to_class (&parmse
, e
, fsym
->ts
, false,
4407 fsym
->attr
.intent
!= INTENT_IN
4408 && (CLASS_DATA (fsym
)->attr
.class_pointer
4409 || CLASS_DATA (fsym
)->attr
.allocatable
),
4411 && e
->expr_type
== EXPR_VARIABLE
4412 && e
->symtree
->n
.sym
->attr
.optional
,
4413 CLASS_DATA (fsym
)->attr
.class_pointer
4414 || CLASS_DATA (fsym
)->attr
.allocatable
);
4418 /* If the procedure requires an explicit interface, the actual
4419 argument is passed according to the corresponding formal
4420 argument. If the corresponding formal argument is a POINTER,
4421 ALLOCATABLE or assumed shape, we do not use g77's calling
4422 convention, and pass the address of the array descriptor
4423 instead. Otherwise we use g77's calling convention. */
4426 && !(fsym
->attr
.pointer
|| fsym
->attr
.allocatable
)
4427 && fsym
->as
&& fsym
->as
->type
!= AS_ASSUMED_SHAPE
4428 && fsym
->as
->type
!= AS_ASSUMED_RANK
;
4430 f
= f
|| !comp
->attr
.always_explicit
;
4432 f
= f
|| !sym
->attr
.always_explicit
;
4434 /* If the argument is a function call that may not create
4435 a temporary for the result, we have to check that we
4436 can do it, i.e. that there is no alias between this
4437 argument and another one. */
4438 if (gfc_get_noncopying_intrinsic_argument (e
) != NULL
)
4444 intent
= fsym
->attr
.intent
;
4446 intent
= INTENT_UNKNOWN
;
4448 if (gfc_check_fncall_dependency (e
, intent
, sym
, args
,
4450 parmse
.force_tmp
= 1;
4452 iarg
= e
->value
.function
.actual
->expr
;
4454 /* Temporary needed if aliasing due to host association. */
4455 if (sym
->attr
.contained
4457 && !sym
->attr
.implicit_pure
4458 && !sym
->attr
.use_assoc
4459 && iarg
->expr_type
== EXPR_VARIABLE
4460 && sym
->ns
== iarg
->symtree
->n
.sym
->ns
)
4461 parmse
.force_tmp
= 1;
4463 /* Ditto within module. */
4464 if (sym
->attr
.use_assoc
4466 && !sym
->attr
.implicit_pure
4467 && iarg
->expr_type
== EXPR_VARIABLE
4468 && sym
->module
== iarg
->symtree
->n
.sym
->module
)
4469 parmse
.force_tmp
= 1;
4472 if (e
->expr_type
== EXPR_VARIABLE
4473 && is_subref_array (e
))
4474 /* The actual argument is a component reference to an
4475 array of derived types. In this case, the argument
4476 is converted to a temporary, which is passed and then
4477 written back after the procedure call. */
4478 gfc_conv_subref_array_arg (&parmse
, e
, f
,
4479 fsym
? fsym
->attr
.intent
: INTENT_INOUT
,
4480 fsym
&& fsym
->attr
.pointer
);
4481 else if (gfc_is_class_array_ref (e
, NULL
)
4482 && fsym
&& fsym
->ts
.type
== BT_DERIVED
)
4483 /* The actual argument is a component reference to an
4484 array of derived types. In this case, the argument
4485 is converted to a temporary, which is passed and then
4486 written back after the procedure call.
4487 OOP-TODO: Insert code so that if the dynamic type is
4488 the same as the declared type, copy-in/copy-out does
4490 gfc_conv_subref_array_arg (&parmse
, e
, f
,
4491 fsym
? fsym
->attr
.intent
: INTENT_INOUT
,
4492 fsym
&& fsym
->attr
.pointer
);
4494 gfc_conv_array_parameter (&parmse
, e
, f
, fsym
, sym
->name
, NULL
);
4496 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4497 allocated on entry, it must be deallocated. */
4498 if (fsym
&& fsym
->attr
.allocatable
4499 && fsym
->attr
.intent
== INTENT_OUT
)
4501 tmp
= build_fold_indirect_ref_loc (input_location
,
4503 tmp
= gfc_trans_dealloc_allocated (tmp
, false, e
);
4504 if (fsym
->attr
.optional
4505 && e
->expr_type
== EXPR_VARIABLE
4506 && e
->symtree
->n
.sym
->attr
.optional
)
4507 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
4509 gfc_conv_expr_present (e
->symtree
->n
.sym
),
4510 tmp
, build_empty_stmt (input_location
));
4511 gfc_add_expr_to_block (&se
->pre
, tmp
);
4516 /* The case with fsym->attr.optional is that of a user subroutine
4517 with an interface indicating an optional argument. When we call
4518 an intrinsic subroutine, however, fsym is NULL, but we might still
4519 have an optional argument, so we proceed to the substitution
4521 if (e
&& (fsym
== NULL
|| fsym
->attr
.optional
))
4523 /* If an optional argument is itself an optional dummy argument,
4524 check its presence and substitute a null if absent. This is
4525 only needed when passing an array to an elemental procedure
4526 as then array elements are accessed - or no NULL pointer is
4527 allowed and a "1" or "0" should be passed if not present.
4528 When passing a non-array-descriptor full array to a
4529 non-array-descriptor dummy, no check is needed. For
4530 array-descriptor actual to array-descriptor dummy, see
4531 PR 41911 for why a check has to be inserted.
4532 fsym == NULL is checked as intrinsics required the descriptor
4533 but do not always set fsym. */
4534 if (e
->expr_type
== EXPR_VARIABLE
4535 && e
->symtree
->n
.sym
->attr
.optional
4536 && ((e
->rank
!= 0 && sym
->attr
.elemental
)
4537 || e
->representation
.length
|| e
->ts
.type
== BT_CHARACTER
4541 && (fsym
->as
->type
== AS_ASSUMED_SHAPE
4542 || fsym
->as
->type
== AS_ASSUMED_RANK
4543 || fsym
->as
->type
== AS_DEFERRED
))))))
4544 gfc_conv_missing_dummy (&parmse
, e
, fsym
? fsym
->ts
: e
->ts
,
4545 e
->representation
.length
);
4550 /* Obtain the character length of an assumed character length
4551 length procedure from the typespec. */
4552 if (fsym
->ts
.type
== BT_CHARACTER
4553 && parmse
.string_length
== NULL_TREE
4554 && e
->ts
.type
== BT_PROCEDURE
4555 && e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
4556 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
!= NULL
4557 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
4559 gfc_conv_const_charlen (e
->symtree
->n
.sym
->ts
.u
.cl
);
4560 parmse
.string_length
= e
->symtree
->n
.sym
->ts
.u
.cl
->backend_decl
;
4564 if (fsym
&& need_interface_mapping
&& e
)
4565 gfc_add_interface_mapping (&mapping
, fsym
, &parmse
, e
);
4567 gfc_add_block_to_block (&se
->pre
, &parmse
.pre
);
4568 gfc_add_block_to_block (&post
, &parmse
.post
);
4570 /* Allocated allocatable components of derived types must be
4571 deallocated for non-variable scalars. Non-variable arrays are
4572 dealt with in trans-array.c(gfc_conv_array_parameter). */
4573 if (e
&& (e
->ts
.type
== BT_DERIVED
|| e
->ts
.type
== BT_CLASS
)
4574 && e
->ts
.u
.derived
->attr
.alloc_comp
4575 && !(e
->symtree
&& e
->symtree
->n
.sym
->attr
.pointer
)
4576 && (e
->expr_type
!= EXPR_VARIABLE
&& !e
->rank
))
4579 tmp
= build_fold_indirect_ref_loc (input_location
,
4581 parm_rank
= e
->rank
;
4589 case (SCALAR_POINTER
):
4590 tmp
= build_fold_indirect_ref_loc (input_location
,
4595 if (e
->expr_type
== EXPR_OP
4596 && e
->value
.op
.op
== INTRINSIC_PARENTHESES
4597 && e
->value
.op
.op1
->expr_type
== EXPR_VARIABLE
)
4600 local_tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
4601 local_tmp
= gfc_copy_alloc_comp (e
->ts
.u
.derived
, local_tmp
, tmp
, parm_rank
);
4602 gfc_add_expr_to_block (&se
->post
, local_tmp
);
4605 if (e
->ts
.type
== BT_DERIVED
&& fsym
&& fsym
->ts
.type
== BT_CLASS
)
4607 /* The derived type is passed to gfc_deallocate_alloc_comp.
4608 Therefore, class actuals can handled correctly but derived
4609 types passed to class formals need the _data component. */
4610 tmp
= gfc_class_data_get (tmp
);
4611 if (!CLASS_DATA (fsym
)->attr
.dimension
)
4612 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
4615 tmp
= gfc_deallocate_alloc_comp (e
->ts
.u
.derived
, tmp
, parm_rank
);
4617 gfc_add_expr_to_block (&se
->post
, tmp
);
4620 /* Add argument checking of passing an unallocated/NULL actual to
4621 a nonallocatable/nonpointer dummy. */
4623 if (gfc_option
.rtcheck
& GFC_RTCHECK_POINTER
&& e
!= NULL
)
4625 symbol_attribute attr
;
4629 if (e
->expr_type
== EXPR_VARIABLE
|| e
->expr_type
== EXPR_FUNCTION
)
4630 attr
= gfc_expr_attr (e
);
4632 goto end_pointer_check
;
4634 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
4635 allocatable to an optional dummy, cf. 12.5.2.12. */
4636 if (fsym
!= NULL
&& fsym
->attr
.optional
&& !attr
.proc_pointer
4637 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
4638 goto end_pointer_check
;
4642 /* If the actual argument is an optional pointer/allocatable and
4643 the formal argument takes an nonpointer optional value,
4644 it is invalid to pass a non-present argument on, even
4645 though there is no technical reason for this in gfortran.
4646 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
4647 tree present
, null_ptr
, type
;
4649 if (attr
.allocatable
4650 && (fsym
== NULL
|| !fsym
->attr
.allocatable
))
4651 asprintf (&msg
, "Allocatable actual argument '%s' is not "
4652 "allocated or not present", e
->symtree
->n
.sym
->name
);
4653 else if (attr
.pointer
4654 && (fsym
== NULL
|| !fsym
->attr
.pointer
))
4655 asprintf (&msg
, "Pointer actual argument '%s' is not "
4656 "associated or not present",
4657 e
->symtree
->n
.sym
->name
);
4658 else if (attr
.proc_pointer
4659 && (fsym
== NULL
|| !fsym
->attr
.proc_pointer
))
4660 asprintf (&msg
, "Proc-pointer actual argument '%s' is not "
4661 "associated or not present",
4662 e
->symtree
->n
.sym
->name
);
4664 goto end_pointer_check
;
4666 present
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
4667 type
= TREE_TYPE (present
);
4668 present
= fold_build2_loc (input_location
, EQ_EXPR
,
4669 boolean_type_node
, present
,
4671 null_pointer_node
));
4672 type
= TREE_TYPE (parmse
.expr
);
4673 null_ptr
= fold_build2_loc (input_location
, EQ_EXPR
,
4674 boolean_type_node
, parmse
.expr
,
4676 null_pointer_node
));
4677 cond
= fold_build2_loc (input_location
, TRUTH_ORIF_EXPR
,
4678 boolean_type_node
, present
, null_ptr
);
4682 if (attr
.allocatable
4683 && (fsym
== NULL
|| !fsym
->attr
.allocatable
))
4684 asprintf (&msg
, "Allocatable actual argument '%s' is not "
4685 "allocated", e
->symtree
->n
.sym
->name
);
4686 else if (attr
.pointer
4687 && (fsym
== NULL
|| !fsym
->attr
.pointer
))
4688 asprintf (&msg
, "Pointer actual argument '%s' is not "
4689 "associated", e
->symtree
->n
.sym
->name
);
4690 else if (attr
.proc_pointer
4691 && (fsym
== NULL
|| !fsym
->attr
.proc_pointer
))
4692 asprintf (&msg
, "Proc-pointer actual argument '%s' is not "
4693 "associated", e
->symtree
->n
.sym
->name
);
4695 goto end_pointer_check
;
4699 /* If the argument is passed by value, we need to strip the
4701 if (!POINTER_TYPE_P (TREE_TYPE (parmse
.expr
)))
4702 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
4704 cond
= fold_build2_loc (input_location
, EQ_EXPR
,
4705 boolean_type_node
, tmp
,
4706 fold_convert (TREE_TYPE (tmp
),
4707 null_pointer_node
));
4710 gfc_trans_runtime_check (true, false, cond
, &se
->pre
, &e
->where
,
4716 /* Deferred length dummies pass the character length by reference
4717 so that the value can be returned. */
4718 if (parmse
.string_length
&& fsym
&& fsym
->ts
.deferred
)
4720 tmp
= parmse
.string_length
;
4721 if (TREE_CODE (tmp
) != VAR_DECL
)
4722 tmp
= gfc_evaluate_now (parmse
.string_length
, &se
->pre
);
4723 parmse
.string_length
= gfc_build_addr_expr (NULL_TREE
, tmp
);
4726 /* Character strings are passed as two parameters, a length and a
4727 pointer - except for Bind(c) which only passes the pointer.
4728 An unlimited polymorphic formal argument likewise does not
4730 if (parmse
.string_length
!= NULL_TREE
4731 && !sym
->attr
.is_bind_c
4732 && !(fsym
&& UNLIMITED_POLY (fsym
)))
4733 vec_safe_push (stringargs
, parmse
.string_length
);
4735 /* When calling __copy for character expressions to unlimited
4736 polymorphic entities, the dst argument needs a string length. */
4737 if (sym
->name
[0] == '_' && e
&& e
->ts
.type
== BT_CHARACTER
4738 && strncmp (sym
->name
, "__vtab_CHARACTER", 16) == 0
4739 && arg
->next
&& arg
->next
->expr
4740 && arg
->next
->expr
->ts
.type
== BT_DERIVED
4741 && arg
->next
->expr
->ts
.u
.derived
->attr
.unlimited_polymorphic
)
4742 vec_safe_push (stringargs
, parmse
.string_length
);
4744 /* For descriptorless coarrays and assumed-shape coarray dummies, we
4745 pass the token and the offset as additional arguments. */
4746 if (fsym
&& fsym
->attr
.codimension
4747 && gfc_option
.coarray
== GFC_FCOARRAY_LIB
4748 && !fsym
->attr
.allocatable
4751 /* Token and offset. */
4752 vec_safe_push (stringargs
, null_pointer_node
);
4753 vec_safe_push (stringargs
, build_int_cst (gfc_array_index_type
, 0));
4754 gcc_assert (fsym
->attr
.optional
);
4756 else if (fsym
&& fsym
->attr
.codimension
4757 && !fsym
->attr
.allocatable
4758 && gfc_option
.coarray
== GFC_FCOARRAY_LIB
)
4760 tree caf_decl
, caf_type
;
4763 caf_decl
= get_tree_for_caf_expr (e
);
4764 caf_type
= TREE_TYPE (caf_decl
);
4766 if (GFC_DESCRIPTOR_TYPE_P (caf_type
)
4767 && GFC_TYPE_ARRAY_AKIND (caf_type
) == GFC_ARRAY_ALLOCATABLE
)
4768 tmp
= gfc_conv_descriptor_token (caf_decl
);
4769 else if (DECL_LANG_SPECIFIC (caf_decl
)
4770 && GFC_DECL_TOKEN (caf_decl
) != NULL_TREE
)
4771 tmp
= GFC_DECL_TOKEN (caf_decl
);
4774 gcc_assert (GFC_ARRAY_TYPE_P (caf_type
)
4775 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) != NULL_TREE
);
4776 tmp
= GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
);
4779 vec_safe_push (stringargs
, tmp
);
4781 if (GFC_DESCRIPTOR_TYPE_P (caf_type
)
4782 && GFC_TYPE_ARRAY_AKIND (caf_type
) == GFC_ARRAY_ALLOCATABLE
)
4783 offset
= build_int_cst (gfc_array_index_type
, 0);
4784 else if (DECL_LANG_SPECIFIC (caf_decl
)
4785 && GFC_DECL_CAF_OFFSET (caf_decl
) != NULL_TREE
)
4786 offset
= GFC_DECL_CAF_OFFSET (caf_decl
);
4787 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) != NULL_TREE
)
4788 offset
= GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
);
4790 offset
= build_int_cst (gfc_array_index_type
, 0);
4792 if (GFC_DESCRIPTOR_TYPE_P (caf_type
))
4793 tmp
= gfc_conv_descriptor_data_get (caf_decl
);
4796 gcc_assert (POINTER_TYPE_P (caf_type
));
4800 if (fsym
->as
->type
== AS_ASSUMED_SHAPE
4801 || (fsym
->as
->type
== AS_ASSUMED_RANK
&& !fsym
->attr
.pointer
4802 && !fsym
->attr
.allocatable
))
4804 gcc_assert (POINTER_TYPE_P (TREE_TYPE (parmse
.expr
)));
4805 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE
4806 (TREE_TYPE (parmse
.expr
))));
4807 tmp2
= build_fold_indirect_ref_loc (input_location
, parmse
.expr
);
4808 tmp2
= gfc_conv_descriptor_data_get (tmp2
);
4810 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse
.expr
)))
4811 tmp2
= gfc_conv_descriptor_data_get (parmse
.expr
);
4814 gcc_assert (POINTER_TYPE_P (TREE_TYPE (parmse
.expr
)));
4818 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
4819 gfc_array_index_type
,
4820 fold_convert (gfc_array_index_type
, tmp2
),
4821 fold_convert (gfc_array_index_type
, tmp
));
4822 offset
= fold_build2_loc (input_location
, PLUS_EXPR
,
4823 gfc_array_index_type
, offset
, tmp
);
4825 vec_safe_push (stringargs
, offset
);
4828 vec_safe_push (arglist
, parmse
.expr
);
4830 gfc_finish_interface_mapping (&mapping
, &se
->pre
, &se
->post
);
4837 if (ts
.type
== BT_CHARACTER
&& sym
->attr
.is_bind_c
)
4838 se
->string_length
= build_int_cst (gfc_charlen_type_node
, 1);
4839 else if (ts
.type
== BT_CHARACTER
)
4841 if (ts
.u
.cl
->length
== NULL
)
4843 /* Assumed character length results are not allowed by 5.1.1.5 of the
4844 standard and are trapped in resolve.c; except in the case of SPREAD
4845 (and other intrinsics?) and dummy functions. In the case of SPREAD,
4846 we take the character length of the first argument for the result.
4847 For dummies, we have to look through the formal argument list for
4848 this function and use the character length found there.*/
4850 cl
.backend_decl
= gfc_create_var (gfc_charlen_type_node
, "slen");
4851 else if (!sym
->attr
.dummy
)
4852 cl
.backend_decl
= (*stringargs
)[0];
4855 formal
= gfc_sym_get_dummy_args (sym
->ns
->proc_name
);
4856 for (; formal
; formal
= formal
->next
)
4857 if (strcmp (formal
->sym
->name
, sym
->name
) == 0)
4858 cl
.backend_decl
= formal
->sym
->ts
.u
.cl
->backend_decl
;
4860 len
= cl
.backend_decl
;
4866 /* Calculate the length of the returned string. */
4867 gfc_init_se (&parmse
, NULL
);
4868 if (need_interface_mapping
)
4869 gfc_apply_interface_mapping (&mapping
, &parmse
, ts
.u
.cl
->length
);
4871 gfc_conv_expr (&parmse
, ts
.u
.cl
->length
);
4872 gfc_add_block_to_block (&se
->pre
, &parmse
.pre
);
4873 gfc_add_block_to_block (&se
->post
, &parmse
.post
);
4875 tmp
= fold_convert (gfc_charlen_type_node
, parmse
.expr
);
4876 tmp
= fold_build2_loc (input_location
, MAX_EXPR
,
4877 gfc_charlen_type_node
, tmp
,
4878 build_int_cst (gfc_charlen_type_node
, 0));
4879 cl
.backend_decl
= tmp
;
4882 /* Set up a charlen structure for it. */
4887 len
= cl
.backend_decl
;
4890 byref
= (comp
&& (comp
->attr
.dimension
|| comp
->ts
.type
== BT_CHARACTER
))
4891 || (!comp
&& gfc_return_by_reference (sym
));
4894 if (se
->direct_byref
)
4896 /* Sometimes, too much indirection can be applied; e.g. for
4897 function_result = array_valued_recursive_function. */
4898 if (TREE_TYPE (TREE_TYPE (se
->expr
))
4899 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se
->expr
)))
4900 && GFC_DESCRIPTOR_TYPE_P
4901 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se
->expr
)))))
4902 se
->expr
= build_fold_indirect_ref_loc (input_location
,
4905 /* If the lhs of an assignment x = f(..) is allocatable and
4906 f2003 is allowed, we must do the automatic reallocation.
4907 TODO - deal with intrinsics, without using a temporary. */
4908 if (gfc_option
.flag_realloc_lhs
4909 && se
->ss
&& se
->ss
->loop_chain
4910 && se
->ss
->loop_chain
->is_alloc_lhs
4911 && !expr
->value
.function
.isym
4912 && sym
->result
->as
!= NULL
)
4914 /* Evaluate the bounds of the result, if known. */
4915 gfc_set_loop_bounds_from_array_spec (&mapping
, se
,
4918 /* Perform the automatic reallocation. */
4919 tmp
= gfc_alloc_allocatable_for_assignment (se
->loop
,
4921 gfc_add_expr_to_block (&se
->pre
, tmp
);
4923 /* Pass the temporary as the first argument. */
4924 result
= info
->descriptor
;
4927 result
= build_fold_indirect_ref_loc (input_location
,
4929 vec_safe_push (retargs
, se
->expr
);
4931 else if (comp
&& comp
->attr
.dimension
)
4933 gcc_assert (se
->loop
&& info
);
4935 /* Set the type of the array. */
4936 tmp
= gfc_typenode_for_spec (&comp
->ts
);
4937 gcc_assert (se
->ss
->dimen
== se
->loop
->dimen
);
4939 /* Evaluate the bounds of the result, if known. */
4940 gfc_set_loop_bounds_from_array_spec (&mapping
, se
, comp
->as
);
4942 /* If the lhs of an assignment x = f(..) is allocatable and
4943 f2003 is allowed, we must not generate the function call
4944 here but should just send back the results of the mapping.
4945 This is signalled by the function ss being flagged. */
4946 if (gfc_option
.flag_realloc_lhs
4947 && se
->ss
&& se
->ss
->is_alloc_lhs
)
4949 gfc_free_interface_mapping (&mapping
);
4950 return has_alternate_specifier
;
4953 /* Create a temporary to store the result. In case the function
4954 returns a pointer, the temporary will be a shallow copy and
4955 mustn't be deallocated. */
4956 callee_alloc
= comp
->attr
.allocatable
|| comp
->attr
.pointer
;
4957 gfc_trans_create_temp_array (&se
->pre
, &se
->post
, se
->ss
,
4958 tmp
, NULL_TREE
, false,
4959 !comp
->attr
.pointer
, callee_alloc
,
4960 &se
->ss
->info
->expr
->where
);
4962 /* Pass the temporary as the first argument. */
4963 result
= info
->descriptor
;
4964 tmp
= gfc_build_addr_expr (NULL_TREE
, result
);
4965 vec_safe_push (retargs
, tmp
);
4967 else if (!comp
&& sym
->result
->attr
.dimension
)
4969 gcc_assert (se
->loop
&& info
);
4971 /* Set the type of the array. */
4972 tmp
= gfc_typenode_for_spec (&ts
);
4973 gcc_assert (se
->ss
->dimen
== se
->loop
->dimen
);
4975 /* Evaluate the bounds of the result, if known. */
4976 gfc_set_loop_bounds_from_array_spec (&mapping
, se
, sym
->result
->as
);
4978 /* If the lhs of an assignment x = f(..) is allocatable and
4979 f2003 is allowed, we must not generate the function call
4980 here but should just send back the results of the mapping.
4981 This is signalled by the function ss being flagged. */
4982 if (gfc_option
.flag_realloc_lhs
4983 && se
->ss
&& se
->ss
->is_alloc_lhs
)
4985 gfc_free_interface_mapping (&mapping
);
4986 return has_alternate_specifier
;
4989 /* Create a temporary to store the result. In case the function
4990 returns a pointer, the temporary will be a shallow copy and
4991 mustn't be deallocated. */
4992 callee_alloc
= sym
->attr
.allocatable
|| sym
->attr
.pointer
;
4993 gfc_trans_create_temp_array (&se
->pre
, &se
->post
, se
->ss
,
4994 tmp
, NULL_TREE
, false,
4995 !sym
->attr
.pointer
, callee_alloc
,
4996 &se
->ss
->info
->expr
->where
);
4998 /* Pass the temporary as the first argument. */
4999 result
= info
->descriptor
;
5000 tmp
= gfc_build_addr_expr (NULL_TREE
, result
);
5001 vec_safe_push (retargs
, tmp
);
5003 else if (ts
.type
== BT_CHARACTER
)
5005 /* Pass the string length. */
5006 type
= gfc_get_character_type (ts
.kind
, ts
.u
.cl
);
5007 type
= build_pointer_type (type
);
5009 /* Return an address to a char[0:len-1]* temporary for
5010 character pointers. */
5011 if ((!comp
&& (sym
->attr
.pointer
|| sym
->attr
.allocatable
))
5012 || (comp
&& (comp
->attr
.pointer
|| comp
->attr
.allocatable
)))
5014 var
= gfc_create_var (type
, "pstr");
5016 if ((!comp
&& sym
->attr
.allocatable
)
5017 || (comp
&& comp
->attr
.allocatable
))
5019 gfc_add_modify (&se
->pre
, var
,
5020 fold_convert (TREE_TYPE (var
),
5021 null_pointer_node
));
5022 tmp
= gfc_call_free (convert (pvoid_type_node
, var
));
5023 gfc_add_expr_to_block (&se
->post
, tmp
);
5026 /* Provide an address expression for the function arguments. */
5027 var
= gfc_build_addr_expr (NULL_TREE
, var
);
5030 var
= gfc_conv_string_tmp (se
, type
, len
);
5032 vec_safe_push (retargs
, var
);
5036 gcc_assert (gfc_option
.flag_f2c
&& ts
.type
== BT_COMPLEX
);
5038 type
= gfc_get_complex_type (ts
.kind
);
5039 var
= gfc_build_addr_expr (NULL_TREE
, gfc_create_var (type
, "cmplx"));
5040 vec_safe_push (retargs
, var
);
5043 /* Add the string length to the argument list. */
5044 if (ts
.type
== BT_CHARACTER
&& ts
.deferred
)
5047 if (TREE_CODE (tmp
) != VAR_DECL
)
5048 tmp
= gfc_evaluate_now (len
, &se
->pre
);
5049 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
5050 vec_safe_push (retargs
, tmp
);
5052 else if (ts
.type
== BT_CHARACTER
)
5053 vec_safe_push (retargs
, len
);
5055 gfc_free_interface_mapping (&mapping
);
5057 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
5058 arglen
= (vec_safe_length (arglist
) + vec_safe_length (optionalargs
)
5059 + vec_safe_length (stringargs
) + vec_safe_length (append_args
));
5060 vec_safe_reserve (retargs
, arglen
);
5062 /* Add the return arguments. */
5063 retargs
->splice (arglist
);
5065 /* Add the hidden present status for optional+value to the arguments. */
5066 retargs
->splice (optionalargs
);
5068 /* Add the hidden string length parameters to the arguments. */
5069 retargs
->splice (stringargs
);
5071 /* We may want to append extra arguments here. This is used e.g. for
5072 calls to libgfortran_matmul_??, which need extra information. */
5073 if (!vec_safe_is_empty (append_args
))
5074 retargs
->splice (append_args
);
5077 /* Generate the actual call. */
5078 if (base_object
== NULL_TREE
)
5079 conv_function_val (se
, sym
, expr
);
5081 conv_base_obj_fcn_val (se
, base_object
, expr
);
5083 /* If there are alternate return labels, function type should be
5084 integer. Can't modify the type in place though, since it can be shared
5085 with other functions. For dummy arguments, the typing is done to
5086 this result, even if it has to be repeated for each call. */
5087 if (has_alternate_specifier
5088 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se
->expr
))) != integer_type_node
)
5090 if (!sym
->attr
.dummy
)
5092 TREE_TYPE (sym
->backend_decl
)
5093 = build_function_type (integer_type_node
,
5094 TYPE_ARG_TYPES (TREE_TYPE (sym
->backend_decl
)));
5095 se
->expr
= gfc_build_addr_expr (NULL_TREE
, sym
->backend_decl
);
5098 TREE_TYPE (TREE_TYPE (TREE_TYPE (se
->expr
))) = integer_type_node
;
5101 fntype
= TREE_TYPE (TREE_TYPE (se
->expr
));
5102 se
->expr
= build_call_vec (TREE_TYPE (fntype
), se
->expr
, arglist
);
5104 /* If we have a pointer function, but we don't want a pointer, e.g.
5107 where f is pointer valued, we have to dereference the result. */
5108 if (!se
->want_pointer
&& !byref
5109 && ((!comp
&& (sym
->attr
.pointer
|| sym
->attr
.allocatable
))
5110 || (comp
&& (comp
->attr
.pointer
|| comp
->attr
.allocatable
))))
5111 se
->expr
= build_fold_indirect_ref_loc (input_location
, se
->expr
);
5113 /* f2c calling conventions require a scalar default real function to
5114 return a double precision result. Convert this back to default
5115 real. We only care about the cases that can happen in Fortran 77.
5117 if (gfc_option
.flag_f2c
&& sym
->ts
.type
== BT_REAL
5118 && sym
->ts
.kind
== gfc_default_real_kind
5119 && !sym
->attr
.always_explicit
)
5120 se
->expr
= fold_convert (gfc_get_real_type (sym
->ts
.kind
), se
->expr
);
5122 /* A pure function may still have side-effects - it may modify its
5124 TREE_SIDE_EFFECTS (se
->expr
) = 1;
5126 if (!sym
->attr
.pure
)
5127 TREE_SIDE_EFFECTS (se
->expr
) = 1;
5132 /* Add the function call to the pre chain. There is no expression. */
5133 gfc_add_expr_to_block (&se
->pre
, se
->expr
);
5134 se
->expr
= NULL_TREE
;
5136 if (!se
->direct_byref
)
5138 if ((sym
->attr
.dimension
&& !comp
) || (comp
&& comp
->attr
.dimension
))
5140 if (gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
)
5142 /* Check the data pointer hasn't been modified. This would
5143 happen in a function returning a pointer. */
5144 tmp
= gfc_conv_descriptor_data_get (info
->descriptor
);
5145 tmp
= fold_build2_loc (input_location
, NE_EXPR
,
5148 gfc_trans_runtime_check (true, false, tmp
, &se
->pre
, NULL
,
5151 se
->expr
= info
->descriptor
;
5152 /* Bundle in the string length. */
5153 se
->string_length
= len
;
5155 else if (ts
.type
== BT_CHARACTER
)
5157 /* Dereference for character pointer results. */
5158 if ((!comp
&& (sym
->attr
.pointer
|| sym
->attr
.allocatable
))
5159 || (comp
&& (comp
->attr
.pointer
|| comp
->attr
.allocatable
)))
5160 se
->expr
= build_fold_indirect_ref_loc (input_location
, var
);
5164 se
->string_length
= len
;
5168 gcc_assert (ts
.type
== BT_COMPLEX
&& gfc_option
.flag_f2c
);
5169 se
->expr
= build_fold_indirect_ref_loc (input_location
, var
);
5174 /* Follow the function call with the argument post block. */
5177 gfc_add_block_to_block (&se
->pre
, &post
);
5179 /* Transformational functions of derived types with allocatable
5180 components must have the result allocatable components copied. */
5181 arg
= expr
->value
.function
.actual
;
5182 if (result
&& arg
&& expr
->rank
5183 && expr
->value
.function
.isym
5184 && expr
->value
.function
.isym
->transformational
5185 && arg
->expr
->ts
.type
== BT_DERIVED
5186 && arg
->expr
->ts
.u
.derived
->attr
.alloc_comp
)
5189 /* Copy the allocatable components. We have to use a
5190 temporary here to prevent source allocatable components
5191 from being corrupted. */
5192 tmp2
= gfc_evaluate_now (result
, &se
->pre
);
5193 tmp
= gfc_copy_alloc_comp (arg
->expr
->ts
.u
.derived
,
5194 result
, tmp2
, expr
->rank
);
5195 gfc_add_expr_to_block (&se
->pre
, tmp
);
5196 tmp
= gfc_copy_allocatable_data (result
, tmp2
, TREE_TYPE(tmp2
),
5198 gfc_add_expr_to_block (&se
->pre
, tmp
);
5200 /* Finally free the temporary's data field. */
5201 tmp
= gfc_conv_descriptor_data_get (tmp2
);
5202 tmp
= gfc_deallocate_with_status (tmp
, NULL_TREE
, NULL_TREE
,
5203 NULL_TREE
, NULL_TREE
, true,
5205 gfc_add_expr_to_block (&se
->pre
, tmp
);
5209 gfc_add_block_to_block (&se
->post
, &post
);
5211 return has_alternate_specifier
;
5215 /* Fill a character string with spaces. */
5218 fill_with_spaces (tree start
, tree type
, tree size
)
5220 stmtblock_t block
, loop
;
5221 tree i
, el
, exit_label
, cond
, tmp
;
5223 /* For a simple char type, we can call memset(). */
5224 if (compare_tree_int (TYPE_SIZE_UNIT (type
), 1) == 0)
5225 return build_call_expr_loc (input_location
,
5226 builtin_decl_explicit (BUILT_IN_MEMSET
),
5228 build_int_cst (gfc_get_int_type (gfc_c_int_kind
),
5229 lang_hooks
.to_target_charset (' ')),
5232 /* Otherwise, we use a loop:
5233 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
5237 /* Initialize variables. */
5238 gfc_init_block (&block
);
5239 i
= gfc_create_var (sizetype
, "i");
5240 gfc_add_modify (&block
, i
, fold_convert (sizetype
, size
));
5241 el
= gfc_create_var (build_pointer_type (type
), "el");
5242 gfc_add_modify (&block
, el
, fold_convert (TREE_TYPE (el
), start
));
5243 exit_label
= gfc_build_label_decl (NULL_TREE
);
5244 TREE_USED (exit_label
) = 1;
5248 gfc_init_block (&loop
);
5250 /* Exit condition. */
5251 cond
= fold_build2_loc (input_location
, LE_EXPR
, boolean_type_node
, i
,
5252 build_zero_cst (sizetype
));
5253 tmp
= build1_v (GOTO_EXPR
, exit_label
);
5254 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
, tmp
,
5255 build_empty_stmt (input_location
));
5256 gfc_add_expr_to_block (&loop
, tmp
);
5259 gfc_add_modify (&loop
,
5260 fold_build1_loc (input_location
, INDIRECT_REF
, type
, el
),
5261 build_int_cst (type
, lang_hooks
.to_target_charset (' ')));
5263 /* Increment loop variables. */
5264 gfc_add_modify (&loop
, i
,
5265 fold_build2_loc (input_location
, MINUS_EXPR
, sizetype
, i
,
5266 TYPE_SIZE_UNIT (type
)));
5267 gfc_add_modify (&loop
, el
,
5268 fold_build_pointer_plus_loc (input_location
,
5269 el
, TYPE_SIZE_UNIT (type
)));
5271 /* Making the loop... actually loop! */
5272 tmp
= gfc_finish_block (&loop
);
5273 tmp
= build1_v (LOOP_EXPR
, tmp
);
5274 gfc_add_expr_to_block (&block
, tmp
);
5276 /* The exit label. */
5277 tmp
= build1_v (LABEL_EXPR
, exit_label
);
5278 gfc_add_expr_to_block (&block
, tmp
);
5281 return gfc_finish_block (&block
);
5285 /* Generate code to copy a string. */
5288 gfc_trans_string_copy (stmtblock_t
* block
, tree dlength
, tree dest
,
5289 int dkind
, tree slength
, tree src
, int skind
)
5291 tree tmp
, dlen
, slen
;
5300 stmtblock_t tempblock
;
5302 gcc_assert (dkind
== skind
);
5304 if (slength
!= NULL_TREE
)
5306 slen
= fold_convert (size_type_node
, gfc_evaluate_now (slength
, block
));
5307 ssc
= gfc_string_to_single_character (slen
, src
, skind
);
5311 slen
= build_int_cst (size_type_node
, 1);
5315 if (dlength
!= NULL_TREE
)
5317 dlen
= fold_convert (size_type_node
, gfc_evaluate_now (dlength
, block
));
5318 dsc
= gfc_string_to_single_character (dlen
, dest
, dkind
);
5322 dlen
= build_int_cst (size_type_node
, 1);
5326 /* Assign directly if the types are compatible. */
5327 if (dsc
!= NULL_TREE
&& ssc
!= NULL_TREE
5328 && TREE_TYPE (dsc
) == TREE_TYPE (ssc
))
5330 gfc_add_modify (block
, dsc
, ssc
);
5334 /* Do nothing if the destination length is zero. */
5335 cond
= fold_build2_loc (input_location
, GT_EXPR
, boolean_type_node
, dlen
,
5336 build_int_cst (size_type_node
, 0));
5338 /* The following code was previously in _gfortran_copy_string:
5340 // The two strings may overlap so we use memmove.
5342 copy_string (GFC_INTEGER_4 destlen, char * dest,
5343 GFC_INTEGER_4 srclen, const char * src)
5345 if (srclen >= destlen)
5347 // This will truncate if too long.
5348 memmove (dest, src, destlen);
5352 memmove (dest, src, srclen);
5354 memset (&dest[srclen], ' ', destlen - srclen);
5358 We're now doing it here for better optimization, but the logic
5361 /* For non-default character kinds, we have to multiply the string
5362 length by the base type size. */
5363 chartype
= gfc_get_char_type (dkind
);
5364 slen
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
5365 fold_convert (size_type_node
, slen
),
5366 fold_convert (size_type_node
,
5367 TYPE_SIZE_UNIT (chartype
)));
5368 dlen
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
5369 fold_convert (size_type_node
, dlen
),
5370 fold_convert (size_type_node
,
5371 TYPE_SIZE_UNIT (chartype
)));
5373 if (dlength
&& POINTER_TYPE_P (TREE_TYPE (dest
)))
5374 dest
= fold_convert (pvoid_type_node
, dest
);
5376 dest
= gfc_build_addr_expr (pvoid_type_node
, dest
);
5378 if (slength
&& POINTER_TYPE_P (TREE_TYPE (src
)))
5379 src
= fold_convert (pvoid_type_node
, src
);
5381 src
= gfc_build_addr_expr (pvoid_type_node
, src
);
5383 /* Truncate string if source is too long. */
5384 cond2
= fold_build2_loc (input_location
, GE_EXPR
, boolean_type_node
, slen
,
5386 tmp2
= build_call_expr_loc (input_location
,
5387 builtin_decl_explicit (BUILT_IN_MEMMOVE
),
5388 3, dest
, src
, dlen
);
5390 /* Else copy and pad with spaces. */
5391 tmp3
= build_call_expr_loc (input_location
,
5392 builtin_decl_explicit (BUILT_IN_MEMMOVE
),
5393 3, dest
, src
, slen
);
5395 tmp4
= fold_build_pointer_plus_loc (input_location
, dest
, slen
);
5396 tmp4
= fill_with_spaces (tmp4
, chartype
,
5397 fold_build2_loc (input_location
, MINUS_EXPR
,
5398 TREE_TYPE(dlen
), dlen
, slen
));
5400 gfc_init_block (&tempblock
);
5401 gfc_add_expr_to_block (&tempblock
, tmp3
);
5402 gfc_add_expr_to_block (&tempblock
, tmp4
);
5403 tmp3
= gfc_finish_block (&tempblock
);
5405 /* The whole copy_string function is there. */
5406 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond2
,
5408 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
, tmp
,
5409 build_empty_stmt (input_location
));
5410 gfc_add_expr_to_block (block
, tmp
);
5414 /* Translate a statement function.
5415 The value of a statement function reference is obtained by evaluating the
5416 expression using the values of the actual arguments for the values of the
5417 corresponding dummy arguments. */
5420 gfc_conv_statement_function (gfc_se
* se
, gfc_expr
* expr
)
5424 gfc_formal_arglist
*fargs
;
5425 gfc_actual_arglist
*args
;
5428 gfc_saved_var
*saved_vars
;
5434 sym
= expr
->symtree
->n
.sym
;
5435 args
= expr
->value
.function
.actual
;
5436 gfc_init_se (&lse
, NULL
);
5437 gfc_init_se (&rse
, NULL
);
5440 for (fargs
= gfc_sym_get_dummy_args (sym
); fargs
; fargs
= fargs
->next
)
5442 saved_vars
= XCNEWVEC (gfc_saved_var
, n
);
5443 temp_vars
= XCNEWVEC (tree
, n
);
5445 for (fargs
= gfc_sym_get_dummy_args (sym
), n
= 0; fargs
;
5446 fargs
= fargs
->next
, n
++)
5448 /* Each dummy shall be specified, explicitly or implicitly, to be
5450 gcc_assert (fargs
->sym
->attr
.dimension
== 0);
5453 if (fsym
->ts
.type
== BT_CHARACTER
)
5455 /* Copy string arguments. */
5458 gcc_assert (fsym
->ts
.u
.cl
&& fsym
->ts
.u
.cl
->length
5459 && fsym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
5461 /* Create a temporary to hold the value. */
5462 if (fsym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
5463 fsym
->ts
.u
.cl
->backend_decl
5464 = gfc_conv_constant_to_tree (fsym
->ts
.u
.cl
->length
);
5466 type
= gfc_get_character_type (fsym
->ts
.kind
, fsym
->ts
.u
.cl
);
5467 temp_vars
[n
] = gfc_create_var (type
, fsym
->name
);
5469 arglen
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
5471 gfc_conv_expr (&rse
, args
->expr
);
5472 gfc_conv_string_parameter (&rse
);
5473 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
5474 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
5476 gfc_trans_string_copy (&se
->pre
, arglen
, temp_vars
[n
], fsym
->ts
.kind
,
5477 rse
.string_length
, rse
.expr
, fsym
->ts
.kind
);
5478 gfc_add_block_to_block (&se
->pre
, &lse
.post
);
5479 gfc_add_block_to_block (&se
->pre
, &rse
.post
);
5483 /* For everything else, just evaluate the expression. */
5485 /* Create a temporary to hold the value. */
5486 type
= gfc_typenode_for_spec (&fsym
->ts
);
5487 temp_vars
[n
] = gfc_create_var (type
, fsym
->name
);
5489 gfc_conv_expr (&lse
, args
->expr
);
5491 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
5492 gfc_add_modify (&se
->pre
, temp_vars
[n
], lse
.expr
);
5493 gfc_add_block_to_block (&se
->pre
, &lse
.post
);
5499 /* Use the temporary variables in place of the real ones. */
5500 for (fargs
= gfc_sym_get_dummy_args (sym
), n
= 0; fargs
;
5501 fargs
= fargs
->next
, n
++)
5502 gfc_shadow_sym (fargs
->sym
, temp_vars
[n
], &saved_vars
[n
]);
5504 gfc_conv_expr (se
, sym
->value
);
5506 if (sym
->ts
.type
== BT_CHARACTER
)
5508 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
5510 /* Force the expression to the correct length. */
5511 if (!INTEGER_CST_P (se
->string_length
)
5512 || tree_int_cst_lt (se
->string_length
,
5513 sym
->ts
.u
.cl
->backend_decl
))
5515 type
= gfc_get_character_type (sym
->ts
.kind
, sym
->ts
.u
.cl
);
5516 tmp
= gfc_create_var (type
, sym
->name
);
5517 tmp
= gfc_build_addr_expr (build_pointer_type (type
), tmp
);
5518 gfc_trans_string_copy (&se
->pre
, sym
->ts
.u
.cl
->backend_decl
, tmp
,
5519 sym
->ts
.kind
, se
->string_length
, se
->expr
,
5523 se
->string_length
= sym
->ts
.u
.cl
->backend_decl
;
5526 /* Restore the original variables. */
5527 for (fargs
= gfc_sym_get_dummy_args (sym
), n
= 0; fargs
;
5528 fargs
= fargs
->next
, n
++)
5529 gfc_restore_sym (fargs
->sym
, &saved_vars
[n
]);
5535 /* Translate a function expression. */
5538 gfc_conv_function_expr (gfc_se
* se
, gfc_expr
* expr
)
5542 if (expr
->value
.function
.isym
)
5544 gfc_conv_intrinsic_function (se
, expr
);
5548 /* expr.value.function.esym is the resolved (specific) function symbol for
5549 most functions. However this isn't set for dummy procedures. */
5550 sym
= expr
->value
.function
.esym
;
5552 sym
= expr
->symtree
->n
.sym
;
5554 /* We distinguish statement functions from general functions to improve
5555 runtime performance. */
5556 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
5558 gfc_conv_statement_function (se
, expr
);
5562 gfc_conv_procedure_call (se
, sym
, expr
->value
.function
.actual
, expr
,
5567 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
5570 is_zero_initializer_p (gfc_expr
* expr
)
5572 if (expr
->expr_type
!= EXPR_CONSTANT
)
5575 /* We ignore constants with prescribed memory representations for now. */
5576 if (expr
->representation
.string
)
5579 switch (expr
->ts
.type
)
5582 return mpz_cmp_si (expr
->value
.integer
, 0) == 0;
5585 return mpfr_zero_p (expr
->value
.real
)
5586 && MPFR_SIGN (expr
->value
.real
) >= 0;
5589 return expr
->value
.logical
== 0;
5592 return mpfr_zero_p (mpc_realref (expr
->value
.complex))
5593 && MPFR_SIGN (mpc_realref (expr
->value
.complex)) >= 0
5594 && mpfr_zero_p (mpc_imagref (expr
->value
.complex))
5595 && MPFR_SIGN (mpc_imagref (expr
->value
.complex)) >= 0;
5605 gfc_conv_array_constructor_expr (gfc_se
* se
, gfc_expr
* expr
)
5610 gcc_assert (ss
!= NULL
&& ss
!= gfc_ss_terminator
);
5611 gcc_assert (ss
->info
->expr
== expr
&& ss
->info
->type
== GFC_SS_CONSTRUCTOR
);
5613 gfc_conv_tmp_array_ref (se
);
5617 /* Build a static initializer. EXPR is the expression for the initial value.
5618 The other parameters describe the variable of the component being
5619 initialized. EXPR may be null. */
5622 gfc_conv_initializer (gfc_expr
* expr
, gfc_typespec
* ts
, tree type
,
5623 bool array
, bool pointer
, bool procptr
)
5627 if (!(expr
|| pointer
|| procptr
))
5630 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
5631 (these are the only two iso_c_binding derived types that can be
5632 used as initialization expressions). If so, we need to modify
5633 the 'expr' to be that for a (void *). */
5634 if (expr
!= NULL
&& expr
->ts
.type
== BT_DERIVED
5635 && expr
->ts
.is_iso_c
&& expr
->ts
.u
.derived
)
5637 gfc_symbol
*derived
= expr
->ts
.u
.derived
;
5639 /* The derived symbol has already been converted to a (void *). Use
5641 expr
= gfc_get_int_expr (derived
->ts
.kind
, NULL
, 0);
5642 expr
->ts
.f90_type
= derived
->ts
.f90_type
;
5644 gfc_init_se (&se
, NULL
);
5645 gfc_conv_constant (&se
, expr
);
5646 gcc_assert (TREE_CODE (se
.expr
) != CONSTRUCTOR
);
5650 if (array
&& !procptr
)
5653 /* Arrays need special handling. */
5655 ctor
= gfc_build_null_descriptor (type
);
5656 /* Special case assigning an array to zero. */
5657 else if (is_zero_initializer_p (expr
))
5658 ctor
= build_constructor (type
, NULL
);
5660 ctor
= gfc_conv_array_initializer (type
, expr
);
5661 TREE_STATIC (ctor
) = 1;
5664 else if (pointer
|| procptr
)
5666 if (ts
->type
== BT_CLASS
&& !procptr
)
5668 gfc_init_se (&se
, NULL
);
5669 gfc_conv_structure (&se
, gfc_class_initializer (ts
, expr
), 1);
5670 gcc_assert (TREE_CODE (se
.expr
) == CONSTRUCTOR
);
5671 TREE_STATIC (se
.expr
) = 1;
5674 else if (!expr
|| expr
->expr_type
== EXPR_NULL
)
5675 return fold_convert (type
, null_pointer_node
);
5678 gfc_init_se (&se
, NULL
);
5679 se
.want_pointer
= 1;
5680 gfc_conv_expr (&se
, expr
);
5681 gcc_assert (TREE_CODE (se
.expr
) != CONSTRUCTOR
);
5691 gfc_init_se (&se
, NULL
);
5692 if (ts
->type
== BT_CLASS
&& expr
->expr_type
== EXPR_NULL
)
5693 gfc_conv_structure (&se
, gfc_class_initializer (ts
, expr
), 1);
5695 gfc_conv_structure (&se
, expr
, 1);
5696 gcc_assert (TREE_CODE (se
.expr
) == CONSTRUCTOR
);
5697 TREE_STATIC (se
.expr
) = 1;
5702 tree ctor
= gfc_conv_string_init (ts
->u
.cl
->backend_decl
,expr
);
5703 TREE_STATIC (ctor
) = 1;
5708 gfc_init_se (&se
, NULL
);
5709 gfc_conv_constant (&se
, expr
);
5710 gcc_assert (TREE_CODE (se
.expr
) != CONSTRUCTOR
);
5717 gfc_trans_subarray_assign (tree dest
, gfc_component
* cm
, gfc_expr
* expr
)
5723 gfc_array_info
*lss_array
;
5730 gfc_start_block (&block
);
5732 /* Initialize the scalarizer. */
5733 gfc_init_loopinfo (&loop
);
5735 gfc_init_se (&lse
, NULL
);
5736 gfc_init_se (&rse
, NULL
);
5739 rss
= gfc_walk_expr (expr
);
5740 if (rss
== gfc_ss_terminator
)
5741 /* The rhs is scalar. Add a ss for the expression. */
5742 rss
= gfc_get_scalar_ss (gfc_ss_terminator
, expr
);
5744 /* Create a SS for the destination. */
5745 lss
= gfc_get_array_ss (gfc_ss_terminator
, NULL
, cm
->as
->rank
,
5747 lss_array
= &lss
->info
->data
.array
;
5748 lss_array
->shape
= gfc_get_shape (cm
->as
->rank
);
5749 lss_array
->descriptor
= dest
;
5750 lss_array
->data
= gfc_conv_array_data (dest
);
5751 lss_array
->offset
= gfc_conv_array_offset (dest
);
5752 for (n
= 0; n
< cm
->as
->rank
; n
++)
5754 lss_array
->start
[n
] = gfc_conv_array_lbound (dest
, n
);
5755 lss_array
->stride
[n
] = gfc_index_one_node
;
5757 mpz_init (lss_array
->shape
[n
]);
5758 mpz_sub (lss_array
->shape
[n
], cm
->as
->upper
[n
]->value
.integer
,
5759 cm
->as
->lower
[n
]->value
.integer
);
5760 mpz_add_ui (lss_array
->shape
[n
], lss_array
->shape
[n
], 1);
5763 /* Associate the SS with the loop. */
5764 gfc_add_ss_to_loop (&loop
, lss
);
5765 gfc_add_ss_to_loop (&loop
, rss
);
5767 /* Calculate the bounds of the scalarization. */
5768 gfc_conv_ss_startstride (&loop
);
5770 /* Setup the scalarizing loops. */
5771 gfc_conv_loop_setup (&loop
, &expr
->where
);
5773 /* Setup the gfc_se structures. */
5774 gfc_copy_loopinfo_to_se (&lse
, &loop
);
5775 gfc_copy_loopinfo_to_se (&rse
, &loop
);
5778 gfc_mark_ss_chain_used (rss
, 1);
5780 gfc_mark_ss_chain_used (lss
, 1);
5782 /* Start the scalarized loop body. */
5783 gfc_start_scalarized_body (&loop
, &body
);
5785 gfc_conv_tmp_array_ref (&lse
);
5786 if (cm
->ts
.type
== BT_CHARACTER
)
5787 lse
.string_length
= cm
->ts
.u
.cl
->backend_decl
;
5789 gfc_conv_expr (&rse
, expr
);
5791 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, cm
->ts
, true, false, true);
5792 gfc_add_expr_to_block (&body
, tmp
);
5794 gcc_assert (rse
.ss
== gfc_ss_terminator
);
5796 /* Generate the copying loops. */
5797 gfc_trans_scalarizing_loops (&loop
, &body
);
5799 /* Wrap the whole thing up. */
5800 gfc_add_block_to_block (&block
, &loop
.pre
);
5801 gfc_add_block_to_block (&block
, &loop
.post
);
5803 gcc_assert (lss_array
->shape
!= NULL
);
5804 gfc_free_shape (&lss_array
->shape
, cm
->as
->rank
);
5805 gfc_cleanup_loop (&loop
);
5807 return gfc_finish_block (&block
);
5812 gfc_trans_alloc_subarray_assign (tree dest
, gfc_component
* cm
,
5822 gfc_expr
*arg
= NULL
;
5824 gfc_start_block (&block
);
5825 gfc_init_se (&se
, NULL
);
5827 /* Get the descriptor for the expressions. */
5828 se
.want_pointer
= 0;
5829 gfc_conv_expr_descriptor (&se
, expr
);
5830 gfc_add_block_to_block (&block
, &se
.pre
);
5831 gfc_add_modify (&block
, dest
, se
.expr
);
5833 /* Deal with arrays of derived types with allocatable components. */
5834 if (cm
->ts
.type
== BT_DERIVED
5835 && cm
->ts
.u
.derived
->attr
.alloc_comp
)
5836 tmp
= gfc_copy_alloc_comp (cm
->ts
.u
.derived
,
5840 tmp
= gfc_duplicate_allocatable (dest
, se
.expr
,
5841 TREE_TYPE(cm
->backend_decl
),
5844 gfc_add_expr_to_block (&block
, tmp
);
5845 gfc_add_block_to_block (&block
, &se
.post
);
5847 if (expr
->expr_type
!= EXPR_VARIABLE
)
5848 gfc_conv_descriptor_data_set (&block
, se
.expr
,
5851 /* We need to know if the argument of a conversion function is a
5852 variable, so that the correct lower bound can be used. */
5853 if (expr
->expr_type
== EXPR_FUNCTION
5854 && expr
->value
.function
.isym
5855 && expr
->value
.function
.isym
->conversion
5856 && expr
->value
.function
.actual
->expr
5857 && expr
->value
.function
.actual
->expr
->expr_type
== EXPR_VARIABLE
)
5858 arg
= expr
->value
.function
.actual
->expr
;
5860 /* Obtain the array spec of full array references. */
5862 as
= gfc_get_full_arrayspec_from_expr (arg
);
5864 as
= gfc_get_full_arrayspec_from_expr (expr
);
5866 /* Shift the lbound and ubound of temporaries to being unity,
5867 rather than zero, based. Always calculate the offset. */
5868 offset
= gfc_conv_descriptor_offset_get (dest
);
5869 gfc_add_modify (&block
, offset
, gfc_index_zero_node
);
5870 tmp2
=gfc_create_var (gfc_array_index_type
, NULL
);
5872 for (n
= 0; n
< expr
->rank
; n
++)
5877 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
5878 TODO It looks as if gfc_conv_expr_descriptor should return
5879 the correct bounds and that the following should not be
5880 necessary. This would simplify gfc_conv_intrinsic_bound
5882 if (as
&& as
->lower
[n
])
5885 gfc_init_se (&lbse
, NULL
);
5886 gfc_conv_expr (&lbse
, as
->lower
[n
]);
5887 gfc_add_block_to_block (&block
, &lbse
.pre
);
5888 lbound
= gfc_evaluate_now (lbse
.expr
, &block
);
5892 tmp
= gfc_get_symbol_decl (arg
->symtree
->n
.sym
);
5893 lbound
= gfc_conv_descriptor_lbound_get (tmp
,
5897 lbound
= gfc_conv_descriptor_lbound_get (dest
,
5900 lbound
= gfc_index_one_node
;
5902 lbound
= fold_convert (gfc_array_index_type
, lbound
);
5904 /* Shift the bounds and set the offset accordingly. */
5905 tmp
= gfc_conv_descriptor_ubound_get (dest
, gfc_rank_cst
[n
]);
5906 span
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
5907 tmp
, gfc_conv_descriptor_lbound_get (dest
, gfc_rank_cst
[n
]));
5908 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
, gfc_array_index_type
,
5910 gfc_conv_descriptor_ubound_set (&block
, dest
,
5911 gfc_rank_cst
[n
], tmp
);
5912 gfc_conv_descriptor_lbound_set (&block
, dest
,
5913 gfc_rank_cst
[n
], lbound
);
5915 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
5916 gfc_conv_descriptor_lbound_get (dest
,
5918 gfc_conv_descriptor_stride_get (dest
,
5920 gfc_add_modify (&block
, tmp2
, tmp
);
5921 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
5923 gfc_conv_descriptor_offset_set (&block
, dest
, tmp
);
5928 /* If a conversion expression has a null data pointer
5929 argument, nullify the allocatable component. */
5933 if (arg
->symtree
->n
.sym
->attr
.allocatable
5934 || arg
->symtree
->n
.sym
->attr
.pointer
)
5936 non_null_expr
= gfc_finish_block (&block
);
5937 gfc_start_block (&block
);
5938 gfc_conv_descriptor_data_set (&block
, dest
,
5940 null_expr
= gfc_finish_block (&block
);
5941 tmp
= gfc_conv_descriptor_data_get (arg
->symtree
->n
.sym
->backend_decl
);
5942 tmp
= build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, tmp
,
5943 fold_convert (TREE_TYPE (tmp
), null_pointer_node
));
5944 return build3_v (COND_EXPR
, tmp
,
5945 null_expr
, non_null_expr
);
5949 return gfc_finish_block (&block
);
5953 /* Assign a single component of a derived type constructor. */
5956 gfc_trans_subcomponent_assign (tree dest
, gfc_component
* cm
, gfc_expr
* expr
)
5963 gfc_start_block (&block
);
5965 if (cm
->attr
.pointer
|| cm
->attr
.proc_pointer
)
5967 gfc_init_se (&se
, NULL
);
5968 /* Pointer component. */
5969 if (cm
->attr
.dimension
&& !cm
->attr
.proc_pointer
)
5971 /* Array pointer. */
5972 if (expr
->expr_type
== EXPR_NULL
)
5973 gfc_conv_descriptor_data_set (&block
, dest
, null_pointer_node
);
5976 se
.direct_byref
= 1;
5978 gfc_conv_expr_descriptor (&se
, expr
);
5979 gfc_add_block_to_block (&block
, &se
.pre
);
5980 gfc_add_block_to_block (&block
, &se
.post
);
5985 /* Scalar pointers. */
5986 se
.want_pointer
= 1;
5987 gfc_conv_expr (&se
, expr
);
5988 gfc_add_block_to_block (&block
, &se
.pre
);
5990 if (expr
->symtree
&& expr
->symtree
->n
.sym
->attr
.proc_pointer
5991 && expr
->symtree
->n
.sym
->attr
.dummy
)
5992 se
.expr
= build_fold_indirect_ref_loc (input_location
, se
.expr
);
5994 gfc_add_modify (&block
, dest
,
5995 fold_convert (TREE_TYPE (dest
), se
.expr
));
5996 gfc_add_block_to_block (&block
, &se
.post
);
5999 else if (cm
->ts
.type
== BT_CLASS
&& expr
->expr_type
== EXPR_NULL
)
6001 /* NULL initialization for CLASS components. */
6002 tmp
= gfc_trans_structure_assign (dest
,
6003 gfc_class_initializer (&cm
->ts
, expr
));
6004 gfc_add_expr_to_block (&block
, tmp
);
6006 else if (cm
->attr
.dimension
&& !cm
->attr
.proc_pointer
)
6008 if (cm
->attr
.allocatable
&& expr
->expr_type
== EXPR_NULL
)
6009 gfc_conv_descriptor_data_set (&block
, dest
, null_pointer_node
);
6010 else if (cm
->attr
.allocatable
)
6012 tmp
= gfc_trans_alloc_subarray_assign (dest
, cm
, expr
);
6013 gfc_add_expr_to_block (&block
, tmp
);
6017 tmp
= gfc_trans_subarray_assign (dest
, cm
, expr
);
6018 gfc_add_expr_to_block (&block
, tmp
);
6021 else if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.f90_type
!= BT_VOID
)
6023 if (expr
->expr_type
!= EXPR_STRUCTURE
)
6025 gfc_init_se (&se
, NULL
);
6026 gfc_conv_expr (&se
, expr
);
6027 gfc_add_block_to_block (&block
, &se
.pre
);
6028 gfc_add_modify (&block
, dest
,
6029 fold_convert (TREE_TYPE (dest
), se
.expr
));
6030 gfc_add_block_to_block (&block
, &se
.post
);
6034 /* Nested constructors. */
6035 tmp
= gfc_trans_structure_assign (dest
, expr
);
6036 gfc_add_expr_to_block (&block
, tmp
);
6041 /* Scalar component. */
6042 gfc_init_se (&se
, NULL
);
6043 gfc_init_se (&lse
, NULL
);
6045 gfc_conv_expr (&se
, expr
);
6046 if (cm
->ts
.type
== BT_CHARACTER
)
6047 lse
.string_length
= cm
->ts
.u
.cl
->backend_decl
;
6049 tmp
= gfc_trans_scalar_assign (&lse
, &se
, cm
->ts
, true, false, true);
6050 gfc_add_expr_to_block (&block
, tmp
);
6052 return gfc_finish_block (&block
);
6055 /* Assign a derived type constructor to a variable. */
6058 gfc_trans_structure_assign (tree dest
, gfc_expr
* expr
)
6066 gfc_start_block (&block
);
6067 cm
= expr
->ts
.u
.derived
->components
;
6069 if (expr
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_C_BINDING
6070 && (expr
->ts
.u
.derived
->intmod_sym_id
== ISOCBINDING_PTR
6071 || expr
->ts
.u
.derived
->intmod_sym_id
== ISOCBINDING_FUNPTR
))
6075 gcc_assert (cm
->backend_decl
== NULL
);
6076 gfc_init_se (&se
, NULL
);
6077 gfc_init_se (&lse
, NULL
);
6078 gfc_conv_expr (&se
, gfc_constructor_first (expr
->value
.constructor
)->expr
);
6080 gfc_add_modify (&block
, lse
.expr
,
6081 fold_convert (TREE_TYPE (lse
.expr
), se
.expr
));
6083 return gfc_finish_block (&block
);
6086 for (c
= gfc_constructor_first (expr
->value
.constructor
);
6087 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
6089 /* Skip absent members in default initializers. */
6093 field
= cm
->backend_decl
;
6094 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (field
),
6095 dest
, field
, NULL_TREE
);
6096 tmp
= gfc_trans_subcomponent_assign (tmp
, cm
, c
->expr
);
6097 gfc_add_expr_to_block (&block
, tmp
);
6099 return gfc_finish_block (&block
);
6102 /* Build an expression for a constructor. If init is nonzero then
6103 this is part of a static variable initializer. */
6106 gfc_conv_structure (gfc_se
* se
, gfc_expr
* expr
, int init
)
6113 vec
<constructor_elt
, va_gc
> *v
= NULL
;
6115 gcc_assert (se
->ss
== NULL
);
6116 gcc_assert (expr
->expr_type
== EXPR_STRUCTURE
);
6117 type
= gfc_typenode_for_spec (&expr
->ts
);
6121 /* Create a temporary variable and fill it in. */
6122 se
->expr
= gfc_create_var (type
, expr
->ts
.u
.derived
->name
);
6123 tmp
= gfc_trans_structure_assign (se
->expr
, expr
);
6124 gfc_add_expr_to_block (&se
->pre
, tmp
);
6128 cm
= expr
->ts
.u
.derived
->components
;
6130 for (c
= gfc_constructor_first (expr
->value
.constructor
);
6131 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
6133 /* Skip absent members in default initializers and allocatable
6134 components. Although the latter have a default initializer
6135 of EXPR_NULL,... by default, the static nullify is not needed
6136 since this is done every time we come into scope. */
6137 if (!c
->expr
|| (cm
->attr
.allocatable
&& cm
->attr
.flavor
!= FL_PROCEDURE
))
6140 if (cm
->initializer
&& cm
->initializer
->expr_type
!= EXPR_NULL
6141 && strcmp (cm
->name
, "_extends") == 0
6142 && cm
->initializer
->symtree
)
6146 vtabs
= cm
->initializer
->symtree
->n
.sym
;
6147 vtab
= gfc_build_addr_expr (NULL_TREE
, gfc_get_symbol_decl (vtabs
));
6148 vtab
= unshare_expr_without_location (vtab
);
6149 CONSTRUCTOR_APPEND_ELT (v
, cm
->backend_decl
, vtab
);
6151 else if (cm
->ts
.u
.derived
&& strcmp (cm
->name
, "_size") == 0)
6153 val
= TYPE_SIZE_UNIT (gfc_get_derived_type (cm
->ts
.u
.derived
));
6154 CONSTRUCTOR_APPEND_ELT (v
, cm
->backend_decl
, val
);
6158 val
= gfc_conv_initializer (c
->expr
, &cm
->ts
,
6159 TREE_TYPE (cm
->backend_decl
),
6160 cm
->attr
.dimension
, cm
->attr
.pointer
,
6161 cm
->attr
.proc_pointer
);
6162 val
= unshare_expr_without_location (val
);
6164 /* Append it to the constructor list. */
6165 CONSTRUCTOR_APPEND_ELT (v
, cm
->backend_decl
, val
);
6168 se
->expr
= build_constructor (type
, v
);
6170 TREE_CONSTANT (se
->expr
) = 1;
6174 /* Translate a substring expression. */
6177 gfc_conv_substring_expr (gfc_se
* se
, gfc_expr
* expr
)
6183 gcc_assert (ref
== NULL
|| ref
->type
== REF_SUBSTRING
);
6185 se
->expr
= gfc_build_wide_string_const (expr
->ts
.kind
,
6186 expr
->value
.character
.length
,
6187 expr
->value
.character
.string
);
6189 se
->string_length
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se
->expr
)));
6190 TYPE_STRING_FLAG (TREE_TYPE (se
->expr
)) = 1;
6193 gfc_conv_substring (se
, ref
, expr
->ts
.kind
, NULL
, &expr
->where
);
6197 /* Entry point for expression translation. Evaluates a scalar quantity.
6198 EXPR is the expression to be translated, and SE is the state structure if
6199 called from within the scalarized. */
6202 gfc_conv_expr (gfc_se
* se
, gfc_expr
* expr
)
6207 if (ss
&& ss
->info
->expr
== expr
6208 && (ss
->info
->type
== GFC_SS_SCALAR
6209 || ss
->info
->type
== GFC_SS_REFERENCE
))
6211 gfc_ss_info
*ss_info
;
6214 /* Substitute a scalar expression evaluated outside the scalarization
6216 se
->expr
= ss_info
->data
.scalar
.value
;
6217 /* If the reference can be NULL, the value field contains the reference,
6218 not the value the reference points to (see gfc_add_loop_ss_code). */
6219 if (ss_info
->can_be_null_ref
)
6220 se
->expr
= build_fold_indirect_ref_loc (input_location
, se
->expr
);
6222 se
->string_length
= ss_info
->string_length
;
6223 gfc_advance_se_ss_chain (se
);
6227 /* We need to convert the expressions for the iso_c_binding derived types.
6228 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
6229 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
6230 typespec for the C_PTR and C_FUNPTR symbols, which has already been
6231 updated to be an integer with a kind equal to the size of a (void *). */
6232 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
->ts
.f90_type
== BT_VOID
)
6234 if (expr
->expr_type
== EXPR_VARIABLE
6235 && (expr
->symtree
->n
.sym
->intmod_sym_id
== ISOCBINDING_NULL_PTR
6236 || expr
->symtree
->n
.sym
->intmod_sym_id
6237 == ISOCBINDING_NULL_FUNPTR
))
6239 /* Set expr_type to EXPR_NULL, which will result in
6240 null_pointer_node being used below. */
6241 expr
->expr_type
= EXPR_NULL
;
6245 /* Update the type/kind of the expression to be what the new
6246 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
6247 expr
->ts
.type
= BT_INTEGER
;
6248 expr
->ts
.f90_type
= BT_VOID
;
6249 expr
->ts
.kind
= gfc_index_integer_kind
;
6253 gfc_fix_class_refs (expr
);
6255 switch (expr
->expr_type
)
6258 gfc_conv_expr_op (se
, expr
);
6262 gfc_conv_function_expr (se
, expr
);
6266 gfc_conv_constant (se
, expr
);
6270 gfc_conv_variable (se
, expr
);
6274 se
->expr
= null_pointer_node
;
6277 case EXPR_SUBSTRING
:
6278 gfc_conv_substring_expr (se
, expr
);
6281 case EXPR_STRUCTURE
:
6282 gfc_conv_structure (se
, expr
, 0);
6286 gfc_conv_array_constructor_expr (se
, expr
);
6295 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
6296 of an assignment. */
6298 gfc_conv_expr_lhs (gfc_se
* se
, gfc_expr
* expr
)
6300 gfc_conv_expr (se
, expr
);
6301 /* All numeric lvalues should have empty post chains. If not we need to
6302 figure out a way of rewriting an lvalue so that it has no post chain. */
6303 gcc_assert (expr
->ts
.type
== BT_CHARACTER
|| !se
->post
.head
);
6306 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
6307 numeric expressions. Used for scalar values where inserting cleanup code
6310 gfc_conv_expr_val (gfc_se
* se
, gfc_expr
* expr
)
6314 gcc_assert (expr
->ts
.type
!= BT_CHARACTER
);
6315 gfc_conv_expr (se
, expr
);
6318 val
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
6319 gfc_add_modify (&se
->pre
, val
, se
->expr
);
6321 gfc_add_block_to_block (&se
->pre
, &se
->post
);
6325 /* Helper to translate an expression and convert it to a particular type. */
6327 gfc_conv_expr_type (gfc_se
* se
, gfc_expr
* expr
, tree type
)
6329 gfc_conv_expr_val (se
, expr
);
6330 se
->expr
= convert (type
, se
->expr
);
6334 /* Converts an expression so that it can be passed by reference. Scalar
6338 gfc_conv_expr_reference (gfc_se
* se
, gfc_expr
* expr
)
6344 if (ss
&& ss
->info
->expr
== expr
6345 && ss
->info
->type
== GFC_SS_REFERENCE
)
6347 /* Returns a reference to the scalar evaluated outside the loop
6349 gfc_conv_expr (se
, expr
);
6350 se
->expr
= gfc_build_addr_expr (NULL_TREE
, se
->expr
);
6354 if (expr
->ts
.type
== BT_CHARACTER
)
6356 gfc_conv_expr (se
, expr
);
6357 gfc_conv_string_parameter (se
);
6361 if (expr
->expr_type
== EXPR_VARIABLE
)
6363 se
->want_pointer
= 1;
6364 gfc_conv_expr (se
, expr
);
6367 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
6368 gfc_add_modify (&se
->pre
, var
, se
->expr
);
6369 gfc_add_block_to_block (&se
->pre
, &se
->post
);
6375 if (expr
->expr_type
== EXPR_FUNCTION
6376 && ((expr
->value
.function
.esym
6377 && expr
->value
.function
.esym
->result
->attr
.pointer
6378 && !expr
->value
.function
.esym
->result
->attr
.dimension
)
6379 || (!expr
->value
.function
.esym
&& !expr
->ref
6380 && expr
->symtree
->n
.sym
->attr
.pointer
6381 && !expr
->symtree
->n
.sym
->attr
.dimension
)))
6383 se
->want_pointer
= 1;
6384 gfc_conv_expr (se
, expr
);
6385 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
6386 gfc_add_modify (&se
->pre
, var
, se
->expr
);
6391 gfc_conv_expr (se
, expr
);
6393 /* Create a temporary var to hold the value. */
6394 if (TREE_CONSTANT (se
->expr
))
6396 tree tmp
= se
->expr
;
6397 STRIP_TYPE_NOPS (tmp
);
6398 var
= build_decl (input_location
,
6399 CONST_DECL
, NULL
, TREE_TYPE (tmp
));
6400 DECL_INITIAL (var
) = tmp
;
6401 TREE_STATIC (var
) = 1;
6406 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
6407 gfc_add_modify (&se
->pre
, var
, se
->expr
);
6409 gfc_add_block_to_block (&se
->pre
, &se
->post
);
6411 /* Take the address of that value. */
6412 se
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
6417 gfc_trans_pointer_assign (gfc_code
* code
)
6419 return gfc_trans_pointer_assignment (code
->expr1
, code
->expr2
);
6423 /* Generate code for a pointer assignment. */
6426 gfc_trans_pointer_assignment (gfc_expr
* expr1
, gfc_expr
* expr2
)
6428 gfc_expr
*expr1_vptr
= NULL
;
6438 gfc_start_block (&block
);
6440 gfc_init_se (&lse
, NULL
);
6442 /* Check whether the expression is a scalar or not; we cannot use
6443 expr1->rank as it can be nonzero for proc pointers. */
6444 ss
= gfc_walk_expr (expr1
);
6445 scalar
= ss
== gfc_ss_terminator
;
6447 gfc_free_ss_chain (ss
);
6449 if (expr1
->ts
.type
== BT_DERIVED
&& expr2
->ts
.type
== BT_CLASS
6450 && expr2
->expr_type
!= EXPR_FUNCTION
)
6452 gfc_add_data_component (expr2
);
6453 /* The following is required as gfc_add_data_component doesn't
6454 update ts.type if there is a tailing REF_ARRAY. */
6455 expr2
->ts
.type
= BT_DERIVED
;
6460 /* Scalar pointers. */
6461 lse
.want_pointer
= 1;
6462 gfc_conv_expr (&lse
, expr1
);
6463 gfc_init_se (&rse
, NULL
);
6464 rse
.want_pointer
= 1;
6465 gfc_conv_expr (&rse
, expr2
);
6467 if (expr1
->symtree
->n
.sym
->attr
.proc_pointer
6468 && expr1
->symtree
->n
.sym
->attr
.dummy
)
6469 lse
.expr
= build_fold_indirect_ref_loc (input_location
,
6472 if (expr2
->symtree
&& expr2
->symtree
->n
.sym
->attr
.proc_pointer
6473 && expr2
->symtree
->n
.sym
->attr
.dummy
)
6474 rse
.expr
= build_fold_indirect_ref_loc (input_location
,
6477 gfc_add_block_to_block (&block
, &lse
.pre
);
6478 gfc_add_block_to_block (&block
, &rse
.pre
);
6480 /* Check character lengths if character expression. The test is only
6481 really added if -fbounds-check is enabled. Exclude deferred
6482 character length lefthand sides. */
6483 if (expr1
->ts
.type
== BT_CHARACTER
&& expr2
->expr_type
!= EXPR_NULL
6484 && !expr1
->ts
.deferred
6485 && !expr1
->symtree
->n
.sym
->attr
.proc_pointer
6486 && !gfc_is_proc_ptr_comp (expr1
))
6488 gcc_assert (expr2
->ts
.type
== BT_CHARACTER
);
6489 gcc_assert (lse
.string_length
&& rse
.string_length
);
6490 gfc_trans_same_strlen_check ("pointer assignment", &expr1
->where
,
6491 lse
.string_length
, rse
.string_length
,
6495 /* The assignment to an deferred character length sets the string
6496 length to that of the rhs. */
6497 if (expr1
->ts
.deferred
)
6499 if (expr2
->expr_type
!= EXPR_NULL
&& lse
.string_length
!= NULL
)
6500 gfc_add_modify (&block
, lse
.string_length
, rse
.string_length
);
6501 else if (lse
.string_length
!= NULL
)
6502 gfc_add_modify (&block
, lse
.string_length
,
6503 build_int_cst (gfc_charlen_type_node
, 0));
6506 if (expr1
->ts
.type
== BT_DERIVED
&& expr2
->ts
.type
== BT_CLASS
)
6507 rse
.expr
= gfc_class_data_get (rse
.expr
);
6509 gfc_add_modify (&block
, lse
.expr
,
6510 fold_convert (TREE_TYPE (lse
.expr
), rse
.expr
));
6512 gfc_add_block_to_block (&block
, &rse
.post
);
6513 gfc_add_block_to_block (&block
, &lse
.post
);
6520 tree strlen_rhs
= NULL_TREE
;
6522 /* Array pointer. Find the last reference on the LHS and if it is an
6523 array section ref, we're dealing with bounds remapping. In this case,
6524 set it to AR_FULL so that gfc_conv_expr_descriptor does
6525 not see it and process the bounds remapping afterwards explicitly. */
6526 for (remap
= expr1
->ref
; remap
; remap
= remap
->next
)
6527 if (!remap
->next
&& remap
->type
== REF_ARRAY
6528 && remap
->u
.ar
.type
== AR_SECTION
)
6530 rank_remap
= (remap
&& remap
->u
.ar
.end
[0]);
6532 gfc_init_se (&lse
, NULL
);
6534 lse
.descriptor_only
= 1;
6535 if (expr2
->expr_type
== EXPR_FUNCTION
&& expr2
->ts
.type
== BT_CLASS
6536 && expr1
->ts
.type
== BT_CLASS
)
6537 expr1_vptr
= gfc_copy_expr (expr1
);
6538 gfc_conv_expr_descriptor (&lse
, expr1
);
6539 strlen_lhs
= lse
.string_length
;
6542 if (expr2
->expr_type
== EXPR_NULL
)
6544 /* Just set the data pointer to null. */
6545 gfc_conv_descriptor_data_set (&lse
.pre
, lse
.expr
, null_pointer_node
);
6547 else if (rank_remap
)
6549 /* If we are rank-remapping, just get the RHS's descriptor and
6550 process this later on. */
6551 gfc_init_se (&rse
, NULL
);
6552 rse
.direct_byref
= 1;
6553 rse
.byref_noassign
= 1;
6555 if (expr2
->expr_type
== EXPR_FUNCTION
&& expr2
->ts
.type
== BT_CLASS
)
6557 gfc_conv_function_expr (&rse
, expr2
);
6559 if (expr1
->ts
.type
!= BT_CLASS
)
6560 rse
.expr
= gfc_class_data_get (rse
.expr
);
6563 tmp
= gfc_create_var (TREE_TYPE (rse
.expr
), "ptrtemp");
6564 gfc_add_modify (&lse
.pre
, tmp
, rse
.expr
);
6566 gfc_add_vptr_component (expr1_vptr
);
6567 gfc_init_se (&rse
, NULL
);
6568 rse
.want_pointer
= 1;
6569 gfc_conv_expr (&rse
, expr1_vptr
);
6570 gfc_add_modify (&lse
.pre
, rse
.expr
,
6571 fold_convert (TREE_TYPE (rse
.expr
),
6572 gfc_class_vptr_get (tmp
)));
6573 rse
.expr
= gfc_class_data_get (tmp
);
6576 else if (expr2
->expr_type
== EXPR_FUNCTION
)
6578 tree bound
[GFC_MAX_DIMENSIONS
];
6581 for (i
= 0; i
< expr2
->rank
; i
++)
6582 bound
[i
] = NULL_TREE
;
6583 tmp
= gfc_typenode_for_spec (&expr2
->ts
);
6584 tmp
= gfc_get_array_type_bounds (tmp
, expr2
->rank
, 0,
6586 GFC_ARRAY_POINTER_CONT
, false);
6587 tmp
= gfc_create_var (tmp
, "ptrtemp");
6589 lse
.direct_byref
= 1;
6590 gfc_conv_expr_descriptor (&lse
, expr2
);
6591 strlen_rhs
= lse
.string_length
;
6596 gfc_conv_expr_descriptor (&rse
, expr2
);
6597 strlen_rhs
= rse
.string_length
;
6600 else if (expr2
->expr_type
== EXPR_VARIABLE
)
6602 /* Assign directly to the LHS's descriptor. */
6603 lse
.direct_byref
= 1;
6604 gfc_conv_expr_descriptor (&lse
, expr2
);
6605 strlen_rhs
= lse
.string_length
;
6607 /* If this is a subreference array pointer assignment, use the rhs
6608 descriptor element size for the lhs span. */
6609 if (expr1
->symtree
->n
.sym
->attr
.subref_array_pointer
)
6611 decl
= expr1
->symtree
->n
.sym
->backend_decl
;
6612 gfc_init_se (&rse
, NULL
);
6613 rse
.descriptor_only
= 1;
6614 gfc_conv_expr (&rse
, expr2
);
6615 tmp
= gfc_get_element_type (TREE_TYPE (rse
.expr
));
6616 tmp
= fold_convert (gfc_array_index_type
, size_in_bytes (tmp
));
6617 if (!INTEGER_CST_P (tmp
))
6618 gfc_add_block_to_block (&lse
.post
, &rse
.pre
);
6619 gfc_add_modify (&lse
.post
, GFC_DECL_SPAN(decl
), tmp
);
6622 else if (expr2
->expr_type
== EXPR_FUNCTION
&& expr2
->ts
.type
== BT_CLASS
)
6624 gfc_init_se (&rse
, NULL
);
6625 rse
.want_pointer
= 1;
6626 gfc_conv_function_expr (&rse
, expr2
);
6627 if (expr1
->ts
.type
!= BT_CLASS
)
6629 rse
.expr
= gfc_class_data_get (rse
.expr
);
6630 gfc_add_modify (&lse
.pre
, desc
, rse
.expr
);
6634 tmp
= gfc_create_var (TREE_TYPE (rse
.expr
), "ptrtemp");
6635 gfc_add_modify (&lse
.pre
, tmp
, rse
.expr
);
6637 gfc_add_vptr_component (expr1_vptr
);
6638 gfc_init_se (&rse
, NULL
);
6639 rse
.want_pointer
= 1;
6640 gfc_conv_expr (&rse
, expr1_vptr
);
6641 gfc_add_modify (&lse
.pre
, rse
.expr
,
6642 fold_convert (TREE_TYPE (rse
.expr
),
6643 gfc_class_vptr_get (tmp
)));
6644 rse
.expr
= gfc_class_data_get (tmp
);
6645 gfc_add_modify (&lse
.pre
, desc
, rse
.expr
);
6650 /* Assign to a temporary descriptor and then copy that
6651 temporary to the pointer. */
6652 tmp
= gfc_create_var (TREE_TYPE (desc
), "ptrtemp");
6654 lse
.direct_byref
= 1;
6655 gfc_conv_expr_descriptor (&lse
, expr2
);
6656 strlen_rhs
= lse
.string_length
;
6657 gfc_add_modify (&lse
.pre
, desc
, tmp
);
6661 gfc_free_expr (expr1_vptr
);
6663 gfc_add_block_to_block (&block
, &lse
.pre
);
6665 gfc_add_block_to_block (&block
, &rse
.pre
);
6667 /* If we do bounds remapping, update LHS descriptor accordingly. */
6671 gcc_assert (remap
->u
.ar
.dimen
== expr1
->rank
);
6675 /* Do rank remapping. We already have the RHS's descriptor
6676 converted in rse and now have to build the correct LHS
6677 descriptor for it. */
6681 tree lbound
, ubound
;
6684 dtype
= gfc_conv_descriptor_dtype (desc
);
6685 tmp
= gfc_get_dtype (TREE_TYPE (desc
));
6686 gfc_add_modify (&block
, dtype
, tmp
);
6688 /* Copy data pointer. */
6689 data
= gfc_conv_descriptor_data_get (rse
.expr
);
6690 gfc_conv_descriptor_data_set (&block
, desc
, data
);
6692 /* Copy offset but adjust it such that it would correspond
6693 to a lbound of zero. */
6694 offs
= gfc_conv_descriptor_offset_get (rse
.expr
);
6695 for (dim
= 0; dim
< expr2
->rank
; ++dim
)
6697 stride
= gfc_conv_descriptor_stride_get (rse
.expr
,
6699 lbound
= gfc_conv_descriptor_lbound_get (rse
.expr
,
6701 tmp
= fold_build2_loc (input_location
, MULT_EXPR
,
6702 gfc_array_index_type
, stride
, lbound
);
6703 offs
= fold_build2_loc (input_location
, PLUS_EXPR
,
6704 gfc_array_index_type
, offs
, tmp
);
6706 gfc_conv_descriptor_offset_set (&block
, desc
, offs
);
6708 /* Set the bounds as declared for the LHS and calculate strides as
6709 well as another offset update accordingly. */
6710 stride
= gfc_conv_descriptor_stride_get (rse
.expr
,
6712 for (dim
= 0; dim
< expr1
->rank
; ++dim
)
6717 gcc_assert (remap
->u
.ar
.start
[dim
] && remap
->u
.ar
.end
[dim
]);
6719 /* Convert declared bounds. */
6720 gfc_init_se (&lower_se
, NULL
);
6721 gfc_init_se (&upper_se
, NULL
);
6722 gfc_conv_expr (&lower_se
, remap
->u
.ar
.start
[dim
]);
6723 gfc_conv_expr (&upper_se
, remap
->u
.ar
.end
[dim
]);
6725 gfc_add_block_to_block (&block
, &lower_se
.pre
);
6726 gfc_add_block_to_block (&block
, &upper_se
.pre
);
6728 lbound
= fold_convert (gfc_array_index_type
, lower_se
.expr
);
6729 ubound
= fold_convert (gfc_array_index_type
, upper_se
.expr
);
6731 lbound
= gfc_evaluate_now (lbound
, &block
);
6732 ubound
= gfc_evaluate_now (ubound
, &block
);
6734 gfc_add_block_to_block (&block
, &lower_se
.post
);
6735 gfc_add_block_to_block (&block
, &upper_se
.post
);
6737 /* Set bounds in descriptor. */
6738 gfc_conv_descriptor_lbound_set (&block
, desc
,
6739 gfc_rank_cst
[dim
], lbound
);
6740 gfc_conv_descriptor_ubound_set (&block
, desc
,
6741 gfc_rank_cst
[dim
], ubound
);
6744 stride
= gfc_evaluate_now (stride
, &block
);
6745 gfc_conv_descriptor_stride_set (&block
, desc
,
6746 gfc_rank_cst
[dim
], stride
);
6748 /* Update offset. */
6749 offs
= gfc_conv_descriptor_offset_get (desc
);
6750 tmp
= fold_build2_loc (input_location
, MULT_EXPR
,
6751 gfc_array_index_type
, lbound
, stride
);
6752 offs
= fold_build2_loc (input_location
, MINUS_EXPR
,
6753 gfc_array_index_type
, offs
, tmp
);
6754 offs
= gfc_evaluate_now (offs
, &block
);
6755 gfc_conv_descriptor_offset_set (&block
, desc
, offs
);
6757 /* Update stride. */
6758 tmp
= gfc_conv_array_extent_dim (lbound
, ubound
, NULL
);
6759 stride
= fold_build2_loc (input_location
, MULT_EXPR
,
6760 gfc_array_index_type
, stride
, tmp
);
6765 /* Bounds remapping. Just shift the lower bounds. */
6767 gcc_assert (expr1
->rank
== expr2
->rank
);
6769 for (dim
= 0; dim
< remap
->u
.ar
.dimen
; ++dim
)
6773 gcc_assert (remap
->u
.ar
.start
[dim
]);
6774 gcc_assert (!remap
->u
.ar
.end
[dim
]);
6775 gfc_init_se (&lbound_se
, NULL
);
6776 gfc_conv_expr (&lbound_se
, remap
->u
.ar
.start
[dim
]);
6778 gfc_add_block_to_block (&block
, &lbound_se
.pre
);
6779 gfc_conv_shift_descriptor_lbound (&block
, desc
,
6780 dim
, lbound_se
.expr
);
6781 gfc_add_block_to_block (&block
, &lbound_se
.post
);
6786 /* Check string lengths if applicable. The check is only really added
6787 to the output code if -fbounds-check is enabled. */
6788 if (expr1
->ts
.type
== BT_CHARACTER
&& expr2
->expr_type
!= EXPR_NULL
)
6790 gcc_assert (expr2
->ts
.type
== BT_CHARACTER
);
6791 gcc_assert (strlen_lhs
&& strlen_rhs
);
6792 gfc_trans_same_strlen_check ("pointer assignment", &expr1
->where
,
6793 strlen_lhs
, strlen_rhs
, &block
);
6796 /* If rank remapping was done, check with -fcheck=bounds that
6797 the target is at least as large as the pointer. */
6798 if (rank_remap
&& (gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
))
6804 lsize
= gfc_conv_descriptor_size (lse
.expr
, expr1
->rank
);
6805 rsize
= gfc_conv_descriptor_size (rse
.expr
, expr2
->rank
);
6807 lsize
= gfc_evaluate_now (lsize
, &block
);
6808 rsize
= gfc_evaluate_now (rsize
, &block
);
6809 fault
= fold_build2_loc (input_location
, LT_EXPR
, boolean_type_node
,
6812 msg
= _("Target of rank remapping is too small (%ld < %ld)");
6813 gfc_trans_runtime_check (true, false, fault
, &block
, &expr2
->where
,
6817 gfc_add_block_to_block (&block
, &lse
.post
);
6819 gfc_add_block_to_block (&block
, &rse
.post
);
6822 return gfc_finish_block (&block
);
6826 /* Makes sure se is suitable for passing as a function string parameter. */
6827 /* TODO: Need to check all callers of this function. It may be abused. */
6830 gfc_conv_string_parameter (gfc_se
* se
)
6834 if (TREE_CODE (se
->expr
) == STRING_CST
)
6836 type
= TREE_TYPE (TREE_TYPE (se
->expr
));
6837 se
->expr
= gfc_build_addr_expr (build_pointer_type (type
), se
->expr
);
6841 if (TYPE_STRING_FLAG (TREE_TYPE (se
->expr
)))
6843 if (TREE_CODE (se
->expr
) != INDIRECT_REF
)
6845 type
= TREE_TYPE (se
->expr
);
6846 se
->expr
= gfc_build_addr_expr (build_pointer_type (type
), se
->expr
);
6850 type
= gfc_get_character_type_len (gfc_default_character_kind
,
6852 type
= build_pointer_type (type
);
6853 se
->expr
= gfc_build_addr_expr (type
, se
->expr
);
6857 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se
->expr
)));
6861 /* Generate code for assignment of scalar variables. Includes character
6862 strings and derived types with allocatable components.
6863 If you know that the LHS has no allocations, set dealloc to false.
6865 DEEP_COPY has no effect if the typespec TS is not a derived type with
6866 allocatable components. Otherwise, if it is set, an explicit copy of each
6867 allocatable component is made. This is necessary as a simple copy of the
6868 whole object would copy array descriptors as is, so that the lhs's
6869 allocatable components would point to the rhs's after the assignment.
6870 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
6871 necessary if the rhs is a non-pointer function, as the allocatable components
6872 are not accessible by other means than the function's result after the
6873 function has returned. It is even more subtle when temporaries are involved,
6874 as the two following examples show:
6875 1. When we evaluate an array constructor, a temporary is created. Thus
6876 there is theoretically no alias possible. However, no deep copy is
6877 made for this temporary, so that if the constructor is made of one or
6878 more variable with allocatable components, those components still point
6879 to the variable's: DEEP_COPY should be set for the assignment from the
6880 temporary to the lhs in that case.
6881 2. When assigning a scalar to an array, we evaluate the scalar value out
6882 of the loop, store it into a temporary variable, and assign from that.
6883 In that case, deep copying when assigning to the temporary would be a
6884 waste of resources; however deep copies should happen when assigning from
6885 the temporary to each array element: again DEEP_COPY should be set for
6886 the assignment from the temporary to the lhs. */
6889 gfc_trans_scalar_assign (gfc_se
* lse
, gfc_se
* rse
, gfc_typespec ts
,
6890 bool l_is_temp
, bool deep_copy
, bool dealloc
)
6896 gfc_init_block (&block
);
6898 if (ts
.type
== BT_CHARACTER
)
6903 if (lse
->string_length
!= NULL_TREE
)
6905 gfc_conv_string_parameter (lse
);
6906 gfc_add_block_to_block (&block
, &lse
->pre
);
6907 llen
= lse
->string_length
;
6910 if (rse
->string_length
!= NULL_TREE
)
6912 gcc_assert (rse
->string_length
!= NULL_TREE
);
6913 gfc_conv_string_parameter (rse
);
6914 gfc_add_block_to_block (&block
, &rse
->pre
);
6915 rlen
= rse
->string_length
;
6918 gfc_trans_string_copy (&block
, llen
, lse
->expr
, ts
.kind
, rlen
,
6919 rse
->expr
, ts
.kind
);
6921 else if (ts
.type
== BT_DERIVED
&& ts
.u
.derived
->attr
.alloc_comp
)
6923 tree tmp_var
= NULL_TREE
;
6926 /* Are the rhs and the lhs the same? */
6929 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
6930 gfc_build_addr_expr (NULL_TREE
, lse
->expr
),
6931 gfc_build_addr_expr (NULL_TREE
, rse
->expr
));
6932 cond
= gfc_evaluate_now (cond
, &lse
->pre
);
6935 /* Deallocate the lhs allocated components as long as it is not
6936 the same as the rhs. This must be done following the assignment
6937 to prevent deallocating data that could be used in the rhs
6939 if (!l_is_temp
&& dealloc
)
6941 tmp_var
= gfc_evaluate_now (lse
->expr
, &lse
->pre
);
6942 tmp
= gfc_deallocate_alloc_comp_no_caf (ts
.u
.derived
, tmp_var
, 0);
6944 tmp
= build3_v (COND_EXPR
, cond
, build_empty_stmt (input_location
),
6946 gfc_add_expr_to_block (&lse
->post
, tmp
);
6949 gfc_add_block_to_block (&block
, &rse
->pre
);
6950 gfc_add_block_to_block (&block
, &lse
->pre
);
6952 gfc_add_modify (&block
, lse
->expr
,
6953 fold_convert (TREE_TYPE (lse
->expr
), rse
->expr
));
6955 /* Restore pointer address of coarray components. */
6956 if (ts
.u
.derived
->attr
.coarray_comp
&& deep_copy
&& tmp_var
!= NULL_TREE
)
6958 tmp
= gfc_reassign_alloc_comp_caf (ts
.u
.derived
, tmp_var
, lse
->expr
);
6959 tmp
= build3_v (COND_EXPR
, cond
, build_empty_stmt (input_location
),
6961 gfc_add_expr_to_block (&block
, tmp
);
6964 /* Do a deep copy if the rhs is a variable, if it is not the
6968 tmp
= gfc_copy_alloc_comp (ts
.u
.derived
, rse
->expr
, lse
->expr
, 0);
6969 tmp
= build3_v (COND_EXPR
, cond
, build_empty_stmt (input_location
),
6971 gfc_add_expr_to_block (&block
, tmp
);
6974 else if (ts
.type
== BT_DERIVED
|| ts
.type
== BT_CLASS
)
6976 gfc_add_block_to_block (&block
, &lse
->pre
);
6977 gfc_add_block_to_block (&block
, &rse
->pre
);
6978 tmp
= fold_build1_loc (input_location
, VIEW_CONVERT_EXPR
,
6979 TREE_TYPE (lse
->expr
), rse
->expr
);
6980 gfc_add_modify (&block
, lse
->expr
, tmp
);
6984 gfc_add_block_to_block (&block
, &lse
->pre
);
6985 gfc_add_block_to_block (&block
, &rse
->pre
);
6987 gfc_add_modify (&block
, lse
->expr
,
6988 fold_convert (TREE_TYPE (lse
->expr
), rse
->expr
));
6991 gfc_add_block_to_block (&block
, &lse
->post
);
6992 gfc_add_block_to_block (&block
, &rse
->post
);
6994 return gfc_finish_block (&block
);
6998 /* There are quite a lot of restrictions on the optimisation in using an
6999 array function assign without a temporary. */
7002 arrayfunc_assign_needs_temporary (gfc_expr
* expr1
, gfc_expr
* expr2
)
7005 bool seen_array_ref
;
7007 gfc_symbol
*sym
= expr1
->symtree
->n
.sym
;
7009 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
7010 if (expr2
->value
.function
.isym
&& !gfc_is_intrinsic_libcall (expr2
))
7013 /* Elemental functions are scalarized so that they don't need a
7014 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
7015 they would need special treatment in gfc_trans_arrayfunc_assign. */
7016 if (expr2
->value
.function
.esym
!= NULL
7017 && expr2
->value
.function
.esym
->attr
.elemental
)
7020 /* Need a temporary if rhs is not FULL or a contiguous section. */
7021 if (expr1
->ref
&& !(gfc_full_array_ref_p (expr1
->ref
, &c
) || c
))
7024 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
7025 if (gfc_ref_needs_temporary_p (expr1
->ref
))
7028 /* Functions returning pointers or allocatables need temporaries. */
7029 c
= expr2
->value
.function
.esym
7030 ? (expr2
->value
.function
.esym
->attr
.pointer
7031 || expr2
->value
.function
.esym
->attr
.allocatable
)
7032 : (expr2
->symtree
->n
.sym
->attr
.pointer
7033 || expr2
->symtree
->n
.sym
->attr
.allocatable
);
7037 /* Character array functions need temporaries unless the
7038 character lengths are the same. */
7039 if (expr2
->ts
.type
== BT_CHARACTER
&& expr2
->rank
> 0)
7041 if (expr1
->ts
.u
.cl
->length
== NULL
7042 || expr1
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
7045 if (expr2
->ts
.u
.cl
->length
== NULL
7046 || expr2
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
7049 if (mpz_cmp (expr1
->ts
.u
.cl
->length
->value
.integer
,
7050 expr2
->ts
.u
.cl
->length
->value
.integer
) != 0)
7054 /* Check that no LHS component references appear during an array
7055 reference. This is needed because we do not have the means to
7056 span any arbitrary stride with an array descriptor. This check
7057 is not needed for the rhs because the function result has to be
7059 seen_array_ref
= false;
7060 for (ref
= expr1
->ref
; ref
; ref
= ref
->next
)
7062 if (ref
->type
== REF_ARRAY
)
7063 seen_array_ref
= true;
7064 else if (ref
->type
== REF_COMPONENT
&& seen_array_ref
)
7068 /* Check for a dependency. */
7069 if (gfc_check_fncall_dependency (expr1
, INTENT_OUT
,
7070 expr2
->value
.function
.esym
,
7071 expr2
->value
.function
.actual
,
7075 /* If we have reached here with an intrinsic function, we do not
7076 need a temporary except in the particular case that reallocation
7077 on assignment is active and the lhs is allocatable and a target. */
7078 if (expr2
->value
.function
.isym
)
7079 return (gfc_option
.flag_realloc_lhs
7080 && sym
->attr
.allocatable
7081 && sym
->attr
.target
);
7083 /* If the LHS is a dummy, we need a temporary if it is not
7085 if (sym
->attr
.dummy
&& sym
->attr
.intent
!= INTENT_OUT
)
7088 /* If the lhs has been host_associated, is in common, a pointer or is
7089 a target and the function is not using a RESULT variable, aliasing
7090 can occur and a temporary is needed. */
7091 if ((sym
->attr
.host_assoc
7092 || sym
->attr
.in_common
7093 || sym
->attr
.pointer
7094 || sym
->attr
.cray_pointee
7095 || sym
->attr
.target
)
7096 && expr2
->symtree
!= NULL
7097 && expr2
->symtree
->n
.sym
== expr2
->symtree
->n
.sym
->result
)
7100 /* A PURE function can unconditionally be called without a temporary. */
7101 if (expr2
->value
.function
.esym
!= NULL
7102 && expr2
->value
.function
.esym
->attr
.pure
)
7105 /* Implicit_pure functions are those which could legally be declared
7107 if (expr2
->value
.function
.esym
!= NULL
7108 && expr2
->value
.function
.esym
->attr
.implicit_pure
)
7111 if (!sym
->attr
.use_assoc
7112 && !sym
->attr
.in_common
7113 && !sym
->attr
.pointer
7114 && !sym
->attr
.target
7115 && !sym
->attr
.cray_pointee
7116 && expr2
->value
.function
.esym
)
7118 /* A temporary is not needed if the function is not contained and
7119 the variable is local or host associated and not a pointer or
7121 if (!expr2
->value
.function
.esym
->attr
.contained
)
7124 /* A temporary is not needed if the lhs has never been host
7125 associated and the procedure is contained. */
7126 else if (!sym
->attr
.host_assoc
)
7129 /* A temporary is not needed if the variable is local and not
7130 a pointer, a target or a result. */
7132 && expr2
->value
.function
.esym
->ns
== sym
->ns
->parent
)
7136 /* Default to temporary use. */
7141 /* Provide the loop info so that the lhs descriptor can be built for
7142 reallocatable assignments from extrinsic function calls. */
7145 realloc_lhs_loop_for_fcn_call (gfc_se
*se
, locus
*where
, gfc_ss
**ss
,
7148 /* Signal that the function call should not be made by
7149 gfc_conv_loop_setup. */
7150 se
->ss
->is_alloc_lhs
= 1;
7151 gfc_init_loopinfo (loop
);
7152 gfc_add_ss_to_loop (loop
, *ss
);
7153 gfc_add_ss_to_loop (loop
, se
->ss
);
7154 gfc_conv_ss_startstride (loop
);
7155 gfc_conv_loop_setup (loop
, where
);
7156 gfc_copy_loopinfo_to_se (se
, loop
);
7157 gfc_add_block_to_block (&se
->pre
, &loop
->pre
);
7158 gfc_add_block_to_block (&se
->pre
, &loop
->post
);
7159 se
->ss
->is_alloc_lhs
= 0;
7163 /* For assignment to a reallocatable lhs from intrinsic functions,
7164 replace the se.expr (ie. the result) with a temporary descriptor.
7165 Null the data field so that the library allocates space for the
7166 result. Free the data of the original descriptor after the function,
7167 in case it appears in an argument expression and transfer the
7168 result to the original descriptor. */
7171 fcncall_realloc_result (gfc_se
*se
, int rank
)
7180 /* Use the allocation done by the library. Substitute the lhs
7181 descriptor with a copy, whose data field is nulled.*/
7182 desc
= build_fold_indirect_ref_loc (input_location
, se
->expr
);
7183 if (POINTER_TYPE_P (TREE_TYPE (desc
)))
7184 desc
= build_fold_indirect_ref_loc (input_location
, desc
);
7186 /* Unallocated, the descriptor does not have a dtype. */
7187 tmp
= gfc_conv_descriptor_dtype (desc
);
7188 gfc_add_modify (&se
->pre
, tmp
, gfc_get_dtype (TREE_TYPE (desc
)));
7190 res_desc
= gfc_evaluate_now (desc
, &se
->pre
);
7191 gfc_conv_descriptor_data_set (&se
->pre
, res_desc
, null_pointer_node
);
7192 se
->expr
= gfc_build_addr_expr (TREE_TYPE (se
->expr
), res_desc
);
7194 /* Free the lhs after the function call and copy the result data to
7195 the lhs descriptor. */
7196 tmp
= gfc_conv_descriptor_data_get (desc
);
7197 zero_cond
= fold_build2_loc (input_location
, EQ_EXPR
,
7198 boolean_type_node
, tmp
,
7199 build_int_cst (TREE_TYPE (tmp
), 0));
7200 zero_cond
= gfc_evaluate_now (zero_cond
, &se
->post
);
7201 tmp
= gfc_call_free (fold_convert (pvoid_type_node
, tmp
));
7202 gfc_add_expr_to_block (&se
->post
, tmp
);
7204 tmp
= gfc_conv_descriptor_data_get (res_desc
);
7205 gfc_conv_descriptor_data_set (&se
->post
, desc
, tmp
);
7207 /* Check that the shapes are the same between lhs and expression. */
7208 for (n
= 0 ; n
< rank
; n
++)
7211 tmp
= gfc_conv_descriptor_lbound_get (desc
, gfc_rank_cst
[n
]);
7212 tmp1
= gfc_conv_descriptor_lbound_get (res_desc
, gfc_rank_cst
[n
]);
7213 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
7214 gfc_array_index_type
, tmp
, tmp1
);
7215 tmp1
= gfc_conv_descriptor_ubound_get (desc
, gfc_rank_cst
[n
]);
7216 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
7217 gfc_array_index_type
, tmp
, tmp1
);
7218 tmp1
= gfc_conv_descriptor_ubound_get (res_desc
, gfc_rank_cst
[n
]);
7219 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
7220 gfc_array_index_type
, tmp
, tmp1
);
7221 tmp
= fold_build2_loc (input_location
, NE_EXPR
,
7222 boolean_type_node
, tmp
,
7223 gfc_index_zero_node
);
7224 tmp
= gfc_evaluate_now (tmp
, &se
->post
);
7225 zero_cond
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
7226 boolean_type_node
, tmp
,
7230 /* 'zero_cond' being true is equal to lhs not being allocated or the
7231 shapes being different. */
7232 zero_cond
= gfc_evaluate_now (zero_cond
, &se
->post
);
7234 /* Now reset the bounds returned from the function call to bounds based
7235 on the lhs lbounds, except where the lhs is not allocated or the shapes
7236 of 'variable and 'expr' are different. Set the offset accordingly. */
7237 offset
= gfc_index_zero_node
;
7238 for (n
= 0 ; n
< rank
; n
++)
7242 lbound
= gfc_conv_descriptor_lbound_get (desc
, gfc_rank_cst
[n
]);
7243 lbound
= fold_build3_loc (input_location
, COND_EXPR
,
7244 gfc_array_index_type
, zero_cond
,
7245 gfc_index_one_node
, lbound
);
7246 lbound
= gfc_evaluate_now (lbound
, &se
->post
);
7248 tmp
= gfc_conv_descriptor_ubound_get (res_desc
, gfc_rank_cst
[n
]);
7249 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
7250 gfc_array_index_type
, tmp
, lbound
);
7251 gfc_conv_descriptor_lbound_set (&se
->post
, desc
,
7252 gfc_rank_cst
[n
], lbound
);
7253 gfc_conv_descriptor_ubound_set (&se
->post
, desc
,
7254 gfc_rank_cst
[n
], tmp
);
7256 /* Set stride and accumulate the offset. */
7257 tmp
= gfc_conv_descriptor_stride_get (res_desc
, gfc_rank_cst
[n
]);
7258 gfc_conv_descriptor_stride_set (&se
->post
, desc
,
7259 gfc_rank_cst
[n
], tmp
);
7260 tmp
= fold_build2_loc (input_location
, MULT_EXPR
,
7261 gfc_array_index_type
, lbound
, tmp
);
7262 offset
= fold_build2_loc (input_location
, MINUS_EXPR
,
7263 gfc_array_index_type
, offset
, tmp
);
7264 offset
= gfc_evaluate_now (offset
, &se
->post
);
7267 gfc_conv_descriptor_offset_set (&se
->post
, desc
, offset
);
7272 /* Try to translate array(:) = func (...), where func is a transformational
7273 array function, without using a temporary. Returns NULL if this isn't the
7277 gfc_trans_arrayfunc_assign (gfc_expr
* expr1
, gfc_expr
* expr2
)
7281 gfc_component
*comp
= NULL
;
7284 if (arrayfunc_assign_needs_temporary (expr1
, expr2
))
7287 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
7289 comp
= gfc_get_proc_ptr_comp (expr2
);
7290 gcc_assert (expr2
->value
.function
.isym
7291 || (comp
&& comp
->attr
.dimension
)
7292 || (!comp
&& gfc_return_by_reference (expr2
->value
.function
.esym
)
7293 && expr2
->value
.function
.esym
->result
->attr
.dimension
));
7295 gfc_init_se (&se
, NULL
);
7296 gfc_start_block (&se
.pre
);
7297 se
.want_pointer
= 1;
7299 gfc_conv_array_parameter (&se
, expr1
, false, NULL
, NULL
, NULL
);
7301 if (expr1
->ts
.type
== BT_DERIVED
7302 && expr1
->ts
.u
.derived
->attr
.alloc_comp
)
7305 tmp
= gfc_deallocate_alloc_comp_no_caf (expr1
->ts
.u
.derived
, se
.expr
,
7307 gfc_add_expr_to_block (&se
.pre
, tmp
);
7310 se
.direct_byref
= 1;
7311 se
.ss
= gfc_walk_expr (expr2
);
7312 gcc_assert (se
.ss
!= gfc_ss_terminator
);
7314 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
7315 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
7316 Clearly, this cannot be done for an allocatable function result, since
7317 the shape of the result is unknown and, in any case, the function must
7318 correctly take care of the reallocation internally. For intrinsic
7319 calls, the array data is freed and the library takes care of allocation.
7320 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
7322 if (gfc_option
.flag_realloc_lhs
7323 && gfc_is_reallocatable_lhs (expr1
)
7324 && !gfc_expr_attr (expr1
).codimension
7325 && !gfc_is_coindexed (expr1
)
7326 && !(expr2
->value
.function
.esym
7327 && expr2
->value
.function
.esym
->result
->attr
.allocatable
))
7329 realloc_lhs_warning (expr1
->ts
.type
, true, &expr1
->where
);
7331 if (!expr2
->value
.function
.isym
)
7333 ss
= gfc_walk_expr (expr1
);
7334 gcc_assert (ss
!= gfc_ss_terminator
);
7336 realloc_lhs_loop_for_fcn_call (&se
, &expr1
->where
, &ss
, &loop
);
7337 ss
->is_alloc_lhs
= 1;
7340 fcncall_realloc_result (&se
, expr1
->rank
);
7343 gfc_conv_function_expr (&se
, expr2
);
7344 gfc_add_block_to_block (&se
.pre
, &se
.post
);
7347 gfc_cleanup_loop (&loop
);
7349 gfc_free_ss_chain (se
.ss
);
7351 return gfc_finish_block (&se
.pre
);
7355 /* Try to efficiently translate array(:) = 0. Return NULL if this
7359 gfc_trans_zero_assign (gfc_expr
* expr
)
7361 tree dest
, len
, type
;
7365 sym
= expr
->symtree
->n
.sym
;
7366 dest
= gfc_get_symbol_decl (sym
);
7368 type
= TREE_TYPE (dest
);
7369 if (POINTER_TYPE_P (type
))
7370 type
= TREE_TYPE (type
);
7371 if (!GFC_ARRAY_TYPE_P (type
))
7374 /* Determine the length of the array. */
7375 len
= GFC_TYPE_ARRAY_SIZE (type
);
7376 if (!len
|| TREE_CODE (len
) != INTEGER_CST
)
7379 tmp
= TYPE_SIZE_UNIT (gfc_get_element_type (type
));
7380 len
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
, len
,
7381 fold_convert (gfc_array_index_type
, tmp
));
7383 /* If we are zeroing a local array avoid taking its address by emitting
7385 if (!POINTER_TYPE_P (TREE_TYPE (dest
)))
7386 return build2_loc (input_location
, MODIFY_EXPR
, void_type_node
,
7387 dest
, build_constructor (TREE_TYPE (dest
),
7390 /* Convert arguments to the correct types. */
7391 dest
= fold_convert (pvoid_type_node
, dest
);
7392 len
= fold_convert (size_type_node
, len
);
7394 /* Construct call to __builtin_memset. */
7395 tmp
= build_call_expr_loc (input_location
,
7396 builtin_decl_explicit (BUILT_IN_MEMSET
),
7397 3, dest
, integer_zero_node
, len
);
7398 return fold_convert (void_type_node
, tmp
);
7402 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
7403 that constructs the call to __builtin_memcpy. */
7406 gfc_build_memcpy_call (tree dst
, tree src
, tree len
)
7410 /* Convert arguments to the correct types. */
7411 if (!POINTER_TYPE_P (TREE_TYPE (dst
)))
7412 dst
= gfc_build_addr_expr (pvoid_type_node
, dst
);
7414 dst
= fold_convert (pvoid_type_node
, dst
);
7416 if (!POINTER_TYPE_P (TREE_TYPE (src
)))
7417 src
= gfc_build_addr_expr (pvoid_type_node
, src
);
7419 src
= fold_convert (pvoid_type_node
, src
);
7421 len
= fold_convert (size_type_node
, len
);
7423 /* Construct call to __builtin_memcpy. */
7424 tmp
= build_call_expr_loc (input_location
,
7425 builtin_decl_explicit (BUILT_IN_MEMCPY
),
7427 return fold_convert (void_type_node
, tmp
);
7431 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
7432 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
7433 source/rhs, both are gfc_full_array_ref_p which have been checked for
7437 gfc_trans_array_copy (gfc_expr
* expr1
, gfc_expr
* expr2
)
7439 tree dst
, dlen
, dtype
;
7440 tree src
, slen
, stype
;
7443 dst
= gfc_get_symbol_decl (expr1
->symtree
->n
.sym
);
7444 src
= gfc_get_symbol_decl (expr2
->symtree
->n
.sym
);
7446 dtype
= TREE_TYPE (dst
);
7447 if (POINTER_TYPE_P (dtype
))
7448 dtype
= TREE_TYPE (dtype
);
7449 stype
= TREE_TYPE (src
);
7450 if (POINTER_TYPE_P (stype
))
7451 stype
= TREE_TYPE (stype
);
7453 if (!GFC_ARRAY_TYPE_P (dtype
) || !GFC_ARRAY_TYPE_P (stype
))
7456 /* Determine the lengths of the arrays. */
7457 dlen
= GFC_TYPE_ARRAY_SIZE (dtype
);
7458 if (!dlen
|| TREE_CODE (dlen
) != INTEGER_CST
)
7460 tmp
= TYPE_SIZE_UNIT (gfc_get_element_type (dtype
));
7461 dlen
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
7462 dlen
, fold_convert (gfc_array_index_type
, tmp
));
7464 slen
= GFC_TYPE_ARRAY_SIZE (stype
);
7465 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
7467 tmp
= TYPE_SIZE_UNIT (gfc_get_element_type (stype
));
7468 slen
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
7469 slen
, fold_convert (gfc_array_index_type
, tmp
));
7471 /* Sanity check that they are the same. This should always be
7472 the case, as we should already have checked for conformance. */
7473 if (!tree_int_cst_equal (slen
, dlen
))
7476 return gfc_build_memcpy_call (dst
, src
, dlen
);
7480 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
7481 this can't be done. EXPR1 is the destination/lhs for which
7482 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
7485 gfc_trans_array_constructor_copy (gfc_expr
* expr1
, gfc_expr
* expr2
)
7487 unsigned HOST_WIDE_INT nelem
;
7493 nelem
= gfc_constant_array_constructor_p (expr2
->value
.constructor
);
7497 dst
= gfc_get_symbol_decl (expr1
->symtree
->n
.sym
);
7498 dtype
= TREE_TYPE (dst
);
7499 if (POINTER_TYPE_P (dtype
))
7500 dtype
= TREE_TYPE (dtype
);
7501 if (!GFC_ARRAY_TYPE_P (dtype
))
7504 /* Determine the lengths of the array. */
7505 len
= GFC_TYPE_ARRAY_SIZE (dtype
);
7506 if (!len
|| TREE_CODE (len
) != INTEGER_CST
)
7509 /* Confirm that the constructor is the same size. */
7510 if (compare_tree_int (len
, nelem
) != 0)
7513 tmp
= TYPE_SIZE_UNIT (gfc_get_element_type (dtype
));
7514 len
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
, len
,
7515 fold_convert (gfc_array_index_type
, tmp
));
7517 stype
= gfc_typenode_for_spec (&expr2
->ts
);
7518 src
= gfc_build_constant_array_constructor (expr2
, stype
);
7520 stype
= TREE_TYPE (src
);
7521 if (POINTER_TYPE_P (stype
))
7522 stype
= TREE_TYPE (stype
);
7524 return gfc_build_memcpy_call (dst
, src
, len
);
7528 /* Tells whether the expression is to be treated as a variable reference. */
7531 expr_is_variable (gfc_expr
*expr
)
7534 gfc_component
*comp
;
7535 gfc_symbol
*func_ifc
;
7537 if (expr
->expr_type
== EXPR_VARIABLE
)
7540 arg
= gfc_get_noncopying_intrinsic_argument (expr
);
7543 gcc_assert (expr
->value
.function
.isym
->id
== GFC_ISYM_TRANSPOSE
);
7544 return expr_is_variable (arg
);
7547 /* A data-pointer-returning function should be considered as a variable
7549 if (expr
->expr_type
== EXPR_FUNCTION
7550 && expr
->ref
== NULL
)
7552 if (expr
->value
.function
.isym
!= NULL
)
7555 if (expr
->value
.function
.esym
!= NULL
)
7557 func_ifc
= expr
->value
.function
.esym
;
7562 gcc_assert (expr
->symtree
);
7563 func_ifc
= expr
->symtree
->n
.sym
;
7570 comp
= gfc_get_proc_ptr_comp (expr
);
7571 if ((expr
->expr_type
== EXPR_PPC
|| expr
->expr_type
== EXPR_FUNCTION
)
7574 func_ifc
= comp
->ts
.interface
;
7578 if (expr
->expr_type
== EXPR_COMPCALL
)
7580 gcc_assert (!expr
->value
.compcall
.tbp
->is_generic
);
7581 func_ifc
= expr
->value
.compcall
.tbp
->u
.specific
->n
.sym
;
7588 gcc_assert (func_ifc
->attr
.function
7589 && func_ifc
->result
!= NULL
);
7590 return func_ifc
->result
->attr
.pointer
;
7594 /* Is the lhs OK for automatic reallocation? */
7597 is_scalar_reallocatable_lhs (gfc_expr
*expr
)
7601 /* An allocatable variable with no reference. */
7602 if (expr
->symtree
->n
.sym
->attr
.allocatable
7606 /* All that can be left are allocatable components. */
7607 if ((expr
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
7608 && expr
->symtree
->n
.sym
->ts
.type
!= BT_CLASS
)
7609 || !expr
->symtree
->n
.sym
->ts
.u
.derived
->attr
.alloc_comp
)
7612 /* Find an allocatable component ref last. */
7613 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
7614 if (ref
->type
== REF_COMPONENT
7616 && ref
->u
.c
.component
->attr
.allocatable
)
7623 /* Allocate or reallocate scalar lhs, as necessary. */
7626 alloc_scalar_allocatable_for_assignment (stmtblock_t
*block
,
7640 if (!expr1
|| expr1
->rank
)
7643 if (!expr2
|| expr2
->rank
)
7646 realloc_lhs_warning (expr2
->ts
.type
, false, &expr2
->where
);
7648 /* Since this is a scalar lhs, we can afford to do this. That is,
7649 there is no risk of side effects being repeated. */
7650 gfc_init_se (&lse
, NULL
);
7651 lse
.want_pointer
= 1;
7652 gfc_conv_expr (&lse
, expr1
);
7654 jump_label1
= gfc_build_label_decl (NULL_TREE
);
7655 jump_label2
= gfc_build_label_decl (NULL_TREE
);
7657 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
7658 tmp
= build_int_cst (TREE_TYPE (lse
.expr
), 0);
7659 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
7661 tmp
= build3_v (COND_EXPR
, cond
,
7662 build1_v (GOTO_EXPR
, jump_label1
),
7663 build_empty_stmt (input_location
));
7664 gfc_add_expr_to_block (block
, tmp
);
7666 if (expr1
->ts
.type
== BT_CHARACTER
&& expr1
->ts
.deferred
)
7668 /* Use the rhs string length and the lhs element size. */
7669 size
= string_length
;
7670 tmp
= TREE_TYPE (gfc_typenode_for_spec (&expr1
->ts
));
7671 tmp
= TYPE_SIZE_UNIT (tmp
);
7672 size_in_bytes
= fold_build2_loc (input_location
, MULT_EXPR
,
7673 TREE_TYPE (tmp
), tmp
,
7674 fold_convert (TREE_TYPE (tmp
), size
));
7678 /* Otherwise use the length in bytes of the rhs. */
7679 size
= TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1
->ts
));
7680 size_in_bytes
= size
;
7683 size_in_bytes
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
,
7684 size_in_bytes
, size_one_node
);
7686 if (expr1
->ts
.type
== BT_DERIVED
&& expr1
->ts
.u
.derived
->attr
.alloc_comp
)
7688 tmp
= build_call_expr_loc (input_location
,
7689 builtin_decl_explicit (BUILT_IN_CALLOC
),
7690 2, build_one_cst (size_type_node
),
7692 tmp
= fold_convert (TREE_TYPE (lse
.expr
), tmp
);
7693 gfc_add_modify (block
, lse
.expr
, tmp
);
7697 tmp
= build_call_expr_loc (input_location
,
7698 builtin_decl_explicit (BUILT_IN_MALLOC
),
7700 tmp
= fold_convert (TREE_TYPE (lse
.expr
), tmp
);
7701 gfc_add_modify (block
, lse
.expr
, tmp
);
7704 if (expr1
->ts
.type
== BT_CHARACTER
&& expr1
->ts
.deferred
)
7706 /* Deferred characters need checking for lhs and rhs string
7707 length. Other deferred parameter variables will have to
7709 tmp
= build1_v (GOTO_EXPR
, jump_label2
);
7710 gfc_add_expr_to_block (block
, tmp
);
7712 tmp
= build1_v (LABEL_EXPR
, jump_label1
);
7713 gfc_add_expr_to_block (block
, tmp
);
7715 /* For a deferred length character, reallocate if lengths of lhs and
7716 rhs are different. */
7717 if (expr1
->ts
.type
== BT_CHARACTER
&& expr1
->ts
.deferred
)
7719 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
7720 expr1
->ts
.u
.cl
->backend_decl
, size
);
7721 /* Jump past the realloc if the lengths are the same. */
7722 tmp
= build3_v (COND_EXPR
, cond
,
7723 build1_v (GOTO_EXPR
, jump_label2
),
7724 build_empty_stmt (input_location
));
7725 gfc_add_expr_to_block (block
, tmp
);
7726 tmp
= build_call_expr_loc (input_location
,
7727 builtin_decl_explicit (BUILT_IN_REALLOC
),
7728 2, fold_convert (pvoid_type_node
, lse
.expr
),
7730 tmp
= fold_convert (TREE_TYPE (lse
.expr
), tmp
);
7731 gfc_add_modify (block
, lse
.expr
, tmp
);
7732 tmp
= build1_v (LABEL_EXPR
, jump_label2
);
7733 gfc_add_expr_to_block (block
, tmp
);
7735 /* Update the lhs character length. */
7736 size
= string_length
;
7737 gfc_add_modify (block
, expr1
->ts
.u
.cl
->backend_decl
, size
);
7742 /* Subroutine of gfc_trans_assignment that actually scalarizes the
7743 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
7744 init_flag indicates initialization expressions and dealloc that no
7745 deallocate prior assignment is needed (if in doubt, set true). */
7748 gfc_trans_assignment_1 (gfc_expr
* expr1
, gfc_expr
* expr2
, bool init_flag
,
7754 gfc_ss
*lss_section
;
7761 bool scalar_to_array
;
7765 /* Assignment of the form lhs = rhs. */
7766 gfc_start_block (&block
);
7768 gfc_init_se (&lse
, NULL
);
7769 gfc_init_se (&rse
, NULL
);
7772 lss
= gfc_walk_expr (expr1
);
7773 if (gfc_is_reallocatable_lhs (expr1
)
7774 && !(expr2
->expr_type
== EXPR_FUNCTION
7775 && expr2
->value
.function
.isym
!= NULL
))
7776 lss
->is_alloc_lhs
= 1;
7778 if (lss
!= gfc_ss_terminator
)
7780 /* The assignment needs scalarization. */
7783 /* Find a non-scalar SS from the lhs. */
7784 while (lss_section
!= gfc_ss_terminator
7785 && lss_section
->info
->type
!= GFC_SS_SECTION
)
7786 lss_section
= lss_section
->next
;
7788 gcc_assert (lss_section
!= gfc_ss_terminator
);
7790 /* Initialize the scalarizer. */
7791 gfc_init_loopinfo (&loop
);
7794 rss
= gfc_walk_expr (expr2
);
7795 if (rss
== gfc_ss_terminator
)
7796 /* The rhs is scalar. Add a ss for the expression. */
7797 rss
= gfc_get_scalar_ss (gfc_ss_terminator
, expr2
);
7799 /* Associate the SS with the loop. */
7800 gfc_add_ss_to_loop (&loop
, lss
);
7801 gfc_add_ss_to_loop (&loop
, rss
);
7803 /* Calculate the bounds of the scalarization. */
7804 gfc_conv_ss_startstride (&loop
);
7805 /* Enable loop reversal. */
7806 for (n
= 0; n
< GFC_MAX_DIMENSIONS
; n
++)
7807 loop
.reverse
[n
] = GFC_ENABLE_REVERSE
;
7808 /* Resolve any data dependencies in the statement. */
7809 gfc_conv_resolve_dependencies (&loop
, lss
, rss
);
7810 /* Setup the scalarizing loops. */
7811 gfc_conv_loop_setup (&loop
, &expr2
->where
);
7813 /* Setup the gfc_se structures. */
7814 gfc_copy_loopinfo_to_se (&lse
, &loop
);
7815 gfc_copy_loopinfo_to_se (&rse
, &loop
);
7818 gfc_mark_ss_chain_used (rss
, 1);
7819 if (loop
.temp_ss
== NULL
)
7822 gfc_mark_ss_chain_used (lss
, 1);
7826 lse
.ss
= loop
.temp_ss
;
7827 gfc_mark_ss_chain_used (lss
, 3);
7828 gfc_mark_ss_chain_used (loop
.temp_ss
, 3);
7831 /* Allow the scalarizer to workshare array assignments. */
7832 if ((ompws_flags
& OMPWS_WORKSHARE_FLAG
) && loop
.temp_ss
== NULL
)
7833 ompws_flags
|= OMPWS_SCALARIZER_WS
;
7835 /* Start the scalarized loop body. */
7836 gfc_start_scalarized_body (&loop
, &body
);
7839 gfc_init_block (&body
);
7841 l_is_temp
= (lss
!= gfc_ss_terminator
&& loop
.temp_ss
!= NULL
);
7843 /* Translate the expression. */
7844 gfc_conv_expr (&rse
, expr2
);
7846 /* Stabilize a string length for temporaries. */
7847 if (expr2
->ts
.type
== BT_CHARACTER
)
7848 string_length
= gfc_evaluate_now (rse
.string_length
, &rse
.pre
);
7850 string_length
= NULL_TREE
;
7854 gfc_conv_tmp_array_ref (&lse
);
7855 if (expr2
->ts
.type
== BT_CHARACTER
)
7856 lse
.string_length
= string_length
;
7859 gfc_conv_expr (&lse
, expr1
);
7861 /* Assignments of scalar derived types with allocatable components
7862 to arrays must be done with a deep copy and the rhs temporary
7863 must have its components deallocated afterwards. */
7864 scalar_to_array
= (expr2
->ts
.type
== BT_DERIVED
7865 && expr2
->ts
.u
.derived
->attr
.alloc_comp
7866 && !expr_is_variable (expr2
)
7867 && !gfc_is_constant_expr (expr2
)
7868 && expr1
->rank
&& !expr2
->rank
);
7869 if (scalar_to_array
&& dealloc
)
7871 tmp
= gfc_deallocate_alloc_comp_no_caf (expr2
->ts
.u
.derived
, rse
.expr
, 0);
7872 gfc_add_expr_to_block (&loop
.post
, tmp
);
7875 /* When assigning a character function result to a deferred-length variable,
7876 the function call must happen before the (re)allocation of the lhs -
7877 otherwise the character length of the result is not known.
7878 NOTE: This relies on having the exact dependence of the length type
7879 parameter available to the caller; gfortran saves it in the .mod files. */
7880 if (gfc_option
.flag_realloc_lhs
&& expr2
->ts
.type
== BT_CHARACTER
7881 && expr1
->ts
.deferred
)
7882 gfc_add_block_to_block (&block
, &rse
.pre
);
7884 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr1
->ts
,
7885 l_is_temp
|| init_flag
,
7886 expr_is_variable (expr2
) || scalar_to_array
7887 || expr2
->expr_type
== EXPR_ARRAY
, dealloc
);
7888 gfc_add_expr_to_block (&body
, tmp
);
7890 if (lss
== gfc_ss_terminator
)
7892 /* F2003: Add the code for reallocation on assignment. */
7893 if (gfc_option
.flag_realloc_lhs
7894 && is_scalar_reallocatable_lhs (expr1
))
7895 alloc_scalar_allocatable_for_assignment (&block
, rse
.string_length
,
7898 /* Use the scalar assignment as is. */
7899 gfc_add_block_to_block (&block
, &body
);
7903 gcc_assert (lse
.ss
== gfc_ss_terminator
7904 && rse
.ss
== gfc_ss_terminator
);
7908 gfc_trans_scalarized_loop_boundary (&loop
, &body
);
7910 /* We need to copy the temporary to the actual lhs. */
7911 gfc_init_se (&lse
, NULL
);
7912 gfc_init_se (&rse
, NULL
);
7913 gfc_copy_loopinfo_to_se (&lse
, &loop
);
7914 gfc_copy_loopinfo_to_se (&rse
, &loop
);
7916 rse
.ss
= loop
.temp_ss
;
7919 gfc_conv_tmp_array_ref (&rse
);
7920 gfc_conv_expr (&lse
, expr1
);
7922 gcc_assert (lse
.ss
== gfc_ss_terminator
7923 && rse
.ss
== gfc_ss_terminator
);
7925 if (expr2
->ts
.type
== BT_CHARACTER
)
7926 rse
.string_length
= string_length
;
7928 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr1
->ts
,
7929 false, false, dealloc
);
7930 gfc_add_expr_to_block (&body
, tmp
);
7933 /* F2003: Allocate or reallocate lhs of allocatable array. */
7934 if (gfc_option
.flag_realloc_lhs
7935 && gfc_is_reallocatable_lhs (expr1
)
7936 && !gfc_expr_attr (expr1
).codimension
7937 && !gfc_is_coindexed (expr1
)
7940 realloc_lhs_warning (expr1
->ts
.type
, true, &expr1
->where
);
7941 ompws_flags
&= ~OMPWS_SCALARIZER_WS
;
7942 tmp
= gfc_alloc_allocatable_for_assignment (&loop
, expr1
, expr2
);
7943 if (tmp
!= NULL_TREE
)
7944 gfc_add_expr_to_block (&loop
.code
[expr1
->rank
- 1], tmp
);
7947 /* Generate the copying loops. */
7948 gfc_trans_scalarizing_loops (&loop
, &body
);
7950 /* Wrap the whole thing up. */
7951 gfc_add_block_to_block (&block
, &loop
.pre
);
7952 gfc_add_block_to_block (&block
, &loop
.post
);
7954 gfc_cleanup_loop (&loop
);
7957 return gfc_finish_block (&block
);
7961 /* Check whether EXPR is a copyable array. */
7964 copyable_array_p (gfc_expr
* expr
)
7966 if (expr
->expr_type
!= EXPR_VARIABLE
)
7969 /* First check it's an array. */
7970 if (expr
->rank
< 1 || !expr
->ref
|| expr
->ref
->next
)
7973 if (!gfc_full_array_ref_p (expr
->ref
, NULL
))
7976 /* Next check that it's of a simple enough type. */
7977 switch (expr
->ts
.type
)
7989 return !expr
->ts
.u
.derived
->attr
.alloc_comp
;
7998 /* Translate an assignment. */
8001 gfc_trans_assignment (gfc_expr
* expr1
, gfc_expr
* expr2
, bool init_flag
,
8006 /* Special case a single function returning an array. */
8007 if (expr2
->expr_type
== EXPR_FUNCTION
&& expr2
->rank
> 0)
8009 tmp
= gfc_trans_arrayfunc_assign (expr1
, expr2
);
8014 /* Special case assigning an array to zero. */
8015 if (copyable_array_p (expr1
)
8016 && is_zero_initializer_p (expr2
))
8018 tmp
= gfc_trans_zero_assign (expr1
);
8023 /* Special case copying one array to another. */
8024 if (copyable_array_p (expr1
)
8025 && copyable_array_p (expr2
)
8026 && gfc_compare_types (&expr1
->ts
, &expr2
->ts
)
8027 && !gfc_check_dependency (expr1
, expr2
, 0))
8029 tmp
= gfc_trans_array_copy (expr1
, expr2
);
8034 /* Special case initializing an array from a constant array constructor. */
8035 if (copyable_array_p (expr1
)
8036 && expr2
->expr_type
== EXPR_ARRAY
8037 && gfc_compare_types (&expr1
->ts
, &expr2
->ts
))
8039 tmp
= gfc_trans_array_constructor_copy (expr1
, expr2
);
8044 /* Fallback to the scalarizer to generate explicit loops. */
8045 return gfc_trans_assignment_1 (expr1
, expr2
, init_flag
, dealloc
);
8049 gfc_trans_init_assign (gfc_code
* code
)
8051 return gfc_trans_assignment (code
->expr1
, code
->expr2
, true, false);
8055 gfc_trans_assign (gfc_code
* code
)
8057 return gfc_trans_assignment (code
->expr1
, code
->expr2
, false, true);