]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/trans-expr.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / fortran / trans-expr.c
1 /* Expression translation
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
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/>. */
21
22 /* trans-expr.c-- generate GENERIC trees for gfc_expr. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "gfortran.h"
28 #include "hash-set.h"
29 #include "vec.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "options.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "stringpool.h"
38 #include "diagnostic-core.h" /* For fatal_error. */
39 #include "langhooks.h"
40 #include "flags.h"
41 #include "arith.h"
42 #include "constructor.h"
43 #include "trans.h"
44 #include "trans-const.h"
45 #include "trans-types.h"
46 #include "trans-array.h"
47 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
48 #include "trans-stmt.h"
49 #include "dependency.h"
50 #include "gimplify.h"
51
52 /* Convert a scalar to an array descriptor. To be used for assumed-rank
53 arrays. */
54
55 static tree
56 get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
57 {
58 enum gfc_array_kind akind;
59
60 if (attr.pointer)
61 akind = GFC_ARRAY_POINTER_CONT;
62 else if (attr.allocatable)
63 akind = GFC_ARRAY_ALLOCATABLE;
64 else
65 akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
66
67 if (POINTER_TYPE_P (TREE_TYPE (scalar)))
68 scalar = TREE_TYPE (scalar);
69 return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, 0, NULL, NULL, 1,
70 akind, !(attr.pointer || attr.target));
71 }
72
73 tree
74 gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
75 {
76 tree desc, type;
77
78 type = get_scalar_to_descriptor_type (scalar, attr);
79 desc = gfc_create_var (type, "desc");
80 DECL_ARTIFICIAL (desc) = 1;
81
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);
87
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)));
94 return desc;
95 }
96
97
98 /* This is the seed for an eventual trans-class.c
99
100 The following parameters should not be used directly since they might
101 in future implementations. Use the corresponding APIs. */
102 #define CLASS_DATA_FIELD 0
103 #define CLASS_VPTR_FIELD 1
104 #define CLASS_LEN_FIELD 2
105 #define VTABLE_HASH_FIELD 0
106 #define VTABLE_SIZE_FIELD 1
107 #define VTABLE_EXTENDS_FIELD 2
108 #define VTABLE_DEF_INIT_FIELD 3
109 #define VTABLE_COPY_FIELD 4
110 #define VTABLE_FINAL_FIELD 5
111
112
113 tree
114 gfc_class_set_static_fields (tree decl, tree vptr, tree data)
115 {
116 tree tmp;
117 tree field;
118 vec<constructor_elt, va_gc> *init = NULL;
119
120 field = TYPE_FIELDS (TREE_TYPE (decl));
121 tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
122 CONSTRUCTOR_APPEND_ELT (init, tmp, data);
123
124 tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
125 CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
126
127 return build_constructor (TREE_TYPE (decl), init);
128 }
129
130
131 tree
132 gfc_class_data_get (tree decl)
133 {
134 tree data;
135 if (POINTER_TYPE_P (TREE_TYPE (decl)))
136 decl = build_fold_indirect_ref_loc (input_location, decl);
137 data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
138 CLASS_DATA_FIELD);
139 return fold_build3_loc (input_location, COMPONENT_REF,
140 TREE_TYPE (data), decl, data,
141 NULL_TREE);
142 }
143
144
145 tree
146 gfc_class_vptr_get (tree decl)
147 {
148 tree vptr;
149 /* For class arrays decl may be a temporary descriptor handle, the vptr is
150 then available through the saved descriptor. */
151 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl)
152 && GFC_DECL_SAVED_DESCRIPTOR (decl))
153 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
154 if (POINTER_TYPE_P (TREE_TYPE (decl)))
155 decl = build_fold_indirect_ref_loc (input_location, decl);
156 vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
157 CLASS_VPTR_FIELD);
158 return fold_build3_loc (input_location, COMPONENT_REF,
159 TREE_TYPE (vptr), decl, vptr,
160 NULL_TREE);
161 }
162
163
164 tree
165 gfc_class_len_get (tree decl)
166 {
167 tree len;
168 /* For class arrays decl may be a temporary descriptor handle, the len is
169 then available through the saved descriptor. */
170 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl)
171 && GFC_DECL_SAVED_DESCRIPTOR (decl))
172 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
173 if (POINTER_TYPE_P (TREE_TYPE (decl)))
174 decl = build_fold_indirect_ref_loc (input_location, decl);
175 len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
176 CLASS_LEN_FIELD);
177 return fold_build3_loc (input_location, COMPONENT_REF,
178 TREE_TYPE (len), decl, len,
179 NULL_TREE);
180 }
181
182
183 /* Get the specified FIELD from the VPTR. */
184
185 static tree
186 vptr_field_get (tree vptr, int fieldno)
187 {
188 tree field;
189 vptr = build_fold_indirect_ref_loc (input_location, vptr);
190 field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
191 fieldno);
192 field = fold_build3_loc (input_location, COMPONENT_REF,
193 TREE_TYPE (field), vptr, field,
194 NULL_TREE);
195 gcc_assert (field);
196 return field;
197 }
198
199
200 /* Get the field from the class' vptr. */
201
202 static tree
203 class_vtab_field_get (tree decl, int fieldno)
204 {
205 tree vptr;
206 vptr = gfc_class_vptr_get (decl);
207 return vptr_field_get (vptr, fieldno);
208 }
209
210
211 /* Define a macro for creating the class_vtab_* and vptr_* accessors in
212 unison. */
213 #define VTAB_GET_FIELD_GEN(name, field) tree \
214 gfc_class_vtab_## name ##_get (tree cl) \
215 { \
216 return class_vtab_field_get (cl, field); \
217 } \
218 \
219 tree \
220 gfc_vptr_## name ##_get (tree vptr) \
221 { \
222 return vptr_field_get (vptr, field); \
223 }
224
225 VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
226 VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
227 VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
228 VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
229 VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
230
231
232 /* The size field is returned as an array index type. Therefore treat
233 it and only it specially. */
234
235 tree
236 gfc_class_vtab_size_get (tree cl)
237 {
238 tree size;
239 size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
240 /* Always return size as an array index type. */
241 size = fold_convert (gfc_array_index_type, size);
242 gcc_assert (size);
243 return size;
244 }
245
246 tree
247 gfc_vptr_size_get (tree vptr)
248 {
249 tree size;
250 size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
251 /* Always return size as an array index type. */
252 size = fold_convert (gfc_array_index_type, size);
253 gcc_assert (size);
254 return size;
255 }
256
257
258 #undef CLASS_DATA_FIELD
259 #undef CLASS_VPTR_FIELD
260 #undef VTABLE_HASH_FIELD
261 #undef VTABLE_SIZE_FIELD
262 #undef VTABLE_EXTENDS_FIELD
263 #undef VTABLE_DEF_INIT_FIELD
264 #undef VTABLE_COPY_FIELD
265 #undef VTABLE_FINAL_FIELD
266
267
268 /* Search for the last _class ref in the chain of references of this
269 expression and cut the chain there. Albeit this routine is similiar
270 to class.c::gfc_add_component_ref (), is there a significant
271 difference: gfc_add_component_ref () concentrates on an array ref to
272 be the last ref in the chain. This routine is oblivious to the kind
273 of refs following. */
274
275 gfc_expr *
276 gfc_find_and_cut_at_last_class_ref (gfc_expr *e)
277 {
278 gfc_expr *base_expr;
279 gfc_ref *ref, *class_ref, *tail;
280
281 /* Find the last class reference. */
282 class_ref = NULL;
283 for (ref = e->ref; ref; ref = ref->next)
284 {
285 if (ref->type == REF_COMPONENT
286 && ref->u.c.component->ts.type == BT_CLASS)
287 class_ref = ref;
288
289 if (ref->next == NULL)
290 break;
291 }
292
293 /* Remove and store all subsequent references after the
294 CLASS reference. */
295 if (class_ref)
296 {
297 tail = class_ref->next;
298 class_ref->next = NULL;
299 }
300 else
301 {
302 tail = e->ref;
303 e->ref = NULL;
304 }
305
306 base_expr = gfc_expr_to_initialize (e);
307
308 /* Restore the original tail expression. */
309 if (class_ref)
310 {
311 gfc_free_ref_list (class_ref->next);
312 class_ref->next = tail;
313 }
314 else
315 {
316 gfc_free_ref_list (e->ref);
317 e->ref = tail;
318 }
319 return base_expr;
320 }
321
322
323 /* Reset the vptr to the declared type, e.g. after deallocation. */
324
325 void
326 gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
327 {
328 gfc_expr *rhs, *lhs = gfc_copy_expr (e);
329 gfc_symbol *vtab;
330 tree tmp;
331 gfc_ref *ref;
332
333 /* If we have a class array, we need go back to the class
334 container. */
335 if (lhs->ref && lhs->ref->next && !lhs->ref->next->next
336 && lhs->ref->next->type == REF_ARRAY
337 && lhs->ref->next->u.ar.type == AR_FULL
338 && lhs->ref->type == REF_COMPONENT
339 && strcmp (lhs->ref->u.c.component->name, "_data") == 0)
340 {
341 gfc_free_ref_list (lhs->ref);
342 lhs->ref = NULL;
343 }
344 else
345 for (ref = lhs->ref; ref; ref = ref->next)
346 if (ref->next && ref->next->next && !ref->next->next->next
347 && ref->next->next->type == REF_ARRAY
348 && ref->next->next->u.ar.type == AR_FULL
349 && ref->next->type == REF_COMPONENT
350 && strcmp (ref->next->u.c.component->name, "_data") == 0)
351 {
352 gfc_free_ref_list (ref->next);
353 ref->next = NULL;
354 }
355
356 gfc_add_vptr_component (lhs);
357
358 if (UNLIMITED_POLY (e))
359 rhs = gfc_get_null_expr (NULL);
360 else
361 {
362 vtab = gfc_find_derived_vtab (e->ts.u.derived);
363 rhs = gfc_lval_expr_from_sym (vtab);
364 }
365 tmp = gfc_trans_pointer_assignment (lhs, rhs);
366 gfc_add_expr_to_block (block, tmp);
367 gfc_free_expr (lhs);
368 gfc_free_expr (rhs);
369 }
370
371
372 /* Reset the len for unlimited polymorphic objects. */
373
374 void
375 gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
376 {
377 gfc_expr *e;
378 gfc_se se_len;
379 e = gfc_find_and_cut_at_last_class_ref (expr);
380 gfc_add_len_component (e);
381 gfc_init_se (&se_len, NULL);
382 gfc_conv_expr (&se_len, e);
383 gfc_add_modify (block, se_len.expr,
384 fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
385 gfc_free_expr (e);
386 }
387
388
389 /* Obtain the vptr of the last class reference in an expression.
390 Return NULL_TREE if no class reference is found. */
391
392 tree
393 gfc_get_vptr_from_expr (tree expr)
394 {
395 tree tmp;
396 tree type;
397
398 for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
399 {
400 type = TREE_TYPE (tmp);
401 while (type)
402 {
403 if (GFC_CLASS_TYPE_P (type))
404 return gfc_class_vptr_get (tmp);
405 if (type != TYPE_CANONICAL (type))
406 type = TYPE_CANONICAL (type);
407 else
408 type = NULL_TREE;
409 }
410 if (TREE_CODE (tmp) == VAR_DECL)
411 break;
412 }
413 return NULL_TREE;
414 }
415
416
417 static void
418 class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
419 bool lhs_type)
420 {
421 tree tmp, tmp2, type;
422
423 gfc_conv_descriptor_data_set (block, lhs_desc,
424 gfc_conv_descriptor_data_get (rhs_desc));
425 gfc_conv_descriptor_offset_set (block, lhs_desc,
426 gfc_conv_descriptor_offset_get (rhs_desc));
427
428 gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
429 gfc_conv_descriptor_dtype (rhs_desc));
430
431 /* Assign the dimension as range-ref. */
432 tmp = gfc_get_descriptor_dimension (lhs_desc);
433 tmp2 = gfc_get_descriptor_dimension (rhs_desc);
434
435 type = lhs_type ? TREE_TYPE (tmp) : TREE_TYPE (tmp2);
436 tmp = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp,
437 gfc_index_zero_node, NULL_TREE, NULL_TREE);
438 tmp2 = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp2,
439 gfc_index_zero_node, NULL_TREE, NULL_TREE);
440 gfc_add_modify (block, tmp, tmp2);
441 }
442
443
444 /* Takes a derived type expression and returns the address of a temporary
445 class object of the 'declared' type. If vptr is not NULL, this is
446 used for the temporary class object.
447 optional_alloc_ptr is false when the dummy is neither allocatable
448 nor a pointer; that's only relevant for the optional handling. */
449 void
450 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
451 gfc_typespec class_ts, tree vptr, bool optional,
452 bool optional_alloc_ptr)
453 {
454 gfc_symbol *vtab;
455 tree cond_optional = NULL_TREE;
456 gfc_ss *ss;
457 tree ctree;
458 tree var;
459 tree tmp;
460
461 /* The derived type needs to be converted to a temporary
462 CLASS object. */
463 tmp = gfc_typenode_for_spec (&class_ts);
464 var = gfc_create_var (tmp, "class");
465
466 /* Set the vptr. */
467 ctree = gfc_class_vptr_get (var);
468
469 if (vptr != NULL_TREE)
470 {
471 /* Use the dynamic vptr. */
472 tmp = vptr;
473 }
474 else
475 {
476 /* In this case the vtab corresponds to the derived type and the
477 vptr must point to it. */
478 vtab = gfc_find_derived_vtab (e->ts.u.derived);
479 gcc_assert (vtab);
480 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
481 }
482 gfc_add_modify (&parmse->pre, ctree,
483 fold_convert (TREE_TYPE (ctree), tmp));
484
485 /* Now set the data field. */
486 ctree = gfc_class_data_get (var);
487
488 if (optional)
489 cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
490
491 if (parmse->ss && parmse->ss->info->useflags)
492 {
493 /* For an array reference in an elemental procedure call we need
494 to retain the ss to provide the scalarized array reference. */
495 gfc_conv_expr_reference (parmse, e);
496 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
497 if (optional)
498 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
499 cond_optional, tmp,
500 fold_convert (TREE_TYPE (tmp), null_pointer_node));
501 gfc_add_modify (&parmse->pre, ctree, tmp);
502
503 }
504 else
505 {
506 ss = gfc_walk_expr (e);
507 if (ss == gfc_ss_terminator)
508 {
509 parmse->ss = NULL;
510 gfc_conv_expr_reference (parmse, e);
511
512 /* Scalar to an assumed-rank array. */
513 if (class_ts.u.derived->components->as)
514 {
515 tree type;
516 type = get_scalar_to_descriptor_type (parmse->expr,
517 gfc_expr_attr (e));
518 gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
519 gfc_get_dtype (type));
520 if (optional)
521 parmse->expr = build3_loc (input_location, COND_EXPR,
522 TREE_TYPE (parmse->expr),
523 cond_optional, parmse->expr,
524 fold_convert (TREE_TYPE (parmse->expr),
525 null_pointer_node));
526 gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
527 }
528 else
529 {
530 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
531 if (optional)
532 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
533 cond_optional, tmp,
534 fold_convert (TREE_TYPE (tmp),
535 null_pointer_node));
536 gfc_add_modify (&parmse->pre, ctree, tmp);
537 }
538 }
539 else
540 {
541 stmtblock_t block;
542 gfc_init_block (&block);
543
544 parmse->ss = ss;
545 gfc_conv_expr_descriptor (parmse, e);
546
547 if (e->rank != class_ts.u.derived->components->as->rank)
548 {
549 gcc_assert (class_ts.u.derived->components->as->type
550 == AS_ASSUMED_RANK);
551 class_array_data_assign (&block, ctree, parmse->expr, false);
552 }
553 else
554 {
555 if (gfc_expr_attr (e).codimension)
556 parmse->expr = fold_build1_loc (input_location,
557 VIEW_CONVERT_EXPR,
558 TREE_TYPE (ctree),
559 parmse->expr);
560 gfc_add_modify (&block, ctree, parmse->expr);
561 }
562
563 if (optional)
564 {
565 tmp = gfc_finish_block (&block);
566
567 gfc_init_block (&block);
568 gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
569
570 tmp = build3_v (COND_EXPR, cond_optional, tmp,
571 gfc_finish_block (&block));
572 gfc_add_expr_to_block (&parmse->pre, tmp);
573 }
574 else
575 gfc_add_block_to_block (&parmse->pre, &block);
576 }
577 }
578
579 if (class_ts.u.derived->components->ts.type == BT_DERIVED
580 && class_ts.u.derived->components->ts.u.derived
581 ->attr.unlimited_polymorphic)
582 {
583 /* Take care about initializing the _len component correctly. */
584 ctree = gfc_class_len_get (var);
585 if (UNLIMITED_POLY (e))
586 {
587 gfc_expr *len;
588 gfc_se se;
589
590 len = gfc_copy_expr (e);
591 gfc_add_len_component (len);
592 gfc_init_se (&se, NULL);
593 gfc_conv_expr (&se, len);
594 if (optional)
595 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
596 cond_optional, se.expr,
597 fold_convert (TREE_TYPE (se.expr),
598 integer_zero_node));
599 else
600 tmp = se.expr;
601 }
602 else
603 tmp = integer_zero_node;
604 gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree),
605 tmp));
606 }
607 /* Pass the address of the class object. */
608 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
609
610 if (optional && optional_alloc_ptr)
611 parmse->expr = build3_loc (input_location, COND_EXPR,
612 TREE_TYPE (parmse->expr),
613 cond_optional, parmse->expr,
614 fold_convert (TREE_TYPE (parmse->expr),
615 null_pointer_node));
616 }
617
618
619 /* Create a new class container, which is required as scalar coarrays
620 have an array descriptor while normal scalars haven't. Optionally,
621 NULL pointer checks are added if the argument is OPTIONAL. */
622
623 static void
624 class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
625 gfc_typespec class_ts, bool optional)
626 {
627 tree var, ctree, tmp;
628 stmtblock_t block;
629 gfc_ref *ref;
630 gfc_ref *class_ref;
631
632 gfc_init_block (&block);
633
634 class_ref = NULL;
635 for (ref = e->ref; ref; ref = ref->next)
636 {
637 if (ref->type == REF_COMPONENT
638 && ref->u.c.component->ts.type == BT_CLASS)
639 class_ref = ref;
640 }
641
642 if (class_ref == NULL
643 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
644 tmp = e->symtree->n.sym->backend_decl;
645 else
646 {
647 /* Remove everything after the last class reference, convert the
648 expression and then recover its tailend once more. */
649 gfc_se tmpse;
650 ref = class_ref->next;
651 class_ref->next = NULL;
652 gfc_init_se (&tmpse, NULL);
653 gfc_conv_expr (&tmpse, e);
654 class_ref->next = ref;
655 tmp = tmpse.expr;
656 }
657
658 var = gfc_typenode_for_spec (&class_ts);
659 var = gfc_create_var (var, "class");
660
661 ctree = gfc_class_vptr_get (var);
662 gfc_add_modify (&block, ctree,
663 fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
664
665 ctree = gfc_class_data_get (var);
666 tmp = gfc_conv_descriptor_data_get (gfc_class_data_get (tmp));
667 gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
668
669 /* Pass the address of the class object. */
670 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
671
672 if (optional)
673 {
674 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
675 tree tmp2;
676
677 tmp = gfc_finish_block (&block);
678
679 gfc_init_block (&block);
680 tmp2 = gfc_class_data_get (var);
681 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
682 null_pointer_node));
683 tmp2 = gfc_finish_block (&block);
684
685 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
686 cond, tmp, tmp2);
687 gfc_add_expr_to_block (&parmse->pre, tmp);
688 }
689 else
690 gfc_add_block_to_block (&parmse->pre, &block);
691 }
692
693
694 /* Takes an intrinsic type expression and returns the address of a temporary
695 class object of the 'declared' type. */
696 void
697 gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
698 gfc_typespec class_ts)
699 {
700 gfc_symbol *vtab;
701 gfc_ss *ss;
702 tree ctree;
703 tree var;
704 tree tmp;
705
706 /* The intrinsic type needs to be converted to a temporary
707 CLASS object. */
708 tmp = gfc_typenode_for_spec (&class_ts);
709 var = gfc_create_var (tmp, "class");
710
711 /* Set the vptr. */
712 ctree = gfc_class_vptr_get (var);
713
714 vtab = gfc_find_vtab (&e->ts);
715 gcc_assert (vtab);
716 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
717 gfc_add_modify (&parmse->pre, ctree,
718 fold_convert (TREE_TYPE (ctree), tmp));
719
720 /* Now set the data field. */
721 ctree = gfc_class_data_get (var);
722 if (parmse->ss && parmse->ss->info->useflags)
723 {
724 /* For an array reference in an elemental procedure call we need
725 to retain the ss to provide the scalarized array reference. */
726 gfc_conv_expr_reference (parmse, e);
727 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
728 gfc_add_modify (&parmse->pre, ctree, tmp);
729 }
730 else
731 {
732 ss = gfc_walk_expr (e);
733 if (ss == gfc_ss_terminator)
734 {
735 parmse->ss = NULL;
736 gfc_conv_expr_reference (parmse, e);
737 if (class_ts.u.derived->components->as
738 && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
739 {
740 tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
741 gfc_expr_attr (e));
742 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
743 TREE_TYPE (ctree), tmp);
744 }
745 else
746 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
747 gfc_add_modify (&parmse->pre, ctree, tmp);
748 }
749 else
750 {
751 parmse->ss = ss;
752 parmse->use_offset = 1;
753 gfc_conv_expr_descriptor (parmse, e);
754 if (class_ts.u.derived->components->as->rank != e->rank)
755 {
756 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
757 TREE_TYPE (ctree), parmse->expr);
758 gfc_add_modify (&parmse->pre, ctree, tmp);
759 }
760 else
761 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
762 }
763 }
764
765 gcc_assert (class_ts.type == BT_CLASS);
766 if (class_ts.u.derived->components->ts.type == BT_DERIVED
767 && class_ts.u.derived->components->ts.u.derived
768 ->attr.unlimited_polymorphic)
769 {
770 ctree = gfc_class_len_get (var);
771 /* When the actual arg is a char array, then set the _len component of the
772 unlimited polymorphic entity, too. */
773 if (e->ts.type == BT_CHARACTER)
774 {
775 /* Start with parmse->string_length because this seems to be set to a
776 correct value more often. */
777 if (parmse->string_length)
778 tmp = parmse->string_length;
779 /* When the string_length is not yet set, then try the backend_decl of
780 the cl. */
781 else if (e->ts.u.cl->backend_decl)
782 tmp = e->ts.u.cl->backend_decl;
783 /* If both of the above approaches fail, then try to generate an
784 expression from the input, which is only feasible currently, when the
785 expression can be evaluated to a constant one. */
786 else
787 {
788 /* Try to simplify the expression. */
789 gfc_simplify_expr (e, 0);
790 if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
791 {
792 /* Amazingly all data is present to compute the length of a
793 constant string, but the expression is not yet there. */
794 e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER, 4,
795 &e->where);
796 mpz_set_ui (e->ts.u.cl->length->value.integer,
797 e->value.character.length);
798 gfc_conv_const_charlen (e->ts.u.cl);
799 e->ts.u.cl->resolved = 1;
800 tmp = e->ts.u.cl->backend_decl;
801 }
802 else
803 {
804 gfc_error ("Can't compute the length of the char array at %L.",
805 &e->where);
806 }
807 }
808 }
809 else
810 tmp = integer_zero_node;
811
812 gfc_add_modify (&parmse->pre, ctree, tmp);
813 }
814 else if (class_ts.type == BT_CLASS
815 && class_ts.u.derived->components
816 && class_ts.u.derived->components->ts.u
817 .derived->attr.unlimited_polymorphic)
818 {
819 ctree = gfc_class_len_get (var);
820 gfc_add_modify (&parmse->pre, ctree,
821 fold_convert (TREE_TYPE (ctree),
822 integer_zero_node));
823 }
824 /* Pass the address of the class object. */
825 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
826 }
827
828
829 /* Takes a scalarized class array expression and returns the
830 address of a temporary scalar class object of the 'declared'
831 type.
832 OOP-TODO: This could be improved by adding code that branched on
833 the dynamic type being the same as the declared type. In this case
834 the original class expression can be passed directly.
835 optional_alloc_ptr is false when the dummy is neither allocatable
836 nor a pointer; that's relevant for the optional handling.
837 Set copyback to true if class container's _data and _vtab pointers
838 might get modified. */
839
840 void
841 gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
842 bool elemental, bool copyback, bool optional,
843 bool optional_alloc_ptr)
844 {
845 tree ctree;
846 tree var;
847 tree tmp;
848 tree vptr;
849 tree cond = NULL_TREE;
850 tree slen = NULL_TREE;
851 gfc_ref *ref;
852 gfc_ref *class_ref;
853 stmtblock_t block;
854 bool full_array = false;
855
856 gfc_init_block (&block);
857
858 class_ref = NULL;
859 for (ref = e->ref; ref; ref = ref->next)
860 {
861 if (ref->type == REF_COMPONENT
862 && ref->u.c.component->ts.type == BT_CLASS)
863 class_ref = ref;
864
865 if (ref->next == NULL)
866 break;
867 }
868
869 if ((ref == NULL || class_ref == ref)
870 && (!class_ts.u.derived->components->as
871 || class_ts.u.derived->components->as->rank != -1))
872 return;
873
874 /* Test for FULL_ARRAY. */
875 if (e->rank == 0 && gfc_expr_attr (e).codimension
876 && gfc_expr_attr (e).dimension)
877 full_array = true;
878 else
879 gfc_is_class_array_ref (e, &full_array);
880
881 /* The derived type needs to be converted to a temporary
882 CLASS object. */
883 tmp = gfc_typenode_for_spec (&class_ts);
884 var = gfc_create_var (tmp, "class");
885
886 /* Set the data. */
887 ctree = gfc_class_data_get (var);
888 if (class_ts.u.derived->components->as
889 && e->rank != class_ts.u.derived->components->as->rank)
890 {
891 if (e->rank == 0)
892 {
893 tree type = get_scalar_to_descriptor_type (parmse->expr,
894 gfc_expr_attr (e));
895 gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
896 gfc_get_dtype (type));
897
898 tmp = gfc_class_data_get (parmse->expr);
899 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
900 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
901
902 gfc_conv_descriptor_data_set (&block, ctree, tmp);
903 }
904 else
905 class_array_data_assign (&block, ctree, parmse->expr, false);
906 }
907 else
908 {
909 if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
910 parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
911 TREE_TYPE (ctree), parmse->expr);
912 gfc_add_modify (&block, ctree, parmse->expr);
913 }
914
915 /* Return the data component, except in the case of scalarized array
916 references, where nullification of the cannot occur and so there
917 is no need. */
918 if (!elemental && full_array && copyback)
919 {
920 if (class_ts.u.derived->components->as
921 && e->rank != class_ts.u.derived->components->as->rank)
922 {
923 if (e->rank == 0)
924 gfc_add_modify (&parmse->post, gfc_class_data_get (parmse->expr),
925 gfc_conv_descriptor_data_get (ctree));
926 else
927 class_array_data_assign (&parmse->post, parmse->expr, ctree, true);
928 }
929 else
930 gfc_add_modify (&parmse->post, parmse->expr, ctree);
931 }
932
933 /* Set the vptr. */
934 ctree = gfc_class_vptr_get (var);
935
936 /* The vptr is the second field of the actual argument.
937 First we have to find the corresponding class reference. */
938
939 tmp = NULL_TREE;
940 if (class_ref == NULL
941 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
942 {
943 tmp = e->symtree->n.sym->backend_decl;
944 if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
945 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
946 slen = integer_zero_node;
947 }
948 else
949 {
950 /* Remove everything after the last class reference, convert the
951 expression and then recover its tailend once more. */
952 gfc_se tmpse;
953 ref = class_ref->next;
954 class_ref->next = NULL;
955 gfc_init_se (&tmpse, NULL);
956 gfc_conv_expr (&tmpse, e);
957 class_ref->next = ref;
958 tmp = tmpse.expr;
959 slen = tmpse.string_length;
960 }
961
962 gcc_assert (tmp != NULL_TREE);
963
964 /* Dereference if needs be. */
965 if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
966 tmp = build_fold_indirect_ref_loc (input_location, tmp);
967
968 vptr = gfc_class_vptr_get (tmp);
969 gfc_add_modify (&block, ctree,
970 fold_convert (TREE_TYPE (ctree), vptr));
971
972 /* Return the vptr component, except in the case of scalarized array
973 references, where the dynamic type cannot change. */
974 if (!elemental && full_array && copyback)
975 gfc_add_modify (&parmse->post, vptr,
976 fold_convert (TREE_TYPE (vptr), ctree));
977
978 /* For unlimited polymorphic objects also set the _len component. */
979 if (class_ts.type == BT_CLASS
980 && class_ts.u.derived->components
981 && class_ts.u.derived->components->ts.u
982 .derived->attr.unlimited_polymorphic)
983 {
984 ctree = gfc_class_len_get (var);
985 if (UNLIMITED_POLY (e))
986 tmp = gfc_class_len_get (tmp);
987 else if (e->ts.type == BT_CHARACTER)
988 {
989 gcc_assert (slen != NULL_TREE);
990 tmp = slen;
991 }
992 else
993 tmp = integer_zero_node;
994 gfc_add_modify (&parmse->pre, ctree,
995 fold_convert (TREE_TYPE (ctree), tmp));
996 }
997
998 if (optional)
999 {
1000 tree tmp2;
1001
1002 cond = gfc_conv_expr_present (e->symtree->n.sym);
1003 /* parmse->pre may contain some preparatory instructions for the
1004 temporary array descriptor. Those may only be executed when the
1005 optional argument is set, therefore add parmse->pre's instructions
1006 to block, which is later guarded by an if (optional_arg_given). */
1007 gfc_add_block_to_block (&parmse->pre, &block);
1008 block.head = parmse->pre.head;
1009 parmse->pre.head = NULL_TREE;
1010 tmp = gfc_finish_block (&block);
1011
1012 if (optional_alloc_ptr)
1013 tmp2 = build_empty_stmt (input_location);
1014 else
1015 {
1016 gfc_init_block (&block);
1017
1018 tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
1019 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
1020 null_pointer_node));
1021 tmp2 = gfc_finish_block (&block);
1022 }
1023
1024 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1025 cond, tmp, tmp2);
1026 gfc_add_expr_to_block (&parmse->pre, tmp);
1027 }
1028 else
1029 gfc_add_block_to_block (&parmse->pre, &block);
1030
1031 /* Pass the address of the class object. */
1032 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1033
1034 if (optional && optional_alloc_ptr)
1035 parmse->expr = build3_loc (input_location, COND_EXPR,
1036 TREE_TYPE (parmse->expr),
1037 cond, parmse->expr,
1038 fold_convert (TREE_TYPE (parmse->expr),
1039 null_pointer_node));
1040 }
1041
1042
1043 /* Given a class array declaration and an index, returns the address
1044 of the referenced element. */
1045
1046 tree
1047 gfc_get_class_array_ref (tree index, tree class_decl)
1048 {
1049 tree data = gfc_class_data_get (class_decl);
1050 tree size = gfc_class_vtab_size_get (class_decl);
1051 tree offset = fold_build2_loc (input_location, MULT_EXPR,
1052 gfc_array_index_type,
1053 index, size);
1054 tree ptr;
1055 data = gfc_conv_descriptor_data_get (data);
1056 ptr = fold_convert (pvoid_type_node, data);
1057 ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
1058 return fold_convert (TREE_TYPE (data), ptr);
1059 }
1060
1061
1062 /* Copies one class expression to another, assuming that if either
1063 'to' or 'from' are arrays they are packed. Should 'from' be
1064 NULL_TREE, the initialization expression for 'to' is used, assuming
1065 that the _vptr is set. */
1066
1067 tree
1068 gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
1069 {
1070 tree fcn;
1071 tree fcn_type;
1072 tree from_data;
1073 tree from_len;
1074 tree to_data;
1075 tree to_len;
1076 tree to_ref;
1077 tree from_ref;
1078 vec<tree, va_gc> *args;
1079 tree tmp;
1080 tree stdcopy;
1081 tree extcopy;
1082 tree index;
1083
1084 args = NULL;
1085 /* To prevent warnings on uninitialized variables. */
1086 from_len = to_len = NULL_TREE;
1087
1088 if (from != NULL_TREE)
1089 fcn = gfc_class_vtab_copy_get (from);
1090 else
1091 fcn = gfc_class_vtab_copy_get (to);
1092
1093 fcn_type = TREE_TYPE (TREE_TYPE (fcn));
1094
1095 if (from != NULL_TREE)
1096 from_data = gfc_class_data_get (from);
1097 else
1098 from_data = gfc_class_vtab_def_init_get (to);
1099
1100 if (unlimited)
1101 {
1102 if (from != NULL_TREE && unlimited)
1103 from_len = gfc_class_len_get (from);
1104 else
1105 from_len = integer_zero_node;
1106 }
1107
1108 to_data = gfc_class_data_get (to);
1109 if (unlimited)
1110 to_len = gfc_class_len_get (to);
1111
1112 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
1113 {
1114 stmtblock_t loopbody;
1115 stmtblock_t body;
1116 stmtblock_t ifbody;
1117 gfc_loopinfo loop;
1118
1119 gfc_init_block (&body);
1120 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1121 gfc_array_index_type, nelems,
1122 gfc_index_one_node);
1123 nelems = gfc_evaluate_now (tmp, &body);
1124 index = gfc_create_var (gfc_array_index_type, "S");
1125
1126 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data)))
1127 {
1128 from_ref = gfc_get_class_array_ref (index, from);
1129 vec_safe_push (args, from_ref);
1130 }
1131 else
1132 vec_safe_push (args, from_data);
1133
1134 to_ref = gfc_get_class_array_ref (index, to);
1135 vec_safe_push (args, to_ref);
1136
1137 tmp = build_call_vec (fcn_type, fcn, args);
1138
1139 /* Build the body of the loop. */
1140 gfc_init_block (&loopbody);
1141 gfc_add_expr_to_block (&loopbody, tmp);
1142
1143 /* Build the loop and return. */
1144 gfc_init_loopinfo (&loop);
1145 loop.dimen = 1;
1146 loop.from[0] = gfc_index_zero_node;
1147 loop.loopvar[0] = index;
1148 loop.to[0] = nelems;
1149 gfc_trans_scalarizing_loops (&loop, &loopbody);
1150 gfc_init_block (&ifbody);
1151 gfc_add_block_to_block (&ifbody, &loop.pre);
1152 stdcopy = gfc_finish_block (&ifbody);
1153 /* In initialization mode from_len is a constant zero. */
1154 if (unlimited && !integer_zerop (from_len))
1155 {
1156 vec_safe_push (args, from_len);
1157 vec_safe_push (args, to_len);
1158 tmp = build_call_vec (fcn_type, fcn, args);
1159 /* Build the body of the loop. */
1160 gfc_init_block (&loopbody);
1161 gfc_add_expr_to_block (&loopbody, tmp);
1162
1163 /* Build the loop and return. */
1164 gfc_init_loopinfo (&loop);
1165 loop.dimen = 1;
1166 loop.from[0] = gfc_index_zero_node;
1167 loop.loopvar[0] = index;
1168 loop.to[0] = nelems;
1169 gfc_trans_scalarizing_loops (&loop, &loopbody);
1170 gfc_init_block (&ifbody);
1171 gfc_add_block_to_block (&ifbody, &loop.pre);
1172 extcopy = gfc_finish_block (&ifbody);
1173
1174 tmp = fold_build2_loc (input_location, GT_EXPR,
1175 boolean_type_node, from_len,
1176 integer_zero_node);
1177 tmp = fold_build3_loc (input_location, COND_EXPR,
1178 void_type_node, tmp, extcopy, stdcopy);
1179 gfc_add_expr_to_block (&body, tmp);
1180 tmp = gfc_finish_block (&body);
1181 }
1182 else
1183 {
1184 gfc_add_expr_to_block (&body, stdcopy);
1185 tmp = gfc_finish_block (&body);
1186 }
1187 gfc_cleanup_loop (&loop);
1188 }
1189 else
1190 {
1191 gcc_assert (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data)));
1192 vec_safe_push (args, from_data);
1193 vec_safe_push (args, to_data);
1194 stdcopy = build_call_vec (fcn_type, fcn, args);
1195
1196 /* In initialization mode from_len is a constant zero. */
1197 if (unlimited && !integer_zerop (from_len))
1198 {
1199 vec_safe_push (args, from_len);
1200 vec_safe_push (args, to_len);
1201 extcopy = build_call_vec (fcn_type, fcn, args);
1202 tmp = fold_build2_loc (input_location, GT_EXPR,
1203 boolean_type_node, from_len,
1204 integer_zero_node);
1205 tmp = fold_build3_loc (input_location, COND_EXPR,
1206 void_type_node, tmp, extcopy, stdcopy);
1207 }
1208 else
1209 tmp = stdcopy;
1210 }
1211
1212 /* Only copy _def_init to to_data, when it is not a NULL-pointer. */
1213 if (from == NULL_TREE)
1214 {
1215 tree cond;
1216 cond = fold_build2_loc (input_location, NE_EXPR,
1217 boolean_type_node,
1218 from_data, null_pointer_node);
1219 tmp = fold_build3_loc (input_location, COND_EXPR,
1220 void_type_node, cond,
1221 tmp, build_empty_stmt (input_location));
1222 }
1223
1224 return tmp;
1225 }
1226
1227
1228 static tree
1229 gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
1230 {
1231 gfc_actual_arglist *actual;
1232 gfc_expr *ppc;
1233 gfc_code *ppc_code;
1234 tree res;
1235
1236 actual = gfc_get_actual_arglist ();
1237 actual->expr = gfc_copy_expr (rhs);
1238 actual->next = gfc_get_actual_arglist ();
1239 actual->next->expr = gfc_copy_expr (lhs);
1240 ppc = gfc_copy_expr (obj);
1241 gfc_add_vptr_component (ppc);
1242 gfc_add_component_ref (ppc, "_copy");
1243 ppc_code = gfc_get_code (EXEC_CALL);
1244 ppc_code->resolved_sym = ppc->symtree->n.sym;
1245 /* Although '_copy' is set to be elemental in class.c, it is
1246 not staying that way. Find out why, sometime.... */
1247 ppc_code->resolved_sym->attr.elemental = 1;
1248 ppc_code->ext.actual = actual;
1249 ppc_code->expr1 = ppc;
1250 /* Since '_copy' is elemental, the scalarizer will take care
1251 of arrays in gfc_trans_call. */
1252 res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
1253 gfc_free_statements (ppc_code);
1254
1255 if (UNLIMITED_POLY(obj))
1256 {
1257 /* Check if rhs is non-NULL. */
1258 gfc_se src;
1259 gfc_init_se (&src, NULL);
1260 gfc_conv_expr (&src, rhs);
1261 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1262 tree cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1263 src.expr, fold_convert (TREE_TYPE (src.expr),
1264 null_pointer_node));
1265 res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
1266 build_empty_stmt (input_location));
1267 }
1268
1269 return res;
1270 }
1271
1272 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
1273 A MEMCPY is needed to copy the full data from the default initializer
1274 of the dynamic type. */
1275
1276 tree
1277 gfc_trans_class_init_assign (gfc_code *code)
1278 {
1279 stmtblock_t block;
1280 tree tmp;
1281 gfc_se dst,src,memsz;
1282 gfc_expr *lhs, *rhs, *sz;
1283
1284 gfc_start_block (&block);
1285
1286 lhs = gfc_copy_expr (code->expr1);
1287 gfc_add_data_component (lhs);
1288
1289 rhs = gfc_copy_expr (code->expr1);
1290 gfc_add_vptr_component (rhs);
1291
1292 /* Make sure that the component backend_decls have been built, which
1293 will not have happened if the derived types concerned have not
1294 been referenced. */
1295 gfc_get_derived_type (rhs->ts.u.derived);
1296 gfc_add_def_init_component (rhs);
1297 /* The _def_init is always scalar. */
1298 rhs->rank = 0;
1299
1300 if (code->expr1->ts.type == BT_CLASS
1301 && CLASS_DATA (code->expr1)->attr.dimension)
1302 tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
1303 else
1304 {
1305 sz = gfc_copy_expr (code->expr1);
1306 gfc_add_vptr_component (sz);
1307 gfc_add_size_component (sz);
1308
1309 gfc_init_se (&dst, NULL);
1310 gfc_init_se (&src, NULL);
1311 gfc_init_se (&memsz, NULL);
1312 gfc_conv_expr (&dst, lhs);
1313 gfc_conv_expr (&src, rhs);
1314 gfc_conv_expr (&memsz, sz);
1315 gfc_add_block_to_block (&block, &src.pre);
1316 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1317
1318 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
1319
1320 if (UNLIMITED_POLY(code->expr1))
1321 {
1322 /* Check if _def_init is non-NULL. */
1323 tree cond = fold_build2_loc (input_location, NE_EXPR,
1324 boolean_type_node, src.expr,
1325 fold_convert (TREE_TYPE (src.expr),
1326 null_pointer_node));
1327 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
1328 tmp, build_empty_stmt (input_location));
1329 }
1330 }
1331
1332 if (code->expr1->symtree->n.sym->attr.optional
1333 || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master)
1334 {
1335 tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
1336 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
1337 present, tmp,
1338 build_empty_stmt (input_location));
1339 }
1340
1341 gfc_add_expr_to_block (&block, tmp);
1342
1343 return gfc_finish_block (&block);
1344 }
1345
1346
1347 /* Translate an assignment to a CLASS object
1348 (pointer or ordinary assignment). */
1349
1350 tree
1351 gfc_trans_class_assign (gfc_expr *expr1, gfc_expr *expr2, gfc_exec_op op)
1352 {
1353 stmtblock_t block;
1354 tree tmp;
1355 gfc_expr *lhs;
1356 gfc_expr *rhs;
1357 gfc_ref *ref;
1358
1359 gfc_start_block (&block);
1360
1361 ref = expr1->ref;
1362 while (ref && ref->next)
1363 ref = ref->next;
1364
1365 /* Class valued proc_pointer assignments do not need any further
1366 preparation. */
1367 if (ref && ref->type == REF_COMPONENT
1368 && ref->u.c.component->attr.proc_pointer
1369 && expr2->expr_type == EXPR_VARIABLE
1370 && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE
1371 && op == EXEC_POINTER_ASSIGN)
1372 goto assign;
1373
1374 if (expr2->ts.type != BT_CLASS)
1375 {
1376 /* Insert an additional assignment which sets the '_vptr' field. */
1377 gfc_symbol *vtab = NULL;
1378 gfc_symtree *st;
1379
1380 lhs = gfc_copy_expr (expr1);
1381 gfc_add_vptr_component (lhs);
1382
1383 if (UNLIMITED_POLY (expr1)
1384 && expr2->expr_type == EXPR_NULL && expr2->ts.type == BT_UNKNOWN)
1385 {
1386 rhs = gfc_get_null_expr (&expr2->where);
1387 goto assign_vptr;
1388 }
1389
1390 if (expr2->expr_type == EXPR_NULL)
1391 vtab = gfc_find_vtab (&expr1->ts);
1392 else
1393 vtab = gfc_find_vtab (&expr2->ts);
1394 gcc_assert (vtab);
1395
1396 rhs = gfc_get_expr ();
1397 rhs->expr_type = EXPR_VARIABLE;
1398 gfc_find_sym_tree (vtab->name, vtab->ns, 1, &st);
1399 rhs->symtree = st;
1400 rhs->ts = vtab->ts;
1401 assign_vptr:
1402 tmp = gfc_trans_pointer_assignment (lhs, rhs);
1403 gfc_add_expr_to_block (&block, tmp);
1404
1405 gfc_free_expr (lhs);
1406 gfc_free_expr (rhs);
1407 }
1408 else if (expr1->ts.type == BT_DERIVED && UNLIMITED_POLY (expr2))
1409 {
1410 /* F2003:C717 only sequence and bind-C types can come here. */
1411 gcc_assert (expr1->ts.u.derived->attr.sequence
1412 || expr1->ts.u.derived->attr.is_bind_c);
1413 gfc_add_data_component (expr2);
1414 goto assign;
1415 }
1416 else if (CLASS_DATA (expr2)->attr.dimension && expr2->expr_type != EXPR_FUNCTION)
1417 {
1418 /* Insert an additional assignment which sets the '_vptr' field. */
1419 lhs = gfc_copy_expr (expr1);
1420 gfc_add_vptr_component (lhs);
1421
1422 rhs = gfc_copy_expr (expr2);
1423 gfc_add_vptr_component (rhs);
1424
1425 tmp = gfc_trans_pointer_assignment (lhs, rhs);
1426 gfc_add_expr_to_block (&block, tmp);
1427
1428 gfc_free_expr (lhs);
1429 gfc_free_expr (rhs);
1430 }
1431
1432 /* Do the actual CLASS assignment. */
1433 if (expr2->ts.type == BT_CLASS
1434 && !CLASS_DATA (expr2)->attr.dimension)
1435 op = EXEC_ASSIGN;
1436 else if (expr2->expr_type != EXPR_FUNCTION || expr2->ts.type != BT_CLASS
1437 || !CLASS_DATA (expr2)->attr.dimension)
1438 gfc_add_data_component (expr1);
1439
1440 assign:
1441
1442 if (op == EXEC_ASSIGN)
1443 tmp = gfc_trans_assignment (expr1, expr2, false, true);
1444 else if (op == EXEC_POINTER_ASSIGN)
1445 tmp = gfc_trans_pointer_assignment (expr1, expr2);
1446 else
1447 gcc_unreachable();
1448
1449 gfc_add_expr_to_block (&block, tmp);
1450
1451 return gfc_finish_block (&block);
1452 }
1453
1454
1455 /* End of prototype trans-class.c */
1456
1457
1458 static void
1459 realloc_lhs_warning (bt type, bool array, locus *where)
1460 {
1461 if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
1462 gfc_warning (OPT_Wrealloc_lhs,
1463 "Code for reallocating the allocatable array at %L will "
1464 "be added", where);
1465 else if (warn_realloc_lhs_all)
1466 gfc_warning (OPT_Wrealloc_lhs_all,
1467 "Code for reallocating the allocatable variable at %L "
1468 "will be added", where);
1469 }
1470
1471
1472 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init);
1473 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
1474 gfc_expr *);
1475
1476 /* Copy the scalarization loop variables. */
1477
1478 static void
1479 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
1480 {
1481 dest->ss = src->ss;
1482 dest->loop = src->loop;
1483 }
1484
1485
1486 /* Initialize a simple expression holder.
1487
1488 Care must be taken when multiple se are created with the same parent.
1489 The child se must be kept in sync. The easiest way is to delay creation
1490 of a child se until after after the previous se has been translated. */
1491
1492 void
1493 gfc_init_se (gfc_se * se, gfc_se * parent)
1494 {
1495 memset (se, 0, sizeof (gfc_se));
1496 gfc_init_block (&se->pre);
1497 gfc_init_block (&se->post);
1498
1499 se->parent = parent;
1500
1501 if (parent)
1502 gfc_copy_se_loopvars (se, parent);
1503 }
1504
1505
1506 /* Advances to the next SS in the chain. Use this rather than setting
1507 se->ss = se->ss->next because all the parents needs to be kept in sync.
1508 See gfc_init_se. */
1509
1510 void
1511 gfc_advance_se_ss_chain (gfc_se * se)
1512 {
1513 gfc_se *p;
1514 gfc_ss *ss;
1515
1516 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
1517
1518 p = se;
1519 /* Walk down the parent chain. */
1520 while (p != NULL)
1521 {
1522 /* Simple consistency check. */
1523 gcc_assert (p->parent == NULL || p->parent->ss == p->ss
1524 || p->parent->ss->nested_ss == p->ss);
1525
1526 /* If we were in a nested loop, the next scalarized expression can be
1527 on the parent ss' next pointer. Thus we should not take the next
1528 pointer blindly, but rather go up one nest level as long as next
1529 is the end of chain. */
1530 ss = p->ss;
1531 while (ss->next == gfc_ss_terminator && ss->parent != NULL)
1532 ss = ss->parent;
1533
1534 p->ss = ss->next;
1535
1536 p = p->parent;
1537 }
1538 }
1539
1540
1541 /* Ensures the result of the expression as either a temporary variable
1542 or a constant so that it can be used repeatedly. */
1543
1544 void
1545 gfc_make_safe_expr (gfc_se * se)
1546 {
1547 tree var;
1548
1549 if (CONSTANT_CLASS_P (se->expr))
1550 return;
1551
1552 /* We need a temporary for this result. */
1553 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
1554 gfc_add_modify (&se->pre, var, se->expr);
1555 se->expr = var;
1556 }
1557
1558
1559 /* Return an expression which determines if a dummy parameter is present.
1560 Also used for arguments to procedures with multiple entry points. */
1561
1562 tree
1563 gfc_conv_expr_present (gfc_symbol * sym)
1564 {
1565 tree decl, cond;
1566
1567 gcc_assert (sym->attr.dummy);
1568 decl = gfc_get_symbol_decl (sym);
1569
1570 /* Intrinsic scalars with VALUE attribute which are passed by value
1571 use a hidden argument to denote the present status. */
1572 if (sym->attr.value && sym->ts.type != BT_CHARACTER
1573 && sym->ts.type != BT_CLASS && sym->ts.type != BT_DERIVED
1574 && !sym->attr.dimension)
1575 {
1576 char name[GFC_MAX_SYMBOL_LEN + 2];
1577 tree tree_name;
1578
1579 gcc_assert (TREE_CODE (decl) == PARM_DECL);
1580 name[0] = '_';
1581 strcpy (&name[1], sym->name);
1582 tree_name = get_identifier (name);
1583
1584 /* Walk function argument list to find hidden arg. */
1585 cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
1586 for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
1587 if (DECL_NAME (cond) == tree_name)
1588 break;
1589
1590 gcc_assert (cond);
1591 return cond;
1592 }
1593
1594 if (TREE_CODE (decl) != PARM_DECL)
1595 {
1596 /* Array parameters use a temporary descriptor, we want the real
1597 parameter. */
1598 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
1599 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
1600 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
1601 }
1602
1603 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
1604 fold_convert (TREE_TYPE (decl), null_pointer_node));
1605
1606 /* Fortran 2008 allows to pass null pointers and non-associated pointers
1607 as actual argument to denote absent dummies. For array descriptors,
1608 we thus also need to check the array descriptor. For BT_CLASS, it
1609 can also occur for scalars and F2003 due to type->class wrapping and
1610 class->class wrapping. Note further that BT_CLASS always uses an
1611 array descriptor for arrays, also for explicit-shape/assumed-size. */
1612
1613 if (!sym->attr.allocatable
1614 && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
1615 || (sym->ts.type == BT_CLASS
1616 && !CLASS_DATA (sym)->attr.allocatable
1617 && !CLASS_DATA (sym)->attr.class_pointer))
1618 && ((gfc_option.allow_std & GFC_STD_F2008) != 0
1619 || sym->ts.type == BT_CLASS))
1620 {
1621 tree tmp;
1622
1623 if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
1624 || sym->as->type == AS_ASSUMED_RANK
1625 || sym->attr.codimension))
1626 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
1627 {
1628 tmp = build_fold_indirect_ref_loc (input_location, decl);
1629 if (sym->ts.type == BT_CLASS)
1630 tmp = gfc_class_data_get (tmp);
1631 tmp = gfc_conv_array_data (tmp);
1632 }
1633 else if (sym->ts.type == BT_CLASS)
1634 tmp = gfc_class_data_get (decl);
1635 else
1636 tmp = NULL_TREE;
1637
1638 if (tmp != NULL_TREE)
1639 {
1640 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
1641 fold_convert (TREE_TYPE (tmp), null_pointer_node));
1642 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1643 boolean_type_node, cond, tmp);
1644 }
1645 }
1646
1647 return cond;
1648 }
1649
1650
1651 /* Converts a missing, dummy argument into a null or zero. */
1652
1653 void
1654 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
1655 {
1656 tree present;
1657 tree tmp;
1658
1659 present = gfc_conv_expr_present (arg->symtree->n.sym);
1660
1661 if (kind > 0)
1662 {
1663 /* Create a temporary and convert it to the correct type. */
1664 tmp = gfc_get_int_type (kind);
1665 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
1666 se->expr));
1667
1668 /* Test for a NULL value. */
1669 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
1670 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
1671 tmp = gfc_evaluate_now (tmp, &se->pre);
1672 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
1673 }
1674 else
1675 {
1676 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
1677 present, se->expr,
1678 build_zero_cst (TREE_TYPE (se->expr)));
1679 tmp = gfc_evaluate_now (tmp, &se->pre);
1680 se->expr = tmp;
1681 }
1682
1683 if (ts.type == BT_CHARACTER)
1684 {
1685 tmp = build_int_cst (gfc_charlen_type_node, 0);
1686 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
1687 present, se->string_length, tmp);
1688 tmp = gfc_evaluate_now (tmp, &se->pre);
1689 se->string_length = tmp;
1690 }
1691 return;
1692 }
1693
1694
1695 /* Get the character length of an expression, looking through gfc_refs
1696 if necessary. */
1697
1698 tree
1699 gfc_get_expr_charlen (gfc_expr *e)
1700 {
1701 gfc_ref *r;
1702 tree length;
1703
1704 gcc_assert (e->expr_type == EXPR_VARIABLE
1705 && e->ts.type == BT_CHARACTER);
1706
1707 length = NULL; /* To silence compiler warning. */
1708
1709 if (is_subref_array (e) && e->ts.u.cl->length)
1710 {
1711 gfc_se tmpse;
1712 gfc_init_se (&tmpse, NULL);
1713 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
1714 e->ts.u.cl->backend_decl = tmpse.expr;
1715 return tmpse.expr;
1716 }
1717
1718 /* First candidate: if the variable is of type CHARACTER, the
1719 expression's length could be the length of the character
1720 variable. */
1721 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
1722 length = e->symtree->n.sym->ts.u.cl->backend_decl;
1723
1724 /* Look through the reference chain for component references. */
1725 for (r = e->ref; r; r = r->next)
1726 {
1727 switch (r->type)
1728 {
1729 case REF_COMPONENT:
1730 if (r->u.c.component->ts.type == BT_CHARACTER)
1731 length = r->u.c.component->ts.u.cl->backend_decl;
1732 break;
1733
1734 case REF_ARRAY:
1735 /* Do nothing. */
1736 break;
1737
1738 default:
1739 /* We should never got substring references here. These will be
1740 broken down by the scalarizer. */
1741 gcc_unreachable ();
1742 break;
1743 }
1744 }
1745
1746 gcc_assert (length != NULL);
1747 return length;
1748 }
1749
1750
1751 /* Return for an expression the backend decl of the coarray. */
1752
1753 tree
1754 gfc_get_tree_for_caf_expr (gfc_expr *expr)
1755 {
1756 tree caf_decl;
1757 bool found = false;
1758 gfc_ref *ref, *comp_ref = NULL;
1759
1760 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
1761
1762 /* Not-implemented diagnostic. */
1763 for (ref = expr->ref; ref; ref = ref->next)
1764 if (ref->type == REF_COMPONENT)
1765 {
1766 comp_ref = ref;
1767 if ((ref->u.c.component->ts.type == BT_CLASS
1768 && !CLASS_DATA (ref->u.c.component)->attr.codimension
1769 && (CLASS_DATA (ref->u.c.component)->attr.pointer
1770 || CLASS_DATA (ref->u.c.component)->attr.allocatable))
1771 || (ref->u.c.component->ts.type != BT_CLASS
1772 && !ref->u.c.component->attr.codimension
1773 && (ref->u.c.component->attr.pointer
1774 || ref->u.c.component->attr.allocatable)))
1775 gfc_error ("Sorry, coindexed access to a pointer or allocatable "
1776 "component of the coindexed coarray at %L is not yet "
1777 "supported", &expr->where);
1778 }
1779 if ((!comp_ref
1780 && ((expr->symtree->n.sym->ts.type == BT_CLASS
1781 && CLASS_DATA (expr->symtree->n.sym)->attr.alloc_comp)
1782 || (expr->symtree->n.sym->ts.type == BT_DERIVED
1783 && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)))
1784 || (comp_ref
1785 && ((comp_ref->u.c.component->ts.type == BT_CLASS
1786 && CLASS_DATA (comp_ref->u.c.component)->attr.alloc_comp)
1787 || (comp_ref->u.c.component->ts.type == BT_DERIVED
1788 && comp_ref->u.c.component->ts.u.derived->attr.alloc_comp))))
1789 gfc_error ("Sorry, coindexed coarray at %L with allocatable component is "
1790 "not yet supported", &expr->where);
1791
1792 if (expr->rank)
1793 {
1794 /* Without the new array descriptor, access like "caf[i]%a(:)%b" is in
1795 general not possible as the required stride multiplier might be not
1796 a multiple of c_sizeof(b). In case of noncoindexed access, the
1797 scalarizer often takes care of it - for coarrays, it always fails. */
1798 for (ref = expr->ref; ref; ref = ref->next)
1799 if (ref->type == REF_COMPONENT
1800 && ((ref->u.c.component->ts.type == BT_CLASS
1801 && CLASS_DATA (ref->u.c.component)->attr.codimension)
1802 || (ref->u.c.component->ts.type != BT_CLASS
1803 && ref->u.c.component->attr.codimension)))
1804 break;
1805 if (ref == NULL)
1806 ref = expr->ref;
1807 for ( ; ref; ref = ref->next)
1808 if (ref->type == REF_ARRAY && ref->u.ar.dimen)
1809 break;
1810 for ( ; ref; ref = ref->next)
1811 if (ref->type == REF_COMPONENT)
1812 gfc_error ("Sorry, coindexed access at %L to a scalar component "
1813 "with an array partref is not yet supported",
1814 &expr->where);
1815 }
1816
1817 caf_decl = expr->symtree->n.sym->backend_decl;
1818 gcc_assert (caf_decl);
1819 if (expr->symtree->n.sym->ts.type == BT_CLASS)
1820 caf_decl = gfc_class_data_get (caf_decl);
1821 if (expr->symtree->n.sym->attr.codimension)
1822 return caf_decl;
1823
1824 /* The following code assumes that the coarray is a component reachable via
1825 only scalar components/variables; the Fortran standard guarantees this. */
1826
1827 for (ref = expr->ref; ref; ref = ref->next)
1828 if (ref->type == REF_COMPONENT)
1829 {
1830 gfc_component *comp = ref->u.c.component;
1831
1832 if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
1833 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1834 caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
1835 TREE_TYPE (comp->backend_decl), caf_decl,
1836 comp->backend_decl, NULL_TREE);
1837 if (comp->ts.type == BT_CLASS)
1838 caf_decl = gfc_class_data_get (caf_decl);
1839 if (comp->attr.codimension)
1840 {
1841 found = true;
1842 break;
1843 }
1844 }
1845 gcc_assert (found && caf_decl);
1846 return caf_decl;
1847 }
1848
1849
1850 /* Obtain the Coarray token - and optionally also the offset. */
1851
1852 void
1853 gfc_get_caf_token_offset (tree *token, tree *offset, tree caf_decl, tree se_expr,
1854 gfc_expr *expr)
1855 {
1856 tree tmp;
1857
1858 /* Coarray token. */
1859 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
1860 {
1861 gcc_assert (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl))
1862 == GFC_ARRAY_ALLOCATABLE
1863 || expr->symtree->n.sym->attr.select_type_temporary);
1864 *token = gfc_conv_descriptor_token (caf_decl);
1865 }
1866 else if (DECL_LANG_SPECIFIC (caf_decl)
1867 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
1868 *token = GFC_DECL_TOKEN (caf_decl);
1869 else
1870 {
1871 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
1872 && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
1873 *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
1874 }
1875
1876 if (offset == NULL)
1877 return;
1878
1879 /* Offset between the coarray base address and the address wanted. */
1880 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
1881 && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
1882 || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
1883 *offset = build_int_cst (gfc_array_index_type, 0);
1884 else if (DECL_LANG_SPECIFIC (caf_decl)
1885 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
1886 *offset = GFC_DECL_CAF_OFFSET (caf_decl);
1887 else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
1888 *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
1889 else
1890 *offset = build_int_cst (gfc_array_index_type, 0);
1891
1892 if (POINTER_TYPE_P (TREE_TYPE (se_expr))
1893 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
1894 {
1895 tmp = build_fold_indirect_ref_loc (input_location, se_expr);
1896 tmp = gfc_conv_descriptor_data_get (tmp);
1897 }
1898 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
1899 tmp = gfc_conv_descriptor_data_get (se_expr);
1900 else
1901 {
1902 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
1903 tmp = se_expr;
1904 }
1905
1906 *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1907 *offset, fold_convert (gfc_array_index_type, tmp));
1908
1909 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
1910 tmp = gfc_conv_descriptor_data_get (caf_decl);
1911 else
1912 {
1913 gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
1914 tmp = caf_decl;
1915 }
1916
1917 *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1918 fold_convert (gfc_array_index_type, *offset),
1919 fold_convert (gfc_array_index_type, tmp));
1920 }
1921
1922
1923 /* Convert the coindex of a coarray into an image index; the result is
1924 image_num = (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
1925 + (idx(3)-lcobound(3))*extend(1)*extent(2) + ... */
1926
1927 tree
1928 gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
1929 {
1930 gfc_ref *ref;
1931 tree lbound, ubound, extent, tmp, img_idx;
1932 gfc_se se;
1933 int i;
1934
1935 for (ref = e->ref; ref; ref = ref->next)
1936 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
1937 break;
1938 gcc_assert (ref != NULL);
1939
1940 img_idx = integer_zero_node;
1941 extent = integer_one_node;
1942 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
1943 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
1944 {
1945 gfc_init_se (&se, NULL);
1946 gfc_conv_expr_type (&se, ref->u.ar.start[i], integer_type_node);
1947 gfc_add_block_to_block (block, &se.pre);
1948 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
1949 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1950 integer_type_node, se.expr,
1951 fold_convert(integer_type_node, lbound));
1952 tmp = fold_build2_loc (input_location, MULT_EXPR, integer_type_node,
1953 extent, tmp);
1954 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1955 img_idx, tmp);
1956 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
1957 {
1958 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
1959 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1960 tmp = fold_convert (integer_type_node, tmp);
1961 extent = fold_build2_loc (input_location, MULT_EXPR,
1962 integer_type_node, extent, tmp);
1963 }
1964 }
1965 else
1966 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
1967 {
1968 gfc_init_se (&se, NULL);
1969 gfc_conv_expr_type (&se, ref->u.ar.start[i], integer_type_node);
1970 gfc_add_block_to_block (block, &se.pre);
1971 lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
1972 lbound = fold_convert (integer_type_node, lbound);
1973 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1974 integer_type_node, se.expr, lbound);
1975 tmp = fold_build2_loc (input_location, MULT_EXPR, integer_type_node,
1976 extent, tmp);
1977 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1978 img_idx, tmp);
1979 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
1980 {
1981 ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
1982 ubound = fold_convert (integer_type_node, ubound);
1983 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1984 integer_type_node, ubound, lbound);
1985 tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1986 tmp, integer_one_node);
1987 extent = fold_build2_loc (input_location, MULT_EXPR,
1988 integer_type_node, extent, tmp);
1989 }
1990 }
1991 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
1992 img_idx, integer_one_node);
1993 return img_idx;
1994 }
1995
1996
1997 /* For each character array constructor subexpression without a ts.u.cl->length,
1998 replace it by its first element (if there aren't any elements, the length
1999 should already be set to zero). */
2000
2001 static void
2002 flatten_array_ctors_without_strlen (gfc_expr* e)
2003 {
2004 gfc_actual_arglist* arg;
2005 gfc_constructor* c;
2006
2007 if (!e)
2008 return;
2009
2010 switch (e->expr_type)
2011 {
2012
2013 case EXPR_OP:
2014 flatten_array_ctors_without_strlen (e->value.op.op1);
2015 flatten_array_ctors_without_strlen (e->value.op.op2);
2016 break;
2017
2018 case EXPR_COMPCALL:
2019 /* TODO: Implement as with EXPR_FUNCTION when needed. */
2020 gcc_unreachable ();
2021
2022 case EXPR_FUNCTION:
2023 for (arg = e->value.function.actual; arg; arg = arg->next)
2024 flatten_array_ctors_without_strlen (arg->expr);
2025 break;
2026
2027 case EXPR_ARRAY:
2028
2029 /* We've found what we're looking for. */
2030 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
2031 {
2032 gfc_constructor *c;
2033 gfc_expr* new_expr;
2034
2035 gcc_assert (e->value.constructor);
2036
2037 c = gfc_constructor_first (e->value.constructor);
2038 new_expr = c->expr;
2039 c->expr = NULL;
2040
2041 flatten_array_ctors_without_strlen (new_expr);
2042 gfc_replace_expr (e, new_expr);
2043 break;
2044 }
2045
2046 /* Otherwise, fall through to handle constructor elements. */
2047 case EXPR_STRUCTURE:
2048 for (c = gfc_constructor_first (e->value.constructor);
2049 c; c = gfc_constructor_next (c))
2050 flatten_array_ctors_without_strlen (c->expr);
2051 break;
2052
2053 default:
2054 break;
2055
2056 }
2057 }
2058
2059
2060 /* Generate code to initialize a string length variable. Returns the
2061 value. For array constructors, cl->length might be NULL and in this case,
2062 the first element of the constructor is needed. expr is the original
2063 expression so we can access it but can be NULL if this is not needed. */
2064
2065 void
2066 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
2067 {
2068 gfc_se se;
2069
2070 gfc_init_se (&se, NULL);
2071
2072 if (!cl->length
2073 && cl->backend_decl
2074 && TREE_CODE (cl->backend_decl) == VAR_DECL)
2075 return;
2076
2077 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
2078 "flatten" array constructors by taking their first element; all elements
2079 should be the same length or a cl->length should be present. */
2080 if (!cl->length)
2081 {
2082 gfc_expr* expr_flat;
2083 gcc_assert (expr);
2084 expr_flat = gfc_copy_expr (expr);
2085 flatten_array_ctors_without_strlen (expr_flat);
2086 gfc_resolve_expr (expr_flat);
2087
2088 gfc_conv_expr (&se, expr_flat);
2089 gfc_add_block_to_block (pblock, &se.pre);
2090 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
2091
2092 gfc_free_expr (expr_flat);
2093 return;
2094 }
2095
2096 /* Convert cl->length. */
2097
2098 gcc_assert (cl->length);
2099
2100 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
2101 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2102 se.expr, build_int_cst (gfc_charlen_type_node, 0));
2103 gfc_add_block_to_block (pblock, &se.pre);
2104
2105 if (cl->backend_decl)
2106 gfc_add_modify (pblock, cl->backend_decl, se.expr);
2107 else
2108 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
2109 }
2110
2111
2112 static void
2113 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
2114 const char *name, locus *where)
2115 {
2116 tree tmp;
2117 tree type;
2118 tree fault;
2119 gfc_se start;
2120 gfc_se end;
2121 char *msg;
2122 mpz_t length;
2123
2124 type = gfc_get_character_type (kind, ref->u.ss.length);
2125 type = build_pointer_type (type);
2126
2127 gfc_init_se (&start, se);
2128 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
2129 gfc_add_block_to_block (&se->pre, &start.pre);
2130
2131 if (integer_onep (start.expr))
2132 gfc_conv_string_parameter (se);
2133 else
2134 {
2135 tmp = start.expr;
2136 STRIP_NOPS (tmp);
2137 /* Avoid multiple evaluation of substring start. */
2138 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2139 start.expr = gfc_evaluate_now (start.expr, &se->pre);
2140
2141 /* Change the start of the string. */
2142 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
2143 tmp = se->expr;
2144 else
2145 tmp = build_fold_indirect_ref_loc (input_location,
2146 se->expr);
2147 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
2148 se->expr = gfc_build_addr_expr (type, tmp);
2149 }
2150
2151 /* Length = end + 1 - start. */
2152 gfc_init_se (&end, se);
2153 if (ref->u.ss.end == NULL)
2154 end.expr = se->string_length;
2155 else
2156 {
2157 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
2158 gfc_add_block_to_block (&se->pre, &end.pre);
2159 }
2160 tmp = end.expr;
2161 STRIP_NOPS (tmp);
2162 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2163 end.expr = gfc_evaluate_now (end.expr, &se->pre);
2164
2165 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2166 {
2167 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
2168 boolean_type_node, start.expr,
2169 end.expr);
2170
2171 /* Check lower bound. */
2172 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
2173 start.expr,
2174 build_int_cst (gfc_charlen_type_node, 1));
2175 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2176 boolean_type_node, nonempty, fault);
2177 if (name)
2178 msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
2179 "is less than one", name);
2180 else
2181 msg = xasprintf ("Substring out of bounds: lower bound (%%ld)"
2182 "is less than one");
2183 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2184 fold_convert (long_integer_type_node,
2185 start.expr));
2186 free (msg);
2187
2188 /* Check upper bound. */
2189 fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
2190 end.expr, se->string_length);
2191 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2192 boolean_type_node, nonempty, fault);
2193 if (name)
2194 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
2195 "exceeds string length (%%ld)", name);
2196 else
2197 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
2198 "exceeds string length (%%ld)");
2199 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2200 fold_convert (long_integer_type_node, end.expr),
2201 fold_convert (long_integer_type_node,
2202 se->string_length));
2203 free (msg);
2204 }
2205
2206 /* Try to calculate the length from the start and end expressions. */
2207 if (ref->u.ss.end
2208 && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
2209 {
2210 int i_len;
2211
2212 i_len = mpz_get_si (length) + 1;
2213 if (i_len < 0)
2214 i_len = 0;
2215
2216 tmp = build_int_cst (gfc_charlen_type_node, i_len);
2217 mpz_clear (length); /* Was initialized by gfc_dep_difference. */
2218 }
2219 else
2220 {
2221 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
2222 end.expr, start.expr);
2223 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
2224 build_int_cst (gfc_charlen_type_node, 1), tmp);
2225 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2226 tmp, build_int_cst (gfc_charlen_type_node, 0));
2227 }
2228
2229 se->string_length = tmp;
2230 }
2231
2232
2233 /* Convert a derived type component reference. */
2234
2235 static void
2236 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
2237 {
2238 gfc_component *c;
2239 tree tmp;
2240 tree decl;
2241 tree field;
2242
2243 c = ref->u.c.component;
2244
2245 if (c->backend_decl == NULL_TREE
2246 && ref->u.c.sym != NULL)
2247 gfc_get_derived_type (ref->u.c.sym);
2248
2249 field = c->backend_decl;
2250 gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
2251 decl = se->expr;
2252
2253 /* Components can correspond to fields of different containing
2254 types, as components are created without context, whereas
2255 a concrete use of a component has the type of decl as context.
2256 So, if the type doesn't match, we search the corresponding
2257 FIELD_DECL in the parent type. To not waste too much time
2258 we cache this result in norestrict_decl. */
2259
2260 if (DECL_FIELD_CONTEXT (field) != TREE_TYPE (decl))
2261 {
2262 tree f2 = c->norestrict_decl;
2263 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
2264 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
2265 if (TREE_CODE (f2) == FIELD_DECL
2266 && DECL_NAME (f2) == DECL_NAME (field))
2267 break;
2268 gcc_assert (f2);
2269 c->norestrict_decl = f2;
2270 field = f2;
2271 }
2272
2273 if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
2274 && strcmp ("_data", c->name) == 0)
2275 {
2276 /* Found a ref to the _data component. Store the associated ref to
2277 the vptr in se->class_vptr. */
2278 se->class_vptr = gfc_class_vptr_get (decl);
2279 }
2280 else
2281 se->class_vptr = NULL_TREE;
2282
2283 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
2284 decl, field, NULL_TREE);
2285
2286 se->expr = tmp;
2287
2288 /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
2289 strlen () conditional below. */
2290 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
2291 && !(c->attr.allocatable && c->ts.deferred))
2292 {
2293 tmp = c->ts.u.cl->backend_decl;
2294 /* Components must always be constant length. */
2295 gcc_assert (tmp && INTEGER_CST_P (tmp));
2296 se->string_length = tmp;
2297 }
2298
2299 if (gfc_deferred_strlen (c, &field))
2300 {
2301 tmp = fold_build3_loc (input_location, COMPONENT_REF,
2302 TREE_TYPE (field),
2303 decl, field, NULL_TREE);
2304 se->string_length = tmp;
2305 }
2306
2307 if (((c->attr.pointer || c->attr.allocatable)
2308 && (!c->attr.dimension && !c->attr.codimension)
2309 && c->ts.type != BT_CHARACTER)
2310 || c->attr.proc_pointer)
2311 se->expr = build_fold_indirect_ref_loc (input_location,
2312 se->expr);
2313 }
2314
2315
2316 /* This function deals with component references to components of the
2317 parent type for derived type extensions. */
2318 static void
2319 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
2320 {
2321 gfc_component *c;
2322 gfc_component *cmp;
2323 gfc_symbol *dt;
2324 gfc_ref parent;
2325
2326 dt = ref->u.c.sym;
2327 c = ref->u.c.component;
2328
2329 /* Return if the component is in the parent type. */
2330 for (cmp = dt->components; cmp; cmp = cmp->next)
2331 if (strcmp (c->name, cmp->name) == 0)
2332 return;
2333
2334 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
2335 parent.type = REF_COMPONENT;
2336 parent.next = NULL;
2337 parent.u.c.sym = dt;
2338 parent.u.c.component = dt->components;
2339
2340 if (dt->backend_decl == NULL)
2341 gfc_get_derived_type (dt);
2342
2343 /* Build the reference and call self. */
2344 gfc_conv_component_ref (se, &parent);
2345 parent.u.c.sym = dt->components->ts.u.derived;
2346 parent.u.c.component = c;
2347 conv_parent_component_references (se, &parent);
2348 }
2349
2350 /* Return the contents of a variable. Also handles reference/pointer
2351 variables (all Fortran pointer references are implicit). */
2352
2353 static void
2354 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
2355 {
2356 gfc_ss *ss;
2357 gfc_ref *ref;
2358 gfc_symbol *sym;
2359 tree parent_decl = NULL_TREE;
2360 int parent_flag;
2361 bool return_value;
2362 bool alternate_entry;
2363 bool entry_master;
2364 bool is_classarray;
2365 bool first_time = true;
2366
2367 sym = expr->symtree->n.sym;
2368 is_classarray = IS_CLASS_ARRAY (sym);
2369 ss = se->ss;
2370 if (ss != NULL)
2371 {
2372 gfc_ss_info *ss_info = ss->info;
2373
2374 /* Check that something hasn't gone horribly wrong. */
2375 gcc_assert (ss != gfc_ss_terminator);
2376 gcc_assert (ss_info->expr == expr);
2377
2378 /* A scalarized term. We already know the descriptor. */
2379 se->expr = ss_info->data.array.descriptor;
2380 se->string_length = ss_info->string_length;
2381 ref = ss_info->data.array.ref;
2382 if (ref)
2383 gcc_assert (ref->type == REF_ARRAY
2384 && ref->u.ar.type != AR_ELEMENT);
2385 else
2386 gfc_conv_tmp_array_ref (se);
2387 }
2388 else
2389 {
2390 tree se_expr = NULL_TREE;
2391
2392 se->expr = gfc_get_symbol_decl (sym);
2393
2394 /* Deal with references to a parent results or entries by storing
2395 the current_function_decl and moving to the parent_decl. */
2396 return_value = sym->attr.function && sym->result == sym;
2397 alternate_entry = sym->attr.function && sym->attr.entry
2398 && sym->result == sym;
2399 entry_master = sym->attr.result
2400 && sym->ns->proc_name->attr.entry_master
2401 && !gfc_return_by_reference (sym->ns->proc_name);
2402 if (current_function_decl)
2403 parent_decl = DECL_CONTEXT (current_function_decl);
2404
2405 if ((se->expr == parent_decl && return_value)
2406 || (sym->ns && sym->ns->proc_name
2407 && parent_decl
2408 && sym->ns->proc_name->backend_decl == parent_decl
2409 && (alternate_entry || entry_master)))
2410 parent_flag = 1;
2411 else
2412 parent_flag = 0;
2413
2414 /* Special case for assigning the return value of a function.
2415 Self recursive functions must have an explicit return value. */
2416 if (return_value && (se->expr == current_function_decl || parent_flag))
2417 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2418
2419 /* Similarly for alternate entry points. */
2420 else if (alternate_entry
2421 && (sym->ns->proc_name->backend_decl == current_function_decl
2422 || parent_flag))
2423 {
2424 gfc_entry_list *el = NULL;
2425
2426 for (el = sym->ns->entries; el; el = el->next)
2427 if (sym == el->sym)
2428 {
2429 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2430 break;
2431 }
2432 }
2433
2434 else if (entry_master
2435 && (sym->ns->proc_name->backend_decl == current_function_decl
2436 || parent_flag))
2437 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2438
2439 if (se_expr)
2440 se->expr = se_expr;
2441
2442 /* Procedure actual arguments. */
2443 else if (sym->attr.flavor == FL_PROCEDURE
2444 && se->expr != current_function_decl)
2445 {
2446 if (!sym->attr.dummy && !sym->attr.proc_pointer)
2447 {
2448 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
2449 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
2450 }
2451 return;
2452 }
2453
2454
2455 /* Dereference the expression, where needed. Since characters
2456 are entirely different from other types, they are treated
2457 separately. */
2458 if (sym->ts.type == BT_CHARACTER)
2459 {
2460 /* Dereference character pointer dummy arguments
2461 or results. */
2462 if ((sym->attr.pointer || sym->attr.allocatable)
2463 && (sym->attr.dummy
2464 || sym->attr.function
2465 || sym->attr.result))
2466 se->expr = build_fold_indirect_ref_loc (input_location,
2467 se->expr);
2468
2469 }
2470 else if (!sym->attr.value)
2471 {
2472 /* Dereference temporaries for class array dummy arguments. */
2473 if (sym->attr.dummy && is_classarray
2474 && GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr)))
2475 {
2476 if (!se->descriptor_only)
2477 se->expr = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
2478
2479 se->expr = build_fold_indirect_ref_loc (input_location,
2480 se->expr);
2481 }
2482
2483 /* Dereference non-character scalar dummy arguments. */
2484 if (sym->attr.dummy && !sym->attr.dimension
2485 && !(sym->attr.codimension && sym->attr.allocatable)
2486 && (sym->ts.type != BT_CLASS
2487 || (!CLASS_DATA (sym)->attr.dimension
2488 && !(CLASS_DATA (sym)->attr.codimension
2489 && CLASS_DATA (sym)->attr.allocatable))))
2490 se->expr = build_fold_indirect_ref_loc (input_location,
2491 se->expr);
2492
2493 /* Dereference scalar hidden result. */
2494 if (flag_f2c && sym->ts.type == BT_COMPLEX
2495 && (sym->attr.function || sym->attr.result)
2496 && !sym->attr.dimension && !sym->attr.pointer
2497 && !sym->attr.always_explicit)
2498 se->expr = build_fold_indirect_ref_loc (input_location,
2499 se->expr);
2500
2501 /* Dereference non-character, non-class pointer variables.
2502 These must be dummies, results, or scalars. */
2503 if (!is_classarray
2504 && (sym->attr.pointer || sym->attr.allocatable
2505 || gfc_is_associate_pointer (sym)
2506 || (sym->as && sym->as->type == AS_ASSUMED_RANK))
2507 && (sym->attr.dummy
2508 || sym->attr.function
2509 || sym->attr.result
2510 || (!sym->attr.dimension
2511 && (!sym->attr.codimension || !sym->attr.allocatable))))
2512 se->expr = build_fold_indirect_ref_loc (input_location,
2513 se->expr);
2514 /* Now treat the class array pointer variables accordingly. */
2515 else if (sym->ts.type == BT_CLASS
2516 && sym->attr.dummy
2517 && (CLASS_DATA (sym)->attr.dimension
2518 || CLASS_DATA (sym)->attr.codimension)
2519 && ((CLASS_DATA (sym)->as
2520 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
2521 || CLASS_DATA (sym)->attr.allocatable
2522 || CLASS_DATA (sym)->attr.class_pointer))
2523 se->expr = build_fold_indirect_ref_loc (input_location,
2524 se->expr);
2525 /* And the case where a non-dummy, non-result, non-function,
2526 non-allotable and non-pointer classarray is present. This case was
2527 previously covered by the first if, but with introducing the
2528 condition !is_classarray there, that case has to be covered
2529 explicitly. */
2530 else if (sym->ts.type == BT_CLASS
2531 && !sym->attr.dummy
2532 && !sym->attr.function
2533 && !sym->attr.result
2534 && (CLASS_DATA (sym)->attr.dimension
2535 || CLASS_DATA (sym)->attr.codimension)
2536 && !CLASS_DATA (sym)->attr.allocatable
2537 && !CLASS_DATA (sym)->attr.class_pointer)
2538 se->expr = build_fold_indirect_ref_loc (input_location,
2539 se->expr);
2540 }
2541
2542 ref = expr->ref;
2543 }
2544
2545 /* For character variables, also get the length. */
2546 if (sym->ts.type == BT_CHARACTER)
2547 {
2548 /* If the character length of an entry isn't set, get the length from
2549 the master function instead. */
2550 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
2551 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
2552 else
2553 se->string_length = sym->ts.u.cl->backend_decl;
2554 gcc_assert (se->string_length);
2555 }
2556
2557 while (ref)
2558 {
2559 switch (ref->type)
2560 {
2561 case REF_ARRAY:
2562 /* Return the descriptor if that's what we want and this is an array
2563 section reference. */
2564 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
2565 return;
2566 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
2567 /* Return the descriptor for array pointers and allocations. */
2568 if (se->want_pointer
2569 && ref->next == NULL && (se->descriptor_only))
2570 return;
2571
2572 gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
2573 /* Return a pointer to an element. */
2574 break;
2575
2576 case REF_COMPONENT:
2577 if (first_time && is_classarray && sym->attr.dummy
2578 && se->descriptor_only
2579 && !CLASS_DATA (sym)->attr.allocatable
2580 && !CLASS_DATA (sym)->attr.class_pointer
2581 && CLASS_DATA (sym)->as
2582 && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
2583 && strcmp ("_data", ref->u.c.component->name) == 0)
2584 /* Skip the first ref of a _data component, because for class
2585 arrays that one is already done by introducing a temporary
2586 array descriptor. */
2587 break;
2588
2589 if (ref->u.c.sym->attr.extension)
2590 conv_parent_component_references (se, ref);
2591
2592 gfc_conv_component_ref (se, ref);
2593 if (!ref->next && ref->u.c.sym->attr.codimension
2594 && se->want_pointer && se->descriptor_only)
2595 return;
2596
2597 break;
2598
2599 case REF_SUBSTRING:
2600 gfc_conv_substring (se, ref, expr->ts.kind,
2601 expr->symtree->name, &expr->where);
2602 break;
2603
2604 default:
2605 gcc_unreachable ();
2606 break;
2607 }
2608 first_time = false;
2609 ref = ref->next;
2610 }
2611 /* Pointer assignment, allocation or pass by reference. Arrays are handled
2612 separately. */
2613 if (se->want_pointer)
2614 {
2615 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
2616 gfc_conv_string_parameter (se);
2617 else
2618 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
2619 }
2620 }
2621
2622
2623 /* Unary ops are easy... Or they would be if ! was a valid op. */
2624
2625 static void
2626 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
2627 {
2628 gfc_se operand;
2629 tree type;
2630
2631 gcc_assert (expr->ts.type != BT_CHARACTER);
2632 /* Initialize the operand. */
2633 gfc_init_se (&operand, se);
2634 gfc_conv_expr_val (&operand, expr->value.op.op1);
2635 gfc_add_block_to_block (&se->pre, &operand.pre);
2636
2637 type = gfc_typenode_for_spec (&expr->ts);
2638
2639 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
2640 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
2641 All other unary operators have an equivalent GIMPLE unary operator. */
2642 if (code == TRUTH_NOT_EXPR)
2643 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
2644 build_int_cst (type, 0));
2645 else
2646 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
2647
2648 }
2649
2650 /* Expand power operator to optimal multiplications when a value is raised
2651 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
2652 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
2653 Programming", 3rd Edition, 1998. */
2654
2655 /* This code is mostly duplicated from expand_powi in the backend.
2656 We establish the "optimal power tree" lookup table with the defined size.
2657 The items in the table are the exponents used to calculate the index
2658 exponents. Any integer n less than the value can get an "addition chain",
2659 with the first node being one. */
2660 #define POWI_TABLE_SIZE 256
2661
2662 /* The table is from builtins.c. */
2663 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2664 {
2665 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2666 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2667 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2668 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2669 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2670 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2671 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2672 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2673 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2674 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2675 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2676 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2677 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2678 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2679 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2680 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2681 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2682 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2683 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2684 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2685 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2686 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2687 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2688 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2689 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2690 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2691 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2692 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2693 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2694 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2695 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2696 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2697 };
2698
2699 /* If n is larger than lookup table's max index, we use the "window
2700 method". */
2701 #define POWI_WINDOW_SIZE 3
2702
2703 /* Recursive function to expand the power operator. The temporary
2704 values are put in tmpvar. The function returns tmpvar[1] ** n. */
2705 static tree
2706 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
2707 {
2708 tree op0;
2709 tree op1;
2710 tree tmp;
2711 int digit;
2712
2713 if (n < POWI_TABLE_SIZE)
2714 {
2715 if (tmpvar[n])
2716 return tmpvar[n];
2717
2718 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
2719 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
2720 }
2721 else if (n & 1)
2722 {
2723 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2724 op0 = gfc_conv_powi (se, n - digit, tmpvar);
2725 op1 = gfc_conv_powi (se, digit, tmpvar);
2726 }
2727 else
2728 {
2729 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
2730 op1 = op0;
2731 }
2732
2733 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
2734 tmp = gfc_evaluate_now (tmp, &se->pre);
2735
2736 if (n < POWI_TABLE_SIZE)
2737 tmpvar[n] = tmp;
2738
2739 return tmp;
2740 }
2741
2742
2743 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
2744 return 1. Else return 0 and a call to runtime library functions
2745 will have to be built. */
2746 static int
2747 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
2748 {
2749 tree cond;
2750 tree tmp;
2751 tree type;
2752 tree vartmp[POWI_TABLE_SIZE];
2753 HOST_WIDE_INT m;
2754 unsigned HOST_WIDE_INT n;
2755 int sgn;
2756 wide_int wrhs = rhs;
2757
2758 /* If exponent is too large, we won't expand it anyway, so don't bother
2759 with large integer values. */
2760 if (!wi::fits_shwi_p (wrhs))
2761 return 0;
2762
2763 m = wrhs.to_shwi ();
2764 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
2765 of the asymmetric range of the integer type. */
2766 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
2767
2768 type = TREE_TYPE (lhs);
2769 sgn = tree_int_cst_sgn (rhs);
2770
2771 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
2772 || optimize_size) && (m > 2 || m < -1))
2773 return 0;
2774
2775 /* rhs == 0 */
2776 if (sgn == 0)
2777 {
2778 se->expr = gfc_build_const (type, integer_one_node);
2779 return 1;
2780 }
2781
2782 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
2783 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
2784 {
2785 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2786 lhs, build_int_cst (TREE_TYPE (lhs), -1));
2787 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2788 lhs, build_int_cst (TREE_TYPE (lhs), 1));
2789
2790 /* If rhs is even,
2791 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
2792 if ((n & 1) == 0)
2793 {
2794 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2795 boolean_type_node, tmp, cond);
2796 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2797 tmp, build_int_cst (type, 1),
2798 build_int_cst (type, 0));
2799 return 1;
2800 }
2801 /* If rhs is odd,
2802 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
2803 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
2804 build_int_cst (type, -1),
2805 build_int_cst (type, 0));
2806 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2807 cond, build_int_cst (type, 1), tmp);
2808 return 1;
2809 }
2810
2811 memset (vartmp, 0, sizeof (vartmp));
2812 vartmp[1] = lhs;
2813 if (sgn == -1)
2814 {
2815 tmp = gfc_build_const (type, integer_one_node);
2816 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
2817 vartmp[1]);
2818 }
2819
2820 se->expr = gfc_conv_powi (se, n, vartmp);
2821
2822 return 1;
2823 }
2824
2825
2826 /* Power op (**). Constant integer exponent has special handling. */
2827
2828 static void
2829 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
2830 {
2831 tree gfc_int4_type_node;
2832 int kind;
2833 int ikind;
2834 int res_ikind_1, res_ikind_2;
2835 gfc_se lse;
2836 gfc_se rse;
2837 tree fndecl = NULL;
2838
2839 gfc_init_se (&lse, se);
2840 gfc_conv_expr_val (&lse, expr->value.op.op1);
2841 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
2842 gfc_add_block_to_block (&se->pre, &lse.pre);
2843
2844 gfc_init_se (&rse, se);
2845 gfc_conv_expr_val (&rse, expr->value.op.op2);
2846 gfc_add_block_to_block (&se->pre, &rse.pre);
2847
2848 if (expr->value.op.op2->ts.type == BT_INTEGER
2849 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
2850 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
2851 return;
2852
2853 gfc_int4_type_node = gfc_get_int_type (4);
2854
2855 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
2856 library routine. But in the end, we have to convert the result back
2857 if this case applies -- with res_ikind_K, we keep track whether operand K
2858 falls into this case. */
2859 res_ikind_1 = -1;
2860 res_ikind_2 = -1;
2861
2862 kind = expr->value.op.op1->ts.kind;
2863 switch (expr->value.op.op2->ts.type)
2864 {
2865 case BT_INTEGER:
2866 ikind = expr->value.op.op2->ts.kind;
2867 switch (ikind)
2868 {
2869 case 1:
2870 case 2:
2871 rse.expr = convert (gfc_int4_type_node, rse.expr);
2872 res_ikind_2 = ikind;
2873 /* Fall through. */
2874
2875 case 4:
2876 ikind = 0;
2877 break;
2878
2879 case 8:
2880 ikind = 1;
2881 break;
2882
2883 case 16:
2884 ikind = 2;
2885 break;
2886
2887 default:
2888 gcc_unreachable ();
2889 }
2890 switch (kind)
2891 {
2892 case 1:
2893 case 2:
2894 if (expr->value.op.op1->ts.type == BT_INTEGER)
2895 {
2896 lse.expr = convert (gfc_int4_type_node, lse.expr);
2897 res_ikind_1 = kind;
2898 }
2899 else
2900 gcc_unreachable ();
2901 /* Fall through. */
2902
2903 case 4:
2904 kind = 0;
2905 break;
2906
2907 case 8:
2908 kind = 1;
2909 break;
2910
2911 case 10:
2912 kind = 2;
2913 break;
2914
2915 case 16:
2916 kind = 3;
2917 break;
2918
2919 default:
2920 gcc_unreachable ();
2921 }
2922
2923 switch (expr->value.op.op1->ts.type)
2924 {
2925 case BT_INTEGER:
2926 if (kind == 3) /* Case 16 was not handled properly above. */
2927 kind = 2;
2928 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
2929 break;
2930
2931 case BT_REAL:
2932 /* Use builtins for real ** int4. */
2933 if (ikind == 0)
2934 {
2935 switch (kind)
2936 {
2937 case 0:
2938 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
2939 break;
2940
2941 case 1:
2942 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
2943 break;
2944
2945 case 2:
2946 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
2947 break;
2948
2949 case 3:
2950 /* Use the __builtin_powil() only if real(kind=16) is
2951 actually the C long double type. */
2952 if (!gfc_real16_is_float128)
2953 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
2954 break;
2955
2956 default:
2957 gcc_unreachable ();
2958 }
2959 }
2960
2961 /* If we don't have a good builtin for this, go for the
2962 library function. */
2963 if (!fndecl)
2964 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
2965 break;
2966
2967 case BT_COMPLEX:
2968 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
2969 break;
2970
2971 default:
2972 gcc_unreachable ();
2973 }
2974 break;
2975
2976 case BT_REAL:
2977 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
2978 break;
2979
2980 case BT_COMPLEX:
2981 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
2982 break;
2983
2984 default:
2985 gcc_unreachable ();
2986 break;
2987 }
2988
2989 se->expr = build_call_expr_loc (input_location,
2990 fndecl, 2, lse.expr, rse.expr);
2991
2992 /* Convert the result back if it is of wrong integer kind. */
2993 if (res_ikind_1 != -1 && res_ikind_2 != -1)
2994 {
2995 /* We want the maximum of both operand kinds as result. */
2996 if (res_ikind_1 < res_ikind_2)
2997 res_ikind_1 = res_ikind_2;
2998 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
2999 }
3000 }
3001
3002
3003 /* Generate code to allocate a string temporary. */
3004
3005 tree
3006 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
3007 {
3008 tree var;
3009 tree tmp;
3010
3011 if (gfc_can_put_var_on_stack (len))
3012 {
3013 /* Create a temporary variable to hold the result. */
3014 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3015 gfc_charlen_type_node, len,
3016 build_int_cst (gfc_charlen_type_node, 1));
3017 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
3018
3019 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
3020 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
3021 else
3022 tmp = build_array_type (TREE_TYPE (type), tmp);
3023
3024 var = gfc_create_var (tmp, "str");
3025 var = gfc_build_addr_expr (type, var);
3026 }
3027 else
3028 {
3029 /* Allocate a temporary to hold the result. */
3030 var = gfc_create_var (type, "pstr");
3031 gcc_assert (POINTER_TYPE_P (type));
3032 tmp = TREE_TYPE (type);
3033 if (TREE_CODE (tmp) == ARRAY_TYPE)
3034 tmp = TREE_TYPE (tmp);
3035 tmp = TYPE_SIZE_UNIT (tmp);
3036 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
3037 fold_convert (size_type_node, len),
3038 fold_convert (size_type_node, tmp));
3039 tmp = gfc_call_malloc (&se->pre, type, tmp);
3040 gfc_add_modify (&se->pre, var, tmp);
3041
3042 /* Free the temporary afterwards. */
3043 tmp = gfc_call_free (convert (pvoid_type_node, var));
3044 gfc_add_expr_to_block (&se->post, tmp);
3045 }
3046
3047 return var;
3048 }
3049
3050
3051 /* Handle a string concatenation operation. A temporary will be allocated to
3052 hold the result. */
3053
3054 static void
3055 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
3056 {
3057 gfc_se lse, rse;
3058 tree len, type, var, tmp, fndecl;
3059
3060 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
3061 && expr->value.op.op2->ts.type == BT_CHARACTER);
3062 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
3063
3064 gfc_init_se (&lse, se);
3065 gfc_conv_expr (&lse, expr->value.op.op1);
3066 gfc_conv_string_parameter (&lse);
3067 gfc_init_se (&rse, se);
3068 gfc_conv_expr (&rse, expr->value.op.op2);
3069 gfc_conv_string_parameter (&rse);
3070
3071 gfc_add_block_to_block (&se->pre, &lse.pre);
3072 gfc_add_block_to_block (&se->pre, &rse.pre);
3073
3074 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
3075 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3076 if (len == NULL_TREE)
3077 {
3078 len = fold_build2_loc (input_location, PLUS_EXPR,
3079 TREE_TYPE (lse.string_length),
3080 lse.string_length, rse.string_length);
3081 }
3082
3083 type = build_pointer_type (type);
3084
3085 var = gfc_conv_string_tmp (se, type, len);
3086
3087 /* Do the actual concatenation. */
3088 if (expr->ts.kind == 1)
3089 fndecl = gfor_fndecl_concat_string;
3090 else if (expr->ts.kind == 4)
3091 fndecl = gfor_fndecl_concat_string_char4;
3092 else
3093 gcc_unreachable ();
3094
3095 tmp = build_call_expr_loc (input_location,
3096 fndecl, 6, len, var, lse.string_length, lse.expr,
3097 rse.string_length, rse.expr);
3098 gfc_add_expr_to_block (&se->pre, tmp);
3099
3100 /* Add the cleanup for the operands. */
3101 gfc_add_block_to_block (&se->pre, &rse.post);
3102 gfc_add_block_to_block (&se->pre, &lse.post);
3103
3104 se->expr = var;
3105 se->string_length = len;
3106 }
3107
3108 /* Translates an op expression. Common (binary) cases are handled by this
3109 function, others are passed on. Recursion is used in either case.
3110 We use the fact that (op1.ts == op2.ts) (except for the power
3111 operator **).
3112 Operators need no special handling for scalarized expressions as long as
3113 they call gfc_conv_simple_val to get their operands.
3114 Character strings get special handling. */
3115
3116 static void
3117 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
3118 {
3119 enum tree_code code;
3120 gfc_se lse;
3121 gfc_se rse;
3122 tree tmp, type;
3123 int lop;
3124 int checkstring;
3125
3126 checkstring = 0;
3127 lop = 0;
3128 switch (expr->value.op.op)
3129 {
3130 case INTRINSIC_PARENTHESES:
3131 if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
3132 && flag_protect_parens)
3133 {
3134 gfc_conv_unary_op (PAREN_EXPR, se, expr);
3135 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
3136 return;
3137 }
3138
3139 /* Fallthrough. */
3140 case INTRINSIC_UPLUS:
3141 gfc_conv_expr (se, expr->value.op.op1);
3142 return;
3143
3144 case INTRINSIC_UMINUS:
3145 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
3146 return;
3147
3148 case INTRINSIC_NOT:
3149 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
3150 return;
3151
3152 case INTRINSIC_PLUS:
3153 code = PLUS_EXPR;
3154 break;
3155
3156 case INTRINSIC_MINUS:
3157 code = MINUS_EXPR;
3158 break;
3159
3160 case INTRINSIC_TIMES:
3161 code = MULT_EXPR;
3162 break;
3163
3164 case INTRINSIC_DIVIDE:
3165 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
3166 an integer, we must round towards zero, so we use a
3167 TRUNC_DIV_EXPR. */
3168 if (expr->ts.type == BT_INTEGER)
3169 code = TRUNC_DIV_EXPR;
3170 else
3171 code = RDIV_EXPR;
3172 break;
3173
3174 case INTRINSIC_POWER:
3175 gfc_conv_power_op (se, expr);
3176 return;
3177
3178 case INTRINSIC_CONCAT:
3179 gfc_conv_concat_op (se, expr);
3180 return;
3181
3182 case INTRINSIC_AND:
3183 code = TRUTH_ANDIF_EXPR;
3184 lop = 1;
3185 break;
3186
3187 case INTRINSIC_OR:
3188 code = TRUTH_ORIF_EXPR;
3189 lop = 1;
3190 break;
3191
3192 /* EQV and NEQV only work on logicals, but since we represent them
3193 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
3194 case INTRINSIC_EQ:
3195 case INTRINSIC_EQ_OS:
3196 case INTRINSIC_EQV:
3197 code = EQ_EXPR;
3198 checkstring = 1;
3199 lop = 1;
3200 break;
3201
3202 case INTRINSIC_NE:
3203 case INTRINSIC_NE_OS:
3204 case INTRINSIC_NEQV:
3205 code = NE_EXPR;
3206 checkstring = 1;
3207 lop = 1;
3208 break;
3209
3210 case INTRINSIC_GT:
3211 case INTRINSIC_GT_OS:
3212 code = GT_EXPR;
3213 checkstring = 1;
3214 lop = 1;
3215 break;
3216
3217 case INTRINSIC_GE:
3218 case INTRINSIC_GE_OS:
3219 code = GE_EXPR;
3220 checkstring = 1;
3221 lop = 1;
3222 break;
3223
3224 case INTRINSIC_LT:
3225 case INTRINSIC_LT_OS:
3226 code = LT_EXPR;
3227 checkstring = 1;
3228 lop = 1;
3229 break;
3230
3231 case INTRINSIC_LE:
3232 case INTRINSIC_LE_OS:
3233 code = LE_EXPR;
3234 checkstring = 1;
3235 lop = 1;
3236 break;
3237
3238 case INTRINSIC_USER:
3239 case INTRINSIC_ASSIGN:
3240 /* These should be converted into function calls by the frontend. */
3241 gcc_unreachable ();
3242
3243 default:
3244 fatal_error (input_location, "Unknown intrinsic op");
3245 return;
3246 }
3247
3248 /* The only exception to this is **, which is handled separately anyway. */
3249 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
3250
3251 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
3252 checkstring = 0;
3253
3254 /* lhs */
3255 gfc_init_se (&lse, se);
3256 gfc_conv_expr (&lse, expr->value.op.op1);
3257 gfc_add_block_to_block (&se->pre, &lse.pre);
3258
3259 /* rhs */
3260 gfc_init_se (&rse, se);
3261 gfc_conv_expr (&rse, expr->value.op.op2);
3262 gfc_add_block_to_block (&se->pre, &rse.pre);
3263
3264 if (checkstring)
3265 {
3266 gfc_conv_string_parameter (&lse);
3267 gfc_conv_string_parameter (&rse);
3268
3269 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
3270 rse.string_length, rse.expr,
3271 expr->value.op.op1->ts.kind,
3272 code);
3273 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
3274 gfc_add_block_to_block (&lse.post, &rse.post);
3275 }
3276
3277 type = gfc_typenode_for_spec (&expr->ts);
3278
3279 if (lop)
3280 {
3281 /* The result of logical ops is always boolean_type_node. */
3282 tmp = fold_build2_loc (input_location, code, boolean_type_node,
3283 lse.expr, rse.expr);
3284 se->expr = convert (type, tmp);
3285 }
3286 else
3287 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
3288
3289 /* Add the post blocks. */
3290 gfc_add_block_to_block (&se->post, &rse.post);
3291 gfc_add_block_to_block (&se->post, &lse.post);
3292 }
3293
3294 /* If a string's length is one, we convert it to a single character. */
3295
3296 tree
3297 gfc_string_to_single_character (tree len, tree str, int kind)
3298 {
3299
3300 if (len == NULL
3301 || !tree_fits_uhwi_p (len)
3302 || !POINTER_TYPE_P (TREE_TYPE (str)))
3303 return NULL_TREE;
3304
3305 if (TREE_INT_CST_LOW (len) == 1)
3306 {
3307 str = fold_convert (gfc_get_pchar_type (kind), str);
3308 return build_fold_indirect_ref_loc (input_location, str);
3309 }
3310
3311 if (kind == 1
3312 && TREE_CODE (str) == ADDR_EXPR
3313 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
3314 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
3315 && array_ref_low_bound (TREE_OPERAND (str, 0))
3316 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
3317 && TREE_INT_CST_LOW (len) > 1
3318 && TREE_INT_CST_LOW (len)
3319 == (unsigned HOST_WIDE_INT)
3320 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
3321 {
3322 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
3323 ret = build_fold_indirect_ref_loc (input_location, ret);
3324 if (TREE_CODE (ret) == INTEGER_CST)
3325 {
3326 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
3327 int i, length = TREE_STRING_LENGTH (string_cst);
3328 const char *ptr = TREE_STRING_POINTER (string_cst);
3329
3330 for (i = 1; i < length; i++)
3331 if (ptr[i] != ' ')
3332 return NULL_TREE;
3333
3334 return ret;
3335 }
3336 }
3337
3338 return NULL_TREE;
3339 }
3340
3341
3342 void
3343 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
3344 {
3345
3346 if (sym->backend_decl)
3347 {
3348 /* This becomes the nominal_type in
3349 function.c:assign_parm_find_data_types. */
3350 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
3351 /* This becomes the passed_type in
3352 function.c:assign_parm_find_data_types. C promotes char to
3353 integer for argument passing. */
3354 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
3355
3356 DECL_BY_REFERENCE (sym->backend_decl) = 0;
3357 }
3358
3359 if (expr != NULL)
3360 {
3361 /* If we have a constant character expression, make it into an
3362 integer. */
3363 if ((*expr)->expr_type == EXPR_CONSTANT)
3364 {
3365 gfc_typespec ts;
3366 gfc_clear_ts (&ts);
3367
3368 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
3369 (int)(*expr)->value.character.string[0]);
3370 if ((*expr)->ts.kind != gfc_c_int_kind)
3371 {
3372 /* The expr needs to be compatible with a C int. If the
3373 conversion fails, then the 2 causes an ICE. */
3374 ts.type = BT_INTEGER;
3375 ts.kind = gfc_c_int_kind;
3376 gfc_convert_type (*expr, &ts, 2);
3377 }
3378 }
3379 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
3380 {
3381 if ((*expr)->ref == NULL)
3382 {
3383 se->expr = gfc_string_to_single_character
3384 (build_int_cst (integer_type_node, 1),
3385 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
3386 gfc_get_symbol_decl
3387 ((*expr)->symtree->n.sym)),
3388 (*expr)->ts.kind);
3389 }
3390 else
3391 {
3392 gfc_conv_variable (se, *expr);
3393 se->expr = gfc_string_to_single_character
3394 (build_int_cst (integer_type_node, 1),
3395 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
3396 se->expr),
3397 (*expr)->ts.kind);
3398 }
3399 }
3400 }
3401 }
3402
3403 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
3404 if STR is a string literal, otherwise return -1. */
3405
3406 static int
3407 gfc_optimize_len_trim (tree len, tree str, int kind)
3408 {
3409 if (kind == 1
3410 && TREE_CODE (str) == ADDR_EXPR
3411 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
3412 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
3413 && array_ref_low_bound (TREE_OPERAND (str, 0))
3414 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
3415 && tree_fits_uhwi_p (len)
3416 && tree_to_uhwi (len) >= 1
3417 && tree_to_uhwi (len)
3418 == (unsigned HOST_WIDE_INT)
3419 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
3420 {
3421 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
3422 folded = build_fold_indirect_ref_loc (input_location, folded);
3423 if (TREE_CODE (folded) == INTEGER_CST)
3424 {
3425 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
3426 int length = TREE_STRING_LENGTH (string_cst);
3427 const char *ptr = TREE_STRING_POINTER (string_cst);
3428
3429 for (; length > 0; length--)
3430 if (ptr[length - 1] != ' ')
3431 break;
3432
3433 return length;
3434 }
3435 }
3436 return -1;
3437 }
3438
3439 /* Helper to build a call to memcmp. */
3440
3441 static tree
3442 build_memcmp_call (tree s1, tree s2, tree n)
3443 {
3444 tree tmp;
3445
3446 if (!POINTER_TYPE_P (TREE_TYPE (s1)))
3447 s1 = gfc_build_addr_expr (pvoid_type_node, s1);
3448 else
3449 s1 = fold_convert (pvoid_type_node, s1);
3450
3451 if (!POINTER_TYPE_P (TREE_TYPE (s2)))
3452 s2 = gfc_build_addr_expr (pvoid_type_node, s2);
3453 else
3454 s2 = fold_convert (pvoid_type_node, s2);
3455
3456 n = fold_convert (size_type_node, n);
3457
3458 tmp = build_call_expr_loc (input_location,
3459 builtin_decl_explicit (BUILT_IN_MEMCMP),
3460 3, s1, s2, n);
3461
3462 return fold_convert (integer_type_node, tmp);
3463 }
3464
3465 /* Compare two strings. If they are all single characters, the result is the
3466 subtraction of them. Otherwise, we build a library call. */
3467
3468 tree
3469 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
3470 enum tree_code code)
3471 {
3472 tree sc1;
3473 tree sc2;
3474 tree fndecl;
3475
3476 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
3477 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
3478
3479 sc1 = gfc_string_to_single_character (len1, str1, kind);
3480 sc2 = gfc_string_to_single_character (len2, str2, kind);
3481
3482 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
3483 {
3484 /* Deal with single character specially. */
3485 sc1 = fold_convert (integer_type_node, sc1);
3486 sc2 = fold_convert (integer_type_node, sc2);
3487 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
3488 sc1, sc2);
3489 }
3490
3491 if ((code == EQ_EXPR || code == NE_EXPR)
3492 && optimize
3493 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
3494 {
3495 /* If one string is a string literal with LEN_TRIM longer
3496 than the length of the second string, the strings
3497 compare unequal. */
3498 int len = gfc_optimize_len_trim (len1, str1, kind);
3499 if (len > 0 && compare_tree_int (len2, len) < 0)
3500 return integer_one_node;
3501 len = gfc_optimize_len_trim (len2, str2, kind);
3502 if (len > 0 && compare_tree_int (len1, len) < 0)
3503 return integer_one_node;
3504 }
3505
3506 /* We can compare via memcpy if the strings are known to be equal
3507 in length and they are
3508 - kind=1
3509 - kind=4 and the comparison is for (in)equality. */
3510
3511 if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
3512 && tree_int_cst_equal (len1, len2)
3513 && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
3514 {
3515 tree tmp;
3516 tree chartype;
3517
3518 chartype = gfc_get_char_type (kind);
3519 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
3520 fold_convert (TREE_TYPE(len1),
3521 TYPE_SIZE_UNIT(chartype)),
3522 len1);
3523 return build_memcmp_call (str1, str2, tmp);
3524 }
3525
3526 /* Build a call for the comparison. */
3527 if (kind == 1)
3528 fndecl = gfor_fndecl_compare_string;
3529 else if (kind == 4)
3530 fndecl = gfor_fndecl_compare_string_char4;
3531 else
3532 gcc_unreachable ();
3533
3534 return build_call_expr_loc (input_location, fndecl, 4,
3535 len1, str1, len2, str2);
3536 }
3537
3538
3539 /* Return the backend_decl for a procedure pointer component. */
3540
3541 static tree
3542 get_proc_ptr_comp (gfc_expr *e)
3543 {
3544 gfc_se comp_se;
3545 gfc_expr *e2;
3546 expr_t old_type;
3547
3548 gfc_init_se (&comp_se, NULL);
3549 e2 = gfc_copy_expr (e);
3550 /* We have to restore the expr type later so that gfc_free_expr frees
3551 the exact same thing that was allocated.
3552 TODO: This is ugly. */
3553 old_type = e2->expr_type;
3554 e2->expr_type = EXPR_VARIABLE;
3555 gfc_conv_expr (&comp_se, e2);
3556 e2->expr_type = old_type;
3557 gfc_free_expr (e2);
3558 return build_fold_addr_expr_loc (input_location, comp_se.expr);
3559 }
3560
3561
3562 /* Convert a typebound function reference from a class object. */
3563 static void
3564 conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
3565 {
3566 gfc_ref *ref;
3567 tree var;
3568
3569 if (TREE_CODE (base_object) != VAR_DECL)
3570 {
3571 var = gfc_create_var (TREE_TYPE (base_object), NULL);
3572 gfc_add_modify (&se->pre, var, base_object);
3573 }
3574 se->expr = gfc_class_vptr_get (base_object);
3575 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
3576 ref = expr->ref;
3577 while (ref && ref->next)
3578 ref = ref->next;
3579 gcc_assert (ref && ref->type == REF_COMPONENT);
3580 if (ref->u.c.sym->attr.extension)
3581 conv_parent_component_references (se, ref);
3582 gfc_conv_component_ref (se, ref);
3583 se->expr = build_fold_addr_expr_loc (input_location, se->expr);
3584 }
3585
3586
3587 static void
3588 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
3589 {
3590 tree tmp;
3591
3592 if (gfc_is_proc_ptr_comp (expr))
3593 tmp = get_proc_ptr_comp (expr);
3594 else if (sym->attr.dummy)
3595 {
3596 tmp = gfc_get_symbol_decl (sym);
3597 if (sym->attr.proc_pointer)
3598 tmp = build_fold_indirect_ref_loc (input_location,
3599 tmp);
3600 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
3601 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
3602 }
3603 else
3604 {
3605 if (!sym->backend_decl)
3606 sym->backend_decl = gfc_get_extern_function_decl (sym);
3607
3608 TREE_USED (sym->backend_decl) = 1;
3609
3610 tmp = sym->backend_decl;
3611
3612 if (sym->attr.cray_pointee)
3613 {
3614 /* TODO - make the cray pointee a pointer to a procedure,
3615 assign the pointer to it and use it for the call. This
3616 will do for now! */
3617 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
3618 gfc_get_symbol_decl (sym->cp_pointer));
3619 tmp = gfc_evaluate_now (tmp, &se->pre);
3620 }
3621
3622 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
3623 {
3624 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
3625 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
3626 }
3627 }
3628 se->expr = tmp;
3629 }
3630
3631
3632 /* Initialize MAPPING. */
3633
3634 void
3635 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
3636 {
3637 mapping->syms = NULL;
3638 mapping->charlens = NULL;
3639 }
3640
3641
3642 /* Free all memory held by MAPPING (but not MAPPING itself). */
3643
3644 void
3645 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
3646 {
3647 gfc_interface_sym_mapping *sym;
3648 gfc_interface_sym_mapping *nextsym;
3649 gfc_charlen *cl;
3650 gfc_charlen *nextcl;
3651
3652 for (sym = mapping->syms; sym; sym = nextsym)
3653 {
3654 nextsym = sym->next;
3655 sym->new_sym->n.sym->formal = NULL;
3656 gfc_free_symbol (sym->new_sym->n.sym);
3657 gfc_free_expr (sym->expr);
3658 free (sym->new_sym);
3659 free (sym);
3660 }
3661 for (cl = mapping->charlens; cl; cl = nextcl)
3662 {
3663 nextcl = cl->next;
3664 gfc_free_expr (cl->length);
3665 free (cl);
3666 }
3667 }
3668
3669
3670 /* Return a copy of gfc_charlen CL. Add the returned structure to
3671 MAPPING so that it will be freed by gfc_free_interface_mapping. */
3672
3673 static gfc_charlen *
3674 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
3675 gfc_charlen * cl)
3676 {
3677 gfc_charlen *new_charlen;
3678
3679 new_charlen = gfc_get_charlen ();
3680 new_charlen->next = mapping->charlens;
3681 new_charlen->length = gfc_copy_expr (cl->length);
3682
3683 mapping->charlens = new_charlen;
3684 return new_charlen;
3685 }
3686
3687
3688 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
3689 array variable that can be used as the actual argument for dummy
3690 argument SYM. Add any initialization code to BLOCK. PACKED is as
3691 for gfc_get_nodesc_array_type and DATA points to the first element
3692 in the passed array. */
3693
3694 static tree
3695 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
3696 gfc_packed packed, tree data)
3697 {
3698 tree type;
3699 tree var;
3700
3701 type = gfc_typenode_for_spec (&sym->ts);
3702 type = gfc_get_nodesc_array_type (type, sym->as, packed,
3703 !sym->attr.target && !sym->attr.pointer
3704 && !sym->attr.proc_pointer);
3705
3706 var = gfc_create_var (type, "ifm");
3707 gfc_add_modify (block, var, fold_convert (type, data));
3708
3709 return var;
3710 }
3711
3712
3713 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
3714 and offset of descriptorless array type TYPE given that it has the same
3715 size as DESC. Add any set-up code to BLOCK. */
3716
3717 static void
3718 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
3719 {
3720 int n;
3721 tree dim;
3722 tree offset;
3723 tree tmp;
3724
3725 offset = gfc_index_zero_node;
3726 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
3727 {
3728 dim = gfc_rank_cst[n];
3729 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
3730 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
3731 {
3732 GFC_TYPE_ARRAY_LBOUND (type, n)
3733 = gfc_conv_descriptor_lbound_get (desc, dim);
3734 GFC_TYPE_ARRAY_UBOUND (type, n)
3735 = gfc_conv_descriptor_ubound_get (desc, dim);
3736 }
3737 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
3738 {
3739 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3740 gfc_array_index_type,
3741 gfc_conv_descriptor_ubound_get (desc, dim),
3742 gfc_conv_descriptor_lbound_get (desc, dim));
3743 tmp = fold_build2_loc (input_location, PLUS_EXPR,
3744 gfc_array_index_type,
3745 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
3746 tmp = gfc_evaluate_now (tmp, block);
3747 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
3748 }
3749 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
3750 GFC_TYPE_ARRAY_LBOUND (type, n),
3751 GFC_TYPE_ARRAY_STRIDE (type, n));
3752 offset = fold_build2_loc (input_location, MINUS_EXPR,
3753 gfc_array_index_type, offset, tmp);
3754 }
3755 offset = gfc_evaluate_now (offset, block);
3756 GFC_TYPE_ARRAY_OFFSET (type) = offset;
3757 }
3758
3759
3760 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
3761 in SE. The caller may still use se->expr and se->string_length after
3762 calling this function. */
3763
3764 void
3765 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
3766 gfc_symbol * sym, gfc_se * se,
3767 gfc_expr *expr)
3768 {
3769 gfc_interface_sym_mapping *sm;
3770 tree desc;
3771 tree tmp;
3772 tree value;
3773 gfc_symbol *new_sym;
3774 gfc_symtree *root;
3775 gfc_symtree *new_symtree;
3776
3777 /* Create a new symbol to represent the actual argument. */
3778 new_sym = gfc_new_symbol (sym->name, NULL);
3779 new_sym->ts = sym->ts;
3780 new_sym->as = gfc_copy_array_spec (sym->as);
3781 new_sym->attr.referenced = 1;
3782 new_sym->attr.dimension = sym->attr.dimension;
3783 new_sym->attr.contiguous = sym->attr.contiguous;
3784 new_sym->attr.codimension = sym->attr.codimension;
3785 new_sym->attr.pointer = sym->attr.pointer;
3786 new_sym->attr.allocatable = sym->attr.allocatable;
3787 new_sym->attr.flavor = sym->attr.flavor;
3788 new_sym->attr.function = sym->attr.function;
3789
3790 /* Ensure that the interface is available and that
3791 descriptors are passed for array actual arguments. */
3792 if (sym->attr.flavor == FL_PROCEDURE)
3793 {
3794 new_sym->formal = expr->symtree->n.sym->formal;
3795 new_sym->attr.always_explicit
3796 = expr->symtree->n.sym->attr.always_explicit;
3797 }
3798
3799 /* Create a fake symtree for it. */
3800 root = NULL;
3801 new_symtree = gfc_new_symtree (&root, sym->name);
3802 new_symtree->n.sym = new_sym;
3803 gcc_assert (new_symtree == root);
3804
3805 /* Create a dummy->actual mapping. */
3806 sm = XCNEW (gfc_interface_sym_mapping);
3807 sm->next = mapping->syms;
3808 sm->old = sym;
3809 sm->new_sym = new_symtree;
3810 sm->expr = gfc_copy_expr (expr);
3811 mapping->syms = sm;
3812
3813 /* Stabilize the argument's value. */
3814 if (!sym->attr.function && se)
3815 se->expr = gfc_evaluate_now (se->expr, &se->pre);
3816
3817 if (sym->ts.type == BT_CHARACTER)
3818 {
3819 /* Create a copy of the dummy argument's length. */
3820 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
3821 sm->expr->ts.u.cl = new_sym->ts.u.cl;
3822
3823 /* If the length is specified as "*", record the length that
3824 the caller is passing. We should use the callee's length
3825 in all other cases. */
3826 if (!new_sym->ts.u.cl->length && se)
3827 {
3828 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
3829 new_sym->ts.u.cl->backend_decl = se->string_length;
3830 }
3831 }
3832
3833 if (!se)
3834 return;
3835
3836 /* Use the passed value as-is if the argument is a function. */
3837 if (sym->attr.flavor == FL_PROCEDURE)
3838 value = se->expr;
3839
3840 /* If the argument is either a string or a pointer to a string,
3841 convert it to a boundless character type. */
3842 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
3843 {
3844 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
3845 tmp = build_pointer_type (tmp);
3846 if (sym->attr.pointer)
3847 value = build_fold_indirect_ref_loc (input_location,
3848 se->expr);
3849 else
3850 value = se->expr;
3851 value = fold_convert (tmp, value);
3852 }
3853
3854 /* If the argument is a scalar, a pointer to an array or an allocatable,
3855 dereference it. */
3856 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
3857 value = build_fold_indirect_ref_loc (input_location,
3858 se->expr);
3859
3860 /* For character(*), use the actual argument's descriptor. */
3861 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
3862 value = build_fold_indirect_ref_loc (input_location,
3863 se->expr);
3864
3865 /* If the argument is an array descriptor, use it to determine
3866 information about the actual argument's shape. */
3867 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
3868 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
3869 {
3870 /* Get the actual argument's descriptor. */
3871 desc = build_fold_indirect_ref_loc (input_location,
3872 se->expr);
3873
3874 /* Create the replacement variable. */
3875 tmp = gfc_conv_descriptor_data_get (desc);
3876 value = gfc_get_interface_mapping_array (&se->pre, sym,
3877 PACKED_NO, tmp);
3878
3879 /* Use DESC to work out the upper bounds, strides and offset. */
3880 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
3881 }
3882 else
3883 /* Otherwise we have a packed array. */
3884 value = gfc_get_interface_mapping_array (&se->pre, sym,
3885 PACKED_FULL, se->expr);
3886
3887 new_sym->backend_decl = value;
3888 }
3889
3890
3891 /* Called once all dummy argument mappings have been added to MAPPING,
3892 but before the mapping is used to evaluate expressions. Pre-evaluate
3893 the length of each argument, adding any initialization code to PRE and
3894 any finalization code to POST. */
3895
3896 void
3897 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
3898 stmtblock_t * pre, stmtblock_t * post)
3899 {
3900 gfc_interface_sym_mapping *sym;
3901 gfc_expr *expr;
3902 gfc_se se;
3903
3904 for (sym = mapping->syms; sym; sym = sym->next)
3905 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
3906 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
3907 {
3908 expr = sym->new_sym->n.sym->ts.u.cl->length;
3909 gfc_apply_interface_mapping_to_expr (mapping, expr);
3910 gfc_init_se (&se, NULL);
3911 gfc_conv_expr (&se, expr);
3912 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
3913 se.expr = gfc_evaluate_now (se.expr, &se.pre);
3914 gfc_add_block_to_block (pre, &se.pre);
3915 gfc_add_block_to_block (post, &se.post);
3916
3917 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
3918 }
3919 }
3920
3921
3922 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3923 constructor C. */
3924
3925 static void
3926 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
3927 gfc_constructor_base base)
3928 {
3929 gfc_constructor *c;
3930 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
3931 {
3932 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
3933 if (c->iterator)
3934 {
3935 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
3936 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
3937 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
3938 }
3939 }
3940 }
3941
3942
3943 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3944 reference REF. */
3945
3946 static void
3947 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
3948 gfc_ref * ref)
3949 {
3950 int n;
3951
3952 for (; ref; ref = ref->next)
3953 switch (ref->type)
3954 {
3955 case REF_ARRAY:
3956 for (n = 0; n < ref->u.ar.dimen; n++)
3957 {
3958 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
3959 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
3960 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
3961 }
3962 break;
3963
3964 case REF_COMPONENT:
3965 break;
3966
3967 case REF_SUBSTRING:
3968 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
3969 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
3970 break;
3971 }
3972 }
3973
3974
3975 /* Convert intrinsic function calls into result expressions. */
3976
3977 static bool
3978 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
3979 {
3980 gfc_symbol *sym;
3981 gfc_expr *new_expr;
3982 gfc_expr *arg1;
3983 gfc_expr *arg2;
3984 int d, dup;
3985
3986 arg1 = expr->value.function.actual->expr;
3987 if (expr->value.function.actual->next)
3988 arg2 = expr->value.function.actual->next->expr;
3989 else
3990 arg2 = NULL;
3991
3992 sym = arg1->symtree->n.sym;
3993
3994 if (sym->attr.dummy)
3995 return false;
3996
3997 new_expr = NULL;
3998
3999 switch (expr->value.function.isym->id)
4000 {
4001 case GFC_ISYM_LEN:
4002 /* TODO figure out why this condition is necessary. */
4003 if (sym->attr.function
4004 && (arg1->ts.u.cl->length == NULL
4005 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
4006 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
4007 return false;
4008
4009 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
4010 break;
4011
4012 case GFC_ISYM_SIZE:
4013 if (!sym->as || sym->as->rank == 0)
4014 return false;
4015
4016 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
4017 {
4018 dup = mpz_get_si (arg2->value.integer);
4019 d = dup - 1;
4020 }
4021 else
4022 {
4023 dup = sym->as->rank;
4024 d = 0;
4025 }
4026
4027 for (; d < dup; d++)
4028 {
4029 gfc_expr *tmp;
4030
4031 if (!sym->as->upper[d] || !sym->as->lower[d])
4032 {
4033 gfc_free_expr (new_expr);
4034 return false;
4035 }
4036
4037 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
4038 gfc_get_int_expr (gfc_default_integer_kind,
4039 NULL, 1));
4040 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
4041 if (new_expr)
4042 new_expr = gfc_multiply (new_expr, tmp);
4043 else
4044 new_expr = tmp;
4045 }
4046 break;
4047
4048 case GFC_ISYM_LBOUND:
4049 case GFC_ISYM_UBOUND:
4050 /* TODO These implementations of lbound and ubound do not limit if
4051 the size < 0, according to F95's 13.14.53 and 13.14.113. */
4052
4053 if (!sym->as || sym->as->rank == 0)
4054 return false;
4055
4056 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
4057 d = mpz_get_si (arg2->value.integer) - 1;
4058 else
4059 /* TODO: If the need arises, this could produce an array of
4060 ubound/lbounds. */
4061 gcc_unreachable ();
4062
4063 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
4064 {
4065 if (sym->as->lower[d])
4066 new_expr = gfc_copy_expr (sym->as->lower[d]);
4067 }
4068 else
4069 {
4070 if (sym->as->upper[d])
4071 new_expr = gfc_copy_expr (sym->as->upper[d]);
4072 }
4073 break;
4074
4075 default:
4076 break;
4077 }
4078
4079 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
4080 if (!new_expr)
4081 return false;
4082
4083 gfc_replace_expr (expr, new_expr);
4084 return true;
4085 }
4086
4087
4088 static void
4089 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
4090 gfc_interface_mapping * mapping)
4091 {
4092 gfc_formal_arglist *f;
4093 gfc_actual_arglist *actual;
4094
4095 actual = expr->value.function.actual;
4096 f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
4097
4098 for (; f && actual; f = f->next, actual = actual->next)
4099 {
4100 if (!actual->expr)
4101 continue;
4102
4103 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
4104 }
4105
4106 if (map_expr->symtree->n.sym->attr.dimension)
4107 {
4108 int d;
4109 gfc_array_spec *as;
4110
4111 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
4112
4113 for (d = 0; d < as->rank; d++)
4114 {
4115 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
4116 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
4117 }
4118
4119 expr->value.function.esym->as = as;
4120 }
4121
4122 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
4123 {
4124 expr->value.function.esym->ts.u.cl->length
4125 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
4126
4127 gfc_apply_interface_mapping_to_expr (mapping,
4128 expr->value.function.esym->ts.u.cl->length);
4129 }
4130 }
4131
4132
4133 /* EXPR is a copy of an expression that appeared in the interface
4134 associated with MAPPING. Walk it recursively looking for references to
4135 dummy arguments that MAPPING maps to actual arguments. Replace each such
4136 reference with a reference to the associated actual argument. */
4137
4138 static void
4139 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
4140 gfc_expr * expr)
4141 {
4142 gfc_interface_sym_mapping *sym;
4143 gfc_actual_arglist *actual;
4144
4145 if (!expr)
4146 return;
4147
4148 /* Copying an expression does not copy its length, so do that here. */
4149 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
4150 {
4151 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
4152 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
4153 }
4154
4155 /* Apply the mapping to any references. */
4156 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
4157
4158 /* ...and to the expression's symbol, if it has one. */
4159 /* TODO Find out why the condition on expr->symtree had to be moved into
4160 the loop rather than being outside it, as originally. */
4161 for (sym = mapping->syms; sym; sym = sym->next)
4162 if (expr->symtree && sym->old == expr->symtree->n.sym)
4163 {
4164 if (sym->new_sym->n.sym->backend_decl)
4165 expr->symtree = sym->new_sym;
4166 else if (sym->expr)
4167 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
4168 }
4169
4170 /* ...and to subexpressions in expr->value. */
4171 switch (expr->expr_type)
4172 {
4173 case EXPR_VARIABLE:
4174 case EXPR_CONSTANT:
4175 case EXPR_NULL:
4176 case EXPR_SUBSTRING:
4177 break;
4178
4179 case EXPR_OP:
4180 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
4181 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
4182 break;
4183
4184 case EXPR_FUNCTION:
4185 for (actual = expr->value.function.actual; actual; actual = actual->next)
4186 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
4187
4188 if (expr->value.function.esym == NULL
4189 && expr->value.function.isym != NULL
4190 && expr->value.function.actual->expr->symtree
4191 && gfc_map_intrinsic_function (expr, mapping))
4192 break;
4193
4194 for (sym = mapping->syms; sym; sym = sym->next)
4195 if (sym->old == expr->value.function.esym)
4196 {
4197 expr->value.function.esym = sym->new_sym->n.sym;
4198 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
4199 expr->value.function.esym->result = sym->new_sym->n.sym;
4200 }
4201 break;
4202
4203 case EXPR_ARRAY:
4204 case EXPR_STRUCTURE:
4205 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
4206 break;
4207
4208 case EXPR_COMPCALL:
4209 case EXPR_PPC:
4210 gcc_unreachable ();
4211 break;
4212 }
4213
4214 return;
4215 }
4216
4217
4218 /* Evaluate interface expression EXPR using MAPPING. Store the result
4219 in SE. */
4220
4221 void
4222 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
4223 gfc_se * se, gfc_expr * expr)
4224 {
4225 expr = gfc_copy_expr (expr);
4226 gfc_apply_interface_mapping_to_expr (mapping, expr);
4227 gfc_conv_expr (se, expr);
4228 se->expr = gfc_evaluate_now (se->expr, &se->pre);
4229 gfc_free_expr (expr);
4230 }
4231
4232
4233 /* Returns a reference to a temporary array into which a component of
4234 an actual argument derived type array is copied and then returned
4235 after the function call. */
4236 void
4237 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
4238 sym_intent intent, bool formal_ptr)
4239 {
4240 gfc_se lse;
4241 gfc_se rse;
4242 gfc_ss *lss;
4243 gfc_ss *rss;
4244 gfc_loopinfo loop;
4245 gfc_loopinfo loop2;
4246 gfc_array_info *info;
4247 tree offset;
4248 tree tmp_index;
4249 tree tmp;
4250 tree base_type;
4251 tree size;
4252 stmtblock_t body;
4253 int n;
4254 int dimen;
4255
4256 gfc_init_se (&lse, NULL);
4257 gfc_init_se (&rse, NULL);
4258
4259 /* Walk the argument expression. */
4260 rss = gfc_walk_expr (expr);
4261
4262 gcc_assert (rss != gfc_ss_terminator);
4263
4264 /* Initialize the scalarizer. */
4265 gfc_init_loopinfo (&loop);
4266 gfc_add_ss_to_loop (&loop, rss);
4267
4268 /* Calculate the bounds of the scalarization. */
4269 gfc_conv_ss_startstride (&loop);
4270
4271 /* Build an ss for the temporary. */
4272 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
4273 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
4274
4275 base_type = gfc_typenode_for_spec (&expr->ts);
4276 if (GFC_ARRAY_TYPE_P (base_type)
4277 || GFC_DESCRIPTOR_TYPE_P (base_type))
4278 base_type = gfc_get_element_type (base_type);
4279
4280 if (expr->ts.type == BT_CLASS)
4281 base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
4282
4283 loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
4284 ? expr->ts.u.cl->backend_decl
4285 : NULL),
4286 loop.dimen);
4287
4288 parmse->string_length = loop.temp_ss->info->string_length;
4289
4290 /* Associate the SS with the loop. */
4291 gfc_add_ss_to_loop (&loop, loop.temp_ss);
4292
4293 /* Setup the scalarizing loops. */
4294 gfc_conv_loop_setup (&loop, &expr->where);
4295
4296 /* Pass the temporary descriptor back to the caller. */
4297 info = &loop.temp_ss->info->data.array;
4298 parmse->expr = info->descriptor;
4299
4300 /* Setup the gfc_se structures. */
4301 gfc_copy_loopinfo_to_se (&lse, &loop);
4302 gfc_copy_loopinfo_to_se (&rse, &loop);
4303
4304 rse.ss = rss;
4305 lse.ss = loop.temp_ss;
4306 gfc_mark_ss_chain_used (rss, 1);
4307 gfc_mark_ss_chain_used (loop.temp_ss, 1);
4308
4309 /* Start the scalarized loop body. */
4310 gfc_start_scalarized_body (&loop, &body);
4311
4312 /* Translate the expression. */
4313 gfc_conv_expr (&rse, expr);
4314
4315 /* Reset the offset for the function call since the loop
4316 is zero based on the data pointer. Note that the temp
4317 comes first in the loop chain since it is added second. */
4318 if (gfc_is_alloc_class_array_function (expr))
4319 {
4320 tmp = loop.ss->loop_chain->info->data.array.descriptor;
4321 gfc_conv_descriptor_offset_set (&loop.pre, tmp,
4322 gfc_index_zero_node);
4323 }
4324
4325 gfc_conv_tmp_array_ref (&lse);
4326
4327 if (intent != INTENT_OUT)
4328 {
4329 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false, true);
4330 gfc_add_expr_to_block (&body, tmp);
4331 gcc_assert (rse.ss == gfc_ss_terminator);
4332 gfc_trans_scalarizing_loops (&loop, &body);
4333 }
4334 else
4335 {
4336 /* Make sure that the temporary declaration survives by merging
4337 all the loop declarations into the current context. */
4338 for (n = 0; n < loop.dimen; n++)
4339 {
4340 gfc_merge_block_scope (&body);
4341 body = loop.code[loop.order[n]];
4342 }
4343 gfc_merge_block_scope (&body);
4344 }
4345
4346 /* Add the post block after the second loop, so that any
4347 freeing of allocated memory is done at the right time. */
4348 gfc_add_block_to_block (&parmse->pre, &loop.pre);
4349
4350 /**********Copy the temporary back again.*********/
4351
4352 gfc_init_se (&lse, NULL);
4353 gfc_init_se (&rse, NULL);
4354
4355 /* Walk the argument expression. */
4356 lss = gfc_walk_expr (expr);
4357 rse.ss = loop.temp_ss;
4358 lse.ss = lss;
4359
4360 /* Initialize the scalarizer. */
4361 gfc_init_loopinfo (&loop2);
4362 gfc_add_ss_to_loop (&loop2, lss);
4363
4364 dimen = rse.ss->dimen;
4365
4366 /* Skip the write-out loop for this case. */
4367 if (gfc_is_alloc_class_array_function (expr))
4368 goto class_array_fcn;
4369
4370 /* Calculate the bounds of the scalarization. */
4371 gfc_conv_ss_startstride (&loop2);
4372
4373 /* Setup the scalarizing loops. */
4374 gfc_conv_loop_setup (&loop2, &expr->where);
4375
4376 gfc_copy_loopinfo_to_se (&lse, &loop2);
4377 gfc_copy_loopinfo_to_se (&rse, &loop2);
4378
4379 gfc_mark_ss_chain_used (lss, 1);
4380 gfc_mark_ss_chain_used (loop.temp_ss, 1);
4381
4382 /* Declare the variable to hold the temporary offset and start the
4383 scalarized loop body. */
4384 offset = gfc_create_var (gfc_array_index_type, NULL);
4385 gfc_start_scalarized_body (&loop2, &body);
4386
4387 /* Build the offsets for the temporary from the loop variables. The
4388 temporary array has lbounds of zero and strides of one in all
4389 dimensions, so this is very simple. The offset is only computed
4390 outside the innermost loop, so the overall transfer could be
4391 optimized further. */
4392 info = &rse.ss->info->data.array;
4393
4394 tmp_index = gfc_index_zero_node;
4395 for (n = dimen - 1; n > 0; n--)
4396 {
4397 tree tmp_str;
4398 tmp = rse.loop->loopvar[n];
4399 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4400 tmp, rse.loop->from[n]);
4401 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4402 tmp, tmp_index);
4403
4404 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
4405 gfc_array_index_type,
4406 rse.loop->to[n-1], rse.loop->from[n-1]);
4407 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
4408 gfc_array_index_type,
4409 tmp_str, gfc_index_one_node);
4410
4411 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
4412 gfc_array_index_type, tmp, tmp_str);
4413 }
4414
4415 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
4416 gfc_array_index_type,
4417 tmp_index, rse.loop->from[0]);
4418 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
4419
4420 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
4421 gfc_array_index_type,
4422 rse.loop->loopvar[0], offset);
4423
4424 /* Now use the offset for the reference. */
4425 tmp = build_fold_indirect_ref_loc (input_location,
4426 info->data);
4427 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
4428
4429 if (expr->ts.type == BT_CHARACTER)
4430 rse.string_length = expr->ts.u.cl->backend_decl;
4431
4432 gfc_conv_expr (&lse, expr);
4433
4434 gcc_assert (lse.ss == gfc_ss_terminator);
4435
4436 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false, true);
4437 gfc_add_expr_to_block (&body, tmp);
4438
4439 /* Generate the copying loops. */
4440 gfc_trans_scalarizing_loops (&loop2, &body);
4441
4442 /* Wrap the whole thing up by adding the second loop to the post-block
4443 and following it by the post-block of the first loop. In this way,
4444 if the temporary needs freeing, it is done after use! */
4445 if (intent != INTENT_IN)
4446 {
4447 gfc_add_block_to_block (&parmse->post, &loop2.pre);
4448 gfc_add_block_to_block (&parmse->post, &loop2.post);
4449 }
4450
4451 class_array_fcn:
4452
4453 gfc_add_block_to_block (&parmse->post, &loop.post);
4454
4455 gfc_cleanup_loop (&loop);
4456 gfc_cleanup_loop (&loop2);
4457
4458 /* Pass the string length to the argument expression. */
4459 if (expr->ts.type == BT_CHARACTER)
4460 parmse->string_length = expr->ts.u.cl->backend_decl;
4461
4462 /* Determine the offset for pointer formal arguments and set the
4463 lbounds to one. */
4464 if (formal_ptr)
4465 {
4466 size = gfc_index_one_node;
4467 offset = gfc_index_zero_node;
4468 for (n = 0; n < dimen; n++)
4469 {
4470 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
4471 gfc_rank_cst[n]);
4472 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4473 gfc_array_index_type, tmp,
4474 gfc_index_one_node);
4475 gfc_conv_descriptor_ubound_set (&parmse->pre,
4476 parmse->expr,
4477 gfc_rank_cst[n],
4478 tmp);
4479 gfc_conv_descriptor_lbound_set (&parmse->pre,
4480 parmse->expr,
4481 gfc_rank_cst[n],
4482 gfc_index_one_node);
4483 size = gfc_evaluate_now (size, &parmse->pre);
4484 offset = fold_build2_loc (input_location, MINUS_EXPR,
4485 gfc_array_index_type,
4486 offset, size);
4487 offset = gfc_evaluate_now (offset, &parmse->pre);
4488 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4489 gfc_array_index_type,
4490 rse.loop->to[n], rse.loop->from[n]);
4491 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4492 gfc_array_index_type,
4493 tmp, gfc_index_one_node);
4494 size = fold_build2_loc (input_location, MULT_EXPR,
4495 gfc_array_index_type, size, tmp);
4496 }
4497
4498 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
4499 offset);
4500 }
4501
4502 /* We want either the address for the data or the address of the descriptor,
4503 depending on the mode of passing array arguments. */
4504 if (g77)
4505 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
4506 else
4507 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
4508
4509 return;
4510 }
4511
4512
4513 /* Generate the code for argument list functions. */
4514
4515 static void
4516 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
4517 {
4518 /* Pass by value for g77 %VAL(arg), pass the address
4519 indirectly for %LOC, else by reference. Thus %REF
4520 is a "do-nothing" and %LOC is the same as an F95
4521 pointer. */
4522 if (strncmp (name, "%VAL", 4) == 0)
4523 gfc_conv_expr (se, expr);
4524 else if (strncmp (name, "%LOC", 4) == 0)
4525 {
4526 gfc_conv_expr_reference (se, expr);
4527 se->expr = gfc_build_addr_expr (NULL, se->expr);
4528 }
4529 else if (strncmp (name, "%REF", 4) == 0)
4530 gfc_conv_expr_reference (se, expr);
4531 else
4532 gfc_error ("Unknown argument list function at %L", &expr->where);
4533 }
4534
4535
4536 /* Generate code for a procedure call. Note can return se->post != NULL.
4537 If se->direct_byref is set then se->expr contains the return parameter.
4538 Return nonzero, if the call has alternate specifiers.
4539 'expr' is only needed for procedure pointer components. */
4540
4541 int
4542 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
4543 gfc_actual_arglist * args, gfc_expr * expr,
4544 vec<tree, va_gc> *append_args)
4545 {
4546 gfc_interface_mapping mapping;
4547 vec<tree, va_gc> *arglist;
4548 vec<tree, va_gc> *retargs;
4549 tree tmp;
4550 tree fntype;
4551 gfc_se parmse;
4552 gfc_array_info *info;
4553 int byref;
4554 int parm_kind;
4555 tree type;
4556 tree var;
4557 tree len;
4558 tree base_object;
4559 vec<tree, va_gc> *stringargs;
4560 vec<tree, va_gc> *optionalargs;
4561 tree result = NULL;
4562 gfc_formal_arglist *formal;
4563 gfc_actual_arglist *arg;
4564 int has_alternate_specifier = 0;
4565 bool need_interface_mapping;
4566 bool callee_alloc;
4567 gfc_typespec ts;
4568 gfc_charlen cl;
4569 gfc_expr *e;
4570 gfc_symbol *fsym;
4571 stmtblock_t post;
4572 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
4573 gfc_component *comp = NULL;
4574 int arglen;
4575
4576 arglist = NULL;
4577 retargs = NULL;
4578 stringargs = NULL;
4579 optionalargs = NULL;
4580 var = NULL_TREE;
4581 len = NULL_TREE;
4582 gfc_clear_ts (&ts);
4583
4584 comp = gfc_get_proc_ptr_comp (expr);
4585
4586 if (se->ss != NULL)
4587 {
4588 if (!sym->attr.elemental && !(comp && comp->attr.elemental))
4589 {
4590 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
4591 if (se->ss->info->useflags)
4592 {
4593 gcc_assert ((!comp && gfc_return_by_reference (sym)
4594 && sym->result->attr.dimension)
4595 || (comp && comp->attr.dimension)
4596 || gfc_is_alloc_class_array_function (expr));
4597 gcc_assert (se->loop != NULL);
4598 /* Access the previously obtained result. */
4599 gfc_conv_tmp_array_ref (se);
4600 return 0;
4601 }
4602 }
4603 info = &se->ss->info->data.array;
4604 }
4605 else
4606 info = NULL;
4607
4608 gfc_init_block (&post);
4609 gfc_init_interface_mapping (&mapping);
4610 if (!comp)
4611 {
4612 formal = gfc_sym_get_dummy_args (sym);
4613 need_interface_mapping = sym->attr.dimension ||
4614 (sym->ts.type == BT_CHARACTER
4615 && sym->ts.u.cl->length
4616 && sym->ts.u.cl->length->expr_type
4617 != EXPR_CONSTANT);
4618 }
4619 else
4620 {
4621 formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
4622 need_interface_mapping = comp->attr.dimension ||
4623 (comp->ts.type == BT_CHARACTER
4624 && comp->ts.u.cl->length
4625 && comp->ts.u.cl->length->expr_type
4626 != EXPR_CONSTANT);
4627 }
4628
4629 base_object = NULL_TREE;
4630
4631 /* Evaluate the arguments. */
4632 for (arg = args; arg != NULL;
4633 arg = arg->next, formal = formal ? formal->next : NULL)
4634 {
4635 e = arg->expr;
4636 fsym = formal ? formal->sym : NULL;
4637 parm_kind = MISSING;
4638
4639 /* Class array expressions are sometimes coming completely unadorned
4640 with either arrayspec or _data component. Correct that here.
4641 OOP-TODO: Move this to the frontend. */
4642 if (e && e->expr_type == EXPR_VARIABLE
4643 && !e->ref
4644 && e->ts.type == BT_CLASS
4645 && (CLASS_DATA (e)->attr.codimension
4646 || CLASS_DATA (e)->attr.dimension))
4647 {
4648 gfc_typespec temp_ts = e->ts;
4649 gfc_add_class_array_ref (e);
4650 e->ts = temp_ts;
4651 }
4652
4653 if (e == NULL)
4654 {
4655 if (se->ignore_optional)
4656 {
4657 /* Some intrinsics have already been resolved to the correct
4658 parameters. */
4659 continue;
4660 }
4661 else if (arg->label)
4662 {
4663 has_alternate_specifier = 1;
4664 continue;
4665 }
4666 else
4667 {
4668 gfc_init_se (&parmse, NULL);
4669
4670 /* For scalar arguments with VALUE attribute which are passed by
4671 value, pass "0" and a hidden argument gives the optional
4672 status. */
4673 if (fsym && fsym->attr.optional && fsym->attr.value
4674 && !fsym->attr.dimension && fsym->ts.type != BT_CHARACTER
4675 && fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED)
4676 {
4677 parmse.expr = fold_convert (gfc_sym_type (fsym),
4678 integer_zero_node);
4679 vec_safe_push (optionalargs, boolean_false_node);
4680 }
4681 else
4682 {
4683 /* Pass a NULL pointer for an absent arg. */
4684 parmse.expr = null_pointer_node;
4685 if (arg->missing_arg_type == BT_CHARACTER)
4686 parmse.string_length = build_int_cst (gfc_charlen_type_node,
4687 0);
4688 }
4689 }
4690 }
4691 else if (arg->expr->expr_type == EXPR_NULL
4692 && fsym && !fsym->attr.pointer
4693 && (fsym->ts.type != BT_CLASS
4694 || !CLASS_DATA (fsym)->attr.class_pointer))
4695 {
4696 /* Pass a NULL pointer to denote an absent arg. */
4697 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
4698 && (fsym->ts.type != BT_CLASS
4699 || !CLASS_DATA (fsym)->attr.allocatable));
4700 gfc_init_se (&parmse, NULL);
4701 parmse.expr = null_pointer_node;
4702 if (arg->missing_arg_type == BT_CHARACTER)
4703 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
4704 }
4705 else if (fsym && fsym->ts.type == BT_CLASS
4706 && e->ts.type == BT_DERIVED)
4707 {
4708 /* The derived type needs to be converted to a temporary
4709 CLASS object. */
4710 gfc_init_se (&parmse, se);
4711 gfc_conv_derived_to_class (&parmse, e, fsym->ts, NULL,
4712 fsym->attr.optional
4713 && e->expr_type == EXPR_VARIABLE
4714 && e->symtree->n.sym->attr.optional,
4715 CLASS_DATA (fsym)->attr.class_pointer
4716 || CLASS_DATA (fsym)->attr.allocatable);
4717 }
4718 else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS)
4719 {
4720 /* The intrinsic type needs to be converted to a temporary
4721 CLASS object for the unlimited polymorphic formal. */
4722 gfc_init_se (&parmse, se);
4723 gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
4724 }
4725 else if (se->ss && se->ss->info->useflags)
4726 {
4727 gfc_ss *ss;
4728
4729 ss = se->ss;
4730
4731 /* An elemental function inside a scalarized loop. */
4732 gfc_init_se (&parmse, se);
4733 parm_kind = ELEMENTAL;
4734
4735 if (fsym && fsym->attr.value)
4736 gfc_conv_expr (&parmse, e);
4737 else
4738 gfc_conv_expr_reference (&parmse, e);
4739
4740 if (e->ts.type == BT_CHARACTER && !e->rank
4741 && e->expr_type == EXPR_FUNCTION)
4742 parmse.expr = build_fold_indirect_ref_loc (input_location,
4743 parmse.expr);
4744
4745 if (fsym && fsym->ts.type == BT_DERIVED
4746 && gfc_is_class_container_ref (e))
4747 {
4748 parmse.expr = gfc_class_data_get (parmse.expr);
4749
4750 if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
4751 && e->symtree->n.sym->attr.optional)
4752 {
4753 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
4754 parmse.expr = build3_loc (input_location, COND_EXPR,
4755 TREE_TYPE (parmse.expr),
4756 cond, parmse.expr,
4757 fold_convert (TREE_TYPE (parmse.expr),
4758 null_pointer_node));
4759 }
4760 }
4761
4762 /* If we are passing an absent array as optional dummy to an
4763 elemental procedure, make sure that we pass NULL when the data
4764 pointer is NULL. We need this extra conditional because of
4765 scalarization which passes arrays elements to the procedure,
4766 ignoring the fact that the array can be absent/unallocated/... */
4767 if (ss->info->can_be_null_ref && ss->info->type != GFC_SS_REFERENCE)
4768 {
4769 tree descriptor_data;
4770
4771 descriptor_data = ss->info->data.array.data;
4772 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
4773 descriptor_data,
4774 fold_convert (TREE_TYPE (descriptor_data),
4775 null_pointer_node));
4776 parmse.expr
4777 = fold_build3_loc (input_location, COND_EXPR,
4778 TREE_TYPE (parmse.expr),
4779 gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
4780 fold_convert (TREE_TYPE (parmse.expr),
4781 null_pointer_node),
4782 parmse.expr);
4783 }
4784
4785 /* The scalarizer does not repackage the reference to a class
4786 array - instead it returns a pointer to the data element. */
4787 if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
4788 gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
4789 fsym->attr.intent != INTENT_IN
4790 && (CLASS_DATA (fsym)->attr.class_pointer
4791 || CLASS_DATA (fsym)->attr.allocatable),
4792 fsym->attr.optional
4793 && e->expr_type == EXPR_VARIABLE
4794 && e->symtree->n.sym->attr.optional,
4795 CLASS_DATA (fsym)->attr.class_pointer
4796 || CLASS_DATA (fsym)->attr.allocatable);
4797 }
4798 else
4799 {
4800 bool scalar;
4801 gfc_ss *argss;
4802
4803 gfc_init_se (&parmse, NULL);
4804
4805 /* Check whether the expression is a scalar or not; we cannot use
4806 e->rank as it can be nonzero for functions arguments. */
4807 argss = gfc_walk_expr (e);
4808 scalar = argss == gfc_ss_terminator;
4809 if (!scalar)
4810 gfc_free_ss_chain (argss);
4811
4812 /* Special handling for passing scalar polymorphic coarrays;
4813 otherwise one passes "class->_data.data" instead of "&class". */
4814 if (e->rank == 0 && e->ts.type == BT_CLASS
4815 && fsym && fsym->ts.type == BT_CLASS
4816 && CLASS_DATA (fsym)->attr.codimension
4817 && !CLASS_DATA (fsym)->attr.dimension)
4818 {
4819 gfc_add_class_array_ref (e);
4820 parmse.want_coarray = 1;
4821 scalar = false;
4822 }
4823
4824 /* A scalar or transformational function. */
4825 if (scalar)
4826 {
4827 if (e->expr_type == EXPR_VARIABLE
4828 && e->symtree->n.sym->attr.cray_pointee
4829 && fsym && fsym->attr.flavor == FL_PROCEDURE)
4830 {
4831 /* The Cray pointer needs to be converted to a pointer to
4832 a type given by the expression. */
4833 gfc_conv_expr (&parmse, e);
4834 type = build_pointer_type (TREE_TYPE (parmse.expr));
4835 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
4836 parmse.expr = convert (type, tmp);
4837 }
4838 else if (fsym && fsym->attr.value)
4839 {
4840 if (fsym->ts.type == BT_CHARACTER
4841 && fsym->ts.is_c_interop
4842 && fsym->ns->proc_name != NULL
4843 && fsym->ns->proc_name->attr.is_bind_c)
4844 {
4845 parmse.expr = NULL;
4846 gfc_conv_scalar_char_value (fsym, &parmse, &e);
4847 if (parmse.expr == NULL)
4848 gfc_conv_expr (&parmse, e);
4849 }
4850 else
4851 {
4852 gfc_conv_expr (&parmse, e);
4853 if (fsym->attr.optional
4854 && fsym->ts.type != BT_CLASS
4855 && fsym->ts.type != BT_DERIVED)
4856 {
4857 if (e->expr_type != EXPR_VARIABLE
4858 || !e->symtree->n.sym->attr.optional
4859 || e->ref != NULL)
4860 vec_safe_push (optionalargs, boolean_true_node);
4861 else
4862 {
4863 tmp = gfc_conv_expr_present (e->symtree->n.sym);
4864 if (!e->symtree->n.sym->attr.value)
4865 parmse.expr
4866 = fold_build3_loc (input_location, COND_EXPR,
4867 TREE_TYPE (parmse.expr),
4868 tmp, parmse.expr,
4869 fold_convert (TREE_TYPE (parmse.expr),
4870 integer_zero_node));
4871
4872 vec_safe_push (optionalargs, tmp);
4873 }
4874 }
4875 }
4876 }
4877 else if (arg->name && arg->name[0] == '%')
4878 /* Argument list functions %VAL, %LOC and %REF are signalled
4879 through arg->name. */
4880 conv_arglist_function (&parmse, arg->expr, arg->name);
4881 else if ((e->expr_type == EXPR_FUNCTION)
4882 && ((e->value.function.esym
4883 && e->value.function.esym->result->attr.pointer)
4884 || (!e->value.function.esym
4885 && e->symtree->n.sym->attr.pointer))
4886 && fsym && fsym->attr.target)
4887 {
4888 gfc_conv_expr (&parmse, e);
4889 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4890 }
4891 else if (e->expr_type == EXPR_FUNCTION
4892 && e->symtree->n.sym->result
4893 && e->symtree->n.sym->result != e->symtree->n.sym
4894 && e->symtree->n.sym->result->attr.proc_pointer)
4895 {
4896 /* Functions returning procedure pointers. */
4897 gfc_conv_expr (&parmse, e);
4898 if (fsym && fsym->attr.proc_pointer)
4899 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4900 }
4901 else
4902 {
4903 if (e->ts.type == BT_CLASS && fsym
4904 && fsym->ts.type == BT_CLASS
4905 && (!CLASS_DATA (fsym)->as
4906 || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
4907 && CLASS_DATA (e)->attr.codimension)
4908 {
4909 gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
4910 gcc_assert (!CLASS_DATA (fsym)->as);
4911 gfc_add_class_array_ref (e);
4912 parmse.want_coarray = 1;
4913 gfc_conv_expr_reference (&parmse, e);
4914 class_scalar_coarray_to_class (&parmse, e, fsym->ts,
4915 fsym->attr.optional
4916 && e->expr_type == EXPR_VARIABLE);
4917 }
4918 else if (e->ts.type == BT_CLASS && fsym
4919 && fsym->ts.type == BT_CLASS
4920 && !CLASS_DATA (fsym)->as
4921 && !CLASS_DATA (e)->as
4922 && strcmp (fsym->ts.u.derived->name,
4923 e->ts.u.derived->name))
4924 {
4925 type = gfc_typenode_for_spec (&fsym->ts);
4926 var = gfc_create_var (type, fsym->name);
4927 gfc_conv_expr (&parmse, e);
4928 if (fsym->attr.optional
4929 && e->expr_type == EXPR_VARIABLE
4930 && e->symtree->n.sym->attr.optional)
4931 {
4932 stmtblock_t block;
4933 tree cond;
4934 tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
4935 cond = fold_build2_loc (input_location, NE_EXPR,
4936 boolean_type_node, tmp,
4937 fold_convert (TREE_TYPE (tmp),
4938 null_pointer_node));
4939 gfc_start_block (&block);
4940 gfc_add_modify (&block, var,
4941 fold_build1_loc (input_location,
4942 VIEW_CONVERT_EXPR,
4943 type, parmse.expr));
4944 gfc_add_expr_to_block (&parmse.pre,
4945 fold_build3_loc (input_location,
4946 COND_EXPR, void_type_node,
4947 cond, gfc_finish_block (&block),
4948 build_empty_stmt (input_location)));
4949 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
4950 parmse.expr = build3_loc (input_location, COND_EXPR,
4951 TREE_TYPE (parmse.expr),
4952 cond, parmse.expr,
4953 fold_convert (TREE_TYPE (parmse.expr),
4954 null_pointer_node));
4955 }
4956 else
4957 {
4958 gfc_add_modify (&parmse.pre, var,
4959 fold_build1_loc (input_location,
4960 VIEW_CONVERT_EXPR,
4961 type, parmse.expr));
4962 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
4963 }
4964 }
4965 else
4966 gfc_conv_expr_reference (&parmse, e);
4967
4968 /* Catch base objects that are not variables. */
4969 if (e->ts.type == BT_CLASS
4970 && e->expr_type != EXPR_VARIABLE
4971 && expr && e == expr->base_expr)
4972 base_object = build_fold_indirect_ref_loc (input_location,
4973 parmse.expr);
4974
4975 /* A class array element needs converting back to be a
4976 class object, if the formal argument is a class object. */
4977 if (fsym && fsym->ts.type == BT_CLASS
4978 && e->ts.type == BT_CLASS
4979 && ((CLASS_DATA (fsym)->as
4980 && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
4981 || CLASS_DATA (e)->attr.dimension))
4982 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
4983 fsym->attr.intent != INTENT_IN
4984 && (CLASS_DATA (fsym)->attr.class_pointer
4985 || CLASS_DATA (fsym)->attr.allocatable),
4986 fsym->attr.optional
4987 && e->expr_type == EXPR_VARIABLE
4988 && e->symtree->n.sym->attr.optional,
4989 CLASS_DATA (fsym)->attr.class_pointer
4990 || CLASS_DATA (fsym)->attr.allocatable);
4991
4992 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
4993 allocated on entry, it must be deallocated. */
4994 if (fsym && fsym->attr.intent == INTENT_OUT
4995 && (fsym->attr.allocatable
4996 || (fsym->ts.type == BT_CLASS
4997 && CLASS_DATA (fsym)->attr.allocatable)))
4998 {
4999 stmtblock_t block;
5000 tree ptr;
5001
5002 gfc_init_block (&block);
5003 ptr = parmse.expr;
5004 if (e->ts.type == BT_CLASS)
5005 ptr = gfc_class_data_get (ptr);
5006
5007 tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
5008 true, e, e->ts);
5009 gfc_add_expr_to_block (&block, tmp);
5010 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5011 void_type_node, ptr,
5012 null_pointer_node);
5013 gfc_add_expr_to_block (&block, tmp);
5014
5015 if (fsym->ts.type == BT_CLASS && UNLIMITED_POLY (fsym))
5016 {
5017 gfc_add_modify (&block, ptr,
5018 fold_convert (TREE_TYPE (ptr),
5019 null_pointer_node));
5020 gfc_add_expr_to_block (&block, tmp);
5021 }
5022 else if (fsym->ts.type == BT_CLASS)
5023 {
5024 gfc_symbol *vtab;
5025 vtab = gfc_find_derived_vtab (fsym->ts.u.derived);
5026 tmp = gfc_get_symbol_decl (vtab);
5027 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5028 ptr = gfc_class_vptr_get (parmse.expr);
5029 gfc_add_modify (&block, ptr,
5030 fold_convert (TREE_TYPE (ptr), tmp));
5031 gfc_add_expr_to_block (&block, tmp);
5032 }
5033
5034 if (fsym->attr.optional
5035 && e->expr_type == EXPR_VARIABLE
5036 && e->symtree->n.sym->attr.optional)
5037 {
5038 tmp = fold_build3_loc (input_location, COND_EXPR,
5039 void_type_node,
5040 gfc_conv_expr_present (e->symtree->n.sym),
5041 gfc_finish_block (&block),
5042 build_empty_stmt (input_location));
5043 }
5044 else
5045 tmp = gfc_finish_block (&block);
5046
5047 gfc_add_expr_to_block (&se->pre, tmp);
5048 }
5049
5050 if (fsym && (fsym->ts.type == BT_DERIVED
5051 || fsym->ts.type == BT_ASSUMED)
5052 && e->ts.type == BT_CLASS
5053 && !CLASS_DATA (e)->attr.dimension
5054 && !CLASS_DATA (e)->attr.codimension)
5055 parmse.expr = gfc_class_data_get (parmse.expr);
5056
5057 /* Wrap scalar variable in a descriptor. We need to convert
5058 the address of a pointer back to the pointer itself before,
5059 we can assign it to the data field. */
5060
5061 if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
5062 && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
5063 {
5064 tmp = parmse.expr;
5065 if (TREE_CODE (tmp) == ADDR_EXPR
5066 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp, 0))))
5067 tmp = TREE_OPERAND (tmp, 0);
5068 parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
5069 fsym->attr);
5070 parmse.expr = gfc_build_addr_expr (NULL_TREE,
5071 parmse.expr);
5072 }
5073 else if (fsym && e->expr_type != EXPR_NULL
5074 && ((fsym->attr.pointer
5075 && fsym->attr.flavor != FL_PROCEDURE)
5076 || (fsym->attr.proc_pointer
5077 && !(e->expr_type == EXPR_VARIABLE
5078 && e->symtree->n.sym->attr.dummy))
5079 || (fsym->attr.proc_pointer
5080 && e->expr_type == EXPR_VARIABLE
5081 && gfc_is_proc_ptr_comp (e))
5082 || (fsym->attr.allocatable
5083 && fsym->attr.flavor != FL_PROCEDURE)))
5084 {
5085 /* Scalar pointer dummy args require an extra level of
5086 indirection. The null pointer already contains
5087 this level of indirection. */
5088 parm_kind = SCALAR_POINTER;
5089 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5090 }
5091 }
5092 }
5093 else if (e->ts.type == BT_CLASS
5094 && fsym && fsym->ts.type == BT_CLASS
5095 && (CLASS_DATA (fsym)->attr.dimension
5096 || CLASS_DATA (fsym)->attr.codimension))
5097 {
5098 /* Pass a class array. */
5099 parmse.use_offset = 1;
5100 gfc_conv_expr_descriptor (&parmse, e);
5101
5102 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5103 allocated on entry, it must be deallocated. */
5104 if (fsym->attr.intent == INTENT_OUT
5105 && CLASS_DATA (fsym)->attr.allocatable)
5106 {
5107 stmtblock_t block;
5108 tree ptr;
5109
5110 gfc_init_block (&block);
5111 ptr = parmse.expr;
5112 ptr = gfc_class_data_get (ptr);
5113
5114 tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
5115 NULL_TREE, NULL_TREE,
5116 NULL_TREE, true, e,
5117 false);
5118 gfc_add_expr_to_block (&block, tmp);
5119 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5120 void_type_node, ptr,
5121 null_pointer_node);
5122 gfc_add_expr_to_block (&block, tmp);
5123 gfc_reset_vptr (&block, e);
5124
5125 if (fsym->attr.optional
5126 && e->expr_type == EXPR_VARIABLE
5127 && (!e->ref
5128 || (e->ref->type == REF_ARRAY
5129 && e->ref->u.ar.type != AR_FULL))
5130 && e->symtree->n.sym->attr.optional)
5131 {
5132 tmp = fold_build3_loc (input_location, COND_EXPR,
5133 void_type_node,
5134 gfc_conv_expr_present (e->symtree->n.sym),
5135 gfc_finish_block (&block),
5136 build_empty_stmt (input_location));
5137 }
5138 else
5139 tmp = gfc_finish_block (&block);
5140
5141 gfc_add_expr_to_block (&se->pre, tmp);
5142 }
5143
5144 /* The conversion does not repackage the reference to a class
5145 array - _data descriptor. */
5146 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
5147 fsym->attr.intent != INTENT_IN
5148 && (CLASS_DATA (fsym)->attr.class_pointer
5149 || CLASS_DATA (fsym)->attr.allocatable),
5150 fsym->attr.optional
5151 && e->expr_type == EXPR_VARIABLE
5152 && e->symtree->n.sym->attr.optional,
5153 CLASS_DATA (fsym)->attr.class_pointer
5154 || CLASS_DATA (fsym)->attr.allocatable);
5155 }
5156 else
5157 {
5158 /* If the procedure requires an explicit interface, the actual
5159 argument is passed according to the corresponding formal
5160 argument. If the corresponding formal argument is a POINTER,
5161 ALLOCATABLE or assumed shape, we do not use g77's calling
5162 convention, and pass the address of the array descriptor
5163 instead. Otherwise we use g77's calling convention. */
5164 bool f;
5165 f = (fsym != NULL)
5166 && !(fsym->attr.pointer || fsym->attr.allocatable)
5167 && fsym->as && fsym->as->type != AS_ASSUMED_SHAPE
5168 && fsym->as->type != AS_ASSUMED_RANK;
5169 if (comp)
5170 f = f || !comp->attr.always_explicit;
5171 else
5172 f = f || !sym->attr.always_explicit;
5173
5174 /* If the argument is a function call that may not create
5175 a temporary for the result, we have to check that we
5176 can do it, i.e. that there is no alias between this
5177 argument and another one. */
5178 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
5179 {
5180 gfc_expr *iarg;
5181 sym_intent intent;
5182
5183 if (fsym != NULL)
5184 intent = fsym->attr.intent;
5185 else
5186 intent = INTENT_UNKNOWN;
5187
5188 if (gfc_check_fncall_dependency (e, intent, sym, args,
5189 NOT_ELEMENTAL))
5190 parmse.force_tmp = 1;
5191
5192 iarg = e->value.function.actual->expr;
5193
5194 /* Temporary needed if aliasing due to host association. */
5195 if (sym->attr.contained
5196 && !sym->attr.pure
5197 && !sym->attr.implicit_pure
5198 && !sym->attr.use_assoc
5199 && iarg->expr_type == EXPR_VARIABLE
5200 && sym->ns == iarg->symtree->n.sym->ns)
5201 parmse.force_tmp = 1;
5202
5203 /* Ditto within module. */
5204 if (sym->attr.use_assoc
5205 && !sym->attr.pure
5206 && !sym->attr.implicit_pure
5207 && iarg->expr_type == EXPR_VARIABLE
5208 && sym->module == iarg->symtree->n.sym->module)
5209 parmse.force_tmp = 1;
5210 }
5211
5212 if (e->expr_type == EXPR_VARIABLE
5213 && is_subref_array (e))
5214 /* The actual argument is a component reference to an
5215 array of derived types. In this case, the argument
5216 is converted to a temporary, which is passed and then
5217 written back after the procedure call. */
5218 gfc_conv_subref_array_arg (&parmse, e, f,
5219 fsym ? fsym->attr.intent : INTENT_INOUT,
5220 fsym && fsym->attr.pointer);
5221 else if (gfc_is_class_array_ref (e, NULL)
5222 && fsym && fsym->ts.type == BT_DERIVED)
5223 /* The actual argument is a component reference to an
5224 array of derived types. In this case, the argument
5225 is converted to a temporary, which is passed and then
5226 written back after the procedure call.
5227 OOP-TODO: Insert code so that if the dynamic type is
5228 the same as the declared type, copy-in/copy-out does
5229 not occur. */
5230 gfc_conv_subref_array_arg (&parmse, e, f,
5231 fsym ? fsym->attr.intent : INTENT_INOUT,
5232 fsym && fsym->attr.pointer);
5233
5234 else if (gfc_is_alloc_class_array_function (e)
5235 && fsym && fsym->ts.type == BT_DERIVED)
5236 /* See previous comment. For function actual argument,
5237 the write out is not needed so the intent is set as
5238 intent in. */
5239 {
5240 e->must_finalize = 1;
5241 gfc_conv_subref_array_arg (&parmse, e, f,
5242 INTENT_IN,
5243 fsym && fsym->attr.pointer);
5244 }
5245 else
5246 gfc_conv_array_parameter (&parmse, e, f, fsym, sym->name, NULL);
5247
5248 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5249 allocated on entry, it must be deallocated. */
5250 if (fsym && fsym->attr.allocatable
5251 && fsym->attr.intent == INTENT_OUT)
5252 {
5253 tmp = build_fold_indirect_ref_loc (input_location,
5254 parmse.expr);
5255 tmp = gfc_trans_dealloc_allocated (tmp, false, e);
5256 if (fsym->attr.optional
5257 && e->expr_type == EXPR_VARIABLE
5258 && e->symtree->n.sym->attr.optional)
5259 tmp = fold_build3_loc (input_location, COND_EXPR,
5260 void_type_node,
5261 gfc_conv_expr_present (e->symtree->n.sym),
5262 tmp, build_empty_stmt (input_location));
5263 gfc_add_expr_to_block (&se->pre, tmp);
5264 }
5265 }
5266 }
5267
5268 /* The case with fsym->attr.optional is that of a user subroutine
5269 with an interface indicating an optional argument. When we call
5270 an intrinsic subroutine, however, fsym is NULL, but we might still
5271 have an optional argument, so we proceed to the substitution
5272 just in case. */
5273 if (e && (fsym == NULL || fsym->attr.optional))
5274 {
5275 /* If an optional argument is itself an optional dummy argument,
5276 check its presence and substitute a null if absent. This is
5277 only needed when passing an array to an elemental procedure
5278 as then array elements are accessed - or no NULL pointer is
5279 allowed and a "1" or "0" should be passed if not present.
5280 When passing a non-array-descriptor full array to a
5281 non-array-descriptor dummy, no check is needed. For
5282 array-descriptor actual to array-descriptor dummy, see
5283 PR 41911 for why a check has to be inserted.
5284 fsym == NULL is checked as intrinsics required the descriptor
5285 but do not always set fsym. */
5286 if (e->expr_type == EXPR_VARIABLE
5287 && e->symtree->n.sym->attr.optional
5288 && ((e->rank != 0 && sym->attr.elemental)
5289 || e->representation.length || e->ts.type == BT_CHARACTER
5290 || (e->rank != 0
5291 && (fsym == NULL
5292 || (fsym-> as
5293 && (fsym->as->type == AS_ASSUMED_SHAPE
5294 || fsym->as->type == AS_ASSUMED_RANK
5295 || fsym->as->type == AS_DEFERRED))))))
5296 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
5297 e->representation.length);
5298 }
5299
5300 if (fsym && e)
5301 {
5302 /* Obtain the character length of an assumed character length
5303 length procedure from the typespec. */
5304 if (fsym->ts.type == BT_CHARACTER
5305 && parmse.string_length == NULL_TREE
5306 && e->ts.type == BT_PROCEDURE
5307 && e->symtree->n.sym->ts.type == BT_CHARACTER
5308 && e->symtree->n.sym->ts.u.cl->length != NULL
5309 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
5310 {
5311 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
5312 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
5313 }
5314 }
5315
5316 if (fsym && need_interface_mapping && e)
5317 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
5318
5319 gfc_add_block_to_block (&se->pre, &parmse.pre);
5320 gfc_add_block_to_block (&post, &parmse.post);
5321
5322 /* Allocated allocatable components of derived types must be
5323 deallocated for non-variable scalars. Non-variable arrays are
5324 dealt with in trans-array.c(gfc_conv_array_parameter). */
5325 if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
5326 && e->ts.u.derived->attr.alloc_comp
5327 && !(e->symtree && e->symtree->n.sym->attr.pointer)
5328 && (e->expr_type != EXPR_VARIABLE && !e->rank))
5329 {
5330 int parm_rank;
5331 tmp = build_fold_indirect_ref_loc (input_location,
5332 parmse.expr);
5333 parm_rank = e->rank;
5334 switch (parm_kind)
5335 {
5336 case (ELEMENTAL):
5337 case (SCALAR):
5338 parm_rank = 0;
5339 break;
5340
5341 case (SCALAR_POINTER):
5342 tmp = build_fold_indirect_ref_loc (input_location,
5343 tmp);
5344 break;
5345 }
5346
5347 if (e->expr_type == EXPR_OP
5348 && e->value.op.op == INTRINSIC_PARENTHESES
5349 && e->value.op.op1->expr_type == EXPR_VARIABLE)
5350 {
5351 tree local_tmp;
5352 local_tmp = gfc_evaluate_now (tmp, &se->pre);
5353 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
5354 gfc_add_expr_to_block (&se->post, local_tmp);
5355 }
5356
5357 if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
5358 {
5359 /* The derived type is passed to gfc_deallocate_alloc_comp.
5360 Therefore, class actuals can handled correctly but derived
5361 types passed to class formals need the _data component. */
5362 tmp = gfc_class_data_get (tmp);
5363 if (!CLASS_DATA (fsym)->attr.dimension)
5364 tmp = build_fold_indirect_ref_loc (input_location, tmp);
5365 }
5366
5367 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
5368
5369 gfc_add_expr_to_block (&se->post, tmp);
5370 }
5371
5372 /* Add argument checking of passing an unallocated/NULL actual to
5373 a nonallocatable/nonpointer dummy. */
5374
5375 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
5376 {
5377 symbol_attribute attr;
5378 char *msg;
5379 tree cond;
5380
5381 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
5382 attr = gfc_expr_attr (e);
5383 else
5384 goto end_pointer_check;
5385
5386 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
5387 allocatable to an optional dummy, cf. 12.5.2.12. */
5388 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
5389 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5390 goto end_pointer_check;
5391
5392 if (attr.optional)
5393 {
5394 /* If the actual argument is an optional pointer/allocatable and
5395 the formal argument takes an nonpointer optional value,
5396 it is invalid to pass a non-present argument on, even
5397 though there is no technical reason for this in gfortran.
5398 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
5399 tree present, null_ptr, type;
5400
5401 if (attr.allocatable
5402 && (fsym == NULL || !fsym->attr.allocatable))
5403 msg = xasprintf ("Allocatable actual argument '%s' is not "
5404 "allocated or not present",
5405 e->symtree->n.sym->name);
5406 else if (attr.pointer
5407 && (fsym == NULL || !fsym->attr.pointer))
5408 msg = xasprintf ("Pointer actual argument '%s' is not "
5409 "associated or not present",
5410 e->symtree->n.sym->name);
5411 else if (attr.proc_pointer
5412 && (fsym == NULL || !fsym->attr.proc_pointer))
5413 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
5414 "associated or not present",
5415 e->symtree->n.sym->name);
5416 else
5417 goto end_pointer_check;
5418
5419 present = gfc_conv_expr_present (e->symtree->n.sym);
5420 type = TREE_TYPE (present);
5421 present = fold_build2_loc (input_location, EQ_EXPR,
5422 boolean_type_node, present,
5423 fold_convert (type,
5424 null_pointer_node));
5425 type = TREE_TYPE (parmse.expr);
5426 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
5427 boolean_type_node, parmse.expr,
5428 fold_convert (type,
5429 null_pointer_node));
5430 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
5431 boolean_type_node, present, null_ptr);
5432 }
5433 else
5434 {
5435 if (attr.allocatable
5436 && (fsym == NULL || !fsym->attr.allocatable))
5437 msg = xasprintf ("Allocatable actual argument '%s' is not "
5438 "allocated", e->symtree->n.sym->name);
5439 else if (attr.pointer
5440 && (fsym == NULL || !fsym->attr.pointer))
5441 msg = xasprintf ("Pointer actual argument '%s' is not "
5442 "associated", e->symtree->n.sym->name);
5443 else if (attr.proc_pointer
5444 && (fsym == NULL || !fsym->attr.proc_pointer))
5445 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
5446 "associated", e->symtree->n.sym->name);
5447 else
5448 goto end_pointer_check;
5449
5450 tmp = parmse.expr;
5451
5452 /* If the argument is passed by value, we need to strip the
5453 INDIRECT_REF. */
5454 if (!POINTER_TYPE_P (TREE_TYPE (parmse.expr)))
5455 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5456
5457 cond = fold_build2_loc (input_location, EQ_EXPR,
5458 boolean_type_node, tmp,
5459 fold_convert (TREE_TYPE (tmp),
5460 null_pointer_node));
5461 }
5462
5463 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
5464 msg);
5465 free (msg);
5466 }
5467 end_pointer_check:
5468
5469 /* Deferred length dummies pass the character length by reference
5470 so that the value can be returned. */
5471 if (parmse.string_length && fsym && fsym->ts.deferred)
5472 {
5473 if (INDIRECT_REF_P (parmse.string_length))
5474 /* In chains of functions/procedure calls the string_length already
5475 is a pointer to the variable holding the length. Therefore
5476 remove the deref on call. */
5477 parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
5478 else
5479 {
5480 tmp = parmse.string_length;
5481 if (TREE_CODE (tmp) != VAR_DECL)
5482 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
5483 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
5484 }
5485 }
5486
5487 /* Character strings are passed as two parameters, a length and a
5488 pointer - except for Bind(c) which only passes the pointer.
5489 An unlimited polymorphic formal argument likewise does not
5490 need the length. */
5491 if (parmse.string_length != NULL_TREE
5492 && !sym->attr.is_bind_c
5493 && !(fsym && UNLIMITED_POLY (fsym)))
5494 vec_safe_push (stringargs, parmse.string_length);
5495
5496 /* When calling __copy for character expressions to unlimited
5497 polymorphic entities, the dst argument needs a string length. */
5498 if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
5499 && strncmp (sym->name, "__vtab_CHARACTER", 16) == 0
5500 && arg->next && arg->next->expr
5501 && arg->next->expr->ts.type == BT_DERIVED
5502 && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
5503 vec_safe_push (stringargs, parmse.string_length);
5504
5505 /* For descriptorless coarrays and assumed-shape coarray dummies, we
5506 pass the token and the offset as additional arguments. */
5507 if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
5508 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
5509 && !fsym->attr.allocatable)
5510 || (fsym->ts.type == BT_CLASS
5511 && CLASS_DATA (fsym)->attr.codimension
5512 && !CLASS_DATA (fsym)->attr.allocatable)))
5513 {
5514 /* Token and offset. */
5515 vec_safe_push (stringargs, null_pointer_node);
5516 vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
5517 gcc_assert (fsym->attr.optional);
5518 }
5519 else if (fsym && flag_coarray == GFC_FCOARRAY_LIB
5520 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
5521 && !fsym->attr.allocatable)
5522 || (fsym->ts.type == BT_CLASS
5523 && CLASS_DATA (fsym)->attr.codimension
5524 && !CLASS_DATA (fsym)->attr.allocatable)))
5525 {
5526 tree caf_decl, caf_type;
5527 tree offset, tmp2;
5528
5529 caf_decl = gfc_get_tree_for_caf_expr (e);
5530 caf_type = TREE_TYPE (caf_decl);
5531
5532 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
5533 && (GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE
5534 || GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_POINTER))
5535 tmp = gfc_conv_descriptor_token (caf_decl);
5536 else if (DECL_LANG_SPECIFIC (caf_decl)
5537 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
5538 tmp = GFC_DECL_TOKEN (caf_decl);
5539 else
5540 {
5541 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
5542 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
5543 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
5544 }
5545
5546 vec_safe_push (stringargs, tmp);
5547
5548 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
5549 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
5550 offset = build_int_cst (gfc_array_index_type, 0);
5551 else if (DECL_LANG_SPECIFIC (caf_decl)
5552 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
5553 offset = GFC_DECL_CAF_OFFSET (caf_decl);
5554 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
5555 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
5556 else
5557 offset = build_int_cst (gfc_array_index_type, 0);
5558
5559 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
5560 tmp = gfc_conv_descriptor_data_get (caf_decl);
5561 else
5562 {
5563 gcc_assert (POINTER_TYPE_P (caf_type));
5564 tmp = caf_decl;
5565 }
5566
5567 tmp2 = fsym->ts.type == BT_CLASS
5568 ? gfc_class_data_get (parmse.expr) : parmse.expr;
5569 if ((fsym->ts.type != BT_CLASS
5570 && (fsym->as->type == AS_ASSUMED_SHAPE
5571 || fsym->as->type == AS_ASSUMED_RANK))
5572 || (fsym->ts.type == BT_CLASS
5573 && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
5574 || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
5575 {
5576 if (fsym->ts.type == BT_CLASS)
5577 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
5578 else
5579 {
5580 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
5581 tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
5582 }
5583 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
5584 tmp2 = gfc_conv_descriptor_data_get (tmp2);
5585 }
5586 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
5587 tmp2 = gfc_conv_descriptor_data_get (tmp2);
5588 else
5589 {
5590 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
5591 }
5592
5593 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5594 gfc_array_index_type,
5595 fold_convert (gfc_array_index_type, tmp2),
5596 fold_convert (gfc_array_index_type, tmp));
5597 offset = fold_build2_loc (input_location, PLUS_EXPR,
5598 gfc_array_index_type, offset, tmp);
5599
5600 vec_safe_push (stringargs, offset);
5601 }
5602
5603 vec_safe_push (arglist, parmse.expr);
5604 }
5605 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
5606
5607 if (comp)
5608 ts = comp->ts;
5609 else
5610 ts = sym->ts;
5611
5612 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
5613 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
5614 else if (ts.type == BT_CHARACTER)
5615 {
5616 if (ts.u.cl->length == NULL)
5617 {
5618 /* Assumed character length results are not allowed by 5.1.1.5 of the
5619 standard and are trapped in resolve.c; except in the case of SPREAD
5620 (and other intrinsics?) and dummy functions. In the case of SPREAD,
5621 we take the character length of the first argument for the result.
5622 For dummies, we have to look through the formal argument list for
5623 this function and use the character length found there.*/
5624 if (ts.deferred)
5625 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
5626 else if (!sym->attr.dummy)
5627 cl.backend_decl = (*stringargs)[0];
5628 else
5629 {
5630 formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
5631 for (; formal; formal = formal->next)
5632 if (strcmp (formal->sym->name, sym->name) == 0)
5633 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
5634 }
5635 len = cl.backend_decl;
5636 }
5637 else
5638 {
5639 tree tmp;
5640
5641 /* Calculate the length of the returned string. */
5642 gfc_init_se (&parmse, NULL);
5643 if (need_interface_mapping)
5644 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
5645 else
5646 gfc_conv_expr (&parmse, ts.u.cl->length);
5647 gfc_add_block_to_block (&se->pre, &parmse.pre);
5648 gfc_add_block_to_block (&se->post, &parmse.post);
5649
5650 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
5651 tmp = fold_build2_loc (input_location, MAX_EXPR,
5652 gfc_charlen_type_node, tmp,
5653 build_int_cst (gfc_charlen_type_node, 0));
5654 cl.backend_decl = tmp;
5655 }
5656
5657 /* Set up a charlen structure for it. */
5658 cl.next = NULL;
5659 cl.length = NULL;
5660 ts.u.cl = &cl;
5661
5662 len = cl.backend_decl;
5663 }
5664
5665 byref = (comp && (comp->attr.dimension || comp->ts.type == BT_CHARACTER))
5666 || (!comp && gfc_return_by_reference (sym));
5667 if (byref)
5668 {
5669 if (se->direct_byref)
5670 {
5671 /* Sometimes, too much indirection can be applied; e.g. for
5672 function_result = array_valued_recursive_function. */
5673 if (TREE_TYPE (TREE_TYPE (se->expr))
5674 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
5675 && GFC_DESCRIPTOR_TYPE_P
5676 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
5677 se->expr = build_fold_indirect_ref_loc (input_location,
5678 se->expr);
5679
5680 /* If the lhs of an assignment x = f(..) is allocatable and
5681 f2003 is allowed, we must do the automatic reallocation.
5682 TODO - deal with intrinsics, without using a temporary. */
5683 if (flag_realloc_lhs
5684 && se->ss && se->ss->loop_chain
5685 && se->ss->loop_chain->is_alloc_lhs
5686 && !expr->value.function.isym
5687 && sym->result->as != NULL)
5688 {
5689 /* Evaluate the bounds of the result, if known. */
5690 gfc_set_loop_bounds_from_array_spec (&mapping, se,
5691 sym->result->as);
5692
5693 /* Perform the automatic reallocation. */
5694 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
5695 expr, NULL);
5696 gfc_add_expr_to_block (&se->pre, tmp);
5697
5698 /* Pass the temporary as the first argument. */
5699 result = info->descriptor;
5700 }
5701 else
5702 result = build_fold_indirect_ref_loc (input_location,
5703 se->expr);
5704 vec_safe_push (retargs, se->expr);
5705 }
5706 else if (comp && comp->attr.dimension)
5707 {
5708 gcc_assert (se->loop && info);
5709
5710 /* Set the type of the array. */
5711 tmp = gfc_typenode_for_spec (&comp->ts);
5712 gcc_assert (se->ss->dimen == se->loop->dimen);
5713
5714 /* Evaluate the bounds of the result, if known. */
5715 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
5716
5717 /* If the lhs of an assignment x = f(..) is allocatable and
5718 f2003 is allowed, we must not generate the function call
5719 here but should just send back the results of the mapping.
5720 This is signalled by the function ss being flagged. */
5721 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
5722 {
5723 gfc_free_interface_mapping (&mapping);
5724 return has_alternate_specifier;
5725 }
5726
5727 /* Create a temporary to store the result. In case the function
5728 returns a pointer, the temporary will be a shallow copy and
5729 mustn't be deallocated. */
5730 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
5731 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5732 tmp, NULL_TREE, false,
5733 !comp->attr.pointer, callee_alloc,
5734 &se->ss->info->expr->where);
5735
5736 /* Pass the temporary as the first argument. */
5737 result = info->descriptor;
5738 tmp = gfc_build_addr_expr (NULL_TREE, result);
5739 vec_safe_push (retargs, tmp);
5740 }
5741 else if (!comp && sym->result->attr.dimension)
5742 {
5743 gcc_assert (se->loop && info);
5744
5745 /* Set the type of the array. */
5746 tmp = gfc_typenode_for_spec (&ts);
5747 gcc_assert (se->ss->dimen == se->loop->dimen);
5748
5749 /* Evaluate the bounds of the result, if known. */
5750 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
5751
5752 /* If the lhs of an assignment x = f(..) is allocatable and
5753 f2003 is allowed, we must not generate the function call
5754 here but should just send back the results of the mapping.
5755 This is signalled by the function ss being flagged. */
5756 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
5757 {
5758 gfc_free_interface_mapping (&mapping);
5759 return has_alternate_specifier;
5760 }
5761
5762 /* Create a temporary to store the result. In case the function
5763 returns a pointer, the temporary will be a shallow copy and
5764 mustn't be deallocated. */
5765 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
5766 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5767 tmp, NULL_TREE, false,
5768 !sym->attr.pointer, callee_alloc,
5769 &se->ss->info->expr->where);
5770
5771 /* Pass the temporary as the first argument. */
5772 result = info->descriptor;
5773 tmp = gfc_build_addr_expr (NULL_TREE, result);
5774 vec_safe_push (retargs, tmp);
5775 }
5776 else if (ts.type == BT_CHARACTER)
5777 {
5778 /* Pass the string length. */
5779 type = gfc_get_character_type (ts.kind, ts.u.cl);
5780 type = build_pointer_type (type);
5781
5782 /* Return an address to a char[0:len-1]* temporary for
5783 character pointers. */
5784 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5785 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
5786 {
5787 var = gfc_create_var (type, "pstr");
5788
5789 if ((!comp && sym->attr.allocatable)
5790 || (comp && comp->attr.allocatable))
5791 {
5792 gfc_add_modify (&se->pre, var,
5793 fold_convert (TREE_TYPE (var),
5794 null_pointer_node));
5795 tmp = gfc_call_free (convert (pvoid_type_node, var));
5796 gfc_add_expr_to_block (&se->post, tmp);
5797 }
5798
5799 /* Provide an address expression for the function arguments. */
5800 var = gfc_build_addr_expr (NULL_TREE, var);
5801 }
5802 else
5803 var = gfc_conv_string_tmp (se, type, len);
5804
5805 vec_safe_push (retargs, var);
5806 }
5807 else
5808 {
5809 gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
5810
5811 type = gfc_get_complex_type (ts.kind);
5812 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
5813 vec_safe_push (retargs, var);
5814 }
5815
5816 /* Add the string length to the argument list. */
5817 if (ts.type == BT_CHARACTER && ts.deferred)
5818 {
5819 tmp = len;
5820 if (TREE_CODE (tmp) != VAR_DECL)
5821 tmp = gfc_evaluate_now (len, &se->pre);
5822 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5823 vec_safe_push (retargs, tmp);
5824 }
5825 else if (ts.type == BT_CHARACTER)
5826 vec_safe_push (retargs, len);
5827 }
5828 gfc_free_interface_mapping (&mapping);
5829
5830 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
5831 arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
5832 + vec_safe_length (stringargs) + vec_safe_length (append_args));
5833 vec_safe_reserve (retargs, arglen);
5834
5835 /* Add the return arguments. */
5836 retargs->splice (arglist);
5837
5838 /* Add the hidden present status for optional+value to the arguments. */
5839 retargs->splice (optionalargs);
5840
5841 /* Add the hidden string length parameters to the arguments. */
5842 retargs->splice (stringargs);
5843
5844 /* We may want to append extra arguments here. This is used e.g. for
5845 calls to libgfortran_matmul_??, which need extra information. */
5846 if (!vec_safe_is_empty (append_args))
5847 retargs->splice (append_args);
5848 arglist = retargs;
5849
5850 /* Generate the actual call. */
5851 if (base_object == NULL_TREE)
5852 conv_function_val (se, sym, expr);
5853 else
5854 conv_base_obj_fcn_val (se, base_object, expr);
5855
5856 /* If there are alternate return labels, function type should be
5857 integer. Can't modify the type in place though, since it can be shared
5858 with other functions. For dummy arguments, the typing is done to
5859 this result, even if it has to be repeated for each call. */
5860 if (has_alternate_specifier
5861 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
5862 {
5863 if (!sym->attr.dummy)
5864 {
5865 TREE_TYPE (sym->backend_decl)
5866 = build_function_type (integer_type_node,
5867 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
5868 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
5869 }
5870 else
5871 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
5872 }
5873
5874 fntype = TREE_TYPE (TREE_TYPE (se->expr));
5875 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
5876
5877 /* If we have a pointer function, but we don't want a pointer, e.g.
5878 something like
5879 x = f()
5880 where f is pointer valued, we have to dereference the result. */
5881 if (!se->want_pointer && !byref
5882 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5883 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
5884 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
5885
5886 /* f2c calling conventions require a scalar default real function to
5887 return a double precision result. Convert this back to default
5888 real. We only care about the cases that can happen in Fortran 77.
5889 */
5890 if (flag_f2c && sym->ts.type == BT_REAL
5891 && sym->ts.kind == gfc_default_real_kind
5892 && !sym->attr.always_explicit)
5893 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
5894
5895 /* A pure function may still have side-effects - it may modify its
5896 parameters. */
5897 TREE_SIDE_EFFECTS (se->expr) = 1;
5898 #if 0
5899 if (!sym->attr.pure)
5900 TREE_SIDE_EFFECTS (se->expr) = 1;
5901 #endif
5902
5903 if (byref)
5904 {
5905 /* Add the function call to the pre chain. There is no expression. */
5906 gfc_add_expr_to_block (&se->pre, se->expr);
5907 se->expr = NULL_TREE;
5908
5909 if (!se->direct_byref)
5910 {
5911 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
5912 {
5913 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
5914 {
5915 /* Check the data pointer hasn't been modified. This would
5916 happen in a function returning a pointer. */
5917 tmp = gfc_conv_descriptor_data_get (info->descriptor);
5918 tmp = fold_build2_loc (input_location, NE_EXPR,
5919 boolean_type_node,
5920 tmp, info->data);
5921 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
5922 gfc_msg_fault);
5923 }
5924 se->expr = info->descriptor;
5925 /* Bundle in the string length. */
5926 se->string_length = len;
5927 }
5928 else if (ts.type == BT_CHARACTER)
5929 {
5930 /* Dereference for character pointer results. */
5931 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5932 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
5933 se->expr = build_fold_indirect_ref_loc (input_location, var);
5934 else
5935 se->expr = var;
5936
5937 se->string_length = len;
5938 }
5939 else
5940 {
5941 gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
5942 se->expr = build_fold_indirect_ref_loc (input_location, var);
5943 }
5944 }
5945 }
5946
5947 /* Follow the function call with the argument post block. */
5948 if (byref)
5949 {
5950 gfc_add_block_to_block (&se->pre, &post);
5951
5952 /* Transformational functions of derived types with allocatable
5953 components must have the result allocatable components copied. */
5954 arg = expr->value.function.actual;
5955 if (result && arg && expr->rank
5956 && expr->value.function.isym
5957 && expr->value.function.isym->transformational
5958 && arg->expr->ts.type == BT_DERIVED
5959 && arg->expr->ts.u.derived->attr.alloc_comp)
5960 {
5961 tree tmp2;
5962 /* Copy the allocatable components. We have to use a
5963 temporary here to prevent source allocatable components
5964 from being corrupted. */
5965 tmp2 = gfc_evaluate_now (result, &se->pre);
5966 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
5967 result, tmp2, expr->rank);
5968 gfc_add_expr_to_block (&se->pre, tmp);
5969 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
5970 expr->rank);
5971 gfc_add_expr_to_block (&se->pre, tmp);
5972
5973 /* Finally free the temporary's data field. */
5974 tmp = gfc_conv_descriptor_data_get (tmp2);
5975 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
5976 NULL_TREE, NULL_TREE, true,
5977 NULL, false);
5978 gfc_add_expr_to_block (&se->pre, tmp);
5979 }
5980 }
5981 else
5982 {
5983 /* For a function with a class array result, save the result as
5984 a temporary, set the info fields needed by the scalarizer and
5985 call the finalization function of the temporary. Note that the
5986 nullification of allocatable components needed by the result
5987 is done in gfc_trans_assignment_1. */
5988 if (expr && ((gfc_is_alloc_class_array_function (expr)
5989 && se->ss && se->ss->loop)
5990 || gfc_is_alloc_class_scalar_function (expr))
5991 && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
5992 && expr->must_finalize)
5993 {
5994 tree final_fndecl;
5995 tree is_final;
5996 int n;
5997 if (se->ss && se->ss->loop)
5998 {
5999 se->expr = gfc_evaluate_now (se->expr, &se->ss->loop->pre);
6000 tmp = gfc_class_data_get (se->expr);
6001 info->descriptor = tmp;
6002 info->data = gfc_conv_descriptor_data_get (tmp);
6003 info->offset = gfc_conv_descriptor_offset_get (tmp);
6004 for (n = 0; n < se->ss->loop->dimen; n++)
6005 {
6006 tree dim = gfc_rank_cst[n];
6007 se->ss->loop->to[n] = gfc_conv_descriptor_ubound_get (tmp, dim);
6008 se->ss->loop->from[n] = gfc_conv_descriptor_lbound_get (tmp, dim);
6009 }
6010 }
6011 else
6012 {
6013 /* TODO Eliminate the doubling of temporaries. This
6014 one is necessary to ensure no memory leakage. */
6015 se->expr = gfc_evaluate_now (se->expr, &se->pre);
6016 tmp = gfc_class_data_get (se->expr);
6017 tmp = gfc_conv_scalar_to_descriptor (se, tmp,
6018 CLASS_DATA (expr->value.function.esym->result)->attr);
6019 }
6020
6021 final_fndecl = gfc_class_vtab_final_get (se->expr);
6022 is_final = fold_build2_loc (input_location, NE_EXPR,
6023 boolean_type_node,
6024 final_fndecl,
6025 fold_convert (TREE_TYPE (final_fndecl),
6026 null_pointer_node));
6027 final_fndecl = build_fold_indirect_ref_loc (input_location,
6028 final_fndecl);
6029 tmp = build_call_expr_loc (input_location,
6030 final_fndecl, 3,
6031 gfc_build_addr_expr (NULL, tmp),
6032 gfc_class_vtab_size_get (se->expr),
6033 boolean_false_node);
6034 tmp = fold_build3_loc (input_location, COND_EXPR,
6035 void_type_node, is_final, tmp,
6036 build_empty_stmt (input_location));
6037
6038 if (se->ss && se->ss->loop)
6039 {
6040 gfc_add_expr_to_block (&se->ss->loop->post, tmp);
6041 tmp = gfc_call_free (convert (pvoid_type_node, info->data));
6042 gfc_add_expr_to_block (&se->ss->loop->post, tmp);
6043 }
6044 else
6045 {
6046 gfc_add_expr_to_block (&se->post, tmp);
6047 tmp = gfc_class_data_get (se->expr);
6048 tmp = gfc_call_free (convert (pvoid_type_node, tmp));
6049 gfc_add_expr_to_block (&se->post, tmp);
6050 }
6051 expr->must_finalize = 0;
6052 }
6053
6054 gfc_add_block_to_block (&se->post, &post);
6055 }
6056
6057 return has_alternate_specifier;
6058 }
6059
6060
6061 /* Fill a character string with spaces. */
6062
6063 static tree
6064 fill_with_spaces (tree start, tree type, tree size)
6065 {
6066 stmtblock_t block, loop;
6067 tree i, el, exit_label, cond, tmp;
6068
6069 /* For a simple char type, we can call memset(). */
6070 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
6071 return build_call_expr_loc (input_location,
6072 builtin_decl_explicit (BUILT_IN_MEMSET),
6073 3, start,
6074 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
6075 lang_hooks.to_target_charset (' ')),
6076 size);
6077
6078 /* Otherwise, we use a loop:
6079 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
6080 *el = (type) ' ';
6081 */
6082
6083 /* Initialize variables. */
6084 gfc_init_block (&block);
6085 i = gfc_create_var (sizetype, "i");
6086 gfc_add_modify (&block, i, fold_convert (sizetype, size));
6087 el = gfc_create_var (build_pointer_type (type), "el");
6088 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
6089 exit_label = gfc_build_label_decl (NULL_TREE);
6090 TREE_USED (exit_label) = 1;
6091
6092
6093 /* Loop body. */
6094 gfc_init_block (&loop);
6095
6096 /* Exit condition. */
6097 cond = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, i,
6098 build_zero_cst (sizetype));
6099 tmp = build1_v (GOTO_EXPR, exit_label);
6100 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
6101 build_empty_stmt (input_location));
6102 gfc_add_expr_to_block (&loop, tmp);
6103
6104 /* Assignment. */
6105 gfc_add_modify (&loop,
6106 fold_build1_loc (input_location, INDIRECT_REF, type, el),
6107 build_int_cst (type, lang_hooks.to_target_charset (' ')));
6108
6109 /* Increment loop variables. */
6110 gfc_add_modify (&loop, i,
6111 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
6112 TYPE_SIZE_UNIT (type)));
6113 gfc_add_modify (&loop, el,
6114 fold_build_pointer_plus_loc (input_location,
6115 el, TYPE_SIZE_UNIT (type)));
6116
6117 /* Making the loop... actually loop! */
6118 tmp = gfc_finish_block (&loop);
6119 tmp = build1_v (LOOP_EXPR, tmp);
6120 gfc_add_expr_to_block (&block, tmp);
6121
6122 /* The exit label. */
6123 tmp = build1_v (LABEL_EXPR, exit_label);
6124 gfc_add_expr_to_block (&block, tmp);
6125
6126
6127 return gfc_finish_block (&block);
6128 }
6129
6130
6131 /* Generate code to copy a string. */
6132
6133 void
6134 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
6135 int dkind, tree slength, tree src, int skind)
6136 {
6137 tree tmp, dlen, slen;
6138 tree dsc;
6139 tree ssc;
6140 tree cond;
6141 tree cond2;
6142 tree tmp2;
6143 tree tmp3;
6144 tree tmp4;
6145 tree chartype;
6146 stmtblock_t tempblock;
6147
6148 gcc_assert (dkind == skind);
6149
6150 if (slength != NULL_TREE)
6151 {
6152 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
6153 ssc = gfc_string_to_single_character (slen, src, skind);
6154 }
6155 else
6156 {
6157 slen = build_int_cst (size_type_node, 1);
6158 ssc = src;
6159 }
6160
6161 if (dlength != NULL_TREE)
6162 {
6163 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
6164 dsc = gfc_string_to_single_character (dlen, dest, dkind);
6165 }
6166 else
6167 {
6168 dlen = build_int_cst (size_type_node, 1);
6169 dsc = dest;
6170 }
6171
6172 /* Assign directly if the types are compatible. */
6173 if (dsc != NULL_TREE && ssc != NULL_TREE
6174 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
6175 {
6176 gfc_add_modify (block, dsc, ssc);
6177 return;
6178 }
6179
6180 /* Do nothing if the destination length is zero. */
6181 cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, dlen,
6182 build_int_cst (size_type_node, 0));
6183
6184 /* The following code was previously in _gfortran_copy_string:
6185
6186 // The two strings may overlap so we use memmove.
6187 void
6188 copy_string (GFC_INTEGER_4 destlen, char * dest,
6189 GFC_INTEGER_4 srclen, const char * src)
6190 {
6191 if (srclen >= destlen)
6192 {
6193 // This will truncate if too long.
6194 memmove (dest, src, destlen);
6195 }
6196 else
6197 {
6198 memmove (dest, src, srclen);
6199 // Pad with spaces.
6200 memset (&dest[srclen], ' ', destlen - srclen);
6201 }
6202 }
6203
6204 We're now doing it here for better optimization, but the logic
6205 is the same. */
6206
6207 /* For non-default character kinds, we have to multiply the string
6208 length by the base type size. */
6209 chartype = gfc_get_char_type (dkind);
6210 slen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6211 fold_convert (size_type_node, slen),
6212 fold_convert (size_type_node,
6213 TYPE_SIZE_UNIT (chartype)));
6214 dlen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6215 fold_convert (size_type_node, dlen),
6216 fold_convert (size_type_node,
6217 TYPE_SIZE_UNIT (chartype)));
6218
6219 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
6220 dest = fold_convert (pvoid_type_node, dest);
6221 else
6222 dest = gfc_build_addr_expr (pvoid_type_node, dest);
6223
6224 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
6225 src = fold_convert (pvoid_type_node, src);
6226 else
6227 src = gfc_build_addr_expr (pvoid_type_node, src);
6228
6229 /* Truncate string if source is too long. */
6230 cond2 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, slen,
6231 dlen);
6232 tmp2 = build_call_expr_loc (input_location,
6233 builtin_decl_explicit (BUILT_IN_MEMMOVE),
6234 3, dest, src, dlen);
6235
6236 /* Else copy and pad with spaces. */
6237 tmp3 = build_call_expr_loc (input_location,
6238 builtin_decl_explicit (BUILT_IN_MEMMOVE),
6239 3, dest, src, slen);
6240
6241 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
6242 tmp4 = fill_with_spaces (tmp4, chartype,
6243 fold_build2_loc (input_location, MINUS_EXPR,
6244 TREE_TYPE(dlen), dlen, slen));
6245
6246 gfc_init_block (&tempblock);
6247 gfc_add_expr_to_block (&tempblock, tmp3);
6248 gfc_add_expr_to_block (&tempblock, tmp4);
6249 tmp3 = gfc_finish_block (&tempblock);
6250
6251 /* The whole copy_string function is there. */
6252 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
6253 tmp2, tmp3);
6254 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
6255 build_empty_stmt (input_location));
6256 gfc_add_expr_to_block (block, tmp);
6257 }
6258
6259
6260 /* Translate a statement function.
6261 The value of a statement function reference is obtained by evaluating the
6262 expression using the values of the actual arguments for the values of the
6263 corresponding dummy arguments. */
6264
6265 static void
6266 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
6267 {
6268 gfc_symbol *sym;
6269 gfc_symbol *fsym;
6270 gfc_formal_arglist *fargs;
6271 gfc_actual_arglist *args;
6272 gfc_se lse;
6273 gfc_se rse;
6274 gfc_saved_var *saved_vars;
6275 tree *temp_vars;
6276 tree type;
6277 tree tmp;
6278 int n;
6279
6280 sym = expr->symtree->n.sym;
6281 args = expr->value.function.actual;
6282 gfc_init_se (&lse, NULL);
6283 gfc_init_se (&rse, NULL);
6284
6285 n = 0;
6286 for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
6287 n++;
6288 saved_vars = XCNEWVEC (gfc_saved_var, n);
6289 temp_vars = XCNEWVEC (tree, n);
6290
6291 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6292 fargs = fargs->next, n++)
6293 {
6294 /* Each dummy shall be specified, explicitly or implicitly, to be
6295 scalar. */
6296 gcc_assert (fargs->sym->attr.dimension == 0);
6297 fsym = fargs->sym;
6298
6299 if (fsym->ts.type == BT_CHARACTER)
6300 {
6301 /* Copy string arguments. */
6302 tree arglen;
6303
6304 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
6305 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
6306
6307 /* Create a temporary to hold the value. */
6308 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
6309 fsym->ts.u.cl->backend_decl
6310 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
6311
6312 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
6313 temp_vars[n] = gfc_create_var (type, fsym->name);
6314
6315 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
6316
6317 gfc_conv_expr (&rse, args->expr);
6318 gfc_conv_string_parameter (&rse);
6319 gfc_add_block_to_block (&se->pre, &lse.pre);
6320 gfc_add_block_to_block (&se->pre, &rse.pre);
6321
6322 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
6323 rse.string_length, rse.expr, fsym->ts.kind);
6324 gfc_add_block_to_block (&se->pre, &lse.post);
6325 gfc_add_block_to_block (&se->pre, &rse.post);
6326 }
6327 else
6328 {
6329 /* For everything else, just evaluate the expression. */
6330
6331 /* Create a temporary to hold the value. */
6332 type = gfc_typenode_for_spec (&fsym->ts);
6333 temp_vars[n] = gfc_create_var (type, fsym->name);
6334
6335 gfc_conv_expr (&lse, args->expr);
6336
6337 gfc_add_block_to_block (&se->pre, &lse.pre);
6338 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
6339 gfc_add_block_to_block (&se->pre, &lse.post);
6340 }
6341
6342 args = args->next;
6343 }
6344
6345 /* Use the temporary variables in place of the real ones. */
6346 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6347 fargs = fargs->next, n++)
6348 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
6349
6350 gfc_conv_expr (se, sym->value);
6351
6352 if (sym->ts.type == BT_CHARACTER)
6353 {
6354 gfc_conv_const_charlen (sym->ts.u.cl);
6355
6356 /* Force the expression to the correct length. */
6357 if (!INTEGER_CST_P (se->string_length)
6358 || tree_int_cst_lt (se->string_length,
6359 sym->ts.u.cl->backend_decl))
6360 {
6361 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
6362 tmp = gfc_create_var (type, sym->name);
6363 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
6364 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
6365 sym->ts.kind, se->string_length, se->expr,
6366 sym->ts.kind);
6367 se->expr = tmp;
6368 }
6369 se->string_length = sym->ts.u.cl->backend_decl;
6370 }
6371
6372 /* Restore the original variables. */
6373 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6374 fargs = fargs->next, n++)
6375 gfc_restore_sym (fargs->sym, &saved_vars[n]);
6376 free (temp_vars);
6377 free (saved_vars);
6378 }
6379
6380
6381 /* Translate a function expression. */
6382
6383 static void
6384 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
6385 {
6386 gfc_symbol *sym;
6387
6388 if (expr->value.function.isym)
6389 {
6390 gfc_conv_intrinsic_function (se, expr);
6391 return;
6392 }
6393
6394 /* expr.value.function.esym is the resolved (specific) function symbol for
6395 most functions. However this isn't set for dummy procedures. */
6396 sym = expr->value.function.esym;
6397 if (!sym)
6398 sym = expr->symtree->n.sym;
6399
6400 /* The IEEE_ARITHMETIC functions are caught here. */
6401 if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
6402 if (gfc_conv_ieee_arithmetic_function (se, expr))
6403 return;
6404
6405 /* We distinguish statement functions from general functions to improve
6406 runtime performance. */
6407 if (sym->attr.proc == PROC_ST_FUNCTION)
6408 {
6409 gfc_conv_statement_function (se, expr);
6410 return;
6411 }
6412
6413 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
6414 NULL);
6415 }
6416
6417
6418 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
6419
6420 static bool
6421 is_zero_initializer_p (gfc_expr * expr)
6422 {
6423 if (expr->expr_type != EXPR_CONSTANT)
6424 return false;
6425
6426 /* We ignore constants with prescribed memory representations for now. */
6427 if (expr->representation.string)
6428 return false;
6429
6430 switch (expr->ts.type)
6431 {
6432 case BT_INTEGER:
6433 return mpz_cmp_si (expr->value.integer, 0) == 0;
6434
6435 case BT_REAL:
6436 return mpfr_zero_p (expr->value.real)
6437 && MPFR_SIGN (expr->value.real) >= 0;
6438
6439 case BT_LOGICAL:
6440 return expr->value.logical == 0;
6441
6442 case BT_COMPLEX:
6443 return mpfr_zero_p (mpc_realref (expr->value.complex))
6444 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
6445 && mpfr_zero_p (mpc_imagref (expr->value.complex))
6446 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
6447
6448 default:
6449 break;
6450 }
6451 return false;
6452 }
6453
6454
6455 static void
6456 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
6457 {
6458 gfc_ss *ss;
6459
6460 ss = se->ss;
6461 gcc_assert (ss != NULL && ss != gfc_ss_terminator);
6462 gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
6463
6464 gfc_conv_tmp_array_ref (se);
6465 }
6466
6467
6468 /* Build a static initializer. EXPR is the expression for the initial value.
6469 The other parameters describe the variable of the component being
6470 initialized. EXPR may be null. */
6471
6472 tree
6473 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
6474 bool array, bool pointer, bool procptr)
6475 {
6476 gfc_se se;
6477
6478 if (!(expr || pointer || procptr))
6479 return NULL_TREE;
6480
6481 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
6482 (these are the only two iso_c_binding derived types that can be
6483 used as initialization expressions). If so, we need to modify
6484 the 'expr' to be that for a (void *). */
6485 if (expr != NULL && expr->ts.type == BT_DERIVED
6486 && expr->ts.is_iso_c && expr->ts.u.derived)
6487 {
6488 gfc_symbol *derived = expr->ts.u.derived;
6489
6490 /* The derived symbol has already been converted to a (void *). Use
6491 its kind. */
6492 expr = gfc_get_int_expr (derived->ts.kind, NULL, 0);
6493 expr->ts.f90_type = derived->ts.f90_type;
6494
6495 gfc_init_se (&se, NULL);
6496 gfc_conv_constant (&se, expr);
6497 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6498 return se.expr;
6499 }
6500
6501 if (array && !procptr)
6502 {
6503 tree ctor;
6504 /* Arrays need special handling. */
6505 if (pointer)
6506 ctor = gfc_build_null_descriptor (type);
6507 /* Special case assigning an array to zero. */
6508 else if (is_zero_initializer_p (expr))
6509 ctor = build_constructor (type, NULL);
6510 else
6511 ctor = gfc_conv_array_initializer (type, expr);
6512 TREE_STATIC (ctor) = 1;
6513 return ctor;
6514 }
6515 else if (pointer || procptr)
6516 {
6517 if (ts->type == BT_CLASS && !procptr)
6518 {
6519 gfc_init_se (&se, NULL);
6520 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
6521 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
6522 TREE_STATIC (se.expr) = 1;
6523 return se.expr;
6524 }
6525 else if (!expr || expr->expr_type == EXPR_NULL)
6526 return fold_convert (type, null_pointer_node);
6527 else
6528 {
6529 gfc_init_se (&se, NULL);
6530 se.want_pointer = 1;
6531 gfc_conv_expr (&se, expr);
6532 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6533 return se.expr;
6534 }
6535 }
6536 else
6537 {
6538 switch (ts->type)
6539 {
6540 case BT_DERIVED:
6541 case BT_CLASS:
6542 gfc_init_se (&se, NULL);
6543 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
6544 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
6545 else
6546 gfc_conv_structure (&se, expr, 1);
6547 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
6548 TREE_STATIC (se.expr) = 1;
6549 return se.expr;
6550
6551 case BT_CHARACTER:
6552 {
6553 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
6554 TREE_STATIC (ctor) = 1;
6555 return ctor;
6556 }
6557
6558 default:
6559 gfc_init_se (&se, NULL);
6560 gfc_conv_constant (&se, expr);
6561 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6562 return se.expr;
6563 }
6564 }
6565 }
6566
6567 static tree
6568 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
6569 {
6570 gfc_se rse;
6571 gfc_se lse;
6572 gfc_ss *rss;
6573 gfc_ss *lss;
6574 gfc_array_info *lss_array;
6575 stmtblock_t body;
6576 stmtblock_t block;
6577 gfc_loopinfo loop;
6578 int n;
6579 tree tmp;
6580
6581 gfc_start_block (&block);
6582
6583 /* Initialize the scalarizer. */
6584 gfc_init_loopinfo (&loop);
6585
6586 gfc_init_se (&lse, NULL);
6587 gfc_init_se (&rse, NULL);
6588
6589 /* Walk the rhs. */
6590 rss = gfc_walk_expr (expr);
6591 if (rss == gfc_ss_terminator)
6592 /* The rhs is scalar. Add a ss for the expression. */
6593 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
6594
6595 /* Create a SS for the destination. */
6596 lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
6597 GFC_SS_COMPONENT);
6598 lss_array = &lss->info->data.array;
6599 lss_array->shape = gfc_get_shape (cm->as->rank);
6600 lss_array->descriptor = dest;
6601 lss_array->data = gfc_conv_array_data (dest);
6602 lss_array->offset = gfc_conv_array_offset (dest);
6603 for (n = 0; n < cm->as->rank; n++)
6604 {
6605 lss_array->start[n] = gfc_conv_array_lbound (dest, n);
6606 lss_array->stride[n] = gfc_index_one_node;
6607
6608 mpz_init (lss_array->shape[n]);
6609 mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
6610 cm->as->lower[n]->value.integer);
6611 mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
6612 }
6613
6614 /* Associate the SS with the loop. */
6615 gfc_add_ss_to_loop (&loop, lss);
6616 gfc_add_ss_to_loop (&loop, rss);
6617
6618 /* Calculate the bounds of the scalarization. */
6619 gfc_conv_ss_startstride (&loop);
6620
6621 /* Setup the scalarizing loops. */
6622 gfc_conv_loop_setup (&loop, &expr->where);
6623
6624 /* Setup the gfc_se structures. */
6625 gfc_copy_loopinfo_to_se (&lse, &loop);
6626 gfc_copy_loopinfo_to_se (&rse, &loop);
6627
6628 rse.ss = rss;
6629 gfc_mark_ss_chain_used (rss, 1);
6630 lse.ss = lss;
6631 gfc_mark_ss_chain_used (lss, 1);
6632
6633 /* Start the scalarized loop body. */
6634 gfc_start_scalarized_body (&loop, &body);
6635
6636 gfc_conv_tmp_array_ref (&lse);
6637 if (cm->ts.type == BT_CHARACTER)
6638 lse.string_length = cm->ts.u.cl->backend_decl;
6639
6640 gfc_conv_expr (&rse, expr);
6641
6642 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false, true);
6643 gfc_add_expr_to_block (&body, tmp);
6644
6645 gcc_assert (rse.ss == gfc_ss_terminator);
6646
6647 /* Generate the copying loops. */
6648 gfc_trans_scalarizing_loops (&loop, &body);
6649
6650 /* Wrap the whole thing up. */
6651 gfc_add_block_to_block (&block, &loop.pre);
6652 gfc_add_block_to_block (&block, &loop.post);
6653
6654 gcc_assert (lss_array->shape != NULL);
6655 gfc_free_shape (&lss_array->shape, cm->as->rank);
6656 gfc_cleanup_loop (&loop);
6657
6658 return gfc_finish_block (&block);
6659 }
6660
6661
6662 static tree
6663 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
6664 gfc_expr * expr)
6665 {
6666 gfc_se se;
6667 stmtblock_t block;
6668 tree offset;
6669 int n;
6670 tree tmp;
6671 tree tmp2;
6672 gfc_array_spec *as;
6673 gfc_expr *arg = NULL;
6674
6675 gfc_start_block (&block);
6676 gfc_init_se (&se, NULL);
6677
6678 /* Get the descriptor for the expressions. */
6679 se.want_pointer = 0;
6680 gfc_conv_expr_descriptor (&se, expr);
6681 gfc_add_block_to_block (&block, &se.pre);
6682 gfc_add_modify (&block, dest, se.expr);
6683
6684 /* Deal with arrays of derived types with allocatable components. */
6685 if (cm->ts.type == BT_DERIVED
6686 && cm->ts.u.derived->attr.alloc_comp)
6687 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
6688 se.expr, dest,
6689 cm->as->rank);
6690 else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
6691 && CLASS_DATA(cm)->attr.allocatable)
6692 {
6693 if (cm->ts.u.derived->attr.alloc_comp)
6694 tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
6695 se.expr, dest,
6696 expr->rank);
6697 else
6698 {
6699 tmp = TREE_TYPE (dest);
6700 tmp = gfc_duplicate_allocatable (dest, se.expr,
6701 tmp, expr->rank, NULL_TREE);
6702 }
6703 }
6704 else
6705 tmp = gfc_duplicate_allocatable (dest, se.expr,
6706 TREE_TYPE(cm->backend_decl),
6707 cm->as->rank, NULL_TREE);
6708
6709 gfc_add_expr_to_block (&block, tmp);
6710 gfc_add_block_to_block (&block, &se.post);
6711
6712 if (expr->expr_type != EXPR_VARIABLE)
6713 gfc_conv_descriptor_data_set (&block, se.expr,
6714 null_pointer_node);
6715
6716 /* We need to know if the argument of a conversion function is a
6717 variable, so that the correct lower bound can be used. */
6718 if (expr->expr_type == EXPR_FUNCTION
6719 && expr->value.function.isym
6720 && expr->value.function.isym->conversion
6721 && expr->value.function.actual->expr
6722 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
6723 arg = expr->value.function.actual->expr;
6724
6725 /* Obtain the array spec of full array references. */
6726 if (arg)
6727 as = gfc_get_full_arrayspec_from_expr (arg);
6728 else
6729 as = gfc_get_full_arrayspec_from_expr (expr);
6730
6731 /* Shift the lbound and ubound of temporaries to being unity,
6732 rather than zero, based. Always calculate the offset. */
6733 offset = gfc_conv_descriptor_offset_get (dest);
6734 gfc_add_modify (&block, offset, gfc_index_zero_node);
6735 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
6736
6737 for (n = 0; n < expr->rank; n++)
6738 {
6739 tree span;
6740 tree lbound;
6741
6742 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
6743 TODO It looks as if gfc_conv_expr_descriptor should return
6744 the correct bounds and that the following should not be
6745 necessary. This would simplify gfc_conv_intrinsic_bound
6746 as well. */
6747 if (as && as->lower[n])
6748 {
6749 gfc_se lbse;
6750 gfc_init_se (&lbse, NULL);
6751 gfc_conv_expr (&lbse, as->lower[n]);
6752 gfc_add_block_to_block (&block, &lbse.pre);
6753 lbound = gfc_evaluate_now (lbse.expr, &block);
6754 }
6755 else if (as && arg)
6756 {
6757 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
6758 lbound = gfc_conv_descriptor_lbound_get (tmp,
6759 gfc_rank_cst[n]);
6760 }
6761 else if (as)
6762 lbound = gfc_conv_descriptor_lbound_get (dest,
6763 gfc_rank_cst[n]);
6764 else
6765 lbound = gfc_index_one_node;
6766
6767 lbound = fold_convert (gfc_array_index_type, lbound);
6768
6769 /* Shift the bounds and set the offset accordingly. */
6770 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
6771 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6772 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
6773 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6774 span, lbound);
6775 gfc_conv_descriptor_ubound_set (&block, dest,
6776 gfc_rank_cst[n], tmp);
6777 gfc_conv_descriptor_lbound_set (&block, dest,
6778 gfc_rank_cst[n], lbound);
6779
6780 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6781 gfc_conv_descriptor_lbound_get (dest,
6782 gfc_rank_cst[n]),
6783 gfc_conv_descriptor_stride_get (dest,
6784 gfc_rank_cst[n]));
6785 gfc_add_modify (&block, tmp2, tmp);
6786 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6787 offset, tmp2);
6788 gfc_conv_descriptor_offset_set (&block, dest, tmp);
6789 }
6790
6791 if (arg)
6792 {
6793 /* If a conversion expression has a null data pointer
6794 argument, nullify the allocatable component. */
6795 tree non_null_expr;
6796 tree null_expr;
6797
6798 if (arg->symtree->n.sym->attr.allocatable
6799 || arg->symtree->n.sym->attr.pointer)
6800 {
6801 non_null_expr = gfc_finish_block (&block);
6802 gfc_start_block (&block);
6803 gfc_conv_descriptor_data_set (&block, dest,
6804 null_pointer_node);
6805 null_expr = gfc_finish_block (&block);
6806 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
6807 tmp = build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
6808 fold_convert (TREE_TYPE (tmp), null_pointer_node));
6809 return build3_v (COND_EXPR, tmp,
6810 null_expr, non_null_expr);
6811 }
6812 }
6813
6814 return gfc_finish_block (&block);
6815 }
6816
6817
6818 /* Allocate or reallocate scalar component, as necessary. */
6819
6820 static void
6821 alloc_scalar_allocatable_for_subcomponent_assignment (stmtblock_t *block,
6822 tree comp,
6823 gfc_component *cm,
6824 gfc_expr *expr2,
6825 gfc_symbol *sym)
6826 {
6827 tree tmp;
6828 tree ptr;
6829 tree size;
6830 tree size_in_bytes;
6831 tree lhs_cl_size = NULL_TREE;
6832
6833 if (!comp)
6834 return;
6835
6836 if (!expr2 || expr2->rank)
6837 return;
6838
6839 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
6840
6841 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
6842 {
6843 char name[GFC_MAX_SYMBOL_LEN+9];
6844 gfc_component *strlen;
6845 /* Use the rhs string length and the lhs element size. */
6846 gcc_assert (expr2->ts.type == BT_CHARACTER);
6847 if (!expr2->ts.u.cl->backend_decl)
6848 {
6849 gfc_conv_string_length (expr2->ts.u.cl, expr2, block);
6850 gcc_assert (expr2->ts.u.cl->backend_decl);
6851 }
6852
6853 size = expr2->ts.u.cl->backend_decl;
6854
6855 /* Ensure that cm->ts.u.cl->backend_decl is a componentref to _%s_length
6856 component. */
6857 sprintf (name, "_%s_length", cm->name);
6858 strlen = gfc_find_component (sym, name, true, true);
6859 lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
6860 gfc_charlen_type_node,
6861 TREE_OPERAND (comp, 0),
6862 strlen->backend_decl, NULL_TREE);
6863
6864 tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
6865 tmp = TYPE_SIZE_UNIT (tmp);
6866 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
6867 TREE_TYPE (tmp), tmp,
6868 fold_convert (TREE_TYPE (tmp), size));
6869 }
6870 else
6871 {
6872 /* Otherwise use the length in bytes of the rhs. */
6873 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
6874 size_in_bytes = size;
6875 }
6876
6877 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
6878 size_in_bytes, size_one_node);
6879
6880 if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
6881 {
6882 tmp = build_call_expr_loc (input_location,
6883 builtin_decl_explicit (BUILT_IN_CALLOC),
6884 2, build_one_cst (size_type_node),
6885 size_in_bytes);
6886 tmp = fold_convert (TREE_TYPE (comp), tmp);
6887 gfc_add_modify (block, comp, tmp);
6888 }
6889 else
6890 {
6891 tmp = build_call_expr_loc (input_location,
6892 builtin_decl_explicit (BUILT_IN_MALLOC),
6893 1, size_in_bytes);
6894 if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
6895 ptr = gfc_class_data_get (comp);
6896 else
6897 ptr = comp;
6898 tmp = fold_convert (TREE_TYPE (ptr), tmp);
6899 gfc_add_modify (block, ptr, tmp);
6900 }
6901
6902 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
6903 /* Update the lhs character length. */
6904 gfc_add_modify (block, lhs_cl_size, size);
6905 }
6906
6907
6908 /* Assign a single component of a derived type constructor. */
6909
6910 static tree
6911 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr,
6912 gfc_symbol *sym, bool init)
6913 {
6914 gfc_se se;
6915 gfc_se lse;
6916 stmtblock_t block;
6917 tree tmp;
6918 tree vtab;
6919
6920 gfc_start_block (&block);
6921
6922 if (cm->attr.pointer || cm->attr.proc_pointer)
6923 {
6924 /* Only care about pointers here, not about allocatables. */
6925 gfc_init_se (&se, NULL);
6926 /* Pointer component. */
6927 if ((cm->attr.dimension || cm->attr.codimension)
6928 && !cm->attr.proc_pointer)
6929 {
6930 /* Array pointer. */
6931 if (expr->expr_type == EXPR_NULL)
6932 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
6933 else
6934 {
6935 se.direct_byref = 1;
6936 se.expr = dest;
6937 gfc_conv_expr_descriptor (&se, expr);
6938 gfc_add_block_to_block (&block, &se.pre);
6939 gfc_add_block_to_block (&block, &se.post);
6940 }
6941 }
6942 else
6943 {
6944 /* Scalar pointers. */
6945 se.want_pointer = 1;
6946 gfc_conv_expr (&se, expr);
6947 gfc_add_block_to_block (&block, &se.pre);
6948
6949 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
6950 && expr->symtree->n.sym->attr.dummy)
6951 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
6952
6953 gfc_add_modify (&block, dest,
6954 fold_convert (TREE_TYPE (dest), se.expr));
6955 gfc_add_block_to_block (&block, &se.post);
6956 }
6957 }
6958 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
6959 {
6960 /* NULL initialization for CLASS components. */
6961 tmp = gfc_trans_structure_assign (dest,
6962 gfc_class_initializer (&cm->ts, expr),
6963 false);
6964 gfc_add_expr_to_block (&block, tmp);
6965 }
6966 else if ((cm->attr.dimension || cm->attr.codimension)
6967 && !cm->attr.proc_pointer)
6968 {
6969 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
6970 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
6971 else if (cm->attr.allocatable)
6972 {
6973 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
6974 gfc_add_expr_to_block (&block, tmp);
6975 }
6976 else
6977 {
6978 tmp = gfc_trans_subarray_assign (dest, cm, expr);
6979 gfc_add_expr_to_block (&block, tmp);
6980 }
6981 }
6982 else if (cm->ts.type == BT_CLASS
6983 && CLASS_DATA (cm)->attr.dimension
6984 && CLASS_DATA (cm)->attr.allocatable
6985 && expr->ts.type == BT_DERIVED)
6986 {
6987 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
6988 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
6989 tmp = gfc_class_vptr_get (dest);
6990 gfc_add_modify (&block, tmp,
6991 fold_convert (TREE_TYPE (tmp), vtab));
6992 tmp = gfc_class_data_get (dest);
6993 tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
6994 gfc_add_expr_to_block (&block, tmp);
6995 }
6996 else if (init && (cm->attr.allocatable
6997 || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable)))
6998 {
6999 /* Take care about non-array allocatable components here. The alloc_*
7000 routine below is motivated by the alloc_scalar_allocatable_for_
7001 assignment() routine, but with the realloc portions removed and
7002 different input. */
7003 alloc_scalar_allocatable_for_subcomponent_assignment (&block,
7004 dest,
7005 cm,
7006 expr,
7007 sym);
7008 /* The remainder of these instructions follow the if (cm->attr.pointer)
7009 if (!cm->attr.dimension) part above. */
7010 gfc_init_se (&se, NULL);
7011 gfc_conv_expr (&se, expr);
7012 gfc_add_block_to_block (&block, &se.pre);
7013
7014 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
7015 && expr->symtree->n.sym->attr.dummy)
7016 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
7017
7018 if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED)
7019 {
7020 tmp = gfc_class_data_get (dest);
7021 tmp = build_fold_indirect_ref_loc (input_location, tmp);
7022 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
7023 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
7024 gfc_add_modify (&block, gfc_class_vptr_get (dest),
7025 fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
7026 }
7027 else
7028 tmp = build_fold_indirect_ref_loc (input_location, dest);
7029
7030 /* For deferred strings insert a memcpy. */
7031 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
7032 {
7033 tree size;
7034 gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
7035 size = size_of_string_in_bytes (cm->ts.kind, se.string_length
7036 ? se.string_length
7037 : expr->ts.u.cl->backend_decl);
7038 tmp = gfc_build_memcpy_call (tmp, se.expr, size);
7039 gfc_add_expr_to_block (&block, tmp);
7040 }
7041 else
7042 gfc_add_modify (&block, tmp,
7043 fold_convert (TREE_TYPE (tmp), se.expr));
7044 gfc_add_block_to_block (&block, &se.post);
7045 }
7046 else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
7047 {
7048 if (expr->expr_type != EXPR_STRUCTURE)
7049 {
7050 tree dealloc = NULL_TREE;
7051 gfc_init_se (&se, NULL);
7052 gfc_conv_expr (&se, expr);
7053 gfc_add_block_to_block (&block, &se.pre);
7054 /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
7055 expression in a temporary variable and deallocate the allocatable
7056 components. Then we can the copy the expression to the result. */
7057 if (cm->ts.u.derived->attr.alloc_comp
7058 && expr->expr_type != EXPR_VARIABLE)
7059 {
7060 se.expr = gfc_evaluate_now (se.expr, &block);
7061 dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
7062 expr->rank);
7063 }
7064 gfc_add_modify (&block, dest,
7065 fold_convert (TREE_TYPE (dest), se.expr));
7066 if (cm->ts.u.derived->attr.alloc_comp
7067 && expr->expr_type != EXPR_NULL)
7068 {
7069 tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
7070 dest, expr->rank);
7071 gfc_add_expr_to_block (&block, tmp);
7072 if (dealloc != NULL_TREE)
7073 gfc_add_expr_to_block (&block, dealloc);
7074 }
7075 gfc_add_block_to_block (&block, &se.post);
7076 }
7077 else
7078 {
7079 /* Nested constructors. */
7080 tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
7081 gfc_add_expr_to_block (&block, tmp);
7082 }
7083 }
7084 else if (gfc_deferred_strlen (cm, &tmp))
7085 {
7086 tree strlen;
7087 strlen = tmp;
7088 gcc_assert (strlen);
7089 strlen = fold_build3_loc (input_location, COMPONENT_REF,
7090 TREE_TYPE (strlen),
7091 TREE_OPERAND (dest, 0),
7092 strlen, NULL_TREE);
7093
7094 if (expr->expr_type == EXPR_NULL)
7095 {
7096 tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
7097 gfc_add_modify (&block, dest, tmp);
7098 tmp = build_int_cst (TREE_TYPE (strlen), 0);
7099 gfc_add_modify (&block, strlen, tmp);
7100 }
7101 else
7102 {
7103 tree size;
7104 gfc_init_se (&se, NULL);
7105 gfc_conv_expr (&se, expr);
7106 size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
7107 tmp = build_call_expr_loc (input_location,
7108 builtin_decl_explicit (BUILT_IN_MALLOC),
7109 1, size);
7110 gfc_add_modify (&block, dest,
7111 fold_convert (TREE_TYPE (dest), tmp));
7112 gfc_add_modify (&block, strlen, se.string_length);
7113 tmp = gfc_build_memcpy_call (dest, se.expr, size);
7114 gfc_add_expr_to_block (&block, tmp);
7115 }
7116 }
7117 else if (!cm->attr.artificial)
7118 {
7119 /* Scalar component (excluding deferred parameters). */
7120 gfc_init_se (&se, NULL);
7121 gfc_init_se (&lse, NULL);
7122
7123 gfc_conv_expr (&se, expr);
7124 if (cm->ts.type == BT_CHARACTER)
7125 lse.string_length = cm->ts.u.cl->backend_decl;
7126 lse.expr = dest;
7127 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, true, false, true);
7128 gfc_add_expr_to_block (&block, tmp);
7129 }
7130 return gfc_finish_block (&block);
7131 }
7132
7133 /* Assign a derived type constructor to a variable. */
7134
7135 static tree
7136 gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init)
7137 {
7138 gfc_constructor *c;
7139 gfc_component *cm;
7140 stmtblock_t block;
7141 tree field;
7142 tree tmp;
7143
7144 gfc_start_block (&block);
7145 cm = expr->ts.u.derived->components;
7146
7147 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
7148 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
7149 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
7150 {
7151 gfc_se se, lse;
7152
7153 gcc_assert (cm->backend_decl == NULL);
7154 gfc_init_se (&se, NULL);
7155 gfc_init_se (&lse, NULL);
7156 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
7157 lse.expr = dest;
7158 gfc_add_modify (&block, lse.expr,
7159 fold_convert (TREE_TYPE (lse.expr), se.expr));
7160
7161 return gfc_finish_block (&block);
7162 }
7163
7164 for (c = gfc_constructor_first (expr->value.constructor);
7165 c; c = gfc_constructor_next (c), cm = cm->next)
7166 {
7167 /* Skip absent members in default initializers. */
7168 if (!c->expr && !cm->attr.allocatable)
7169 continue;
7170
7171 field = cm->backend_decl;
7172 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
7173 dest, field, NULL_TREE);
7174 if (!c->expr)
7175 {
7176 gfc_expr *e = gfc_get_null_expr (NULL);
7177 tmp = gfc_trans_subcomponent_assign (tmp, cm, e, expr->ts.u.derived,
7178 init);
7179 gfc_free_expr (e);
7180 }
7181 else
7182 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr,
7183 expr->ts.u.derived, init);
7184 gfc_add_expr_to_block (&block, tmp);
7185 }
7186 return gfc_finish_block (&block);
7187 }
7188
7189 /* Build an expression for a constructor. If init is nonzero then
7190 this is part of a static variable initializer. */
7191
7192 void
7193 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
7194 {
7195 gfc_constructor *c;
7196 gfc_component *cm;
7197 tree val;
7198 tree type;
7199 tree tmp;
7200 vec<constructor_elt, va_gc> *v = NULL;
7201
7202 gcc_assert (se->ss == NULL);
7203 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
7204 type = gfc_typenode_for_spec (&expr->ts);
7205
7206 if (!init)
7207 {
7208 /* Create a temporary variable and fill it in. */
7209 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
7210 /* The symtree in expr is NULL, if the code to generate is for
7211 initializing the static members only. */
7212 tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL);
7213 gfc_add_expr_to_block (&se->pre, tmp);
7214 return;
7215 }
7216
7217 cm = expr->ts.u.derived->components;
7218
7219 for (c = gfc_constructor_first (expr->value.constructor);
7220 c; c = gfc_constructor_next (c), cm = cm->next)
7221 {
7222 /* Skip absent members in default initializers and allocatable
7223 components. Although the latter have a default initializer
7224 of EXPR_NULL,... by default, the static nullify is not needed
7225 since this is done every time we come into scope. */
7226 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
7227 continue;
7228
7229 if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
7230 && strcmp (cm->name, "_extends") == 0
7231 && cm->initializer->symtree)
7232 {
7233 tree vtab;
7234 gfc_symbol *vtabs;
7235 vtabs = cm->initializer->symtree->n.sym;
7236 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
7237 vtab = unshare_expr_without_location (vtab);
7238 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
7239 }
7240 else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
7241 {
7242 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
7243 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
7244 fold_convert (TREE_TYPE (cm->backend_decl),
7245 val));
7246 }
7247 else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
7248 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
7249 fold_convert (TREE_TYPE (cm->backend_decl),
7250 integer_zero_node));
7251 else
7252 {
7253 val = gfc_conv_initializer (c->expr, &cm->ts,
7254 TREE_TYPE (cm->backend_decl),
7255 cm->attr.dimension, cm->attr.pointer,
7256 cm->attr.proc_pointer);
7257 val = unshare_expr_without_location (val);
7258
7259 /* Append it to the constructor list. */
7260 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
7261 }
7262 }
7263 se->expr = build_constructor (type, v);
7264 if (init)
7265 TREE_CONSTANT (se->expr) = 1;
7266 }
7267
7268
7269 /* Translate a substring expression. */
7270
7271 static void
7272 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
7273 {
7274 gfc_ref *ref;
7275
7276 ref = expr->ref;
7277
7278 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
7279
7280 se->expr = gfc_build_wide_string_const (expr->ts.kind,
7281 expr->value.character.length,
7282 expr->value.character.string);
7283
7284 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
7285 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
7286
7287 if (ref)
7288 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
7289 }
7290
7291
7292 /* Entry point for expression translation. Evaluates a scalar quantity.
7293 EXPR is the expression to be translated, and SE is the state structure if
7294 called from within the scalarized. */
7295
7296 void
7297 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
7298 {
7299 gfc_ss *ss;
7300
7301 ss = se->ss;
7302 if (ss && ss->info->expr == expr
7303 && (ss->info->type == GFC_SS_SCALAR
7304 || ss->info->type == GFC_SS_REFERENCE))
7305 {
7306 gfc_ss_info *ss_info;
7307
7308 ss_info = ss->info;
7309 /* Substitute a scalar expression evaluated outside the scalarization
7310 loop. */
7311 se->expr = ss_info->data.scalar.value;
7312 if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
7313 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
7314
7315 se->string_length = ss_info->string_length;
7316 gfc_advance_se_ss_chain (se);
7317 return;
7318 }
7319
7320 /* We need to convert the expressions for the iso_c_binding derived types.
7321 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
7322 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
7323 typespec for the C_PTR and C_FUNPTR symbols, which has already been
7324 updated to be an integer with a kind equal to the size of a (void *). */
7325 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
7326 && expr->ts.u.derived->attr.is_bind_c)
7327 {
7328 if (expr->expr_type == EXPR_VARIABLE
7329 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
7330 || expr->symtree->n.sym->intmod_sym_id
7331 == ISOCBINDING_NULL_FUNPTR))
7332 {
7333 /* Set expr_type to EXPR_NULL, which will result in
7334 null_pointer_node being used below. */
7335 expr->expr_type = EXPR_NULL;
7336 }
7337 else
7338 {
7339 /* Update the type/kind of the expression to be what the new
7340 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
7341 expr->ts.type = BT_INTEGER;
7342 expr->ts.f90_type = BT_VOID;
7343 expr->ts.kind = gfc_index_integer_kind;
7344 }
7345 }
7346
7347 gfc_fix_class_refs (expr);
7348
7349 switch (expr->expr_type)
7350 {
7351 case EXPR_OP:
7352 gfc_conv_expr_op (se, expr);
7353 break;
7354
7355 case EXPR_FUNCTION:
7356 gfc_conv_function_expr (se, expr);
7357 break;
7358
7359 case EXPR_CONSTANT:
7360 gfc_conv_constant (se, expr);
7361 break;
7362
7363 case EXPR_VARIABLE:
7364 gfc_conv_variable (se, expr);
7365 break;
7366
7367 case EXPR_NULL:
7368 se->expr = null_pointer_node;
7369 break;
7370
7371 case EXPR_SUBSTRING:
7372 gfc_conv_substring_expr (se, expr);
7373 break;
7374
7375 case EXPR_STRUCTURE:
7376 gfc_conv_structure (se, expr, 0);
7377 break;
7378
7379 case EXPR_ARRAY:
7380 gfc_conv_array_constructor_expr (se, expr);
7381 break;
7382
7383 default:
7384 gcc_unreachable ();
7385 break;
7386 }
7387 }
7388
7389 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
7390 of an assignment. */
7391 void
7392 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
7393 {
7394 gfc_conv_expr (se, expr);
7395 /* All numeric lvalues should have empty post chains. If not we need to
7396 figure out a way of rewriting an lvalue so that it has no post chain. */
7397 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
7398 }
7399
7400 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
7401 numeric expressions. Used for scalar values where inserting cleanup code
7402 is inconvenient. */
7403 void
7404 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
7405 {
7406 tree val;
7407
7408 gcc_assert (expr->ts.type != BT_CHARACTER);
7409 gfc_conv_expr (se, expr);
7410 if (se->post.head)
7411 {
7412 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
7413 gfc_add_modify (&se->pre, val, se->expr);
7414 se->expr = val;
7415 gfc_add_block_to_block (&se->pre, &se->post);
7416 }
7417 }
7418
7419 /* Helper to translate an expression and convert it to a particular type. */
7420 void
7421 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
7422 {
7423 gfc_conv_expr_val (se, expr);
7424 se->expr = convert (type, se->expr);
7425 }
7426
7427
7428 /* Converts an expression so that it can be passed by reference. Scalar
7429 values only. */
7430
7431 void
7432 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
7433 {
7434 gfc_ss *ss;
7435 tree var;
7436
7437 ss = se->ss;
7438 if (ss && ss->info->expr == expr
7439 && ss->info->type == GFC_SS_REFERENCE)
7440 {
7441 /* Returns a reference to the scalar evaluated outside the loop
7442 for this case. */
7443 gfc_conv_expr (se, expr);
7444
7445 if (expr->ts.type == BT_CHARACTER
7446 && expr->expr_type != EXPR_FUNCTION)
7447 gfc_conv_string_parameter (se);
7448 else
7449 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
7450
7451 return;
7452 }
7453
7454 if (expr->ts.type == BT_CHARACTER)
7455 {
7456 gfc_conv_expr (se, expr);
7457 gfc_conv_string_parameter (se);
7458 return;
7459 }
7460
7461 if (expr->expr_type == EXPR_VARIABLE)
7462 {
7463 se->want_pointer = 1;
7464 gfc_conv_expr (se, expr);
7465 if (se->post.head)
7466 {
7467 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7468 gfc_add_modify (&se->pre, var, se->expr);
7469 gfc_add_block_to_block (&se->pre, &se->post);
7470 se->expr = var;
7471 }
7472 return;
7473 }
7474
7475 if (expr->expr_type == EXPR_FUNCTION
7476 && ((expr->value.function.esym
7477 && expr->value.function.esym->result->attr.pointer
7478 && !expr->value.function.esym->result->attr.dimension)
7479 || (!expr->value.function.esym && !expr->ref
7480 && expr->symtree->n.sym->attr.pointer
7481 && !expr->symtree->n.sym->attr.dimension)))
7482 {
7483 se->want_pointer = 1;
7484 gfc_conv_expr (se, expr);
7485 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7486 gfc_add_modify (&se->pre, var, se->expr);
7487 se->expr = var;
7488 return;
7489 }
7490
7491 gfc_conv_expr (se, expr);
7492
7493 /* Create a temporary var to hold the value. */
7494 if (TREE_CONSTANT (se->expr))
7495 {
7496 tree tmp = se->expr;
7497 STRIP_TYPE_NOPS (tmp);
7498 var = build_decl (input_location,
7499 CONST_DECL, NULL, TREE_TYPE (tmp));
7500 DECL_INITIAL (var) = tmp;
7501 TREE_STATIC (var) = 1;
7502 pushdecl (var);
7503 }
7504 else
7505 {
7506 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7507 gfc_add_modify (&se->pre, var, se->expr);
7508 }
7509 gfc_add_block_to_block (&se->pre, &se->post);
7510
7511 /* Take the address of that value. */
7512 se->expr = gfc_build_addr_expr (NULL_TREE, var);
7513 if (expr->ts.type == BT_DERIVED && expr->rank
7514 && !gfc_is_finalizable (expr->ts.u.derived, NULL)
7515 && expr->ts.u.derived->attr.alloc_comp
7516 && expr->expr_type != EXPR_VARIABLE)
7517 {
7518 tree tmp;
7519
7520 tmp = build_fold_indirect_ref_loc (input_location, se->expr);
7521 tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
7522
7523 /* The components shall be deallocated before
7524 their containing entity. */
7525 gfc_prepend_expr_to_block (&se->post, tmp);
7526 }
7527 }
7528
7529
7530 tree
7531 gfc_trans_pointer_assign (gfc_code * code)
7532 {
7533 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
7534 }
7535
7536
7537 /* Generate code for a pointer assignment. */
7538
7539 tree
7540 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
7541 {
7542 gfc_expr *expr1_vptr = NULL;
7543 gfc_se lse;
7544 gfc_se rse;
7545 stmtblock_t block;
7546 tree desc;
7547 tree tmp;
7548 tree decl;
7549 bool scalar;
7550 gfc_ss *ss;
7551
7552 gfc_start_block (&block);
7553
7554 gfc_init_se (&lse, NULL);
7555
7556 /* Check whether the expression is a scalar or not; we cannot use
7557 expr1->rank as it can be nonzero for proc pointers. */
7558 ss = gfc_walk_expr (expr1);
7559 scalar = ss == gfc_ss_terminator;
7560 if (!scalar)
7561 gfc_free_ss_chain (ss);
7562
7563 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
7564 && expr2->expr_type != EXPR_FUNCTION)
7565 {
7566 gfc_add_data_component (expr2);
7567 /* The following is required as gfc_add_data_component doesn't
7568 update ts.type if there is a tailing REF_ARRAY. */
7569 expr2->ts.type = BT_DERIVED;
7570 }
7571
7572 if (scalar)
7573 {
7574 /* Scalar pointers. */
7575 lse.want_pointer = 1;
7576 gfc_conv_expr (&lse, expr1);
7577 gfc_init_se (&rse, NULL);
7578 rse.want_pointer = 1;
7579 gfc_conv_expr (&rse, expr2);
7580
7581 if (expr1->symtree->n.sym->attr.proc_pointer
7582 && expr1->symtree->n.sym->attr.dummy)
7583 lse.expr = build_fold_indirect_ref_loc (input_location,
7584 lse.expr);
7585
7586 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
7587 && expr2->symtree->n.sym->attr.dummy)
7588 rse.expr = build_fold_indirect_ref_loc (input_location,
7589 rse.expr);
7590
7591 gfc_add_block_to_block (&block, &lse.pre);
7592 gfc_add_block_to_block (&block, &rse.pre);
7593
7594 /* For string assignments to unlimited polymorphic pointers add an
7595 assignment of the string_length to the _len component of the
7596 pointer. */
7597 if ((expr1->ts.type == BT_CLASS || expr1->ts.type == BT_DERIVED)
7598 && expr1->ts.u.derived->attr.unlimited_polymorphic
7599 && (expr2->ts.type == BT_CHARACTER ||
7600 ((expr2->ts.type == BT_DERIVED || expr2->ts.type == BT_CLASS)
7601 && expr2->ts.u.derived->attr.unlimited_polymorphic)))
7602 {
7603 gfc_expr *len_comp;
7604 gfc_se se;
7605 len_comp = gfc_get_len_component (expr1);
7606 gfc_init_se (&se, NULL);
7607 gfc_conv_expr (&se, len_comp);
7608
7609 /* ptr % _len = len (str) */
7610 gfc_add_modify (&block, se.expr, rse.string_length);
7611 lse.string_length = se.expr;
7612 gfc_free_expr (len_comp);
7613 }
7614
7615 /* Check character lengths if character expression. The test is only
7616 really added if -fbounds-check is enabled. Exclude deferred
7617 character length lefthand sides. */
7618 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
7619 && !expr1->ts.deferred
7620 && !expr1->symtree->n.sym->attr.proc_pointer
7621 && !gfc_is_proc_ptr_comp (expr1))
7622 {
7623 gcc_assert (expr2->ts.type == BT_CHARACTER);
7624 gcc_assert (lse.string_length && rse.string_length);
7625 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
7626 lse.string_length, rse.string_length,
7627 &block);
7628 }
7629
7630 /* The assignment to an deferred character length sets the string
7631 length to that of the rhs. */
7632 if (expr1->ts.deferred)
7633 {
7634 if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
7635 gfc_add_modify (&block, lse.string_length, rse.string_length);
7636 else if (lse.string_length != NULL)
7637 gfc_add_modify (&block, lse.string_length,
7638 build_int_cst (gfc_charlen_type_node, 0));
7639 }
7640
7641 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS)
7642 rse.expr = gfc_class_data_get (rse.expr);
7643
7644 gfc_add_modify (&block, lse.expr,
7645 fold_convert (TREE_TYPE (lse.expr), rse.expr));
7646
7647 gfc_add_block_to_block (&block, &rse.post);
7648 gfc_add_block_to_block (&block, &lse.post);
7649 }
7650 else
7651 {
7652 gfc_ref* remap;
7653 bool rank_remap;
7654 tree strlen_lhs;
7655 tree strlen_rhs = NULL_TREE;
7656
7657 /* Array pointer. Find the last reference on the LHS and if it is an
7658 array section ref, we're dealing with bounds remapping. In this case,
7659 set it to AR_FULL so that gfc_conv_expr_descriptor does
7660 not see it and process the bounds remapping afterwards explicitly. */
7661 for (remap = expr1->ref; remap; remap = remap->next)
7662 if (!remap->next && remap->type == REF_ARRAY
7663 && remap->u.ar.type == AR_SECTION)
7664 break;
7665 rank_remap = (remap && remap->u.ar.end[0]);
7666
7667 gfc_init_se (&lse, NULL);
7668 if (remap)
7669 lse.descriptor_only = 1;
7670 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS
7671 && expr1->ts.type == BT_CLASS)
7672 expr1_vptr = gfc_copy_expr (expr1);
7673 gfc_conv_expr_descriptor (&lse, expr1);
7674 strlen_lhs = lse.string_length;
7675 desc = lse.expr;
7676
7677 if (expr2->expr_type == EXPR_NULL)
7678 {
7679 /* Just set the data pointer to null. */
7680 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
7681 }
7682 else if (rank_remap)
7683 {
7684 /* If we are rank-remapping, just get the RHS's descriptor and
7685 process this later on. */
7686 gfc_init_se (&rse, NULL);
7687 rse.direct_byref = 1;
7688 rse.byref_noassign = 1;
7689
7690 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
7691 {
7692 gfc_conv_function_expr (&rse, expr2);
7693
7694 if (expr1->ts.type != BT_CLASS)
7695 rse.expr = gfc_class_data_get (rse.expr);
7696 else
7697 {
7698 gfc_add_block_to_block (&block, &rse.pre);
7699 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
7700 gfc_add_modify (&lse.pre, tmp, rse.expr);
7701
7702 gfc_add_vptr_component (expr1_vptr);
7703 gfc_init_se (&rse, NULL);
7704 rse.want_pointer = 1;
7705 gfc_conv_expr (&rse, expr1_vptr);
7706 gfc_add_modify (&lse.pre, rse.expr,
7707 fold_convert (TREE_TYPE (rse.expr),
7708 gfc_class_vptr_get (tmp)));
7709 rse.expr = gfc_class_data_get (tmp);
7710 }
7711 }
7712 else if (expr2->expr_type == EXPR_FUNCTION)
7713 {
7714 tree bound[GFC_MAX_DIMENSIONS];
7715 int i;
7716
7717 for (i = 0; i < expr2->rank; i++)
7718 bound[i] = NULL_TREE;
7719 tmp = gfc_typenode_for_spec (&expr2->ts);
7720 tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
7721 bound, bound, 0,
7722 GFC_ARRAY_POINTER_CONT, false);
7723 tmp = gfc_create_var (tmp, "ptrtemp");
7724 lse.descriptor_only = 0;
7725 lse.expr = tmp;
7726 lse.direct_byref = 1;
7727 gfc_conv_expr_descriptor (&lse, expr2);
7728 strlen_rhs = lse.string_length;
7729 rse.expr = tmp;
7730 }
7731 else
7732 {
7733 gfc_conv_expr_descriptor (&rse, expr2);
7734 strlen_rhs = rse.string_length;
7735 }
7736 }
7737 else if (expr2->expr_type == EXPR_VARIABLE)
7738 {
7739 /* Assign directly to the LHS's descriptor. */
7740 lse.descriptor_only = 0;
7741 lse.direct_byref = 1;
7742 gfc_conv_expr_descriptor (&lse, expr2);
7743 strlen_rhs = lse.string_length;
7744
7745 /* If this is a subreference array pointer assignment, use the rhs
7746 descriptor element size for the lhs span. */
7747 if (expr1->symtree->n.sym->attr.subref_array_pointer)
7748 {
7749 decl = expr1->symtree->n.sym->backend_decl;
7750 gfc_init_se (&rse, NULL);
7751 rse.descriptor_only = 1;
7752 gfc_conv_expr (&rse, expr2);
7753 tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
7754 tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
7755 if (!INTEGER_CST_P (tmp))
7756 gfc_add_block_to_block (&lse.post, &rse.pre);
7757 gfc_add_modify (&lse.post, GFC_DECL_SPAN(decl), tmp);
7758 }
7759 }
7760 else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
7761 {
7762 gfc_init_se (&rse, NULL);
7763 rse.want_pointer = 1;
7764 gfc_conv_function_expr (&rse, expr2);
7765 if (expr1->ts.type != BT_CLASS)
7766 {
7767 rse.expr = gfc_class_data_get (rse.expr);
7768 gfc_add_modify (&lse.pre, desc, rse.expr);
7769 }
7770 else
7771 {
7772 gfc_add_block_to_block (&block, &rse.pre);
7773 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
7774 gfc_add_modify (&lse.pre, tmp, rse.expr);
7775
7776 gfc_add_vptr_component (expr1_vptr);
7777 gfc_init_se (&rse, NULL);
7778 rse.want_pointer = 1;
7779 gfc_conv_expr (&rse, expr1_vptr);
7780 gfc_add_modify (&lse.pre, rse.expr,
7781 fold_convert (TREE_TYPE (rse.expr),
7782 gfc_class_vptr_get (tmp)));
7783 rse.expr = gfc_class_data_get (tmp);
7784 gfc_add_modify (&lse.pre, desc, rse.expr);
7785 }
7786 }
7787 else
7788 {
7789 /* Assign to a temporary descriptor and then copy that
7790 temporary to the pointer. */
7791 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
7792 lse.descriptor_only = 0;
7793 lse.expr = tmp;
7794 lse.direct_byref = 1;
7795 gfc_conv_expr_descriptor (&lse, expr2);
7796 strlen_rhs = lse.string_length;
7797 gfc_add_modify (&lse.pre, desc, tmp);
7798 }
7799
7800 if (expr1_vptr)
7801 gfc_free_expr (expr1_vptr);
7802
7803 gfc_add_block_to_block (&block, &lse.pre);
7804 if (rank_remap)
7805 gfc_add_block_to_block (&block, &rse.pre);
7806
7807 /* If we do bounds remapping, update LHS descriptor accordingly. */
7808 if (remap)
7809 {
7810 int dim;
7811 gcc_assert (remap->u.ar.dimen == expr1->rank);
7812
7813 if (rank_remap)
7814 {
7815 /* Do rank remapping. We already have the RHS's descriptor
7816 converted in rse and now have to build the correct LHS
7817 descriptor for it. */
7818
7819 tree dtype, data;
7820 tree offs, stride;
7821 tree lbound, ubound;
7822
7823 /* Set dtype. */
7824 dtype = gfc_conv_descriptor_dtype (desc);
7825 tmp = gfc_get_dtype (TREE_TYPE (desc));
7826 gfc_add_modify (&block, dtype, tmp);
7827
7828 /* Copy data pointer. */
7829 data = gfc_conv_descriptor_data_get (rse.expr);
7830 gfc_conv_descriptor_data_set (&block, desc, data);
7831
7832 /* Copy offset but adjust it such that it would correspond
7833 to a lbound of zero. */
7834 offs = gfc_conv_descriptor_offset_get (rse.expr);
7835 for (dim = 0; dim < expr2->rank; ++dim)
7836 {
7837 stride = gfc_conv_descriptor_stride_get (rse.expr,
7838 gfc_rank_cst[dim]);
7839 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
7840 gfc_rank_cst[dim]);
7841 tmp = fold_build2_loc (input_location, MULT_EXPR,
7842 gfc_array_index_type, stride, lbound);
7843 offs = fold_build2_loc (input_location, PLUS_EXPR,
7844 gfc_array_index_type, offs, tmp);
7845 }
7846 gfc_conv_descriptor_offset_set (&block, desc, offs);
7847
7848 /* Set the bounds as declared for the LHS and calculate strides as
7849 well as another offset update accordingly. */
7850 stride = gfc_conv_descriptor_stride_get (rse.expr,
7851 gfc_rank_cst[0]);
7852 for (dim = 0; dim < expr1->rank; ++dim)
7853 {
7854 gfc_se lower_se;
7855 gfc_se upper_se;
7856
7857 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
7858
7859 /* Convert declared bounds. */
7860 gfc_init_se (&lower_se, NULL);
7861 gfc_init_se (&upper_se, NULL);
7862 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
7863 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
7864
7865 gfc_add_block_to_block (&block, &lower_se.pre);
7866 gfc_add_block_to_block (&block, &upper_se.pre);
7867
7868 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
7869 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
7870
7871 lbound = gfc_evaluate_now (lbound, &block);
7872 ubound = gfc_evaluate_now (ubound, &block);
7873
7874 gfc_add_block_to_block (&block, &lower_se.post);
7875 gfc_add_block_to_block (&block, &upper_se.post);
7876
7877 /* Set bounds in descriptor. */
7878 gfc_conv_descriptor_lbound_set (&block, desc,
7879 gfc_rank_cst[dim], lbound);
7880 gfc_conv_descriptor_ubound_set (&block, desc,
7881 gfc_rank_cst[dim], ubound);
7882
7883 /* Set stride. */
7884 stride = gfc_evaluate_now (stride, &block);
7885 gfc_conv_descriptor_stride_set (&block, desc,
7886 gfc_rank_cst[dim], stride);
7887
7888 /* Update offset. */
7889 offs = gfc_conv_descriptor_offset_get (desc);
7890 tmp = fold_build2_loc (input_location, MULT_EXPR,
7891 gfc_array_index_type, lbound, stride);
7892 offs = fold_build2_loc (input_location, MINUS_EXPR,
7893 gfc_array_index_type, offs, tmp);
7894 offs = gfc_evaluate_now (offs, &block);
7895 gfc_conv_descriptor_offset_set (&block, desc, offs);
7896
7897 /* Update stride. */
7898 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
7899 stride = fold_build2_loc (input_location, MULT_EXPR,
7900 gfc_array_index_type, stride, tmp);
7901 }
7902 }
7903 else
7904 {
7905 /* Bounds remapping. Just shift the lower bounds. */
7906
7907 gcc_assert (expr1->rank == expr2->rank);
7908
7909 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
7910 {
7911 gfc_se lbound_se;
7912
7913 gcc_assert (remap->u.ar.start[dim]);
7914 gcc_assert (!remap->u.ar.end[dim]);
7915 gfc_init_se (&lbound_se, NULL);
7916 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
7917
7918 gfc_add_block_to_block (&block, &lbound_se.pre);
7919 gfc_conv_shift_descriptor_lbound (&block, desc,
7920 dim, lbound_se.expr);
7921 gfc_add_block_to_block (&block, &lbound_se.post);
7922 }
7923 }
7924 }
7925
7926 /* Check string lengths if applicable. The check is only really added
7927 to the output code if -fbounds-check is enabled. */
7928 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
7929 {
7930 gcc_assert (expr2->ts.type == BT_CHARACTER);
7931 gcc_assert (strlen_lhs && strlen_rhs);
7932 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
7933 strlen_lhs, strlen_rhs, &block);
7934 }
7935
7936 /* If rank remapping was done, check with -fcheck=bounds that
7937 the target is at least as large as the pointer. */
7938 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
7939 {
7940 tree lsize, rsize;
7941 tree fault;
7942 const char* msg;
7943
7944 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
7945 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
7946
7947 lsize = gfc_evaluate_now (lsize, &block);
7948 rsize = gfc_evaluate_now (rsize, &block);
7949 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
7950 rsize, lsize);
7951
7952 msg = _("Target of rank remapping is too small (%ld < %ld)");
7953 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
7954 msg, rsize, lsize);
7955 }
7956
7957 gfc_add_block_to_block (&block, &lse.post);
7958 if (rank_remap)
7959 gfc_add_block_to_block (&block, &rse.post);
7960 }
7961
7962 return gfc_finish_block (&block);
7963 }
7964
7965
7966 /* Makes sure se is suitable for passing as a function string parameter. */
7967 /* TODO: Need to check all callers of this function. It may be abused. */
7968
7969 void
7970 gfc_conv_string_parameter (gfc_se * se)
7971 {
7972 tree type;
7973
7974 if (TREE_CODE (se->expr) == STRING_CST)
7975 {
7976 type = TREE_TYPE (TREE_TYPE (se->expr));
7977 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
7978 return;
7979 }
7980
7981 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
7982 {
7983 if (TREE_CODE (se->expr) != INDIRECT_REF)
7984 {
7985 type = TREE_TYPE (se->expr);
7986 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
7987 }
7988 else
7989 {
7990 type = gfc_get_character_type_len (gfc_default_character_kind,
7991 se->string_length);
7992 type = build_pointer_type (type);
7993 se->expr = gfc_build_addr_expr (type, se->expr);
7994 }
7995 }
7996
7997 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
7998 }
7999
8000
8001 /* Generate code for assignment of scalar variables. Includes character
8002 strings and derived types with allocatable components.
8003 If you know that the LHS has no allocations, set dealloc to false.
8004
8005 DEEP_COPY has no effect if the typespec TS is not a derived type with
8006 allocatable components. Otherwise, if it is set, an explicit copy of each
8007 allocatable component is made. This is necessary as a simple copy of the
8008 whole object would copy array descriptors as is, so that the lhs's
8009 allocatable components would point to the rhs's after the assignment.
8010 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
8011 necessary if the rhs is a non-pointer function, as the allocatable components
8012 are not accessible by other means than the function's result after the
8013 function has returned. It is even more subtle when temporaries are involved,
8014 as the two following examples show:
8015 1. When we evaluate an array constructor, a temporary is created. Thus
8016 there is theoretically no alias possible. However, no deep copy is
8017 made for this temporary, so that if the constructor is made of one or
8018 more variable with allocatable components, those components still point
8019 to the variable's: DEEP_COPY should be set for the assignment from the
8020 temporary to the lhs in that case.
8021 2. When assigning a scalar to an array, we evaluate the scalar value out
8022 of the loop, store it into a temporary variable, and assign from that.
8023 In that case, deep copying when assigning to the temporary would be a
8024 waste of resources; however deep copies should happen when assigning from
8025 the temporary to each array element: again DEEP_COPY should be set for
8026 the assignment from the temporary to the lhs. */
8027
8028 tree
8029 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
8030 bool l_is_temp, bool deep_copy, bool dealloc)
8031 {
8032 stmtblock_t block;
8033 tree tmp;
8034 tree cond;
8035
8036 gfc_init_block (&block);
8037
8038 if (ts.type == BT_CHARACTER)
8039 {
8040 tree rlen = NULL;
8041 tree llen = NULL;
8042
8043 if (lse->string_length != NULL_TREE)
8044 {
8045 gfc_conv_string_parameter (lse);
8046 gfc_add_block_to_block (&block, &lse->pre);
8047 llen = lse->string_length;
8048 }
8049
8050 if (rse->string_length != NULL_TREE)
8051 {
8052 gcc_assert (rse->string_length != NULL_TREE);
8053 gfc_conv_string_parameter (rse);
8054 gfc_add_block_to_block (&block, &rse->pre);
8055 rlen = rse->string_length;
8056 }
8057
8058 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
8059 rse->expr, ts.kind);
8060 }
8061 else if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
8062 {
8063 tree tmp_var = NULL_TREE;
8064 cond = NULL_TREE;
8065
8066 /* Are the rhs and the lhs the same? */
8067 if (deep_copy)
8068 {
8069 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8070 gfc_build_addr_expr (NULL_TREE, lse->expr),
8071 gfc_build_addr_expr (NULL_TREE, rse->expr));
8072 cond = gfc_evaluate_now (cond, &lse->pre);
8073 }
8074
8075 /* Deallocate the lhs allocated components as long as it is not
8076 the same as the rhs. This must be done following the assignment
8077 to prevent deallocating data that could be used in the rhs
8078 expression. */
8079 if (!l_is_temp && dealloc)
8080 {
8081 tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
8082 tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var, 0);
8083 if (deep_copy)
8084 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8085 tmp);
8086 gfc_add_expr_to_block (&lse->post, tmp);
8087 }
8088
8089 gfc_add_block_to_block (&block, &rse->pre);
8090 gfc_add_block_to_block (&block, &lse->pre);
8091
8092 gfc_add_modify (&block, lse->expr,
8093 fold_convert (TREE_TYPE (lse->expr), rse->expr));
8094
8095 /* Restore pointer address of coarray components. */
8096 if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
8097 {
8098 tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
8099 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8100 tmp);
8101 gfc_add_expr_to_block (&block, tmp);
8102 }
8103
8104 /* Do a deep copy if the rhs is a variable, if it is not the
8105 same as the lhs. */
8106 if (deep_copy)
8107 {
8108 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0);
8109 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8110 tmp);
8111 gfc_add_expr_to_block (&block, tmp);
8112 }
8113 }
8114 else if (ts.type == BT_DERIVED || ts.type == BT_CLASS)
8115 {
8116 gfc_add_block_to_block (&block, &lse->pre);
8117 gfc_add_block_to_block (&block, &rse->pre);
8118 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
8119 TREE_TYPE (lse->expr), rse->expr);
8120 gfc_add_modify (&block, lse->expr, tmp);
8121 }
8122 else
8123 {
8124 gfc_add_block_to_block (&block, &lse->pre);
8125 gfc_add_block_to_block (&block, &rse->pre);
8126
8127 gfc_add_modify (&block, lse->expr,
8128 fold_convert (TREE_TYPE (lse->expr), rse->expr));
8129 }
8130
8131 gfc_add_block_to_block (&block, &lse->post);
8132 gfc_add_block_to_block (&block, &rse->post);
8133
8134 return gfc_finish_block (&block);
8135 }
8136
8137
8138 /* There are quite a lot of restrictions on the optimisation in using an
8139 array function assign without a temporary. */
8140
8141 static bool
8142 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
8143 {
8144 gfc_ref * ref;
8145 bool seen_array_ref;
8146 bool c = false;
8147 gfc_symbol *sym = expr1->symtree->n.sym;
8148
8149 /* Play it safe with class functions assigned to a derived type. */
8150 if (gfc_is_alloc_class_array_function (expr2)
8151 && expr1->ts.type == BT_DERIVED)
8152 return true;
8153
8154 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
8155 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
8156 return true;
8157
8158 /* Elemental functions are scalarized so that they don't need a
8159 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
8160 they would need special treatment in gfc_trans_arrayfunc_assign. */
8161 if (expr2->value.function.esym != NULL
8162 && expr2->value.function.esym->attr.elemental)
8163 return true;
8164
8165 /* Need a temporary if rhs is not FULL or a contiguous section. */
8166 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
8167 return true;
8168
8169 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
8170 if (gfc_ref_needs_temporary_p (expr1->ref))
8171 return true;
8172
8173 /* Functions returning pointers or allocatables need temporaries. */
8174 c = expr2->value.function.esym
8175 ? (expr2->value.function.esym->attr.pointer
8176 || expr2->value.function.esym->attr.allocatable)
8177 : (expr2->symtree->n.sym->attr.pointer
8178 || expr2->symtree->n.sym->attr.allocatable);
8179 if (c)
8180 return true;
8181
8182 /* Character array functions need temporaries unless the
8183 character lengths are the same. */
8184 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
8185 {
8186 if (expr1->ts.u.cl->length == NULL
8187 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8188 return true;
8189
8190 if (expr2->ts.u.cl->length == NULL
8191 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8192 return true;
8193
8194 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
8195 expr2->ts.u.cl->length->value.integer) != 0)
8196 return true;
8197 }
8198
8199 /* Check that no LHS component references appear during an array
8200 reference. This is needed because we do not have the means to
8201 span any arbitrary stride with an array descriptor. This check
8202 is not needed for the rhs because the function result has to be
8203 a complete type. */
8204 seen_array_ref = false;
8205 for (ref = expr1->ref; ref; ref = ref->next)
8206 {
8207 if (ref->type == REF_ARRAY)
8208 seen_array_ref= true;
8209 else if (ref->type == REF_COMPONENT && seen_array_ref)
8210 return true;
8211 }
8212
8213 /* Check for a dependency. */
8214 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
8215 expr2->value.function.esym,
8216 expr2->value.function.actual,
8217 NOT_ELEMENTAL))
8218 return true;
8219
8220 /* If we have reached here with an intrinsic function, we do not
8221 need a temporary except in the particular case that reallocation
8222 on assignment is active and the lhs is allocatable and a target. */
8223 if (expr2->value.function.isym)
8224 return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target);
8225
8226 /* If the LHS is a dummy, we need a temporary if it is not
8227 INTENT(OUT). */
8228 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
8229 return true;
8230
8231 /* If the lhs has been host_associated, is in common, a pointer or is
8232 a target and the function is not using a RESULT variable, aliasing
8233 can occur and a temporary is needed. */
8234 if ((sym->attr.host_assoc
8235 || sym->attr.in_common
8236 || sym->attr.pointer
8237 || sym->attr.cray_pointee
8238 || sym->attr.target)
8239 && expr2->symtree != NULL
8240 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
8241 return true;
8242
8243 /* A PURE function can unconditionally be called without a temporary. */
8244 if (expr2->value.function.esym != NULL
8245 && expr2->value.function.esym->attr.pure)
8246 return false;
8247
8248 /* Implicit_pure functions are those which could legally be declared
8249 to be PURE. */
8250 if (expr2->value.function.esym != NULL
8251 && expr2->value.function.esym->attr.implicit_pure)
8252 return false;
8253
8254 if (!sym->attr.use_assoc
8255 && !sym->attr.in_common
8256 && !sym->attr.pointer
8257 && !sym->attr.target
8258 && !sym->attr.cray_pointee
8259 && expr2->value.function.esym)
8260 {
8261 /* A temporary is not needed if the function is not contained and
8262 the variable is local or host associated and not a pointer or
8263 a target. */
8264 if (!expr2->value.function.esym->attr.contained)
8265 return false;
8266
8267 /* A temporary is not needed if the lhs has never been host
8268 associated and the procedure is contained. */
8269 else if (!sym->attr.host_assoc)
8270 return false;
8271
8272 /* A temporary is not needed if the variable is local and not
8273 a pointer, a target or a result. */
8274 if (sym->ns->parent
8275 && expr2->value.function.esym->ns == sym->ns->parent)
8276 return false;
8277 }
8278
8279 /* Default to temporary use. */
8280 return true;
8281 }
8282
8283
8284 /* Provide the loop info so that the lhs descriptor can be built for
8285 reallocatable assignments from extrinsic function calls. */
8286
8287 static void
8288 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
8289 gfc_loopinfo *loop)
8290 {
8291 /* Signal that the function call should not be made by
8292 gfc_conv_loop_setup. */
8293 se->ss->is_alloc_lhs = 1;
8294 gfc_init_loopinfo (loop);
8295 gfc_add_ss_to_loop (loop, *ss);
8296 gfc_add_ss_to_loop (loop, se->ss);
8297 gfc_conv_ss_startstride (loop);
8298 gfc_conv_loop_setup (loop, where);
8299 gfc_copy_loopinfo_to_se (se, loop);
8300 gfc_add_block_to_block (&se->pre, &loop->pre);
8301 gfc_add_block_to_block (&se->pre, &loop->post);
8302 se->ss->is_alloc_lhs = 0;
8303 }
8304
8305
8306 /* For assignment to a reallocatable lhs from intrinsic functions,
8307 replace the se.expr (ie. the result) with a temporary descriptor.
8308 Null the data field so that the library allocates space for the
8309 result. Free the data of the original descriptor after the function,
8310 in case it appears in an argument expression and transfer the
8311 result to the original descriptor. */
8312
8313 static void
8314 fcncall_realloc_result (gfc_se *se, int rank)
8315 {
8316 tree desc;
8317 tree res_desc;
8318 tree tmp;
8319 tree offset;
8320 tree zero_cond;
8321 int n;
8322
8323 /* Use the allocation done by the library. Substitute the lhs
8324 descriptor with a copy, whose data field is nulled.*/
8325 desc = build_fold_indirect_ref_loc (input_location, se->expr);
8326 if (POINTER_TYPE_P (TREE_TYPE (desc)))
8327 desc = build_fold_indirect_ref_loc (input_location, desc);
8328
8329 /* Unallocated, the descriptor does not have a dtype. */
8330 tmp = gfc_conv_descriptor_dtype (desc);
8331 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
8332
8333 res_desc = gfc_evaluate_now (desc, &se->pre);
8334 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
8335 se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
8336
8337 /* Free the lhs after the function call and copy the result data to
8338 the lhs descriptor. */
8339 tmp = gfc_conv_descriptor_data_get (desc);
8340 zero_cond = fold_build2_loc (input_location, EQ_EXPR,
8341 boolean_type_node, tmp,
8342 build_int_cst (TREE_TYPE (tmp), 0));
8343 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
8344 tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
8345 gfc_add_expr_to_block (&se->post, tmp);
8346
8347 tmp = gfc_conv_descriptor_data_get (res_desc);
8348 gfc_conv_descriptor_data_set (&se->post, desc, tmp);
8349
8350 /* Check that the shapes are the same between lhs and expression. */
8351 for (n = 0 ; n < rank; n++)
8352 {
8353 tree tmp1;
8354 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
8355 tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
8356 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8357 gfc_array_index_type, tmp, tmp1);
8358 tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
8359 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8360 gfc_array_index_type, tmp, tmp1);
8361 tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
8362 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8363 gfc_array_index_type, tmp, tmp1);
8364 tmp = fold_build2_loc (input_location, NE_EXPR,
8365 boolean_type_node, tmp,
8366 gfc_index_zero_node);
8367 tmp = gfc_evaluate_now (tmp, &se->post);
8368 zero_cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
8369 boolean_type_node, tmp,
8370 zero_cond);
8371 }
8372
8373 /* 'zero_cond' being true is equal to lhs not being allocated or the
8374 shapes being different. */
8375 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
8376
8377 /* Now reset the bounds returned from the function call to bounds based
8378 on the lhs lbounds, except where the lhs is not allocated or the shapes
8379 of 'variable and 'expr' are different. Set the offset accordingly. */
8380 offset = gfc_index_zero_node;
8381 for (n = 0 ; n < rank; n++)
8382 {
8383 tree lbound;
8384
8385 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
8386 lbound = fold_build3_loc (input_location, COND_EXPR,
8387 gfc_array_index_type, zero_cond,
8388 gfc_index_one_node, lbound);
8389 lbound = gfc_evaluate_now (lbound, &se->post);
8390
8391 tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
8392 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8393 gfc_array_index_type, tmp, lbound);
8394 gfc_conv_descriptor_lbound_set (&se->post, desc,
8395 gfc_rank_cst[n], lbound);
8396 gfc_conv_descriptor_ubound_set (&se->post, desc,
8397 gfc_rank_cst[n], tmp);
8398
8399 /* Set stride and accumulate the offset. */
8400 tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
8401 gfc_conv_descriptor_stride_set (&se->post, desc,
8402 gfc_rank_cst[n], tmp);
8403 tmp = fold_build2_loc (input_location, MULT_EXPR,
8404 gfc_array_index_type, lbound, tmp);
8405 offset = fold_build2_loc (input_location, MINUS_EXPR,
8406 gfc_array_index_type, offset, tmp);
8407 offset = gfc_evaluate_now (offset, &se->post);
8408 }
8409
8410 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
8411 }
8412
8413
8414
8415 /* Try to translate array(:) = func (...), where func is a transformational
8416 array function, without using a temporary. Returns NULL if this isn't the
8417 case. */
8418
8419 static tree
8420 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
8421 {
8422 gfc_se se;
8423 gfc_ss *ss = NULL;
8424 gfc_component *comp = NULL;
8425 gfc_loopinfo loop;
8426
8427 if (arrayfunc_assign_needs_temporary (expr1, expr2))
8428 return NULL;
8429
8430 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
8431 functions. */
8432 comp = gfc_get_proc_ptr_comp (expr2);
8433 gcc_assert (expr2->value.function.isym
8434 || (comp && comp->attr.dimension)
8435 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
8436 && expr2->value.function.esym->result->attr.dimension));
8437
8438 gfc_init_se (&se, NULL);
8439 gfc_start_block (&se.pre);
8440 se.want_pointer = 1;
8441
8442 gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
8443
8444 if (expr1->ts.type == BT_DERIVED
8445 && expr1->ts.u.derived->attr.alloc_comp)
8446 {
8447 tree tmp;
8448 tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, se.expr,
8449 expr1->rank);
8450 gfc_add_expr_to_block (&se.pre, tmp);
8451 }
8452
8453 se.direct_byref = 1;
8454 se.ss = gfc_walk_expr (expr2);
8455 gcc_assert (se.ss != gfc_ss_terminator);
8456
8457 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
8458 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
8459 Clearly, this cannot be done for an allocatable function result, since
8460 the shape of the result is unknown and, in any case, the function must
8461 correctly take care of the reallocation internally. For intrinsic
8462 calls, the array data is freed and the library takes care of allocation.
8463 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
8464 to the library. */
8465 if (flag_realloc_lhs
8466 && gfc_is_reallocatable_lhs (expr1)
8467 && !gfc_expr_attr (expr1).codimension
8468 && !gfc_is_coindexed (expr1)
8469 && !(expr2->value.function.esym
8470 && expr2->value.function.esym->result->attr.allocatable))
8471 {
8472 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
8473
8474 if (!expr2->value.function.isym)
8475 {
8476 ss = gfc_walk_expr (expr1);
8477 gcc_assert (ss != gfc_ss_terminator);
8478
8479 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
8480 ss->is_alloc_lhs = 1;
8481 }
8482 else
8483 fcncall_realloc_result (&se, expr1->rank);
8484 }
8485
8486 gfc_conv_function_expr (&se, expr2);
8487 gfc_add_block_to_block (&se.pre, &se.post);
8488
8489 if (ss)
8490 gfc_cleanup_loop (&loop);
8491 else
8492 gfc_free_ss_chain (se.ss);
8493
8494 return gfc_finish_block (&se.pre);
8495 }
8496
8497
8498 /* Try to efficiently translate array(:) = 0. Return NULL if this
8499 can't be done. */
8500
8501 static tree
8502 gfc_trans_zero_assign (gfc_expr * expr)
8503 {
8504 tree dest, len, type;
8505 tree tmp;
8506 gfc_symbol *sym;
8507
8508 sym = expr->symtree->n.sym;
8509 dest = gfc_get_symbol_decl (sym);
8510
8511 type = TREE_TYPE (dest);
8512 if (POINTER_TYPE_P (type))
8513 type = TREE_TYPE (type);
8514 if (!GFC_ARRAY_TYPE_P (type))
8515 return NULL_TREE;
8516
8517 /* Determine the length of the array. */
8518 len = GFC_TYPE_ARRAY_SIZE (type);
8519 if (!len || TREE_CODE (len) != INTEGER_CST)
8520 return NULL_TREE;
8521
8522 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
8523 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
8524 fold_convert (gfc_array_index_type, tmp));
8525
8526 /* If we are zeroing a local array avoid taking its address by emitting
8527 a = {} instead. */
8528 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
8529 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
8530 dest, build_constructor (TREE_TYPE (dest),
8531 NULL));
8532
8533 /* Convert arguments to the correct types. */
8534 dest = fold_convert (pvoid_type_node, dest);
8535 len = fold_convert (size_type_node, len);
8536
8537 /* Construct call to __builtin_memset. */
8538 tmp = build_call_expr_loc (input_location,
8539 builtin_decl_explicit (BUILT_IN_MEMSET),
8540 3, dest, integer_zero_node, len);
8541 return fold_convert (void_type_node, tmp);
8542 }
8543
8544
8545 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
8546 that constructs the call to __builtin_memcpy. */
8547
8548 tree
8549 gfc_build_memcpy_call (tree dst, tree src, tree len)
8550 {
8551 tree tmp;
8552
8553 /* Convert arguments to the correct types. */
8554 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
8555 dst = gfc_build_addr_expr (pvoid_type_node, dst);
8556 else
8557 dst = fold_convert (pvoid_type_node, dst);
8558
8559 if (!POINTER_TYPE_P (TREE_TYPE (src)))
8560 src = gfc_build_addr_expr (pvoid_type_node, src);
8561 else
8562 src = fold_convert (pvoid_type_node, src);
8563
8564 len = fold_convert (size_type_node, len);
8565
8566 /* Construct call to __builtin_memcpy. */
8567 tmp = build_call_expr_loc (input_location,
8568 builtin_decl_explicit (BUILT_IN_MEMCPY),
8569 3, dst, src, len);
8570 return fold_convert (void_type_node, tmp);
8571 }
8572
8573
8574 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
8575 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
8576 source/rhs, both are gfc_full_array_ref_p which have been checked for
8577 dependencies. */
8578
8579 static tree
8580 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
8581 {
8582 tree dst, dlen, dtype;
8583 tree src, slen, stype;
8584 tree tmp;
8585
8586 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
8587 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
8588
8589 dtype = TREE_TYPE (dst);
8590 if (POINTER_TYPE_P (dtype))
8591 dtype = TREE_TYPE (dtype);
8592 stype = TREE_TYPE (src);
8593 if (POINTER_TYPE_P (stype))
8594 stype = TREE_TYPE (stype);
8595
8596 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
8597 return NULL_TREE;
8598
8599 /* Determine the lengths of the arrays. */
8600 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
8601 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
8602 return NULL_TREE;
8603 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
8604 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8605 dlen, fold_convert (gfc_array_index_type, tmp));
8606
8607 slen = GFC_TYPE_ARRAY_SIZE (stype);
8608 if (!slen || TREE_CODE (slen) != INTEGER_CST)
8609 return NULL_TREE;
8610 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
8611 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8612 slen, fold_convert (gfc_array_index_type, tmp));
8613
8614 /* Sanity check that they are the same. This should always be
8615 the case, as we should already have checked for conformance. */
8616 if (!tree_int_cst_equal (slen, dlen))
8617 return NULL_TREE;
8618
8619 return gfc_build_memcpy_call (dst, src, dlen);
8620 }
8621
8622
8623 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
8624 this can't be done. EXPR1 is the destination/lhs for which
8625 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
8626
8627 static tree
8628 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
8629 {
8630 unsigned HOST_WIDE_INT nelem;
8631 tree dst, dtype;
8632 tree src, stype;
8633 tree len;
8634 tree tmp;
8635
8636 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
8637 if (nelem == 0)
8638 return NULL_TREE;
8639
8640 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
8641 dtype = TREE_TYPE (dst);
8642 if (POINTER_TYPE_P (dtype))
8643 dtype = TREE_TYPE (dtype);
8644 if (!GFC_ARRAY_TYPE_P (dtype))
8645 return NULL_TREE;
8646
8647 /* Determine the lengths of the array. */
8648 len = GFC_TYPE_ARRAY_SIZE (dtype);
8649 if (!len || TREE_CODE (len) != INTEGER_CST)
8650 return NULL_TREE;
8651
8652 /* Confirm that the constructor is the same size. */
8653 if (compare_tree_int (len, nelem) != 0)
8654 return NULL_TREE;
8655
8656 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
8657 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
8658 fold_convert (gfc_array_index_type, tmp));
8659
8660 stype = gfc_typenode_for_spec (&expr2->ts);
8661 src = gfc_build_constant_array_constructor (expr2, stype);
8662
8663 stype = TREE_TYPE (src);
8664 if (POINTER_TYPE_P (stype))
8665 stype = TREE_TYPE (stype);
8666
8667 return gfc_build_memcpy_call (dst, src, len);
8668 }
8669
8670
8671 /* Tells whether the expression is to be treated as a variable reference. */
8672
8673 static bool
8674 expr_is_variable (gfc_expr *expr)
8675 {
8676 gfc_expr *arg;
8677 gfc_component *comp;
8678 gfc_symbol *func_ifc;
8679
8680 if (expr->expr_type == EXPR_VARIABLE)
8681 return true;
8682
8683 arg = gfc_get_noncopying_intrinsic_argument (expr);
8684 if (arg)
8685 {
8686 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
8687 return expr_is_variable (arg);
8688 }
8689
8690 /* A data-pointer-returning function should be considered as a variable
8691 too. */
8692 if (expr->expr_type == EXPR_FUNCTION
8693 && expr->ref == NULL)
8694 {
8695 if (expr->value.function.isym != NULL)
8696 return false;
8697
8698 if (expr->value.function.esym != NULL)
8699 {
8700 func_ifc = expr->value.function.esym;
8701 goto found_ifc;
8702 }
8703 else
8704 {
8705 gcc_assert (expr->symtree);
8706 func_ifc = expr->symtree->n.sym;
8707 goto found_ifc;
8708 }
8709
8710 gcc_unreachable ();
8711 }
8712
8713 comp = gfc_get_proc_ptr_comp (expr);
8714 if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
8715 && comp)
8716 {
8717 func_ifc = comp->ts.interface;
8718 goto found_ifc;
8719 }
8720
8721 if (expr->expr_type == EXPR_COMPCALL)
8722 {
8723 gcc_assert (!expr->value.compcall.tbp->is_generic);
8724 func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
8725 goto found_ifc;
8726 }
8727
8728 return false;
8729
8730 found_ifc:
8731 gcc_assert (func_ifc->attr.function
8732 && func_ifc->result != NULL);
8733 return func_ifc->result->attr.pointer;
8734 }
8735
8736
8737 /* Is the lhs OK for automatic reallocation? */
8738
8739 static bool
8740 is_scalar_reallocatable_lhs (gfc_expr *expr)
8741 {
8742 gfc_ref * ref;
8743
8744 /* An allocatable variable with no reference. */
8745 if (expr->symtree->n.sym->attr.allocatable
8746 && !expr->ref)
8747 return true;
8748
8749 /* All that can be left are allocatable components. */
8750 if ((expr->symtree->n.sym->ts.type != BT_DERIVED
8751 && expr->symtree->n.sym->ts.type != BT_CLASS)
8752 || !expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
8753 return false;
8754
8755 /* Find an allocatable component ref last. */
8756 for (ref = expr->ref; ref; ref = ref->next)
8757 if (ref->type == REF_COMPONENT
8758 && !ref->next
8759 && ref->u.c.component->attr.allocatable)
8760 return true;
8761
8762 return false;
8763 }
8764
8765
8766 /* Allocate or reallocate scalar lhs, as necessary. */
8767
8768 static void
8769 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
8770 tree string_length,
8771 gfc_expr *expr1,
8772 gfc_expr *expr2)
8773
8774 {
8775 tree cond;
8776 tree tmp;
8777 tree size;
8778 tree size_in_bytes;
8779 tree jump_label1;
8780 tree jump_label2;
8781 gfc_se lse;
8782
8783 if (!expr1 || expr1->rank)
8784 return;
8785
8786 if (!expr2 || expr2->rank)
8787 return;
8788
8789 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
8790
8791 /* Since this is a scalar lhs, we can afford to do this. That is,
8792 there is no risk of side effects being repeated. */
8793 gfc_init_se (&lse, NULL);
8794 lse.want_pointer = 1;
8795 gfc_conv_expr (&lse, expr1);
8796
8797 jump_label1 = gfc_build_label_decl (NULL_TREE);
8798 jump_label2 = gfc_build_label_decl (NULL_TREE);
8799
8800 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
8801 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
8802 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
8803 lse.expr, tmp);
8804 tmp = build3_v (COND_EXPR, cond,
8805 build1_v (GOTO_EXPR, jump_label1),
8806 build_empty_stmt (input_location));
8807 gfc_add_expr_to_block (block, tmp);
8808
8809 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
8810 {
8811 /* Use the rhs string length and the lhs element size. */
8812 size = string_length;
8813 tmp = TREE_TYPE (gfc_typenode_for_spec (&expr1->ts));
8814 tmp = TYPE_SIZE_UNIT (tmp);
8815 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
8816 TREE_TYPE (tmp), tmp,
8817 fold_convert (TREE_TYPE (tmp), size));
8818 }
8819 else
8820 {
8821 /* Otherwise use the length in bytes of the rhs. */
8822 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
8823 size_in_bytes = size;
8824 }
8825
8826 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
8827 size_in_bytes, size_one_node);
8828
8829 if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
8830 {
8831 tmp = build_call_expr_loc (input_location,
8832 builtin_decl_explicit (BUILT_IN_CALLOC),
8833 2, build_one_cst (size_type_node),
8834 size_in_bytes);
8835 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
8836 gfc_add_modify (block, lse.expr, tmp);
8837 }
8838 else
8839 {
8840 tmp = build_call_expr_loc (input_location,
8841 builtin_decl_explicit (BUILT_IN_MALLOC),
8842 1, size_in_bytes);
8843 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
8844 gfc_add_modify (block, lse.expr, tmp);
8845 }
8846
8847 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
8848 {
8849 /* Deferred characters need checking for lhs and rhs string
8850 length. Other deferred parameter variables will have to
8851 come here too. */
8852 tmp = build1_v (GOTO_EXPR, jump_label2);
8853 gfc_add_expr_to_block (block, tmp);
8854 }
8855 tmp = build1_v (LABEL_EXPR, jump_label1);
8856 gfc_add_expr_to_block (block, tmp);
8857
8858 /* For a deferred length character, reallocate if lengths of lhs and
8859 rhs are different. */
8860 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
8861 {
8862 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8863 lse.string_length, size);
8864 /* Jump past the realloc if the lengths are the same. */
8865 tmp = build3_v (COND_EXPR, cond,
8866 build1_v (GOTO_EXPR, jump_label2),
8867 build_empty_stmt (input_location));
8868 gfc_add_expr_to_block (block, tmp);
8869 tmp = build_call_expr_loc (input_location,
8870 builtin_decl_explicit (BUILT_IN_REALLOC),
8871 2, fold_convert (pvoid_type_node, lse.expr),
8872 size_in_bytes);
8873 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
8874 gfc_add_modify (block, lse.expr, tmp);
8875 tmp = build1_v (LABEL_EXPR, jump_label2);
8876 gfc_add_expr_to_block (block, tmp);
8877
8878 /* Update the lhs character length. */
8879 size = string_length;
8880 gfc_add_modify (block, lse.string_length, size);
8881 }
8882 }
8883
8884 /* Check for assignments of the type
8885
8886 a = a + 4
8887
8888 to make sure we do not check for reallocation unneccessarily. */
8889
8890
8891 static bool
8892 is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
8893 {
8894 gfc_actual_arglist *a;
8895 gfc_expr *e1, *e2;
8896
8897 switch (expr2->expr_type)
8898 {
8899 case EXPR_VARIABLE:
8900 return gfc_dep_compare_expr (expr1, expr2) == 0;
8901
8902 case EXPR_FUNCTION:
8903 if (expr2->value.function.esym
8904 && expr2->value.function.esym->attr.elemental)
8905 {
8906 for (a = expr2->value.function.actual; a != NULL; a = a->next)
8907 {
8908 e1 = a->expr;
8909 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
8910 return false;
8911 }
8912 return true;
8913 }
8914 else if (expr2->value.function.isym
8915 && expr2->value.function.isym->elemental)
8916 {
8917 for (a = expr2->value.function.actual; a != NULL; a = a->next)
8918 {
8919 e1 = a->expr;
8920 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
8921 return false;
8922 }
8923 return true;
8924 }
8925
8926 break;
8927
8928 case EXPR_OP:
8929 switch (expr2->value.op.op)
8930 {
8931 case INTRINSIC_NOT:
8932 case INTRINSIC_UPLUS:
8933 case INTRINSIC_UMINUS:
8934 case INTRINSIC_PARENTHESES:
8935 return is_runtime_conformable (expr1, expr2->value.op.op1);
8936
8937 case INTRINSIC_PLUS:
8938 case INTRINSIC_MINUS:
8939 case INTRINSIC_TIMES:
8940 case INTRINSIC_DIVIDE:
8941 case INTRINSIC_POWER:
8942 case INTRINSIC_AND:
8943 case INTRINSIC_OR:
8944 case INTRINSIC_EQV:
8945 case INTRINSIC_NEQV:
8946 case INTRINSIC_EQ:
8947 case INTRINSIC_NE:
8948 case INTRINSIC_GT:
8949 case INTRINSIC_GE:
8950 case INTRINSIC_LT:
8951 case INTRINSIC_LE:
8952 case INTRINSIC_EQ_OS:
8953 case INTRINSIC_NE_OS:
8954 case INTRINSIC_GT_OS:
8955 case INTRINSIC_GE_OS:
8956 case INTRINSIC_LT_OS:
8957 case INTRINSIC_LE_OS:
8958
8959 e1 = expr2->value.op.op1;
8960 e2 = expr2->value.op.op2;
8961
8962 if (e1->rank == 0 && e2->rank > 0)
8963 return is_runtime_conformable (expr1, e2);
8964 else if (e1->rank > 0 && e2->rank == 0)
8965 return is_runtime_conformable (expr1, e1);
8966 else if (e1->rank > 0 && e2->rank > 0)
8967 return is_runtime_conformable (expr1, e1)
8968 && is_runtime_conformable (expr1, e2);
8969 break;
8970
8971 default:
8972 break;
8973
8974 }
8975
8976 break;
8977
8978 default:
8979 break;
8980 }
8981 return false;
8982 }
8983
8984 /* Subroutine of gfc_trans_assignment that actually scalarizes the
8985 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
8986 init_flag indicates initialization expressions and dealloc that no
8987 deallocate prior assignment is needed (if in doubt, set true). */
8988
8989 static tree
8990 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
8991 bool dealloc)
8992 {
8993 gfc_se lse;
8994 gfc_se rse;
8995 gfc_ss *lss;
8996 gfc_ss *lss_section;
8997 gfc_ss *rss;
8998 gfc_loopinfo loop;
8999 tree tmp;
9000 stmtblock_t block;
9001 stmtblock_t body;
9002 bool l_is_temp;
9003 bool scalar_to_array;
9004 tree string_length;
9005 int n;
9006
9007 /* Assignment of the form lhs = rhs. */
9008 gfc_start_block (&block);
9009
9010 gfc_init_se (&lse, NULL);
9011 gfc_init_se (&rse, NULL);
9012
9013 /* Walk the lhs. */
9014 lss = gfc_walk_expr (expr1);
9015 if (gfc_is_reallocatable_lhs (expr1)
9016 && !(expr2->expr_type == EXPR_FUNCTION
9017 && expr2->value.function.isym != NULL))
9018 lss->is_alloc_lhs = 1;
9019 rss = NULL;
9020
9021 if ((expr1->ts.type == BT_DERIVED)
9022 && (gfc_is_alloc_class_array_function (expr2)
9023 || gfc_is_alloc_class_scalar_function (expr2)))
9024 expr2->must_finalize = 1;
9025
9026 if (lss != gfc_ss_terminator)
9027 {
9028 /* The assignment needs scalarization. */
9029 lss_section = lss;
9030
9031 /* Find a non-scalar SS from the lhs. */
9032 while (lss_section != gfc_ss_terminator
9033 && lss_section->info->type != GFC_SS_SECTION)
9034 lss_section = lss_section->next;
9035
9036 gcc_assert (lss_section != gfc_ss_terminator);
9037
9038 /* Initialize the scalarizer. */
9039 gfc_init_loopinfo (&loop);
9040
9041 /* Walk the rhs. */
9042 rss = gfc_walk_expr (expr2);
9043 if (rss == gfc_ss_terminator)
9044 /* The rhs is scalar. Add a ss for the expression. */
9045 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
9046
9047 /* Associate the SS with the loop. */
9048 gfc_add_ss_to_loop (&loop, lss);
9049 gfc_add_ss_to_loop (&loop, rss);
9050
9051 /* Calculate the bounds of the scalarization. */
9052 gfc_conv_ss_startstride (&loop);
9053 /* Enable loop reversal. */
9054 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
9055 loop.reverse[n] = GFC_ENABLE_REVERSE;
9056 /* Resolve any data dependencies in the statement. */
9057 gfc_conv_resolve_dependencies (&loop, lss, rss);
9058 /* Setup the scalarizing loops. */
9059 gfc_conv_loop_setup (&loop, &expr2->where);
9060
9061 /* Setup the gfc_se structures. */
9062 gfc_copy_loopinfo_to_se (&lse, &loop);
9063 gfc_copy_loopinfo_to_se (&rse, &loop);
9064
9065 rse.ss = rss;
9066 gfc_mark_ss_chain_used (rss, 1);
9067 if (loop.temp_ss == NULL)
9068 {
9069 lse.ss = lss;
9070 gfc_mark_ss_chain_used (lss, 1);
9071 }
9072 else
9073 {
9074 lse.ss = loop.temp_ss;
9075 gfc_mark_ss_chain_used (lss, 3);
9076 gfc_mark_ss_chain_used (loop.temp_ss, 3);
9077 }
9078
9079 /* Allow the scalarizer to workshare array assignments. */
9080 if ((ompws_flags & OMPWS_WORKSHARE_FLAG) && loop.temp_ss == NULL)
9081 ompws_flags |= OMPWS_SCALARIZER_WS;
9082
9083 /* Start the scalarized loop body. */
9084 gfc_start_scalarized_body (&loop, &body);
9085 }
9086 else
9087 gfc_init_block (&body);
9088
9089 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
9090
9091 /* Translate the expression. */
9092 gfc_conv_expr (&rse, expr2);
9093
9094 /* Deal with the case of a scalar class function assigned to a derived type. */
9095 if (gfc_is_alloc_class_scalar_function (expr2)
9096 && expr1->ts.type == BT_DERIVED)
9097 {
9098 rse.expr = gfc_class_data_get (rse.expr);
9099 rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
9100 }
9101
9102 /* Stabilize a string length for temporaries. */
9103 if (expr2->ts.type == BT_CHARACTER)
9104 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
9105 else
9106 string_length = NULL_TREE;
9107
9108 if (l_is_temp)
9109 {
9110 gfc_conv_tmp_array_ref (&lse);
9111 if (expr2->ts.type == BT_CHARACTER)
9112 lse.string_length = string_length;
9113 }
9114 else
9115 gfc_conv_expr (&lse, expr1);
9116
9117 /* Assignments of scalar derived types with allocatable components
9118 to arrays must be done with a deep copy and the rhs temporary
9119 must have its components deallocated afterwards. */
9120 scalar_to_array = (expr2->ts.type == BT_DERIVED
9121 && expr2->ts.u.derived->attr.alloc_comp
9122 && !expr_is_variable (expr2)
9123 && !gfc_is_constant_expr (expr2)
9124 && expr1->rank && !expr2->rank);
9125 scalar_to_array |= (expr1->ts.type == BT_DERIVED
9126 && expr1->rank
9127 && expr1->ts.u.derived->attr.alloc_comp
9128 && gfc_is_alloc_class_scalar_function (expr2));
9129 if (scalar_to_array && dealloc)
9130 {
9131 tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
9132 gfc_add_expr_to_block (&loop.post, tmp);
9133 }
9134
9135 /* When assigning a character function result to a deferred-length variable,
9136 the function call must happen before the (re)allocation of the lhs -
9137 otherwise the character length of the result is not known.
9138 NOTE: This relies on having the exact dependence of the length type
9139 parameter available to the caller; gfortran saves it in the .mod files. */
9140 if (flag_realloc_lhs && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred)
9141 gfc_add_block_to_block (&block, &rse.pre);
9142
9143 /* Nullify the allocatable components corresponding to those of the lhs
9144 derived type, so that the finalization of the function result does not
9145 affect the lhs of the assignment. Prepend is used to ensure that the
9146 nullification occurs before the call to the finalizer. In the case of
9147 a scalar to array assignment, this is done in gfc_trans_scalar_assign
9148 as part of the deep copy. */
9149 if (!scalar_to_array && (expr1->ts.type == BT_DERIVED)
9150 && (gfc_is_alloc_class_array_function (expr2)
9151 || gfc_is_alloc_class_scalar_function (expr2)))
9152 {
9153 tmp = rse.expr;
9154 tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
9155 gfc_prepend_expr_to_block (&rse.post, tmp);
9156 if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
9157 gfc_add_block_to_block (&loop.post, &rse.post);
9158 }
9159
9160 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
9161 l_is_temp || init_flag,
9162 expr_is_variable (expr2) || scalar_to_array
9163 || expr2->expr_type == EXPR_ARRAY, dealloc);
9164 gfc_add_expr_to_block (&body, tmp);
9165
9166 if (lss == gfc_ss_terminator)
9167 {
9168 /* F2003: Add the code for reallocation on assignment. */
9169 if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1))
9170 alloc_scalar_allocatable_for_assignment (&block, string_length,
9171 expr1, expr2);
9172
9173 /* Use the scalar assignment as is. */
9174 gfc_add_block_to_block (&block, &body);
9175 }
9176 else
9177 {
9178 gcc_assert (lse.ss == gfc_ss_terminator
9179 && rse.ss == gfc_ss_terminator);
9180
9181 if (l_is_temp)
9182 {
9183 gfc_trans_scalarized_loop_boundary (&loop, &body);
9184
9185 /* We need to copy the temporary to the actual lhs. */
9186 gfc_init_se (&lse, NULL);
9187 gfc_init_se (&rse, NULL);
9188 gfc_copy_loopinfo_to_se (&lse, &loop);
9189 gfc_copy_loopinfo_to_se (&rse, &loop);
9190
9191 rse.ss = loop.temp_ss;
9192 lse.ss = lss;
9193
9194 gfc_conv_tmp_array_ref (&rse);
9195 gfc_conv_expr (&lse, expr1);
9196
9197 gcc_assert (lse.ss == gfc_ss_terminator
9198 && rse.ss == gfc_ss_terminator);
9199
9200 if (expr2->ts.type == BT_CHARACTER)
9201 rse.string_length = string_length;
9202
9203 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
9204 false, false, dealloc);
9205 gfc_add_expr_to_block (&body, tmp);
9206 }
9207
9208 /* F2003: Allocate or reallocate lhs of allocatable array. */
9209 if (flag_realloc_lhs
9210 && gfc_is_reallocatable_lhs (expr1)
9211 && !gfc_expr_attr (expr1).codimension
9212 && !gfc_is_coindexed (expr1)
9213 && expr2->rank
9214 && !is_runtime_conformable (expr1, expr2))
9215 {
9216 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
9217 ompws_flags &= ~OMPWS_SCALARIZER_WS;
9218 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
9219 if (tmp != NULL_TREE)
9220 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
9221 }
9222
9223 /* Generate the copying loops. */
9224 gfc_trans_scalarizing_loops (&loop, &body);
9225
9226 /* Wrap the whole thing up. */
9227 gfc_add_block_to_block (&block, &loop.pre);
9228 gfc_add_block_to_block (&block, &loop.post);
9229
9230 gfc_cleanup_loop (&loop);
9231 }
9232
9233 return gfc_finish_block (&block);
9234 }
9235
9236
9237 /* Check whether EXPR is a copyable array. */
9238
9239 static bool
9240 copyable_array_p (gfc_expr * expr)
9241 {
9242 if (expr->expr_type != EXPR_VARIABLE)
9243 return false;
9244
9245 /* First check it's an array. */
9246 if (expr->rank < 1 || !expr->ref || expr->ref->next)
9247 return false;
9248
9249 if (!gfc_full_array_ref_p (expr->ref, NULL))
9250 return false;
9251
9252 /* Next check that it's of a simple enough type. */
9253 switch (expr->ts.type)
9254 {
9255 case BT_INTEGER:
9256 case BT_REAL:
9257 case BT_COMPLEX:
9258 case BT_LOGICAL:
9259 return true;
9260
9261 case BT_CHARACTER:
9262 return false;
9263
9264 case BT_DERIVED:
9265 return !expr->ts.u.derived->attr.alloc_comp;
9266
9267 default:
9268 break;
9269 }
9270
9271 return false;
9272 }
9273
9274 /* Translate an assignment. */
9275
9276 tree
9277 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
9278 bool dealloc)
9279 {
9280 tree tmp;
9281
9282 /* Special case a single function returning an array. */
9283 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
9284 {
9285 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
9286 if (tmp)
9287 return tmp;
9288 }
9289
9290 /* Special case assigning an array to zero. */
9291 if (copyable_array_p (expr1)
9292 && is_zero_initializer_p (expr2))
9293 {
9294 tmp = gfc_trans_zero_assign (expr1);
9295 if (tmp)
9296 return tmp;
9297 }
9298
9299 /* Special case copying one array to another. */
9300 if (copyable_array_p (expr1)
9301 && copyable_array_p (expr2)
9302 && gfc_compare_types (&expr1->ts, &expr2->ts)
9303 && !gfc_check_dependency (expr1, expr2, 0))
9304 {
9305 tmp = gfc_trans_array_copy (expr1, expr2);
9306 if (tmp)
9307 return tmp;
9308 }
9309
9310 /* Special case initializing an array from a constant array constructor. */
9311 if (copyable_array_p (expr1)
9312 && expr2->expr_type == EXPR_ARRAY
9313 && gfc_compare_types (&expr1->ts, &expr2->ts))
9314 {
9315 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
9316 if (tmp)
9317 return tmp;
9318 }
9319
9320 /* Fallback to the scalarizer to generate explicit loops. */
9321 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc);
9322 }
9323
9324 tree
9325 gfc_trans_init_assign (gfc_code * code)
9326 {
9327 return gfc_trans_assignment (code->expr1, code->expr2, true, false);
9328 }
9329
9330 tree
9331 gfc_trans_assign (gfc_code * code)
9332 {
9333 return gfc_trans_assignment (code->expr1, code->expr2, false, true);
9334 }