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