1 /* Expression translation
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
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"
31 #include "stringpool.h"
32 #include "diagnostic-core.h" /* For fatal_error. */
33 #include "fold-const.h"
34 #include "langhooks.h"
36 #include "constructor.h"
37 #include "trans-const.h"
38 #include "trans-types.h"
39 #include "trans-array.h"
40 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
41 #include "trans-stmt.h"
42 #include "dependency.h"
45 /* Convert a scalar to an array descriptor. To be used for assumed-rank
49 get_scalar_to_descriptor_type (tree scalar
, symbol_attribute attr
)
51 enum gfc_array_kind akind
;
54 akind
= GFC_ARRAY_POINTER_CONT
;
55 else if (attr
.allocatable
)
56 akind
= GFC_ARRAY_ALLOCATABLE
;
58 akind
= GFC_ARRAY_ASSUMED_SHAPE_CONT
;
60 if (POINTER_TYPE_P (TREE_TYPE (scalar
)))
61 scalar
= TREE_TYPE (scalar
);
62 return gfc_get_array_type_bounds (TREE_TYPE (scalar
), 0, 0, NULL
, NULL
, 1,
63 akind
, !(attr
.pointer
|| attr
.target
));
67 gfc_conv_scalar_to_descriptor (gfc_se
*se
, tree scalar
, symbol_attribute attr
)
71 type
= get_scalar_to_descriptor_type (scalar
, attr
);
72 desc
= gfc_create_var (type
, "desc");
73 DECL_ARTIFICIAL (desc
) = 1;
75 if (CONSTANT_CLASS_P (scalar
))
78 tmp
= gfc_create_var (TREE_TYPE (scalar
), "scalar");
79 gfc_add_modify (&se
->pre
, tmp
, scalar
);
82 if (!POINTER_TYPE_P (TREE_TYPE (scalar
)))
83 scalar
= gfc_build_addr_expr (NULL_TREE
, scalar
);
84 gfc_add_modify (&se
->pre
, gfc_conv_descriptor_dtype (desc
),
85 gfc_get_dtype (type
));
86 gfc_conv_descriptor_data_set (&se
->pre
, desc
, scalar
);
88 /* Copy pointer address back - but only if it could have changed and
89 if the actual argument is a pointer and not, e.g., NULL(). */
90 if ((attr
.pointer
|| attr
.allocatable
) && attr
.intent
!= INTENT_IN
)
91 gfc_add_modify (&se
->post
, scalar
,
92 fold_convert (TREE_TYPE (scalar
),
93 gfc_conv_descriptor_data_get (desc
)));
98 /* Get the coarray token from the ultimate array or component ref.
99 Returns a NULL_TREE, when the ref object is not allocatable or pointer. */
102 gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se
*outerse
, gfc_expr
*expr
)
104 gfc_symbol
*sym
= expr
->symtree
->n
.sym
;
105 bool is_coarray
= sym
->attr
.codimension
;
106 gfc_expr
*caf_expr
= gfc_copy_expr (expr
);
107 gfc_ref
*ref
= caf_expr
->ref
, *last_caf_ref
= NULL
;
111 if (ref
->type
== REF_COMPONENT
112 && (ref
->u
.c
.component
->attr
.allocatable
113 || ref
->u
.c
.component
->attr
.pointer
)
114 && (is_coarray
|| ref
->u
.c
.component
->attr
.codimension
))
119 if (last_caf_ref
== NULL
)
122 tree comp
= last_caf_ref
->u
.c
.component
->caf_token
, caf
;
124 bool comp_ref
= !last_caf_ref
->u
.c
.component
->attr
.dimension
;
125 if (comp
== NULL_TREE
&& comp_ref
)
127 gfc_init_se (&se
, outerse
);
128 gfc_free_ref_list (last_caf_ref
->next
);
129 last_caf_ref
->next
= NULL
;
130 caf_expr
->rank
= comp_ref
? 0 : last_caf_ref
->u
.c
.component
->as
->rank
;
131 se
.want_pointer
= comp_ref
;
132 gfc_conv_expr (&se
, caf_expr
);
133 gfc_add_block_to_block (&outerse
->pre
, &se
.pre
);
135 if (TREE_CODE (se
.expr
) == COMPONENT_REF
&& comp_ref
)
136 se
.expr
= TREE_OPERAND (se
.expr
, 0);
137 gfc_free_expr (caf_expr
);
140 caf
= fold_build3_loc (input_location
, COMPONENT_REF
,
141 TREE_TYPE (comp
), se
.expr
, comp
, NULL_TREE
);
143 caf
= gfc_conv_descriptor_token (se
.expr
);
144 return gfc_build_addr_expr (NULL_TREE
, caf
);
148 /* This is the seed for an eventual trans-class.c
150 The following parameters should not be used directly since they might
151 in future implementations. Use the corresponding APIs. */
152 #define CLASS_DATA_FIELD 0
153 #define CLASS_VPTR_FIELD 1
154 #define CLASS_LEN_FIELD 2
155 #define VTABLE_HASH_FIELD 0
156 #define VTABLE_SIZE_FIELD 1
157 #define VTABLE_EXTENDS_FIELD 2
158 #define VTABLE_DEF_INIT_FIELD 3
159 #define VTABLE_COPY_FIELD 4
160 #define VTABLE_FINAL_FIELD 5
161 #define VTABLE_DEALLOCATE_FIELD 6
165 gfc_class_set_static_fields (tree decl
, tree vptr
, tree data
)
169 vec
<constructor_elt
, va_gc
> *init
= NULL
;
171 field
= TYPE_FIELDS (TREE_TYPE (decl
));
172 tmp
= gfc_advance_chain (field
, CLASS_DATA_FIELD
);
173 CONSTRUCTOR_APPEND_ELT (init
, tmp
, data
);
175 tmp
= gfc_advance_chain (field
, CLASS_VPTR_FIELD
);
176 CONSTRUCTOR_APPEND_ELT (init
, tmp
, vptr
);
178 return build_constructor (TREE_TYPE (decl
), init
);
183 gfc_class_data_get (tree decl
)
186 if (POINTER_TYPE_P (TREE_TYPE (decl
)))
187 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
188 data
= gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl
)),
190 return fold_build3_loc (input_location
, COMPONENT_REF
,
191 TREE_TYPE (data
), decl
, data
,
197 gfc_class_vptr_get (tree decl
)
200 /* For class arrays decl may be a temporary descriptor handle, the vptr is
201 then available through the saved descriptor. */
202 if (VAR_P (decl
) && DECL_LANG_SPECIFIC (decl
)
203 && GFC_DECL_SAVED_DESCRIPTOR (decl
))
204 decl
= GFC_DECL_SAVED_DESCRIPTOR (decl
);
205 if (POINTER_TYPE_P (TREE_TYPE (decl
)))
206 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
207 vptr
= gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl
)),
209 return fold_build3_loc (input_location
, COMPONENT_REF
,
210 TREE_TYPE (vptr
), decl
, vptr
,
216 gfc_class_len_get (tree decl
)
219 /* For class arrays decl may be a temporary descriptor handle, the len is
220 then available through the saved descriptor. */
221 if (VAR_P (decl
) && DECL_LANG_SPECIFIC (decl
)
222 && GFC_DECL_SAVED_DESCRIPTOR (decl
))
223 decl
= GFC_DECL_SAVED_DESCRIPTOR (decl
);
224 if (POINTER_TYPE_P (TREE_TYPE (decl
)))
225 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
226 len
= gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl
)),
228 return fold_build3_loc (input_location
, COMPONENT_REF
,
229 TREE_TYPE (len
), decl
, len
,
234 /* Try to get the _len component of a class. When the class is not unlimited
235 poly, i.e. no _len field exists, then return a zero node. */
238 gfc_class_len_or_zero_get (tree decl
)
241 /* For class arrays decl may be a temporary descriptor handle, the vptr is
242 then available through the saved descriptor. */
243 if (VAR_P (decl
) && DECL_LANG_SPECIFIC (decl
)
244 && GFC_DECL_SAVED_DESCRIPTOR (decl
))
245 decl
= GFC_DECL_SAVED_DESCRIPTOR (decl
);
246 if (POINTER_TYPE_P (TREE_TYPE (decl
)))
247 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
248 len
= gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl
)),
250 return len
!= NULL_TREE
? fold_build3_loc (input_location
, COMPONENT_REF
,
251 TREE_TYPE (len
), decl
, len
,
257 /* Get the specified FIELD from the VPTR. */
260 vptr_field_get (tree vptr
, int fieldno
)
263 vptr
= build_fold_indirect_ref_loc (input_location
, vptr
);
264 field
= gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr
)),
266 field
= fold_build3_loc (input_location
, COMPONENT_REF
,
267 TREE_TYPE (field
), vptr
, field
,
274 /* Get the field from the class' vptr. */
277 class_vtab_field_get (tree decl
, int fieldno
)
280 vptr
= gfc_class_vptr_get (decl
);
281 return vptr_field_get (vptr
, fieldno
);
285 /* Define a macro for creating the class_vtab_* and vptr_* accessors in
287 #define VTAB_GET_FIELD_GEN(name, field) tree \
288 gfc_class_vtab_## name ##_get (tree cl) \
290 return class_vtab_field_get (cl, field); \
294 gfc_vptr_## name ##_get (tree vptr) \
296 return vptr_field_get (vptr, field); \
299 VTAB_GET_FIELD_GEN (hash
, VTABLE_HASH_FIELD
)
300 VTAB_GET_FIELD_GEN (extends
, VTABLE_EXTENDS_FIELD
)
301 VTAB_GET_FIELD_GEN (def_init
, VTABLE_DEF_INIT_FIELD
)
302 VTAB_GET_FIELD_GEN (copy
, VTABLE_COPY_FIELD
)
303 VTAB_GET_FIELD_GEN (final
, VTABLE_FINAL_FIELD
)
304 VTAB_GET_FIELD_GEN (deallocate
, VTABLE_DEALLOCATE_FIELD
)
307 /* The size field is returned as an array index type. Therefore treat
308 it and only it specially. */
311 gfc_class_vtab_size_get (tree cl
)
314 size
= class_vtab_field_get (cl
, VTABLE_SIZE_FIELD
);
315 /* Always return size as an array index type. */
316 size
= fold_convert (gfc_array_index_type
, size
);
322 gfc_vptr_size_get (tree vptr
)
325 size
= vptr_field_get (vptr
, VTABLE_SIZE_FIELD
);
326 /* Always return size as an array index type. */
327 size
= fold_convert (gfc_array_index_type
, size
);
333 #undef CLASS_DATA_FIELD
334 #undef CLASS_VPTR_FIELD
335 #undef CLASS_LEN_FIELD
336 #undef VTABLE_HASH_FIELD
337 #undef VTABLE_SIZE_FIELD
338 #undef VTABLE_EXTENDS_FIELD
339 #undef VTABLE_DEF_INIT_FIELD
340 #undef VTABLE_COPY_FIELD
341 #undef VTABLE_FINAL_FIELD
344 /* Search for the last _class ref in the chain of references of this
345 expression and cut the chain there. Albeit this routine is similiar
346 to class.c::gfc_add_component_ref (), is there a significant
347 difference: gfc_add_component_ref () concentrates on an array ref to
348 be the last ref in the chain. This routine is oblivious to the kind
349 of refs following. */
352 gfc_find_and_cut_at_last_class_ref (gfc_expr
*e
)
355 gfc_ref
*ref
, *class_ref
, *tail
= NULL
, *array_ref
;
357 /* Find the last class reference. */
360 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
362 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
!= AR_ELEMENT
)
365 if (ref
->type
== REF_COMPONENT
366 && ref
->u
.c
.component
->ts
.type
== BT_CLASS
)
368 /* Component to the right of a part reference with nonzero rank
369 must not have the ALLOCATABLE attribute. If attempts are
370 made to reference such a component reference, an error results
371 followed by an ICE. */
372 if (array_ref
&& CLASS_DATA (ref
->u
.c
.component
)->attr
.allocatable
)
377 if (ref
->next
== NULL
)
381 /* Remove and store all subsequent references after the
385 tail
= class_ref
->next
;
386 class_ref
->next
= NULL
;
388 else if (e
->symtree
&& e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
394 base_expr
= gfc_expr_to_initialize (e
);
396 /* Restore the original tail expression. */
399 gfc_free_ref_list (class_ref
->next
);
400 class_ref
->next
= tail
;
402 else if (e
->symtree
&& e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
404 gfc_free_ref_list (e
->ref
);
411 /* Reset the vptr to the declared type, e.g. after deallocation. */
414 gfc_reset_vptr (stmtblock_t
*block
, gfc_expr
*e
)
421 /* Evaluate the expression and obtain the vptr from it. */
422 gfc_init_se (&se
, NULL
);
424 gfc_conv_expr_descriptor (&se
, e
);
426 gfc_conv_expr (&se
, e
);
427 gfc_add_block_to_block (block
, &se
.pre
);
428 vptr
= gfc_get_vptr_from_expr (se
.expr
);
430 /* If a vptr is not found, we can do nothing more. */
431 if (vptr
== NULL_TREE
)
434 if (UNLIMITED_POLY (e
))
435 gfc_add_modify (block
, vptr
, build_int_cst (TREE_TYPE (vptr
), 0));
438 /* Return the vptr to the address of the declared type. */
439 vtab
= gfc_find_derived_vtab (e
->ts
.u
.derived
);
440 vtable
= vtab
->backend_decl
;
441 if (vtable
== NULL_TREE
)
442 vtable
= gfc_get_symbol_decl (vtab
);
443 vtable
= gfc_build_addr_expr (NULL
, vtable
);
444 vtable
= fold_convert (TREE_TYPE (vptr
), vtable
);
445 gfc_add_modify (block
, vptr
, vtable
);
450 /* Reset the len for unlimited polymorphic objects. */
453 gfc_reset_len (stmtblock_t
*block
, gfc_expr
*expr
)
457 e
= gfc_find_and_cut_at_last_class_ref (expr
);
460 gfc_add_len_component (e
);
461 gfc_init_se (&se_len
, NULL
);
462 gfc_conv_expr (&se_len
, e
);
463 gfc_add_modify (block
, se_len
.expr
,
464 fold_convert (TREE_TYPE (se_len
.expr
), integer_zero_node
));
469 /* Obtain the vptr of the last class reference in an expression.
470 Return NULL_TREE if no class reference is found. */
473 gfc_get_vptr_from_expr (tree expr
)
478 for (tmp
= expr
; tmp
; tmp
= TREE_OPERAND (tmp
, 0))
480 type
= TREE_TYPE (tmp
);
483 if (GFC_CLASS_TYPE_P (type
))
484 return gfc_class_vptr_get (tmp
);
485 if (type
!= TYPE_CANONICAL (type
))
486 type
= TYPE_CANONICAL (type
);
490 if (VAR_P (tmp
) || TREE_CODE (tmp
) == PARM_DECL
)
494 if (POINTER_TYPE_P (TREE_TYPE (tmp
)))
495 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
497 if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp
)))
498 return gfc_class_vptr_get (tmp
);
505 class_array_data_assign (stmtblock_t
*block
, tree lhs_desc
, tree rhs_desc
,
508 tree tmp
, tmp2
, type
;
510 gfc_conv_descriptor_data_set (block
, lhs_desc
,
511 gfc_conv_descriptor_data_get (rhs_desc
));
512 gfc_conv_descriptor_offset_set (block
, lhs_desc
,
513 gfc_conv_descriptor_offset_get (rhs_desc
));
515 gfc_add_modify (block
, gfc_conv_descriptor_dtype (lhs_desc
),
516 gfc_conv_descriptor_dtype (rhs_desc
));
518 /* Assign the dimension as range-ref. */
519 tmp
= gfc_get_descriptor_dimension (lhs_desc
);
520 tmp2
= gfc_get_descriptor_dimension (rhs_desc
);
522 type
= lhs_type
? TREE_TYPE (tmp
) : TREE_TYPE (tmp2
);
523 tmp
= build4_loc (input_location
, ARRAY_RANGE_REF
, type
, tmp
,
524 gfc_index_zero_node
, NULL_TREE
, NULL_TREE
);
525 tmp2
= build4_loc (input_location
, ARRAY_RANGE_REF
, type
, tmp2
,
526 gfc_index_zero_node
, NULL_TREE
, NULL_TREE
);
527 gfc_add_modify (block
, tmp
, tmp2
);
531 /* Takes a derived type expression and returns the address of a temporary
532 class object of the 'declared' type. If vptr is not NULL, this is
533 used for the temporary class object.
534 optional_alloc_ptr is false when the dummy is neither allocatable
535 nor a pointer; that's only relevant for the optional handling. */
537 gfc_conv_derived_to_class (gfc_se
*parmse
, gfc_expr
*e
,
538 gfc_typespec class_ts
, tree vptr
, bool optional
,
539 bool optional_alloc_ptr
)
542 tree cond_optional
= NULL_TREE
;
548 /* The derived type needs to be converted to a temporary
550 tmp
= gfc_typenode_for_spec (&class_ts
);
551 var
= gfc_create_var (tmp
, "class");
554 ctree
= gfc_class_vptr_get (var
);
556 if (vptr
!= NULL_TREE
)
558 /* Use the dynamic vptr. */
563 /* In this case the vtab corresponds to the derived type and the
564 vptr must point to it. */
565 vtab
= gfc_find_derived_vtab (e
->ts
.u
.derived
);
567 tmp
= gfc_build_addr_expr (NULL_TREE
, gfc_get_symbol_decl (vtab
));
569 gfc_add_modify (&parmse
->pre
, ctree
,
570 fold_convert (TREE_TYPE (ctree
), tmp
));
572 /* Now set the data field. */
573 ctree
= gfc_class_data_get (var
);
576 cond_optional
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
578 if (parmse
->expr
&& POINTER_TYPE_P (TREE_TYPE (parmse
->expr
)))
580 /* If there is a ready made pointer to a derived type, use it
581 rather than evaluating the expression again. */
582 tmp
= fold_convert (TREE_TYPE (ctree
), parmse
->expr
);
583 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
585 else if (parmse
->ss
&& parmse
->ss
->info
&& parmse
->ss
->info
->useflags
)
587 /* For an array reference in an elemental procedure call we need
588 to retain the ss to provide the scalarized array reference. */
589 gfc_conv_expr_reference (parmse
, e
);
590 tmp
= fold_convert (TREE_TYPE (ctree
), parmse
->expr
);
592 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
594 fold_convert (TREE_TYPE (tmp
), null_pointer_node
));
595 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
599 ss
= gfc_walk_expr (e
);
600 if (ss
== gfc_ss_terminator
)
603 gfc_conv_expr_reference (parmse
, e
);
605 /* Scalar to an assumed-rank array. */
606 if (class_ts
.u
.derived
->components
->as
)
609 type
= get_scalar_to_descriptor_type (parmse
->expr
,
611 gfc_add_modify (&parmse
->pre
, gfc_conv_descriptor_dtype (ctree
),
612 gfc_get_dtype (type
));
614 parmse
->expr
= build3_loc (input_location
, COND_EXPR
,
615 TREE_TYPE (parmse
->expr
),
616 cond_optional
, parmse
->expr
,
617 fold_convert (TREE_TYPE (parmse
->expr
),
619 gfc_conv_descriptor_data_set (&parmse
->pre
, ctree
, parmse
->expr
);
623 tmp
= fold_convert (TREE_TYPE (ctree
), parmse
->expr
);
625 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
627 fold_convert (TREE_TYPE (tmp
),
629 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
635 gfc_init_block (&block
);
638 gfc_conv_expr_descriptor (parmse
, e
);
640 if (e
->rank
!= class_ts
.u
.derived
->components
->as
->rank
)
642 gcc_assert (class_ts
.u
.derived
->components
->as
->type
644 class_array_data_assign (&block
, ctree
, parmse
->expr
, false);
648 if (gfc_expr_attr (e
).codimension
)
649 parmse
->expr
= fold_build1_loc (input_location
,
653 gfc_add_modify (&block
, ctree
, parmse
->expr
);
658 tmp
= gfc_finish_block (&block
);
660 gfc_init_block (&block
);
661 gfc_conv_descriptor_data_set (&block
, ctree
, null_pointer_node
);
663 tmp
= build3_v (COND_EXPR
, cond_optional
, tmp
,
664 gfc_finish_block (&block
));
665 gfc_add_expr_to_block (&parmse
->pre
, tmp
);
668 gfc_add_block_to_block (&parmse
->pre
, &block
);
672 if (class_ts
.u
.derived
->components
->ts
.type
== BT_DERIVED
673 && class_ts
.u
.derived
->components
->ts
.u
.derived
674 ->attr
.unlimited_polymorphic
)
676 /* Take care about initializing the _len component correctly. */
677 ctree
= gfc_class_len_get (var
);
678 if (UNLIMITED_POLY (e
))
683 len
= gfc_copy_expr (e
);
684 gfc_add_len_component (len
);
685 gfc_init_se (&se
, NULL
);
686 gfc_conv_expr (&se
, len
);
688 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (se
.expr
),
689 cond_optional
, se
.expr
,
690 fold_convert (TREE_TYPE (se
.expr
),
696 tmp
= integer_zero_node
;
697 gfc_add_modify (&parmse
->pre
, ctree
, fold_convert (TREE_TYPE (ctree
),
700 /* Pass the address of the class object. */
701 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
703 if (optional
&& optional_alloc_ptr
)
704 parmse
->expr
= build3_loc (input_location
, COND_EXPR
,
705 TREE_TYPE (parmse
->expr
),
706 cond_optional
, parmse
->expr
,
707 fold_convert (TREE_TYPE (parmse
->expr
),
712 /* Create a new class container, which is required as scalar coarrays
713 have an array descriptor while normal scalars haven't. Optionally,
714 NULL pointer checks are added if the argument is OPTIONAL. */
717 class_scalar_coarray_to_class (gfc_se
*parmse
, gfc_expr
*e
,
718 gfc_typespec class_ts
, bool optional
)
720 tree var
, ctree
, tmp
;
725 gfc_init_block (&block
);
728 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
730 if (ref
->type
== REF_COMPONENT
731 && ref
->u
.c
.component
->ts
.type
== BT_CLASS
)
735 if (class_ref
== NULL
736 && e
->symtree
&& e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
737 tmp
= e
->symtree
->n
.sym
->backend_decl
;
740 /* Remove everything after the last class reference, convert the
741 expression and then recover its tailend once more. */
743 ref
= class_ref
->next
;
744 class_ref
->next
= NULL
;
745 gfc_init_se (&tmpse
, NULL
);
746 gfc_conv_expr (&tmpse
, e
);
747 class_ref
->next
= ref
;
751 var
= gfc_typenode_for_spec (&class_ts
);
752 var
= gfc_create_var (var
, "class");
754 ctree
= gfc_class_vptr_get (var
);
755 gfc_add_modify (&block
, ctree
,
756 fold_convert (TREE_TYPE (ctree
), gfc_class_vptr_get (tmp
)));
758 ctree
= gfc_class_data_get (var
);
759 tmp
= gfc_conv_descriptor_data_get (gfc_class_data_get (tmp
));
760 gfc_add_modify (&block
, ctree
, fold_convert (TREE_TYPE (ctree
), tmp
));
762 /* Pass the address of the class object. */
763 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
767 tree cond
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
770 tmp
= gfc_finish_block (&block
);
772 gfc_init_block (&block
);
773 tmp2
= gfc_class_data_get (var
);
774 gfc_add_modify (&block
, tmp2
, fold_convert (TREE_TYPE (tmp2
),
776 tmp2
= gfc_finish_block (&block
);
778 tmp
= build3_loc (input_location
, COND_EXPR
, void_type_node
,
780 gfc_add_expr_to_block (&parmse
->pre
, tmp
);
783 gfc_add_block_to_block (&parmse
->pre
, &block
);
787 /* Takes an intrinsic type expression and returns the address of a temporary
788 class object of the 'declared' type. */
790 gfc_conv_intrinsic_to_class (gfc_se
*parmse
, gfc_expr
*e
,
791 gfc_typespec class_ts
)
799 /* The intrinsic type needs to be converted to a temporary
801 tmp
= gfc_typenode_for_spec (&class_ts
);
802 var
= gfc_create_var (tmp
, "class");
805 ctree
= gfc_class_vptr_get (var
);
807 vtab
= gfc_find_vtab (&e
->ts
);
809 tmp
= gfc_build_addr_expr (NULL_TREE
, gfc_get_symbol_decl (vtab
));
810 gfc_add_modify (&parmse
->pre
, ctree
,
811 fold_convert (TREE_TYPE (ctree
), tmp
));
813 /* Now set the data field. */
814 ctree
= gfc_class_data_get (var
);
815 if (parmse
->ss
&& parmse
->ss
->info
->useflags
)
817 /* For an array reference in an elemental procedure call we need
818 to retain the ss to provide the scalarized array reference. */
819 gfc_conv_expr_reference (parmse
, e
);
820 tmp
= fold_convert (TREE_TYPE (ctree
), parmse
->expr
);
821 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
825 ss
= gfc_walk_expr (e
);
826 if (ss
== gfc_ss_terminator
)
829 gfc_conv_expr_reference (parmse
, e
);
830 if (class_ts
.u
.derived
->components
->as
831 && class_ts
.u
.derived
->components
->as
->type
== AS_ASSUMED_RANK
)
833 tmp
= gfc_conv_scalar_to_descriptor (parmse
, parmse
->expr
,
835 tmp
= fold_build1_loc (input_location
, VIEW_CONVERT_EXPR
,
836 TREE_TYPE (ctree
), tmp
);
839 tmp
= fold_convert (TREE_TYPE (ctree
), parmse
->expr
);
840 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
845 parmse
->use_offset
= 1;
846 gfc_conv_expr_descriptor (parmse
, e
);
847 if (class_ts
.u
.derived
->components
->as
->rank
!= e
->rank
)
849 tmp
= fold_build1_loc (input_location
, VIEW_CONVERT_EXPR
,
850 TREE_TYPE (ctree
), parmse
->expr
);
851 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
854 gfc_add_modify (&parmse
->pre
, ctree
, parmse
->expr
);
858 gcc_assert (class_ts
.type
== BT_CLASS
);
859 if (class_ts
.u
.derived
->components
->ts
.type
== BT_DERIVED
860 && class_ts
.u
.derived
->components
->ts
.u
.derived
861 ->attr
.unlimited_polymorphic
)
863 ctree
= gfc_class_len_get (var
);
864 /* When the actual arg is a char array, then set the _len component of the
865 unlimited polymorphic entity to the length of the string. */
866 if (e
->ts
.type
== BT_CHARACTER
)
868 /* Start with parmse->string_length because this seems to be set to a
869 correct value more often. */
870 if (parmse
->string_length
)
871 tmp
= parmse
->string_length
;
872 /* When the string_length is not yet set, then try the backend_decl of
874 else if (e
->ts
.u
.cl
->backend_decl
)
875 tmp
= e
->ts
.u
.cl
->backend_decl
;
876 /* If both of the above approaches fail, then try to generate an
877 expression from the input, which is only feasible currently, when the
878 expression can be evaluated to a constant one. */
881 /* Try to simplify the expression. */
882 gfc_simplify_expr (e
, 0);
883 if (e
->expr_type
== EXPR_CONSTANT
&& !e
->ts
.u
.cl
->resolved
)
885 /* Amazingly all data is present to compute the length of a
886 constant string, but the expression is not yet there. */
887 e
->ts
.u
.cl
->length
= gfc_get_constant_expr (BT_INTEGER
, 4,
889 mpz_set_ui (e
->ts
.u
.cl
->length
->value
.integer
,
890 e
->value
.character
.length
);
891 gfc_conv_const_charlen (e
->ts
.u
.cl
);
892 e
->ts
.u
.cl
->resolved
= 1;
893 tmp
= e
->ts
.u
.cl
->backend_decl
;
897 gfc_error ("Can't compute the length of the char array at %L.",
903 tmp
= integer_zero_node
;
905 gfc_add_modify (&parmse
->pre
, ctree
, tmp
);
907 else if (class_ts
.type
== BT_CLASS
908 && class_ts
.u
.derived
->components
909 && class_ts
.u
.derived
->components
->ts
.u
910 .derived
->attr
.unlimited_polymorphic
)
912 ctree
= gfc_class_len_get (var
);
913 gfc_add_modify (&parmse
->pre
, ctree
,
914 fold_convert (TREE_TYPE (ctree
),
917 /* Pass the address of the class object. */
918 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
922 /* Takes a scalarized class array expression and returns the
923 address of a temporary scalar class object of the 'declared'
925 OOP-TODO: This could be improved by adding code that branched on
926 the dynamic type being the same as the declared type. In this case
927 the original class expression can be passed directly.
928 optional_alloc_ptr is false when the dummy is neither allocatable
929 nor a pointer; that's relevant for the optional handling.
930 Set copyback to true if class container's _data and _vtab pointers
931 might get modified. */
934 gfc_conv_class_to_class (gfc_se
*parmse
, gfc_expr
*e
, gfc_typespec class_ts
,
935 bool elemental
, bool copyback
, bool optional
,
936 bool optional_alloc_ptr
)
942 tree cond
= NULL_TREE
;
943 tree slen
= NULL_TREE
;
947 bool full_array
= false;
949 gfc_init_block (&block
);
952 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
954 if (ref
->type
== REF_COMPONENT
955 && ref
->u
.c
.component
->ts
.type
== BT_CLASS
)
958 if (ref
->next
== NULL
)
962 if ((ref
== NULL
|| class_ref
== ref
)
963 && (!class_ts
.u
.derived
->components
->as
964 || class_ts
.u
.derived
->components
->as
->rank
!= -1))
967 /* Test for FULL_ARRAY. */
968 if (e
->rank
== 0 && gfc_expr_attr (e
).codimension
969 && gfc_expr_attr (e
).dimension
)
972 gfc_is_class_array_ref (e
, &full_array
);
974 /* The derived type needs to be converted to a temporary
976 tmp
= gfc_typenode_for_spec (&class_ts
);
977 var
= gfc_create_var (tmp
, "class");
980 ctree
= gfc_class_data_get (var
);
981 if (class_ts
.u
.derived
->components
->as
982 && e
->rank
!= class_ts
.u
.derived
->components
->as
->rank
)
986 tree type
= get_scalar_to_descriptor_type (parmse
->expr
,
988 gfc_add_modify (&block
, gfc_conv_descriptor_dtype (ctree
),
989 gfc_get_dtype (type
));
991 tmp
= gfc_class_data_get (parmse
->expr
);
992 if (!POINTER_TYPE_P (TREE_TYPE (tmp
)))
993 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
995 gfc_conv_descriptor_data_set (&block
, ctree
, tmp
);
998 class_array_data_assign (&block
, ctree
, parmse
->expr
, false);
1002 if (TREE_TYPE (parmse
->expr
) != TREE_TYPE (ctree
))
1003 parmse
->expr
= fold_build1_loc (input_location
, VIEW_CONVERT_EXPR
,
1004 TREE_TYPE (ctree
), parmse
->expr
);
1005 gfc_add_modify (&block
, ctree
, parmse
->expr
);
1008 /* Return the data component, except in the case of scalarized array
1009 references, where nullification of the cannot occur and so there
1011 if (!elemental
&& full_array
&& copyback
)
1013 if (class_ts
.u
.derived
->components
->as
1014 && e
->rank
!= class_ts
.u
.derived
->components
->as
->rank
)
1017 gfc_add_modify (&parmse
->post
, gfc_class_data_get (parmse
->expr
),
1018 gfc_conv_descriptor_data_get (ctree
));
1020 class_array_data_assign (&parmse
->post
, parmse
->expr
, ctree
, true);
1023 gfc_add_modify (&parmse
->post
, parmse
->expr
, ctree
);
1027 ctree
= gfc_class_vptr_get (var
);
1029 /* The vptr is the second field of the actual argument.
1030 First we have to find the corresponding class reference. */
1033 if (class_ref
== NULL
1034 && e
->symtree
&& e
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
1036 tmp
= e
->symtree
->n
.sym
->backend_decl
;
1038 if (TREE_CODE (tmp
) == FUNCTION_DECL
)
1039 tmp
= gfc_get_fake_result_decl (e
->symtree
->n
.sym
, 0);
1041 if (DECL_LANG_SPECIFIC (tmp
) && GFC_DECL_SAVED_DESCRIPTOR (tmp
))
1042 tmp
= GFC_DECL_SAVED_DESCRIPTOR (tmp
);
1044 slen
= integer_zero_node
;
1048 /* Remove everything after the last class reference, convert the
1049 expression and then recover its tailend once more. */
1051 ref
= class_ref
->next
;
1052 class_ref
->next
= NULL
;
1053 gfc_init_se (&tmpse
, NULL
);
1054 gfc_conv_expr (&tmpse
, e
);
1055 class_ref
->next
= ref
;
1057 slen
= tmpse
.string_length
;
1060 gcc_assert (tmp
!= NULL_TREE
);
1062 /* Dereference if needs be. */
1063 if (TREE_CODE (TREE_TYPE (tmp
)) == REFERENCE_TYPE
)
1064 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
1066 vptr
= gfc_class_vptr_get (tmp
);
1067 gfc_add_modify (&block
, ctree
,
1068 fold_convert (TREE_TYPE (ctree
), vptr
));
1070 /* Return the vptr component, except in the case of scalarized array
1071 references, where the dynamic type cannot change. */
1072 if (!elemental
&& full_array
&& copyback
)
1073 gfc_add_modify (&parmse
->post
, vptr
,
1074 fold_convert (TREE_TYPE (vptr
), ctree
));
1076 /* For unlimited polymorphic objects also set the _len component. */
1077 if (class_ts
.type
== BT_CLASS
1078 && class_ts
.u
.derived
->components
1079 && class_ts
.u
.derived
->components
->ts
.u
1080 .derived
->attr
.unlimited_polymorphic
)
1082 ctree
= gfc_class_len_get (var
);
1083 if (UNLIMITED_POLY (e
))
1084 tmp
= gfc_class_len_get (tmp
);
1085 else if (e
->ts
.type
== BT_CHARACTER
)
1087 gcc_assert (slen
!= NULL_TREE
);
1091 tmp
= integer_zero_node
;
1092 gfc_add_modify (&parmse
->pre
, ctree
,
1093 fold_convert (TREE_TYPE (ctree
), tmp
));
1095 /* Return the len component, except in the case of scalarized array
1096 references, where the dynamic type cannot change. */
1097 if (!elemental
&& full_array
&& copyback
)
1098 gfc_add_modify (&parmse
->post
, tmp
,
1099 fold_convert (TREE_TYPE (tmp
), ctree
));
1106 cond
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
1107 /* parmse->pre may contain some preparatory instructions for the
1108 temporary array descriptor. Those may only be executed when the
1109 optional argument is set, therefore add parmse->pre's instructions
1110 to block, which is later guarded by an if (optional_arg_given). */
1111 gfc_add_block_to_block (&parmse
->pre
, &block
);
1112 block
.head
= parmse
->pre
.head
;
1113 parmse
->pre
.head
= NULL_TREE
;
1114 tmp
= gfc_finish_block (&block
);
1116 if (optional_alloc_ptr
)
1117 tmp2
= build_empty_stmt (input_location
);
1120 gfc_init_block (&block
);
1122 tmp2
= gfc_conv_descriptor_data_get (gfc_class_data_get (var
));
1123 gfc_add_modify (&block
, tmp2
, fold_convert (TREE_TYPE (tmp2
),
1124 null_pointer_node
));
1125 tmp2
= gfc_finish_block (&block
);
1128 tmp
= build3_loc (input_location
, COND_EXPR
, void_type_node
,
1130 gfc_add_expr_to_block (&parmse
->pre
, tmp
);
1133 gfc_add_block_to_block (&parmse
->pre
, &block
);
1135 /* Pass the address of the class object. */
1136 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
1138 if (optional
&& optional_alloc_ptr
)
1139 parmse
->expr
= build3_loc (input_location
, COND_EXPR
,
1140 TREE_TYPE (parmse
->expr
),
1142 fold_convert (TREE_TYPE (parmse
->expr
),
1143 null_pointer_node
));
1147 /* Given a class array declaration and an index, returns the address
1148 of the referenced element. */
1151 gfc_get_class_array_ref (tree index
, tree class_decl
, tree data_comp
)
1153 tree data
= data_comp
!= NULL_TREE
? data_comp
:
1154 gfc_class_data_get (class_decl
);
1155 tree size
= gfc_class_vtab_size_get (class_decl
);
1156 tree offset
= fold_build2_loc (input_location
, MULT_EXPR
,
1157 gfc_array_index_type
,
1160 data
= gfc_conv_descriptor_data_get (data
);
1161 ptr
= fold_convert (pvoid_type_node
, data
);
1162 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, offset
);
1163 return fold_convert (TREE_TYPE (data
), ptr
);
1167 /* Copies one class expression to another, assuming that if either
1168 'to' or 'from' are arrays they are packed. Should 'from' be
1169 NULL_TREE, the initialization expression for 'to' is used, assuming
1170 that the _vptr is set. */
1173 gfc_copy_class_to_class (tree from
, tree to
, tree nelems
, bool unlimited
)
1183 vec
<tree
, va_gc
> *args
;
1188 bool is_from_desc
= false, is_to_class
= false;
1191 /* To prevent warnings on uninitialized variables. */
1192 from_len
= to_len
= NULL_TREE
;
1194 if (from
!= NULL_TREE
)
1195 fcn
= gfc_class_vtab_copy_get (from
);
1197 fcn
= gfc_class_vtab_copy_get (to
);
1199 fcn_type
= TREE_TYPE (TREE_TYPE (fcn
));
1201 if (from
!= NULL_TREE
)
1203 is_from_desc
= GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from
));
1207 from
= GFC_DECL_SAVED_DESCRIPTOR (from
);
1211 /* Check that from is a class. When the class is part of a coarray,
1212 then from is a common pointer and is to be used as is. */
1213 tmp
= POINTER_TYPE_P (TREE_TYPE (from
))
1214 ? build_fold_indirect_ref (from
) : from
;
1216 (GFC_CLASS_TYPE_P (TREE_TYPE (tmp
))
1217 || (DECL_P (tmp
) && GFC_DECL_CLASS (tmp
)))
1218 ? gfc_class_data_get (from
) : from
;
1219 is_from_desc
= GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data
));
1223 from_data
= gfc_class_vtab_def_init_get (to
);
1227 if (from
!= NULL_TREE
&& unlimited
)
1228 from_len
= gfc_class_len_or_zero_get (from
);
1230 from_len
= integer_zero_node
;
1233 if (GFC_CLASS_TYPE_P (TREE_TYPE (to
)))
1236 to_data
= gfc_class_data_get (to
);
1238 to_len
= gfc_class_len_get (to
);
1241 /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to. */
1244 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data
)))
1246 stmtblock_t loopbody
;
1250 tree orig_nelems
= nelems
; /* Needed for bounds check. */
1252 gfc_init_block (&body
);
1253 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
1254 gfc_array_index_type
, nelems
,
1255 gfc_index_one_node
);
1256 nelems
= gfc_evaluate_now (tmp
, &body
);
1257 index
= gfc_create_var (gfc_array_index_type
, "S");
1261 from_ref
= gfc_get_class_array_ref (index
, from
, from_data
);
1262 vec_safe_push (args
, from_ref
);
1265 vec_safe_push (args
, from_data
);
1268 to_ref
= gfc_get_class_array_ref (index
, to
, to_data
);
1271 tmp
= gfc_conv_array_data (to
);
1272 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
1273 to_ref
= gfc_build_addr_expr (NULL_TREE
,
1274 gfc_build_array_ref (tmp
, index
, to
));
1276 vec_safe_push (args
, to_ref
);
1278 /* Add bounds check. */
1279 if ((gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
) > 0 && is_from_desc
)
1282 const char *name
= "<<unknown>>";
1286 name
= (const char *)(DECL_NAME (to
)->identifier
.id
.str
);
1288 from_len
= gfc_conv_descriptor_size (from_data
, 1);
1289 tmp
= fold_build2_loc (input_location
, NE_EXPR
,
1290 boolean_type_node
, from_len
, orig_nelems
);
1291 msg
= xasprintf ("Array bound mismatch for dimension %d "
1292 "of array '%s' (%%ld/%%ld)",
1295 gfc_trans_runtime_check (true, false, tmp
, &body
,
1296 &gfc_current_locus
, msg
,
1297 fold_convert (long_integer_type_node
, orig_nelems
),
1298 fold_convert (long_integer_type_node
, from_len
));
1303 tmp
= build_call_vec (fcn_type
, fcn
, args
);
1305 /* Build the body of the loop. */
1306 gfc_init_block (&loopbody
);
1307 gfc_add_expr_to_block (&loopbody
, tmp
);
1309 /* Build the loop and return. */
1310 gfc_init_loopinfo (&loop
);
1312 loop
.from
[0] = gfc_index_zero_node
;
1313 loop
.loopvar
[0] = index
;
1314 loop
.to
[0] = nelems
;
1315 gfc_trans_scalarizing_loops (&loop
, &loopbody
);
1316 gfc_init_block (&ifbody
);
1317 gfc_add_block_to_block (&ifbody
, &loop
.pre
);
1318 stdcopy
= gfc_finish_block (&ifbody
);
1319 /* In initialization mode from_len is a constant zero. */
1320 if (unlimited
&& !integer_zerop (from_len
))
1322 vec_safe_push (args
, from_len
);
1323 vec_safe_push (args
, to_len
);
1324 tmp
= build_call_vec (fcn_type
, fcn
, args
);
1325 /* Build the body of the loop. */
1326 gfc_init_block (&loopbody
);
1327 gfc_add_expr_to_block (&loopbody
, tmp
);
1329 /* Build the loop and return. */
1330 gfc_init_loopinfo (&loop
);
1332 loop
.from
[0] = gfc_index_zero_node
;
1333 loop
.loopvar
[0] = index
;
1334 loop
.to
[0] = nelems
;
1335 gfc_trans_scalarizing_loops (&loop
, &loopbody
);
1336 gfc_init_block (&ifbody
);
1337 gfc_add_block_to_block (&ifbody
, &loop
.pre
);
1338 extcopy
= gfc_finish_block (&ifbody
);
1340 tmp
= fold_build2_loc (input_location
, GT_EXPR
,
1341 boolean_type_node
, from_len
,
1343 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
1344 void_type_node
, tmp
, extcopy
, stdcopy
);
1345 gfc_add_expr_to_block (&body
, tmp
);
1346 tmp
= gfc_finish_block (&body
);
1350 gfc_add_expr_to_block (&body
, stdcopy
);
1351 tmp
= gfc_finish_block (&body
);
1353 gfc_cleanup_loop (&loop
);
1357 gcc_assert (!is_from_desc
);
1358 vec_safe_push (args
, from_data
);
1359 vec_safe_push (args
, to_data
);
1360 stdcopy
= build_call_vec (fcn_type
, fcn
, args
);
1362 /* In initialization mode from_len is a constant zero. */
1363 if (unlimited
&& !integer_zerop (from_len
))
1365 vec_safe_push (args
, from_len
);
1366 vec_safe_push (args
, to_len
);
1367 extcopy
= build_call_vec (fcn_type
, fcn
, args
);
1368 tmp
= fold_build2_loc (input_location
, GT_EXPR
,
1369 boolean_type_node
, from_len
,
1371 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
1372 void_type_node
, tmp
, extcopy
, stdcopy
);
1378 /* Only copy _def_init to to_data, when it is not a NULL-pointer. */
1379 if (from
== NULL_TREE
)
1382 cond
= fold_build2_loc (input_location
, NE_EXPR
,
1384 from_data
, null_pointer_node
);
1385 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
1386 void_type_node
, cond
,
1387 tmp
, build_empty_stmt (input_location
));
1395 gfc_trans_class_array_init_assign (gfc_expr
*rhs
, gfc_expr
*lhs
, gfc_expr
*obj
)
1397 gfc_actual_arglist
*actual
;
1402 actual
= gfc_get_actual_arglist ();
1403 actual
->expr
= gfc_copy_expr (rhs
);
1404 actual
->next
= gfc_get_actual_arglist ();
1405 actual
->next
->expr
= gfc_copy_expr (lhs
);
1406 ppc
= gfc_copy_expr (obj
);
1407 gfc_add_vptr_component (ppc
);
1408 gfc_add_component_ref (ppc
, "_copy");
1409 ppc_code
= gfc_get_code (EXEC_CALL
);
1410 ppc_code
->resolved_sym
= ppc
->symtree
->n
.sym
;
1411 /* Although '_copy' is set to be elemental in class.c, it is
1412 not staying that way. Find out why, sometime.... */
1413 ppc_code
->resolved_sym
->attr
.elemental
= 1;
1414 ppc_code
->ext
.actual
= actual
;
1415 ppc_code
->expr1
= ppc
;
1416 /* Since '_copy' is elemental, the scalarizer will take care
1417 of arrays in gfc_trans_call. */
1418 res
= gfc_trans_call (ppc_code
, false, NULL
, NULL
, false);
1419 gfc_free_statements (ppc_code
);
1421 if (UNLIMITED_POLY(obj
))
1423 /* Check if rhs is non-NULL. */
1425 gfc_init_se (&src
, NULL
);
1426 gfc_conv_expr (&src
, rhs
);
1427 src
.expr
= gfc_build_addr_expr (NULL_TREE
, src
.expr
);
1428 tree cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
1429 src
.expr
, fold_convert (TREE_TYPE (src
.expr
),
1430 null_pointer_node
));
1431 res
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (res
), cond
, res
,
1432 build_empty_stmt (input_location
));
1438 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
1439 A MEMCPY is needed to copy the full data from the default initializer
1440 of the dynamic type. */
1443 gfc_trans_class_init_assign (gfc_code
*code
)
1447 gfc_se dst
,src
,memsz
;
1448 gfc_expr
*lhs
, *rhs
, *sz
;
1450 gfc_start_block (&block
);
1452 lhs
= gfc_copy_expr (code
->expr1
);
1453 gfc_add_data_component (lhs
);
1455 rhs
= gfc_copy_expr (code
->expr1
);
1456 gfc_add_vptr_component (rhs
);
1458 /* Make sure that the component backend_decls have been built, which
1459 will not have happened if the derived types concerned have not
1461 gfc_get_derived_type (rhs
->ts
.u
.derived
);
1462 gfc_add_def_init_component (rhs
);
1463 /* The _def_init is always scalar. */
1466 if (code
->expr1
->ts
.type
== BT_CLASS
1467 && CLASS_DATA (code
->expr1
)->attr
.dimension
)
1469 gfc_array_spec
*tmparr
= gfc_get_array_spec ();
1470 *tmparr
= *CLASS_DATA (code
->expr1
)->as
;
1471 gfc_add_full_array_ref (lhs
, tmparr
);
1472 tmp
= gfc_trans_class_array_init_assign (rhs
, lhs
, code
->expr1
);
1476 sz
= gfc_copy_expr (code
->expr1
);
1477 gfc_add_vptr_component (sz
);
1478 gfc_add_size_component (sz
);
1480 gfc_init_se (&dst
, NULL
);
1481 gfc_init_se (&src
, NULL
);
1482 gfc_init_se (&memsz
, NULL
);
1483 gfc_conv_expr (&dst
, lhs
);
1484 gfc_conv_expr (&src
, rhs
);
1485 gfc_conv_expr (&memsz
, sz
);
1486 gfc_add_block_to_block (&block
, &src
.pre
);
1487 src
.expr
= gfc_build_addr_expr (NULL_TREE
, src
.expr
);
1489 tmp
= gfc_build_memcpy_call (dst
.expr
, src
.expr
, memsz
.expr
);
1491 if (UNLIMITED_POLY(code
->expr1
))
1493 /* Check if _def_init is non-NULL. */
1494 tree cond
= fold_build2_loc (input_location
, NE_EXPR
,
1495 boolean_type_node
, src
.expr
,
1496 fold_convert (TREE_TYPE (src
.expr
),
1497 null_pointer_node
));
1498 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
), cond
,
1499 tmp
, build_empty_stmt (input_location
));
1503 if (code
->expr1
->symtree
->n
.sym
->attr
.optional
1504 || code
->expr1
->symtree
->n
.sym
->ns
->proc_name
->attr
.entry_master
)
1506 tree present
= gfc_conv_expr_present (code
->expr1
->symtree
->n
.sym
);
1507 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
),
1509 build_empty_stmt (input_location
));
1512 gfc_add_expr_to_block (&block
, tmp
);
1514 return gfc_finish_block (&block
);
1518 /* End of prototype trans-class.c */
1522 realloc_lhs_warning (bt type
, bool array
, locus
*where
)
1524 if (array
&& type
!= BT_CLASS
&& type
!= BT_DERIVED
&& warn_realloc_lhs
)
1525 gfc_warning (OPT_Wrealloc_lhs
,
1526 "Code for reallocating the allocatable array at %L will "
1528 else if (warn_realloc_lhs_all
)
1529 gfc_warning (OPT_Wrealloc_lhs_all
,
1530 "Code for reallocating the allocatable variable at %L "
1531 "will be added", where
);
1535 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping
*,
1538 /* Copy the scalarization loop variables. */
1541 gfc_copy_se_loopvars (gfc_se
* dest
, gfc_se
* src
)
1544 dest
->loop
= src
->loop
;
1548 /* Initialize a simple expression holder.
1550 Care must be taken when multiple se are created with the same parent.
1551 The child se must be kept in sync. The easiest way is to delay creation
1552 of a child se until after after the previous se has been translated. */
1555 gfc_init_se (gfc_se
* se
, gfc_se
* parent
)
1557 memset (se
, 0, sizeof (gfc_se
));
1558 gfc_init_block (&se
->pre
);
1559 gfc_init_block (&se
->post
);
1561 se
->parent
= parent
;
1564 gfc_copy_se_loopvars (se
, parent
);
1568 /* Advances to the next SS in the chain. Use this rather than setting
1569 se->ss = se->ss->next because all the parents needs to be kept in sync.
1573 gfc_advance_se_ss_chain (gfc_se
* se
)
1578 gcc_assert (se
!= NULL
&& se
->ss
!= NULL
&& se
->ss
!= gfc_ss_terminator
);
1581 /* Walk down the parent chain. */
1584 /* Simple consistency check. */
1585 gcc_assert (p
->parent
== NULL
|| p
->parent
->ss
== p
->ss
1586 || p
->parent
->ss
->nested_ss
== p
->ss
);
1588 /* If we were in a nested loop, the next scalarized expression can be
1589 on the parent ss' next pointer. Thus we should not take the next
1590 pointer blindly, but rather go up one nest level as long as next
1591 is the end of chain. */
1593 while (ss
->next
== gfc_ss_terminator
&& ss
->parent
!= NULL
)
1603 /* Ensures the result of the expression as either a temporary variable
1604 or a constant so that it can be used repeatedly. */
1607 gfc_make_safe_expr (gfc_se
* se
)
1611 if (CONSTANT_CLASS_P (se
->expr
))
1614 /* We need a temporary for this result. */
1615 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
1616 gfc_add_modify (&se
->pre
, var
, se
->expr
);
1621 /* Return an expression which determines if a dummy parameter is present.
1622 Also used for arguments to procedures with multiple entry points. */
1625 gfc_conv_expr_present (gfc_symbol
* sym
)
1629 gcc_assert (sym
->attr
.dummy
);
1630 decl
= gfc_get_symbol_decl (sym
);
1632 /* Intrinsic scalars with VALUE attribute which are passed by value
1633 use a hidden argument to denote the present status. */
1634 if (sym
->attr
.value
&& sym
->ts
.type
!= BT_CHARACTER
1635 && sym
->ts
.type
!= BT_CLASS
&& sym
->ts
.type
!= BT_DERIVED
1636 && !sym
->attr
.dimension
)
1638 char name
[GFC_MAX_SYMBOL_LEN
+ 2];
1641 gcc_assert (TREE_CODE (decl
) == PARM_DECL
);
1643 strcpy (&name
[1], sym
->name
);
1644 tree_name
= get_identifier (name
);
1646 /* Walk function argument list to find hidden arg. */
1647 cond
= DECL_ARGUMENTS (DECL_CONTEXT (decl
));
1648 for ( ; cond
!= NULL_TREE
; cond
= TREE_CHAIN (cond
))
1649 if (DECL_NAME (cond
) == tree_name
)
1656 if (TREE_CODE (decl
) != PARM_DECL
)
1658 /* Array parameters use a temporary descriptor, we want the real
1660 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl
))
1661 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl
)));
1662 decl
= GFC_DECL_SAVED_DESCRIPTOR (decl
);
1665 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
, decl
,
1666 fold_convert (TREE_TYPE (decl
), null_pointer_node
));
1668 /* Fortran 2008 allows to pass null pointers and non-associated pointers
1669 as actual argument to denote absent dummies. For array descriptors,
1670 we thus also need to check the array descriptor. For BT_CLASS, it
1671 can also occur for scalars and F2003 due to type->class wrapping and
1672 class->class wrapping. Note further that BT_CLASS always uses an
1673 array descriptor for arrays, also for explicit-shape/assumed-size. */
1675 if (!sym
->attr
.allocatable
1676 && ((sym
->ts
.type
!= BT_CLASS
&& !sym
->attr
.pointer
)
1677 || (sym
->ts
.type
== BT_CLASS
1678 && !CLASS_DATA (sym
)->attr
.allocatable
1679 && !CLASS_DATA (sym
)->attr
.class_pointer
))
1680 && ((gfc_option
.allow_std
& GFC_STD_F2008
) != 0
1681 || sym
->ts
.type
== BT_CLASS
))
1685 if ((sym
->as
&& (sym
->as
->type
== AS_ASSUMED_SHAPE
1686 || sym
->as
->type
== AS_ASSUMED_RANK
1687 || sym
->attr
.codimension
))
1688 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->as
))
1690 tmp
= build_fold_indirect_ref_loc (input_location
, decl
);
1691 if (sym
->ts
.type
== BT_CLASS
)
1692 tmp
= gfc_class_data_get (tmp
);
1693 tmp
= gfc_conv_array_data (tmp
);
1695 else if (sym
->ts
.type
== BT_CLASS
)
1696 tmp
= gfc_class_data_get (decl
);
1700 if (tmp
!= NULL_TREE
)
1702 tmp
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
, tmp
,
1703 fold_convert (TREE_TYPE (tmp
), null_pointer_node
));
1704 cond
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
1705 boolean_type_node
, cond
, tmp
);
1713 /* Converts a missing, dummy argument into a null or zero. */
1716 gfc_conv_missing_dummy (gfc_se
* se
, gfc_expr
* arg
, gfc_typespec ts
, int kind
)
1721 present
= gfc_conv_expr_present (arg
->symtree
->n
.sym
);
1725 /* Create a temporary and convert it to the correct type. */
1726 tmp
= gfc_get_int_type (kind
);
1727 tmp
= fold_convert (tmp
, build_fold_indirect_ref_loc (input_location
,
1730 /* Test for a NULL value. */
1731 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (tmp
), present
,
1732 tmp
, fold_convert (TREE_TYPE (tmp
), integer_one_node
));
1733 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
1734 se
->expr
= gfc_build_addr_expr (NULL_TREE
, tmp
);
1738 tmp
= build3_loc (input_location
, COND_EXPR
, TREE_TYPE (se
->expr
),
1740 build_zero_cst (TREE_TYPE (se
->expr
)));
1741 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
1745 if (ts
.type
== BT_CHARACTER
)
1747 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
1748 tmp
= fold_build3_loc (input_location
, COND_EXPR
, gfc_charlen_type_node
,
1749 present
, se
->string_length
, tmp
);
1750 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
1751 se
->string_length
= tmp
;
1757 /* Get the character length of an expression, looking through gfc_refs
1761 gfc_get_expr_charlen (gfc_expr
*e
)
1766 gcc_assert (e
->expr_type
== EXPR_VARIABLE
1767 && e
->ts
.type
== BT_CHARACTER
);
1769 length
= NULL
; /* To silence compiler warning. */
1771 if (is_subref_array (e
) && e
->ts
.u
.cl
->length
)
1774 gfc_init_se (&tmpse
, NULL
);
1775 gfc_conv_expr_type (&tmpse
, e
->ts
.u
.cl
->length
, gfc_charlen_type_node
);
1776 e
->ts
.u
.cl
->backend_decl
= tmpse
.expr
;
1780 /* First candidate: if the variable is of type CHARACTER, the
1781 expression's length could be the length of the character
1783 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
)
1784 length
= e
->symtree
->n
.sym
->ts
.u
.cl
->backend_decl
;
1786 /* Look through the reference chain for component references. */
1787 for (r
= e
->ref
; r
; r
= r
->next
)
1792 if (r
->u
.c
.component
->ts
.type
== BT_CHARACTER
)
1793 length
= r
->u
.c
.component
->ts
.u
.cl
->backend_decl
;
1801 /* We should never got substring references here. These will be
1802 broken down by the scalarizer. */
1808 gcc_assert (length
!= NULL
);
1813 /* Return for an expression the backend decl of the coarray. */
1816 gfc_get_tree_for_caf_expr (gfc_expr
*expr
)
1822 gcc_assert (expr
&& expr
->expr_type
== EXPR_VARIABLE
);
1824 /* Not-implemented diagnostic. */
1825 if (expr
->symtree
->n
.sym
->ts
.type
== BT_CLASS
1826 && UNLIMITED_POLY (expr
->symtree
->n
.sym
)
1827 && CLASS_DATA (expr
->symtree
->n
.sym
)->attr
.codimension
)
1828 gfc_error ("Sorry, coindexed access to an unlimited polymorphic object at "
1829 "%L is not supported", &expr
->where
);
1831 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1832 if (ref
->type
== REF_COMPONENT
)
1834 if (ref
->u
.c
.component
->ts
.type
== BT_CLASS
1835 && UNLIMITED_POLY (ref
->u
.c
.component
)
1836 && CLASS_DATA (ref
->u
.c
.component
)->attr
.codimension
)
1837 gfc_error ("Sorry, coindexed access to an unlimited polymorphic "
1838 "component at %L is not supported", &expr
->where
);
1841 /* Make sure the backend_decl is present before accessing it. */
1842 caf_decl
= expr
->symtree
->n
.sym
->backend_decl
== NULL_TREE
1843 ? gfc_get_symbol_decl (expr
->symtree
->n
.sym
)
1844 : expr
->symtree
->n
.sym
->backend_decl
;
1846 if (expr
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
1848 if (expr
->ref
&& expr
->ref
->type
== REF_ARRAY
)
1850 caf_decl
= gfc_class_data_get (caf_decl
);
1851 if (CLASS_DATA (expr
->symtree
->n
.sym
)->attr
.codimension
)
1854 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1856 if (ref
->type
== REF_COMPONENT
1857 && strcmp (ref
->u
.c
.component
->name
, "_data") != 0)
1859 caf_decl
= gfc_class_data_get (caf_decl
);
1860 if (CLASS_DATA (expr
->symtree
->n
.sym
)->attr
.codimension
)
1864 else if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.dimen
)
1868 if (expr
->symtree
->n
.sym
->attr
.codimension
)
1871 /* The following code assumes that the coarray is a component reachable via
1872 only scalar components/variables; the Fortran standard guarantees this. */
1874 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1875 if (ref
->type
== REF_COMPONENT
)
1877 gfc_component
*comp
= ref
->u
.c
.component
;
1879 if (POINTER_TYPE_P (TREE_TYPE (caf_decl
)))
1880 caf_decl
= build_fold_indirect_ref_loc (input_location
, caf_decl
);
1881 caf_decl
= fold_build3_loc (input_location
, COMPONENT_REF
,
1882 TREE_TYPE (comp
->backend_decl
), caf_decl
,
1883 comp
->backend_decl
, NULL_TREE
);
1884 if (comp
->ts
.type
== BT_CLASS
)
1886 caf_decl
= gfc_class_data_get (caf_decl
);
1887 if (CLASS_DATA (comp
)->attr
.codimension
)
1893 if (comp
->attr
.codimension
)
1899 gcc_assert (found
&& caf_decl
);
1904 /* Obtain the Coarray token - and optionally also the offset. */
1907 gfc_get_caf_token_offset (gfc_se
*se
, tree
*token
, tree
*offset
, tree caf_decl
,
1908 tree se_expr
, gfc_expr
*expr
)
1912 /* Coarray token. */
1913 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl
)))
1915 gcc_assert (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl
))
1916 == GFC_ARRAY_ALLOCATABLE
1917 || expr
->symtree
->n
.sym
->attr
.select_type_temporary
);
1918 *token
= gfc_conv_descriptor_token (caf_decl
);
1920 else if (DECL_LANG_SPECIFIC (caf_decl
)
1921 && GFC_DECL_TOKEN (caf_decl
) != NULL_TREE
)
1922 *token
= GFC_DECL_TOKEN (caf_decl
);
1925 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl
))
1926 && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl
)) != NULL_TREE
);
1927 *token
= GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl
));
1933 /* Offset between the coarray base address and the address wanted. */
1934 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl
))
1935 && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl
)) == GFC_ARRAY_ALLOCATABLE
1936 || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl
)) == GFC_ARRAY_POINTER
))
1937 *offset
= build_int_cst (gfc_array_index_type
, 0);
1938 else if (DECL_LANG_SPECIFIC (caf_decl
)
1939 && GFC_DECL_CAF_OFFSET (caf_decl
) != NULL_TREE
)
1940 *offset
= GFC_DECL_CAF_OFFSET (caf_decl
);
1941 else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl
)) != NULL_TREE
)
1942 *offset
= GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl
));
1944 *offset
= build_int_cst (gfc_array_index_type
, 0);
1946 if (POINTER_TYPE_P (TREE_TYPE (se_expr
))
1947 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr
))))
1949 tmp
= build_fold_indirect_ref_loc (input_location
, se_expr
);
1950 tmp
= gfc_conv_descriptor_data_get (tmp
);
1952 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr
)))
1953 tmp
= gfc_conv_descriptor_data_get (se_expr
);
1956 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr
)));
1960 *offset
= fold_build2_loc (input_location
, PLUS_EXPR
, gfc_array_index_type
,
1961 *offset
, fold_convert (gfc_array_index_type
, tmp
));
1963 if (expr
->symtree
->n
.sym
->ts
.type
== BT_DERIVED
1964 && expr
->symtree
->n
.sym
->attr
.codimension
1965 && expr
->symtree
->n
.sym
->ts
.u
.derived
->attr
.alloc_comp
)
1967 gfc_expr
*base_expr
= gfc_copy_expr (expr
);
1968 gfc_ref
*ref
= base_expr
->ref
;
1971 // Iterate through the refs until the last one.
1975 if (ref
->type
== REF_ARRAY
1976 && ref
->u
.ar
.type
!= AR_FULL
)
1978 const int ranksum
= ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
;
1980 for (i
= 0; i
< ranksum
; ++i
)
1982 ref
->u
.ar
.start
[i
] = NULL
;
1983 ref
->u
.ar
.end
[i
] = NULL
;
1985 ref
->u
.ar
.type
= AR_FULL
;
1987 gfc_init_se (&base_se
, NULL
);
1988 if (gfc_caf_attr (base_expr
).dimension
)
1990 gfc_conv_expr_descriptor (&base_se
, base_expr
);
1991 tmp
= gfc_conv_descriptor_data_get (base_se
.expr
);
1995 gfc_conv_expr (&base_se
, base_expr
);
1999 gfc_free_expr (base_expr
);
2000 gfc_add_block_to_block (&se
->pre
, &base_se
.pre
);
2001 gfc_add_block_to_block (&se
->post
, &base_se
.post
);
2003 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl
)))
2004 tmp
= gfc_conv_descriptor_data_get (caf_decl
);
2007 gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl
)));
2011 *offset
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
2012 fold_convert (gfc_array_index_type
, *offset
),
2013 fold_convert (gfc_array_index_type
, tmp
));
2017 /* Convert the coindex of a coarray into an image index; the result is
2018 image_num = (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
2019 + (idx(3)-lcobound(3))*extend(1)*extent(2) + ... */
2022 gfc_caf_get_image_index (stmtblock_t
*block
, gfc_expr
*e
, tree desc
)
2025 tree lbound
, ubound
, extent
, tmp
, img_idx
;
2029 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
2030 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
2032 gcc_assert (ref
!= NULL
);
2034 if (ref
->u
.ar
.dimen_type
[ref
->u
.ar
.dimen
] == DIMEN_THIS_IMAGE
)
2036 return build_call_expr_loc (input_location
, gfor_fndecl_caf_this_image
, 1,
2040 img_idx
= integer_zero_node
;
2041 extent
= integer_one_node
;
2042 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc
)))
2043 for (i
= ref
->u
.ar
.dimen
; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
2045 gfc_init_se (&se
, NULL
);
2046 gfc_conv_expr_type (&se
, ref
->u
.ar
.start
[i
], integer_type_node
);
2047 gfc_add_block_to_block (block
, &se
.pre
);
2048 lbound
= gfc_conv_descriptor_lbound_get (desc
, gfc_rank_cst
[i
]);
2049 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
2050 integer_type_node
, se
.expr
,
2051 fold_convert(integer_type_node
, lbound
));
2052 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, integer_type_node
,
2054 img_idx
= fold_build2_loc (input_location
, PLUS_EXPR
, integer_type_node
,
2056 if (i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
- 1)
2058 ubound
= gfc_conv_descriptor_ubound_get (desc
, gfc_rank_cst
[i
]);
2059 tmp
= gfc_conv_array_extent_dim (lbound
, ubound
, NULL
);
2060 tmp
= fold_convert (integer_type_node
, tmp
);
2061 extent
= fold_build2_loc (input_location
, MULT_EXPR
,
2062 integer_type_node
, extent
, tmp
);
2066 for (i
= ref
->u
.ar
.dimen
; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
2068 gfc_init_se (&se
, NULL
);
2069 gfc_conv_expr_type (&se
, ref
->u
.ar
.start
[i
], integer_type_node
);
2070 gfc_add_block_to_block (block
, &se
.pre
);
2071 lbound
= GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc
), i
);
2072 lbound
= fold_convert (integer_type_node
, lbound
);
2073 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
2074 integer_type_node
, se
.expr
, lbound
);
2075 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, integer_type_node
,
2077 img_idx
= fold_build2_loc (input_location
, PLUS_EXPR
, integer_type_node
,
2079 if (i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
- 1)
2081 ubound
= GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc
), i
);
2082 ubound
= fold_convert (integer_type_node
, ubound
);
2083 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
2084 integer_type_node
, ubound
, lbound
);
2085 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
, integer_type_node
,
2086 tmp
, integer_one_node
);
2087 extent
= fold_build2_loc (input_location
, MULT_EXPR
,
2088 integer_type_node
, extent
, tmp
);
2091 img_idx
= fold_build2_loc (input_location
, PLUS_EXPR
, integer_type_node
,
2092 img_idx
, integer_one_node
);
2097 /* For each character array constructor subexpression without a ts.u.cl->length,
2098 replace it by its first element (if there aren't any elements, the length
2099 should already be set to zero). */
2102 flatten_array_ctors_without_strlen (gfc_expr
* e
)
2104 gfc_actual_arglist
* arg
;
2110 switch (e
->expr_type
)
2114 flatten_array_ctors_without_strlen (e
->value
.op
.op1
);
2115 flatten_array_ctors_without_strlen (e
->value
.op
.op2
);
2119 /* TODO: Implement as with EXPR_FUNCTION when needed. */
2123 for (arg
= e
->value
.function
.actual
; arg
; arg
= arg
->next
)
2124 flatten_array_ctors_without_strlen (arg
->expr
);
2129 /* We've found what we're looking for. */
2130 if (e
->ts
.type
== BT_CHARACTER
&& !e
->ts
.u
.cl
->length
)
2135 gcc_assert (e
->value
.constructor
);
2137 c
= gfc_constructor_first (e
->value
.constructor
);
2141 flatten_array_ctors_without_strlen (new_expr
);
2142 gfc_replace_expr (e
, new_expr
);
2146 /* Otherwise, fall through to handle constructor elements. */
2148 case EXPR_STRUCTURE
:
2149 for (c
= gfc_constructor_first (e
->value
.constructor
);
2150 c
; c
= gfc_constructor_next (c
))
2151 flatten_array_ctors_without_strlen (c
->expr
);
2161 /* Generate code to initialize a string length variable. Returns the
2162 value. For array constructors, cl->length might be NULL and in this case,
2163 the first element of the constructor is needed. expr is the original
2164 expression so we can access it but can be NULL if this is not needed. */
2167 gfc_conv_string_length (gfc_charlen
* cl
, gfc_expr
* expr
, stmtblock_t
* pblock
)
2171 gfc_init_se (&se
, NULL
);
2173 if (!cl
->length
&& cl
->backend_decl
&& VAR_P (cl
->backend_decl
))
2176 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
2177 "flatten" array constructors by taking their first element; all elements
2178 should be the same length or a cl->length should be present. */
2181 gfc_expr
* expr_flat
;
2183 expr_flat
= gfc_copy_expr (expr
);
2184 flatten_array_ctors_without_strlen (expr_flat
);
2185 gfc_resolve_expr (expr_flat
);
2187 gfc_conv_expr (&se
, expr_flat
);
2188 gfc_add_block_to_block (pblock
, &se
.pre
);
2189 cl
->backend_decl
= convert (gfc_charlen_type_node
, se
.string_length
);
2191 gfc_free_expr (expr_flat
);
2195 /* Convert cl->length. */
2197 gcc_assert (cl
->length
);
2199 gfc_conv_expr_type (&se
, cl
->length
, gfc_charlen_type_node
);
2200 se
.expr
= fold_build2_loc (input_location
, MAX_EXPR
, gfc_charlen_type_node
,
2201 se
.expr
, build_int_cst (gfc_charlen_type_node
, 0));
2202 gfc_add_block_to_block (pblock
, &se
.pre
);
2204 if (cl
->backend_decl
)
2205 gfc_add_modify (pblock
, cl
->backend_decl
, se
.expr
);
2207 cl
->backend_decl
= gfc_evaluate_now (se
.expr
, pblock
);
2212 gfc_conv_substring (gfc_se
* se
, gfc_ref
* ref
, int kind
,
2213 const char *name
, locus
*where
)
2223 type
= gfc_get_character_type (kind
, ref
->u
.ss
.length
);
2224 type
= build_pointer_type (type
);
2226 gfc_init_se (&start
, se
);
2227 gfc_conv_expr_type (&start
, ref
->u
.ss
.start
, gfc_charlen_type_node
);
2228 gfc_add_block_to_block (&se
->pre
, &start
.pre
);
2230 if (integer_onep (start
.expr
))
2231 gfc_conv_string_parameter (se
);
2236 /* Avoid multiple evaluation of substring start. */
2237 if (!CONSTANT_CLASS_P (tmp
) && !DECL_P (tmp
))
2238 start
.expr
= gfc_evaluate_now (start
.expr
, &se
->pre
);
2240 /* Change the start of the string. */
2241 if (TYPE_STRING_FLAG (TREE_TYPE (se
->expr
)))
2244 tmp
= build_fold_indirect_ref_loc (input_location
,
2246 tmp
= gfc_build_array_ref (tmp
, start
.expr
, NULL
);
2247 se
->expr
= gfc_build_addr_expr (type
, tmp
);
2250 /* Length = end + 1 - start. */
2251 gfc_init_se (&end
, se
);
2252 if (ref
->u
.ss
.end
== NULL
)
2253 end
.expr
= se
->string_length
;
2256 gfc_conv_expr_type (&end
, ref
->u
.ss
.end
, gfc_charlen_type_node
);
2257 gfc_add_block_to_block (&se
->pre
, &end
.pre
);
2261 if (!CONSTANT_CLASS_P (tmp
) && !DECL_P (tmp
))
2262 end
.expr
= gfc_evaluate_now (end
.expr
, &se
->pre
);
2264 if (gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
)
2266 tree nonempty
= fold_build2_loc (input_location
, LE_EXPR
,
2267 boolean_type_node
, start
.expr
,
2270 /* Check lower bound. */
2271 fault
= fold_build2_loc (input_location
, LT_EXPR
, boolean_type_node
,
2273 build_int_cst (gfc_charlen_type_node
, 1));
2274 fault
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
2275 boolean_type_node
, nonempty
, fault
);
2277 msg
= xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
2278 "is less than one", name
);
2280 msg
= xasprintf ("Substring out of bounds: lower bound (%%ld) "
2281 "is less than one");
2282 gfc_trans_runtime_check (true, false, fault
, &se
->pre
, where
, msg
,
2283 fold_convert (long_integer_type_node
,
2287 /* Check upper bound. */
2288 fault
= fold_build2_loc (input_location
, GT_EXPR
, boolean_type_node
,
2289 end
.expr
, se
->string_length
);
2290 fault
= fold_build2_loc (input_location
, TRUTH_ANDIF_EXPR
,
2291 boolean_type_node
, nonempty
, fault
);
2293 msg
= xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
2294 "exceeds string length (%%ld)", name
);
2296 msg
= xasprintf ("Substring out of bounds: upper bound (%%ld) "
2297 "exceeds string length (%%ld)");
2298 gfc_trans_runtime_check (true, false, fault
, &se
->pre
, where
, msg
,
2299 fold_convert (long_integer_type_node
, end
.expr
),
2300 fold_convert (long_integer_type_node
,
2301 se
->string_length
));
2305 /* Try to calculate the length from the start and end expressions. */
2307 && gfc_dep_difference (ref
->u
.ss
.end
, ref
->u
.ss
.start
, &length
))
2311 i_len
= mpz_get_si (length
) + 1;
2315 tmp
= build_int_cst (gfc_charlen_type_node
, i_len
);
2316 mpz_clear (length
); /* Was initialized by gfc_dep_difference. */
2320 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_charlen_type_node
,
2321 end
.expr
, start
.expr
);
2322 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
, gfc_charlen_type_node
,
2323 build_int_cst (gfc_charlen_type_node
, 1), tmp
);
2324 tmp
= fold_build2_loc (input_location
, MAX_EXPR
, gfc_charlen_type_node
,
2325 tmp
, build_int_cst (gfc_charlen_type_node
, 0));
2328 se
->string_length
= tmp
;
2332 /* Convert a derived type component reference. */
2335 gfc_conv_component_ref (gfc_se
* se
, gfc_ref
* ref
)
2343 c
= ref
->u
.c
.component
;
2345 if (c
->backend_decl
== NULL_TREE
2346 && ref
->u
.c
.sym
!= NULL
)
2347 gfc_get_derived_type (ref
->u
.c
.sym
);
2349 field
= c
->backend_decl
;
2350 gcc_assert (field
&& TREE_CODE (field
) == FIELD_DECL
);
2352 context
= DECL_FIELD_CONTEXT (field
);
2354 /* Components can correspond to fields of different containing
2355 types, as components are created without context, whereas
2356 a concrete use of a component has the type of decl as context.
2357 So, if the type doesn't match, we search the corresponding
2358 FIELD_DECL in the parent type. To not waste too much time
2359 we cache this result in norestrict_decl.
2360 On the other hand, if the context is a UNION or a MAP (a
2361 RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL. */
2363 if (context
!= TREE_TYPE (decl
)
2364 && !( TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
/* Field is union */
2365 || TREE_CODE (context
) == UNION_TYPE
)) /* Field is map */
2367 tree f2
= c
->norestrict_decl
;
2368 if (!f2
|| DECL_FIELD_CONTEXT (f2
) != TREE_TYPE (decl
))
2369 for (f2
= TYPE_FIELDS (TREE_TYPE (decl
)); f2
; f2
= DECL_CHAIN (f2
))
2370 if (TREE_CODE (f2
) == FIELD_DECL
2371 && DECL_NAME (f2
) == DECL_NAME (field
))
2374 c
->norestrict_decl
= f2
;
2378 if (ref
->u
.c
.sym
&& ref
->u
.c
.sym
->ts
.type
== BT_CLASS
2379 && strcmp ("_data", c
->name
) == 0)
2381 /* Found a ref to the _data component. Store the associated ref to
2382 the vptr in se->class_vptr. */
2383 se
->class_vptr
= gfc_class_vptr_get (decl
);
2386 se
->class_vptr
= NULL_TREE
;
2388 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (field
),
2389 decl
, field
, NULL_TREE
);
2393 /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
2394 strlen () conditional below. */
2395 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.proc_pointer
2396 && !(c
->attr
.allocatable
&& c
->ts
.deferred
))
2398 tmp
= c
->ts
.u
.cl
->backend_decl
;
2399 /* Components must always be constant length. */
2400 gcc_assert (tmp
&& INTEGER_CST_P (tmp
));
2401 se
->string_length
= tmp
;
2404 if (gfc_deferred_strlen (c
, &field
))
2406 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
2408 decl
, field
, NULL_TREE
);
2409 se
->string_length
= tmp
;
2412 if (((c
->attr
.pointer
|| c
->attr
.allocatable
)
2413 && (!c
->attr
.dimension
&& !c
->attr
.codimension
)
2414 && c
->ts
.type
!= BT_CHARACTER
)
2415 || c
->attr
.proc_pointer
)
2416 se
->expr
= build_fold_indirect_ref_loc (input_location
,
2421 /* This function deals with component references to components of the
2422 parent type for derived type extensions. */
2424 conv_parent_component_references (gfc_se
* se
, gfc_ref
* ref
)
2432 c
= ref
->u
.c
.component
;
2434 /* Return if the component is in the parent type. */
2435 for (cmp
= dt
->components
; cmp
; cmp
= cmp
->next
)
2436 if (strcmp (c
->name
, cmp
->name
) == 0)
2439 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
2440 parent
.type
= REF_COMPONENT
;
2442 parent
.u
.c
.sym
= dt
;
2443 parent
.u
.c
.component
= dt
->components
;
2445 if (dt
->backend_decl
== NULL
)
2446 gfc_get_derived_type (dt
);
2448 /* Build the reference and call self. */
2449 gfc_conv_component_ref (se
, &parent
);
2450 parent
.u
.c
.sym
= dt
->components
->ts
.u
.derived
;
2451 parent
.u
.c
.component
= c
;
2452 conv_parent_component_references (se
, &parent
);
2455 /* Return the contents of a variable. Also handles reference/pointer
2456 variables (all Fortran pointer references are implicit). */
2459 gfc_conv_variable (gfc_se
* se
, gfc_expr
* expr
)
2464 tree parent_decl
= NULL_TREE
;
2467 bool alternate_entry
;
2470 bool first_time
= true;
2472 sym
= expr
->symtree
->n
.sym
;
2473 is_classarray
= IS_CLASS_ARRAY (sym
);
2477 gfc_ss_info
*ss_info
= ss
->info
;
2479 /* Check that something hasn't gone horribly wrong. */
2480 gcc_assert (ss
!= gfc_ss_terminator
);
2481 gcc_assert (ss_info
->expr
== expr
);
2483 /* A scalarized term. We already know the descriptor. */
2484 se
->expr
= ss_info
->data
.array
.descriptor
;
2485 se
->string_length
= ss_info
->string_length
;
2486 ref
= ss_info
->data
.array
.ref
;
2488 gcc_assert (ref
->type
== REF_ARRAY
2489 && ref
->u
.ar
.type
!= AR_ELEMENT
);
2491 gfc_conv_tmp_array_ref (se
);
2495 tree se_expr
= NULL_TREE
;
2497 se
->expr
= gfc_get_symbol_decl (sym
);
2499 /* Deal with references to a parent results or entries by storing
2500 the current_function_decl and moving to the parent_decl. */
2501 return_value
= sym
->attr
.function
&& sym
->result
== sym
;
2502 alternate_entry
= sym
->attr
.function
&& sym
->attr
.entry
2503 && sym
->result
== sym
;
2504 entry_master
= sym
->attr
.result
2505 && sym
->ns
->proc_name
->attr
.entry_master
2506 && !gfc_return_by_reference (sym
->ns
->proc_name
);
2507 if (current_function_decl
)
2508 parent_decl
= DECL_CONTEXT (current_function_decl
);
2510 if ((se
->expr
== parent_decl
&& return_value
)
2511 || (sym
->ns
&& sym
->ns
->proc_name
2513 && sym
->ns
->proc_name
->backend_decl
== parent_decl
2514 && (alternate_entry
|| entry_master
)))
2519 /* Special case for assigning the return value of a function.
2520 Self recursive functions must have an explicit return value. */
2521 if (return_value
&& (se
->expr
== current_function_decl
|| parent_flag
))
2522 se_expr
= gfc_get_fake_result_decl (sym
, parent_flag
);
2524 /* Similarly for alternate entry points. */
2525 else if (alternate_entry
2526 && (sym
->ns
->proc_name
->backend_decl
== current_function_decl
2529 gfc_entry_list
*el
= NULL
;
2531 for (el
= sym
->ns
->entries
; el
; el
= el
->next
)
2534 se_expr
= gfc_get_fake_result_decl (sym
, parent_flag
);
2539 else if (entry_master
2540 && (sym
->ns
->proc_name
->backend_decl
== current_function_decl
2542 se_expr
= gfc_get_fake_result_decl (sym
, parent_flag
);
2547 /* Procedure actual arguments. Look out for temporary variables
2548 with the same attributes as function values. */
2549 else if (!sym
->attr
.temporary
2550 && sym
->attr
.flavor
== FL_PROCEDURE
2551 && se
->expr
!= current_function_decl
)
2553 if (!sym
->attr
.dummy
&& !sym
->attr
.proc_pointer
)
2555 gcc_assert (TREE_CODE (se
->expr
) == FUNCTION_DECL
);
2556 se
->expr
= gfc_build_addr_expr (NULL_TREE
, se
->expr
);
2562 /* Dereference the expression, where needed. Since characters
2563 are entirely different from other types, they are treated
2565 if (sym
->ts
.type
== BT_CHARACTER
)
2567 /* Dereference character pointer dummy arguments
2569 if ((sym
->attr
.pointer
|| sym
->attr
.allocatable
)
2571 || sym
->attr
.function
2572 || sym
->attr
.result
))
2573 se
->expr
= build_fold_indirect_ref_loc (input_location
,
2577 else if (!sym
->attr
.value
)
2579 /* Dereference temporaries for class array dummy arguments. */
2580 if (sym
->attr
.dummy
&& is_classarray
2581 && GFC_ARRAY_TYPE_P (TREE_TYPE (se
->expr
)))
2583 if (!se
->descriptor_only
)
2584 se
->expr
= GFC_DECL_SAVED_DESCRIPTOR (se
->expr
);
2586 se
->expr
= build_fold_indirect_ref_loc (input_location
,
2590 /* Dereference non-character scalar dummy arguments. */
2591 if (sym
->attr
.dummy
&& !sym
->attr
.dimension
2592 && !(sym
->attr
.codimension
&& sym
->attr
.allocatable
)
2593 && (sym
->ts
.type
!= BT_CLASS
2594 || (!CLASS_DATA (sym
)->attr
.dimension
2595 && !(CLASS_DATA (sym
)->attr
.codimension
2596 && CLASS_DATA (sym
)->attr
.allocatable
))))
2597 se
->expr
= build_fold_indirect_ref_loc (input_location
,
2600 /* Dereference scalar hidden result. */
2601 if (flag_f2c
&& sym
->ts
.type
== BT_COMPLEX
2602 && (sym
->attr
.function
|| sym
->attr
.result
)
2603 && !sym
->attr
.dimension
&& !sym
->attr
.pointer
2604 && !sym
->attr
.always_explicit
)
2605 se
->expr
= build_fold_indirect_ref_loc (input_location
,
2608 /* Dereference non-character, non-class pointer variables.
2609 These must be dummies, results, or scalars. */
2611 && (sym
->attr
.pointer
|| sym
->attr
.allocatable
2612 || gfc_is_associate_pointer (sym
)
2613 || (sym
->as
&& sym
->as
->type
== AS_ASSUMED_RANK
))
2615 || sym
->attr
.function
2617 || (!sym
->attr
.dimension
2618 && (!sym
->attr
.codimension
|| !sym
->attr
.allocatable
))))
2619 se
->expr
= build_fold_indirect_ref_loc (input_location
,
2621 /* Now treat the class array pointer variables accordingly. */
2622 else if (sym
->ts
.type
== BT_CLASS
2624 && (CLASS_DATA (sym
)->attr
.dimension
2625 || CLASS_DATA (sym
)->attr
.codimension
)
2626 && ((CLASS_DATA (sym
)->as
2627 && CLASS_DATA (sym
)->as
->type
== AS_ASSUMED_RANK
)
2628 || CLASS_DATA (sym
)->attr
.allocatable
2629 || CLASS_DATA (sym
)->attr
.class_pointer
))
2630 se
->expr
= build_fold_indirect_ref_loc (input_location
,
2632 /* And the case where a non-dummy, non-result, non-function,
2633 non-allotable and non-pointer classarray is present. This case was
2634 previously covered by the first if, but with introducing the
2635 condition !is_classarray there, that case has to be covered
2637 else if (sym
->ts
.type
== BT_CLASS
2639 && !sym
->attr
.function
2640 && !sym
->attr
.result
2641 && (CLASS_DATA (sym
)->attr
.dimension
2642 || CLASS_DATA (sym
)->attr
.codimension
)
2644 || !CLASS_DATA (sym
)->attr
.allocatable
)
2645 && !CLASS_DATA (sym
)->attr
.class_pointer
)
2646 se
->expr
= build_fold_indirect_ref_loc (input_location
,
2653 /* For character variables, also get the length. */
2654 if (sym
->ts
.type
== BT_CHARACTER
)
2656 /* If the character length of an entry isn't set, get the length from
2657 the master function instead. */
2658 if (sym
->attr
.entry
&& !sym
->ts
.u
.cl
->backend_decl
)
2659 se
->string_length
= sym
->ns
->proc_name
->ts
.u
.cl
->backend_decl
;
2661 se
->string_length
= sym
->ts
.u
.cl
->backend_decl
;
2662 gcc_assert (se
->string_length
);
2670 /* Return the descriptor if that's what we want and this is an array
2671 section reference. */
2672 if (se
->descriptor_only
&& ref
->u
.ar
.type
!= AR_ELEMENT
)
2674 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
2675 /* Return the descriptor for array pointers and allocations. */
2676 if (se
->want_pointer
2677 && ref
->next
== NULL
&& (se
->descriptor_only
))
2680 gfc_conv_array_ref (se
, &ref
->u
.ar
, expr
, &expr
->where
);
2681 /* Return a pointer to an element. */
2685 if (first_time
&& is_classarray
&& sym
->attr
.dummy
2686 && se
->descriptor_only
2687 && !CLASS_DATA (sym
)->attr
.allocatable
2688 && !CLASS_DATA (sym
)->attr
.class_pointer
2689 && CLASS_DATA (sym
)->as
2690 && CLASS_DATA (sym
)->as
->type
!= AS_ASSUMED_RANK
2691 && strcmp ("_data", ref
->u
.c
.component
->name
) == 0)
2692 /* Skip the first ref of a _data component, because for class
2693 arrays that one is already done by introducing a temporary
2694 array descriptor. */
2697 if (ref
->u
.c
.sym
->attr
.extension
)
2698 conv_parent_component_references (se
, ref
);
2700 gfc_conv_component_ref (se
, ref
);
2701 if (!ref
->next
&& ref
->u
.c
.sym
->attr
.codimension
2702 && se
->want_pointer
&& se
->descriptor_only
)
2708 gfc_conv_substring (se
, ref
, expr
->ts
.kind
,
2709 expr
->symtree
->name
, &expr
->where
);
2719 /* Pointer assignment, allocation or pass by reference. Arrays are handled
2721 if (se
->want_pointer
)
2723 if (expr
->ts
.type
== BT_CHARACTER
&& !gfc_is_proc_ptr_comp (expr
))
2724 gfc_conv_string_parameter (se
);
2726 se
->expr
= gfc_build_addr_expr (NULL_TREE
, se
->expr
);
2731 /* Unary ops are easy... Or they would be if ! was a valid op. */
2734 gfc_conv_unary_op (enum tree_code code
, gfc_se
* se
, gfc_expr
* expr
)
2739 gcc_assert (expr
->ts
.type
!= BT_CHARACTER
);
2740 /* Initialize the operand. */
2741 gfc_init_se (&operand
, se
);
2742 gfc_conv_expr_val (&operand
, expr
->value
.op
.op1
);
2743 gfc_add_block_to_block (&se
->pre
, &operand
.pre
);
2745 type
= gfc_typenode_for_spec (&expr
->ts
);
2747 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
2748 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
2749 All other unary operators have an equivalent GIMPLE unary operator. */
2750 if (code
== TRUTH_NOT_EXPR
)
2751 se
->expr
= fold_build2_loc (input_location
, EQ_EXPR
, type
, operand
.expr
,
2752 build_int_cst (type
, 0));
2754 se
->expr
= fold_build1_loc (input_location
, code
, type
, operand
.expr
);
2758 /* Expand power operator to optimal multiplications when a value is raised
2759 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
2760 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
2761 Programming", 3rd Edition, 1998. */
2763 /* This code is mostly duplicated from expand_powi in the backend.
2764 We establish the "optimal power tree" lookup table with the defined size.
2765 The items in the table are the exponents used to calculate the index
2766 exponents. Any integer n less than the value can get an "addition chain",
2767 with the first node being one. */
2768 #define POWI_TABLE_SIZE 256
2770 /* The table is from builtins.c. */
2771 static const unsigned char powi_table
[POWI_TABLE_SIZE
] =
2773 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2774 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2775 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2776 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2777 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2778 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2779 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2780 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2781 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2782 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2783 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2784 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2785 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2786 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2787 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2788 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2789 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2790 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2791 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2792 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2793 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2794 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2795 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2796 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2797 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2798 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2799 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2800 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2801 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2802 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2803 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2804 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2807 /* If n is larger than lookup table's max index, we use the "window
2809 #define POWI_WINDOW_SIZE 3
2811 /* Recursive function to expand the power operator. The temporary
2812 values are put in tmpvar. The function returns tmpvar[1] ** n. */
2814 gfc_conv_powi (gfc_se
* se
, unsigned HOST_WIDE_INT n
, tree
* tmpvar
)
2821 if (n
< POWI_TABLE_SIZE
)
2826 op0
= gfc_conv_powi (se
, n
- powi_table
[n
], tmpvar
);
2827 op1
= gfc_conv_powi (se
, powi_table
[n
], tmpvar
);
2831 digit
= n
& ((1 << POWI_WINDOW_SIZE
) - 1);
2832 op0
= gfc_conv_powi (se
, n
- digit
, tmpvar
);
2833 op1
= gfc_conv_powi (se
, digit
, tmpvar
);
2837 op0
= gfc_conv_powi (se
, n
>> 1, tmpvar
);
2841 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, TREE_TYPE (op0
), op0
, op1
);
2842 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
2844 if (n
< POWI_TABLE_SIZE
)
2851 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
2852 return 1. Else return 0 and a call to runtime library functions
2853 will have to be built. */
2855 gfc_conv_cst_int_power (gfc_se
* se
, tree lhs
, tree rhs
)
2860 tree vartmp
[POWI_TABLE_SIZE
];
2862 unsigned HOST_WIDE_INT n
;
2864 wide_int wrhs
= rhs
;
2866 /* If exponent is too large, we won't expand it anyway, so don't bother
2867 with large integer values. */
2868 if (!wi::fits_shwi_p (wrhs
))
2871 m
= wrhs
.to_shwi ();
2872 /* Use the wide_int's routine to reliably get the absolute value on all
2873 platforms. Then convert it to a HOST_WIDE_INT like above. */
2874 n
= wi::abs (wrhs
).to_shwi ();
2876 type
= TREE_TYPE (lhs
);
2877 sgn
= tree_int_cst_sgn (rhs
);
2879 if (((FLOAT_TYPE_P (type
) && !flag_unsafe_math_optimizations
)
2880 || optimize_size
) && (m
> 2 || m
< -1))
2886 se
->expr
= gfc_build_const (type
, integer_one_node
);
2890 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
2891 if ((sgn
== -1) && (TREE_CODE (type
) == INTEGER_TYPE
))
2893 tmp
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
2894 lhs
, build_int_cst (TREE_TYPE (lhs
), -1));
2895 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
2896 lhs
, build_int_cst (TREE_TYPE (lhs
), 1));
2899 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
2902 tmp
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
2903 boolean_type_node
, tmp
, cond
);
2904 se
->expr
= fold_build3_loc (input_location
, COND_EXPR
, type
,
2905 tmp
, build_int_cst (type
, 1),
2906 build_int_cst (type
, 0));
2910 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
2911 tmp
= fold_build3_loc (input_location
, COND_EXPR
, type
, tmp
,
2912 build_int_cst (type
, -1),
2913 build_int_cst (type
, 0));
2914 se
->expr
= fold_build3_loc (input_location
, COND_EXPR
, type
,
2915 cond
, build_int_cst (type
, 1), tmp
);
2919 memset (vartmp
, 0, sizeof (vartmp
));
2923 tmp
= gfc_build_const (type
, integer_one_node
);
2924 vartmp
[1] = fold_build2_loc (input_location
, RDIV_EXPR
, type
, tmp
,
2928 se
->expr
= gfc_conv_powi (se
, n
, vartmp
);
2934 /* Power op (**). Constant integer exponent has special handling. */
2937 gfc_conv_power_op (gfc_se
* se
, gfc_expr
* expr
)
2939 tree gfc_int4_type_node
;
2942 int res_ikind_1
, res_ikind_2
;
2947 gfc_init_se (&lse
, se
);
2948 gfc_conv_expr_val (&lse
, expr
->value
.op
.op1
);
2949 lse
.expr
= gfc_evaluate_now (lse
.expr
, &lse
.pre
);
2950 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
2952 gfc_init_se (&rse
, se
);
2953 gfc_conv_expr_val (&rse
, expr
->value
.op
.op2
);
2954 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
2956 if (expr
->value
.op
.op2
->ts
.type
== BT_INTEGER
2957 && expr
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
)
2958 if (gfc_conv_cst_int_power (se
, lse
.expr
, rse
.expr
))
2961 gfc_int4_type_node
= gfc_get_int_type (4);
2963 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
2964 library routine. But in the end, we have to convert the result back
2965 if this case applies -- with res_ikind_K, we keep track whether operand K
2966 falls into this case. */
2970 kind
= expr
->value
.op
.op1
->ts
.kind
;
2971 switch (expr
->value
.op
.op2
->ts
.type
)
2974 ikind
= expr
->value
.op
.op2
->ts
.kind
;
2979 rse
.expr
= convert (gfc_int4_type_node
, rse
.expr
);
2980 res_ikind_2
= ikind
;
3002 if (expr
->value
.op
.op1
->ts
.type
== BT_INTEGER
)
3004 lse
.expr
= convert (gfc_int4_type_node
, lse
.expr
);
3031 switch (expr
->value
.op
.op1
->ts
.type
)
3034 if (kind
== 3) /* Case 16 was not handled properly above. */
3036 fndecl
= gfor_fndecl_math_powi
[kind
][ikind
].integer
;
3040 /* Use builtins for real ** int4. */
3046 fndecl
= builtin_decl_explicit (BUILT_IN_POWIF
);
3050 fndecl
= builtin_decl_explicit (BUILT_IN_POWI
);
3054 fndecl
= builtin_decl_explicit (BUILT_IN_POWIL
);
3058 /* Use the __builtin_powil() only if real(kind=16) is
3059 actually the C long double type. */
3060 if (!gfc_real16_is_float128
)
3061 fndecl
= builtin_decl_explicit (BUILT_IN_POWIL
);
3069 /* If we don't have a good builtin for this, go for the
3070 library function. */
3072 fndecl
= gfor_fndecl_math_powi
[kind
][ikind
].real
;
3076 fndecl
= gfor_fndecl_math_powi
[kind
][ikind
].cmplx
;
3085 fndecl
= gfc_builtin_decl_for_float_kind (BUILT_IN_POW
, kind
);
3089 fndecl
= gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW
, kind
);
3097 se
->expr
= build_call_expr_loc (input_location
,
3098 fndecl
, 2, lse
.expr
, rse
.expr
);
3100 /* Convert the result back if it is of wrong integer kind. */
3101 if (res_ikind_1
!= -1 && res_ikind_2
!= -1)
3103 /* We want the maximum of both operand kinds as result. */
3104 if (res_ikind_1
< res_ikind_2
)
3105 res_ikind_1
= res_ikind_2
;
3106 se
->expr
= convert (gfc_get_int_type (res_ikind_1
), se
->expr
);
3111 /* Generate code to allocate a string temporary. */
3114 gfc_conv_string_tmp (gfc_se
* se
, tree type
, tree len
)
3119 if (gfc_can_put_var_on_stack (len
))
3121 /* Create a temporary variable to hold the result. */
3122 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
3123 gfc_charlen_type_node
, len
,
3124 build_int_cst (gfc_charlen_type_node
, 1));
3125 tmp
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
, tmp
);
3127 if (TREE_CODE (TREE_TYPE (type
)) == ARRAY_TYPE
)
3128 tmp
= build_array_type (TREE_TYPE (TREE_TYPE (type
)), tmp
);
3130 tmp
= build_array_type (TREE_TYPE (type
), tmp
);
3132 var
= gfc_create_var (tmp
, "str");
3133 var
= gfc_build_addr_expr (type
, var
);
3137 /* Allocate a temporary to hold the result. */
3138 var
= gfc_create_var (type
, "pstr");
3139 gcc_assert (POINTER_TYPE_P (type
));
3140 tmp
= TREE_TYPE (type
);
3141 if (TREE_CODE (tmp
) == ARRAY_TYPE
)
3142 tmp
= TREE_TYPE (tmp
);
3143 tmp
= TYPE_SIZE_UNIT (tmp
);
3144 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
3145 fold_convert (size_type_node
, len
),
3146 fold_convert (size_type_node
, tmp
));
3147 tmp
= gfc_call_malloc (&se
->pre
, type
, tmp
);
3148 gfc_add_modify (&se
->pre
, var
, tmp
);
3150 /* Free the temporary afterwards. */
3151 tmp
= gfc_call_free (var
);
3152 gfc_add_expr_to_block (&se
->post
, tmp
);
3159 /* Handle a string concatenation operation. A temporary will be allocated to
3163 gfc_conv_concat_op (gfc_se
* se
, gfc_expr
* expr
)
3166 tree len
, type
, var
, tmp
, fndecl
;
3168 gcc_assert (expr
->value
.op
.op1
->ts
.type
== BT_CHARACTER
3169 && expr
->value
.op
.op2
->ts
.type
== BT_CHARACTER
);
3170 gcc_assert (expr
->value
.op
.op1
->ts
.kind
== expr
->value
.op
.op2
->ts
.kind
);
3172 gfc_init_se (&lse
, se
);
3173 gfc_conv_expr (&lse
, expr
->value
.op
.op1
);
3174 gfc_conv_string_parameter (&lse
);
3175 gfc_init_se (&rse
, se
);
3176 gfc_conv_expr (&rse
, expr
->value
.op
.op2
);
3177 gfc_conv_string_parameter (&rse
);
3179 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
3180 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
3182 type
= gfc_get_character_type (expr
->ts
.kind
, expr
->ts
.u
.cl
);
3183 len
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
3184 if (len
== NULL_TREE
)
3186 len
= fold_build2_loc (input_location
, PLUS_EXPR
,
3187 TREE_TYPE (lse
.string_length
),
3188 lse
.string_length
, rse
.string_length
);
3191 type
= build_pointer_type (type
);
3193 var
= gfc_conv_string_tmp (se
, type
, len
);
3195 /* Do the actual concatenation. */
3196 if (expr
->ts
.kind
== 1)
3197 fndecl
= gfor_fndecl_concat_string
;
3198 else if (expr
->ts
.kind
== 4)
3199 fndecl
= gfor_fndecl_concat_string_char4
;
3203 tmp
= build_call_expr_loc (input_location
,
3204 fndecl
, 6, len
, var
, lse
.string_length
, lse
.expr
,
3205 rse
.string_length
, rse
.expr
);
3206 gfc_add_expr_to_block (&se
->pre
, tmp
);
3208 /* Add the cleanup for the operands. */
3209 gfc_add_block_to_block (&se
->pre
, &rse
.post
);
3210 gfc_add_block_to_block (&se
->pre
, &lse
.post
);
3213 se
->string_length
= len
;
3216 /* Translates an op expression. Common (binary) cases are handled by this
3217 function, others are passed on. Recursion is used in either case.
3218 We use the fact that (op1.ts == op2.ts) (except for the power
3220 Operators need no special handling for scalarized expressions as long as
3221 they call gfc_conv_simple_val to get their operands.
3222 Character strings get special handling. */
3225 gfc_conv_expr_op (gfc_se
* se
, gfc_expr
* expr
)
3227 enum tree_code code
;
3236 switch (expr
->value
.op
.op
)
3238 case INTRINSIC_PARENTHESES
:
3239 if ((expr
->ts
.type
== BT_REAL
|| expr
->ts
.type
== BT_COMPLEX
)
3240 && flag_protect_parens
)
3242 gfc_conv_unary_op (PAREN_EXPR
, se
, expr
);
3243 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se
->expr
)));
3248 case INTRINSIC_UPLUS
:
3249 gfc_conv_expr (se
, expr
->value
.op
.op1
);
3252 case INTRINSIC_UMINUS
:
3253 gfc_conv_unary_op (NEGATE_EXPR
, se
, expr
);
3257 gfc_conv_unary_op (TRUTH_NOT_EXPR
, se
, expr
);
3260 case INTRINSIC_PLUS
:
3264 case INTRINSIC_MINUS
:
3268 case INTRINSIC_TIMES
:
3272 case INTRINSIC_DIVIDE
:
3273 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
3274 an integer, we must round towards zero, so we use a
3276 if (expr
->ts
.type
== BT_INTEGER
)
3277 code
= TRUNC_DIV_EXPR
;
3282 case INTRINSIC_POWER
:
3283 gfc_conv_power_op (se
, expr
);
3286 case INTRINSIC_CONCAT
:
3287 gfc_conv_concat_op (se
, expr
);
3291 code
= TRUTH_ANDIF_EXPR
;
3296 code
= TRUTH_ORIF_EXPR
;
3300 /* EQV and NEQV only work on logicals, but since we represent them
3301 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
3303 case INTRINSIC_EQ_OS
:
3311 case INTRINSIC_NE_OS
:
3312 case INTRINSIC_NEQV
:
3319 case INTRINSIC_GT_OS
:
3326 case INTRINSIC_GE_OS
:
3333 case INTRINSIC_LT_OS
:
3340 case INTRINSIC_LE_OS
:
3346 case INTRINSIC_USER
:
3347 case INTRINSIC_ASSIGN
:
3348 /* These should be converted into function calls by the frontend. */
3352 fatal_error (input_location
, "Unknown intrinsic op");
3356 /* The only exception to this is **, which is handled separately anyway. */
3357 gcc_assert (expr
->value
.op
.op1
->ts
.type
== expr
->value
.op
.op2
->ts
.type
);
3359 if (checkstring
&& expr
->value
.op
.op1
->ts
.type
!= BT_CHARACTER
)
3363 gfc_init_se (&lse
, se
);
3364 gfc_conv_expr (&lse
, expr
->value
.op
.op1
);
3365 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
3368 gfc_init_se (&rse
, se
);
3369 gfc_conv_expr (&rse
, expr
->value
.op
.op2
);
3370 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
3374 gfc_conv_string_parameter (&lse
);
3375 gfc_conv_string_parameter (&rse
);
3377 lse
.expr
= gfc_build_compare_string (lse
.string_length
, lse
.expr
,
3378 rse
.string_length
, rse
.expr
,
3379 expr
->value
.op
.op1
->ts
.kind
,
3381 rse
.expr
= build_int_cst (TREE_TYPE (lse
.expr
), 0);
3382 gfc_add_block_to_block (&lse
.post
, &rse
.post
);
3385 type
= gfc_typenode_for_spec (&expr
->ts
);
3389 /* The result of logical ops is always boolean_type_node. */
3390 tmp
= fold_build2_loc (input_location
, code
, boolean_type_node
,
3391 lse
.expr
, rse
.expr
);
3392 se
->expr
= convert (type
, tmp
);
3395 se
->expr
= fold_build2_loc (input_location
, code
, type
, lse
.expr
, rse
.expr
);
3397 /* Add the post blocks. */
3398 gfc_add_block_to_block (&se
->post
, &rse
.post
);
3399 gfc_add_block_to_block (&se
->post
, &lse
.post
);
3402 /* If a string's length is one, we convert it to a single character. */
3405 gfc_string_to_single_character (tree len
, tree str
, int kind
)
3409 || !tree_fits_uhwi_p (len
)
3410 || !POINTER_TYPE_P (TREE_TYPE (str
)))
3413 if (TREE_INT_CST_LOW (len
) == 1)
3415 str
= fold_convert (gfc_get_pchar_type (kind
), str
);
3416 return build_fold_indirect_ref_loc (input_location
, str
);
3420 && TREE_CODE (str
) == ADDR_EXPR
3421 && TREE_CODE (TREE_OPERAND (str
, 0)) == ARRAY_REF
3422 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str
, 0), 0)) == STRING_CST
3423 && array_ref_low_bound (TREE_OPERAND (str
, 0))
3424 == TREE_OPERAND (TREE_OPERAND (str
, 0), 1)
3425 && TREE_INT_CST_LOW (len
) > 1
3426 && TREE_INT_CST_LOW (len
)
3427 == (unsigned HOST_WIDE_INT
)
3428 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str
, 0), 0)))
3430 tree ret
= fold_convert (gfc_get_pchar_type (kind
), str
);
3431 ret
= build_fold_indirect_ref_loc (input_location
, ret
);
3432 if (TREE_CODE (ret
) == INTEGER_CST
)
3434 tree string_cst
= TREE_OPERAND (TREE_OPERAND (str
, 0), 0);
3435 int i
, length
= TREE_STRING_LENGTH (string_cst
);
3436 const char *ptr
= TREE_STRING_POINTER (string_cst
);
3438 for (i
= 1; i
< length
; i
++)
3451 gfc_conv_scalar_char_value (gfc_symbol
*sym
, gfc_se
*se
, gfc_expr
**expr
)
3454 if (sym
->backend_decl
)
3456 /* This becomes the nominal_type in
3457 function.c:assign_parm_find_data_types. */
3458 TREE_TYPE (sym
->backend_decl
) = unsigned_char_type_node
;
3459 /* This becomes the passed_type in
3460 function.c:assign_parm_find_data_types. C promotes char to
3461 integer for argument passing. */
3462 DECL_ARG_TYPE (sym
->backend_decl
) = unsigned_type_node
;
3464 DECL_BY_REFERENCE (sym
->backend_decl
) = 0;
3469 /* If we have a constant character expression, make it into an
3471 if ((*expr
)->expr_type
== EXPR_CONSTANT
)
3476 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
3477 (int)(*expr
)->value
.character
.string
[0]);
3478 if ((*expr
)->ts
.kind
!= gfc_c_int_kind
)
3480 /* The expr needs to be compatible with a C int. If the
3481 conversion fails, then the 2 causes an ICE. */
3482 ts
.type
= BT_INTEGER
;
3483 ts
.kind
= gfc_c_int_kind
;
3484 gfc_convert_type (*expr
, &ts
, 2);
3487 else if (se
!= NULL
&& (*expr
)->expr_type
== EXPR_VARIABLE
)
3489 if ((*expr
)->ref
== NULL
)
3491 se
->expr
= gfc_string_to_single_character
3492 (build_int_cst (integer_type_node
, 1),
3493 gfc_build_addr_expr (gfc_get_pchar_type ((*expr
)->ts
.kind
),
3495 ((*expr
)->symtree
->n
.sym
)),
3500 gfc_conv_variable (se
, *expr
);
3501 se
->expr
= gfc_string_to_single_character
3502 (build_int_cst (integer_type_node
, 1),
3503 gfc_build_addr_expr (gfc_get_pchar_type ((*expr
)->ts
.kind
),
3511 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
3512 if STR is a string literal, otherwise return -1. */
3515 gfc_optimize_len_trim (tree len
, tree str
, int kind
)
3518 && TREE_CODE (str
) == ADDR_EXPR
3519 && TREE_CODE (TREE_OPERAND (str
, 0)) == ARRAY_REF
3520 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str
, 0), 0)) == STRING_CST
3521 && array_ref_low_bound (TREE_OPERAND (str
, 0))
3522 == TREE_OPERAND (TREE_OPERAND (str
, 0), 1)
3523 && tree_fits_uhwi_p (len
)
3524 && tree_to_uhwi (len
) >= 1
3525 && tree_to_uhwi (len
)
3526 == (unsigned HOST_WIDE_INT
)
3527 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str
, 0), 0)))
3529 tree folded
= fold_convert (gfc_get_pchar_type (kind
), str
);
3530 folded
= build_fold_indirect_ref_loc (input_location
, folded
);
3531 if (TREE_CODE (folded
) == INTEGER_CST
)
3533 tree string_cst
= TREE_OPERAND (TREE_OPERAND (str
, 0), 0);
3534 int length
= TREE_STRING_LENGTH (string_cst
);
3535 const char *ptr
= TREE_STRING_POINTER (string_cst
);
3537 for (; length
> 0; length
--)
3538 if (ptr
[length
- 1] != ' ')
3547 /* Helper to build a call to memcmp. */
3550 build_memcmp_call (tree s1
, tree s2
, tree n
)
3554 if (!POINTER_TYPE_P (TREE_TYPE (s1
)))
3555 s1
= gfc_build_addr_expr (pvoid_type_node
, s1
);
3557 s1
= fold_convert (pvoid_type_node
, s1
);
3559 if (!POINTER_TYPE_P (TREE_TYPE (s2
)))
3560 s2
= gfc_build_addr_expr (pvoid_type_node
, s2
);
3562 s2
= fold_convert (pvoid_type_node
, s2
);
3564 n
= fold_convert (size_type_node
, n
);
3566 tmp
= build_call_expr_loc (input_location
,
3567 builtin_decl_explicit (BUILT_IN_MEMCMP
),
3570 return fold_convert (integer_type_node
, tmp
);
3573 /* Compare two strings. If they are all single characters, the result is the
3574 subtraction of them. Otherwise, we build a library call. */
3577 gfc_build_compare_string (tree len1
, tree str1
, tree len2
, tree str2
, int kind
,
3578 enum tree_code code
)
3584 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1
)));
3585 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2
)));
3587 sc1
= gfc_string_to_single_character (len1
, str1
, kind
);
3588 sc2
= gfc_string_to_single_character (len2
, str2
, kind
);
3590 if (sc1
!= NULL_TREE
&& sc2
!= NULL_TREE
)
3592 /* Deal with single character specially. */
3593 sc1
= fold_convert (integer_type_node
, sc1
);
3594 sc2
= fold_convert (integer_type_node
, sc2
);
3595 return fold_build2_loc (input_location
, MINUS_EXPR
, integer_type_node
,
3599 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
3601 && INTEGER_CST_P (len1
) && INTEGER_CST_P (len2
))
3603 /* If one string is a string literal with LEN_TRIM longer
3604 than the length of the second string, the strings
3606 int len
= gfc_optimize_len_trim (len1
, str1
, kind
);
3607 if (len
> 0 && compare_tree_int (len2
, len
) < 0)
3608 return integer_one_node
;
3609 len
= gfc_optimize_len_trim (len2
, str2
, kind
);
3610 if (len
> 0 && compare_tree_int (len1
, len
) < 0)
3611 return integer_one_node
;
3614 /* We can compare via memcpy if the strings are known to be equal
3615 in length and they are
3617 - kind=4 and the comparison is for (in)equality. */
3619 if (INTEGER_CST_P (len1
) && INTEGER_CST_P (len2
)
3620 && tree_int_cst_equal (len1
, len2
)
3621 && (kind
== 1 || code
== EQ_EXPR
|| code
== NE_EXPR
))
3626 chartype
= gfc_get_char_type (kind
);
3627 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, TREE_TYPE(len1
),
3628 fold_convert (TREE_TYPE(len1
),
3629 TYPE_SIZE_UNIT(chartype
)),
3631 return build_memcmp_call (str1
, str2
, tmp
);
3634 /* Build a call for the comparison. */
3636 fndecl
= gfor_fndecl_compare_string
;
3638 fndecl
= gfor_fndecl_compare_string_char4
;
3642 return build_call_expr_loc (input_location
, fndecl
, 4,
3643 len1
, str1
, len2
, str2
);
3647 /* Return the backend_decl for a procedure pointer component. */
3650 get_proc_ptr_comp (gfc_expr
*e
)
3656 gfc_init_se (&comp_se
, NULL
);
3657 e2
= gfc_copy_expr (e
);
3658 /* We have to restore the expr type later so that gfc_free_expr frees
3659 the exact same thing that was allocated.
3660 TODO: This is ugly. */
3661 old_type
= e2
->expr_type
;
3662 e2
->expr_type
= EXPR_VARIABLE
;
3663 gfc_conv_expr (&comp_se
, e2
);
3664 e2
->expr_type
= old_type
;
3666 return build_fold_addr_expr_loc (input_location
, comp_se
.expr
);
3670 /* Convert a typebound function reference from a class object. */
3672 conv_base_obj_fcn_val (gfc_se
* se
, tree base_object
, gfc_expr
* expr
)
3677 if (!VAR_P (base_object
))
3679 var
= gfc_create_var (TREE_TYPE (base_object
), NULL
);
3680 gfc_add_modify (&se
->pre
, var
, base_object
);
3682 se
->expr
= gfc_class_vptr_get (base_object
);
3683 se
->expr
= build_fold_indirect_ref_loc (input_location
, se
->expr
);
3685 while (ref
&& ref
->next
)
3687 gcc_assert (ref
&& ref
->type
== REF_COMPONENT
);
3688 if (ref
->u
.c
.sym
->attr
.extension
)
3689 conv_parent_component_references (se
, ref
);
3690 gfc_conv_component_ref (se
, ref
);
3691 se
->expr
= build_fold_addr_expr_loc (input_location
, se
->expr
);
3696 conv_function_val (gfc_se
* se
, gfc_symbol
* sym
, gfc_expr
* expr
)
3700 if (gfc_is_proc_ptr_comp (expr
))
3701 tmp
= get_proc_ptr_comp (expr
);
3702 else if (sym
->attr
.dummy
)
3704 tmp
= gfc_get_symbol_decl (sym
);
3705 if (sym
->attr
.proc_pointer
)
3706 tmp
= build_fold_indirect_ref_loc (input_location
,
3708 gcc_assert (TREE_CODE (TREE_TYPE (tmp
)) == POINTER_TYPE
3709 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp
))) == FUNCTION_TYPE
);
3713 if (!sym
->backend_decl
)
3714 sym
->backend_decl
= gfc_get_extern_function_decl (sym
);
3716 TREE_USED (sym
->backend_decl
) = 1;
3718 tmp
= sym
->backend_decl
;
3720 if (sym
->attr
.cray_pointee
)
3722 /* TODO - make the cray pointee a pointer to a procedure,
3723 assign the pointer to it and use it for the call. This
3725 tmp
= convert (build_pointer_type (TREE_TYPE (tmp
)),
3726 gfc_get_symbol_decl (sym
->cp_pointer
));
3727 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
3730 if (!POINTER_TYPE_P (TREE_TYPE (tmp
)))
3732 gcc_assert (TREE_CODE (tmp
) == FUNCTION_DECL
);
3733 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
3740 /* Initialize MAPPING. */
3743 gfc_init_interface_mapping (gfc_interface_mapping
* mapping
)
3745 mapping
->syms
= NULL
;
3746 mapping
->charlens
= NULL
;
3750 /* Free all memory held by MAPPING (but not MAPPING itself). */
3753 gfc_free_interface_mapping (gfc_interface_mapping
* mapping
)
3755 gfc_interface_sym_mapping
*sym
;
3756 gfc_interface_sym_mapping
*nextsym
;
3758 gfc_charlen
*nextcl
;
3760 for (sym
= mapping
->syms
; sym
; sym
= nextsym
)
3762 nextsym
= sym
->next
;
3763 sym
->new_sym
->n
.sym
->formal
= NULL
;
3764 gfc_free_symbol (sym
->new_sym
->n
.sym
);
3765 gfc_free_expr (sym
->expr
);
3766 free (sym
->new_sym
);
3769 for (cl
= mapping
->charlens
; cl
; cl
= nextcl
)
3772 gfc_free_expr (cl
->length
);
3778 /* Return a copy of gfc_charlen CL. Add the returned structure to
3779 MAPPING so that it will be freed by gfc_free_interface_mapping. */
3781 static gfc_charlen
*
3782 gfc_get_interface_mapping_charlen (gfc_interface_mapping
* mapping
,
3785 gfc_charlen
*new_charlen
;
3787 new_charlen
= gfc_get_charlen ();
3788 new_charlen
->next
= mapping
->charlens
;
3789 new_charlen
->length
= gfc_copy_expr (cl
->length
);
3791 mapping
->charlens
= new_charlen
;
3796 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
3797 array variable that can be used as the actual argument for dummy
3798 argument SYM. Add any initialization code to BLOCK. PACKED is as
3799 for gfc_get_nodesc_array_type and DATA points to the first element
3800 in the passed array. */
3803 gfc_get_interface_mapping_array (stmtblock_t
* block
, gfc_symbol
* sym
,
3804 gfc_packed packed
, tree data
)
3809 type
= gfc_typenode_for_spec (&sym
->ts
);
3810 type
= gfc_get_nodesc_array_type (type
, sym
->as
, packed
,
3811 !sym
->attr
.target
&& !sym
->attr
.pointer
3812 && !sym
->attr
.proc_pointer
);
3814 var
= gfc_create_var (type
, "ifm");
3815 gfc_add_modify (block
, var
, fold_convert (type
, data
));
3821 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
3822 and offset of descriptorless array type TYPE given that it has the same
3823 size as DESC. Add any set-up code to BLOCK. */
3826 gfc_set_interface_mapping_bounds (stmtblock_t
* block
, tree type
, tree desc
)
3833 offset
= gfc_index_zero_node
;
3834 for (n
= 0; n
< GFC_TYPE_ARRAY_RANK (type
); n
++)
3836 dim
= gfc_rank_cst
[n
];
3837 GFC_TYPE_ARRAY_STRIDE (type
, n
) = gfc_conv_array_stride (desc
, n
);
3838 if (GFC_TYPE_ARRAY_LBOUND (type
, n
) == NULL_TREE
)
3840 GFC_TYPE_ARRAY_LBOUND (type
, n
)
3841 = gfc_conv_descriptor_lbound_get (desc
, dim
);
3842 GFC_TYPE_ARRAY_UBOUND (type
, n
)
3843 = gfc_conv_descriptor_ubound_get (desc
, dim
);
3845 else if (GFC_TYPE_ARRAY_UBOUND (type
, n
) == NULL_TREE
)
3847 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
3848 gfc_array_index_type
,
3849 gfc_conv_descriptor_ubound_get (desc
, dim
),
3850 gfc_conv_descriptor_lbound_get (desc
, dim
));
3851 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
3852 gfc_array_index_type
,
3853 GFC_TYPE_ARRAY_LBOUND (type
, n
), tmp
);
3854 tmp
= gfc_evaluate_now (tmp
, block
);
3855 GFC_TYPE_ARRAY_UBOUND (type
, n
) = tmp
;
3857 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
3858 GFC_TYPE_ARRAY_LBOUND (type
, n
),
3859 GFC_TYPE_ARRAY_STRIDE (type
, n
));
3860 offset
= fold_build2_loc (input_location
, MINUS_EXPR
,
3861 gfc_array_index_type
, offset
, tmp
);
3863 offset
= gfc_evaluate_now (offset
, block
);
3864 GFC_TYPE_ARRAY_OFFSET (type
) = offset
;
3868 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
3869 in SE. The caller may still use se->expr and se->string_length after
3870 calling this function. */
3873 gfc_add_interface_mapping (gfc_interface_mapping
* mapping
,
3874 gfc_symbol
* sym
, gfc_se
* se
,
3877 gfc_interface_sym_mapping
*sm
;
3881 gfc_symbol
*new_sym
;
3883 gfc_symtree
*new_symtree
;
3885 /* Create a new symbol to represent the actual argument. */
3886 new_sym
= gfc_new_symbol (sym
->name
, NULL
);
3887 new_sym
->ts
= sym
->ts
;
3888 new_sym
->as
= gfc_copy_array_spec (sym
->as
);
3889 new_sym
->attr
.referenced
= 1;
3890 new_sym
->attr
.dimension
= sym
->attr
.dimension
;
3891 new_sym
->attr
.contiguous
= sym
->attr
.contiguous
;
3892 new_sym
->attr
.codimension
= sym
->attr
.codimension
;
3893 new_sym
->attr
.pointer
= sym
->attr
.pointer
;
3894 new_sym
->attr
.allocatable
= sym
->attr
.allocatable
;
3895 new_sym
->attr
.flavor
= sym
->attr
.flavor
;
3896 new_sym
->attr
.function
= sym
->attr
.function
;
3898 /* Ensure that the interface is available and that
3899 descriptors are passed for array actual arguments. */
3900 if (sym
->attr
.flavor
== FL_PROCEDURE
)
3902 new_sym
->formal
= expr
->symtree
->n
.sym
->formal
;
3903 new_sym
->attr
.always_explicit
3904 = expr
->symtree
->n
.sym
->attr
.always_explicit
;
3907 /* Create a fake symtree for it. */
3909 new_symtree
= gfc_new_symtree (&root
, sym
->name
);
3910 new_symtree
->n
.sym
= new_sym
;
3911 gcc_assert (new_symtree
== root
);
3913 /* Create a dummy->actual mapping. */
3914 sm
= XCNEW (gfc_interface_sym_mapping
);
3915 sm
->next
= mapping
->syms
;
3917 sm
->new_sym
= new_symtree
;
3918 sm
->expr
= gfc_copy_expr (expr
);
3921 /* Stabilize the argument's value. */
3922 if (!sym
->attr
.function
&& se
)
3923 se
->expr
= gfc_evaluate_now (se
->expr
, &se
->pre
);
3925 if (sym
->ts
.type
== BT_CHARACTER
)
3927 /* Create a copy of the dummy argument's length. */
3928 new_sym
->ts
.u
.cl
= gfc_get_interface_mapping_charlen (mapping
, sym
->ts
.u
.cl
);
3929 sm
->expr
->ts
.u
.cl
= new_sym
->ts
.u
.cl
;
3931 /* If the length is specified as "*", record the length that
3932 the caller is passing. We should use the callee's length
3933 in all other cases. */
3934 if (!new_sym
->ts
.u
.cl
->length
&& se
)
3936 se
->string_length
= gfc_evaluate_now (se
->string_length
, &se
->pre
);
3937 new_sym
->ts
.u
.cl
->backend_decl
= se
->string_length
;
3944 /* Use the passed value as-is if the argument is a function. */
3945 if (sym
->attr
.flavor
== FL_PROCEDURE
)
3948 /* If the argument is a pass-by-value scalar, use the value as is. */
3949 else if (!sym
->attr
.dimension
&& sym
->attr
.value
)
3952 /* If the argument is either a string or a pointer to a string,
3953 convert it to a boundless character type. */
3954 else if (!sym
->attr
.dimension
&& sym
->ts
.type
== BT_CHARACTER
)
3956 tmp
= gfc_get_character_type_len (sym
->ts
.kind
, NULL
);
3957 tmp
= build_pointer_type (tmp
);
3958 if (sym
->attr
.pointer
)
3959 value
= build_fold_indirect_ref_loc (input_location
,
3963 value
= fold_convert (tmp
, value
);
3966 /* If the argument is a scalar, a pointer to an array or an allocatable,
3968 else if (!sym
->attr
.dimension
|| sym
->attr
.pointer
|| sym
->attr
.allocatable
)
3969 value
= build_fold_indirect_ref_loc (input_location
,
3972 /* For character(*), use the actual argument's descriptor. */
3973 else if (sym
->ts
.type
== BT_CHARACTER
&& !new_sym
->ts
.u
.cl
->length
)
3974 value
= build_fold_indirect_ref_loc (input_location
,
3977 /* If the argument is an array descriptor, use it to determine
3978 information about the actual argument's shape. */
3979 else if (POINTER_TYPE_P (TREE_TYPE (se
->expr
))
3980 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se
->expr
))))
3982 /* Get the actual argument's descriptor. */
3983 desc
= build_fold_indirect_ref_loc (input_location
,
3986 /* Create the replacement variable. */
3987 tmp
= gfc_conv_descriptor_data_get (desc
);
3988 value
= gfc_get_interface_mapping_array (&se
->pre
, sym
,
3991 /* Use DESC to work out the upper bounds, strides and offset. */
3992 gfc_set_interface_mapping_bounds (&se
->pre
, TREE_TYPE (value
), desc
);
3995 /* Otherwise we have a packed array. */
3996 value
= gfc_get_interface_mapping_array (&se
->pre
, sym
,
3997 PACKED_FULL
, se
->expr
);
3999 new_sym
->backend_decl
= value
;
4003 /* Called once all dummy argument mappings have been added to MAPPING,
4004 but before the mapping is used to evaluate expressions. Pre-evaluate
4005 the length of each argument, adding any initialization code to PRE and
4006 any finalization code to POST. */
4009 gfc_finish_interface_mapping (gfc_interface_mapping
* mapping
,
4010 stmtblock_t
* pre
, stmtblock_t
* post
)
4012 gfc_interface_sym_mapping
*sym
;
4016 for (sym
= mapping
->syms
; sym
; sym
= sym
->next
)
4017 if (sym
->new_sym
->n
.sym
->ts
.type
== BT_CHARACTER
4018 && !sym
->new_sym
->n
.sym
->ts
.u
.cl
->backend_decl
)
4020 expr
= sym
->new_sym
->n
.sym
->ts
.u
.cl
->length
;
4021 gfc_apply_interface_mapping_to_expr (mapping
, expr
);
4022 gfc_init_se (&se
, NULL
);
4023 gfc_conv_expr (&se
, expr
);
4024 se
.expr
= fold_convert (gfc_charlen_type_node
, se
.expr
);
4025 se
.expr
= gfc_evaluate_now (se
.expr
, &se
.pre
);
4026 gfc_add_block_to_block (pre
, &se
.pre
);
4027 gfc_add_block_to_block (post
, &se
.post
);
4029 sym
->new_sym
->n
.sym
->ts
.u
.cl
->backend_decl
= se
.expr
;
4034 /* Like gfc_apply_interface_mapping_to_expr, but applied to
4038 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping
* mapping
,
4039 gfc_constructor_base base
)
4042 for (c
= gfc_constructor_first (base
); c
; c
= gfc_constructor_next (c
))
4044 gfc_apply_interface_mapping_to_expr (mapping
, c
->expr
);
4047 gfc_apply_interface_mapping_to_expr (mapping
, c
->iterator
->start
);
4048 gfc_apply_interface_mapping_to_expr (mapping
, c
->iterator
->end
);
4049 gfc_apply_interface_mapping_to_expr (mapping
, c
->iterator
->step
);
4055 /* Like gfc_apply_interface_mapping_to_expr, but applied to
4059 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping
* mapping
,
4064 for (; ref
; ref
= ref
->next
)
4068 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
4070 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ar
.start
[n
]);
4071 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ar
.end
[n
]);
4072 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ar
.stride
[n
]);
4080 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ss
.start
);
4081 gfc_apply_interface_mapping_to_expr (mapping
, ref
->u
.ss
.end
);
4087 /* Convert intrinsic function calls into result expressions. */
4090 gfc_map_intrinsic_function (gfc_expr
*expr
, gfc_interface_mapping
*mapping
)
4098 arg1
= expr
->value
.function
.actual
->expr
;
4099 if (expr
->value
.function
.actual
->next
)
4100 arg2
= expr
->value
.function
.actual
->next
->expr
;
4104 sym
= arg1
->symtree
->n
.sym
;
4106 if (sym
->attr
.dummy
)
4111 switch (expr
->value
.function
.isym
->id
)
4114 /* TODO figure out why this condition is necessary. */
4115 if (sym
->attr
.function
4116 && (arg1
->ts
.u
.cl
->length
== NULL
4117 || (arg1
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4118 && arg1
->ts
.u
.cl
->length
->expr_type
!= EXPR_VARIABLE
)))
4121 new_expr
= gfc_copy_expr (arg1
->ts
.u
.cl
->length
);
4124 case GFC_ISYM_LEN_TRIM
:
4125 new_expr
= gfc_copy_expr (arg1
);
4126 gfc_apply_interface_mapping_to_expr (mapping
, new_expr
);
4131 gfc_replace_expr (arg1
, new_expr
);
4135 if (!sym
->as
|| sym
->as
->rank
== 0)
4138 if (arg2
&& arg2
->expr_type
== EXPR_CONSTANT
)
4140 dup
= mpz_get_si (arg2
->value
.integer
);
4145 dup
= sym
->as
->rank
;
4149 for (; d
< dup
; d
++)
4153 if (!sym
->as
->upper
[d
] || !sym
->as
->lower
[d
])
4155 gfc_free_expr (new_expr
);
4159 tmp
= gfc_add (gfc_copy_expr (sym
->as
->upper
[d
]),
4160 gfc_get_int_expr (gfc_default_integer_kind
,
4162 tmp
= gfc_subtract (tmp
, gfc_copy_expr (sym
->as
->lower
[d
]));
4164 new_expr
= gfc_multiply (new_expr
, tmp
);
4170 case GFC_ISYM_LBOUND
:
4171 case GFC_ISYM_UBOUND
:
4172 /* TODO These implementations of lbound and ubound do not limit if
4173 the size < 0, according to F95's 13.14.53 and 13.14.113. */
4175 if (!sym
->as
|| sym
->as
->rank
== 0)
4178 if (arg2
&& arg2
->expr_type
== EXPR_CONSTANT
)
4179 d
= mpz_get_si (arg2
->value
.integer
) - 1;
4181 /* TODO: If the need arises, this could produce an array of
4185 if (expr
->value
.function
.isym
->id
== GFC_ISYM_LBOUND
)
4187 if (sym
->as
->lower
[d
])
4188 new_expr
= gfc_copy_expr (sym
->as
->lower
[d
]);
4192 if (sym
->as
->upper
[d
])
4193 new_expr
= gfc_copy_expr (sym
->as
->upper
[d
]);
4201 gfc_apply_interface_mapping_to_expr (mapping
, new_expr
);
4205 gfc_replace_expr (expr
, new_expr
);
4211 gfc_map_fcn_formal_to_actual (gfc_expr
*expr
, gfc_expr
*map_expr
,
4212 gfc_interface_mapping
* mapping
)
4214 gfc_formal_arglist
*f
;
4215 gfc_actual_arglist
*actual
;
4217 actual
= expr
->value
.function
.actual
;
4218 f
= gfc_sym_get_dummy_args (map_expr
->symtree
->n
.sym
);
4220 for (; f
&& actual
; f
= f
->next
, actual
= actual
->next
)
4225 gfc_add_interface_mapping (mapping
, f
->sym
, NULL
, actual
->expr
);
4228 if (map_expr
->symtree
->n
.sym
->attr
.dimension
)
4233 as
= gfc_copy_array_spec (map_expr
->symtree
->n
.sym
->as
);
4235 for (d
= 0; d
< as
->rank
; d
++)
4237 gfc_apply_interface_mapping_to_expr (mapping
, as
->lower
[d
]);
4238 gfc_apply_interface_mapping_to_expr (mapping
, as
->upper
[d
]);
4241 expr
->value
.function
.esym
->as
= as
;
4244 if (map_expr
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
)
4246 expr
->value
.function
.esym
->ts
.u
.cl
->length
4247 = gfc_copy_expr (map_expr
->symtree
->n
.sym
->ts
.u
.cl
->length
);
4249 gfc_apply_interface_mapping_to_expr (mapping
,
4250 expr
->value
.function
.esym
->ts
.u
.cl
->length
);
4255 /* EXPR is a copy of an expression that appeared in the interface
4256 associated with MAPPING. Walk it recursively looking for references to
4257 dummy arguments that MAPPING maps to actual arguments. Replace each such
4258 reference with a reference to the associated actual argument. */
4261 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping
* mapping
,
4264 gfc_interface_sym_mapping
*sym
;
4265 gfc_actual_arglist
*actual
;
4270 /* Copying an expression does not copy its length, so do that here. */
4271 if (expr
->ts
.type
== BT_CHARACTER
&& expr
->ts
.u
.cl
)
4273 expr
->ts
.u
.cl
= gfc_get_interface_mapping_charlen (mapping
, expr
->ts
.u
.cl
);
4274 gfc_apply_interface_mapping_to_expr (mapping
, expr
->ts
.u
.cl
->length
);
4277 /* Apply the mapping to any references. */
4278 gfc_apply_interface_mapping_to_ref (mapping
, expr
->ref
);
4280 /* ...and to the expression's symbol, if it has one. */
4281 /* TODO Find out why the condition on expr->symtree had to be moved into
4282 the loop rather than being outside it, as originally. */
4283 for (sym
= mapping
->syms
; sym
; sym
= sym
->next
)
4284 if (expr
->symtree
&& sym
->old
== expr
->symtree
->n
.sym
)
4286 if (sym
->new_sym
->n
.sym
->backend_decl
)
4287 expr
->symtree
= sym
->new_sym
;
4289 gfc_replace_expr (expr
, gfc_copy_expr (sym
->expr
));
4292 /* ...and to subexpressions in expr->value. */
4293 switch (expr
->expr_type
)
4298 case EXPR_SUBSTRING
:
4302 gfc_apply_interface_mapping_to_expr (mapping
, expr
->value
.op
.op1
);
4303 gfc_apply_interface_mapping_to_expr (mapping
, expr
->value
.op
.op2
);
4307 for (actual
= expr
->value
.function
.actual
; actual
; actual
= actual
->next
)
4308 gfc_apply_interface_mapping_to_expr (mapping
, actual
->expr
);
4310 if (expr
->value
.function
.esym
== NULL
4311 && expr
->value
.function
.isym
!= NULL
4312 && expr
->value
.function
.actual
->expr
->symtree
4313 && gfc_map_intrinsic_function (expr
, mapping
))
4316 for (sym
= mapping
->syms
; sym
; sym
= sym
->next
)
4317 if (sym
->old
== expr
->value
.function
.esym
)
4319 expr
->value
.function
.esym
= sym
->new_sym
->n
.sym
;
4320 gfc_map_fcn_formal_to_actual (expr
, sym
->expr
, mapping
);
4321 expr
->value
.function
.esym
->result
= sym
->new_sym
->n
.sym
;
4326 case EXPR_STRUCTURE
:
4327 gfc_apply_interface_mapping_to_cons (mapping
, expr
->value
.constructor
);
4340 /* Evaluate interface expression EXPR using MAPPING. Store the result
4344 gfc_apply_interface_mapping (gfc_interface_mapping
* mapping
,
4345 gfc_se
* se
, gfc_expr
* expr
)
4347 expr
= gfc_copy_expr (expr
);
4348 gfc_apply_interface_mapping_to_expr (mapping
, expr
);
4349 gfc_conv_expr (se
, expr
);
4350 se
->expr
= gfc_evaluate_now (se
->expr
, &se
->pre
);
4351 gfc_free_expr (expr
);
4355 /* Returns a reference to a temporary array into which a component of
4356 an actual argument derived type array is copied and then returned
4357 after the function call. */
4359 gfc_conv_subref_array_arg (gfc_se
* parmse
, gfc_expr
* expr
, int g77
,
4360 sym_intent intent
, bool formal_ptr
)
4368 gfc_array_info
*info
;
4378 gfc_init_se (&lse
, NULL
);
4379 gfc_init_se (&rse
, NULL
);
4381 /* Walk the argument expression. */
4382 rss
= gfc_walk_expr (expr
);
4384 gcc_assert (rss
!= gfc_ss_terminator
);
4386 /* Initialize the scalarizer. */
4387 gfc_init_loopinfo (&loop
);
4388 gfc_add_ss_to_loop (&loop
, rss
);
4390 /* Calculate the bounds of the scalarization. */
4391 gfc_conv_ss_startstride (&loop
);
4393 /* Build an ss for the temporary. */
4394 if (expr
->ts
.type
== BT_CHARACTER
&& !expr
->ts
.u
.cl
->backend_decl
)
4395 gfc_conv_string_length (expr
->ts
.u
.cl
, expr
, &parmse
->pre
);
4397 base_type
= gfc_typenode_for_spec (&expr
->ts
);
4398 if (GFC_ARRAY_TYPE_P (base_type
)
4399 || GFC_DESCRIPTOR_TYPE_P (base_type
))
4400 base_type
= gfc_get_element_type (base_type
);
4402 if (expr
->ts
.type
== BT_CLASS
)
4403 base_type
= gfc_typenode_for_spec (&CLASS_DATA (expr
)->ts
);
4405 loop
.temp_ss
= gfc_get_temp_ss (base_type
, ((expr
->ts
.type
== BT_CHARACTER
)
4406 ? expr
->ts
.u
.cl
->backend_decl
4410 parmse
->string_length
= loop
.temp_ss
->info
->string_length
;
4412 /* Associate the SS with the loop. */
4413 gfc_add_ss_to_loop (&loop
, loop
.temp_ss
);
4415 /* Setup the scalarizing loops. */
4416 gfc_conv_loop_setup (&loop
, &expr
->where
);
4418 /* Pass the temporary descriptor back to the caller. */
4419 info
= &loop
.temp_ss
->info
->data
.array
;
4420 parmse
->expr
= info
->descriptor
;
4422 /* Setup the gfc_se structures. */
4423 gfc_copy_loopinfo_to_se (&lse
, &loop
);
4424 gfc_copy_loopinfo_to_se (&rse
, &loop
);
4427 lse
.ss
= loop
.temp_ss
;
4428 gfc_mark_ss_chain_used (rss
, 1);
4429 gfc_mark_ss_chain_used (loop
.temp_ss
, 1);
4431 /* Start the scalarized loop body. */
4432 gfc_start_scalarized_body (&loop
, &body
);
4434 /* Translate the expression. */
4435 gfc_conv_expr (&rse
, expr
);
4437 /* Reset the offset for the function call since the loop
4438 is zero based on the data pointer. Note that the temp
4439 comes first in the loop chain since it is added second. */
4440 if (gfc_is_alloc_class_array_function (expr
))
4442 tmp
= loop
.ss
->loop_chain
->info
->data
.array
.descriptor
;
4443 gfc_conv_descriptor_offset_set (&loop
.pre
, tmp
,
4444 gfc_index_zero_node
);
4447 gfc_conv_tmp_array_ref (&lse
);
4449 if (intent
!= INTENT_OUT
)
4451 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr
->ts
, false, false);
4452 gfc_add_expr_to_block (&body
, tmp
);
4453 gcc_assert (rse
.ss
== gfc_ss_terminator
);
4454 gfc_trans_scalarizing_loops (&loop
, &body
);
4458 /* Make sure that the temporary declaration survives by merging
4459 all the loop declarations into the current context. */
4460 for (n
= 0; n
< loop
.dimen
; n
++)
4462 gfc_merge_block_scope (&body
);
4463 body
= loop
.code
[loop
.order
[n
]];
4465 gfc_merge_block_scope (&body
);
4468 /* Add the post block after the second loop, so that any
4469 freeing of allocated memory is done at the right time. */
4470 gfc_add_block_to_block (&parmse
->pre
, &loop
.pre
);
4472 /**********Copy the temporary back again.*********/
4474 gfc_init_se (&lse
, NULL
);
4475 gfc_init_se (&rse
, NULL
);
4477 /* Walk the argument expression. */
4478 lss
= gfc_walk_expr (expr
);
4479 rse
.ss
= loop
.temp_ss
;
4482 /* Initialize the scalarizer. */
4483 gfc_init_loopinfo (&loop2
);
4484 gfc_add_ss_to_loop (&loop2
, lss
);
4486 dimen
= rse
.ss
->dimen
;
4488 /* Skip the write-out loop for this case. */
4489 if (gfc_is_alloc_class_array_function (expr
))
4490 goto class_array_fcn
;
4492 /* Calculate the bounds of the scalarization. */
4493 gfc_conv_ss_startstride (&loop2
);
4495 /* Setup the scalarizing loops. */
4496 gfc_conv_loop_setup (&loop2
, &expr
->where
);
4498 gfc_copy_loopinfo_to_se (&lse
, &loop2
);
4499 gfc_copy_loopinfo_to_se (&rse
, &loop2
);
4501 gfc_mark_ss_chain_used (lss
, 1);
4502 gfc_mark_ss_chain_used (loop
.temp_ss
, 1);
4504 /* Declare the variable to hold the temporary offset and start the
4505 scalarized loop body. */
4506 offset
= gfc_create_var (gfc_array_index_type
, NULL
);
4507 gfc_start_scalarized_body (&loop2
, &body
);
4509 /* Build the offsets for the temporary from the loop variables. The
4510 temporary array has lbounds of zero and strides of one in all
4511 dimensions, so this is very simple. The offset is only computed
4512 outside the innermost loop, so the overall transfer could be
4513 optimized further. */
4514 info
= &rse
.ss
->info
->data
.array
;
4516 tmp_index
= gfc_index_zero_node
;
4517 for (n
= dimen
- 1; n
> 0; n
--)
4520 tmp
= rse
.loop
->loopvar
[n
];
4521 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
4522 tmp
, rse
.loop
->from
[n
]);
4523 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
, gfc_array_index_type
,
4526 tmp_str
= fold_build2_loc (input_location
, MINUS_EXPR
,
4527 gfc_array_index_type
,
4528 rse
.loop
->to
[n
-1], rse
.loop
->from
[n
-1]);
4529 tmp_str
= fold_build2_loc (input_location
, PLUS_EXPR
,
4530 gfc_array_index_type
,
4531 tmp_str
, gfc_index_one_node
);
4533 tmp_index
= fold_build2_loc (input_location
, MULT_EXPR
,
4534 gfc_array_index_type
, tmp
, tmp_str
);
4537 tmp_index
= fold_build2_loc (input_location
, MINUS_EXPR
,
4538 gfc_array_index_type
,
4539 tmp_index
, rse
.loop
->from
[0]);
4540 gfc_add_modify (&rse
.loop
->code
[0], offset
, tmp_index
);
4542 tmp_index
= fold_build2_loc (input_location
, PLUS_EXPR
,
4543 gfc_array_index_type
,
4544 rse
.loop
->loopvar
[0], offset
);
4546 /* Now use the offset for the reference. */
4547 tmp
= build_fold_indirect_ref_loc (input_location
,
4549 rse
.expr
= gfc_build_array_ref (tmp
, tmp_index
, NULL
);
4551 if (expr
->ts
.type
== BT_CHARACTER
)
4552 rse
.string_length
= expr
->ts
.u
.cl
->backend_decl
;
4554 gfc_conv_expr (&lse
, expr
);
4556 gcc_assert (lse
.ss
== gfc_ss_terminator
);
4558 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr
->ts
, false, true);
4559 gfc_add_expr_to_block (&body
, tmp
);
4561 /* Generate the copying loops. */
4562 gfc_trans_scalarizing_loops (&loop2
, &body
);
4564 /* Wrap the whole thing up by adding the second loop to the post-block
4565 and following it by the post-block of the first loop. In this way,
4566 if the temporary needs freeing, it is done after use! */
4567 if (intent
!= INTENT_IN
)
4569 gfc_add_block_to_block (&parmse
->post
, &loop2
.pre
);
4570 gfc_add_block_to_block (&parmse
->post
, &loop2
.post
);
4575 gfc_add_block_to_block (&parmse
->post
, &loop
.post
);
4577 gfc_cleanup_loop (&loop
);
4578 gfc_cleanup_loop (&loop2
);
4580 /* Pass the string length to the argument expression. */
4581 if (expr
->ts
.type
== BT_CHARACTER
)
4582 parmse
->string_length
= expr
->ts
.u
.cl
->backend_decl
;
4584 /* Determine the offset for pointer formal arguments and set the
4588 size
= gfc_index_one_node
;
4589 offset
= gfc_index_zero_node
;
4590 for (n
= 0; n
< dimen
; n
++)
4592 tmp
= gfc_conv_descriptor_ubound_get (parmse
->expr
,
4594 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
4595 gfc_array_index_type
, tmp
,
4596 gfc_index_one_node
);
4597 gfc_conv_descriptor_ubound_set (&parmse
->pre
,
4601 gfc_conv_descriptor_lbound_set (&parmse
->pre
,
4604 gfc_index_one_node
);
4605 size
= gfc_evaluate_now (size
, &parmse
->pre
);
4606 offset
= fold_build2_loc (input_location
, MINUS_EXPR
,
4607 gfc_array_index_type
,
4609 offset
= gfc_evaluate_now (offset
, &parmse
->pre
);
4610 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
4611 gfc_array_index_type
,
4612 rse
.loop
->to
[n
], rse
.loop
->from
[n
]);
4613 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
4614 gfc_array_index_type
,
4615 tmp
, gfc_index_one_node
);
4616 size
= fold_build2_loc (input_location
, MULT_EXPR
,
4617 gfc_array_index_type
, size
, tmp
);
4620 gfc_conv_descriptor_offset_set (&parmse
->pre
, parmse
->expr
,
4624 /* We want either the address for the data or the address of the descriptor,
4625 depending on the mode of passing array arguments. */
4627 parmse
->expr
= gfc_conv_descriptor_data_get (parmse
->expr
);
4629 parmse
->expr
= gfc_build_addr_expr (NULL_TREE
, parmse
->expr
);
4635 /* Generate the code for argument list functions. */
4638 conv_arglist_function (gfc_se
*se
, gfc_expr
*expr
, const char *name
)
4640 /* Pass by value for g77 %VAL(arg), pass the address
4641 indirectly for %LOC, else by reference. Thus %REF
4642 is a "do-nothing" and %LOC is the same as an F95
4644 if (strncmp (name
, "%VAL", 4) == 0)
4645 gfc_conv_expr (se
, expr
);
4646 else if (strncmp (name
, "%LOC", 4) == 0)
4648 gfc_conv_expr_reference (se
, expr
);
4649 se
->expr
= gfc_build_addr_expr (NULL
, se
->expr
);
4651 else if (strncmp (name
, "%REF", 4) == 0)
4652 gfc_conv_expr_reference (se
, expr
);
4654 gfc_error ("Unknown argument list function at %L", &expr
->where
);
4658 /* This function tells whether the middle-end representation of the expression
4659 E given as input may point to data otherwise accessible through a variable
4661 It is assumed that the only expressions that may alias are variables,
4662 and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
4664 This function is used to decide whether freeing an expression's allocatable
4665 components is safe or should be avoided.
4667 If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
4668 its elements are copied from a variable. This ARRAY_MAY_ALIAS trick
4669 is necessary because for array constructors, aliasing depends on how
4671 - If E is an array constructor used as argument to an elemental procedure,
4672 the array, which is generated through shallow copy by the scalarizer,
4673 is used directly and can alias the expressions it was copied from.
4674 - If E is an array constructor used as argument to a non-elemental
4675 procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
4676 the array as in the previous case, but then that array is used
4677 to initialize a new descriptor through deep copy. There is no alias
4678 possible in that case.
4679 Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
4683 expr_may_alias_variables (gfc_expr
*e
, bool array_may_alias
)
4687 if (e
->expr_type
== EXPR_VARIABLE
)
4689 else if (e
->expr_type
== EXPR_FUNCTION
)
4691 gfc_symbol
*proc_ifc
= gfc_get_proc_ifc_for_expr (e
);
4693 if (proc_ifc
->result
!= NULL
4694 && ((proc_ifc
->result
->ts
.type
== BT_CLASS
4695 && proc_ifc
->result
->ts
.u
.derived
->attr
.is_class
4696 && CLASS_DATA (proc_ifc
->result
)->attr
.class_pointer
)
4697 || proc_ifc
->result
->attr
.pointer
))
4702 else if (e
->expr_type
!= EXPR_ARRAY
|| !array_may_alias
)
4705 for (c
= gfc_constructor_first (e
->value
.constructor
);
4706 c
; c
= gfc_constructor_next (c
))
4708 && expr_may_alias_variables (c
->expr
, array_may_alias
))
4715 /* Generate code for a procedure call. Note can return se->post != NULL.
4716 If se->direct_byref is set then se->expr contains the return parameter.
4717 Return nonzero, if the call has alternate specifiers.
4718 'expr' is only needed for procedure pointer components. */
4721 gfc_conv_procedure_call (gfc_se
* se
, gfc_symbol
* sym
,
4722 gfc_actual_arglist
* args
, gfc_expr
* expr
,
4723 vec
<tree
, va_gc
> *append_args
)
4725 gfc_interface_mapping mapping
;
4726 vec
<tree
, va_gc
> *arglist
;
4727 vec
<tree
, va_gc
> *retargs
;
4731 gfc_array_info
*info
;
4738 vec
<tree
, va_gc
> *stringargs
;
4739 vec
<tree
, va_gc
> *optionalargs
;
4741 gfc_formal_arglist
*formal
;
4742 gfc_actual_arglist
*arg
;
4743 int has_alternate_specifier
= 0;
4744 bool need_interface_mapping
;
4752 enum {MISSING
= 0, ELEMENTAL
, SCALAR
, SCALAR_POINTER
, ARRAY
};
4753 gfc_component
*comp
= NULL
;
4760 optionalargs
= NULL
;
4765 comp
= gfc_get_proc_ptr_comp (expr
);
4767 bool elemental_proc
= (comp
4768 && comp
->ts
.interface
4769 && comp
->ts
.interface
->attr
.elemental
)
4770 || (comp
&& comp
->attr
.elemental
)
4771 || sym
->attr
.elemental
;
4775 if (!elemental_proc
)
4777 gcc_assert (se
->ss
->info
->type
== GFC_SS_FUNCTION
);
4778 if (se
->ss
->info
->useflags
)
4780 gcc_assert ((!comp
&& gfc_return_by_reference (sym
)
4781 && sym
->result
->attr
.dimension
)
4782 || (comp
&& comp
->attr
.dimension
)
4783 || gfc_is_alloc_class_array_function (expr
));
4784 gcc_assert (se
->loop
!= NULL
);
4785 /* Access the previously obtained result. */
4786 gfc_conv_tmp_array_ref (se
);
4790 info
= &se
->ss
->info
->data
.array
;
4795 gfc_init_block (&post
);
4796 gfc_init_interface_mapping (&mapping
);
4799 formal
= gfc_sym_get_dummy_args (sym
);
4800 need_interface_mapping
= sym
->attr
.dimension
||
4801 (sym
->ts
.type
== BT_CHARACTER
4802 && sym
->ts
.u
.cl
->length
4803 && sym
->ts
.u
.cl
->length
->expr_type
4808 formal
= comp
->ts
.interface
? comp
->ts
.interface
->formal
: NULL
;
4809 need_interface_mapping
= comp
->attr
.dimension
||
4810 (comp
->ts
.type
== BT_CHARACTER
4811 && comp
->ts
.u
.cl
->length
4812 && comp
->ts
.u
.cl
->length
->expr_type
4816 base_object
= NULL_TREE
;
4817 /* For _vprt->_copy () routines no formal symbol is present. Nevertheless
4818 is the third and fourth argument to such a function call a value
4819 denoting the number of elements to copy (i.e., most of the time the
4820 length of a deferred length string). */
4821 ulim_copy
= (formal
== NULL
)
4822 && UNLIMITED_POLY (sym
)
4823 && comp
&& (strcmp ("_copy", comp
->name
) == 0);
4825 /* Evaluate the arguments. */
4826 for (arg
= args
, argc
= 0; arg
!= NULL
;
4827 arg
= arg
->next
, formal
= formal
? formal
->next
: NULL
, ++argc
)
4830 fsym
= formal
? formal
->sym
: NULL
;
4831 parm_kind
= MISSING
;
4833 /* If the procedure requires an explicit interface, the actual
4834 argument is passed according to the corresponding formal
4835 argument. If the corresponding formal argument is a POINTER,
4836 ALLOCATABLE or assumed shape, we do not use g77's calling
4837 convention, and pass the address of the array descriptor
4838 instead. Otherwise we use g77's calling convention, in other words
4839 pass the array data pointer without descriptor. */
4840 bool nodesc_arg
= fsym
!= NULL
4841 && !(fsym
->attr
.pointer
|| fsym
->attr
.allocatable
)
4843 && fsym
->as
->type
!= AS_ASSUMED_SHAPE
4844 && fsym
->as
->type
!= AS_ASSUMED_RANK
;
4846 nodesc_arg
= nodesc_arg
|| !comp
->attr
.always_explicit
;
4848 nodesc_arg
= nodesc_arg
|| !sym
->attr
.always_explicit
;
4850 /* Class array expressions are sometimes coming completely unadorned
4851 with either arrayspec or _data component. Correct that here.
4852 OOP-TODO: Move this to the frontend. */
4853 if (e
&& e
->expr_type
== EXPR_VARIABLE
4855 && e
->ts
.type
== BT_CLASS
4856 && (CLASS_DATA (e
)->attr
.codimension
4857 || CLASS_DATA (e
)->attr
.dimension
))
4859 gfc_typespec temp_ts
= e
->ts
;
4860 gfc_add_class_array_ref (e
);
4866 if (se
->ignore_optional
)
4868 /* Some intrinsics have already been resolved to the correct
4872 else if (arg
->label
)
4874 has_alternate_specifier
= 1;
4879 gfc_init_se (&parmse
, NULL
);
4881 /* For scalar arguments with VALUE attribute which are passed by
4882 value, pass "0" and a hidden argument gives the optional
4884 if (fsym
&& fsym
->attr
.optional
&& fsym
->attr
.value
4885 && !fsym
->attr
.dimension
&& fsym
->ts
.type
!= BT_CHARACTER
4886 && fsym
->ts
.type
!= BT_CLASS
&& fsym
->ts
.type
!= BT_DERIVED
)
4888 parmse
.expr
= fold_convert (gfc_sym_type (fsym
),
4890 vec_safe_push (optionalargs
, boolean_false_node
);
4894 /* Pass a NULL pointer for an absent arg. */
4895 parmse
.expr
= null_pointer_node
;
4896 if (arg
->missing_arg_type
== BT_CHARACTER
)
4897 parmse
.string_length
= build_int_cst (gfc_charlen_type_node
,
4902 else if (arg
->expr
->expr_type
== EXPR_NULL
4903 && fsym
&& !fsym
->attr
.pointer
4904 && (fsym
->ts
.type
!= BT_CLASS
4905 || !CLASS_DATA (fsym
)->attr
.class_pointer
))
4907 /* Pass a NULL pointer to denote an absent arg. */
4908 gcc_assert (fsym
->attr
.optional
&& !fsym
->attr
.allocatable
4909 && (fsym
->ts
.type
!= BT_CLASS
4910 || !CLASS_DATA (fsym
)->attr
.allocatable
));
4911 gfc_init_se (&parmse
, NULL
);
4912 parmse
.expr
= null_pointer_node
;
4913 if (arg
->missing_arg_type
== BT_CHARACTER
)
4914 parmse
.string_length
= build_int_cst (gfc_charlen_type_node
, 0);
4916 else if (fsym
&& fsym
->ts
.type
== BT_CLASS
4917 && e
->ts
.type
== BT_DERIVED
)
4919 /* The derived type needs to be converted to a temporary
4921 gfc_init_se (&parmse
, se
);
4922 gfc_conv_derived_to_class (&parmse
, e
, fsym
->ts
, NULL
,
4924 && e
->expr_type
== EXPR_VARIABLE
4925 && e
->symtree
->n
.sym
->attr
.optional
,
4926 CLASS_DATA (fsym
)->attr
.class_pointer
4927 || CLASS_DATA (fsym
)->attr
.allocatable
);
4929 else if (UNLIMITED_POLY (fsym
) && e
->ts
.type
!= BT_CLASS
)
4931 /* The intrinsic type needs to be converted to a temporary
4932 CLASS object for the unlimited polymorphic formal. */
4933 gfc_init_se (&parmse
, se
);
4934 gfc_conv_intrinsic_to_class (&parmse
, e
, fsym
->ts
);
4936 else if (se
->ss
&& se
->ss
->info
->useflags
)
4942 /* An elemental function inside a scalarized loop. */
4943 gfc_init_se (&parmse
, se
);
4944 parm_kind
= ELEMENTAL
;
4946 /* When no fsym is present, ulim_copy is set and this is a third or
4947 fourth argument, use call-by-value instead of by reference to
4948 hand the length properties to the copy routine (i.e., most of the
4949 time this will be a call to a __copy_character_* routine where the
4950 third and fourth arguments are the lengths of a deferred length
4952 if ((fsym
&& fsym
->attr
.value
)
4953 || (ulim_copy
&& (argc
== 2 || argc
== 3)))
4954 gfc_conv_expr (&parmse
, e
);
4956 gfc_conv_expr_reference (&parmse
, e
);
4958 if (e
->ts
.type
== BT_CHARACTER
&& !e
->rank
4959 && e
->expr_type
== EXPR_FUNCTION
)
4960 parmse
.expr
= build_fold_indirect_ref_loc (input_location
,
4963 if (fsym
&& fsym
->ts
.type
== BT_DERIVED
4964 && gfc_is_class_container_ref (e
))
4966 parmse
.expr
= gfc_class_data_get (parmse
.expr
);
4968 if (fsym
->attr
.optional
&& e
->expr_type
== EXPR_VARIABLE
4969 && e
->symtree
->n
.sym
->attr
.optional
)
4971 tree cond
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
4972 parmse
.expr
= build3_loc (input_location
, COND_EXPR
,
4973 TREE_TYPE (parmse
.expr
),
4975 fold_convert (TREE_TYPE (parmse
.expr
),
4976 null_pointer_node
));
4980 /* If we are passing an absent array as optional dummy to an
4981 elemental procedure, make sure that we pass NULL when the data
4982 pointer is NULL. We need this extra conditional because of
4983 scalarization which passes arrays elements to the procedure,
4984 ignoring the fact that the array can be absent/unallocated/... */
4985 if (ss
->info
->can_be_null_ref
&& ss
->info
->type
!= GFC_SS_REFERENCE
)
4987 tree descriptor_data
;
4989 descriptor_data
= ss
->info
->data
.array
.data
;
4990 tmp
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
4992 fold_convert (TREE_TYPE (descriptor_data
),
4993 null_pointer_node
));
4995 = fold_build3_loc (input_location
, COND_EXPR
,
4996 TREE_TYPE (parmse
.expr
),
4997 gfc_unlikely (tmp
, PRED_FORTRAN_ABSENT_DUMMY
),
4998 fold_convert (TREE_TYPE (parmse
.expr
),
5003 /* The scalarizer does not repackage the reference to a class
5004 array - instead it returns a pointer to the data element. */
5005 if (fsym
&& fsym
->ts
.type
== BT_CLASS
&& e
->ts
.type
== BT_CLASS
)
5006 gfc_conv_class_to_class (&parmse
, e
, fsym
->ts
, true,
5007 fsym
->attr
.intent
!= INTENT_IN
5008 && (CLASS_DATA (fsym
)->attr
.class_pointer
5009 || CLASS_DATA (fsym
)->attr
.allocatable
),
5011 && e
->expr_type
== EXPR_VARIABLE
5012 && e
->symtree
->n
.sym
->attr
.optional
,
5013 CLASS_DATA (fsym
)->attr
.class_pointer
5014 || CLASS_DATA (fsym
)->attr
.allocatable
);
5021 gfc_init_se (&parmse
, NULL
);
5023 /* Check whether the expression is a scalar or not; we cannot use
5024 e->rank as it can be nonzero for functions arguments. */
5025 argss
= gfc_walk_expr (e
);
5026 scalar
= argss
== gfc_ss_terminator
;
5028 gfc_free_ss_chain (argss
);
5030 /* Special handling for passing scalar polymorphic coarrays;
5031 otherwise one passes "class->_data.data" instead of "&class". */
5032 if (e
->rank
== 0 && e
->ts
.type
== BT_CLASS
5033 && fsym
&& fsym
->ts
.type
== BT_CLASS
5034 && CLASS_DATA (fsym
)->attr
.codimension
5035 && !CLASS_DATA (fsym
)->attr
.dimension
)
5037 gfc_add_class_array_ref (e
);
5038 parmse
.want_coarray
= 1;
5042 /* A scalar or transformational function. */
5045 if (e
->expr_type
== EXPR_VARIABLE
5046 && e
->symtree
->n
.sym
->attr
.cray_pointee
5047 && fsym
&& fsym
->attr
.flavor
== FL_PROCEDURE
)
5049 /* The Cray pointer needs to be converted to a pointer to
5050 a type given by the expression. */
5051 gfc_conv_expr (&parmse
, e
);
5052 type
= build_pointer_type (TREE_TYPE (parmse
.expr
));
5053 tmp
= gfc_get_symbol_decl (e
->symtree
->n
.sym
->cp_pointer
);
5054 parmse
.expr
= convert (type
, tmp
);
5056 else if (fsym
&& fsym
->attr
.value
)
5058 if (fsym
->ts
.type
== BT_CHARACTER
5059 && fsym
->ts
.is_c_interop
5060 && fsym
->ns
->proc_name
!= NULL
5061 && fsym
->ns
->proc_name
->attr
.is_bind_c
)
5064 gfc_conv_scalar_char_value (fsym
, &parmse
, &e
);
5065 if (parmse
.expr
== NULL
)
5066 gfc_conv_expr (&parmse
, e
);
5070 gfc_conv_expr (&parmse
, e
);
5071 if (fsym
->attr
.optional
5072 && fsym
->ts
.type
!= BT_CLASS
5073 && fsym
->ts
.type
!= BT_DERIVED
)
5075 if (e
->expr_type
!= EXPR_VARIABLE
5076 || !e
->symtree
->n
.sym
->attr
.optional
5078 vec_safe_push (optionalargs
, boolean_true_node
);
5081 tmp
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
5082 if (!e
->symtree
->n
.sym
->attr
.value
)
5084 = fold_build3_loc (input_location
, COND_EXPR
,
5085 TREE_TYPE (parmse
.expr
),
5087 fold_convert (TREE_TYPE (parmse
.expr
),
5088 integer_zero_node
));
5090 vec_safe_push (optionalargs
, tmp
);
5095 else if (arg
->name
&& arg
->name
[0] == '%')
5096 /* Argument list functions %VAL, %LOC and %REF are signalled
5097 through arg->name. */
5098 conv_arglist_function (&parmse
, arg
->expr
, arg
->name
);
5099 else if ((e
->expr_type
== EXPR_FUNCTION
)
5100 && ((e
->value
.function
.esym
5101 && e
->value
.function
.esym
->result
->attr
.pointer
)
5102 || (!e
->value
.function
.esym
5103 && e
->symtree
->n
.sym
->attr
.pointer
))
5104 && fsym
&& fsym
->attr
.target
)
5106 gfc_conv_expr (&parmse
, e
);
5107 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
, parmse
.expr
);
5109 else if (e
->expr_type
== EXPR_FUNCTION
5110 && e
->symtree
->n
.sym
->result
5111 && e
->symtree
->n
.sym
->result
!= e
->symtree
->n
.sym
5112 && e
->symtree
->n
.sym
->result
->attr
.proc_pointer
)
5114 /* Functions returning procedure pointers. */
5115 gfc_conv_expr (&parmse
, e
);
5116 if (fsym
&& fsym
->attr
.proc_pointer
)
5117 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
, parmse
.expr
);
5121 if (e
->ts
.type
== BT_CLASS
&& fsym
5122 && fsym
->ts
.type
== BT_CLASS
5123 && (!CLASS_DATA (fsym
)->as
5124 || CLASS_DATA (fsym
)->as
->type
!= AS_ASSUMED_RANK
)
5125 && CLASS_DATA (e
)->attr
.codimension
)
5127 gcc_assert (!CLASS_DATA (fsym
)->attr
.codimension
);
5128 gcc_assert (!CLASS_DATA (fsym
)->as
);
5129 gfc_add_class_array_ref (e
);
5130 parmse
.want_coarray
= 1;
5131 gfc_conv_expr_reference (&parmse
, e
);
5132 class_scalar_coarray_to_class (&parmse
, e
, fsym
->ts
,
5134 && e
->expr_type
== EXPR_VARIABLE
);
5136 else if (e
->ts
.type
== BT_CLASS
&& fsym
5137 && fsym
->ts
.type
== BT_CLASS
5138 && !CLASS_DATA (fsym
)->as
5139 && !CLASS_DATA (e
)->as
5140 && strcmp (fsym
->ts
.u
.derived
->name
,
5141 e
->ts
.u
.derived
->name
))
5143 type
= gfc_typenode_for_spec (&fsym
->ts
);
5144 var
= gfc_create_var (type
, fsym
->name
);
5145 gfc_conv_expr (&parmse
, e
);
5146 if (fsym
->attr
.optional
5147 && e
->expr_type
== EXPR_VARIABLE
5148 && e
->symtree
->n
.sym
->attr
.optional
)
5152 tmp
= gfc_build_addr_expr (NULL_TREE
, parmse
.expr
);
5153 cond
= fold_build2_loc (input_location
, NE_EXPR
,
5154 boolean_type_node
, tmp
,
5155 fold_convert (TREE_TYPE (tmp
),
5156 null_pointer_node
));
5157 gfc_start_block (&block
);
5158 gfc_add_modify (&block
, var
,
5159 fold_build1_loc (input_location
,
5161 type
, parmse
.expr
));
5162 gfc_add_expr_to_block (&parmse
.pre
,
5163 fold_build3_loc (input_location
,
5164 COND_EXPR
, void_type_node
,
5165 cond
, gfc_finish_block (&block
),
5166 build_empty_stmt (input_location
)));
5167 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
, var
);
5168 parmse
.expr
= build3_loc (input_location
, COND_EXPR
,
5169 TREE_TYPE (parmse
.expr
),
5171 fold_convert (TREE_TYPE (parmse
.expr
),
5172 null_pointer_node
));
5176 gfc_add_modify (&parmse
.pre
, var
,
5177 fold_build1_loc (input_location
,
5179 type
, parmse
.expr
));
5180 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
, var
);
5184 gfc_conv_expr_reference (&parmse
, e
);
5186 /* Catch base objects that are not variables. */
5187 if (e
->ts
.type
== BT_CLASS
5188 && e
->expr_type
!= EXPR_VARIABLE
5189 && expr
&& e
== expr
->base_expr
)
5190 base_object
= build_fold_indirect_ref_loc (input_location
,
5193 /* A class array element needs converting back to be a
5194 class object, if the formal argument is a class object. */
5195 if (fsym
&& fsym
->ts
.type
== BT_CLASS
5196 && e
->ts
.type
== BT_CLASS
5197 && ((CLASS_DATA (fsym
)->as
5198 && CLASS_DATA (fsym
)->as
->type
== AS_ASSUMED_RANK
)
5199 || CLASS_DATA (e
)->attr
.dimension
))
5200 gfc_conv_class_to_class (&parmse
, e
, fsym
->ts
, false,
5201 fsym
->attr
.intent
!= INTENT_IN
5202 && (CLASS_DATA (fsym
)->attr
.class_pointer
5203 || CLASS_DATA (fsym
)->attr
.allocatable
),
5205 && e
->expr_type
== EXPR_VARIABLE
5206 && e
->symtree
->n
.sym
->attr
.optional
,
5207 CLASS_DATA (fsym
)->attr
.class_pointer
5208 || CLASS_DATA (fsym
)->attr
.allocatable
);
5210 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5211 allocated on entry, it must be deallocated. */
5212 if (fsym
&& fsym
->attr
.intent
== INTENT_OUT
5213 && (fsym
->attr
.allocatable
5214 || (fsym
->ts
.type
== BT_CLASS
5215 && CLASS_DATA (fsym
)->attr
.allocatable
)))
5220 gfc_init_block (&block
);
5222 if (e
->ts
.type
== BT_CLASS
)
5223 ptr
= gfc_class_data_get (ptr
);
5225 tmp
= gfc_deallocate_scalar_with_status (ptr
, NULL_TREE
,
5228 gfc_add_expr_to_block (&block
, tmp
);
5229 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
5230 void_type_node
, ptr
,
5232 gfc_add_expr_to_block (&block
, tmp
);
5234 if (fsym
->ts
.type
== BT_CLASS
&& UNLIMITED_POLY (fsym
))
5236 gfc_add_modify (&block
, ptr
,
5237 fold_convert (TREE_TYPE (ptr
),
5238 null_pointer_node
));
5239 gfc_add_expr_to_block (&block
, tmp
);
5241 else if (fsym
->ts
.type
== BT_CLASS
)
5244 vtab
= gfc_find_derived_vtab (fsym
->ts
.u
.derived
);
5245 tmp
= gfc_get_symbol_decl (vtab
);
5246 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
5247 ptr
= gfc_class_vptr_get (parmse
.expr
);
5248 gfc_add_modify (&block
, ptr
,
5249 fold_convert (TREE_TYPE (ptr
), tmp
));
5250 gfc_add_expr_to_block (&block
, tmp
);
5253 if (fsym
->attr
.optional
5254 && e
->expr_type
== EXPR_VARIABLE
5255 && e
->symtree
->n
.sym
->attr
.optional
)
5257 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
5259 gfc_conv_expr_present (e
->symtree
->n
.sym
),
5260 gfc_finish_block (&block
),
5261 build_empty_stmt (input_location
));
5264 tmp
= gfc_finish_block (&block
);
5266 gfc_add_expr_to_block (&se
->pre
, tmp
);
5269 if (fsym
&& (fsym
->ts
.type
== BT_DERIVED
5270 || fsym
->ts
.type
== BT_ASSUMED
)
5271 && e
->ts
.type
== BT_CLASS
5272 && !CLASS_DATA (e
)->attr
.dimension
5273 && !CLASS_DATA (e
)->attr
.codimension
)
5274 parmse
.expr
= gfc_class_data_get (parmse
.expr
);
5276 /* Wrap scalar variable in a descriptor. We need to convert
5277 the address of a pointer back to the pointer itself before,
5278 we can assign it to the data field. */
5280 if (fsym
&& fsym
->as
&& fsym
->as
->type
== AS_ASSUMED_RANK
5281 && fsym
->ts
.type
!= BT_CLASS
&& e
->expr_type
!= EXPR_NULL
)
5284 if (TREE_CODE (tmp
) == ADDR_EXPR
5285 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp
, 0))))
5286 tmp
= TREE_OPERAND (tmp
, 0);
5287 parmse
.expr
= gfc_conv_scalar_to_descriptor (&parmse
, tmp
,
5289 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
,
5292 else if (fsym
&& e
->expr_type
!= EXPR_NULL
5293 && ((fsym
->attr
.pointer
5294 && fsym
->attr
.flavor
!= FL_PROCEDURE
)
5295 || (fsym
->attr
.proc_pointer
5296 && !(e
->expr_type
== EXPR_VARIABLE
5297 && e
->symtree
->n
.sym
->attr
.dummy
))
5298 || (fsym
->attr
.proc_pointer
5299 && e
->expr_type
== EXPR_VARIABLE
5300 && gfc_is_proc_ptr_comp (e
))
5301 || (fsym
->attr
.allocatable
5302 && fsym
->attr
.flavor
!= FL_PROCEDURE
)))
5304 /* Scalar pointer dummy args require an extra level of
5305 indirection. The null pointer already contains
5306 this level of indirection. */
5307 parm_kind
= SCALAR_POINTER
;
5308 parmse
.expr
= gfc_build_addr_expr (NULL_TREE
, parmse
.expr
);
5312 else if (e
->ts
.type
== BT_CLASS
5313 && fsym
&& fsym
->ts
.type
== BT_CLASS
5314 && (CLASS_DATA (fsym
)->attr
.dimension
5315 || CLASS_DATA (fsym
)->attr
.codimension
))
5317 /* Pass a class array. */
5318 parmse
.use_offset
= 1;
5319 gfc_conv_expr_descriptor (&parmse
, e
);
5321 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5322 allocated on entry, it must be deallocated. */
5323 if (fsym
->attr
.intent
== INTENT_OUT
5324 && CLASS_DATA (fsym
)->attr
.allocatable
)
5329 gfc_init_block (&block
);
5331 ptr
= gfc_class_data_get (ptr
);
5333 tmp
= gfc_deallocate_with_status (ptr
, NULL_TREE
,
5334 NULL_TREE
, NULL_TREE
,
5336 GFC_CAF_COARRAY_NOCOARRAY
);
5337 gfc_add_expr_to_block (&block
, tmp
);
5338 tmp
= fold_build2_loc (input_location
, MODIFY_EXPR
,
5339 void_type_node
, ptr
,
5341 gfc_add_expr_to_block (&block
, tmp
);
5342 gfc_reset_vptr (&block
, e
);
5344 if (fsym
->attr
.optional
5345 && e
->expr_type
== EXPR_VARIABLE
5347 || (e
->ref
->type
== REF_ARRAY
5348 && e
->ref
->u
.ar
.type
!= AR_FULL
))
5349 && e
->symtree
->n
.sym
->attr
.optional
)
5351 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
5353 gfc_conv_expr_present (e
->symtree
->n
.sym
),
5354 gfc_finish_block (&block
),
5355 build_empty_stmt (input_location
));
5358 tmp
= gfc_finish_block (&block
);
5360 gfc_add_expr_to_block (&se
->pre
, tmp
);
5363 /* The conversion does not repackage the reference to a class
5364 array - _data descriptor. */
5365 gfc_conv_class_to_class (&parmse
, e
, fsym
->ts
, false,
5366 fsym
->attr
.intent
!= INTENT_IN
5367 && (CLASS_DATA (fsym
)->attr
.class_pointer
5368 || CLASS_DATA (fsym
)->attr
.allocatable
),
5370 && e
->expr_type
== EXPR_VARIABLE
5371 && e
->symtree
->n
.sym
->attr
.optional
,
5372 CLASS_DATA (fsym
)->attr
.class_pointer
5373 || CLASS_DATA (fsym
)->attr
.allocatable
);
5377 /* If the argument is a function call that may not create
5378 a temporary for the result, we have to check that we
5379 can do it, i.e. that there is no alias between this
5380 argument and another one. */
5381 if (gfc_get_noncopying_intrinsic_argument (e
) != NULL
)
5387 intent
= fsym
->attr
.intent
;
5389 intent
= INTENT_UNKNOWN
;
5391 if (gfc_check_fncall_dependency (e
, intent
, sym
, args
,
5393 parmse
.force_tmp
= 1;
5395 iarg
= e
->value
.function
.actual
->expr
;
5397 /* Temporary needed if aliasing due to host association. */
5398 if (sym
->attr
.contained
5400 && !sym
->attr
.implicit_pure
5401 && !sym
->attr
.use_assoc
5402 && iarg
->expr_type
== EXPR_VARIABLE
5403 && sym
->ns
== iarg
->symtree
->n
.sym
->ns
)
5404 parmse
.force_tmp
= 1;
5406 /* Ditto within module. */
5407 if (sym
->attr
.use_assoc
5409 && !sym
->attr
.implicit_pure
5410 && iarg
->expr_type
== EXPR_VARIABLE
5411 && sym
->module
== iarg
->symtree
->n
.sym
->module
)
5412 parmse
.force_tmp
= 1;
5415 if (e
->expr_type
== EXPR_VARIABLE
5416 && is_subref_array (e
))
5417 /* The actual argument is a component reference to an
5418 array of derived types. In this case, the argument
5419 is converted to a temporary, which is passed and then
5420 written back after the procedure call. */
5421 gfc_conv_subref_array_arg (&parmse
, e
, nodesc_arg
,
5422 fsym
? fsym
->attr
.intent
: INTENT_INOUT
,
5423 fsym
&& fsym
->attr
.pointer
);
5424 else if (gfc_is_class_array_ref (e
, NULL
)
5425 && fsym
&& fsym
->ts
.type
== BT_DERIVED
)
5426 /* The actual argument is a component reference to an
5427 array of derived types. In this case, the argument
5428 is converted to a temporary, which is passed and then
5429 written back after the procedure call.
5430 OOP-TODO: Insert code so that if the dynamic type is
5431 the same as the declared type, copy-in/copy-out does
5433 gfc_conv_subref_array_arg (&parmse
, e
, nodesc_arg
,
5434 fsym
? fsym
->attr
.intent
: INTENT_INOUT
,
5435 fsym
&& fsym
->attr
.pointer
);
5437 else if (gfc_is_alloc_class_array_function (e
)
5438 && fsym
&& fsym
->ts
.type
== BT_DERIVED
)
5439 /* See previous comment. For function actual argument,
5440 the write out is not needed so the intent is set as
5443 e
->must_finalize
= 1;
5444 gfc_conv_subref_array_arg (&parmse
, e
, nodesc_arg
,
5446 fsym
&& fsym
->attr
.pointer
);
5449 gfc_conv_array_parameter (&parmse
, e
, nodesc_arg
, fsym
,
5452 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5453 allocated on entry, it must be deallocated. */
5454 if (fsym
&& fsym
->attr
.allocatable
5455 && fsym
->attr
.intent
== INTENT_OUT
)
5457 tmp
= build_fold_indirect_ref_loc (input_location
,
5459 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp
)))
5460 tmp
= gfc_conv_descriptor_data_get (tmp
);
5461 tmp
= gfc_deallocate_with_status (tmp
, NULL_TREE
, NULL_TREE
,
5462 NULL_TREE
, NULL_TREE
, true,
5464 GFC_CAF_COARRAY_NOCOARRAY
);
5465 if (fsym
->attr
.optional
5466 && e
->expr_type
== EXPR_VARIABLE
5467 && e
->symtree
->n
.sym
->attr
.optional
)
5468 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
5470 gfc_conv_expr_present (e
->symtree
->n
.sym
),
5471 tmp
, build_empty_stmt (input_location
));
5472 gfc_add_expr_to_block (&se
->pre
, tmp
);
5477 /* The case with fsym->attr.optional is that of a user subroutine
5478 with an interface indicating an optional argument. When we call
5479 an intrinsic subroutine, however, fsym is NULL, but we might still
5480 have an optional argument, so we proceed to the substitution
5482 if (e
&& (fsym
== NULL
|| fsym
->attr
.optional
))
5484 /* If an optional argument is itself an optional dummy argument,
5485 check its presence and substitute a null if absent. This is
5486 only needed when passing an array to an elemental procedure
5487 as then array elements are accessed - or no NULL pointer is
5488 allowed and a "1" or "0" should be passed if not present.
5489 When passing a non-array-descriptor full array to a
5490 non-array-descriptor dummy, no check is needed. For
5491 array-descriptor actual to array-descriptor dummy, see
5492 PR 41911 for why a check has to be inserted.
5493 fsym == NULL is checked as intrinsics required the descriptor
5494 but do not always set fsym. */
5495 if (e
->expr_type
== EXPR_VARIABLE
5496 && e
->symtree
->n
.sym
->attr
.optional
5497 && ((e
->rank
!= 0 && elemental_proc
)
5498 || e
->representation
.length
|| e
->ts
.type
== BT_CHARACTER
5502 && (fsym
->as
->type
== AS_ASSUMED_SHAPE
5503 || fsym
->as
->type
== AS_ASSUMED_RANK
5504 || fsym
->as
->type
== AS_DEFERRED
))))))
5505 gfc_conv_missing_dummy (&parmse
, e
, fsym
? fsym
->ts
: e
->ts
,
5506 e
->representation
.length
);
5511 /* Obtain the character length of an assumed character length
5512 length procedure from the typespec. */
5513 if (fsym
->ts
.type
== BT_CHARACTER
5514 && parmse
.string_length
== NULL_TREE
5515 && e
->ts
.type
== BT_PROCEDURE
5516 && e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
5517 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
!= NULL
5518 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
5520 gfc_conv_const_charlen (e
->symtree
->n
.sym
->ts
.u
.cl
);
5521 parmse
.string_length
= e
->symtree
->n
.sym
->ts
.u
.cl
->backend_decl
;
5525 if (fsym
&& need_interface_mapping
&& e
)
5526 gfc_add_interface_mapping (&mapping
, fsym
, &parmse
, e
);
5528 gfc_add_block_to_block (&se
->pre
, &parmse
.pre
);
5529 gfc_add_block_to_block (&post
, &parmse
.post
);
5531 /* Allocated allocatable components of derived types must be
5532 deallocated for non-variable scalars, array arguments to elemental
5533 procedures, and array arguments with descriptor to non-elemental
5534 procedures. As bounds information for descriptorless arrays is no
5535 longer available here, they are dealt with in trans-array.c
5536 (gfc_conv_array_parameter). */
5537 if (e
&& (e
->ts
.type
== BT_DERIVED
|| e
->ts
.type
== BT_CLASS
)
5538 && e
->ts
.u
.derived
->attr
.alloc_comp
5539 && (e
->rank
== 0 || elemental_proc
|| !nodesc_arg
)
5540 && !expr_may_alias_variables (e
, elemental_proc
))
5543 /* It is known the e returns a structure type with at least one
5544 allocatable component. When e is a function, ensure that the
5545 function is called once only by using a temporary variable. */
5546 if (!DECL_P (parmse
.expr
))
5547 parmse
.expr
= gfc_evaluate_now_loc (input_location
,
5548 parmse
.expr
, &se
->pre
);
5550 if (fsym
&& fsym
->attr
.value
)
5553 tmp
= build_fold_indirect_ref_loc (input_location
,
5556 parm_rank
= e
->rank
;
5564 case (SCALAR_POINTER
):
5565 tmp
= build_fold_indirect_ref_loc (input_location
,
5570 if (e
->expr_type
== EXPR_OP
5571 && e
->value
.op
.op
== INTRINSIC_PARENTHESES
5572 && e
->value
.op
.op1
->expr_type
== EXPR_VARIABLE
)
5575 local_tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
5576 local_tmp
= gfc_copy_alloc_comp (e
->ts
.u
.derived
, local_tmp
, tmp
,
5578 gfc_add_expr_to_block (&se
->post
, local_tmp
);
5581 if (e
->ts
.type
== BT_DERIVED
&& fsym
&& fsym
->ts
.type
== BT_CLASS
)
5583 /* The derived type is passed to gfc_deallocate_alloc_comp.
5584 Therefore, class actuals can handled correctly but derived
5585 types passed to class formals need the _data component. */
5586 tmp
= gfc_class_data_get (tmp
);
5587 if (!CLASS_DATA (fsym
)->attr
.dimension
)
5588 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
5591 tmp
= gfc_deallocate_alloc_comp (e
->ts
.u
.derived
, tmp
, parm_rank
);
5593 gfc_prepend_expr_to_block (&post
, tmp
);
5596 /* Add argument checking of passing an unallocated/NULL actual to
5597 a nonallocatable/nonpointer dummy. */
5599 if (gfc_option
.rtcheck
& GFC_RTCHECK_POINTER
&& e
!= NULL
)
5601 symbol_attribute attr
;
5605 if (e
->expr_type
== EXPR_VARIABLE
|| e
->expr_type
== EXPR_FUNCTION
)
5606 attr
= gfc_expr_attr (e
);
5608 goto end_pointer_check
;
5610 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
5611 allocatable to an optional dummy, cf. 12.5.2.12. */
5612 if (fsym
!= NULL
&& fsym
->attr
.optional
&& !attr
.proc_pointer
5613 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
5614 goto end_pointer_check
;
5618 /* If the actual argument is an optional pointer/allocatable and
5619 the formal argument takes an nonpointer optional value,
5620 it is invalid to pass a non-present argument on, even
5621 though there is no technical reason for this in gfortran.
5622 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
5623 tree present
, null_ptr
, type
;
5625 if (attr
.allocatable
5626 && (fsym
== NULL
|| !fsym
->attr
.allocatable
))
5627 msg
= xasprintf ("Allocatable actual argument '%s' is not "
5628 "allocated or not present",
5629 e
->symtree
->n
.sym
->name
);
5630 else if (attr
.pointer
5631 && (fsym
== NULL
|| !fsym
->attr
.pointer
))
5632 msg
= xasprintf ("Pointer actual argument '%s' is not "
5633 "associated or not present",
5634 e
->symtree
->n
.sym
->name
);
5635 else if (attr
.proc_pointer
5636 && (fsym
== NULL
|| !fsym
->attr
.proc_pointer
))
5637 msg
= xasprintf ("Proc-pointer actual argument '%s' is not "
5638 "associated or not present",
5639 e
->symtree
->n
.sym
->name
);
5641 goto end_pointer_check
;
5643 present
= gfc_conv_expr_present (e
->symtree
->n
.sym
);
5644 type
= TREE_TYPE (present
);
5645 present
= fold_build2_loc (input_location
, EQ_EXPR
,
5646 boolean_type_node
, present
,
5648 null_pointer_node
));
5649 type
= TREE_TYPE (parmse
.expr
);
5650 null_ptr
= fold_build2_loc (input_location
, EQ_EXPR
,
5651 boolean_type_node
, parmse
.expr
,
5653 null_pointer_node
));
5654 cond
= fold_build2_loc (input_location
, TRUTH_ORIF_EXPR
,
5655 boolean_type_node
, present
, null_ptr
);
5659 if (attr
.allocatable
5660 && (fsym
== NULL
|| !fsym
->attr
.allocatable
))
5661 msg
= xasprintf ("Allocatable actual argument '%s' is not "
5662 "allocated", e
->symtree
->n
.sym
->name
);
5663 else if (attr
.pointer
5664 && (fsym
== NULL
|| !fsym
->attr
.pointer
))
5665 msg
= xasprintf ("Pointer actual argument '%s' is not "
5666 "associated", e
->symtree
->n
.sym
->name
);
5667 else if (attr
.proc_pointer
5668 && (fsym
== NULL
|| !fsym
->attr
.proc_pointer
))
5669 msg
= xasprintf ("Proc-pointer actual argument '%s' is not "
5670 "associated", e
->symtree
->n
.sym
->name
);
5672 goto end_pointer_check
;
5676 /* If the argument is passed by value, we need to strip the
5678 if (!POINTER_TYPE_P (TREE_TYPE (parmse
.expr
)))
5679 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
5681 cond
= fold_build2_loc (input_location
, EQ_EXPR
,
5682 boolean_type_node
, tmp
,
5683 fold_convert (TREE_TYPE (tmp
),
5684 null_pointer_node
));
5687 gfc_trans_runtime_check (true, false, cond
, &se
->pre
, &e
->where
,
5693 /* Deferred length dummies pass the character length by reference
5694 so that the value can be returned. */
5695 if (parmse
.string_length
&& fsym
&& fsym
->ts
.deferred
)
5697 if (INDIRECT_REF_P (parmse
.string_length
))
5698 /* In chains of functions/procedure calls the string_length already
5699 is a pointer to the variable holding the length. Therefore
5700 remove the deref on call. */
5701 parmse
.string_length
= TREE_OPERAND (parmse
.string_length
, 0);
5704 tmp
= parmse
.string_length
;
5705 if (!VAR_P (tmp
) && TREE_CODE (tmp
) != COMPONENT_REF
)
5706 tmp
= gfc_evaluate_now (parmse
.string_length
, &se
->pre
);
5707 parmse
.string_length
= gfc_build_addr_expr (NULL_TREE
, tmp
);
5711 /* Character strings are passed as two parameters, a length and a
5712 pointer - except for Bind(c) which only passes the pointer.
5713 An unlimited polymorphic formal argument likewise does not
5715 if (parmse
.string_length
!= NULL_TREE
5716 && !sym
->attr
.is_bind_c
5717 && !(fsym
&& UNLIMITED_POLY (fsym
)))
5718 vec_safe_push (stringargs
, parmse
.string_length
);
5720 /* When calling __copy for character expressions to unlimited
5721 polymorphic entities, the dst argument needs a string length. */
5722 if (sym
->name
[0] == '_' && e
&& e
->ts
.type
== BT_CHARACTER
5723 && strncmp (sym
->name
, "__vtab_CHARACTER", 16) == 0
5724 && arg
->next
&& arg
->next
->expr
5725 && (arg
->next
->expr
->ts
.type
== BT_DERIVED
5726 || arg
->next
->expr
->ts
.type
== BT_CLASS
)
5727 && arg
->next
->expr
->ts
.u
.derived
->attr
.unlimited_polymorphic
)
5728 vec_safe_push (stringargs
, parmse
.string_length
);
5730 /* For descriptorless coarrays and assumed-shape coarray dummies, we
5731 pass the token and the offset as additional arguments. */
5732 if (fsym
&& e
== NULL
&& flag_coarray
== GFC_FCOARRAY_LIB
5733 && ((fsym
->ts
.type
!= BT_CLASS
&& fsym
->attr
.codimension
5734 && !fsym
->attr
.allocatable
)
5735 || (fsym
->ts
.type
== BT_CLASS
5736 && CLASS_DATA (fsym
)->attr
.codimension
5737 && !CLASS_DATA (fsym
)->attr
.allocatable
)))
5739 /* Token and offset. */
5740 vec_safe_push (stringargs
, null_pointer_node
);
5741 vec_safe_push (stringargs
, build_int_cst (gfc_array_index_type
, 0));
5742 gcc_assert (fsym
->attr
.optional
);
5744 else if (fsym
&& flag_coarray
== GFC_FCOARRAY_LIB
5745 && ((fsym
->ts
.type
!= BT_CLASS
&& fsym
->attr
.codimension
5746 && !fsym
->attr
.allocatable
)
5747 || (fsym
->ts
.type
== BT_CLASS
5748 && CLASS_DATA (fsym
)->attr
.codimension
5749 && !CLASS_DATA (fsym
)->attr
.allocatable
)))
5751 tree caf_decl
, caf_type
;
5754 caf_decl
= gfc_get_tree_for_caf_expr (e
);
5755 caf_type
= TREE_TYPE (caf_decl
);
5757 if (GFC_DESCRIPTOR_TYPE_P (caf_type
)
5758 && (GFC_TYPE_ARRAY_AKIND (caf_type
) == GFC_ARRAY_ALLOCATABLE
5759 || GFC_TYPE_ARRAY_AKIND (caf_type
) == GFC_ARRAY_POINTER
))
5760 tmp
= gfc_conv_descriptor_token (caf_decl
);
5761 else if (DECL_LANG_SPECIFIC (caf_decl
)
5762 && GFC_DECL_TOKEN (caf_decl
) != NULL_TREE
)
5763 tmp
= GFC_DECL_TOKEN (caf_decl
);
5766 gcc_assert (GFC_ARRAY_TYPE_P (caf_type
)
5767 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
) != NULL_TREE
);
5768 tmp
= GFC_TYPE_ARRAY_CAF_TOKEN (caf_type
);
5771 vec_safe_push (stringargs
, tmp
);
5773 if (GFC_DESCRIPTOR_TYPE_P (caf_type
)
5774 && GFC_TYPE_ARRAY_AKIND (caf_type
) == GFC_ARRAY_ALLOCATABLE
)
5775 offset
= build_int_cst (gfc_array_index_type
, 0);
5776 else if (DECL_LANG_SPECIFIC (caf_decl
)
5777 && GFC_DECL_CAF_OFFSET (caf_decl
) != NULL_TREE
)
5778 offset
= GFC_DECL_CAF_OFFSET (caf_decl
);
5779 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
) != NULL_TREE
)
5780 offset
= GFC_TYPE_ARRAY_CAF_OFFSET (caf_type
);
5782 offset
= build_int_cst (gfc_array_index_type
, 0);
5784 if (GFC_DESCRIPTOR_TYPE_P (caf_type
))
5785 tmp
= gfc_conv_descriptor_data_get (caf_decl
);
5788 gcc_assert (POINTER_TYPE_P (caf_type
));
5792 tmp2
= fsym
->ts
.type
== BT_CLASS
5793 ? gfc_class_data_get (parmse
.expr
) : parmse
.expr
;
5794 if ((fsym
->ts
.type
!= BT_CLASS
5795 && (fsym
->as
->type
== AS_ASSUMED_SHAPE
5796 || fsym
->as
->type
== AS_ASSUMED_RANK
))
5797 || (fsym
->ts
.type
== BT_CLASS
5798 && (CLASS_DATA (fsym
)->as
->type
== AS_ASSUMED_SHAPE
5799 || CLASS_DATA (fsym
)->as
->type
== AS_ASSUMED_RANK
)))
5801 if (fsym
->ts
.type
== BT_CLASS
)
5802 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2
)));
5805 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2
)));
5806 tmp2
= build_fold_indirect_ref_loc (input_location
, tmp2
);
5808 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2
)));
5809 tmp2
= gfc_conv_descriptor_data_get (tmp2
);
5811 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2
)))
5812 tmp2
= gfc_conv_descriptor_data_get (tmp2
);
5815 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2
)));
5818 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
5819 gfc_array_index_type
,
5820 fold_convert (gfc_array_index_type
, tmp2
),
5821 fold_convert (gfc_array_index_type
, tmp
));
5822 offset
= fold_build2_loc (input_location
, PLUS_EXPR
,
5823 gfc_array_index_type
, offset
, tmp
);
5825 vec_safe_push (stringargs
, offset
);
5828 vec_safe_push (arglist
, parmse
.expr
);
5830 gfc_finish_interface_mapping (&mapping
, &se
->pre
, &se
->post
);
5834 else if (sym
->ts
.type
== BT_CLASS
)
5835 ts
= CLASS_DATA (sym
)->ts
;
5839 if (ts
.type
== BT_CHARACTER
&& sym
->attr
.is_bind_c
)
5840 se
->string_length
= build_int_cst (gfc_charlen_type_node
, 1);
5841 else if (ts
.type
== BT_CHARACTER
)
5843 if (ts
.u
.cl
->length
== NULL
)
5845 /* Assumed character length results are not allowed by 5.1.1.5 of the
5846 standard and are trapped in resolve.c; except in the case of SPREAD
5847 (and other intrinsics?) and dummy functions. In the case of SPREAD,
5848 we take the character length of the first argument for the result.
5849 For dummies, we have to look through the formal argument list for
5850 this function and use the character length found there.*/
5852 cl
.backend_decl
= gfc_create_var (gfc_charlen_type_node
, "slen");
5853 else if (!sym
->attr
.dummy
)
5854 cl
.backend_decl
= (*stringargs
)[0];
5857 formal
= gfc_sym_get_dummy_args (sym
->ns
->proc_name
);
5858 for (; formal
; formal
= formal
->next
)
5859 if (strcmp (formal
->sym
->name
, sym
->name
) == 0)
5860 cl
.backend_decl
= formal
->sym
->ts
.u
.cl
->backend_decl
;
5862 len
= cl
.backend_decl
;
5868 /* Calculate the length of the returned string. */
5869 gfc_init_se (&parmse
, NULL
);
5870 if (need_interface_mapping
)
5871 gfc_apply_interface_mapping (&mapping
, &parmse
, ts
.u
.cl
->length
);
5873 gfc_conv_expr (&parmse
, ts
.u
.cl
->length
);
5874 gfc_add_block_to_block (&se
->pre
, &parmse
.pre
);
5875 gfc_add_block_to_block (&se
->post
, &parmse
.post
);
5877 tmp
= fold_convert (gfc_charlen_type_node
, parmse
.expr
);
5878 tmp
= fold_build2_loc (input_location
, MAX_EXPR
,
5879 gfc_charlen_type_node
, tmp
,
5880 build_int_cst (gfc_charlen_type_node
, 0));
5881 cl
.backend_decl
= tmp
;
5884 /* Set up a charlen structure for it. */
5889 len
= cl
.backend_decl
;
5892 byref
= (comp
&& (comp
->attr
.dimension
5893 || (comp
->ts
.type
== BT_CHARACTER
&& !sym
->attr
.is_bind_c
)))
5894 || (!comp
&& gfc_return_by_reference (sym
));
5897 if (se
->direct_byref
)
5899 /* Sometimes, too much indirection can be applied; e.g. for
5900 function_result = array_valued_recursive_function. */
5901 if (TREE_TYPE (TREE_TYPE (se
->expr
))
5902 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se
->expr
)))
5903 && GFC_DESCRIPTOR_TYPE_P
5904 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se
->expr
)))))
5905 se
->expr
= build_fold_indirect_ref_loc (input_location
,
5908 /* If the lhs of an assignment x = f(..) is allocatable and
5909 f2003 is allowed, we must do the automatic reallocation.
5910 TODO - deal with intrinsics, without using a temporary. */
5911 if (flag_realloc_lhs
5912 && se
->ss
&& se
->ss
->loop_chain
5913 && se
->ss
->loop_chain
->is_alloc_lhs
5914 && !expr
->value
.function
.isym
5915 && sym
->result
->as
!= NULL
)
5917 /* Evaluate the bounds of the result, if known. */
5918 gfc_set_loop_bounds_from_array_spec (&mapping
, se
,
5921 /* Perform the automatic reallocation. */
5922 tmp
= gfc_alloc_allocatable_for_assignment (se
->loop
,
5924 gfc_add_expr_to_block (&se
->pre
, tmp
);
5926 /* Pass the temporary as the first argument. */
5927 result
= info
->descriptor
;
5930 result
= build_fold_indirect_ref_loc (input_location
,
5932 vec_safe_push (retargs
, se
->expr
);
5934 else if (comp
&& comp
->attr
.dimension
)
5936 gcc_assert (se
->loop
&& info
);
5938 /* Set the type of the array. */
5939 tmp
= gfc_typenode_for_spec (&comp
->ts
);
5940 gcc_assert (se
->ss
->dimen
== se
->loop
->dimen
);
5942 /* Evaluate the bounds of the result, if known. */
5943 gfc_set_loop_bounds_from_array_spec (&mapping
, se
, comp
->as
);
5945 /* If the lhs of an assignment x = f(..) is allocatable and
5946 f2003 is allowed, we must not generate the function call
5947 here but should just send back the results of the mapping.
5948 This is signalled by the function ss being flagged. */
5949 if (flag_realloc_lhs
&& se
->ss
&& se
->ss
->is_alloc_lhs
)
5951 gfc_free_interface_mapping (&mapping
);
5952 return has_alternate_specifier
;
5955 /* Create a temporary to store the result. In case the function
5956 returns a pointer, the temporary will be a shallow copy and
5957 mustn't be deallocated. */
5958 callee_alloc
= comp
->attr
.allocatable
|| comp
->attr
.pointer
;
5959 gfc_trans_create_temp_array (&se
->pre
, &se
->post
, se
->ss
,
5960 tmp
, NULL_TREE
, false,
5961 !comp
->attr
.pointer
, callee_alloc
,
5962 &se
->ss
->info
->expr
->where
);
5964 /* Pass the temporary as the first argument. */
5965 result
= info
->descriptor
;
5966 tmp
= gfc_build_addr_expr (NULL_TREE
, result
);
5967 vec_safe_push (retargs
, tmp
);
5969 else if (!comp
&& sym
->result
->attr
.dimension
)
5971 gcc_assert (se
->loop
&& info
);
5973 /* Set the type of the array. */
5974 tmp
= gfc_typenode_for_spec (&ts
);
5975 gcc_assert (se
->ss
->dimen
== se
->loop
->dimen
);
5977 /* Evaluate the bounds of the result, if known. */
5978 gfc_set_loop_bounds_from_array_spec (&mapping
, se
, sym
->result
->as
);
5980 /* If the lhs of an assignment x = f(..) is allocatable and
5981 f2003 is allowed, we must not generate the function call
5982 here but should just send back the results of the mapping.
5983 This is signalled by the function ss being flagged. */
5984 if (flag_realloc_lhs
&& se
->ss
&& se
->ss
->is_alloc_lhs
)
5986 gfc_free_interface_mapping (&mapping
);
5987 return has_alternate_specifier
;
5990 /* Create a temporary to store the result. In case the function
5991 returns a pointer, the temporary will be a shallow copy and
5992 mustn't be deallocated. */
5993 callee_alloc
= sym
->attr
.allocatable
|| sym
->attr
.pointer
;
5994 gfc_trans_create_temp_array (&se
->pre
, &se
->post
, se
->ss
,
5995 tmp
, NULL_TREE
, false,
5996 !sym
->attr
.pointer
, callee_alloc
,
5997 &se
->ss
->info
->expr
->where
);
5999 /* Pass the temporary as the first argument. */
6000 result
= info
->descriptor
;
6001 tmp
= gfc_build_addr_expr (NULL_TREE
, result
);
6002 vec_safe_push (retargs
, tmp
);
6004 else if (ts
.type
== BT_CHARACTER
)
6006 /* Pass the string length. */
6007 type
= gfc_get_character_type (ts
.kind
, ts
.u
.cl
);
6008 type
= build_pointer_type (type
);
6010 /* Emit a DECL_EXPR for the VLA type. */
6011 tmp
= TREE_TYPE (type
);
6013 && TREE_CODE (TYPE_SIZE (tmp
)) != INTEGER_CST
)
6015 tmp
= build_decl (input_location
, TYPE_DECL
, NULL_TREE
, tmp
);
6016 DECL_ARTIFICIAL (tmp
) = 1;
6017 DECL_IGNORED_P (tmp
) = 1;
6018 tmp
= fold_build1_loc (input_location
, DECL_EXPR
,
6019 TREE_TYPE (tmp
), tmp
);
6020 gfc_add_expr_to_block (&se
->pre
, tmp
);
6023 /* Return an address to a char[0:len-1]* temporary for
6024 character pointers. */
6025 if ((!comp
&& (sym
->attr
.pointer
|| sym
->attr
.allocatable
))
6026 || (comp
&& (comp
->attr
.pointer
|| comp
->attr
.allocatable
)))
6028 var
= gfc_create_var (type
, "pstr");
6030 if ((!comp
&& sym
->attr
.allocatable
)
6031 || (comp
&& comp
->attr
.allocatable
))
6033 gfc_add_modify (&se
->pre
, var
,
6034 fold_convert (TREE_TYPE (var
),
6035 null_pointer_node
));
6036 tmp
= gfc_call_free (var
);
6037 gfc_add_expr_to_block (&se
->post
, tmp
);
6040 /* Provide an address expression for the function arguments. */
6041 var
= gfc_build_addr_expr (NULL_TREE
, var
);
6044 var
= gfc_conv_string_tmp (se
, type
, len
);
6046 vec_safe_push (retargs
, var
);
6050 gcc_assert (flag_f2c
&& ts
.type
== BT_COMPLEX
);
6052 type
= gfc_get_complex_type (ts
.kind
);
6053 var
= gfc_build_addr_expr (NULL_TREE
, gfc_create_var (type
, "cmplx"));
6054 vec_safe_push (retargs
, var
);
6057 /* Add the string length to the argument list. */
6058 if (ts
.type
== BT_CHARACTER
&& ts
.deferred
)
6062 tmp
= gfc_evaluate_now (len
, &se
->pre
);
6063 TREE_STATIC (tmp
) = 1;
6064 gfc_add_modify (&se
->pre
, tmp
,
6065 build_int_cst (TREE_TYPE (tmp
), 0));
6066 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
6067 vec_safe_push (retargs
, tmp
);
6069 else if (ts
.type
== BT_CHARACTER
)
6070 vec_safe_push (retargs
, len
);
6072 gfc_free_interface_mapping (&mapping
);
6074 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
6075 arglen
= (vec_safe_length (arglist
) + vec_safe_length (optionalargs
)
6076 + vec_safe_length (stringargs
) + vec_safe_length (append_args
));
6077 vec_safe_reserve (retargs
, arglen
);
6079 /* Add the return arguments. */
6080 vec_safe_splice (retargs
, arglist
);
6082 /* Add the hidden present status for optional+value to the arguments. */
6083 vec_safe_splice (retargs
, optionalargs
);
6085 /* Add the hidden string length parameters to the arguments. */
6086 vec_safe_splice (retargs
, stringargs
);
6088 /* We may want to append extra arguments here. This is used e.g. for
6089 calls to libgfortran_matmul_??, which need extra information. */
6090 vec_safe_splice (retargs
, append_args
);
6094 /* Generate the actual call. */
6095 if (base_object
== NULL_TREE
)
6096 conv_function_val (se
, sym
, expr
);
6098 conv_base_obj_fcn_val (se
, base_object
, expr
);
6100 /* If there are alternate return labels, function type should be
6101 integer. Can't modify the type in place though, since it can be shared
6102 with other functions. For dummy arguments, the typing is done to
6103 this result, even if it has to be repeated for each call. */
6104 if (has_alternate_specifier
6105 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se
->expr
))) != integer_type_node
)
6107 if (!sym
->attr
.dummy
)
6109 TREE_TYPE (sym
->backend_decl
)
6110 = build_function_type (integer_type_node
,
6111 TYPE_ARG_TYPES (TREE_TYPE (sym
->backend_decl
)));
6112 se
->expr
= gfc_build_addr_expr (NULL_TREE
, sym
->backend_decl
);
6115 TREE_TYPE (TREE_TYPE (TREE_TYPE (se
->expr
))) = integer_type_node
;
6118 fntype
= TREE_TYPE (TREE_TYPE (se
->expr
));
6119 se
->expr
= build_call_vec (TREE_TYPE (fntype
), se
->expr
, arglist
);
6121 /* Allocatable scalar function results must be freed and nullified
6122 after use. This necessitates the creation of a temporary to
6123 hold the result to prevent duplicate calls. */
6124 if (!byref
&& sym
->ts
.type
!= BT_CHARACTER
6125 && sym
->attr
.allocatable
&& !sym
->attr
.dimension
)
6127 tmp
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
6128 gfc_add_modify (&se
->pre
, tmp
, se
->expr
);
6130 tmp
= gfc_call_free (tmp
);
6131 gfc_add_expr_to_block (&post
, tmp
);
6132 gfc_add_modify (&post
, se
->expr
, build_int_cst (TREE_TYPE (se
->expr
), 0));
6135 /* If we have a pointer function, but we don't want a pointer, e.g.
6138 where f is pointer valued, we have to dereference the result. */
6139 if (!se
->want_pointer
&& !byref
6140 && ((!comp
&& (sym
->attr
.pointer
|| sym
->attr
.allocatable
))
6141 || (comp
&& (comp
->attr
.pointer
|| comp
->attr
.allocatable
))))
6142 se
->expr
= build_fold_indirect_ref_loc (input_location
, se
->expr
);
6144 /* f2c calling conventions require a scalar default real function to
6145 return a double precision result. Convert this back to default
6146 real. We only care about the cases that can happen in Fortran 77.
6148 if (flag_f2c
&& sym
->ts
.type
== BT_REAL
6149 && sym
->ts
.kind
== gfc_default_real_kind
6150 && !sym
->attr
.always_explicit
)
6151 se
->expr
= fold_convert (gfc_get_real_type (sym
->ts
.kind
), se
->expr
);
6153 /* A pure function may still have side-effects - it may modify its
6155 TREE_SIDE_EFFECTS (se
->expr
) = 1;
6157 if (!sym
->attr
.pure
)
6158 TREE_SIDE_EFFECTS (se
->expr
) = 1;
6163 /* Add the function call to the pre chain. There is no expression. */
6164 gfc_add_expr_to_block (&se
->pre
, se
->expr
);
6165 se
->expr
= NULL_TREE
;
6167 if (!se
->direct_byref
)
6169 if ((sym
->attr
.dimension
&& !comp
) || (comp
&& comp
->attr
.dimension
))
6171 if (gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
)
6173 /* Check the data pointer hasn't been modified. This would
6174 happen in a function returning a pointer. */
6175 tmp
= gfc_conv_descriptor_data_get (info
->descriptor
);
6176 tmp
= fold_build2_loc (input_location
, NE_EXPR
,
6179 gfc_trans_runtime_check (true, false, tmp
, &se
->pre
, NULL
,
6182 se
->expr
= info
->descriptor
;
6183 /* Bundle in the string length. */
6184 se
->string_length
= len
;
6186 else if (ts
.type
== BT_CHARACTER
)
6188 /* Dereference for character pointer results. */
6189 if ((!comp
&& (sym
->attr
.pointer
|| sym
->attr
.allocatable
))
6190 || (comp
&& (comp
->attr
.pointer
|| comp
->attr
.allocatable
)))
6191 se
->expr
= build_fold_indirect_ref_loc (input_location
, var
);
6195 se
->string_length
= len
;
6199 gcc_assert (ts
.type
== BT_COMPLEX
&& flag_f2c
);
6200 se
->expr
= build_fold_indirect_ref_loc (input_location
, var
);
6205 /* Associate the rhs class object's meta-data with the result, when the
6206 result is a temporary. */
6207 if (args
&& args
->expr
&& args
->expr
->ts
.type
== BT_CLASS
6208 && sym
->ts
.type
== BT_CLASS
&& result
!= NULL_TREE
&& DECL_P (result
)
6209 && !GFC_CLASS_TYPE_P (TREE_TYPE (result
)))
6212 gfc_expr
*class_expr
= gfc_find_and_cut_at_last_class_ref (args
->expr
);
6214 gfc_init_se (&parmse
, NULL
);
6215 parmse
.data_not_needed
= 1;
6216 gfc_conv_expr (&parmse
, class_expr
);
6217 if (!DECL_LANG_SPECIFIC (result
))
6218 gfc_allocate_lang_decl (result
);
6219 GFC_DECL_SAVED_DESCRIPTOR (result
) = parmse
.expr
;
6220 gfc_free_expr (class_expr
);
6221 gcc_assert (parmse
.pre
.head
== NULL_TREE
6222 && parmse
.post
.head
== NULL_TREE
);
6225 /* Follow the function call with the argument post block. */
6228 gfc_add_block_to_block (&se
->pre
, &post
);
6230 /* Transformational functions of derived types with allocatable
6231 components must have the result allocatable components copied when the
6232 argument is actually given. */
6233 arg
= expr
->value
.function
.actual
;
6234 if (result
&& arg
&& expr
->rank
6235 && expr
->value
.function
.isym
6236 && expr
->value
.function
.isym
->transformational
6238 && arg
->expr
->ts
.type
== BT_DERIVED
6239 && arg
->expr
->ts
.u
.derived
->attr
.alloc_comp
)
6242 /* Copy the allocatable components. We have to use a
6243 temporary here to prevent source allocatable components
6244 from being corrupted. */
6245 tmp2
= gfc_evaluate_now (result
, &se
->pre
);
6246 tmp
= gfc_copy_alloc_comp (arg
->expr
->ts
.u
.derived
,
6247 result
, tmp2
, expr
->rank
, 0);
6248 gfc_add_expr_to_block (&se
->pre
, tmp
);
6249 tmp
= gfc_copy_allocatable_data (result
, tmp2
, TREE_TYPE(tmp2
),
6251 gfc_add_expr_to_block (&se
->pre
, tmp
);
6253 /* Finally free the temporary's data field. */
6254 tmp
= gfc_conv_descriptor_data_get (tmp2
);
6255 tmp
= gfc_deallocate_with_status (tmp
, NULL_TREE
, NULL_TREE
,
6256 NULL_TREE
, NULL_TREE
, true,
6257 NULL
, GFC_CAF_COARRAY_NOCOARRAY
);
6258 gfc_add_expr_to_block (&se
->pre
, tmp
);
6263 /* For a function with a class array result, save the result as
6264 a temporary, set the info fields needed by the scalarizer and
6265 call the finalization function of the temporary. Note that the
6266 nullification of allocatable components needed by the result
6267 is done in gfc_trans_assignment_1. */
6268 if (expr
&& ((gfc_is_alloc_class_array_function (expr
)
6269 && se
->ss
&& se
->ss
->loop
)
6270 || gfc_is_alloc_class_scalar_function (expr
))
6271 && se
->expr
&& GFC_CLASS_TYPE_P (TREE_TYPE (se
->expr
))
6272 && expr
->must_finalize
)
6277 if (se
->ss
&& se
->ss
->loop
)
6279 se
->expr
= gfc_evaluate_now (se
->expr
, &se
->ss
->loop
->pre
);
6280 tmp
= gfc_class_data_get (se
->expr
);
6281 info
->descriptor
= tmp
;
6282 info
->data
= gfc_conv_descriptor_data_get (tmp
);
6283 info
->offset
= gfc_conv_descriptor_offset_get (tmp
);
6284 for (n
= 0; n
< se
->ss
->loop
->dimen
; n
++)
6286 tree dim
= gfc_rank_cst
[n
];
6287 se
->ss
->loop
->to
[n
] = gfc_conv_descriptor_ubound_get (tmp
, dim
);
6288 se
->ss
->loop
->from
[n
] = gfc_conv_descriptor_lbound_get (tmp
, dim
);
6293 /* TODO Eliminate the doubling of temporaries. This
6294 one is necessary to ensure no memory leakage. */
6295 se
->expr
= gfc_evaluate_now (se
->expr
, &se
->pre
);
6296 tmp
= gfc_class_data_get (se
->expr
);
6297 tmp
= gfc_conv_scalar_to_descriptor (se
, tmp
,
6298 CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
);
6301 final_fndecl
= gfc_class_vtab_final_get (se
->expr
);
6302 is_final
= fold_build2_loc (input_location
, NE_EXPR
,
6305 fold_convert (TREE_TYPE (final_fndecl
),
6306 null_pointer_node
));
6307 final_fndecl
= build_fold_indirect_ref_loc (input_location
,
6309 tmp
= build_call_expr_loc (input_location
,
6311 gfc_build_addr_expr (NULL
, tmp
),
6312 gfc_class_vtab_size_get (se
->expr
),
6313 boolean_false_node
);
6314 tmp
= fold_build3_loc (input_location
, COND_EXPR
,
6315 void_type_node
, is_final
, tmp
,
6316 build_empty_stmt (input_location
));
6318 if (se
->ss
&& se
->ss
->loop
)
6320 gfc_add_expr_to_block (&se
->ss
->loop
->post
, tmp
);
6321 tmp
= gfc_call_free (info
->data
);
6322 gfc_add_expr_to_block (&se
->ss
->loop
->post
, tmp
);
6326 gfc_add_expr_to_block (&se
->post
, tmp
);
6327 tmp
= gfc_class_data_get (se
->expr
);
6328 tmp
= gfc_call_free (tmp
);
6329 gfc_add_expr_to_block (&se
->post
, tmp
);
6331 expr
->must_finalize
= 0;
6334 gfc_add_block_to_block (&se
->post
, &post
);
6337 return has_alternate_specifier
;
6341 /* Fill a character string with spaces. */
6344 fill_with_spaces (tree start
, tree type
, tree size
)
6346 stmtblock_t block
, loop
;
6347 tree i
, el
, exit_label
, cond
, tmp
;
6349 /* For a simple char type, we can call memset(). */
6350 if (compare_tree_int (TYPE_SIZE_UNIT (type
), 1) == 0)
6351 return build_call_expr_loc (input_location
,
6352 builtin_decl_explicit (BUILT_IN_MEMSET
),
6354 build_int_cst (gfc_get_int_type (gfc_c_int_kind
),
6355 lang_hooks
.to_target_charset (' ')),
6358 /* Otherwise, we use a loop:
6359 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
6363 /* Initialize variables. */
6364 gfc_init_block (&block
);
6365 i
= gfc_create_var (sizetype
, "i");
6366 gfc_add_modify (&block
, i
, fold_convert (sizetype
, size
));
6367 el
= gfc_create_var (build_pointer_type (type
), "el");
6368 gfc_add_modify (&block
, el
, fold_convert (TREE_TYPE (el
), start
));
6369 exit_label
= gfc_build_label_decl (NULL_TREE
);
6370 TREE_USED (exit_label
) = 1;
6374 gfc_init_block (&loop
);
6376 /* Exit condition. */
6377 cond
= fold_build2_loc (input_location
, LE_EXPR
, boolean_type_node
, i
,
6378 build_zero_cst (sizetype
));
6379 tmp
= build1_v (GOTO_EXPR
, exit_label
);
6380 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
, tmp
,
6381 build_empty_stmt (input_location
));
6382 gfc_add_expr_to_block (&loop
, tmp
);
6385 gfc_add_modify (&loop
,
6386 fold_build1_loc (input_location
, INDIRECT_REF
, type
, el
),
6387 build_int_cst (type
, lang_hooks
.to_target_charset (' ')));
6389 /* Increment loop variables. */
6390 gfc_add_modify (&loop
, i
,
6391 fold_build2_loc (input_location
, MINUS_EXPR
, sizetype
, i
,
6392 TYPE_SIZE_UNIT (type
)));
6393 gfc_add_modify (&loop
, el
,
6394 fold_build_pointer_plus_loc (input_location
,
6395 el
, TYPE_SIZE_UNIT (type
)));
6397 /* Making the loop... actually loop! */
6398 tmp
= gfc_finish_block (&loop
);
6399 tmp
= build1_v (LOOP_EXPR
, tmp
);
6400 gfc_add_expr_to_block (&block
, tmp
);
6402 /* The exit label. */
6403 tmp
= build1_v (LABEL_EXPR
, exit_label
);
6404 gfc_add_expr_to_block (&block
, tmp
);
6407 return gfc_finish_block (&block
);
6411 /* Generate code to copy a string. */
6414 gfc_trans_string_copy (stmtblock_t
* block
, tree dlength
, tree dest
,
6415 int dkind
, tree slength
, tree src
, int skind
)
6417 tree tmp
, dlen
, slen
;
6426 stmtblock_t tempblock
;
6428 gcc_assert (dkind
== skind
);
6430 if (slength
!= NULL_TREE
)
6432 slen
= fold_convert (size_type_node
, gfc_evaluate_now (slength
, block
));
6433 ssc
= gfc_string_to_single_character (slen
, src
, skind
);
6437 slen
= build_int_cst (size_type_node
, 1);
6441 if (dlength
!= NULL_TREE
)
6443 dlen
= fold_convert (size_type_node
, gfc_evaluate_now (dlength
, block
));
6444 dsc
= gfc_string_to_single_character (dlen
, dest
, dkind
);
6448 dlen
= build_int_cst (size_type_node
, 1);
6452 /* Assign directly if the types are compatible. */
6453 if (dsc
!= NULL_TREE
&& ssc
!= NULL_TREE
6454 && TREE_TYPE (dsc
) == TREE_TYPE (ssc
))
6456 gfc_add_modify (block
, dsc
, ssc
);
6460 /* The string copy algorithm below generates code like
6463 memmove (dest, src, min(dlen, slen));
6465 memset(&dest[slen], ' ', dlen - slen);
6469 /* Do nothing if the destination length is zero. */
6470 cond
= fold_build2_loc (input_location
, GT_EXPR
, boolean_type_node
, dlen
,
6471 build_int_cst (size_type_node
, 0));
6473 /* For non-default character kinds, we have to multiply the string
6474 length by the base type size. */
6475 chartype
= gfc_get_char_type (dkind
);
6476 slen
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
6477 fold_convert (size_type_node
, slen
),
6478 fold_convert (size_type_node
,
6479 TYPE_SIZE_UNIT (chartype
)));
6480 dlen
= fold_build2_loc (input_location
, MULT_EXPR
, size_type_node
,
6481 fold_convert (size_type_node
, dlen
),
6482 fold_convert (size_type_node
,
6483 TYPE_SIZE_UNIT (chartype
)));
6485 if (dlength
&& POINTER_TYPE_P (TREE_TYPE (dest
)))
6486 dest
= fold_convert (pvoid_type_node
, dest
);
6488 dest
= gfc_build_addr_expr (pvoid_type_node
, dest
);
6490 if (slength
&& POINTER_TYPE_P (TREE_TYPE (src
)))
6491 src
= fold_convert (pvoid_type_node
, src
);
6493 src
= gfc_build_addr_expr (pvoid_type_node
, src
);
6495 /* First do the memmove. */
6496 tmp2
= fold_build2_loc (input_location
, MIN_EXPR
, TREE_TYPE (dlen
), dlen
,
6498 tmp2
= build_call_expr_loc (input_location
,
6499 builtin_decl_explicit (BUILT_IN_MEMMOVE
),
6500 3, dest
, src
, tmp2
);
6501 stmtblock_t tmpblock2
;
6502 gfc_init_block (&tmpblock2
);
6503 gfc_add_expr_to_block (&tmpblock2
, tmp2
);
6505 /* If the destination is longer, fill the end with spaces. */
6506 cond2
= fold_build2_loc (input_location
, LT_EXPR
, boolean_type_node
, slen
,
6509 /* Wstringop-overflow appears at -O3 even though this warning is not
6510 explicitly available in fortran nor can it be switched off. If the
6511 source length is a constant, its negative appears as a very large
6512 postive number and triggers the warning in BUILTIN_MEMSET. Fixing
6513 the result of the MINUS_EXPR suppresses this spurious warning. */
6514 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
6515 TREE_TYPE(dlen
), dlen
, slen
);
6516 if (slength
&& TREE_CONSTANT (slength
))
6517 tmp
= gfc_evaluate_now (tmp
, block
);
6519 tmp4
= fold_build_pointer_plus_loc (input_location
, dest
, slen
);
6520 tmp4
= fill_with_spaces (tmp4
, chartype
, tmp
);
6522 gfc_init_block (&tempblock
);
6523 gfc_add_expr_to_block (&tempblock
, tmp4
);
6524 tmp3
= gfc_finish_block (&tempblock
);
6526 /* The whole copy_string function is there. */
6527 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond2
,
6528 tmp3
, build_empty_stmt (input_location
));
6529 gfc_add_expr_to_block (&tmpblock2
, tmp
);
6530 tmp
= gfc_finish_block (&tmpblock2
);
6531 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
, cond
, tmp
,
6532 build_empty_stmt (input_location
));
6533 gfc_add_expr_to_block (block
, tmp
);
6537 /* Translate a statement function.
6538 The value of a statement function reference is obtained by evaluating the
6539 expression using the values of the actual arguments for the values of the
6540 corresponding dummy arguments. */
6543 gfc_conv_statement_function (gfc_se
* se
, gfc_expr
* expr
)
6547 gfc_formal_arglist
*fargs
;
6548 gfc_actual_arglist
*args
;
6551 gfc_saved_var
*saved_vars
;
6557 sym
= expr
->symtree
->n
.sym
;
6558 args
= expr
->value
.function
.actual
;
6559 gfc_init_se (&lse
, NULL
);
6560 gfc_init_se (&rse
, NULL
);
6563 for (fargs
= gfc_sym_get_dummy_args (sym
); fargs
; fargs
= fargs
->next
)
6565 saved_vars
= XCNEWVEC (gfc_saved_var
, n
);
6566 temp_vars
= XCNEWVEC (tree
, n
);
6568 for (fargs
= gfc_sym_get_dummy_args (sym
), n
= 0; fargs
;
6569 fargs
= fargs
->next
, n
++)
6571 /* Each dummy shall be specified, explicitly or implicitly, to be
6573 gcc_assert (fargs
->sym
->attr
.dimension
== 0);
6576 if (fsym
->ts
.type
== BT_CHARACTER
)
6578 /* Copy string arguments. */
6581 gcc_assert (fsym
->ts
.u
.cl
&& fsym
->ts
.u
.cl
->length
6582 && fsym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
6584 /* Create a temporary to hold the value. */
6585 if (fsym
->ts
.u
.cl
->backend_decl
== NULL_TREE
)
6586 fsym
->ts
.u
.cl
->backend_decl
6587 = gfc_conv_constant_to_tree (fsym
->ts
.u
.cl
->length
);
6589 type
= gfc_get_character_type (fsym
->ts
.kind
, fsym
->ts
.u
.cl
);
6590 temp_vars
[n
] = gfc_create_var (type
, fsym
->name
);
6592 arglen
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
6594 gfc_conv_expr (&rse
, args
->expr
);
6595 gfc_conv_string_parameter (&rse
);
6596 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
6597 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
6599 gfc_trans_string_copy (&se
->pre
, arglen
, temp_vars
[n
], fsym
->ts
.kind
,
6600 rse
.string_length
, rse
.expr
, fsym
->ts
.kind
);
6601 gfc_add_block_to_block (&se
->pre
, &lse
.post
);
6602 gfc_add_block_to_block (&se
->pre
, &rse
.post
);
6606 /* For everything else, just evaluate the expression. */
6608 /* Create a temporary to hold the value. */
6609 type
= gfc_typenode_for_spec (&fsym
->ts
);
6610 temp_vars
[n
] = gfc_create_var (type
, fsym
->name
);
6612 gfc_conv_expr (&lse
, args
->expr
);
6614 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
6615 gfc_add_modify (&se
->pre
, temp_vars
[n
], lse
.expr
);
6616 gfc_add_block_to_block (&se
->pre
, &lse
.post
);
6622 /* Use the temporary variables in place of the real ones. */
6623 for (fargs
= gfc_sym_get_dummy_args (sym
), n
= 0; fargs
;
6624 fargs
= fargs
->next
, n
++)
6625 gfc_shadow_sym (fargs
->sym
, temp_vars
[n
], &saved_vars
[n
]);
6627 gfc_conv_expr (se
, sym
->value
);
6629 if (sym
->ts
.type
== BT_CHARACTER
)
6631 gfc_conv_const_charlen (sym
->ts
.u
.cl
);
6633 /* Force the expression to the correct length. */
6634 if (!INTEGER_CST_P (se
->string_length
)
6635 || tree_int_cst_lt (se
->string_length
,
6636 sym
->ts
.u
.cl
->backend_decl
))
6638 type
= gfc_get_character_type (sym
->ts
.kind
, sym
->ts
.u
.cl
);
6639 tmp
= gfc_create_var (type
, sym
->name
);
6640 tmp
= gfc_build_addr_expr (build_pointer_type (type
), tmp
);
6641 gfc_trans_string_copy (&se
->pre
, sym
->ts
.u
.cl
->backend_decl
, tmp
,
6642 sym
->ts
.kind
, se
->string_length
, se
->expr
,
6646 se
->string_length
= sym
->ts
.u
.cl
->backend_decl
;
6649 /* Restore the original variables. */
6650 for (fargs
= gfc_sym_get_dummy_args (sym
), n
= 0; fargs
;
6651 fargs
= fargs
->next
, n
++)
6652 gfc_restore_sym (fargs
->sym
, &saved_vars
[n
]);
6658 /* Translate a function expression. */
6661 gfc_conv_function_expr (gfc_se
* se
, gfc_expr
* expr
)
6665 if (expr
->value
.function
.isym
)
6667 gfc_conv_intrinsic_function (se
, expr
);
6671 /* expr.value.function.esym is the resolved (specific) function symbol for
6672 most functions. However this isn't set for dummy procedures. */
6673 sym
= expr
->value
.function
.esym
;
6675 sym
= expr
->symtree
->n
.sym
;
6677 /* The IEEE_ARITHMETIC functions are caught here. */
6678 if (sym
->from_intmod
== INTMOD_IEEE_ARITHMETIC
)
6679 if (gfc_conv_ieee_arithmetic_function (se
, expr
))
6682 /* We distinguish statement functions from general functions to improve
6683 runtime performance. */
6684 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
6686 gfc_conv_statement_function (se
, expr
);
6690 gfc_conv_procedure_call (se
, sym
, expr
->value
.function
.actual
, expr
,
6695 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
6698 is_zero_initializer_p (gfc_expr
* expr
)
6700 if (expr
->expr_type
!= EXPR_CONSTANT
)
6703 /* We ignore constants with prescribed memory representations for now. */
6704 if (expr
->representation
.string
)
6707 switch (expr
->ts
.type
)
6710 return mpz_cmp_si (expr
->value
.integer
, 0) == 0;
6713 return mpfr_zero_p (expr
->value
.real
)
6714 && MPFR_SIGN (expr
->value
.real
) >= 0;
6717 return expr
->value
.logical
== 0;
6720 return mpfr_zero_p (mpc_realref (expr
->value
.complex))
6721 && MPFR_SIGN (mpc_realref (expr
->value
.complex)) >= 0
6722 && mpfr_zero_p (mpc_imagref (expr
->value
.complex))
6723 && MPFR_SIGN (mpc_imagref (expr
->value
.complex)) >= 0;
6733 gfc_conv_array_constructor_expr (gfc_se
* se
, gfc_expr
* expr
)
6738 gcc_assert (ss
!= NULL
&& ss
!= gfc_ss_terminator
);
6739 gcc_assert (ss
->info
->expr
== expr
&& ss
->info
->type
== GFC_SS_CONSTRUCTOR
);
6741 gfc_conv_tmp_array_ref (se
);
6745 /* Build a static initializer. EXPR is the expression for the initial value.
6746 The other parameters describe the variable of the component being
6747 initialized. EXPR may be null. */
6750 gfc_conv_initializer (gfc_expr
* expr
, gfc_typespec
* ts
, tree type
,
6751 bool array
, bool pointer
, bool procptr
)
6755 if (flag_coarray
!= GFC_FCOARRAY_LIB
&& ts
->type
== BT_DERIVED
6756 && ts
->u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
6757 && ts
->u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
)
6758 return build_constructor (type
, NULL
);
6760 if (!(expr
|| pointer
|| procptr
))
6763 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
6764 (these are the only two iso_c_binding derived types that can be
6765 used as initialization expressions). If so, we need to modify
6766 the 'expr' to be that for a (void *). */
6767 if (expr
!= NULL
&& expr
->ts
.type
== BT_DERIVED
6768 && expr
->ts
.is_iso_c
&& expr
->ts
.u
.derived
)
6770 gfc_symbol
*derived
= expr
->ts
.u
.derived
;
6772 /* The derived symbol has already been converted to a (void *). Use
6774 expr
= gfc_get_int_expr (derived
->ts
.kind
, NULL
, 0);
6775 expr
->ts
.f90_type
= derived
->ts
.f90_type
;
6777 gfc_init_se (&se
, NULL
);
6778 gfc_conv_constant (&se
, expr
);
6779 gcc_assert (TREE_CODE (se
.expr
) != CONSTRUCTOR
);
6783 if (array
&& !procptr
)
6786 /* Arrays need special handling. */
6788 ctor
= gfc_build_null_descriptor (type
);
6789 /* Special case assigning an array to zero. */
6790 else if (is_zero_initializer_p (expr
))
6791 ctor
= build_constructor (type
, NULL
);
6793 ctor
= gfc_conv_array_initializer (type
, expr
);
6794 TREE_STATIC (ctor
) = 1;
6797 else if (pointer
|| procptr
)
6799 if (ts
->type
== BT_CLASS
&& !procptr
)
6801 gfc_init_se (&se
, NULL
);
6802 gfc_conv_structure (&se
, gfc_class_initializer (ts
, expr
), 1);
6803 gcc_assert (TREE_CODE (se
.expr
) == CONSTRUCTOR
);
6804 TREE_STATIC (se
.expr
) = 1;
6807 else if (!expr
|| expr
->expr_type
== EXPR_NULL
)
6808 return fold_convert (type
, null_pointer_node
);
6811 gfc_init_se (&se
, NULL
);
6812 se
.want_pointer
= 1;
6813 gfc_conv_expr (&se
, expr
);
6814 gcc_assert (TREE_CODE (se
.expr
) != CONSTRUCTOR
);
6824 gfc_init_se (&se
, NULL
);
6825 if (ts
->type
== BT_CLASS
&& expr
->expr_type
== EXPR_NULL
)
6826 gfc_conv_structure (&se
, gfc_class_initializer (ts
, expr
), 1);
6828 gfc_conv_structure (&se
, expr
, 1);
6829 gcc_assert (TREE_CODE (se
.expr
) == CONSTRUCTOR
);
6830 TREE_STATIC (se
.expr
) = 1;
6835 tree ctor
= gfc_conv_string_init (ts
->u
.cl
->backend_decl
,expr
);
6836 TREE_STATIC (ctor
) = 1;
6841 gfc_init_se (&se
, NULL
);
6842 gfc_conv_constant (&se
, expr
);
6843 gcc_assert (TREE_CODE (se
.expr
) != CONSTRUCTOR
);
6850 gfc_trans_subarray_assign (tree dest
, gfc_component
* cm
, gfc_expr
* expr
)
6856 gfc_array_info
*lss_array
;
6863 gfc_start_block (&block
);
6865 /* Initialize the scalarizer. */
6866 gfc_init_loopinfo (&loop
);
6868 gfc_init_se (&lse
, NULL
);
6869 gfc_init_se (&rse
, NULL
);
6872 rss
= gfc_walk_expr (expr
);
6873 if (rss
== gfc_ss_terminator
)
6874 /* The rhs is scalar. Add a ss for the expression. */
6875 rss
= gfc_get_scalar_ss (gfc_ss_terminator
, expr
);
6877 /* Create a SS for the destination. */
6878 lss
= gfc_get_array_ss (gfc_ss_terminator
, NULL
, cm
->as
->rank
,
6880 lss_array
= &lss
->info
->data
.array
;
6881 lss_array
->shape
= gfc_get_shape (cm
->as
->rank
);
6882 lss_array
->descriptor
= dest
;
6883 lss_array
->data
= gfc_conv_array_data (dest
);
6884 lss_array
->offset
= gfc_conv_array_offset (dest
);
6885 for (n
= 0; n
< cm
->as
->rank
; n
++)
6887 lss_array
->start
[n
] = gfc_conv_array_lbound (dest
, n
);
6888 lss_array
->stride
[n
] = gfc_index_one_node
;
6890 mpz_init (lss_array
->shape
[n
]);
6891 mpz_sub (lss_array
->shape
[n
], cm
->as
->upper
[n
]->value
.integer
,
6892 cm
->as
->lower
[n
]->value
.integer
);
6893 mpz_add_ui (lss_array
->shape
[n
], lss_array
->shape
[n
], 1);
6896 /* Associate the SS with the loop. */
6897 gfc_add_ss_to_loop (&loop
, lss
);
6898 gfc_add_ss_to_loop (&loop
, rss
);
6900 /* Calculate the bounds of the scalarization. */
6901 gfc_conv_ss_startstride (&loop
);
6903 /* Setup the scalarizing loops. */
6904 gfc_conv_loop_setup (&loop
, &expr
->where
);
6906 /* Setup the gfc_se structures. */
6907 gfc_copy_loopinfo_to_se (&lse
, &loop
);
6908 gfc_copy_loopinfo_to_se (&rse
, &loop
);
6911 gfc_mark_ss_chain_used (rss
, 1);
6913 gfc_mark_ss_chain_used (lss
, 1);
6915 /* Start the scalarized loop body. */
6916 gfc_start_scalarized_body (&loop
, &body
);
6918 gfc_conv_tmp_array_ref (&lse
);
6919 if (cm
->ts
.type
== BT_CHARACTER
)
6920 lse
.string_length
= cm
->ts
.u
.cl
->backend_decl
;
6922 gfc_conv_expr (&rse
, expr
);
6924 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, cm
->ts
, true, false);
6925 gfc_add_expr_to_block (&body
, tmp
);
6927 gcc_assert (rse
.ss
== gfc_ss_terminator
);
6929 /* Generate the copying loops. */
6930 gfc_trans_scalarizing_loops (&loop
, &body
);
6932 /* Wrap the whole thing up. */
6933 gfc_add_block_to_block (&block
, &loop
.pre
);
6934 gfc_add_block_to_block (&block
, &loop
.post
);
6936 gcc_assert (lss_array
->shape
!= NULL
);
6937 gfc_free_shape (&lss_array
->shape
, cm
->as
->rank
);
6938 gfc_cleanup_loop (&loop
);
6940 return gfc_finish_block (&block
);
6945 gfc_trans_alloc_subarray_assign (tree dest
, gfc_component
* cm
,
6955 gfc_expr
*arg
= NULL
;
6957 gfc_start_block (&block
);
6958 gfc_init_se (&se
, NULL
);
6960 /* Get the descriptor for the expressions. */
6961 se
.want_pointer
= 0;
6962 gfc_conv_expr_descriptor (&se
, expr
);
6963 gfc_add_block_to_block (&block
, &se
.pre
);
6964 gfc_add_modify (&block
, dest
, se
.expr
);
6966 /* Deal with arrays of derived types with allocatable components. */
6967 if (gfc_bt_struct (cm
->ts
.type
)
6968 && cm
->ts
.u
.derived
->attr
.alloc_comp
)
6969 // TODO: Fix caf_mode
6970 tmp
= gfc_copy_alloc_comp (cm
->ts
.u
.derived
,
6973 else if (cm
->ts
.type
== BT_CLASS
&& expr
->ts
.type
== BT_DERIVED
6974 && CLASS_DATA(cm
)->attr
.allocatable
)
6976 if (cm
->ts
.u
.derived
->attr
.alloc_comp
)
6977 // TODO: Fix caf_mode
6978 tmp
= gfc_copy_alloc_comp (expr
->ts
.u
.derived
,
6983 tmp
= TREE_TYPE (dest
);
6984 tmp
= gfc_duplicate_allocatable (dest
, se
.expr
,
6985 tmp
, expr
->rank
, NULL_TREE
);
6989 tmp
= gfc_duplicate_allocatable (dest
, se
.expr
,
6990 TREE_TYPE(cm
->backend_decl
),
6991 cm
->as
->rank
, NULL_TREE
);
6993 gfc_add_expr_to_block (&block
, tmp
);
6994 gfc_add_block_to_block (&block
, &se
.post
);
6996 if (expr
->expr_type
!= EXPR_VARIABLE
)
6997 gfc_conv_descriptor_data_set (&block
, se
.expr
,
7000 /* We need to know if the argument of a conversion function is a
7001 variable, so that the correct lower bound can be used. */
7002 if (expr
->expr_type
== EXPR_FUNCTION
7003 && expr
->value
.function
.isym
7004 && expr
->value
.function
.isym
->conversion
7005 && expr
->value
.function
.actual
->expr
7006 && expr
->value
.function
.actual
->expr
->expr_type
== EXPR_VARIABLE
)
7007 arg
= expr
->value
.function
.actual
->expr
;
7009 /* Obtain the array spec of full array references. */
7011 as
= gfc_get_full_arrayspec_from_expr (arg
);
7013 as
= gfc_get_full_arrayspec_from_expr (expr
);
7015 /* Shift the lbound and ubound of temporaries to being unity,
7016 rather than zero, based. Always calculate the offset. */
7017 offset
= gfc_conv_descriptor_offset_get (dest
);
7018 gfc_add_modify (&block
, offset
, gfc_index_zero_node
);
7019 tmp2
=gfc_create_var (gfc_array_index_type
, NULL
);
7021 for (n
= 0; n
< expr
->rank
; n
++)
7026 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
7027 TODO It looks as if gfc_conv_expr_descriptor should return
7028 the correct bounds and that the following should not be
7029 necessary. This would simplify gfc_conv_intrinsic_bound
7031 if (as
&& as
->lower
[n
])
7034 gfc_init_se (&lbse
, NULL
);
7035 gfc_conv_expr (&lbse
, as
->lower
[n
]);
7036 gfc_add_block_to_block (&block
, &lbse
.pre
);
7037 lbound
= gfc_evaluate_now (lbse
.expr
, &block
);
7041 tmp
= gfc_get_symbol_decl (arg
->symtree
->n
.sym
);
7042 lbound
= gfc_conv_descriptor_lbound_get (tmp
,
7046 lbound
= gfc_conv_descriptor_lbound_get (dest
,
7049 lbound
= gfc_index_one_node
;
7051 lbound
= fold_convert (gfc_array_index_type
, lbound
);
7053 /* Shift the bounds and set the offset accordingly. */
7054 tmp
= gfc_conv_descriptor_ubound_get (dest
, gfc_rank_cst
[n
]);
7055 span
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
7056 tmp
, gfc_conv_descriptor_lbound_get (dest
, gfc_rank_cst
[n
]));
7057 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
, gfc_array_index_type
,
7059 gfc_conv_descriptor_ubound_set (&block
, dest
,
7060 gfc_rank_cst
[n
], tmp
);
7061 gfc_conv_descriptor_lbound_set (&block
, dest
,
7062 gfc_rank_cst
[n
], lbound
);
7064 tmp
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
7065 gfc_conv_descriptor_lbound_get (dest
,
7067 gfc_conv_descriptor_stride_get (dest
,
7069 gfc_add_modify (&block
, tmp2
, tmp
);
7070 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, gfc_array_index_type
,
7072 gfc_conv_descriptor_offset_set (&block
, dest
, tmp
);
7077 /* If a conversion expression has a null data pointer
7078 argument, nullify the allocatable component. */
7082 if (arg
->symtree
->n
.sym
->attr
.allocatable
7083 || arg
->symtree
->n
.sym
->attr
.pointer
)
7085 non_null_expr
= gfc_finish_block (&block
);
7086 gfc_start_block (&block
);
7087 gfc_conv_descriptor_data_set (&block
, dest
,
7089 null_expr
= gfc_finish_block (&block
);
7090 tmp
= gfc_conv_descriptor_data_get (arg
->symtree
->n
.sym
->backend_decl
);
7091 tmp
= build2_loc (input_location
, EQ_EXPR
, boolean_type_node
, tmp
,
7092 fold_convert (TREE_TYPE (tmp
), null_pointer_node
));
7093 return build3_v (COND_EXPR
, tmp
,
7094 null_expr
, non_null_expr
);
7098 return gfc_finish_block (&block
);
7102 /* Allocate or reallocate scalar component, as necessary. */
7105 alloc_scalar_allocatable_for_subcomponent_assignment (stmtblock_t
*block
,
7115 tree lhs_cl_size
= NULL_TREE
;
7120 if (!expr2
|| expr2
->rank
)
7123 realloc_lhs_warning (expr2
->ts
.type
, false, &expr2
->where
);
7125 if (cm
->ts
.type
== BT_CHARACTER
&& cm
->ts
.deferred
)
7127 char name
[GFC_MAX_SYMBOL_LEN
+9];
7128 gfc_component
*strlen
;
7129 /* Use the rhs string length and the lhs element size. */
7130 gcc_assert (expr2
->ts
.type
== BT_CHARACTER
);
7131 if (!expr2
->ts
.u
.cl
->backend_decl
)
7133 gfc_conv_string_length (expr2
->ts
.u
.cl
, expr2
, block
);
7134 gcc_assert (expr2
->ts
.u
.cl
->backend_decl
);
7137 size
= expr2
->ts
.u
.cl
->backend_decl
;
7139 /* Ensure that cm->ts.u.cl->backend_decl is a componentref to _%s_length
7141 sprintf (name
, "_%s_length", cm
->name
);
7142 strlen
= gfc_find_component (sym
, name
, true, true, NULL
);
7143 lhs_cl_size
= fold_build3_loc (input_location
, COMPONENT_REF
,
7144 gfc_charlen_type_node
,
7145 TREE_OPERAND (comp
, 0),
7146 strlen
->backend_decl
, NULL_TREE
);
7148 tmp
= TREE_TYPE (gfc_typenode_for_spec (&cm
->ts
));
7149 tmp
= TYPE_SIZE_UNIT (tmp
);
7150 size_in_bytes
= fold_build2_loc (input_location
, MULT_EXPR
,
7151 TREE_TYPE (tmp
), tmp
,
7152 fold_convert (TREE_TYPE (tmp
), size
));
7154 else if (cm
->ts
.type
== BT_CLASS
)
7156 gcc_assert (expr2
->ts
.type
== BT_CLASS
|| expr2
->ts
.type
== BT_DERIVED
);
7157 if (expr2
->ts
.type
== BT_DERIVED
)
7159 tmp
= gfc_get_symbol_decl (expr2
->ts
.u
.derived
);
7160 size
= TYPE_SIZE_UNIT (tmp
);
7166 e2vtab
= gfc_find_and_cut_at_last_class_ref (expr2
);
7167 gfc_add_vptr_component (e2vtab
);
7168 gfc_add_size_component (e2vtab
);
7169 gfc_init_se (&se
, NULL
);
7170 gfc_conv_expr (&se
, e2vtab
);
7171 gfc_add_block_to_block (block
, &se
.pre
);
7172 size
= fold_convert (size_type_node
, se
.expr
);
7173 gfc_free_expr (e2vtab
);
7175 size_in_bytes
= size
;
7179 /* Otherwise use the length in bytes of the rhs. */
7180 size
= TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm
->ts
));
7181 size_in_bytes
= size
;
7184 size_in_bytes
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
,
7185 size_in_bytes
, size_one_node
);
7187 if (cm
->ts
.type
== BT_DERIVED
&& cm
->ts
.u
.derived
->attr
.alloc_comp
)
7189 tmp
= build_call_expr_loc (input_location
,
7190 builtin_decl_explicit (BUILT_IN_CALLOC
),
7191 2, build_one_cst (size_type_node
),
7193 tmp
= fold_convert (TREE_TYPE (comp
), tmp
);
7194 gfc_add_modify (block
, comp
, tmp
);
7198 tmp
= build_call_expr_loc (input_location
,
7199 builtin_decl_explicit (BUILT_IN_MALLOC
),
7201 if (GFC_CLASS_TYPE_P (TREE_TYPE (comp
)))
7202 ptr
= gfc_class_data_get (comp
);
7205 tmp
= fold_convert (TREE_TYPE (ptr
), tmp
);
7206 gfc_add_modify (block
, ptr
, tmp
);
7209 if (cm
->ts
.type
== BT_CHARACTER
&& cm
->ts
.deferred
)
7210 /* Update the lhs character length. */
7211 gfc_add_modify (block
, lhs_cl_size
, size
);
7215 /* Assign a single component of a derived type constructor. */
7218 gfc_trans_subcomponent_assign (tree dest
, gfc_component
* cm
, gfc_expr
* expr
,
7219 gfc_symbol
*sym
, bool init
)
7227 gfc_start_block (&block
);
7229 if (cm
->attr
.pointer
|| cm
->attr
.proc_pointer
)
7231 /* Only care about pointers here, not about allocatables. */
7232 gfc_init_se (&se
, NULL
);
7233 /* Pointer component. */
7234 if ((cm
->attr
.dimension
|| cm
->attr
.codimension
)
7235 && !cm
->attr
.proc_pointer
)
7237 /* Array pointer. */
7238 if (expr
->expr_type
== EXPR_NULL
)
7239 gfc_conv_descriptor_data_set (&block
, dest
, null_pointer_node
);
7242 se
.direct_byref
= 1;
7244 gfc_conv_expr_descriptor (&se
, expr
);
7245 gfc_add_block_to_block (&block
, &se
.pre
);
7246 gfc_add_block_to_block (&block
, &se
.post
);
7251 /* Scalar pointers. */
7252 se
.want_pointer
= 1;
7253 gfc_conv_expr (&se
, expr
);
7254 gfc_add_block_to_block (&block
, &se
.pre
);
7256 if (expr
->symtree
&& expr
->symtree
->n
.sym
->attr
.proc_pointer
7257 && expr
->symtree
->n
.sym
->attr
.dummy
)
7258 se
.expr
= build_fold_indirect_ref_loc (input_location
, se
.expr
);
7260 gfc_add_modify (&block
, dest
,
7261 fold_convert (TREE_TYPE (dest
), se
.expr
));
7262 gfc_add_block_to_block (&block
, &se
.post
);
7265 else if (cm
->ts
.type
== BT_CLASS
&& expr
->expr_type
== EXPR_NULL
)
7267 /* NULL initialization for CLASS components. */
7268 tmp
= gfc_trans_structure_assign (dest
,
7269 gfc_class_initializer (&cm
->ts
, expr
),
7271 gfc_add_expr_to_block (&block
, tmp
);
7273 else if ((cm
->attr
.dimension
|| cm
->attr
.codimension
)
7274 && !cm
->attr
.proc_pointer
)
7276 if (cm
->attr
.allocatable
&& expr
->expr_type
== EXPR_NULL
)
7277 gfc_conv_descriptor_data_set (&block
, dest
, null_pointer_node
);
7278 else if (cm
->attr
.allocatable
)
7280 tmp
= gfc_trans_alloc_subarray_assign (dest
, cm
, expr
);
7281 gfc_add_expr_to_block (&block
, tmp
);
7285 tmp
= gfc_trans_subarray_assign (dest
, cm
, expr
);
7286 gfc_add_expr_to_block (&block
, tmp
);
7289 else if (cm
->ts
.type
== BT_CLASS
7290 && CLASS_DATA (cm
)->attr
.dimension
7291 && CLASS_DATA (cm
)->attr
.allocatable
7292 && expr
->ts
.type
== BT_DERIVED
)
7294 vtab
= gfc_get_symbol_decl (gfc_find_vtab (&expr
->ts
));
7295 vtab
= gfc_build_addr_expr (NULL_TREE
, vtab
);
7296 tmp
= gfc_class_vptr_get (dest
);
7297 gfc_add_modify (&block
, tmp
,
7298 fold_convert (TREE_TYPE (tmp
), vtab
));
7299 tmp
= gfc_class_data_get (dest
);
7300 tmp
= gfc_trans_alloc_subarray_assign (tmp
, cm
, expr
);
7301 gfc_add_expr_to_block (&block
, tmp
);
7303 else if (init
&& cm
->attr
.allocatable
&& expr
->expr_type
== EXPR_NULL
)
7305 /* NULL initialization for allocatable components. */
7306 gfc_add_modify (&block
, dest
, fold_convert (TREE_TYPE (dest
),
7307 null_pointer_node
));
7309 else if (init
&& (cm
->attr
.allocatable
7310 || (cm
->ts
.type
== BT_CLASS
&& CLASS_DATA (cm
)->attr
.allocatable
7311 && expr
->ts
.type
!= BT_CLASS
)))
7313 /* Take care about non-array allocatable components here. The alloc_*
7314 routine below is motivated by the alloc_scalar_allocatable_for_
7315 assignment() routine, but with the realloc portions removed and
7317 alloc_scalar_allocatable_for_subcomponent_assignment (&block
,
7322 /* The remainder of these instructions follow the if (cm->attr.pointer)
7323 if (!cm->attr.dimension) part above. */
7324 gfc_init_se (&se
, NULL
);
7325 gfc_conv_expr (&se
, expr
);
7326 gfc_add_block_to_block (&block
, &se
.pre
);
7328 if (expr
->symtree
&& expr
->symtree
->n
.sym
->attr
.proc_pointer
7329 && expr
->symtree
->n
.sym
->attr
.dummy
)
7330 se
.expr
= build_fold_indirect_ref_loc (input_location
, se
.expr
);
7332 if (cm
->ts
.type
== BT_CLASS
&& expr
->ts
.type
== BT_DERIVED
)
7334 tmp
= gfc_class_data_get (dest
);
7335 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
7336 vtab
= gfc_get_symbol_decl (gfc_find_vtab (&expr
->ts
));
7337 vtab
= gfc_build_addr_expr (NULL_TREE
, vtab
);
7338 gfc_add_modify (&block
, gfc_class_vptr_get (dest
),
7339 fold_convert (TREE_TYPE (gfc_class_vptr_get (dest
)), vtab
));
7342 tmp
= build_fold_indirect_ref_loc (input_location
, dest
);
7344 /* For deferred strings insert a memcpy. */
7345 if (cm
->ts
.type
== BT_CHARACTER
&& cm
->ts
.deferred
)
7348 gcc_assert (se
.string_length
|| expr
->ts
.u
.cl
->backend_decl
);
7349 size
= size_of_string_in_bytes (cm
->ts
.kind
, se
.string_length
7351 : expr
->ts
.u
.cl
->backend_decl
);
7352 tmp
= gfc_build_memcpy_call (tmp
, se
.expr
, size
);
7353 gfc_add_expr_to_block (&block
, tmp
);
7356 gfc_add_modify (&block
, tmp
,
7357 fold_convert (TREE_TYPE (tmp
), se
.expr
));
7358 gfc_add_block_to_block (&block
, &se
.post
);
7360 else if (expr
->ts
.type
== BT_UNION
)
7363 gfc_constructor
*c
= gfc_constructor_first (expr
->value
.constructor
);
7364 /* We mark that the entire union should be initialized with a contrived
7365 EXPR_NULL expression at the beginning. */
7366 if (c
!= NULL
&& c
->n
.component
== NULL
7367 && c
->expr
!= NULL
&& c
->expr
->expr_type
== EXPR_NULL
)
7369 tmp
= build2_loc (input_location
, MODIFY_EXPR
, void_type_node
,
7370 dest
, build_constructor (TREE_TYPE (dest
), NULL
));
7371 gfc_add_expr_to_block (&block
, tmp
);
7372 c
= gfc_constructor_next (c
);
7374 /* The following constructor expression, if any, represents a specific
7375 map intializer, as given by the user. */
7376 if (c
!= NULL
&& c
->expr
!= NULL
)
7378 gcc_assert (expr
->expr_type
== EXPR_STRUCTURE
);
7379 tmp
= gfc_trans_structure_assign (dest
, expr
, expr
->symtree
!= NULL
);
7380 gfc_add_expr_to_block (&block
, tmp
);
7383 else if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.f90_type
!= BT_VOID
)
7385 if (expr
->expr_type
!= EXPR_STRUCTURE
)
7387 tree dealloc
= NULL_TREE
;
7388 gfc_init_se (&se
, NULL
);
7389 gfc_conv_expr (&se
, expr
);
7390 gfc_add_block_to_block (&block
, &se
.pre
);
7391 /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
7392 expression in a temporary variable and deallocate the allocatable
7393 components. Then we can the copy the expression to the result. */
7394 if (cm
->ts
.u
.derived
->attr
.alloc_comp
7395 && expr
->expr_type
!= EXPR_VARIABLE
)
7397 se
.expr
= gfc_evaluate_now (se
.expr
, &block
);
7398 dealloc
= gfc_deallocate_alloc_comp (cm
->ts
.u
.derived
, se
.expr
,
7401 gfc_add_modify (&block
, dest
,
7402 fold_convert (TREE_TYPE (dest
), se
.expr
));
7403 if (cm
->ts
.u
.derived
->attr
.alloc_comp
7404 && expr
->expr_type
!= EXPR_NULL
)
7406 // TODO: Fix caf_mode
7407 tmp
= gfc_copy_alloc_comp (cm
->ts
.u
.derived
, se
.expr
,
7408 dest
, expr
->rank
, 0);
7409 gfc_add_expr_to_block (&block
, tmp
);
7410 if (dealloc
!= NULL_TREE
)
7411 gfc_add_expr_to_block (&block
, dealloc
);
7413 gfc_add_block_to_block (&block
, &se
.post
);
7417 /* Nested constructors. */
7418 tmp
= gfc_trans_structure_assign (dest
, expr
, expr
->symtree
!= NULL
);
7419 gfc_add_expr_to_block (&block
, tmp
);
7422 else if (gfc_deferred_strlen (cm
, &tmp
))
7426 gcc_assert (strlen
);
7427 strlen
= fold_build3_loc (input_location
, COMPONENT_REF
,
7429 TREE_OPERAND (dest
, 0),
7432 if (expr
->expr_type
== EXPR_NULL
)
7434 tmp
= build_int_cst (TREE_TYPE (cm
->backend_decl
), 0);
7435 gfc_add_modify (&block
, dest
, tmp
);
7436 tmp
= build_int_cst (TREE_TYPE (strlen
), 0);
7437 gfc_add_modify (&block
, strlen
, tmp
);
7442 gfc_init_se (&se
, NULL
);
7443 gfc_conv_expr (&se
, expr
);
7444 size
= size_of_string_in_bytes (cm
->ts
.kind
, se
.string_length
);
7445 tmp
= build_call_expr_loc (input_location
,
7446 builtin_decl_explicit (BUILT_IN_MALLOC
),
7448 gfc_add_modify (&block
, dest
,
7449 fold_convert (TREE_TYPE (dest
), tmp
));
7450 gfc_add_modify (&block
, strlen
, se
.string_length
);
7451 tmp
= gfc_build_memcpy_call (dest
, se
.expr
, size
);
7452 gfc_add_expr_to_block (&block
, tmp
);
7455 else if (!cm
->attr
.artificial
)
7457 /* Scalar component (excluding deferred parameters). */
7458 gfc_init_se (&se
, NULL
);
7459 gfc_init_se (&lse
, NULL
);
7461 gfc_conv_expr (&se
, expr
);
7462 if (cm
->ts
.type
== BT_CHARACTER
)
7463 lse
.string_length
= cm
->ts
.u
.cl
->backend_decl
;
7465 tmp
= gfc_trans_scalar_assign (&lse
, &se
, cm
->ts
, false, false);
7466 gfc_add_expr_to_block (&block
, tmp
);
7468 return gfc_finish_block (&block
);
7471 /* Assign a derived type constructor to a variable. */
7474 gfc_trans_structure_assign (tree dest
, gfc_expr
* expr
, bool init
, bool coarray
)
7483 gfc_start_block (&block
);
7484 cm
= expr
->ts
.u
.derived
->components
;
7486 if (expr
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_C_BINDING
7487 && (expr
->ts
.u
.derived
->intmod_sym_id
== ISOCBINDING_PTR
7488 || expr
->ts
.u
.derived
->intmod_sym_id
== ISOCBINDING_FUNPTR
))
7492 gfc_init_se (&se
, NULL
);
7493 gfc_init_se (&lse
, NULL
);
7494 gfc_conv_expr (&se
, gfc_constructor_first (expr
->value
.constructor
)->expr
);
7496 gfc_add_modify (&block
, lse
.expr
,
7497 fold_convert (TREE_TYPE (lse
.expr
), se
.expr
));
7499 return gfc_finish_block (&block
);
7503 gfc_init_se (&se
, NULL
);
7505 for (c
= gfc_constructor_first (expr
->value
.constructor
);
7506 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
7508 /* Skip absent members in default initializers. */
7509 if (!c
->expr
&& !cm
->attr
.allocatable
)
7512 /* Register the component with the caf-lib before it is initialized.
7513 Register only allocatable components, that are not coarray'ed
7514 components (%comp[*]). Only register when the constructor is not the
7516 if (coarray
&& !cm
->attr
.codimension
7517 && (cm
->attr
.allocatable
|| cm
->attr
.pointer
)
7518 && (!c
->expr
|| c
->expr
->expr_type
== EXPR_NULL
))
7520 tree token
, desc
, size
;
7521 bool is_array
= cm
->ts
.type
== BT_CLASS
7522 ? CLASS_DATA (cm
)->attr
.dimension
: cm
->attr
.dimension
;
7524 field
= cm
->backend_decl
;
7525 field
= fold_build3_loc (input_location
, COMPONENT_REF
,
7526 TREE_TYPE (field
), dest
, field
, NULL_TREE
);
7527 if (cm
->ts
.type
== BT_CLASS
)
7528 field
= gfc_class_data_get (field
);
7530 token
= is_array
? gfc_conv_descriptor_token (field
)
7531 : fold_build3_loc (input_location
, COMPONENT_REF
,
7532 TREE_TYPE (cm
->caf_token
), dest
,
7533 cm
->caf_token
, NULL_TREE
);
7537 /* The _caf_register routine looks at the rank of the array
7538 descriptor to decide whether the data registered is an array
7540 int rank
= cm
->ts
.type
== BT_CLASS
? CLASS_DATA (cm
)->as
->rank
7542 /* When the rank is not known just set a positive rank, which
7543 suffices to recognize the data as array. */
7546 size
= integer_zero_node
;
7548 gfc_add_modify (&block
, gfc_conv_descriptor_dtype (desc
),
7549 build_int_cst (gfc_array_index_type
, rank
));
7553 desc
= gfc_conv_scalar_to_descriptor (&se
, field
,
7554 cm
->ts
.type
== BT_CLASS
7555 ? CLASS_DATA (cm
)->attr
7557 size
= TYPE_SIZE_UNIT (TREE_TYPE (field
));
7559 gfc_add_block_to_block (&block
, &se
.pre
);
7560 tmp
= build_call_expr_loc (input_location
, gfor_fndecl_caf_register
,
7561 7, size
, build_int_cst (
7563 GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY
),
7564 gfc_build_addr_expr (pvoid_type_node
,
7566 gfc_build_addr_expr (NULL_TREE
, desc
),
7567 null_pointer_node
, null_pointer_node
,
7569 gfc_add_expr_to_block (&block
, tmp
);
7571 field
= cm
->backend_decl
;
7572 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (field
),
7573 dest
, field
, NULL_TREE
);
7576 gfc_expr
*e
= gfc_get_null_expr (NULL
);
7577 tmp
= gfc_trans_subcomponent_assign (tmp
, cm
, e
, expr
->ts
.u
.derived
,
7582 tmp
= gfc_trans_subcomponent_assign (tmp
, cm
, c
->expr
,
7583 expr
->ts
.u
.derived
, init
);
7584 gfc_add_expr_to_block (&block
, tmp
);
7586 return gfc_finish_block (&block
);
7590 gfc_conv_union_initializer (vec
<constructor_elt
, va_gc
> *v
,
7591 gfc_component
*un
, gfc_expr
*init
)
7593 gfc_constructor
*ctor
;
7595 if (un
->ts
.type
!= BT_UNION
|| un
== NULL
|| init
== NULL
)
7598 ctor
= gfc_constructor_first (init
->value
.constructor
);
7600 if (ctor
== NULL
|| ctor
->expr
== NULL
)
7603 gcc_assert (init
->expr_type
== EXPR_STRUCTURE
);
7605 /* If we have an 'initialize all' constructor, do it first. */
7606 if (ctor
->expr
->expr_type
== EXPR_NULL
)
7608 tree union_type
= TREE_TYPE (un
->backend_decl
);
7609 tree val
= build_constructor (union_type
, NULL
);
7610 CONSTRUCTOR_APPEND_ELT (v
, un
->backend_decl
, val
);
7611 ctor
= gfc_constructor_next (ctor
);
7614 /* Add the map initializer on top. */
7615 if (ctor
!= NULL
&& ctor
->expr
!= NULL
)
7617 gcc_assert (ctor
->expr
->expr_type
== EXPR_STRUCTURE
);
7618 tree val
= gfc_conv_initializer (ctor
->expr
, &un
->ts
,
7619 TREE_TYPE (un
->backend_decl
),
7620 un
->attr
.dimension
, un
->attr
.pointer
,
7621 un
->attr
.proc_pointer
);
7622 CONSTRUCTOR_APPEND_ELT (v
, un
->backend_decl
, val
);
7626 /* Build an expression for a constructor. If init is nonzero then
7627 this is part of a static variable initializer. */
7630 gfc_conv_structure (gfc_se
* se
, gfc_expr
* expr
, int init
)
7637 vec
<constructor_elt
, va_gc
> *v
= NULL
;
7639 gcc_assert (se
->ss
== NULL
);
7640 gcc_assert (expr
->expr_type
== EXPR_STRUCTURE
);
7641 type
= gfc_typenode_for_spec (&expr
->ts
);
7645 /* Create a temporary variable and fill it in. */
7646 se
->expr
= gfc_create_var (type
, expr
->ts
.u
.derived
->name
);
7647 /* The symtree in expr is NULL, if the code to generate is for
7648 initializing the static members only. */
7649 tmp
= gfc_trans_structure_assign (se
->expr
, expr
, expr
->symtree
!= NULL
,
7651 gfc_add_expr_to_block (&se
->pre
, tmp
);
7655 cm
= expr
->ts
.u
.derived
->components
;
7657 for (c
= gfc_constructor_first (expr
->value
.constructor
);
7658 c
; c
= gfc_constructor_next (c
), cm
= cm
->next
)
7660 /* Skip absent members in default initializers and allocatable
7661 components. Although the latter have a default initializer
7662 of EXPR_NULL,... by default, the static nullify is not needed
7663 since this is done every time we come into scope. */
7664 if (!c
->expr
|| (cm
->attr
.allocatable
&& cm
->attr
.flavor
!= FL_PROCEDURE
))
7667 if (cm
->initializer
&& cm
->initializer
->expr_type
!= EXPR_NULL
7668 && strcmp (cm
->name
, "_extends") == 0
7669 && cm
->initializer
->symtree
)
7673 vtabs
= cm
->initializer
->symtree
->n
.sym
;
7674 vtab
= gfc_build_addr_expr (NULL_TREE
, gfc_get_symbol_decl (vtabs
));
7675 vtab
= unshare_expr_without_location (vtab
);
7676 CONSTRUCTOR_APPEND_ELT (v
, cm
->backend_decl
, vtab
);
7678 else if (cm
->ts
.u
.derived
&& strcmp (cm
->name
, "_size") == 0)
7680 val
= TYPE_SIZE_UNIT (gfc_get_derived_type (cm
->ts
.u
.derived
));
7681 CONSTRUCTOR_APPEND_ELT (v
, cm
->backend_decl
,
7682 fold_convert (TREE_TYPE (cm
->backend_decl
),
7685 else if (cm
->ts
.type
== BT_INTEGER
&& strcmp (cm
->name
, "_len") == 0)
7686 CONSTRUCTOR_APPEND_ELT (v
, cm
->backend_decl
,
7687 fold_convert (TREE_TYPE (cm
->backend_decl
),
7688 integer_zero_node
));
7689 else if (cm
->ts
.type
== BT_UNION
)
7690 gfc_conv_union_initializer (v
, cm
, c
->expr
);
7693 val
= gfc_conv_initializer (c
->expr
, &cm
->ts
,
7694 TREE_TYPE (cm
->backend_decl
),
7695 cm
->attr
.dimension
, cm
->attr
.pointer
,
7696 cm
->attr
.proc_pointer
);
7697 val
= unshare_expr_without_location (val
);
7699 /* Append it to the constructor list. */
7700 CONSTRUCTOR_APPEND_ELT (v
, cm
->backend_decl
, val
);
7704 se
->expr
= build_constructor (type
, v
);
7706 TREE_CONSTANT (se
->expr
) = 1;
7710 /* Translate a substring expression. */
7713 gfc_conv_substring_expr (gfc_se
* se
, gfc_expr
* expr
)
7719 gcc_assert (ref
== NULL
|| ref
->type
== REF_SUBSTRING
);
7721 se
->expr
= gfc_build_wide_string_const (expr
->ts
.kind
,
7722 expr
->value
.character
.length
,
7723 expr
->value
.character
.string
);
7725 se
->string_length
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se
->expr
)));
7726 TYPE_STRING_FLAG (TREE_TYPE (se
->expr
)) = 1;
7729 gfc_conv_substring (se
, ref
, expr
->ts
.kind
, NULL
, &expr
->where
);
7733 /* Entry point for expression translation. Evaluates a scalar quantity.
7734 EXPR is the expression to be translated, and SE is the state structure if
7735 called from within the scalarized. */
7738 gfc_conv_expr (gfc_se
* se
, gfc_expr
* expr
)
7743 if (ss
&& ss
->info
->expr
== expr
7744 && (ss
->info
->type
== GFC_SS_SCALAR
7745 || ss
->info
->type
== GFC_SS_REFERENCE
))
7747 gfc_ss_info
*ss_info
;
7750 /* Substitute a scalar expression evaluated outside the scalarization
7752 se
->expr
= ss_info
->data
.scalar
.value
;
7753 if (gfc_scalar_elemental_arg_saved_as_reference (ss_info
))
7754 se
->expr
= build_fold_indirect_ref_loc (input_location
, se
->expr
);
7756 se
->string_length
= ss_info
->string_length
;
7757 gfc_advance_se_ss_chain (se
);
7761 /* We need to convert the expressions for the iso_c_binding derived types.
7762 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
7763 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
7764 typespec for the C_PTR and C_FUNPTR symbols, which has already been
7765 updated to be an integer with a kind equal to the size of a (void *). */
7766 if (expr
->ts
.type
== BT_DERIVED
&& expr
->ts
.u
.derived
->ts
.f90_type
== BT_VOID
7767 && expr
->ts
.u
.derived
->attr
.is_bind_c
)
7769 if (expr
->expr_type
== EXPR_VARIABLE
7770 && (expr
->symtree
->n
.sym
->intmod_sym_id
== ISOCBINDING_NULL_PTR
7771 || expr
->symtree
->n
.sym
->intmod_sym_id
7772 == ISOCBINDING_NULL_FUNPTR
))
7774 /* Set expr_type to EXPR_NULL, which will result in
7775 null_pointer_node being used below. */
7776 expr
->expr_type
= EXPR_NULL
;
7780 /* Update the type/kind of the expression to be what the new
7781 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
7782 expr
->ts
.type
= BT_INTEGER
;
7783 expr
->ts
.f90_type
= BT_VOID
;
7784 expr
->ts
.kind
= gfc_index_integer_kind
;
7788 gfc_fix_class_refs (expr
);
7790 switch (expr
->expr_type
)
7793 gfc_conv_expr_op (se
, expr
);
7797 gfc_conv_function_expr (se
, expr
);
7801 gfc_conv_constant (se
, expr
);
7805 gfc_conv_variable (se
, expr
);
7809 se
->expr
= null_pointer_node
;
7812 case EXPR_SUBSTRING
:
7813 gfc_conv_substring_expr (se
, expr
);
7816 case EXPR_STRUCTURE
:
7817 gfc_conv_structure (se
, expr
, 0);
7821 gfc_conv_array_constructor_expr (se
, expr
);
7830 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
7831 of an assignment. */
7833 gfc_conv_expr_lhs (gfc_se
* se
, gfc_expr
* expr
)
7835 gfc_conv_expr (se
, expr
);
7836 /* All numeric lvalues should have empty post chains. If not we need to
7837 figure out a way of rewriting an lvalue so that it has no post chain. */
7838 gcc_assert (expr
->ts
.type
== BT_CHARACTER
|| !se
->post
.head
);
7841 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
7842 numeric expressions. Used for scalar values where inserting cleanup code
7845 gfc_conv_expr_val (gfc_se
* se
, gfc_expr
* expr
)
7849 gcc_assert (expr
->ts
.type
!= BT_CHARACTER
);
7850 gfc_conv_expr (se
, expr
);
7853 val
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
7854 gfc_add_modify (&se
->pre
, val
, se
->expr
);
7856 gfc_add_block_to_block (&se
->pre
, &se
->post
);
7860 /* Helper to translate an expression and convert it to a particular type. */
7862 gfc_conv_expr_type (gfc_se
* se
, gfc_expr
* expr
, tree type
)
7864 gfc_conv_expr_val (se
, expr
);
7865 se
->expr
= convert (type
, se
->expr
);
7869 /* Converts an expression so that it can be passed by reference. Scalar
7873 gfc_conv_expr_reference (gfc_se
* se
, gfc_expr
* expr
)
7879 if (ss
&& ss
->info
->expr
== expr
7880 && ss
->info
->type
== GFC_SS_REFERENCE
)
7882 /* Returns a reference to the scalar evaluated outside the loop
7884 gfc_conv_expr (se
, expr
);
7886 if (expr
->ts
.type
== BT_CHARACTER
7887 && expr
->expr_type
!= EXPR_FUNCTION
)
7888 gfc_conv_string_parameter (se
);
7890 se
->expr
= gfc_build_addr_expr (NULL_TREE
, se
->expr
);
7895 if (expr
->ts
.type
== BT_CHARACTER
)
7897 gfc_conv_expr (se
, expr
);
7898 gfc_conv_string_parameter (se
);
7902 if (expr
->expr_type
== EXPR_VARIABLE
)
7904 se
->want_pointer
= 1;
7905 gfc_conv_expr (se
, expr
);
7908 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
7909 gfc_add_modify (&se
->pre
, var
, se
->expr
);
7910 gfc_add_block_to_block (&se
->pre
, &se
->post
);
7916 if (expr
->expr_type
== EXPR_FUNCTION
7917 && ((expr
->value
.function
.esym
7918 && expr
->value
.function
.esym
->result
->attr
.pointer
7919 && !expr
->value
.function
.esym
->result
->attr
.dimension
)
7920 || (!expr
->value
.function
.esym
&& !expr
->ref
7921 && expr
->symtree
->n
.sym
->attr
.pointer
7922 && !expr
->symtree
->n
.sym
->attr
.dimension
)))
7924 se
->want_pointer
= 1;
7925 gfc_conv_expr (se
, expr
);
7926 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
7927 gfc_add_modify (&se
->pre
, var
, se
->expr
);
7932 gfc_conv_expr (se
, expr
);
7934 /* Create a temporary var to hold the value. */
7935 if (TREE_CONSTANT (se
->expr
))
7937 tree tmp
= se
->expr
;
7938 STRIP_TYPE_NOPS (tmp
);
7939 var
= build_decl (input_location
,
7940 CONST_DECL
, NULL
, TREE_TYPE (tmp
));
7941 DECL_INITIAL (var
) = tmp
;
7942 TREE_STATIC (var
) = 1;
7947 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
7948 gfc_add_modify (&se
->pre
, var
, se
->expr
);
7950 gfc_add_block_to_block (&se
->pre
, &se
->post
);
7952 /* Take the address of that value. */
7953 se
->expr
= gfc_build_addr_expr (NULL_TREE
, var
);
7957 /* Get the _len component for an unlimited polymorphic expression. */
7960 trans_get_upoly_len (stmtblock_t
*block
, gfc_expr
*expr
)
7963 gfc_ref
*ref
= expr
->ref
;
7965 gfc_init_se (&se
, NULL
);
7966 while (ref
&& ref
->next
)
7968 gfc_add_len_component (expr
);
7969 gfc_conv_expr (&se
, expr
);
7970 gfc_add_block_to_block (block
, &se
.pre
);
7971 gcc_assert (se
.post
.head
== NULL_TREE
);
7974 gfc_free_ref_list (ref
->next
);
7979 gfc_free_ref_list (expr
->ref
);
7986 /* Assign _vptr and _len components as appropriate. BLOCK should be a
7987 statement-list outside of the scalarizer-loop. When code is generated, that
7988 depends on the scalarized expression, it is added to RSE.PRE.
7989 Returns le's _vptr tree and when set the len expressions in to_lenp and
7990 from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
7994 trans_class_vptr_len_assignment (stmtblock_t
*block
, gfc_expr
* le
,
7995 gfc_expr
* re
, gfc_se
*rse
,
7996 tree
* to_lenp
, tree
* from_lenp
)
7999 gfc_expr
* vptr_expr
;
8000 tree tmp
, to_len
= NULL_TREE
, from_len
= NULL_TREE
, lhs_vptr
;
8001 bool set_vptr
= false, temp_rhs
= false;
8002 stmtblock_t
*pre
= block
;
8004 /* Create a temporary for complicated expressions. */
8005 if (re
->expr_type
!= EXPR_VARIABLE
&& re
->expr_type
!= EXPR_NULL
8006 && rse
->expr
!= NULL_TREE
&& !DECL_P (rse
->expr
))
8008 tmp
= gfc_create_var (TREE_TYPE (rse
->expr
), "rhs");
8010 gfc_add_modify (&rse
->pre
, tmp
, rse
->expr
);
8015 /* Get the _vptr for the left-hand side expression. */
8016 gfc_init_se (&se
, NULL
);
8017 vptr_expr
= gfc_find_and_cut_at_last_class_ref (le
);
8018 if (vptr_expr
!= NULL
&& gfc_expr_attr (vptr_expr
).class_ok
)
8020 /* Care about _len for unlimited polymorphic entities. */
8021 if (UNLIMITED_POLY (vptr_expr
)
8022 || (vptr_expr
->ts
.type
== BT_DERIVED
8023 && vptr_expr
->ts
.u
.derived
->attr
.unlimited_polymorphic
))
8024 to_len
= trans_get_upoly_len (block
, vptr_expr
);
8025 gfc_add_vptr_component (vptr_expr
);
8029 vptr_expr
= gfc_lval_expr_from_sym (gfc_find_vtab (&le
->ts
));
8030 se
.want_pointer
= 1;
8031 gfc_conv_expr (&se
, vptr_expr
);
8032 gfc_free_expr (vptr_expr
);
8033 gfc_add_block_to_block (block
, &se
.pre
);
8034 gcc_assert (se
.post
.head
== NULL_TREE
);
8036 STRIP_NOPS (lhs_vptr
);
8038 /* Set the _vptr only when the left-hand side of the assignment is a
8042 /* Get the vptr from the rhs expression only, when it is variable.
8043 Functions are expected to be assigned to a temporary beforehand. */
8044 vptr_expr
= re
->expr_type
== EXPR_VARIABLE
8045 ? gfc_find_and_cut_at_last_class_ref (re
)
8047 if (vptr_expr
!= NULL
&& vptr_expr
->ts
.type
== BT_CLASS
)
8049 if (to_len
!= NULL_TREE
)
8051 /* Get the _len information from the rhs. */
8052 if (UNLIMITED_POLY (vptr_expr
)
8053 || (vptr_expr
->ts
.type
== BT_DERIVED
8054 && vptr_expr
->ts
.u
.derived
->attr
.unlimited_polymorphic
))
8055 from_len
= trans_get_upoly_len (block
, vptr_expr
);
8057 gfc_add_vptr_component (vptr_expr
);
8061 if (re
->expr_type
== EXPR_VARIABLE
8062 && DECL_P (re
->symtree
->n
.sym
->backend_decl
)
8063 && DECL_LANG_SPECIFIC (re
->symtree
->n
.sym
->backend_decl
)
8064 && GFC_DECL_SAVED_DESCRIPTOR (re
->symtree
->n
.sym
->backend_decl
)
8065 && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
8066 re
->symtree
->n
.sym
->backend_decl
))))
8069 se
.expr
= gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
8070 re
->symtree
->n
.sym
->backend_decl
));
8072 from_len
= gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
8073 re
->symtree
->n
.sym
->backend_decl
));
8075 else if (temp_rhs
&& re
->ts
.type
== BT_CLASS
)
8078 se
.expr
= gfc_class_vptr_get (rse
->expr
);
8080 else if (re
->expr_type
!= EXPR_NULL
)
8081 /* Only when rhs is non-NULL use its declared type for vptr
8083 vptr_expr
= gfc_lval_expr_from_sym (gfc_find_vtab (&re
->ts
));
8085 /* When the rhs is NULL use the vtab of lhs' declared type. */
8086 vptr_expr
= gfc_lval_expr_from_sym (gfc_find_vtab (&le
->ts
));
8091 gfc_init_se (&se
, NULL
);
8092 se
.want_pointer
= 1;
8093 gfc_conv_expr (&se
, vptr_expr
);
8094 gfc_free_expr (vptr_expr
);
8095 gfc_add_block_to_block (block
, &se
.pre
);
8096 gcc_assert (se
.post
.head
== NULL_TREE
);
8098 gfc_add_modify (pre
, lhs_vptr
, fold_convert (TREE_TYPE (lhs_vptr
),
8101 if (to_len
!= NULL_TREE
)
8103 /* The _len component needs to be set. Figure how to get the
8104 value of the right-hand side. */
8105 if (from_len
== NULL_TREE
)
8107 if (rse
->string_length
!= NULL_TREE
)
8108 from_len
= rse
->string_length
;
8109 else if (re
->ts
.type
== BT_CHARACTER
&& re
->ts
.u
.cl
->length
)
8111 from_len
= gfc_get_expr_charlen (re
);
8112 gfc_init_se (&se
, NULL
);
8113 gfc_conv_expr (&se
, re
->ts
.u
.cl
->length
);
8114 gfc_add_block_to_block (block
, &se
.pre
);
8115 gcc_assert (se
.post
.head
== NULL_TREE
);
8116 from_len
= gfc_evaluate_now (se
.expr
, block
);
8119 from_len
= integer_zero_node
;
8121 gfc_add_modify (pre
, to_len
, fold_convert (TREE_TYPE (to_len
),
8126 /* Return the _len trees only, when requested. */
8130 *from_lenp
= from_len
;
8135 /* Assign tokens for pointer components. */
8138 trans_caf_token_assign (gfc_se
*lse
, gfc_se
*rse
, gfc_expr
*expr1
,
8141 symbol_attribute lhs_attr
, rhs_attr
;
8142 tree tmp
, lhs_tok
, rhs_tok
;
8143 /* Flag to indicated component refs on the rhs. */
8146 lhs_attr
= gfc_caf_attr (expr1
);
8147 if (expr2
->expr_type
!= EXPR_NULL
)
8149 rhs_attr
= gfc_caf_attr (expr2
, false, &rhs_cr
);
8150 if (lhs_attr
.codimension
&& rhs_attr
.codimension
)
8152 lhs_tok
= gfc_get_ultimate_alloc_ptr_comps_caf_token (lse
, expr1
);
8153 lhs_tok
= build_fold_indirect_ref (lhs_tok
);
8156 rhs_tok
= gfc_get_ultimate_alloc_ptr_comps_caf_token (rse
, expr2
);
8160 caf_decl
= gfc_get_tree_for_caf_expr (expr2
);
8161 gfc_get_caf_token_offset (rse
, &rhs_tok
, NULL
, caf_decl
,
8164 tmp
= build2_loc (input_location
, MODIFY_EXPR
, void_type_node
,
8166 fold_convert (TREE_TYPE (lhs_tok
), rhs_tok
));
8167 gfc_prepend_expr_to_block (&lse
->post
, tmp
);
8170 else if (lhs_attr
.codimension
)
8172 lhs_tok
= gfc_get_ultimate_alloc_ptr_comps_caf_token (lse
, expr1
);
8173 lhs_tok
= build_fold_indirect_ref (lhs_tok
);
8174 tmp
= build2_loc (input_location
, MODIFY_EXPR
, void_type_node
,
8175 lhs_tok
, null_pointer_node
);
8176 gfc_prepend_expr_to_block (&lse
->post
, tmp
);
8180 /* Indentify class valued proc_pointer assignments. */
8183 pointer_assignment_is_proc_pointer (gfc_expr
* expr1
, gfc_expr
* expr2
)
8188 while (ref
&& ref
->next
)
8191 return ref
&& ref
->type
== REF_COMPONENT
8192 && ref
->u
.c
.component
->attr
.proc_pointer
8193 && expr2
->expr_type
== EXPR_VARIABLE
8194 && expr2
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
;
8199 gfc_trans_pointer_assign (gfc_code
* code
)
8201 return gfc_trans_pointer_assignment (code
->expr1
, code
->expr2
);
8205 /* Generate code for a pointer assignment. */
8208 gfc_trans_pointer_assignment (gfc_expr
* expr1
, gfc_expr
* expr2
)
8216 bool scalar
, non_proc_pointer_assign
;
8219 gfc_start_block (&block
);
8221 gfc_init_se (&lse
, NULL
);
8223 /* Usually testing whether this is not a proc pointer assignment. */
8224 non_proc_pointer_assign
= !pointer_assignment_is_proc_pointer (expr1
, expr2
);
8226 /* Check whether the expression is a scalar or not; we cannot use
8227 expr1->rank as it can be nonzero for proc pointers. */
8228 ss
= gfc_walk_expr (expr1
);
8229 scalar
= ss
== gfc_ss_terminator
;
8231 gfc_free_ss_chain (ss
);
8233 if (expr1
->ts
.type
== BT_DERIVED
&& expr2
->ts
.type
== BT_CLASS
8234 && expr2
->expr_type
!= EXPR_FUNCTION
&& non_proc_pointer_assign
)
8236 gfc_add_data_component (expr2
);
8237 /* The following is required as gfc_add_data_component doesn't
8238 update ts.type if there is a tailing REF_ARRAY. */
8239 expr2
->ts
.type
= BT_DERIVED
;
8244 /* Scalar pointers. */
8245 lse
.want_pointer
= 1;
8246 gfc_conv_expr (&lse
, expr1
);
8247 gfc_init_se (&rse
, NULL
);
8248 rse
.want_pointer
= 1;
8249 gfc_conv_expr (&rse
, expr2
);
8251 if (non_proc_pointer_assign
&& expr1
->ts
.type
== BT_CLASS
)
8253 trans_class_vptr_len_assignment (&block
, expr1
, expr2
, &rse
, NULL
,
8255 lse
.expr
= gfc_class_data_get (lse
.expr
);
8258 if (expr1
->symtree
->n
.sym
->attr
.proc_pointer
8259 && expr1
->symtree
->n
.sym
->attr
.dummy
)
8260 lse
.expr
= build_fold_indirect_ref_loc (input_location
,
8263 if (expr2
->symtree
&& expr2
->symtree
->n
.sym
->attr
.proc_pointer
8264 && expr2
->symtree
->n
.sym
->attr
.dummy
)
8265 rse
.expr
= build_fold_indirect_ref_loc (input_location
,
8268 gfc_add_block_to_block (&block
, &lse
.pre
);
8269 gfc_add_block_to_block (&block
, &rse
.pre
);
8271 /* Check character lengths if character expression. The test is only
8272 really added if -fbounds-check is enabled. Exclude deferred
8273 character length lefthand sides. */
8274 if (expr1
->ts
.type
== BT_CHARACTER
&& expr2
->expr_type
!= EXPR_NULL
8275 && !expr1
->ts
.deferred
8276 && !expr1
->symtree
->n
.sym
->attr
.proc_pointer
8277 && !gfc_is_proc_ptr_comp (expr1
))
8279 gcc_assert (expr2
->ts
.type
== BT_CHARACTER
);
8280 gcc_assert (lse
.string_length
&& rse
.string_length
);
8281 gfc_trans_same_strlen_check ("pointer assignment", &expr1
->where
,
8282 lse
.string_length
, rse
.string_length
,
8286 /* The assignment to an deferred character length sets the string
8287 length to that of the rhs. */
8288 if (expr1
->ts
.deferred
)
8290 if (expr2
->expr_type
!= EXPR_NULL
&& lse
.string_length
!= NULL
)
8291 gfc_add_modify (&block
, lse
.string_length
, rse
.string_length
);
8292 else if (lse
.string_length
!= NULL
)
8293 gfc_add_modify (&block
, lse
.string_length
,
8294 build_int_cst (gfc_charlen_type_node
, 0));
8297 gfc_add_modify (&block
, lse
.expr
,
8298 fold_convert (TREE_TYPE (lse
.expr
), rse
.expr
));
8300 /* Also set the tokens for pointer components in derived typed
8302 if (flag_coarray
== GFC_FCOARRAY_LIB
)
8303 trans_caf_token_assign (&lse
, &rse
, expr1
, expr2
);
8305 gfc_add_block_to_block (&block
, &rse
.post
);
8306 gfc_add_block_to_block (&block
, &lse
.post
);
8312 tree expr1_vptr
= NULL_TREE
;
8314 tree strlen_rhs
= NULL_TREE
;
8316 /* Array pointer. Find the last reference on the LHS and if it is an
8317 array section ref, we're dealing with bounds remapping. In this case,
8318 set it to AR_FULL so that gfc_conv_expr_descriptor does
8319 not see it and process the bounds remapping afterwards explicitly. */
8320 for (remap
= expr1
->ref
; remap
; remap
= remap
->next
)
8321 if (!remap
->next
&& remap
->type
== REF_ARRAY
8322 && remap
->u
.ar
.type
== AR_SECTION
)
8324 rank_remap
= (remap
&& remap
->u
.ar
.end
[0]);
8326 gfc_init_se (&lse
, NULL
);
8328 lse
.descriptor_only
= 1;
8329 gfc_conv_expr_descriptor (&lse
, expr1
);
8330 strlen_lhs
= lse
.string_length
;
8333 if (expr2
->expr_type
== EXPR_NULL
)
8335 /* Just set the data pointer to null. */
8336 gfc_conv_descriptor_data_set (&lse
.pre
, lse
.expr
, null_pointer_node
);
8338 else if (rank_remap
)
8340 /* If we are rank-remapping, just get the RHS's descriptor and
8341 process this later on. */
8342 gfc_init_se (&rse
, NULL
);
8343 rse
.direct_byref
= 1;
8344 rse
.byref_noassign
= 1;
8346 if (expr2
->expr_type
== EXPR_FUNCTION
&& expr2
->ts
.type
== BT_CLASS
)
8348 gfc_conv_function_expr (&rse
, expr2
);
8350 if (expr1
->ts
.type
!= BT_CLASS
)
8351 rse
.expr
= gfc_class_data_get (rse
.expr
);
8354 expr1_vptr
= trans_class_vptr_len_assignment (&block
, expr1
,
8357 gfc_add_block_to_block (&block
, &rse
.pre
);
8358 tmp
= gfc_create_var (TREE_TYPE (rse
.expr
), "ptrtemp");
8359 gfc_add_modify (&lse
.pre
, tmp
, rse
.expr
);
8361 gfc_add_modify (&lse
.pre
, expr1_vptr
,
8362 fold_convert (TREE_TYPE (expr1_vptr
),
8363 gfc_class_vptr_get (tmp
)));
8364 rse
.expr
= gfc_class_data_get (tmp
);
8367 else if (expr2
->expr_type
== EXPR_FUNCTION
)
8369 tree bound
[GFC_MAX_DIMENSIONS
];
8372 for (i
= 0; i
< expr2
->rank
; i
++)
8373 bound
[i
] = NULL_TREE
;
8374 tmp
= gfc_typenode_for_spec (&expr2
->ts
);
8375 tmp
= gfc_get_array_type_bounds (tmp
, expr2
->rank
, 0,
8377 GFC_ARRAY_POINTER_CONT
, false);
8378 tmp
= gfc_create_var (tmp
, "ptrtemp");
8379 rse
.descriptor_only
= 0;
8381 rse
.direct_byref
= 1;
8382 gfc_conv_expr_descriptor (&rse
, expr2
);
8383 strlen_rhs
= rse
.string_length
;
8388 gfc_conv_expr_descriptor (&rse
, expr2
);
8389 strlen_rhs
= rse
.string_length
;
8390 if (expr1
->ts
.type
== BT_CLASS
)
8391 expr1_vptr
= trans_class_vptr_len_assignment (&block
, expr1
,
8396 else if (expr2
->expr_type
== EXPR_VARIABLE
)
8398 /* Assign directly to the LHS's descriptor. */
8399 lse
.descriptor_only
= 0;
8400 lse
.direct_byref
= 1;
8401 gfc_conv_expr_descriptor (&lse
, expr2
);
8402 strlen_rhs
= lse
.string_length
;
8404 /* If this is a subreference array pointer assignment, use the rhs
8405 descriptor element size for the lhs span. */
8406 if (expr1
->symtree
->n
.sym
->attr
.subref_array_pointer
)
8408 decl
= expr1
->symtree
->n
.sym
->backend_decl
;
8409 gfc_init_se (&rse
, NULL
);
8410 rse
.descriptor_only
= 1;
8411 gfc_conv_expr (&rse
, expr2
);
8412 if (expr1
->ts
.type
== BT_CLASS
)
8413 trans_class_vptr_len_assignment (&block
, expr1
, expr2
, &rse
,
8415 tmp
= gfc_get_element_type (TREE_TYPE (rse
.expr
));
8416 tmp
= fold_convert (gfc_array_index_type
, size_in_bytes (tmp
));
8417 if (!INTEGER_CST_P (tmp
))
8418 gfc_add_block_to_block (&lse
.post
, &rse
.pre
);
8419 gfc_add_modify (&lse
.post
, GFC_DECL_SPAN(decl
), tmp
);
8421 else if (expr1
->ts
.type
== BT_CLASS
)
8423 rse
.expr
= NULL_TREE
;
8424 rse
.string_length
= NULL_TREE
;
8425 trans_class_vptr_len_assignment (&block
, expr1
, expr2
, &rse
,
8429 else if (expr2
->expr_type
== EXPR_FUNCTION
&& expr2
->ts
.type
== BT_CLASS
)
8431 gfc_init_se (&rse
, NULL
);
8432 rse
.want_pointer
= 1;
8433 gfc_conv_function_expr (&rse
, expr2
);
8434 if (expr1
->ts
.type
!= BT_CLASS
)
8436 rse
.expr
= gfc_class_data_get (rse
.expr
);
8437 gfc_add_modify (&lse
.pre
, desc
, rse
.expr
);
8441 expr1_vptr
= trans_class_vptr_len_assignment (&block
, expr1
,
8444 gfc_add_block_to_block (&block
, &rse
.pre
);
8445 tmp
= gfc_create_var (TREE_TYPE (rse
.expr
), "ptrtemp");
8446 gfc_add_modify (&lse
.pre
, tmp
, rse
.expr
);
8448 gfc_add_modify (&lse
.pre
, expr1_vptr
,
8449 fold_convert (TREE_TYPE (expr1_vptr
),
8450 gfc_class_vptr_get (tmp
)));
8451 rse
.expr
= gfc_class_data_get (tmp
);
8452 gfc_add_modify (&lse
.pre
, desc
, rse
.expr
);
8457 /* Assign to a temporary descriptor and then copy that
8458 temporary to the pointer. */
8459 tmp
= gfc_create_var (TREE_TYPE (desc
), "ptrtemp");
8460 lse
.descriptor_only
= 0;
8462 lse
.direct_byref
= 1;
8463 gfc_conv_expr_descriptor (&lse
, expr2
);
8464 strlen_rhs
= lse
.string_length
;
8465 gfc_add_modify (&lse
.pre
, desc
, tmp
);
8468 gfc_add_block_to_block (&block
, &lse
.pre
);
8470 gfc_add_block_to_block (&block
, &rse
.pre
);
8472 /* If we do bounds remapping, update LHS descriptor accordingly. */
8476 gcc_assert (remap
->u
.ar
.dimen
== expr1
->rank
);
8480 /* Do rank remapping. We already have the RHS's descriptor
8481 converted in rse and now have to build the correct LHS
8482 descriptor for it. */
8486 tree lbound
, ubound
;
8489 dtype
= gfc_conv_descriptor_dtype (desc
);
8490 tmp
= gfc_get_dtype (TREE_TYPE (desc
));
8491 gfc_add_modify (&block
, dtype
, tmp
);
8493 /* Copy data pointer. */
8494 data
= gfc_conv_descriptor_data_get (rse
.expr
);
8495 gfc_conv_descriptor_data_set (&block
, desc
, data
);
8497 /* Copy offset but adjust it such that it would correspond
8498 to a lbound of zero. */
8499 offs
= gfc_conv_descriptor_offset_get (rse
.expr
);
8500 for (dim
= 0; dim
< expr2
->rank
; ++dim
)
8502 stride
= gfc_conv_descriptor_stride_get (rse
.expr
,
8504 lbound
= gfc_conv_descriptor_lbound_get (rse
.expr
,
8506 tmp
= fold_build2_loc (input_location
, MULT_EXPR
,
8507 gfc_array_index_type
, stride
, lbound
);
8508 offs
= fold_build2_loc (input_location
, PLUS_EXPR
,
8509 gfc_array_index_type
, offs
, tmp
);
8511 gfc_conv_descriptor_offset_set (&block
, desc
, offs
);
8513 /* Set the bounds as declared for the LHS and calculate strides as
8514 well as another offset update accordingly. */
8515 stride
= gfc_conv_descriptor_stride_get (rse
.expr
,
8517 for (dim
= 0; dim
< expr1
->rank
; ++dim
)
8522 gcc_assert (remap
->u
.ar
.start
[dim
] && remap
->u
.ar
.end
[dim
]);
8524 /* Convert declared bounds. */
8525 gfc_init_se (&lower_se
, NULL
);
8526 gfc_init_se (&upper_se
, NULL
);
8527 gfc_conv_expr (&lower_se
, remap
->u
.ar
.start
[dim
]);
8528 gfc_conv_expr (&upper_se
, remap
->u
.ar
.end
[dim
]);
8530 gfc_add_block_to_block (&block
, &lower_se
.pre
);
8531 gfc_add_block_to_block (&block
, &upper_se
.pre
);
8533 lbound
= fold_convert (gfc_array_index_type
, lower_se
.expr
);
8534 ubound
= fold_convert (gfc_array_index_type
, upper_se
.expr
);
8536 lbound
= gfc_evaluate_now (lbound
, &block
);
8537 ubound
= gfc_evaluate_now (ubound
, &block
);
8539 gfc_add_block_to_block (&block
, &lower_se
.post
);
8540 gfc_add_block_to_block (&block
, &upper_se
.post
);
8542 /* Set bounds in descriptor. */
8543 gfc_conv_descriptor_lbound_set (&block
, desc
,
8544 gfc_rank_cst
[dim
], lbound
);
8545 gfc_conv_descriptor_ubound_set (&block
, desc
,
8546 gfc_rank_cst
[dim
], ubound
);
8549 stride
= gfc_evaluate_now (stride
, &block
);
8550 gfc_conv_descriptor_stride_set (&block
, desc
,
8551 gfc_rank_cst
[dim
], stride
);
8553 /* Update offset. */
8554 offs
= gfc_conv_descriptor_offset_get (desc
);
8555 tmp
= fold_build2_loc (input_location
, MULT_EXPR
,
8556 gfc_array_index_type
, lbound
, stride
);
8557 offs
= fold_build2_loc (input_location
, MINUS_EXPR
,
8558 gfc_array_index_type
, offs
, tmp
);
8559 offs
= gfc_evaluate_now (offs
, &block
);
8560 gfc_conv_descriptor_offset_set (&block
, desc
, offs
);
8562 /* Update stride. */
8563 tmp
= gfc_conv_array_extent_dim (lbound
, ubound
, NULL
);
8564 stride
= fold_build2_loc (input_location
, MULT_EXPR
,
8565 gfc_array_index_type
, stride
, tmp
);
8570 /* Bounds remapping. Just shift the lower bounds. */
8572 gcc_assert (expr1
->rank
== expr2
->rank
);
8574 for (dim
= 0; dim
< remap
->u
.ar
.dimen
; ++dim
)
8578 gcc_assert (remap
->u
.ar
.start
[dim
]);
8579 gcc_assert (!remap
->u
.ar
.end
[dim
]);
8580 gfc_init_se (&lbound_se
, NULL
);
8581 gfc_conv_expr (&lbound_se
, remap
->u
.ar
.start
[dim
]);
8583 gfc_add_block_to_block (&block
, &lbound_se
.pre
);
8584 gfc_conv_shift_descriptor_lbound (&block
, desc
,
8585 dim
, lbound_se
.expr
);
8586 gfc_add_block_to_block (&block
, &lbound_se
.post
);
8591 /* Check string lengths if applicable. The check is only really added
8592 to the output code if -fbounds-check is enabled. */
8593 if (expr1
->ts
.type
== BT_CHARACTER
&& expr2
->expr_type
!= EXPR_NULL
)
8595 gcc_assert (expr2
->ts
.type
== BT_CHARACTER
);
8596 gcc_assert (strlen_lhs
&& strlen_rhs
);
8597 gfc_trans_same_strlen_check ("pointer assignment", &expr1
->where
,
8598 strlen_lhs
, strlen_rhs
, &block
);
8601 /* If rank remapping was done, check with -fcheck=bounds that
8602 the target is at least as large as the pointer. */
8603 if (rank_remap
&& (gfc_option
.rtcheck
& GFC_RTCHECK_BOUNDS
))
8609 lsize
= gfc_conv_descriptor_size (lse
.expr
, expr1
->rank
);
8610 rsize
= gfc_conv_descriptor_size (rse
.expr
, expr2
->rank
);
8612 lsize
= gfc_evaluate_now (lsize
, &block
);
8613 rsize
= gfc_evaluate_now (rsize
, &block
);
8614 fault
= fold_build2_loc (input_location
, LT_EXPR
, boolean_type_node
,
8617 msg
= _("Target of rank remapping is too small (%ld < %ld)");
8618 gfc_trans_runtime_check (true, false, fault
, &block
, &expr2
->where
,
8622 gfc_add_block_to_block (&block
, &lse
.post
);
8624 gfc_add_block_to_block (&block
, &rse
.post
);
8627 return gfc_finish_block (&block
);
8631 /* Makes sure se is suitable for passing as a function string parameter. */
8632 /* TODO: Need to check all callers of this function. It may be abused. */
8635 gfc_conv_string_parameter (gfc_se
* se
)
8639 if (TREE_CODE (se
->expr
) == STRING_CST
)
8641 type
= TREE_TYPE (TREE_TYPE (se
->expr
));
8642 se
->expr
= gfc_build_addr_expr (build_pointer_type (type
), se
->expr
);
8646 if (TYPE_STRING_FLAG (TREE_TYPE (se
->expr
)))
8648 if (TREE_CODE (se
->expr
) != INDIRECT_REF
)
8650 type
= TREE_TYPE (se
->expr
);
8651 se
->expr
= gfc_build_addr_expr (build_pointer_type (type
), se
->expr
);
8655 type
= gfc_get_character_type_len (gfc_default_character_kind
,
8657 type
= build_pointer_type (type
);
8658 se
->expr
= gfc_build_addr_expr (type
, se
->expr
);
8662 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se
->expr
)));
8666 /* Generate code for assignment of scalar variables. Includes character
8667 strings and derived types with allocatable components.
8668 If you know that the LHS has no allocations, set dealloc to false.
8670 DEEP_COPY has no effect if the typespec TS is not a derived type with
8671 allocatable components. Otherwise, if it is set, an explicit copy of each
8672 allocatable component is made. This is necessary as a simple copy of the
8673 whole object would copy array descriptors as is, so that the lhs's
8674 allocatable components would point to the rhs's after the assignment.
8675 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
8676 necessary if the rhs is a non-pointer function, as the allocatable components
8677 are not accessible by other means than the function's result after the
8678 function has returned. It is even more subtle when temporaries are involved,
8679 as the two following examples show:
8680 1. When we evaluate an array constructor, a temporary is created. Thus
8681 there is theoretically no alias possible. However, no deep copy is
8682 made for this temporary, so that if the constructor is made of one or
8683 more variable with allocatable components, those components still point
8684 to the variable's: DEEP_COPY should be set for the assignment from the
8685 temporary to the lhs in that case.
8686 2. When assigning a scalar to an array, we evaluate the scalar value out
8687 of the loop, store it into a temporary variable, and assign from that.
8688 In that case, deep copying when assigning to the temporary would be a
8689 waste of resources; however deep copies should happen when assigning from
8690 the temporary to each array element: again DEEP_COPY should be set for
8691 the assignment from the temporary to the lhs. */
8694 gfc_trans_scalar_assign (gfc_se
* lse
, gfc_se
* rse
, gfc_typespec ts
,
8695 bool deep_copy
, bool dealloc
, bool in_coarray
)
8701 gfc_init_block (&block
);
8703 if (ts
.type
== BT_CHARACTER
)
8708 if (lse
->string_length
!= NULL_TREE
)
8710 gfc_conv_string_parameter (lse
);
8711 gfc_add_block_to_block (&block
, &lse
->pre
);
8712 llen
= lse
->string_length
;
8715 if (rse
->string_length
!= NULL_TREE
)
8717 gfc_conv_string_parameter (rse
);
8718 gfc_add_block_to_block (&block
, &rse
->pre
);
8719 rlen
= rse
->string_length
;
8722 gfc_trans_string_copy (&block
, llen
, lse
->expr
, ts
.kind
, rlen
,
8723 rse
->expr
, ts
.kind
);
8725 else if (gfc_bt_struct (ts
.type
) && ts
.u
.derived
->attr
.alloc_comp
)
8727 tree tmp_var
= NULL_TREE
;
8730 /* Are the rhs and the lhs the same? */
8733 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
8734 gfc_build_addr_expr (NULL_TREE
, lse
->expr
),
8735 gfc_build_addr_expr (NULL_TREE
, rse
->expr
));
8736 cond
= gfc_evaluate_now (cond
, &lse
->pre
);
8739 /* Deallocate the lhs allocated components as long as it is not
8740 the same as the rhs. This must be done following the assignment
8741 to prevent deallocating data that could be used in the rhs
8745 tmp_var
= gfc_evaluate_now (lse
->expr
, &lse
->pre
);
8746 tmp
= gfc_deallocate_alloc_comp_no_caf (ts
.u
.derived
, tmp_var
, 0);
8748 tmp
= build3_v (COND_EXPR
, cond
, build_empty_stmt (input_location
),
8750 gfc_add_expr_to_block (&lse
->post
, tmp
);
8753 gfc_add_block_to_block (&block
, &rse
->pre
);
8754 gfc_add_block_to_block (&block
, &lse
->pre
);
8756 gfc_add_modify (&block
, lse
->expr
,
8757 fold_convert (TREE_TYPE (lse
->expr
), rse
->expr
));
8759 /* Restore pointer address of coarray components. */
8760 if (ts
.u
.derived
->attr
.coarray_comp
&& deep_copy
&& tmp_var
!= NULL_TREE
)
8762 tmp
= gfc_reassign_alloc_comp_caf (ts
.u
.derived
, tmp_var
, lse
->expr
);
8763 tmp
= build3_v (COND_EXPR
, cond
, build_empty_stmt (input_location
),
8765 gfc_add_expr_to_block (&block
, tmp
);
8768 /* Do a deep copy if the rhs is a variable, if it is not the
8772 int caf_mode
= in_coarray
? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
8773 | GFC_STRUCTURE_CAF_MODE_IN_COARRAY
) : 0;
8774 tmp
= gfc_copy_alloc_comp (ts
.u
.derived
, rse
->expr
, lse
->expr
, 0,
8776 tmp
= build3_v (COND_EXPR
, cond
, build_empty_stmt (input_location
),
8778 gfc_add_expr_to_block (&block
, tmp
);
8781 else if (gfc_bt_struct (ts
.type
) || ts
.type
== BT_CLASS
)
8783 gfc_add_block_to_block (&block
, &lse
->pre
);
8784 gfc_add_block_to_block (&block
, &rse
->pre
);
8785 tmp
= fold_build1_loc (input_location
, VIEW_CONVERT_EXPR
,
8786 TREE_TYPE (lse
->expr
), rse
->expr
);
8787 gfc_add_modify (&block
, lse
->expr
, tmp
);
8791 gfc_add_block_to_block (&block
, &lse
->pre
);
8792 gfc_add_block_to_block (&block
, &rse
->pre
);
8794 gfc_add_modify (&block
, lse
->expr
,
8795 fold_convert (TREE_TYPE (lse
->expr
), rse
->expr
));
8798 gfc_add_block_to_block (&block
, &lse
->post
);
8799 gfc_add_block_to_block (&block
, &rse
->post
);
8801 return gfc_finish_block (&block
);
8805 /* There are quite a lot of restrictions on the optimisation in using an
8806 array function assign without a temporary. */
8809 arrayfunc_assign_needs_temporary (gfc_expr
* expr1
, gfc_expr
* expr2
)
8812 bool seen_array_ref
;
8814 gfc_symbol
*sym
= expr1
->symtree
->n
.sym
;
8816 /* Play it safe with class functions assigned to a derived type. */
8817 if (gfc_is_alloc_class_array_function (expr2
)
8818 && expr1
->ts
.type
== BT_DERIVED
)
8821 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
8822 if (expr2
->value
.function
.isym
&& !gfc_is_intrinsic_libcall (expr2
))
8825 /* Elemental functions are scalarized so that they don't need a
8826 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
8827 they would need special treatment in gfc_trans_arrayfunc_assign. */
8828 if (expr2
->value
.function
.esym
!= NULL
8829 && expr2
->value
.function
.esym
->attr
.elemental
)
8832 /* Need a temporary if rhs is not FULL or a contiguous section. */
8833 if (expr1
->ref
&& !(gfc_full_array_ref_p (expr1
->ref
, &c
) || c
))
8836 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
8837 if (gfc_ref_needs_temporary_p (expr1
->ref
))
8840 /* Functions returning pointers or allocatables need temporaries. */
8841 c
= expr2
->value
.function
.esym
8842 ? (expr2
->value
.function
.esym
->attr
.pointer
8843 || expr2
->value
.function
.esym
->attr
.allocatable
)
8844 : (expr2
->symtree
->n
.sym
->attr
.pointer
8845 || expr2
->symtree
->n
.sym
->attr
.allocatable
);
8849 /* Character array functions need temporaries unless the
8850 character lengths are the same. */
8851 if (expr2
->ts
.type
== BT_CHARACTER
&& expr2
->rank
> 0)
8853 if (expr1
->ts
.u
.cl
->length
== NULL
8854 || expr1
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
8857 if (expr2
->ts
.u
.cl
->length
== NULL
8858 || expr2
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
8861 if (mpz_cmp (expr1
->ts
.u
.cl
->length
->value
.integer
,
8862 expr2
->ts
.u
.cl
->length
->value
.integer
) != 0)
8866 /* Check that no LHS component references appear during an array
8867 reference. This is needed because we do not have the means to
8868 span any arbitrary stride with an array descriptor. This check
8869 is not needed for the rhs because the function result has to be
8871 seen_array_ref
= false;
8872 for (ref
= expr1
->ref
; ref
; ref
= ref
->next
)
8874 if (ref
->type
== REF_ARRAY
)
8875 seen_array_ref
= true;
8876 else if (ref
->type
== REF_COMPONENT
&& seen_array_ref
)
8880 /* Check for a dependency. */
8881 if (gfc_check_fncall_dependency (expr1
, INTENT_OUT
,
8882 expr2
->value
.function
.esym
,
8883 expr2
->value
.function
.actual
,
8887 /* If we have reached here with an intrinsic function, we do not
8888 need a temporary except in the particular case that reallocation
8889 on assignment is active and the lhs is allocatable and a target. */
8890 if (expr2
->value
.function
.isym
)
8891 return (flag_realloc_lhs
&& sym
->attr
.allocatable
&& sym
->attr
.target
);
8893 /* If the LHS is a dummy, we need a temporary if it is not
8895 if (sym
->attr
.dummy
&& sym
->attr
.intent
!= INTENT_OUT
)
8898 /* If the lhs has been host_associated, is in common, a pointer or is
8899 a target and the function is not using a RESULT variable, aliasing
8900 can occur and a temporary is needed. */
8901 if ((sym
->attr
.host_assoc
8902 || sym
->attr
.in_common
8903 || sym
->attr
.pointer
8904 || sym
->attr
.cray_pointee
8905 || sym
->attr
.target
)
8906 && expr2
->symtree
!= NULL
8907 && expr2
->symtree
->n
.sym
== expr2
->symtree
->n
.sym
->result
)
8910 /* A PURE function can unconditionally be called without a temporary. */
8911 if (expr2
->value
.function
.esym
!= NULL
8912 && expr2
->value
.function
.esym
->attr
.pure
)
8915 /* Implicit_pure functions are those which could legally be declared
8917 if (expr2
->value
.function
.esym
!= NULL
8918 && expr2
->value
.function
.esym
->attr
.implicit_pure
)
8921 if (!sym
->attr
.use_assoc
8922 && !sym
->attr
.in_common
8923 && !sym
->attr
.pointer
8924 && !sym
->attr
.target
8925 && !sym
->attr
.cray_pointee
8926 && expr2
->value
.function
.esym
)
8928 /* A temporary is not needed if the function is not contained and
8929 the variable is local or host associated and not a pointer or
8931 if (!expr2
->value
.function
.esym
->attr
.contained
)
8934 /* A temporary is not needed if the lhs has never been host
8935 associated and the procedure is contained. */
8936 else if (!sym
->attr
.host_assoc
)
8939 /* A temporary is not needed if the variable is local and not
8940 a pointer, a target or a result. */
8942 && expr2
->value
.function
.esym
->ns
== sym
->ns
->parent
)
8946 /* Default to temporary use. */
8951 /* Provide the loop info so that the lhs descriptor can be built for
8952 reallocatable assignments from extrinsic function calls. */
8955 realloc_lhs_loop_for_fcn_call (gfc_se
*se
, locus
*where
, gfc_ss
**ss
,
8958 /* Signal that the function call should not be made by
8959 gfc_conv_loop_setup. */
8960 se
->ss
->is_alloc_lhs
= 1;
8961 gfc_init_loopinfo (loop
);
8962 gfc_add_ss_to_loop (loop
, *ss
);
8963 gfc_add_ss_to_loop (loop
, se
->ss
);
8964 gfc_conv_ss_startstride (loop
);
8965 gfc_conv_loop_setup (loop
, where
);
8966 gfc_copy_loopinfo_to_se (se
, loop
);
8967 gfc_add_block_to_block (&se
->pre
, &loop
->pre
);
8968 gfc_add_block_to_block (&se
->pre
, &loop
->post
);
8969 se
->ss
->is_alloc_lhs
= 0;
8973 /* For assignment to a reallocatable lhs from intrinsic functions,
8974 replace the se.expr (ie. the result) with a temporary descriptor.
8975 Null the data field so that the library allocates space for the
8976 result. Free the data of the original descriptor after the function,
8977 in case it appears in an argument expression and transfer the
8978 result to the original descriptor. */
8981 fcncall_realloc_result (gfc_se
*se
, int rank
)
8990 /* Use the allocation done by the library. Substitute the lhs
8991 descriptor with a copy, whose data field is nulled.*/
8992 desc
= build_fold_indirect_ref_loc (input_location
, se
->expr
);
8993 if (POINTER_TYPE_P (TREE_TYPE (desc
)))
8994 desc
= build_fold_indirect_ref_loc (input_location
, desc
);
8996 /* Unallocated, the descriptor does not have a dtype. */
8997 tmp
= gfc_conv_descriptor_dtype (desc
);
8998 gfc_add_modify (&se
->pre
, tmp
, gfc_get_dtype (TREE_TYPE (desc
)));
9000 res_desc
= gfc_evaluate_now (desc
, &se
->pre
);
9001 gfc_conv_descriptor_data_set (&se
->pre
, res_desc
, null_pointer_node
);
9002 se
->expr
= gfc_build_addr_expr (NULL_TREE
, res_desc
);
9004 /* Free the lhs after the function call and copy the result data to
9005 the lhs descriptor. */
9006 tmp
= gfc_conv_descriptor_data_get (desc
);
9007 zero_cond
= fold_build2_loc (input_location
, EQ_EXPR
,
9008 boolean_type_node
, tmp
,
9009 build_int_cst (TREE_TYPE (tmp
), 0));
9010 zero_cond
= gfc_evaluate_now (zero_cond
, &se
->post
);
9011 tmp
= gfc_call_free (tmp
);
9012 gfc_add_expr_to_block (&se
->post
, tmp
);
9014 tmp
= gfc_conv_descriptor_data_get (res_desc
);
9015 gfc_conv_descriptor_data_set (&se
->post
, desc
, tmp
);
9017 /* Check that the shapes are the same between lhs and expression. */
9018 for (n
= 0 ; n
< rank
; n
++)
9021 tmp
= gfc_conv_descriptor_lbound_get (desc
, gfc_rank_cst
[n
]);
9022 tmp1
= gfc_conv_descriptor_lbound_get (res_desc
, gfc_rank_cst
[n
]);
9023 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
9024 gfc_array_index_type
, tmp
, tmp1
);
9025 tmp1
= gfc_conv_descriptor_ubound_get (desc
, gfc_rank_cst
[n
]);
9026 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
9027 gfc_array_index_type
, tmp
, tmp1
);
9028 tmp1
= gfc_conv_descriptor_ubound_get (res_desc
, gfc_rank_cst
[n
]);
9029 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
9030 gfc_array_index_type
, tmp
, tmp1
);
9031 tmp
= fold_build2_loc (input_location
, NE_EXPR
,
9032 boolean_type_node
, tmp
,
9033 gfc_index_zero_node
);
9034 tmp
= gfc_evaluate_now (tmp
, &se
->post
);
9035 zero_cond
= fold_build2_loc (input_location
, TRUTH_OR_EXPR
,
9036 boolean_type_node
, tmp
,
9040 /* 'zero_cond' being true is equal to lhs not being allocated or the
9041 shapes being different. */
9042 zero_cond
= gfc_evaluate_now (zero_cond
, &se
->post
);
9044 /* Now reset the bounds returned from the function call to bounds based
9045 on the lhs lbounds, except where the lhs is not allocated or the shapes
9046 of 'variable and 'expr' are different. Set the offset accordingly. */
9047 offset
= gfc_index_zero_node
;
9048 for (n
= 0 ; n
< rank
; n
++)
9052 lbound
= gfc_conv_descriptor_lbound_get (desc
, gfc_rank_cst
[n
]);
9053 lbound
= fold_build3_loc (input_location
, COND_EXPR
,
9054 gfc_array_index_type
, zero_cond
,
9055 gfc_index_one_node
, lbound
);
9056 lbound
= gfc_evaluate_now (lbound
, &se
->post
);
9058 tmp
= gfc_conv_descriptor_ubound_get (res_desc
, gfc_rank_cst
[n
]);
9059 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
9060 gfc_array_index_type
, tmp
, lbound
);
9061 gfc_conv_descriptor_lbound_set (&se
->post
, desc
,
9062 gfc_rank_cst
[n
], lbound
);
9063 gfc_conv_descriptor_ubound_set (&se
->post
, desc
,
9064 gfc_rank_cst
[n
], tmp
);
9066 /* Set stride and accumulate the offset. */
9067 tmp
= gfc_conv_descriptor_stride_get (res_desc
, gfc_rank_cst
[n
]);
9068 gfc_conv_descriptor_stride_set (&se
->post
, desc
,
9069 gfc_rank_cst
[n
], tmp
);
9070 tmp
= fold_build2_loc (input_location
, MULT_EXPR
,
9071 gfc_array_index_type
, lbound
, tmp
);
9072 offset
= fold_build2_loc (input_location
, MINUS_EXPR
,
9073 gfc_array_index_type
, offset
, tmp
);
9074 offset
= gfc_evaluate_now (offset
, &se
->post
);
9077 gfc_conv_descriptor_offset_set (&se
->post
, desc
, offset
);
9082 /* Try to translate array(:) = func (...), where func is a transformational
9083 array function, without using a temporary. Returns NULL if this isn't the
9087 gfc_trans_arrayfunc_assign (gfc_expr
* expr1
, gfc_expr
* expr2
)
9091 gfc_component
*comp
= NULL
;
9094 if (arrayfunc_assign_needs_temporary (expr1
, expr2
))
9097 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
9099 comp
= gfc_get_proc_ptr_comp (expr2
);
9100 gcc_assert (expr2
->value
.function
.isym
9101 || (comp
&& comp
->attr
.dimension
)
9102 || (!comp
&& gfc_return_by_reference (expr2
->value
.function
.esym
)
9103 && expr2
->value
.function
.esym
->result
->attr
.dimension
));
9105 gfc_init_se (&se
, NULL
);
9106 gfc_start_block (&se
.pre
);
9107 se
.want_pointer
= 1;
9109 gfc_conv_array_parameter (&se
, expr1
, false, NULL
, NULL
, NULL
);
9111 if (expr1
->ts
.type
== BT_DERIVED
9112 && expr1
->ts
.u
.derived
->attr
.alloc_comp
)
9115 tmp
= gfc_deallocate_alloc_comp_no_caf (expr1
->ts
.u
.derived
, se
.expr
,
9117 gfc_add_expr_to_block (&se
.pre
, tmp
);
9120 se
.direct_byref
= 1;
9121 se
.ss
= gfc_walk_expr (expr2
);
9122 gcc_assert (se
.ss
!= gfc_ss_terminator
);
9124 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
9125 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
9126 Clearly, this cannot be done for an allocatable function result, since
9127 the shape of the result is unknown and, in any case, the function must
9128 correctly take care of the reallocation internally. For intrinsic
9129 calls, the array data is freed and the library takes care of allocation.
9130 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
9132 if (flag_realloc_lhs
9133 && gfc_is_reallocatable_lhs (expr1
)
9134 && !gfc_expr_attr (expr1
).codimension
9135 && !gfc_is_coindexed (expr1
)
9136 && !(expr2
->value
.function
.esym
9137 && expr2
->value
.function
.esym
->result
->attr
.allocatable
))
9139 realloc_lhs_warning (expr1
->ts
.type
, true, &expr1
->where
);
9141 if (!expr2
->value
.function
.isym
)
9143 ss
= gfc_walk_expr (expr1
);
9144 gcc_assert (ss
!= gfc_ss_terminator
);
9146 realloc_lhs_loop_for_fcn_call (&se
, &expr1
->where
, &ss
, &loop
);
9147 ss
->is_alloc_lhs
= 1;
9150 fcncall_realloc_result (&se
, expr1
->rank
);
9153 gfc_conv_function_expr (&se
, expr2
);
9154 gfc_add_block_to_block (&se
.pre
, &se
.post
);
9157 gfc_cleanup_loop (&loop
);
9159 gfc_free_ss_chain (se
.ss
);
9161 return gfc_finish_block (&se
.pre
);
9165 /* Try to efficiently translate array(:) = 0. Return NULL if this
9169 gfc_trans_zero_assign (gfc_expr
* expr
)
9171 tree dest
, len
, type
;
9175 sym
= expr
->symtree
->n
.sym
;
9176 dest
= gfc_get_symbol_decl (sym
);
9178 type
= TREE_TYPE (dest
);
9179 if (POINTER_TYPE_P (type
))
9180 type
= TREE_TYPE (type
);
9181 if (!GFC_ARRAY_TYPE_P (type
))
9184 /* Determine the length of the array. */
9185 len
= GFC_TYPE_ARRAY_SIZE (type
);
9186 if (!len
|| TREE_CODE (len
) != INTEGER_CST
)
9189 tmp
= TYPE_SIZE_UNIT (gfc_get_element_type (type
));
9190 len
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
, len
,
9191 fold_convert (gfc_array_index_type
, tmp
));
9193 /* If we are zeroing a local array avoid taking its address by emitting
9195 if (!POINTER_TYPE_P (TREE_TYPE (dest
)))
9196 return build2_loc (input_location
, MODIFY_EXPR
, void_type_node
,
9197 dest
, build_constructor (TREE_TYPE (dest
),
9200 /* Convert arguments to the correct types. */
9201 dest
= fold_convert (pvoid_type_node
, dest
);
9202 len
= fold_convert (size_type_node
, len
);
9204 /* Construct call to __builtin_memset. */
9205 tmp
= build_call_expr_loc (input_location
,
9206 builtin_decl_explicit (BUILT_IN_MEMSET
),
9207 3, dest
, integer_zero_node
, len
);
9208 return fold_convert (void_type_node
, tmp
);
9212 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
9213 that constructs the call to __builtin_memcpy. */
9216 gfc_build_memcpy_call (tree dst
, tree src
, tree len
)
9220 /* Convert arguments to the correct types. */
9221 if (!POINTER_TYPE_P (TREE_TYPE (dst
)))
9222 dst
= gfc_build_addr_expr (pvoid_type_node
, dst
);
9224 dst
= fold_convert (pvoid_type_node
, dst
);
9226 if (!POINTER_TYPE_P (TREE_TYPE (src
)))
9227 src
= gfc_build_addr_expr (pvoid_type_node
, src
);
9229 src
= fold_convert (pvoid_type_node
, src
);
9231 len
= fold_convert (size_type_node
, len
);
9233 /* Construct call to __builtin_memcpy. */
9234 tmp
= build_call_expr_loc (input_location
,
9235 builtin_decl_explicit (BUILT_IN_MEMCPY
),
9237 return fold_convert (void_type_node
, tmp
);
9241 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
9242 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
9243 source/rhs, both are gfc_full_array_ref_p which have been checked for
9247 gfc_trans_array_copy (gfc_expr
* expr1
, gfc_expr
* expr2
)
9249 tree dst
, dlen
, dtype
;
9250 tree src
, slen
, stype
;
9253 dst
= gfc_get_symbol_decl (expr1
->symtree
->n
.sym
);
9254 src
= gfc_get_symbol_decl (expr2
->symtree
->n
.sym
);
9256 dtype
= TREE_TYPE (dst
);
9257 if (POINTER_TYPE_P (dtype
))
9258 dtype
= TREE_TYPE (dtype
);
9259 stype
= TREE_TYPE (src
);
9260 if (POINTER_TYPE_P (stype
))
9261 stype
= TREE_TYPE (stype
);
9263 if (!GFC_ARRAY_TYPE_P (dtype
) || !GFC_ARRAY_TYPE_P (stype
))
9266 /* Determine the lengths of the arrays. */
9267 dlen
= GFC_TYPE_ARRAY_SIZE (dtype
);
9268 if (!dlen
|| TREE_CODE (dlen
) != INTEGER_CST
)
9270 tmp
= TYPE_SIZE_UNIT (gfc_get_element_type (dtype
));
9271 dlen
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
9272 dlen
, fold_convert (gfc_array_index_type
, tmp
));
9274 slen
= GFC_TYPE_ARRAY_SIZE (stype
);
9275 if (!slen
|| TREE_CODE (slen
) != INTEGER_CST
)
9277 tmp
= TYPE_SIZE_UNIT (gfc_get_element_type (stype
));
9278 slen
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
,
9279 slen
, fold_convert (gfc_array_index_type
, tmp
));
9281 /* Sanity check that they are the same. This should always be
9282 the case, as we should already have checked for conformance. */
9283 if (!tree_int_cst_equal (slen
, dlen
))
9286 return gfc_build_memcpy_call (dst
, src
, dlen
);
9290 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
9291 this can't be done. EXPR1 is the destination/lhs for which
9292 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
9295 gfc_trans_array_constructor_copy (gfc_expr
* expr1
, gfc_expr
* expr2
)
9297 unsigned HOST_WIDE_INT nelem
;
9303 nelem
= gfc_constant_array_constructor_p (expr2
->value
.constructor
);
9307 dst
= gfc_get_symbol_decl (expr1
->symtree
->n
.sym
);
9308 dtype
= TREE_TYPE (dst
);
9309 if (POINTER_TYPE_P (dtype
))
9310 dtype
= TREE_TYPE (dtype
);
9311 if (!GFC_ARRAY_TYPE_P (dtype
))
9314 /* Determine the lengths of the array. */
9315 len
= GFC_TYPE_ARRAY_SIZE (dtype
);
9316 if (!len
|| TREE_CODE (len
) != INTEGER_CST
)
9319 /* Confirm that the constructor is the same size. */
9320 if (compare_tree_int (len
, nelem
) != 0)
9323 tmp
= TYPE_SIZE_UNIT (gfc_get_element_type (dtype
));
9324 len
= fold_build2_loc (input_location
, MULT_EXPR
, gfc_array_index_type
, len
,
9325 fold_convert (gfc_array_index_type
, tmp
));
9327 stype
= gfc_typenode_for_spec (&expr2
->ts
);
9328 src
= gfc_build_constant_array_constructor (expr2
, stype
);
9330 stype
= TREE_TYPE (src
);
9331 if (POINTER_TYPE_P (stype
))
9332 stype
= TREE_TYPE (stype
);
9334 return gfc_build_memcpy_call (dst
, src
, len
);
9338 /* Tells whether the expression is to be treated as a variable reference. */
9341 gfc_expr_is_variable (gfc_expr
*expr
)
9344 gfc_component
*comp
;
9345 gfc_symbol
*func_ifc
;
9347 if (expr
->expr_type
== EXPR_VARIABLE
)
9350 arg
= gfc_get_noncopying_intrinsic_argument (expr
);
9353 gcc_assert (expr
->value
.function
.isym
->id
== GFC_ISYM_TRANSPOSE
);
9354 return gfc_expr_is_variable (arg
);
9357 /* A data-pointer-returning function should be considered as a variable
9359 if (expr
->expr_type
== EXPR_FUNCTION
9360 && expr
->ref
== NULL
)
9362 if (expr
->value
.function
.isym
!= NULL
)
9365 if (expr
->value
.function
.esym
!= NULL
)
9367 func_ifc
= expr
->value
.function
.esym
;
9372 gcc_assert (expr
->symtree
);
9373 func_ifc
= expr
->symtree
->n
.sym
;
9380 comp
= gfc_get_proc_ptr_comp (expr
);
9381 if ((expr
->expr_type
== EXPR_PPC
|| expr
->expr_type
== EXPR_FUNCTION
)
9384 func_ifc
= comp
->ts
.interface
;
9388 if (expr
->expr_type
== EXPR_COMPCALL
)
9390 gcc_assert (!expr
->value
.compcall
.tbp
->is_generic
);
9391 func_ifc
= expr
->value
.compcall
.tbp
->u
.specific
->n
.sym
;
9398 gcc_assert (func_ifc
->attr
.function
9399 && func_ifc
->result
!= NULL
);
9400 return func_ifc
->result
->attr
.pointer
;
9404 /* Is the lhs OK for automatic reallocation? */
9407 is_scalar_reallocatable_lhs (gfc_expr
*expr
)
9411 /* An allocatable variable with no reference. */
9412 if (expr
->symtree
->n
.sym
->attr
.allocatable
9416 /* All that can be left are allocatable components. However, we do
9417 not check for allocatable components here because the expression
9418 could be an allocatable component of a pointer component. */
9419 if (expr
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
9420 && expr
->symtree
->n
.sym
->ts
.type
!= BT_CLASS
)
9423 /* Find an allocatable component ref last. */
9424 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
9425 if (ref
->type
== REF_COMPONENT
9427 && ref
->u
.c
.component
->attr
.allocatable
)
9434 /* Allocate or reallocate scalar lhs, as necessary. */
9437 alloc_scalar_allocatable_for_assignment (stmtblock_t
*block
,
9452 if (!expr1
|| expr1
->rank
)
9455 if (!expr2
|| expr2
->rank
)
9458 for (ref
= expr1
->ref
; ref
; ref
= ref
->next
)
9459 if (ref
->type
== REF_SUBSTRING
)
9462 realloc_lhs_warning (expr2
->ts
.type
, false, &expr2
->where
);
9464 /* Since this is a scalar lhs, we can afford to do this. That is,
9465 there is no risk of side effects being repeated. */
9466 gfc_init_se (&lse
, NULL
);
9467 lse
.want_pointer
= 1;
9468 gfc_conv_expr (&lse
, expr1
);
9470 jump_label1
= gfc_build_label_decl (NULL_TREE
);
9471 jump_label2
= gfc_build_label_decl (NULL_TREE
);
9473 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
9474 tmp
= build_int_cst (TREE_TYPE (lse
.expr
), 0);
9475 cond
= fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
9477 tmp
= build3_v (COND_EXPR
, cond
,
9478 build1_v (GOTO_EXPR
, jump_label1
),
9479 build_empty_stmt (input_location
));
9480 gfc_add_expr_to_block (block
, tmp
);
9482 if (expr1
->ts
.type
== BT_CHARACTER
&& expr1
->ts
.deferred
)
9484 /* Use the rhs string length and the lhs element size. */
9485 size
= string_length
;
9486 tmp
= TREE_TYPE (gfc_typenode_for_spec (&expr1
->ts
));
9487 tmp
= TYPE_SIZE_UNIT (tmp
);
9488 size_in_bytes
= fold_build2_loc (input_location
, MULT_EXPR
,
9489 TREE_TYPE (tmp
), tmp
,
9490 fold_convert (TREE_TYPE (tmp
), size
));
9494 /* Otherwise use the length in bytes of the rhs. */
9495 size
= TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1
->ts
));
9496 size_in_bytes
= size
;
9499 size_in_bytes
= fold_build2_loc (input_location
, MAX_EXPR
, size_type_node
,
9500 size_in_bytes
, size_one_node
);
9502 if (gfc_caf_attr (expr1
).codimension
&& flag_coarray
== GFC_FCOARRAY_LIB
)
9504 tree caf_decl
, token
;
9506 symbol_attribute attr
;
9508 gfc_clear_attr (&attr
);
9509 gfc_init_se (&caf_se
, NULL
);
9511 caf_decl
= gfc_get_tree_for_caf_expr (expr1
);
9512 gfc_get_caf_token_offset (&caf_se
, &token
, NULL
, caf_decl
, NULL_TREE
,
9514 gfc_add_block_to_block (block
, &caf_se
.pre
);
9515 gfc_allocate_allocatable (block
, lse
.expr
, size_in_bytes
,
9516 gfc_build_addr_expr (NULL_TREE
, token
),
9517 NULL_TREE
, NULL_TREE
, NULL_TREE
, jump_label1
,
9520 else if (expr1
->ts
.type
== BT_DERIVED
&& expr1
->ts
.u
.derived
->attr
.alloc_comp
)
9522 tmp
= build_call_expr_loc (input_location
,
9523 builtin_decl_explicit (BUILT_IN_CALLOC
),
9524 2, build_one_cst (size_type_node
),
9526 tmp
= fold_convert (TREE_TYPE (lse
.expr
), tmp
);
9527 gfc_add_modify (block
, lse
.expr
, tmp
);
9531 tmp
= build_call_expr_loc (input_location
,
9532 builtin_decl_explicit (BUILT_IN_MALLOC
),
9534 tmp
= fold_convert (TREE_TYPE (lse
.expr
), tmp
);
9535 gfc_add_modify (block
, lse
.expr
, tmp
);
9538 if (expr1
->ts
.type
== BT_CHARACTER
&& expr1
->ts
.deferred
)
9540 /* Deferred characters need checking for lhs and rhs string
9541 length. Other deferred parameter variables will have to
9543 tmp
= build1_v (GOTO_EXPR
, jump_label2
);
9544 gfc_add_expr_to_block (block
, tmp
);
9546 tmp
= build1_v (LABEL_EXPR
, jump_label1
);
9547 gfc_add_expr_to_block (block
, tmp
);
9549 /* For a deferred length character, reallocate if lengths of lhs and
9550 rhs are different. */
9551 if (expr1
->ts
.type
== BT_CHARACTER
&& expr1
->ts
.deferred
)
9553 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
9554 lse
.string_length
, size
);
9555 /* Jump past the realloc if the lengths are the same. */
9556 tmp
= build3_v (COND_EXPR
, cond
,
9557 build1_v (GOTO_EXPR
, jump_label2
),
9558 build_empty_stmt (input_location
));
9559 gfc_add_expr_to_block (block
, tmp
);
9560 tmp
= build_call_expr_loc (input_location
,
9561 builtin_decl_explicit (BUILT_IN_REALLOC
),
9562 2, fold_convert (pvoid_type_node
, lse
.expr
),
9564 tmp
= fold_convert (TREE_TYPE (lse
.expr
), tmp
);
9565 gfc_add_modify (block
, lse
.expr
, tmp
);
9566 tmp
= build1_v (LABEL_EXPR
, jump_label2
);
9567 gfc_add_expr_to_block (block
, tmp
);
9569 /* Update the lhs character length. */
9570 size
= string_length
;
9571 gfc_add_modify (block
, lse
.string_length
, size
);
9575 /* Check for assignments of the type
9579 to make sure we do not check for reallocation unneccessarily. */
9583 is_runtime_conformable (gfc_expr
*expr1
, gfc_expr
*expr2
)
9585 gfc_actual_arglist
*a
;
9588 switch (expr2
->expr_type
)
9591 return gfc_dep_compare_expr (expr1
, expr2
) == 0;
9594 if (expr2
->value
.function
.esym
9595 && expr2
->value
.function
.esym
->attr
.elemental
)
9597 for (a
= expr2
->value
.function
.actual
; a
!= NULL
; a
= a
->next
)
9600 if (e1
&& e1
->rank
> 0 && !is_runtime_conformable (expr1
, e1
))
9605 else if (expr2
->value
.function
.isym
9606 && expr2
->value
.function
.isym
->elemental
)
9608 for (a
= expr2
->value
.function
.actual
; a
!= NULL
; a
= a
->next
)
9611 if (e1
&& e1
->rank
> 0 && !is_runtime_conformable (expr1
, e1
))
9620 switch (expr2
->value
.op
.op
)
9623 case INTRINSIC_UPLUS
:
9624 case INTRINSIC_UMINUS
:
9625 case INTRINSIC_PARENTHESES
:
9626 return is_runtime_conformable (expr1
, expr2
->value
.op
.op1
);
9628 case INTRINSIC_PLUS
:
9629 case INTRINSIC_MINUS
:
9630 case INTRINSIC_TIMES
:
9631 case INTRINSIC_DIVIDE
:
9632 case INTRINSIC_POWER
:
9636 case INTRINSIC_NEQV
:
9643 case INTRINSIC_EQ_OS
:
9644 case INTRINSIC_NE_OS
:
9645 case INTRINSIC_GT_OS
:
9646 case INTRINSIC_GE_OS
:
9647 case INTRINSIC_LT_OS
:
9648 case INTRINSIC_LE_OS
:
9650 e1
= expr2
->value
.op
.op1
;
9651 e2
= expr2
->value
.op
.op2
;
9653 if (e1
->rank
== 0 && e2
->rank
> 0)
9654 return is_runtime_conformable (expr1
, e2
);
9655 else if (e1
->rank
> 0 && e2
->rank
== 0)
9656 return is_runtime_conformable (expr1
, e1
);
9657 else if (e1
->rank
> 0 && e2
->rank
> 0)
9658 return is_runtime_conformable (expr1
, e1
)
9659 && is_runtime_conformable (expr1
, e2
);
9677 trans_class_assignment (stmtblock_t
*block
, gfc_expr
*lhs
, gfc_expr
*rhs
,
9678 gfc_se
*lse
, gfc_se
*rse
, bool use_vptr_copy
,
9681 tree tmp
, fcn
, stdcopy
, to_len
, from_len
, vptr
;
9682 vec
<tree
, va_gc
> *args
= NULL
;
9684 vptr
= trans_class_vptr_len_assignment (block
, lhs
, rhs
, rse
, &to_len
,
9687 /* Generate allocation of the lhs. */
9693 tmp
= gfc_vptr_size_get (vptr
);
9694 class_han
= GFC_CLASS_TYPE_P (TREE_TYPE (lse
->expr
))
9695 ? gfc_class_data_get (lse
->expr
) : lse
->expr
;
9696 gfc_init_block (&alloc
);
9697 gfc_allocate_using_malloc (&alloc
, class_han
, tmp
, NULL_TREE
);
9698 tmp
= fold_build2_loc (input_location
, EQ_EXPR
,
9699 boolean_type_node
, class_han
,
9700 build_int_cst (prvoid_type_node
, 0));
9701 tmp
= fold_build3_loc (input_location
, COND_EXPR
, void_type_node
,
9703 PRED_FORTRAN_FAIL_ALLOC
),
9704 gfc_finish_block (&alloc
),
9705 build_empty_stmt (input_location
));
9706 gfc_add_expr_to_block (&lse
->pre
, tmp
);
9709 fcn
= gfc_vptr_copy_get (vptr
);
9711 tmp
= GFC_CLASS_TYPE_P (TREE_TYPE (rse
->expr
))
9712 ? gfc_class_data_get (rse
->expr
) : rse
->expr
;
9715 if (!POINTER_TYPE_P (TREE_TYPE (tmp
))
9716 || INDIRECT_REF_P (tmp
)
9717 || (rhs
->ts
.type
== BT_DERIVED
9718 && rhs
->ts
.u
.derived
->attr
.unlimited_polymorphic
9719 && !rhs
->ts
.u
.derived
->attr
.pointer
9720 && !rhs
->ts
.u
.derived
->attr
.allocatable
)
9721 || (UNLIMITED_POLY (rhs
)
9722 && !CLASS_DATA (rhs
)->attr
.pointer
9723 && !CLASS_DATA (rhs
)->attr
.allocatable
))
9724 vec_safe_push (args
, gfc_build_addr_expr (NULL_TREE
, tmp
));
9726 vec_safe_push (args
, tmp
);
9727 tmp
= GFC_CLASS_TYPE_P (TREE_TYPE (lse
->expr
))
9728 ? gfc_class_data_get (lse
->expr
) : lse
->expr
;
9729 if (!POINTER_TYPE_P (TREE_TYPE (tmp
))
9730 || INDIRECT_REF_P (tmp
)
9731 || (lhs
->ts
.type
== BT_DERIVED
9732 && lhs
->ts
.u
.derived
->attr
.unlimited_polymorphic
9733 && !lhs
->ts
.u
.derived
->attr
.pointer
9734 && !lhs
->ts
.u
.derived
->attr
.allocatable
)
9735 || (UNLIMITED_POLY (lhs
)
9736 && !CLASS_DATA (lhs
)->attr
.pointer
9737 && !CLASS_DATA (lhs
)->attr
.allocatable
))
9738 vec_safe_push (args
, gfc_build_addr_expr (NULL_TREE
, tmp
));
9740 vec_safe_push (args
, tmp
);
9742 stdcopy
= build_call_vec (TREE_TYPE (TREE_TYPE (fcn
)), fcn
, args
);
9744 if (to_len
!= NULL_TREE
&& !integer_zerop (from_len
))
9747 vec_safe_push (args
, from_len
);
9748 vec_safe_push (args
, to_len
);
9749 extcopy
= build_call_vec (TREE_TYPE (TREE_TYPE (fcn
)), fcn
, args
);
9751 tmp
= fold_build2_loc (input_location
, GT_EXPR
,
9752 boolean_type_node
, from_len
,
9754 return fold_build3_loc (input_location
, COND_EXPR
,
9755 void_type_node
, tmp
,
9763 tree rhst
= GFC_CLASS_TYPE_P (TREE_TYPE (lse
->expr
))
9764 ? gfc_class_data_get (lse
->expr
) : lse
->expr
;
9766 gfc_init_block (&tblock
);
9767 if (!POINTER_TYPE_P (TREE_TYPE (tmp
)))
9768 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
9769 if (!POINTER_TYPE_P (TREE_TYPE (rhst
)))
9770 rhst
= gfc_build_addr_expr (NULL_TREE
, rhst
);
9771 /* When coming from a ptr_copy lhs and rhs are swapped. */
9772 gfc_add_modify_loc (input_location
, &tblock
, rhst
,
9773 fold_convert (TREE_TYPE (rhst
), tmp
));
9774 return gfc_finish_block (&tblock
);
9778 /* Subroutine of gfc_trans_assignment that actually scalarizes the
9779 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
9780 init_flag indicates initialization expressions and dealloc that no
9781 deallocate prior assignment is needed (if in doubt, set true).
9782 When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
9783 routine instead of a pointer assignment. Alias resolution is only done,
9784 when MAY_ALIAS is set (the default). This flag is used by ALLOCATE()
9785 where it is known, that newly allocated memory on the lhs can never be
9786 an alias of the rhs. */
9789 gfc_trans_assignment_1 (gfc_expr
* expr1
, gfc_expr
* expr2
, bool init_flag
,
9790 bool dealloc
, bool use_vptr_copy
, bool may_alias
)
9795 gfc_ss
*lss_section
;
9802 bool scalar_to_array
;
9805 bool maybe_workshare
= false, lhs_refs_comp
= false, rhs_refs_comp
= false;
9806 symbol_attribute lhs_caf_attr
, rhs_caf_attr
, lhs_attr
;
9807 bool is_poly_assign
;
9809 /* Assignment of the form lhs = rhs. */
9810 gfc_start_block (&block
);
9812 gfc_init_se (&lse
, NULL
);
9813 gfc_init_se (&rse
, NULL
);
9816 lss
= gfc_walk_expr (expr1
);
9817 if (gfc_is_reallocatable_lhs (expr1
)
9818 && !(expr2
->expr_type
== EXPR_FUNCTION
9819 && expr2
->value
.function
.isym
!= NULL
))
9820 lss
->is_alloc_lhs
= 1;
9823 if ((expr1
->ts
.type
== BT_DERIVED
)
9824 && (gfc_is_alloc_class_array_function (expr2
)
9825 || gfc_is_alloc_class_scalar_function (expr2
)))
9826 expr2
->must_finalize
= 1;
9828 /* Checking whether a class assignment is desired is quite complicated and
9829 needed at two locations, so do it once only before the information is
9831 lhs_attr
= gfc_expr_attr (expr1
);
9832 is_poly_assign
= (use_vptr_copy
|| lhs_attr
.pointer
9833 || (lhs_attr
.allocatable
&& !lhs_attr
.dimension
))
9834 && (expr1
->ts
.type
== BT_CLASS
9835 || gfc_is_class_array_ref (expr1
, NULL
)
9836 || gfc_is_class_scalar_expr (expr1
)
9837 || gfc_is_class_array_ref (expr2
, NULL
)
9838 || gfc_is_class_scalar_expr (expr2
));
9841 /* Only analyze the expressions for coarray properties, when in coarray-lib
9843 if (flag_coarray
== GFC_FCOARRAY_LIB
)
9845 lhs_caf_attr
= gfc_caf_attr (expr1
, false, &lhs_refs_comp
);
9846 rhs_caf_attr
= gfc_caf_attr (expr2
, false, &rhs_refs_comp
);
9849 if (lss
!= gfc_ss_terminator
)
9851 /* The assignment needs scalarization. */
9854 /* Find a non-scalar SS from the lhs. */
9855 while (lss_section
!= gfc_ss_terminator
9856 && lss_section
->info
->type
!= GFC_SS_SECTION
)
9857 lss_section
= lss_section
->next
;
9859 gcc_assert (lss_section
!= gfc_ss_terminator
);
9861 /* Initialize the scalarizer. */
9862 gfc_init_loopinfo (&loop
);
9865 rss
= gfc_walk_expr (expr2
);
9866 if (rss
== gfc_ss_terminator
)
9867 /* The rhs is scalar. Add a ss for the expression. */
9868 rss
= gfc_get_scalar_ss (gfc_ss_terminator
, expr2
);
9869 /* When doing a class assign, then the handle to the rhs needs to be a
9870 pointer to allow for polymorphism. */
9871 if (is_poly_assign
&& expr2
->rank
== 0 && !UNLIMITED_POLY (expr2
))
9872 rss
->info
->type
= GFC_SS_REFERENCE
;
9874 /* Associate the SS with the loop. */
9875 gfc_add_ss_to_loop (&loop
, lss
);
9876 gfc_add_ss_to_loop (&loop
, rss
);
9878 /* Calculate the bounds of the scalarization. */
9879 gfc_conv_ss_startstride (&loop
);
9880 /* Enable loop reversal. */
9881 for (n
= 0; n
< GFC_MAX_DIMENSIONS
; n
++)
9882 loop
.reverse
[n
] = GFC_ENABLE_REVERSE
;
9883 /* Resolve any data dependencies in the statement. */
9885 gfc_conv_resolve_dependencies (&loop
, lss
, rss
);
9886 /* Setup the scalarizing loops. */
9887 gfc_conv_loop_setup (&loop
, &expr2
->where
);
9889 /* Setup the gfc_se structures. */
9890 gfc_copy_loopinfo_to_se (&lse
, &loop
);
9891 gfc_copy_loopinfo_to_se (&rse
, &loop
);
9894 gfc_mark_ss_chain_used (rss
, 1);
9895 if (loop
.temp_ss
== NULL
)
9898 gfc_mark_ss_chain_used (lss
, 1);
9902 lse
.ss
= loop
.temp_ss
;
9903 gfc_mark_ss_chain_used (lss
, 3);
9904 gfc_mark_ss_chain_used (loop
.temp_ss
, 3);
9907 /* Allow the scalarizer to workshare array assignments. */
9908 if ((ompws_flags
& (OMPWS_WORKSHARE_FLAG
| OMPWS_SCALARIZER_BODY
))
9909 == OMPWS_WORKSHARE_FLAG
9910 && loop
.temp_ss
== NULL
)
9912 maybe_workshare
= true;
9913 ompws_flags
|= OMPWS_SCALARIZER_WS
| OMPWS_SCALARIZER_BODY
;
9916 /* Start the scalarized loop body. */
9917 gfc_start_scalarized_body (&loop
, &body
);
9920 gfc_init_block (&body
);
9922 l_is_temp
= (lss
!= gfc_ss_terminator
&& loop
.temp_ss
!= NULL
);
9924 /* Translate the expression. */
9925 rse
.want_coarray
= flag_coarray
== GFC_FCOARRAY_LIB
&& init_flag
9926 && lhs_caf_attr
.codimension
;
9927 gfc_conv_expr (&rse
, expr2
);
9929 /* Deal with the case of a scalar class function assigned to a derived type. */
9930 if (gfc_is_alloc_class_scalar_function (expr2
)
9931 && expr1
->ts
.type
== BT_DERIVED
)
9933 rse
.expr
= gfc_class_data_get (rse
.expr
);
9934 rse
.expr
= build_fold_indirect_ref_loc (input_location
, rse
.expr
);
9937 /* Stabilize a string length for temporaries. */
9938 if (expr2
->ts
.type
== BT_CHARACTER
&& !expr1
->ts
.deferred
9939 && !(VAR_P (rse
.string_length
)
9940 || TREE_CODE (rse
.string_length
) == PARM_DECL
9941 || TREE_CODE (rse
.string_length
) == INDIRECT_REF
))
9942 string_length
= gfc_evaluate_now (rse
.string_length
, &rse
.pre
);
9943 else if (expr2
->ts
.type
== BT_CHARACTER
)
9944 string_length
= rse
.string_length
;
9946 string_length
= NULL_TREE
;
9950 gfc_conv_tmp_array_ref (&lse
);
9951 if (expr2
->ts
.type
== BT_CHARACTER
)
9952 lse
.string_length
= string_length
;
9956 gfc_conv_expr (&lse
, expr1
);
9957 if (gfc_option
.rtcheck
& GFC_RTCHECK_MEM
9959 && gfc_expr_attr (expr1
).allocatable
9966 tmp
= INDIRECT_REF_P (lse
.expr
)
9967 ? gfc_build_addr_expr (NULL_TREE
, lse
.expr
) : lse
.expr
;
9969 /* We should only get array references here. */
9970 gcc_assert (TREE_CODE (tmp
) == POINTER_PLUS_EXPR
9971 || TREE_CODE (tmp
) == ARRAY_REF
);
9973 /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
9974 or the array itself(ARRAY_REF). */
9975 tmp
= TREE_OPERAND (tmp
, 0);
9977 /* Provide the address of the array. */
9978 if (TREE_CODE (lse
.expr
) == ARRAY_REF
)
9979 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
9981 cond
= fold_build2_loc (input_location
, EQ_EXPR
, boolean_type_node
,
9982 tmp
, build_int_cst (TREE_TYPE (tmp
), 0));
9983 msg
= _("Assignment of scalar to unallocated array");
9984 gfc_trans_runtime_check (true, false, cond
, &loop
.pre
,
9985 &expr1
->where
, msg
);
9989 /* Assignments of scalar derived types with allocatable components
9990 to arrays must be done with a deep copy and the rhs temporary
9991 must have its components deallocated afterwards. */
9992 scalar_to_array
= (expr2
->ts
.type
== BT_DERIVED
9993 && expr2
->ts
.u
.derived
->attr
.alloc_comp
9994 && !gfc_expr_is_variable (expr2
)
9995 && expr1
->rank
&& !expr2
->rank
);
9996 scalar_to_array
|= (expr1
->ts
.type
== BT_DERIVED
9998 && expr1
->ts
.u
.derived
->attr
.alloc_comp
9999 && gfc_is_alloc_class_scalar_function (expr2
));
10000 if (scalar_to_array
&& dealloc
)
10002 tmp
= gfc_deallocate_alloc_comp_no_caf (expr2
->ts
.u
.derived
, rse
.expr
, 0);
10003 gfc_prepend_expr_to_block (&loop
.post
, tmp
);
10006 /* When assigning a character function result to a deferred-length variable,
10007 the function call must happen before the (re)allocation of the lhs -
10008 otherwise the character length of the result is not known.
10009 NOTE: This relies on having the exact dependence of the length type
10010 parameter available to the caller; gfortran saves it in the .mod files.
10011 NOTE ALSO: The concatenation operation generates a temporary pointer,
10012 whose allocation must go to the innermost loop. */
10013 if (flag_realloc_lhs
10014 && expr2
->ts
.type
== BT_CHARACTER
&& expr1
->ts
.deferred
10015 && !(lss
!= gfc_ss_terminator
10016 && expr2
->expr_type
== EXPR_OP
10017 && expr2
->value
.op
.op
== INTRINSIC_CONCAT
))
10018 gfc_add_block_to_block (&block
, &rse
.pre
);
10020 /* Nullify the allocatable components corresponding to those of the lhs
10021 derived type, so that the finalization of the function result does not
10022 affect the lhs of the assignment. Prepend is used to ensure that the
10023 nullification occurs before the call to the finalizer. In the case of
10024 a scalar to array assignment, this is done in gfc_trans_scalar_assign
10025 as part of the deep copy. */
10026 if (!scalar_to_array
&& expr1
->ts
.type
== BT_DERIVED
10027 && (gfc_is_alloc_class_array_function (expr2
)
10028 || gfc_is_alloc_class_scalar_function (expr2
)))
10031 tmp
= gfc_nullify_alloc_comp (expr1
->ts
.u
.derived
, rse
.expr
, 0);
10032 gfc_prepend_expr_to_block (&rse
.post
, tmp
);
10033 if (lss
!= gfc_ss_terminator
&& rss
== gfc_ss_terminator
)
10034 gfc_add_block_to_block (&loop
.post
, &rse
.post
);
10037 if (is_poly_assign
)
10038 tmp
= trans_class_assignment (&body
, expr1
, expr2
, &lse
, &rse
,
10039 use_vptr_copy
|| (lhs_attr
.allocatable
10040 && !lhs_attr
.dimension
),
10041 flag_realloc_lhs
&& !lhs_attr
.pointer
);
10042 else if (flag_coarray
== GFC_FCOARRAY_LIB
10043 && lhs_caf_attr
.codimension
&& rhs_caf_attr
.codimension
10044 && ((lhs_caf_attr
.allocatable
&& lhs_refs_comp
)
10045 || (rhs_caf_attr
.allocatable
&& rhs_refs_comp
)))
10047 /* Only detour to caf_send[get][_by_ref] () when the lhs or rhs is an
10048 allocatable component, because those need to be accessed via the
10049 caf-runtime. No need to check for coindexes here, because resolve
10050 has rewritten those already. */
10052 gfc_actual_arglist a1
, a2
;
10053 /* Clear the structures to prevent accessing garbage. */
10054 memset (&code
, '\0', sizeof (gfc_code
));
10055 memset (&a1
, '\0', sizeof (gfc_actual_arglist
));
10056 memset (&a2
, '\0', sizeof (gfc_actual_arglist
));
10061 code
.ext
.actual
= &a1
;
10062 code
.resolved_isym
= gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND
);
10063 tmp
= gfc_conv_intrinsic_subroutine (&code
);
10066 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr1
->ts
,
10067 gfc_expr_is_variable (expr2
)
10069 || expr2
->expr_type
== EXPR_ARRAY
,
10070 !(l_is_temp
|| init_flag
) && dealloc
,
10071 expr1
->symtree
->n
.sym
->attr
.codimension
);
10072 /* Add the pre blocks to the body. */
10073 gfc_add_block_to_block (&body
, &rse
.pre
);
10074 gfc_add_block_to_block (&body
, &lse
.pre
);
10075 gfc_add_expr_to_block (&body
, tmp
);
10076 /* Add the post blocks to the body. */
10077 gfc_add_block_to_block (&body
, &rse
.post
);
10078 gfc_add_block_to_block (&body
, &lse
.post
);
10080 if (lss
== gfc_ss_terminator
)
10082 /* F2003: Add the code for reallocation on assignment. */
10083 if (flag_realloc_lhs
&& is_scalar_reallocatable_lhs (expr1
)
10084 && !is_poly_assign
)
10085 alloc_scalar_allocatable_for_assignment (&block
, string_length
,
10088 /* Use the scalar assignment as is. */
10089 gfc_add_block_to_block (&block
, &body
);
10093 gcc_assert (lse
.ss
== gfc_ss_terminator
10094 && rse
.ss
== gfc_ss_terminator
);
10098 gfc_trans_scalarized_loop_boundary (&loop
, &body
);
10100 /* We need to copy the temporary to the actual lhs. */
10101 gfc_init_se (&lse
, NULL
);
10102 gfc_init_se (&rse
, NULL
);
10103 gfc_copy_loopinfo_to_se (&lse
, &loop
);
10104 gfc_copy_loopinfo_to_se (&rse
, &loop
);
10106 rse
.ss
= loop
.temp_ss
;
10109 gfc_conv_tmp_array_ref (&rse
);
10110 gfc_conv_expr (&lse
, expr1
);
10112 gcc_assert (lse
.ss
== gfc_ss_terminator
10113 && rse
.ss
== gfc_ss_terminator
);
10115 if (expr2
->ts
.type
== BT_CHARACTER
)
10116 rse
.string_length
= string_length
;
10118 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr1
->ts
,
10120 gfc_add_expr_to_block (&body
, tmp
);
10123 /* F2003: Allocate or reallocate lhs of allocatable array. */
10124 if (flag_realloc_lhs
10125 && gfc_is_reallocatable_lhs (expr1
)
10127 && !is_runtime_conformable (expr1
, expr2
))
10129 realloc_lhs_warning (expr1
->ts
.type
, true, &expr1
->where
);
10130 ompws_flags
&= ~OMPWS_SCALARIZER_WS
;
10131 tmp
= gfc_alloc_allocatable_for_assignment (&loop
, expr1
, expr2
);
10132 if (tmp
!= NULL_TREE
)
10133 gfc_add_expr_to_block (&loop
.code
[expr1
->rank
- 1], tmp
);
10136 if (maybe_workshare
)
10137 ompws_flags
&= ~OMPWS_SCALARIZER_BODY
;
10139 /* Generate the copying loops. */
10140 gfc_trans_scalarizing_loops (&loop
, &body
);
10142 /* Wrap the whole thing up. */
10143 gfc_add_block_to_block (&block
, &loop
.pre
);
10144 gfc_add_block_to_block (&block
, &loop
.post
);
10146 gfc_cleanup_loop (&loop
);
10149 return gfc_finish_block (&block
);
10153 /* Check whether EXPR is a copyable array. */
10156 copyable_array_p (gfc_expr
* expr
)
10158 if (expr
->expr_type
!= EXPR_VARIABLE
)
10161 /* First check it's an array. */
10162 if (expr
->rank
< 1 || !expr
->ref
|| expr
->ref
->next
)
10165 if (!gfc_full_array_ref_p (expr
->ref
, NULL
))
10168 /* Next check that it's of a simple enough type. */
10169 switch (expr
->ts
.type
)
10181 return !expr
->ts
.u
.derived
->attr
.alloc_comp
;
10190 /* Translate an assignment. */
10193 gfc_trans_assignment (gfc_expr
* expr1
, gfc_expr
* expr2
, bool init_flag
,
10194 bool dealloc
, bool use_vptr_copy
, bool may_alias
)
10198 /* Special case a single function returning an array. */
10199 if (expr2
->expr_type
== EXPR_FUNCTION
&& expr2
->rank
> 0)
10201 tmp
= gfc_trans_arrayfunc_assign (expr1
, expr2
);
10206 /* Special case assigning an array to zero. */
10207 if (copyable_array_p (expr1
)
10208 && is_zero_initializer_p (expr2
))
10210 tmp
= gfc_trans_zero_assign (expr1
);
10215 /* Special case copying one array to another. */
10216 if (copyable_array_p (expr1
)
10217 && copyable_array_p (expr2
)
10218 && gfc_compare_types (&expr1
->ts
, &expr2
->ts
)
10219 && !gfc_check_dependency (expr1
, expr2
, 0))
10221 tmp
= gfc_trans_array_copy (expr1
, expr2
);
10226 /* Special case initializing an array from a constant array constructor. */
10227 if (copyable_array_p (expr1
)
10228 && expr2
->expr_type
== EXPR_ARRAY
10229 && gfc_compare_types (&expr1
->ts
, &expr2
->ts
))
10231 tmp
= gfc_trans_array_constructor_copy (expr1
, expr2
);
10236 /* Fallback to the scalarizer to generate explicit loops. */
10237 return gfc_trans_assignment_1 (expr1
, expr2
, init_flag
, dealloc
,
10238 use_vptr_copy
, may_alias
);
10242 gfc_trans_init_assign (gfc_code
* code
)
10244 return gfc_trans_assignment (code
->expr1
, code
->expr2
, true, false, true);
10248 gfc_trans_assign (gfc_code
* code
)
10250 return gfc_trans_assignment (code
->expr1
, code
->expr2
, false, true);