]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/trans-array.cc
Daily bump.
[thirdparty/gcc.git] / gcc / fortran / trans-array.cc
CommitLineData
6de9cd9a 1/* Array translation routines
83ffe9cd 2 Copyright (C) 2002-2023 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 21
e53b6e56 22/* trans-array.cc-- Various array related code, including scalarization,
6de9cd9a
DN
23 allocation, initialization and other support routines. */
24
25/* How the scalarizer works.
26 In gfortran, array expressions use the same core routines as scalar
27 expressions.
28 First, a Scalarization State (SS) chain is built. This is done by walking
29 the expression tree, and building a linear list of the terms in the
30 expression. As the tree is walked, scalar subexpressions are translated.
31
32 The scalarization parameters are stored in a gfc_loopinfo structure.
33 First the start and stride of each term is calculated by
34 gfc_conv_ss_startstride. During this process the expressions for the array
35 descriptors and data pointers are also translated.
36
37 If the expression is an assignment, we must then resolve any dependencies.
eea58adb 38 In Fortran all the rhs values of an assignment must be evaluated before
6de9cd9a
DN
39 any assignments take place. This can require a temporary array to store the
40 values. We also require a temporary when we are passing array expressions
df2fba9e 41 or vector subscripts as procedure parameters.
6de9cd9a
DN
42
43 Array sections are passed without copying to a temporary. These use the
44 scalarizer to determine the shape of the section. The flag
45 loop->array_parameter tells the scalarizer that the actual values and loop
46 variables will not be required.
47
48 The function gfc_conv_loop_setup generates the scalarization setup code.
49 It determines the range of the scalarizing loop variables. If a temporary
50 is required, this is created and initialized. Code for scalar expressions
51 taken outside the loop is also generated at this time. Next the offset and
52 scaling required to translate from loop variables to array indices for each
53 term is calculated.
54
55 A call to gfc_start_scalarized_body marks the start of the scalarized
56 expression. This creates a scope and declares the loop variables. Before
57 calling this gfc_make_ss_chain_used must be used to indicate which terms
58 will be used inside this loop.
59
60 The scalar gfc_conv_* functions are then used to build the main body of the
61 scalarization loop. Scalarization loop variables and precalculated scalar
1f2959f0 62 values are automatically substituted. Note that gfc_advance_se_ss_chain
6de9cd9a
DN
63 must be used, rather than changing the se->ss directly.
64
65 For assignment expressions requiring a temporary two sub loops are
66 generated. The first stores the result of the expression in the temporary,
67 the second copies it to the result. A call to
68 gfc_trans_scalarized_loop_boundary marks the end of the main loop code and
69 the start of the copying loop. The temporary may be less than full rank.
70
71 Finally gfc_trans_scalarizing_loops is called to generate the implicit do
72 loops. The loops are added to the pre chain of the loopinfo. The post
73 chain may still contain cleanup code.
74
75 After the loop code has been added into its parent scope gfc_cleanup_loop
76 is called to free all the SS allocated by the scalarizer. */
77
78#include "config.h"
79#include "system.h"
80#include "coretypes.h"
c7131fb2 81#include "options.h"
2adfab87
AM
82#include "tree.h"
83#include "gfortran.h"
45b0be94 84#include "gimple-expr.h"
2adfab87 85#include "trans.h"
2adfab87 86#include "fold-const.h"
b7e75771 87#include "constructor.h"
6de9cd9a
DN
88#include "trans-types.h"
89#include "trans-array.h"
90#include "trans-const.h"
91#include "dependency.h"
92
b7e75771 93static bool gfc_get_array_constructor_size (mpz_t *, gfc_constructor_base);
6de9cd9a 94
13413760 95/* The contents of this structure aren't actually used, just the address. */
6de9cd9a
DN
96static gfc_ss gfc_ss_terminator_var;
97gfc_ss * const gfc_ss_terminator = &gfc_ss_terminator_var;
98
6de9cd9a
DN
99
100static tree
101gfc_array_dataptr_type (tree desc)
102{
103 return (GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc)));
104}
105
64f96237
TB
106/* Build expressions to access members of the CFI descriptor. */
107#define CFI_FIELD_BASE_ADDR 0
108#define CFI_FIELD_ELEM_LEN 1
109#define CFI_FIELD_VERSION 2
110#define CFI_FIELD_RANK 3
111#define CFI_FIELD_ATTRIBUTE 4
112#define CFI_FIELD_TYPE 5
113#define CFI_FIELD_DIM 6
114
115#define CFI_DIM_FIELD_LOWER_BOUND 0
116#define CFI_DIM_FIELD_EXTENT 1
117#define CFI_DIM_FIELD_SM 2
118
119static tree
120gfc_get_cfi_descriptor_field (tree desc, unsigned field_idx)
121{
122 tree type = TREE_TYPE (desc);
123 gcc_assert (TREE_CODE (type) == RECORD_TYPE
124 && TYPE_FIELDS (type)
125 && (strcmp ("base_addr",
126 IDENTIFIER_POINTER (DECL_NAME (TYPE_FIELDS (type))))
127 == 0));
128 tree field = gfc_advance_chain (TYPE_FIELDS (type), field_idx);
129 gcc_assert (field != NULL_TREE);
130
131 return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
132 desc, field, NULL_TREE);
133}
134
135tree
136gfc_get_cfi_desc_base_addr (tree desc)
137{
138 return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_BASE_ADDR);
139}
140
141tree
142gfc_get_cfi_desc_elem_len (tree desc)
143{
144 return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_ELEM_LEN);
145}
146
147tree
148gfc_get_cfi_desc_version (tree desc)
149{
150 return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_VERSION);
151}
152
153tree
154gfc_get_cfi_desc_rank (tree desc)
155{
156 return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_RANK);
157}
158
159tree
160gfc_get_cfi_desc_type (tree desc)
161{
162 return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_TYPE);
163}
164
165tree
166gfc_get_cfi_desc_attribute (tree desc)
167{
168 return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_ATTRIBUTE);
169}
170
171static tree
172gfc_get_cfi_dim_item (tree desc, tree idx, unsigned field_idx)
173{
174 tree tmp = gfc_get_cfi_descriptor_field (desc, CFI_FIELD_DIM);
7964ab6c 175 tmp = gfc_build_array_ref (tmp, idx, NULL_TREE, true);
64f96237
TB
176 tree field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (tmp)), field_idx);
177 gcc_assert (field != NULL_TREE);
178 return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
179 tmp, field, NULL_TREE);
180}
181
182tree
183gfc_get_cfi_dim_lbound (tree desc, tree idx)
184{
185 return gfc_get_cfi_dim_item (desc, idx, CFI_DIM_FIELD_LOWER_BOUND);
186}
187
188tree
189gfc_get_cfi_dim_extent (tree desc, tree idx)
190{
191 return gfc_get_cfi_dim_item (desc, idx, CFI_DIM_FIELD_EXTENT);
192}
193
194tree
195gfc_get_cfi_dim_sm (tree desc, tree idx)
196{
197 return gfc_get_cfi_dim_item (desc, idx, CFI_DIM_FIELD_SM);
198}
199
200#undef CFI_FIELD_BASE_ADDR
201#undef CFI_FIELD_ELEM_LEN
202#undef CFI_FIELD_VERSION
203#undef CFI_FIELD_RANK
204#undef CFI_FIELD_ATTRIBUTE
205#undef CFI_FIELD_TYPE
206#undef CFI_FIELD_DIM
207
208#undef CFI_DIM_FIELD_LOWER_BOUND
209#undef CFI_DIM_FIELD_EXTENT
210#undef CFI_DIM_FIELD_SM
6de9cd9a
DN
211
212/* Build expressions to access the members of an array descriptor.
213 It's surprisingly easy to mess up here, so never access
214 an array descriptor by "brute force", always use these
215 functions. This also avoids problems if we change the format
216 of an array descriptor.
217
218 To understand these magic numbers, look at the comments
e53b6e56 219 before gfc_build_array_type() in trans-types.cc.
6de9cd9a
DN
220
221 The code within these defines should be the only code which knows the format
222 of an array descriptor.
223
224 Any code just needing to read obtain the bounds of an array should use
225 gfc_conv_array_* rather than the following functions as these will return
226 know constant values, and work with arrays which do not have descriptors.
227
228 Don't forget to #undef these! */
229
230#define DATA_FIELD 0
231#define OFFSET_FIELD 1
232#define DTYPE_FIELD 2
ff3598bc
PT
233#define SPAN_FIELD 3
234#define DIMENSION_FIELD 4
235#define CAF_TOKEN_FIELD 5
6de9cd9a
DN
236
237#define STRIDE_SUBFIELD 0
238#define LBOUND_SUBFIELD 1
239#define UBOUND_SUBFIELD 2
240
6d65ddca
RB
241static tree
242gfc_get_descriptor_field (tree desc, unsigned field_idx)
243{
244 tree type = TREE_TYPE (desc);
245 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
246
247 tree field = gfc_advance_chain (TYPE_FIELDS (type), field_idx);
248 gcc_assert (field != NULL_TREE);
249
250 return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
251 desc, field, NULL_TREE);
252}
253
4c73896d
RH
254/* This provides READ-ONLY access to the data field. The field itself
255 doesn't have the proper type. */
256
6de9cd9a 257tree
4c73896d 258gfc_conv_descriptor_data_get (tree desc)
6de9cd9a 259{
6d65ddca 260 tree type = TREE_TYPE (desc);
92e63bd2 261 if (TREE_CODE (type) == REFERENCE_TYPE)
6d65ddca 262 gcc_unreachable ();
4c73896d 263
6d65ddca
RB
264 tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
265 return fold_convert (GFC_TYPE_ARRAY_DATAPTR_TYPE (type), field);
4c73896d
RH
266}
267
07beea0d
AH
268/* This provides WRITE access to the data field.
269
270 TUPLES_P is true if we are generating tuples.
f04986a9 271
07beea0d
AH
272 This function gets called through the following macros:
273 gfc_conv_descriptor_data_set
726a989a 274 gfc_conv_descriptor_data_set. */
4c73896d
RH
275
276void
726a989a 277gfc_conv_descriptor_data_set (stmtblock_t *block, tree desc, tree value)
4c73896d 278{
6d65ddca
RB
279 tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
280 gfc_add_modify (block, field, fold_convert (TREE_TYPE (field), value));
4c73896d
RH
281}
282
283
284/* This provides address access to the data field. This should only be
285 used by array allocation, passing this on to the runtime. */
286
287tree
288gfc_conv_descriptor_data_addr (tree desc)
289{
6d65ddca
RB
290 tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
291 return gfc_build_addr_expr (NULL_TREE, field);
6de9cd9a
DN
292}
293
568e8e1e 294static tree
6de9cd9a
DN
295gfc_conv_descriptor_offset (tree desc)
296{
6d65ddca
RB
297 tree field = gfc_get_descriptor_field (desc, OFFSET_FIELD);
298 gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
299 return field;
6de9cd9a
DN
300}
301
568e8e1e
PT
302tree
303gfc_conv_descriptor_offset_get (tree desc)
304{
305 return gfc_conv_descriptor_offset (desc);
306}
307
308void
309gfc_conv_descriptor_offset_set (stmtblock_t *block, tree desc,
310 tree value)
311{
312 tree t = gfc_conv_descriptor_offset (desc);
313 gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
314}
315
316
6de9cd9a
DN
317tree
318gfc_conv_descriptor_dtype (tree desc)
319{
6d65ddca
RB
320 tree field = gfc_get_descriptor_field (desc, DTYPE_FIELD);
321 gcc_assert (TREE_TYPE (field) == get_dtype_type_node ());
322 return field;
6de9cd9a
DN
323}
324
ff3598bc
PT
325static tree
326gfc_conv_descriptor_span (tree desc)
327{
6d65ddca
RB
328 tree field = gfc_get_descriptor_field (desc, SPAN_FIELD);
329 gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
330 return field;
ff3598bc
PT
331}
332
333tree
334gfc_conv_descriptor_span_get (tree desc)
335{
336 return gfc_conv_descriptor_span (desc);
337}
338
339void
340gfc_conv_descriptor_span_set (stmtblock_t *block, tree desc,
341 tree value)
342{
343 tree t = gfc_conv_descriptor_span (desc);
344 gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
345}
346
c62c6622 347
17aa6ab6
MM
348tree
349gfc_conv_descriptor_rank (tree desc)
350{
351 tree tmp;
352 tree dtype;
353
354 dtype = gfc_conv_descriptor_dtype (desc);
7fb43006 355 tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_RANK);
db06a76e 356 gcc_assert (tmp != NULL_TREE
7fb43006
PT
357 && TREE_TYPE (tmp) == signed_char_type_node);
358 return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
359 dtype, tmp, NULL_TREE);
17aa6ab6
MM
360}
361
362
db06a76e
PT
363/* Return the element length from the descriptor dtype field. */
364
365tree
366gfc_conv_descriptor_elem_len (tree desc)
367{
368 tree tmp;
369 tree dtype;
370
371 dtype = gfc_conv_descriptor_dtype (desc);
372 tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
373 GFC_DTYPE_ELEM_LEN);
374 gcc_assert (tmp != NULL_TREE
375 && TREE_TYPE (tmp) == size_type_node);
376 return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
377 dtype, tmp, NULL_TREE);
378}
379
380
bbf18dc5
PT
381tree
382gfc_conv_descriptor_attribute (tree desc)
383{
384 tree tmp;
385 tree dtype;
386
387 dtype = gfc_conv_descriptor_dtype (desc);
388 tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
389 GFC_DTYPE_ATTRIBUTE);
390 gcc_assert (tmp!= NULL_TREE
391 && TREE_TYPE (tmp) == short_integer_type_node);
392 return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
393 dtype, tmp, NULL_TREE);
394}
395
64f96237
TB
396tree
397gfc_conv_descriptor_type (tree desc)
398{
399 tree tmp;
400 tree dtype;
401
402 dtype = gfc_conv_descriptor_dtype (desc);
403 tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_TYPE);
404 gcc_assert (tmp!= NULL_TREE
405 && TREE_TYPE (tmp) == signed_char_type_node);
406 return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
407 dtype, tmp, NULL_TREE);
408}
409
c62c6622
TB
410tree
411gfc_get_descriptor_dimension (tree desc)
6de9cd9a 412{
6d65ddca
RB
413 tree field = gfc_get_descriptor_field (desc, DIMENSION_FIELD);
414 gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
415 && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == RECORD_TYPE);
416 return field;
c62c6622
TB
417}
418
419
420static tree
421gfc_conv_descriptor_dimension (tree desc, tree dim)
422{
423 tree tmp;
424
425 tmp = gfc_get_descriptor_dimension (desc);
426
7964ab6c 427 return gfc_build_array_ref (tmp, dim, NULL_TREE, true);
6de9cd9a
DN
428}
429
af232d48
TB
430
431tree
432gfc_conv_descriptor_token (tree desc)
433{
f19626cf 434 gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
6d65ddca 435 tree field = gfc_get_descriptor_field (desc, CAF_TOKEN_FIELD);
16023efc 436 /* Should be a restricted pointer - except in the finalization wrapper. */
6d65ddca
RB
437 gcc_assert (TREE_TYPE (field) == prvoid_type_node
438 || TREE_TYPE (field) == pvoid_type_node);
439 return field;
440}
441
442static tree
443gfc_conv_descriptor_subfield (tree desc, tree dim, unsigned field_idx)
444{
445 tree tmp = gfc_conv_descriptor_dimension (desc, dim);
446 tree field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (tmp)), field_idx);
447 gcc_assert (field != NULL_TREE);
af232d48
TB
448
449 return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
6d65ddca 450 tmp, field, NULL_TREE);
af232d48
TB
451}
452
568e8e1e 453static tree
6de9cd9a
DN
454gfc_conv_descriptor_stride (tree desc, tree dim)
455{
6d65ddca
RB
456 tree field = gfc_conv_descriptor_subfield (desc, dim, STRIDE_SUBFIELD);
457 gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
458 return field;
6de9cd9a
DN
459}
460
461tree
568e8e1e
PT
462gfc_conv_descriptor_stride_get (tree desc, tree dim)
463{
a3788c44
MM
464 tree type = TREE_TYPE (desc);
465 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
466 if (integer_zerop (dim)
fe4e525c
TB
467 && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
468 ||GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT
c62c6622 469 ||GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT
fe4e525c 470 ||GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT))
a3788c44
MM
471 return gfc_index_one_node;
472
568e8e1e
PT
473 return gfc_conv_descriptor_stride (desc, dim);
474}
475
476void
477gfc_conv_descriptor_stride_set (stmtblock_t *block, tree desc,
478 tree dim, tree value)
479{
480 tree t = gfc_conv_descriptor_stride (desc, dim);
481 gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
482}
483
484static tree
6de9cd9a
DN
485gfc_conv_descriptor_lbound (tree desc, tree dim)
486{
6d65ddca
RB
487 tree field = gfc_conv_descriptor_subfield (desc, dim, LBOUND_SUBFIELD);
488 gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
489 return field;
6de9cd9a
DN
490}
491
492tree
568e8e1e
PT
493gfc_conv_descriptor_lbound_get (tree desc, tree dim)
494{
495 return gfc_conv_descriptor_lbound (desc, dim);
496}
497
498void
499gfc_conv_descriptor_lbound_set (stmtblock_t *block, tree desc,
500 tree dim, tree value)
501{
502 tree t = gfc_conv_descriptor_lbound (desc, dim);
503 gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
504}
505
506static tree
6de9cd9a
DN
507gfc_conv_descriptor_ubound (tree desc, tree dim)
508{
6d65ddca
RB
509 tree field = gfc_conv_descriptor_subfield (desc, dim, UBOUND_SUBFIELD);
510 gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
511 return field;
6de9cd9a
DN
512}
513
568e8e1e
PT
514tree
515gfc_conv_descriptor_ubound_get (tree desc, tree dim)
516{
517 return gfc_conv_descriptor_ubound (desc, dim);
518}
519
520void
521gfc_conv_descriptor_ubound_set (stmtblock_t *block, tree desc,
522 tree dim, tree value)
523{
524 tree t = gfc_conv_descriptor_ubound (desc, dim);
525 gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
526}
6de9cd9a 527
49de9e73 528/* Build a null array descriptor constructor. */
6de9cd9a 529
331c72f3
PB
530tree
531gfc_build_null_descriptor (tree type)
6de9cd9a 532{
6de9cd9a 533 tree field;
331c72f3 534 tree tmp;
6de9cd9a 535
6e45f57b
PB
536 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
537 gcc_assert (DATA_FIELD == 0);
6de9cd9a
DN
538 field = TYPE_FIELDS (type);
539
331c72f3 540 /* Set a NULL data pointer. */
4038c495 541 tmp = build_constructor_single (type, field, null_pointer_node);
6de9cd9a 542 TREE_CONSTANT (tmp) = 1;
331c72f3
PB
543 /* All other fields are ignored. */
544
545 return tmp;
6de9cd9a
DN
546}
547
548
99d821c0
DK
549/* Modify a descriptor such that the lbound of a given dimension is the value
550 specified. This also updates ubound and offset accordingly. */
551
552void
553gfc_conv_shift_descriptor_lbound (stmtblock_t* block, tree desc,
554 int dim, tree new_lbound)
555{
556 tree offs, ubound, lbound, stride;
557 tree diff, offs_diff;
558
559 new_lbound = fold_convert (gfc_array_index_type, new_lbound);
560
561 offs = gfc_conv_descriptor_offset_get (desc);
562 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
563 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
564 stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[dim]);
565
566 /* Get difference (new - old) by which to shift stuff. */
94471a56
TB
567 diff = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
568 new_lbound, lbound);
99d821c0
DK
569
570 /* Shift ubound and offset accordingly. This has to be done before
571 updating the lbound, as they depend on the lbound expression! */
94471a56
TB
572 ubound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
573 ubound, diff);
99d821c0 574 gfc_conv_descriptor_ubound_set (block, desc, gfc_rank_cst[dim], ubound);
94471a56
TB
575 offs_diff = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
576 diff, stride);
577 offs = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
578 offs, offs_diff);
99d821c0
DK
579 gfc_conv_descriptor_offset_set (block, desc, offs);
580
581 /* Finally set lbound to value we want. */
582 gfc_conv_descriptor_lbound_set (block, desc, gfc_rank_cst[dim], new_lbound);
583}
584
585
e53b6e56 586/* Obtain offsets for trans-types.cc(gfc_get_array_descr_info). */
ff3598bc
PT
587
588void
589gfc_get_descriptor_offsets_for_info (const_tree desc_type, tree *data_off,
2297a38e
JJ
590 tree *dtype_off, tree *span_off,
591 tree *dim_off, tree *dim_size,
592 tree *stride_suboff, tree *lower_suboff,
593 tree *upper_suboff)
ff3598bc
PT
594{
595 tree field;
596 tree type;
597
598 type = TYPE_MAIN_VARIANT (desc_type);
f2adfb89 599 field = gfc_advance_chain (TYPE_FIELDS (type), DATA_FIELD);
ff3598bc
PT
600 *data_off = byte_position (field);
601 field = gfc_advance_chain (TYPE_FIELDS (type), DTYPE_FIELD);
602 *dtype_off = byte_position (field);
2297a38e
JJ
603 field = gfc_advance_chain (TYPE_FIELDS (type), SPAN_FIELD);
604 *span_off = byte_position (field);
ff3598bc
PT
605 field = gfc_advance_chain (TYPE_FIELDS (type), DIMENSION_FIELD);
606 *dim_off = byte_position (field);
607 type = TREE_TYPE (TREE_TYPE (field));
608 *dim_size = TYPE_SIZE_UNIT (type);
609 field = gfc_advance_chain (TYPE_FIELDS (type), STRIDE_SUBFIELD);
610 *stride_suboff = byte_position (field);
611 field = gfc_advance_chain (TYPE_FIELDS (type), LBOUND_SUBFIELD);
612 *lower_suboff = byte_position (field);
613 field = gfc_advance_chain (TYPE_FIELDS (type), UBOUND_SUBFIELD);
614 *upper_suboff = byte_position (field);
615}
616
617
6de9cd9a
DN
618/* Cleanup those #defines. */
619
620#undef DATA_FIELD
621#undef OFFSET_FIELD
622#undef DTYPE_FIELD
ff3598bc 623#undef SPAN_FIELD
6de9cd9a 624#undef DIMENSION_FIELD
af232d48 625#undef CAF_TOKEN_FIELD
6de9cd9a
DN
626#undef STRIDE_SUBFIELD
627#undef LBOUND_SUBFIELD
628#undef UBOUND_SUBFIELD
629
630
631/* Mark a SS chain as used. Flags specifies in which loops the SS is used.
632 flags & 1 = Main loop body.
633 flags & 2 = temp copy loop. */
634
635void
636gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
637{
638 for (; ss != gfc_ss_terminator; ss = ss->next)
7a412892 639 ss->info->useflags = flags;
6de9cd9a
DN
640}
641
6de9cd9a
DN
642
643/* Free a gfc_ss chain. */
644
fcba5509 645void
6de9cd9a
DN
646gfc_free_ss_chain (gfc_ss * ss)
647{
648 gfc_ss *next;
649
650 while (ss != gfc_ss_terminator)
651 {
6e45f57b 652 gcc_assert (ss != NULL);
6de9cd9a
DN
653 next = ss->next;
654 gfc_free_ss (ss);
655 ss = next;
656 }
657}
658
659
bcc4d4e0
MM
660static void
661free_ss_info (gfc_ss_info *ss_info)
662{
2960a368
TB
663 int n;
664
c7bf4f1e
MM
665 ss_info->refcount--;
666 if (ss_info->refcount > 0)
667 return;
668
669 gcc_assert (ss_info->refcount == 0);
bcc4d4e0
MM
670
671 switch (ss_info->type)
6de9cd9a
DN
672 {
673 case GFC_SS_SECTION:
2960a368
TB
674 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
675 if (ss_info->data.array.subscript[n])
676 gfc_free_ss_chain (ss_info->data.array.subscript[n]);
6de9cd9a
DN
677 break;
678
679 default:
680 break;
681 }
682
2960a368
TB
683 free (ss_info);
684}
685
686
687/* Free a SS. */
688
689void
690gfc_free_ss (gfc_ss * ss)
691{
692 free_ss_info (ss->info);
cede9502 693 free (ss);
6de9cd9a
DN
694}
695
696
66877276
MM
697/* Creates and initializes an array type gfc_ss struct. */
698
699gfc_ss *
700gfc_get_array_ss (gfc_ss *next, gfc_expr *expr, int dimen, gfc_ss_type type)
701{
702 gfc_ss *ss;
bcc4d4e0 703 gfc_ss_info *ss_info;
66877276
MM
704 int i;
705
bcc4d4e0 706 ss_info = gfc_get_ss_info ();
c7bf4f1e 707 ss_info->refcount++;
bcc4d4e0 708 ss_info->type = type;
f98cfd3c 709 ss_info->expr = expr;
bcc4d4e0 710
66877276 711 ss = gfc_get_ss ();
bcc4d4e0 712 ss->info = ss_info;
66877276 713 ss->next = next;
cb4b9eae
MM
714 ss->dimen = dimen;
715 for (i = 0; i < ss->dimen; i++)
716 ss->dim[i] = i;
66877276
MM
717
718 return ss;
719}
720
721
a1ae4f43
MM
722/* Creates and initializes a temporary type gfc_ss struct. */
723
724gfc_ss *
725gfc_get_temp_ss (tree type, tree string_length, int dimen)
726{
727 gfc_ss *ss;
bcc4d4e0 728 gfc_ss_info *ss_info;
cb4b9eae 729 int i;
a1ae4f43 730
bcc4d4e0 731 ss_info = gfc_get_ss_info ();
c7bf4f1e 732 ss_info->refcount++;
bcc4d4e0 733 ss_info->type = GFC_SS_TEMP;
a0add3be 734 ss_info->string_length = string_length;
961e73ac 735 ss_info->data.temp.type = type;
bcc4d4e0 736
a1ae4f43 737 ss = gfc_get_ss ();
bcc4d4e0 738 ss->info = ss_info;
a1ae4f43 739 ss->next = gfc_ss_terminator;
cb4b9eae
MM
740 ss->dimen = dimen;
741 for (i = 0; i < ss->dimen; i++)
742 ss->dim[i] = i;
a1ae4f43
MM
743
744 return ss;
745}
f04986a9 746
26f77530
MM
747
748/* Creates and initializes a scalar type gfc_ss struct. */
749
750gfc_ss *
751gfc_get_scalar_ss (gfc_ss *next, gfc_expr *expr)
752{
753 gfc_ss *ss;
bcc4d4e0
MM
754 gfc_ss_info *ss_info;
755
756 ss_info = gfc_get_ss_info ();
c7bf4f1e 757 ss_info->refcount++;
bcc4d4e0 758 ss_info->type = GFC_SS_SCALAR;
f98cfd3c 759 ss_info->expr = expr;
26f77530
MM
760
761 ss = gfc_get_ss ();
bcc4d4e0 762 ss->info = ss_info;
26f77530 763 ss->next = next;
26f77530
MM
764
765 return ss;
766}
a1ae4f43
MM
767
768
6de9cd9a
DN
769/* Free all the SS associated with a loop. */
770
771void
772gfc_cleanup_loop (gfc_loopinfo * loop)
773{
4616ef9b 774 gfc_loopinfo *loop_next, **ploop;
6de9cd9a
DN
775 gfc_ss *ss;
776 gfc_ss *next;
777
778 ss = loop->ss;
779 while (ss != gfc_ss_terminator)
780 {
6e45f57b 781 gcc_assert (ss != NULL);
6de9cd9a
DN
782 next = ss->loop_chain;
783 gfc_free_ss (ss);
784 ss = next;
785 }
4616ef9b
MM
786
787 /* Remove reference to self in the parent loop. */
788 if (loop->parent)
789 for (ploop = &loop->parent->nested; *ploop; ploop = &(*ploop)->next)
790 if (*ploop == loop)
791 {
792 *ploop = loop->next;
793 break;
794 }
795
796 /* Free non-freed nested loops. */
797 for (loop = loop->nested; loop; loop = loop_next)
798 {
799 loop_next = loop->next;
800 gfc_cleanup_loop (loop);
801 free (loop);
802 }
6de9cd9a
DN
803}
804
805
4615abe8
MM
806static void
807set_ss_loop (gfc_ss *ss, gfc_loopinfo *loop)
808{
809 int n;
810
811 for (; ss != gfc_ss_terminator; ss = ss->next)
812 {
813 ss->loop = loop;
814
815 if (ss->info->type == GFC_SS_SCALAR
816 || ss->info->type == GFC_SS_REFERENCE
817 || ss->info->type == GFC_SS_TEMP)
818 continue;
819
820 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
821 if (ss->info->data.array.subscript[n] != NULL)
822 set_ss_loop (ss->info->data.array.subscript[n], loop);
823 }
824}
825
826
6de9cd9a
DN
827/* Associate a SS chain with a loop. */
828
829void
830gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
831{
832 gfc_ss *ss;
9d758043 833 gfc_loopinfo *nested_loop;
6de9cd9a
DN
834
835 if (head == gfc_ss_terminator)
836 return;
837
4615abe8
MM
838 set_ss_loop (head, loop);
839
6de9cd9a
DN
840 ss = head;
841 for (; ss && ss != gfc_ss_terminator; ss = ss->next)
842 {
9d758043
MM
843 if (ss->nested_ss)
844 {
845 nested_loop = ss->nested_ss->loop;
846
847 /* More than one ss can belong to the same loop. Hence, we add the
848 loop to the chain only if it is different from the previously
849 added one, to avoid duplicate nested loops. */
850 if (nested_loop != loop->nested)
851 {
4616ef9b
MM
852 gcc_assert (nested_loop->parent == NULL);
853 nested_loop->parent = loop;
854
9d758043
MM
855 gcc_assert (nested_loop->next == NULL);
856 nested_loop->next = loop->nested;
857 loop->nested = nested_loop;
858 }
4616ef9b
MM
859 else
860 gcc_assert (nested_loop->parent == loop);
9d758043
MM
861 }
862
6de9cd9a
DN
863 if (ss->next == gfc_ss_terminator)
864 ss->loop_chain = loop->ss;
865 else
866 ss->loop_chain = ss->next;
867 }
6e45f57b 868 gcc_assert (ss == gfc_ss_terminator);
6de9cd9a
DN
869 loop->ss = head;
870}
871
872
ff3598bc
PT
873/* Returns true if the expression is an array pointer. */
874
875static bool
876is_pointer_array (tree expr)
877{
ff3598bc
PT
878 if (expr == NULL_TREE
879 || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr))
880 || GFC_CLASS_TYPE_P (TREE_TYPE (expr)))
881 return false;
882
883 if (TREE_CODE (expr) == VAR_DECL
884 && GFC_DECL_PTR_ARRAY_P (expr))
885 return true;
886
887 if (TREE_CODE (expr) == PARM_DECL
888 && GFC_DECL_PTR_ARRAY_P (expr))
889 return true;
890
891 if (TREE_CODE (expr) == INDIRECT_REF
892 && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 0)))
893 return true;
894
895 /* The field declaration is marked as an pointer array. */
896 if (TREE_CODE (expr) == COMPONENT_REF
897 && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 1))
898 && !GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
899 return true;
900
901 return false;
902}
903
904
0d78e4aa
PT
905/* If the symbol or expression reference a CFI descriptor, return the
906 pointer to the converted gfc descriptor. If an array reference is
907 present as the last argument, check that it is the one applied to
908 the CFI descriptor in the expression. Note that the CFI object is
909 always the symbol in the expression! */
910
911static bool
912get_CFI_desc (gfc_symbol *sym, gfc_expr *expr,
913 tree *desc, gfc_array_ref *ar)
914{
915 tree tmp;
916
917 if (!is_CFI_desc (sym, expr))
918 return false;
919
920 if (expr && ar)
921 {
922 if (!(expr->ref && expr->ref->type == REF_ARRAY)
923 || (&expr->ref->u.ar != ar))
924 return false;
925 }
926
927 if (sym == NULL)
928 tmp = expr->symtree->n.sym->backend_decl;
929 else
930 tmp = sym->backend_decl;
931
9995ce07 932 if (tmp && DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
0d78e4aa
PT
933 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
934
935 *desc = tmp;
936 return true;
937}
938
939
ff3598bc
PT
940/* Return the span of an array. */
941
f82f425b
PT
942tree
943gfc_get_array_span (tree desc, gfc_expr *expr)
ff3598bc
PT
944{
945 tree tmp;
946
64f96237
TB
947 if (is_pointer_array (desc)
948 || (get_CFI_desc (NULL, expr, &desc, NULL)
949 && (POINTER_TYPE_P (TREE_TYPE (desc))
950 ? GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc)))
951 : GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))))
0d78e4aa
PT
952 {
953 if (POINTER_TYPE_P (TREE_TYPE (desc)))
954 desc = build_fold_indirect_ref_loc (input_location, desc);
955
956 /* This will have the span field set. */
957 tmp = gfc_conv_descriptor_span_get (desc);
958 }
64f96237
TB
959 else if (expr->ts.type == BT_ASSUMED)
960 {
961 if (DECL_LANG_SPECIFIC (desc) && GFC_DECL_SAVED_DESCRIPTOR (desc))
962 desc = GFC_DECL_SAVED_DESCRIPTOR (desc);
963 if (POINTER_TYPE_P (TREE_TYPE (desc)))
964 desc = build_fold_indirect_ref_loc (input_location, desc);
965 tmp = gfc_conv_descriptor_span_get (desc);
966 }
ff3598bc
PT
967 else if (TREE_CODE (desc) == COMPONENT_REF
968 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
969 && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (desc, 0))))
970 {
971 /* The descriptor is a class _data field and so use the vtable
972 size for the receiving span field. */
973 tmp = gfc_get_vptr_from_expr (desc);
974 tmp = gfc_vptr_size_get (tmp);
975 }
976 else if (expr && expr->expr_type == EXPR_VARIABLE
977 && expr->symtree->n.sym->ts.type == BT_CLASS
978 && expr->ref->type == REF_COMPONENT
979 && expr->ref->next->type == REF_ARRAY
980 && expr->ref->next->next == NULL
981 && CLASS_DATA (expr->symtree->n.sym)->attr.dimension)
982 {
983 /* Dummys come in sometimes with the descriptor detached from
984 the class field or declaration. */
985 tmp = gfc_class_vptr_get (expr->symtree->n.sym->backend_decl);
986 tmp = gfc_vptr_size_get (tmp);
987 }
988 else
989 {
990 /* If none of the fancy stuff works, the span is the element
e8db6cd5
PT
991 size of the array. Attempt to deal with unbounded character
992 types if possible. Otherwise, return NULL_TREE. */
ff3598bc 993 tmp = gfc_get_element_type (TREE_TYPE (desc));
d514626e 994 if (tmp && TREE_CODE (tmp) == ARRAY_TYPE && TYPE_STRING_FLAG (tmp))
e8db6cd5 995 {
d514626e
JRFS
996 gcc_assert (expr->ts.type == BT_CHARACTER);
997
998 tmp = gfc_get_character_len_in_bytes (tmp);
999
1000 if (tmp == NULL_TREE || integer_zerop (tmp))
1001 {
1002 tree bs;
1003
1004 tmp = gfc_get_expr_charlen (expr);
1005 tmp = fold_convert (gfc_array_index_type, tmp);
1006 bs = build_int_cst (gfc_array_index_type, expr->ts.kind);
1007 tmp = fold_build2_loc (input_location, MULT_EXPR,
1008 gfc_array_index_type, tmp, bs);
1009 }
1010
1011 tmp = (tmp && !integer_zerop (tmp))
1012 ? (fold_convert (gfc_array_index_type, tmp)) : (NULL_TREE);
e8db6cd5
PT
1013 }
1014 else
1015 tmp = fold_convert (gfc_array_index_type,
1016 size_in_bytes (tmp));
ff3598bc
PT
1017 }
1018 return tmp;
1019}
1020
1021
331c72f3
PB
1022/* Generate an initializer for a static pointer or allocatable array. */
1023
1024void
1025gfc_trans_static_array_pointer (gfc_symbol * sym)
1026{
1027 tree type;
1028
6e45f57b 1029 gcc_assert (TREE_STATIC (sym->backend_decl));
331c72f3
PB
1030 /* Just zero the data member. */
1031 type = TREE_TYPE (sym->backend_decl);
df7df328 1032 DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
331c72f3
PB
1033}
1034
1035
62ab4a54
RS
1036/* If the bounds of SE's loop have not yet been set, see if they can be
1037 determined from array spec AS, which is the array spec of a called
1038 function. MAPPING maps the callee's dummy arguments to the values
1039 that the caller is passing. Add any initialization and finalization
1040 code to SE. */
1041
1042void
1043gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
1044 gfc_se * se, gfc_array_spec * as)
1045{
5125d6d5 1046 int n, dim, total_dim;
62ab4a54 1047 gfc_se tmpse;
5125d6d5 1048 gfc_ss *ss;
62ab4a54
RS
1049 tree lower;
1050 tree upper;
1051 tree tmp;
1052
5125d6d5
MM
1053 total_dim = 0;
1054
1055 if (!as || as->type != AS_EXPLICIT)
1056 return;
1057
1058 for (ss = se->ss; ss; ss = ss->parent)
1059 {
1060 total_dim += ss->loop->dimen;
1061 for (n = 0; n < ss->loop->dimen; n++)
1062 {
1063 /* The bound is known, nothing to do. */
1064 if (ss->loop->to[n] != NULL_TREE)
1065 continue;
1066
1067 dim = ss->dim[n];
1068 gcc_assert (dim < as->rank);
1069 gcc_assert (ss->loop->dimen <= as->rank);
1070
1071 /* Evaluate the lower bound. */
1072 gfc_init_se (&tmpse, NULL);
1073 gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
1074 gfc_add_block_to_block (&se->pre, &tmpse.pre);
1075 gfc_add_block_to_block (&se->post, &tmpse.post);
1076 lower = fold_convert (gfc_array_index_type, tmpse.expr);
1077
1078 /* ...and the upper bound. */
1079 gfc_init_se (&tmpse, NULL);
1080 gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
1081 gfc_add_block_to_block (&se->pre, &tmpse.pre);
1082 gfc_add_block_to_block (&se->post, &tmpse.post);
1083 upper = fold_convert (gfc_array_index_type, tmpse.expr);
1084
1085 /* Set the upper bound of the loop to UPPER - LOWER. */
1086 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1087 gfc_array_index_type, upper, lower);
1088 tmp = gfc_evaluate_now (tmp, &se->pre);
1089 ss->loop->to[n] = tmp;
1090 }
1091 }
1092
1093 gcc_assert (total_dim == as->rank);
62ab4a54
RS
1094}
1095
1096
6de9cd9a 1097/* Generate code to allocate an array temporary, or create a variable to
5b0b7251
EE
1098 hold the data. If size is NULL, zero the descriptor so that the
1099 callee will allocate the array. If DEALLOC is true, also generate code to
1100 free the array afterwards.
ec25720b 1101
12f681a0
DK
1102 If INITIAL is not NULL, it is packed using internal_pack and the result used
1103 as data instead of allocating a fresh, unitialized area of memory.
1104
62ab4a54 1105 Initialization code is added to PRE and finalization code to POST.
ec25720b
RS
1106 DYNAMIC is true if the caller may want to extend the array later
1107 using realloc. This prevents us from putting the array on the stack. */
6de9cd9a
DN
1108
1109static void
62ab4a54 1110gfc_trans_allocate_array_storage (stmtblock_t * pre, stmtblock_t * post,
6d63e468 1111 gfc_array_info * info, tree size, tree nelem,
12f681a0 1112 tree initial, bool dynamic, bool dealloc)
6de9cd9a
DN
1113{
1114 tree tmp;
6de9cd9a 1115 tree desc;
6de9cd9a
DN
1116 bool onstack;
1117
1118 desc = info->descriptor;
4c73896d 1119 info->offset = gfc_index_zero_node;
ec25720b 1120 if (size == NULL_TREE || integer_zerop (size))
6de9cd9a 1121 {
fc90a8f2 1122 /* A callee allocated array. */
62ab4a54 1123 gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
fc90a8f2 1124 onstack = FALSE;
6de9cd9a
DN
1125 }
1126 else
1127 {
fc90a8f2 1128 /* Allocate the temporary. */
12f681a0 1129 onstack = !dynamic && initial == NULL_TREE
203c7ebf 1130 && (flag_stack_arrays
c76f8d52 1131 || gfc_can_put_var_on_stack (size));
fc90a8f2
PB
1132
1133 if (onstack)
1134 {
1135 /* Make a temporary variable to hold the data. */
94471a56
TB
1136 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (nelem),
1137 nelem, gfc_index_one_node);
c76f8d52 1138 tmp = gfc_evaluate_now (tmp, pre);
fc90a8f2
PB
1139 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1140 tmp);
1141 tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
1142 tmp);
1143 tmp = gfc_create_var (tmp, "A");
c76f8d52
MM
1144 /* If we're here only because of -fstack-arrays we have to
1145 emit a DECL_EXPR to make the gimplifier emit alloca calls. */
1146 if (!gfc_can_put_var_on_stack (size))
1147 gfc_add_expr_to_block (pre,
1148 fold_build1_loc (input_location,
1149 DECL_EXPR, TREE_TYPE (tmp),
1150 tmp));
628c189e 1151 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
62ab4a54 1152 gfc_conv_descriptor_data_set (pre, desc, tmp);
fc90a8f2 1153 }
6de9cd9a 1154 else
fc90a8f2 1155 {
12f681a0
DK
1156 /* Allocate memory to hold the data or call internal_pack. */
1157 if (initial == NULL_TREE)
1158 {
1159 tmp = gfc_call_malloc (pre, NULL, size);
1160 tmp = gfc_evaluate_now (tmp, pre);
1161 }
1162 else
1163 {
1164 tree packed;
1165 tree source_data;
1166 tree was_packed;
1167 stmtblock_t do_copying;
1168
1169 tmp = TREE_TYPE (initial); /* Pointer to descriptor. */
1170 gcc_assert (TREE_CODE (tmp) == POINTER_TYPE);
1171 tmp = TREE_TYPE (tmp); /* The descriptor itself. */
1172 tmp = gfc_get_element_type (tmp);
12f681a0
DK
1173 packed = gfc_create_var (build_pointer_type (tmp), "data");
1174
db3927fb
AH
1175 tmp = build_call_expr_loc (input_location,
1176 gfor_fndecl_in_pack, 1, initial);
12f681a0
DK
1177 tmp = fold_convert (TREE_TYPE (packed), tmp);
1178 gfc_add_modify (pre, packed, tmp);
1179
db3927fb
AH
1180 tmp = build_fold_indirect_ref_loc (input_location,
1181 initial);
12f681a0
DK
1182 source_data = gfc_conv_descriptor_data_get (tmp);
1183
1184 /* internal_pack may return source->data without any allocation
1185 or copying if it is already packed. If that's the case, we
1186 need to allocate and copy manually. */
1187
1188 gfc_start_block (&do_copying);
1189 tmp = gfc_call_malloc (&do_copying, NULL, size);
1190 tmp = fold_convert (TREE_TYPE (packed), tmp);
1191 gfc_add_modify (&do_copying, packed, tmp);
1192 tmp = gfc_build_memcpy_call (packed, source_data, size);
1193 gfc_add_expr_to_block (&do_copying, tmp);
1194
94471a56 1195 was_packed = fold_build2_loc (input_location, EQ_EXPR,
63ee5404 1196 logical_type_node, packed,
94471a56 1197 source_data);
12f681a0 1198 tmp = gfc_finish_block (&do_copying);
c2255bc4
AH
1199 tmp = build3_v (COND_EXPR, was_packed, tmp,
1200 build_empty_stmt (input_location));
12f681a0
DK
1201 gfc_add_expr_to_block (pre, tmp);
1202
1203 tmp = fold_convert (pvoid_type_node, packed);
1204 }
1205
62ab4a54 1206 gfc_conv_descriptor_data_set (pre, desc, tmp);
fc90a8f2 1207 }
6de9cd9a 1208 }
4c73896d 1209 info->data = gfc_conv_descriptor_data_get (desc);
6de9cd9a
DN
1210
1211 /* The offset is zero because we create temporaries with a zero
1212 lower bound. */
568e8e1e 1213 gfc_conv_descriptor_offset_set (pre, desc, gfc_index_zero_node);
6de9cd9a 1214
5b0b7251 1215 if (dealloc && !onstack)
6de9cd9a
DN
1216 {
1217 /* Free the temporary. */
4c73896d 1218 tmp = gfc_conv_descriptor_data_get (desc);
107051a5 1219 tmp = gfc_call_free (tmp);
62ab4a54 1220 gfc_add_expr_to_block (post, tmp);
6de9cd9a
DN
1221 }
1222}
1223
1224
d6b3a0d7
MM
1225/* Get the scalarizer array dimension corresponding to actual array dimension
1226 given by ARRAY_DIM.
1227
1228 For example, if SS represents the array ref a(1,:,:,1), it is a
1229 bidimensional scalarizer array, and the result would be 0 for ARRAY_DIM=1,
1230 and 1 for ARRAY_DIM=2.
1231 If SS represents transpose(a(:,1,1,:)), it is again a bidimensional
1232 scalarizer array, and the result would be 1 for ARRAY_DIM=0 and 0 for
1233 ARRAY_DIM=3.
1234 If SS represents sum(a(:,:,:,1), dim=1), it is a 2+1-dimensional scalarizer
1235 array. If called on the inner ss, the result would be respectively 0,1,2 for
1236 ARRAY_DIM=0,1,2. If called on the outer ss, the result would be 0,1
1237 for ARRAY_DIM=1,2. */
99da3840
MM
1238
1239static int
d6b3a0d7 1240get_scalarizer_dim_for_array_dim (gfc_ss *ss, int array_dim)
99da3840 1241{
d6b3a0d7
MM
1242 int array_ref_dim;
1243 int n;
99da3840
MM
1244
1245 array_ref_dim = 0;
99da3840 1246
d6b3a0d7
MM
1247 for (; ss; ss = ss->parent)
1248 for (n = 0; n < ss->dimen; n++)
1249 if (ss->dim[n] < array_dim)
1250 array_ref_dim++;
99da3840
MM
1251
1252 return array_ref_dim;
1253}
1254
1255
d6b3a0d7
MM
1256static gfc_ss *
1257innermost_ss (gfc_ss *ss)
1258{
1259 while (ss->nested_ss != NULL)
1260 ss = ss->nested_ss;
1261
1262 return ss;
1263}
1264
1265
1266
1267/* Get the array reference dimension corresponding to the given loop dimension.
1268 It is different from the true array dimension given by the dim array in
1269 the case of a partial array reference (i.e. a(:,:,1,:) for example)
1270 It is different from the loop dimension in the case of a transposed array.
1271 */
1272
1273static int
1274get_array_ref_dim_for_loop_dim (gfc_ss *ss, int loop_dim)
1275{
1276 return get_scalarizer_dim_for_array_dim (innermost_ss (ss),
1277 ss->dim[loop_dim]);
1278}
1279
1280
ce8dcc91
PT
1281/* Use the information in the ss to obtain the required information about
1282 the type and size of an array temporary, when the lhs in an assignment
1283 is a class expression. */
1284
1285static tree
1286get_class_info_from_ss (stmtblock_t * pre, gfc_ss *ss, tree *eltype)
1287{
1288 gfc_ss *lhs_ss;
1289 gfc_ss *rhs_ss;
1290 tree tmp;
1291 tree tmp2;
1292 tree vptr;
1293 tree rhs_class_expr = NULL_TREE;
1294 tree lhs_class_expr = NULL_TREE;
1295 bool unlimited_rhs = false;
1296 bool unlimited_lhs = false;
1297 bool rhs_function = false;
1298 gfc_symbol *vtab;
1299
1300 /* The second element in the loop chain contains the source for the
1301 temporary; ie. the rhs of the assignment. */
1302 rhs_ss = ss->loop->ss->loop_chain;
1303
1304 if (rhs_ss != gfc_ss_terminator
1305 && rhs_ss->info
1306 && rhs_ss->info->expr
1307 && rhs_ss->info->expr->ts.type == BT_CLASS
1308 && rhs_ss->info->data.array.descriptor)
1309 {
29a52989
PT
1310 if (rhs_ss->info->expr->expr_type != EXPR_VARIABLE)
1311 rhs_class_expr
1312 = gfc_get_class_from_expr (rhs_ss->info->data.array.descriptor);
1313 else
1314 rhs_class_expr = gfc_get_class_from_gfc_expr (rhs_ss->info->expr);
ce8dcc91
PT
1315 unlimited_rhs = UNLIMITED_POLY (rhs_ss->info->expr);
1316 if (rhs_ss->info->expr->expr_type == EXPR_FUNCTION)
1317 rhs_function = true;
1318 }
1319
1320 /* For an assignment the lhs is the next element in the loop chain.
1321 If we have a class rhs, this had better be a class variable
1322 expression! */
1323 lhs_ss = rhs_ss->loop_chain;
1324 if (lhs_ss != gfc_ss_terminator
1325 && lhs_ss->info
1326 && lhs_ss->info->expr
1327 && lhs_ss->info->expr->expr_type ==EXPR_VARIABLE
1328 && lhs_ss->info->expr->ts.type == BT_CLASS)
1329 {
1330 tmp = lhs_ss->info->data.array.descriptor;
1331 unlimited_lhs = UNLIMITED_POLY (rhs_ss->info->expr);
1332 }
1333 else
1334 tmp = NULL_TREE;
1335
1336 /* Get the lhs class expression. */
1337 if (tmp != NULL_TREE && lhs_ss->loop_chain == gfc_ss_terminator)
1338 lhs_class_expr = gfc_get_class_from_expr (tmp);
1339 else
1340 return rhs_class_expr;
1341
1342 gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (lhs_class_expr)));
1343
1344 /* Set the lhs vptr and, if necessary, the _len field. */
1345 if (rhs_class_expr)
1346 {
1347 /* Both lhs and rhs are class expressions. */
1348 tmp = gfc_class_vptr_get (lhs_class_expr);
1349 gfc_add_modify (pre, tmp,
1350 fold_convert (TREE_TYPE (tmp),
1351 gfc_class_vptr_get (rhs_class_expr)));
1352 if (unlimited_lhs)
1353 {
1354 tmp = gfc_class_len_get (lhs_class_expr);
1355 if (unlimited_rhs)
1356 tmp2 = gfc_class_len_get (rhs_class_expr);
1357 else
1358 tmp2 = build_int_cst (TREE_TYPE (tmp), 0);
1359 gfc_add_modify (pre, tmp, tmp2);
1360 }
1361
1362 if (rhs_function)
1363 {
1364 tmp = gfc_class_data_get (rhs_class_expr);
1365 gfc_conv_descriptor_offset_set (pre, tmp, gfc_index_zero_node);
1366 }
1367 }
1368 else
1369 {
1370 /* lhs is class and rhs is intrinsic or derived type. */
1371 *eltype = TREE_TYPE (rhs_ss->info->data.array.descriptor);
1372 *eltype = gfc_get_element_type (*eltype);
1373 vtab = gfc_find_vtab (&rhs_ss->info->expr->ts);
1374 vptr = vtab->backend_decl;
1375 if (vptr == NULL_TREE)
1376 vptr = gfc_get_symbol_decl (vtab);
1377 vptr = gfc_build_addr_expr (NULL_TREE, vptr);
1378 tmp = gfc_class_vptr_get (lhs_class_expr);
1379 gfc_add_modify (pre, tmp,
1380 fold_convert (TREE_TYPE (tmp), vptr));
1381
1382 if (unlimited_lhs)
1383 {
1384 tmp = gfc_class_len_get (lhs_class_expr);
1385 if (rhs_ss->info
1386 && rhs_ss->info->expr
1387 && rhs_ss->info->expr->ts.type == BT_CHARACTER)
1388 tmp2 = build_int_cst (TREE_TYPE (tmp),
1389 rhs_ss->info->expr->ts.kind);
1390 else
1391 tmp2 = build_int_cst (TREE_TYPE (tmp), 0);
1392 gfc_add_modify (pre, tmp, tmp2);
1393 }
1394 }
1395
1396 return rhs_class_expr;
1397}
1398
1399
1400
8e119f1b 1401/* Generate code to create and initialize the descriptor for a temporary
e7dc5b4f 1402 array. This is used for both temporaries needed by the scalarizer, and
8e119f1b
EE
1403 functions returning arrays. Adjusts the loop variables to be
1404 zero-based, and calculates the loop bounds for callee allocated arrays.
1405 Allocate the array unless it's callee allocated (we have a callee
1406 allocated array if 'callee_alloc' is true, or if loop->to[n] is
1407 NULL_TREE for any n). Also fills in the descriptor, data and offset
1408 fields of info if known. Returns the size of the array, or NULL for a
1409 callee allocated array.
ec25720b 1410
866e6d1b
PT
1411 'eltype' == NULL signals that the temporary should be a class object.
1412 The 'initial' expression is used to obtain the size of the dynamic
6bd2c800 1413 type; otherwise the allocation and initialization proceeds as for any
866e6d1b
PT
1414 other expression
1415
12f681a0 1416 PRE, POST, INITIAL, DYNAMIC and DEALLOC are as for
41645793 1417 gfc_trans_allocate_array_storage. */
6de9cd9a
DN
1418
1419tree
41645793 1420gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post, gfc_ss * ss,
12f681a0
DK
1421 tree eltype, tree initial, bool dynamic,
1422 bool dealloc, bool callee_alloc, locus * where)
6de9cd9a 1423{
41645793 1424 gfc_loopinfo *loop;
06cd4e1b 1425 gfc_ss *s;
6d63e468 1426 gfc_array_info *info;
99da3840 1427 tree from[GFC_MAX_DIMENSIONS], to[GFC_MAX_DIMENSIONS];
6de9cd9a
DN
1428 tree type;
1429 tree desc;
1430 tree tmp;
1431 tree size;
1432 tree nelem;
da4340a1
TK
1433 tree cond;
1434 tree or_expr;
0a524296 1435 tree elemsize;
866e6d1b 1436 tree class_expr = NULL_TREE;
99da3840 1437 int n, dim, tmp_dim;
d35335e3 1438 int total_dim = 0;
99da3840 1439
866e6d1b
PT
1440 /* This signals a class array for which we need the size of the
1441 dynamic type. Generate an eltype and then the class expression. */
1442 if (eltype == NULL_TREE && initial)
1443 {
99c25a87
TB
1444 gcc_assert (POINTER_TYPE_P (TREE_TYPE (initial)));
1445 class_expr = build_fold_indirect_ref_loc (input_location, initial);
866e6d1b 1446 /* Obtain the structure (class) expression. */
ce8dcc91 1447 class_expr = gfc_get_class_from_expr (class_expr);
866e6d1b
PT
1448 gcc_assert (class_expr);
1449 }
1450
ce8dcc91
PT
1451 /* Otherwise, some expressions, such as class functions, arising from
1452 dependency checking in assignments come here with class element type.
1453 The descriptor can be obtained from the ss->info and then converted
1454 to the class object. */
1455 if (class_expr == NULL_TREE && GFC_CLASS_TYPE_P (eltype))
1456 class_expr = get_class_info_from_ss (pre, ss, &eltype);
1457
1458 /* If the dynamic type is not available, use the declared type. */
1459 if (eltype && GFC_CLASS_TYPE_P (eltype))
1460 eltype = gfc_get_element_type (TREE_TYPE (TYPE_FIELDS (eltype)));
1461
1462 if (class_expr == NULL_TREE)
1463 elemsize = fold_convert (gfc_array_index_type,
1464 TYPE_SIZE_UNIT (eltype));
1465 else
1466 {
1467 /* Unlimited polymorphic entities are initialised with NULL vptr. They
1468 can be tested for by checking if the len field is present. If so
1469 test the vptr before using the vtable size. */
1470 tmp = gfc_class_vptr_get (class_expr);
1471 tmp = fold_build2_loc (input_location, NE_EXPR,
1472 logical_type_node,
1473 tmp, build_int_cst (TREE_TYPE (tmp), 0));
1474 elemsize = fold_build3_loc (input_location, COND_EXPR,
1475 gfc_array_index_type,
1476 tmp,
1477 gfc_class_vtab_size_get (class_expr),
1478 gfc_index_zero_node);
1479 elemsize = gfc_evaluate_now (elemsize, pre);
1480 elemsize = gfc_resize_class_size_with_len (pre, class_expr, elemsize);
1481 /* Casting the data as a character of the dynamic length ensures that
1482 assignment of elements works when needed. */
1483 eltype = gfc_get_character_type_len (1, elemsize);
1484 }
1485
99da3840
MM
1486 memset (from, 0, sizeof (from));
1487 memset (to, 0, sizeof (to));
6de9cd9a 1488
1838afec 1489 info = &ss->info->data.array;
f44d2277 1490
cb4b9eae 1491 gcc_assert (ss->dimen > 0);
41645793 1492 gcc_assert (ss->loop->dimen == ss->dimen);
bdfd2ff0 1493
73e42eef 1494 if (warn_array_temporaries && where)
48749dbc
MLI
1495 gfc_warning (OPT_Warray_temporaries,
1496 "Creating array temporary at %L", where);
bdfd2ff0 1497
6de9cd9a 1498 /* Set the lower bound to zero. */
06cd4e1b 1499 for (s = ss; s; s = s->parent)
6de9cd9a 1500 {
06cd4e1b 1501 loop = s->loop;
99da3840 1502
06cd4e1b
MM
1503 total_dim += loop->dimen;
1504 for (n = 0; n < loop->dimen; n++)
1505 {
1506 dim = s->dim[n];
1507
1508 /* Callee allocated arrays may not have a known bound yet. */
1509 if (loop->to[n])
1510 loop->to[n] = gfc_evaluate_now (
99da3840
MM
1511 fold_build2_loc (input_location, MINUS_EXPR,
1512 gfc_array_index_type,
1513 loop->to[n], loop->from[n]),
1514 pre);
06cd4e1b
MM
1515 loop->from[n] = gfc_index_zero_node;
1516
1517 /* We have just changed the loop bounds, we must clear the
1518 corresponding specloop, so that delta calculation is not skipped
121c82c9 1519 later in gfc_set_delta. */
06cd4e1b
MM
1520 loop->specloop[n] = NULL;
1521
1522 /* We are constructing the temporary's descriptor based on the loop
1523 dimensions. As the dimensions may be accessed in arbitrary order
1524 (think of transpose) the size taken from the n'th loop may not map
1525 to the n'th dimension of the array. We need to reconstruct loop
1526 infos in the right order before using it to set the descriptor
1527 bounds. */
1528 tmp_dim = get_scalarizer_dim_for_array_dim (ss, dim);
1529 from[tmp_dim] = loop->from[n];
1530 to[tmp_dim] = loop->to[n];
1531
1532 info->delta[dim] = gfc_index_zero_node;
1533 info->start[dim] = gfc_index_zero_node;
1534 info->end[dim] = gfc_index_zero_node;
1535 info->stride[dim] = gfc_index_one_node;
1536 }
6de9cd9a
DN
1537 }
1538
13413760 1539 /* Initialize the descriptor. */
6de9cd9a 1540 type =
d35335e3 1541 gfc_get_array_type_bounds (eltype, total_dim, 0, from, to, 1,
10174ddf 1542 GFC_ARRAY_UNKNOWN, true);
6de9cd9a
DN
1543 desc = gfc_create_var (type, "atmp");
1544 GFC_DECL_PACKED_ARRAY (desc) = 1;
1545
c83e6ebf
RB
1546 /* Emit a DECL_EXPR for the variable sized array type in
1547 GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
1548 sizes works correctly. */
1549 tree arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (type));
1550 if (! TYPE_NAME (arraytype))
1551 TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
1552 NULL_TREE, arraytype);
1553 gfc_add_expr_to_block (pre, build1 (DECL_EXPR,
1554 arraytype, TYPE_NAME (arraytype)));
1555
9a0e09f3
PT
1556 if (class_expr != NULL_TREE)
1557 {
1558 tree class_data;
1559 tree dtype;
1560
1561 /* Create a class temporary. */
1562 tmp = gfc_create_var (TREE_TYPE (class_expr), "ctmp");
1563 gfc_add_modify (pre, tmp, class_expr);
1564
1565 /* Assign the new descriptor to the _data field. This allows the
1566 vptr _copy to be used for scalarized assignment since the class
1567 temporary can be found from the descriptor. */
1568 class_data = gfc_class_data_get (tmp);
1569 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1570 TREE_TYPE (desc), desc);
1571 gfc_add_modify (pre, class_data, tmp);
1572
1573 /* Take the dtype from the class expression. */
1574 dtype = gfc_conv_descriptor_dtype (gfc_class_data_get (class_expr));
1575 tmp = gfc_conv_descriptor_dtype (class_data);
1576 gfc_add_modify (pre, tmp, dtype);
1577
1578 /* Point desc to the class _data field. */
1579 desc = class_data;
1580 }
1581 else
1582 {
1583 /* Fill in the array dtype. */
1584 tmp = gfc_conv_descriptor_dtype (desc);
1585 gfc_add_modify (pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
1586 }
1587
1588 info->descriptor = desc;
1589 size = gfc_index_one_node;
6de9cd9a 1590
7ab92584
SB
1591 /*
1592 Fill in the bounds and stride. This is a packed array, so:
1593
6de9cd9a
DN
1594 size = 1;
1595 for (n = 0; n < rank; n++)
7ab92584
SB
1596 {
1597 stride[n] = size
1598 delta = ubound[n] + 1 - lbound[n];
12f681a0 1599 size = size * delta;
7ab92584
SB
1600 }
1601 size = size * sizeof(element);
1602 */
1603
da4340a1
TK
1604 or_expr = NULL_TREE;
1605
ea5e803f 1606 /* If there is at least one null loop->to[n], it is a callee allocated
45bc572c 1607 array. */
d35335e3
MM
1608 for (n = 0; n < total_dim; n++)
1609 if (to[n] == NULL_TREE)
45bc572c
MM
1610 {
1611 size = NULL_TREE;
1612 break;
1613 }
1614
f28cd38e 1615 if (size == NULL_TREE)
06cd4e1b
MM
1616 for (s = ss; s; s = s->parent)
1617 for (n = 0; n < s->loop->dimen; n++)
12f681a0 1618 {
f6a40ccd 1619 dim = get_scalarizer_dim_for_array_dim (ss, s->dim[n]);
f28cd38e 1620
fc90a8f2
PB
1621 /* For a callee allocated array express the loop bounds in terms
1622 of the descriptor fields. */
94471a56 1623 tmp = fold_build2_loc (input_location,
9157ccb2 1624 MINUS_EXPR, gfc_array_index_type,
2b63684b
MM
1625 gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]),
1626 gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]));
06cd4e1b 1627 s->loop->to[n] = tmp;
12f681a0 1628 }
f28cd38e
MM
1629 else
1630 {
d35335e3 1631 for (n = 0; n < total_dim; n++)
f28cd38e
MM
1632 {
1633 /* Store the stride and bound components in the descriptor. */
1634 gfc_conv_descriptor_stride_set (pre, desc, gfc_rank_cst[n], size);
6de9cd9a 1635
f28cd38e
MM
1636 gfc_conv_descriptor_lbound_set (pre, desc, gfc_rank_cst[n],
1637 gfc_index_zero_node);
6de9cd9a 1638
f28cd38e 1639 gfc_conv_descriptor_ubound_set (pre, desc, gfc_rank_cst[n], to[n]);
6de9cd9a 1640
f28cd38e
MM
1641 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1642 gfc_array_index_type,
1643 to[n], gfc_index_one_node);
6de9cd9a 1644
f28cd38e 1645 /* Check whether the size for this dimension is negative. */
63ee5404 1646 cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
f28cd38e
MM
1647 tmp, gfc_index_zero_node);
1648 cond = gfc_evaluate_now (cond, pre);
da4340a1 1649
f28cd38e
MM
1650 if (n == 0)
1651 or_expr = cond;
1652 else
1653 or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
63ee5404 1654 logical_type_node, or_expr, cond);
da4340a1 1655
f28cd38e
MM
1656 size = fold_build2_loc (input_location, MULT_EXPR,
1657 gfc_array_index_type, size, tmp);
1658 size = gfc_evaluate_now (size, pre);
1659 }
6de9cd9a
DN
1660 }
1661
6de9cd9a 1662 /* Get the size of the array. */
8e119f1b 1663 if (size && !callee_alloc)
da4340a1 1664 {
999ffb1a
FXC
1665 /* If or_expr is true, then the extent in at least one
1666 dimension is zero and the size is set to zero. */
94471a56
TB
1667 size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
1668 or_expr, gfc_index_zero_node, size);
da4340a1 1669
fcac9229 1670 nelem = size;
94471a56 1671 size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
866e6d1b 1672 size, elemsize);
da4340a1 1673 }
8e119f1b 1674 else
da4340a1
TK
1675 {
1676 nelem = size;
1677 size = NULL_TREE;
1678 }
6de9cd9a 1679
0a524296
PT
1680 /* Set the span. */
1681 tmp = fold_convert (gfc_array_index_type, elemsize);
1682 gfc_conv_descriptor_span_set (pre, desc, tmp);
1683
12f681a0
DK
1684 gfc_trans_allocate_array_storage (pre, post, info, size, nelem, initial,
1685 dynamic, dealloc);
6de9cd9a 1686
06cd4e1b
MM
1687 while (ss->parent)
1688 ss = ss->parent;
1689
41645793
MM
1690 if (ss->dimen > ss->loop->temp_dim)
1691 ss->loop->temp_dim = ss->dimen;
6de9cd9a
DN
1692
1693 return size;
1694}
1695
1696
ec25720b
RS
1697/* Return the number of iterations in a loop that starts at START,
1698 ends at END, and has step STEP. */
1699
1700static tree
1701gfc_get_iteration_count (tree start, tree end, tree step)
1702{
1703 tree tmp;
1704 tree type;
1705
1706 type = TREE_TYPE (step);
94471a56
TB
1707 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, end, start);
1708 tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, tmp, step);
1709 tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp,
1710 build_int_cst (type, 1));
1711 tmp = fold_build2_loc (input_location, MAX_EXPR, type, tmp,
1712 build_int_cst (type, 0));
ec25720b
RS
1713 return fold_convert (gfc_array_index_type, tmp);
1714}
1715
1716
1717/* Extend the data in array DESC by EXTRA elements. */
1718
1719static void
1720gfc_grow_array (stmtblock_t * pblock, tree desc, tree extra)
1721{
5039610b 1722 tree arg0, arg1;
ec25720b
RS
1723 tree tmp;
1724 tree size;
1725 tree ubound;
1726
1727 if (integer_zerop (extra))
1728 return;
1729
568e8e1e 1730 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[0]);
ec25720b
RS
1731
1732 /* Add EXTRA to the upper bound. */
94471a56
TB
1733 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1734 ubound, extra);
568e8e1e 1735 gfc_conv_descriptor_ubound_set (pblock, desc, gfc_rank_cst[0], tmp);
ec25720b
RS
1736
1737 /* Get the value of the current data pointer. */
5039610b 1738 arg0 = gfc_conv_descriptor_data_get (desc);
ec25720b
RS
1739
1740 /* Calculate the new array size. */
1741 size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
94471a56
TB
1742 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1743 ubound, gfc_index_one_node);
1744 arg1 = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
1745 fold_convert (size_type_node, tmp),
1746 fold_convert (size_type_node, size));
ec25720b 1747
4376b7cf
FXC
1748 /* Call the realloc() function. */
1749 tmp = gfc_call_realloc (pblock, arg0, arg1);
ec25720b
RS
1750 gfc_conv_descriptor_data_set (pblock, desc, tmp);
1751}
1752
1753
1754/* Return true if the bounds of iterator I can only be determined
1755 at run time. */
1756
1757static inline bool
1758gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
1759{
1760 return (i->start->expr_type != EXPR_CONSTANT
1761 || i->end->expr_type != EXPR_CONSTANT
1762 || i->step->expr_type != EXPR_CONSTANT);
1763}
1764
1765
1766/* Split the size of constructor element EXPR into the sum of two terms,
1767 one of which can be determined at compile time and one of which must
1768 be calculated at run time. Set *SIZE to the former and return true
1769 if the latter might be nonzero. */
1770
1771static bool
1772gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
1773{
1774 if (expr->expr_type == EXPR_ARRAY)
1775 return gfc_get_array_constructor_size (size, expr->value.constructor);
1776 else if (expr->rank > 0)
1777 {
1778 /* Calculate everything at run time. */
1779 mpz_set_ui (*size, 0);
1780 return true;
1781 }
1782 else
1783 {
1784 /* A single element. */
1785 mpz_set_ui (*size, 1);
1786 return false;
1787 }
1788}
1789
1790
1791/* Like gfc_get_array_constructor_element_size, but applied to the whole
1792 of array constructor C. */
1793
1794static bool
b7e75771 1795gfc_get_array_constructor_size (mpz_t * size, gfc_constructor_base base)
ec25720b 1796{
b7e75771 1797 gfc_constructor *c;
ec25720b
RS
1798 gfc_iterator *i;
1799 mpz_t val;
1800 mpz_t len;
1801 bool dynamic;
1802
1803 mpz_set_ui (*size, 0);
1804 mpz_init (len);
1805 mpz_init (val);
1806
1807 dynamic = false;
b7e75771 1808 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
ec25720b
RS
1809 {
1810 i = c->iterator;
1811 if (i && gfc_iterator_has_dynamic_bounds (i))
1812 dynamic = true;
1813 else
1814 {
1815 dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
1816 if (i)
1817 {
1818 /* Multiply the static part of the element size by the
1819 number of iterations. */
1820 mpz_sub (val, i->end->value.integer, i->start->value.integer);
1821 mpz_fdiv_q (val, val, i->step->value.integer);
1822 mpz_add_ui (val, val, 1);
1823 if (mpz_sgn (val) > 0)
1824 mpz_mul (len, len, val);
1825 else
1826 mpz_set_ui (len, 0);
1827 }
1828 mpz_add (*size, *size, len);
1829 }
1830 }
1831 mpz_clear (len);
1832 mpz_clear (val);
1833 return dynamic;
1834}
1835
1836
6de9cd9a
DN
1837/* Make sure offset is a variable. */
1838
1839static void
1840gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
1841 tree * offsetvar)
1842{
1843 /* We should have already created the offset variable. We cannot
13413760 1844 create it here because we may be in an inner scope. */
6e45f57b 1845 gcc_assert (*offsetvar != NULL_TREE);
726a989a 1846 gfc_add_modify (pblock, *offsetvar, *poffset);
6de9cd9a
DN
1847 *poffset = *offsetvar;
1848 TREE_USED (*offsetvar) = 1;
1849}
1850
1851
c03fc95d 1852/* Variables needed for bounds-checking. */
32be9f94 1853static bool first_len;
f04986a9 1854static tree first_len_val;
c03fc95d 1855static bool typespec_chararray_ctor;
40f20186
PB
1856
1857static void
ec25720b 1858gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
40f20186
PB
1859 tree offset, gfc_se * se, gfc_expr * expr)
1860{
1861 tree tmp;
40f20186
PB
1862
1863 gfc_conv_expr (se, expr);
1864
1865 /* Store the value. */
db3927fb
AH
1866 tmp = build_fold_indirect_ref_loc (input_location,
1867 gfc_conv_descriptor_data_get (desc));
1d6b7f39 1868 tmp = gfc_build_array_ref (tmp, offset, NULL);
32be9f94 1869
40f20186
PB
1870 if (expr->ts.type == BT_CHARACTER)
1871 {
691da334
FXC
1872 int i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
1873 tree esize;
1874
1875 esize = size_in_bytes (gfc_get_element_type (TREE_TYPE (desc)));
1876 esize = fold_convert (gfc_charlen_type_node, esize);
94471a56 1877 esize = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
f622221a
JB
1878 TREE_TYPE (esize), esize,
1879 build_int_cst (TREE_TYPE (esize),
691da334
FXC
1880 gfc_character_kinds[i].bit_size / 8));
1881
40f20186
PB
1882 gfc_conv_string_parameter (se);
1883 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
1884 {
1885 /* The temporary is an array of pointers. */
1886 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
726a989a 1887 gfc_add_modify (&se->pre, tmp, se->expr);
40f20186
PB
1888 }
1889 else
1890 {
1891 /* The temporary is an array of string values. */
d393bbd7 1892 tmp = gfc_build_addr_expr (gfc_get_pchar_type (expr->ts.kind), tmp);
40f20186
PB
1893 /* We know the temporary and the value will be the same length,
1894 so can use memcpy. */
d393bbd7
FXC
1895 gfc_trans_string_copy (&se->pre, esize, tmp, expr->ts.kind,
1896 se->string_length, se->expr, expr->ts.kind);
32be9f94 1897 }
d3d3011f 1898 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !typespec_chararray_ctor)
32be9f94
PT
1899 {
1900 if (first_len)
1901 {
726a989a 1902 gfc_add_modify (&se->pre, first_len_val,
85c2c761 1903 fold_convert (TREE_TYPE (first_len_val),
e10e60cb 1904 se->string_length));
32be9f94
PT
1905 first_len = false;
1906 }
1907 else
1908 {
1909 /* Verify that all constructor elements are of the same
1910 length. */
e10e60cb
JB
1911 tree rhs = fold_convert (TREE_TYPE (first_len_val),
1912 se->string_length);
94471a56 1913 tree cond = fold_build2_loc (input_location, NE_EXPR,
63ee5404 1914 logical_type_node, first_len_val,
e10e60cb 1915 rhs);
32be9f94 1916 gfc_trans_runtime_check
0d52899f 1917 (true, false, cond, &se->pre, &expr->where,
32be9f94
PT
1918 "Different CHARACTER lengths (%ld/%ld) in array constructor",
1919 fold_convert (long_integer_type_node, first_len_val),
1920 fold_convert (long_integer_type_node, se->string_length));
1921 }
40f20186
PB
1922 }
1923 }
5233d455
PT
1924 else if (GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
1925 && !GFC_CLASS_TYPE_P (gfc_get_element_type (TREE_TYPE (desc))))
1926 {
1927 /* Assignment of a CLASS array constructor to a derived type array. */
1928 if (expr->expr_type == EXPR_FUNCTION)
1929 se->expr = gfc_evaluate_now (se->expr, pblock);
1930 se->expr = gfc_class_data_get (se->expr);
1931 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
1932 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
1933 gfc_add_modify (&se->pre, tmp, se->expr);
1934 }
40f20186
PB
1935 else
1936 {
1937 /* TODO: Should the frontend already have done this conversion? */
1938 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
726a989a 1939 gfc_add_modify (&se->pre, tmp, se->expr);
40f20186
PB
1940 }
1941
1942 gfc_add_block_to_block (pblock, &se->pre);
1943 gfc_add_block_to_block (pblock, &se->post);
1944}
1945
1946
ec25720b
RS
1947/* Add the contents of an array to the constructor. DYNAMIC is as for
1948 gfc_trans_array_constructor_value. */
6de9cd9a
DN
1949
1950static void
1951gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
1952 tree type ATTRIBUTE_UNUSED,
ec25720b
RS
1953 tree desc, gfc_expr * expr,
1954 tree * poffset, tree * offsetvar,
1955 bool dynamic)
6de9cd9a
DN
1956{
1957 gfc_se se;
1958 gfc_ss *ss;
1959 gfc_loopinfo loop;
1960 stmtblock_t body;
1961 tree tmp;
ec25720b
RS
1962 tree size;
1963 int n;
6de9cd9a
DN
1964
1965 /* We need this to be a variable so we can increment it. */
1966 gfc_put_offset_into_var (pblock, poffset, offsetvar);
1967
1968 gfc_init_se (&se, NULL);
1969
1970 /* Walk the array expression. */
1971 ss = gfc_walk_expr (expr);
6e45f57b 1972 gcc_assert (ss != gfc_ss_terminator);
6de9cd9a
DN
1973
1974 /* Initialize the scalarizer. */
1975 gfc_init_loopinfo (&loop);
1976 gfc_add_ss_to_loop (&loop, ss);
1977
1978 /* Initialize the loop. */
1979 gfc_conv_ss_startstride (&loop);
bdfd2ff0 1980 gfc_conv_loop_setup (&loop, &expr->where);
6de9cd9a 1981
ec25720b
RS
1982 /* Make sure the constructed array has room for the new data. */
1983 if (dynamic)
1984 {
1985 /* Set SIZE to the total number of elements in the subarray. */
1986 size = gfc_index_one_node;
1987 for (n = 0; n < loop.dimen; n++)
1988 {
1989 tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
1990 gfc_index_one_node);
94471a56
TB
1991 size = fold_build2_loc (input_location, MULT_EXPR,
1992 gfc_array_index_type, size, tmp);
ec25720b
RS
1993 }
1994
1995 /* Grow the constructed array by SIZE elements. */
1996 gfc_grow_array (&loop.pre, desc, size);
1997 }
1998
6de9cd9a
DN
1999 /* Make the loop body. */
2000 gfc_mark_ss_chain_used (ss, 1);
2001 gfc_start_scalarized_body (&loop, &body);
2002 gfc_copy_loopinfo_to_se (&se, &loop);
2003 se.ss = ss;
2004
ec25720b 2005 gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
6e45f57b 2006 gcc_assert (se.ss == gfc_ss_terminator);
6de9cd9a
DN
2007
2008 /* Increment the offset. */
94471a56
TB
2009 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2010 *poffset, gfc_index_one_node);
726a989a 2011 gfc_add_modify (&body, *poffset, tmp);
6de9cd9a
DN
2012
2013 /* Finish the loop. */
6de9cd9a
DN
2014 gfc_trans_scalarizing_loops (&loop, &body);
2015 gfc_add_block_to_block (&loop.pre, &loop.post);
2016 tmp = gfc_finish_block (&loop.pre);
2017 gfc_add_expr_to_block (pblock, tmp);
2018
2019 gfc_cleanup_loop (&loop);
2020}
2021
2022
ec25720b
RS
2023/* Assign the values to the elements of an array constructor. DYNAMIC
2024 is true if descriptor DESC only contains enough data for the static
2025 size calculated by gfc_get_array_constructor_size. When true, memory
2026 for the dynamic parts must be allocated using realloc. */
6de9cd9a
DN
2027
2028static void
2029gfc_trans_array_constructor_value (stmtblock_t * pblock, tree type,
b7e75771 2030 tree desc, gfc_constructor_base base,
ec25720b
RS
2031 tree * poffset, tree * offsetvar,
2032 bool dynamic)
6de9cd9a
DN
2033{
2034 tree tmp;
b63b1f86
MM
2035 tree start = NULL_TREE;
2036 tree end = NULL_TREE;
2037 tree step = NULL_TREE;
6de9cd9a 2038 stmtblock_t body;
6de9cd9a 2039 gfc_se se;
ec25720b 2040 mpz_t size;
b7e75771 2041 gfc_constructor *c;
6de9cd9a 2042
beb64b4a
DF
2043 tree shadow_loopvar = NULL_TREE;
2044 gfc_saved_var saved_loopvar;
2045
ec25720b 2046 mpz_init (size);
b7e75771 2047 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
6de9cd9a
DN
2048 {
2049 /* If this is an iterator or an array, the offset must be a variable. */
2050 if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
2051 gfc_put_offset_into_var (pblock, poffset, offsetvar);
2052
beb64b4a
DF
2053 /* Shadowing the iterator avoids changing its value and saves us from
2054 keeping track of it. Further, it makes sure that there's always a
2055 backend-decl for the symbol, even if there wasn't one before,
2056 e.g. in the case of an iterator that appears in a specification
2057 expression in an interface mapping. */
2058 if (c->iterator)
2059 {
b63b1f86
MM
2060 gfc_symbol *sym;
2061 tree type;
2062
2063 /* Evaluate loop bounds before substituting the loop variable
2064 in case they depend on it. Such a case is invalid, but it is
2065 not more expensive to do the right thing here.
2066 See PR 44354. */
2067 gfc_init_se (&se, NULL);
2068 gfc_conv_expr_val (&se, c->iterator->start);
2069 gfc_add_block_to_block (pblock, &se.pre);
2070 start = gfc_evaluate_now (se.expr, pblock);
2071
2072 gfc_init_se (&se, NULL);
2073 gfc_conv_expr_val (&se, c->iterator->end);
2074 gfc_add_block_to_block (pblock, &se.pre);
2075 end = gfc_evaluate_now (se.expr, pblock);
2076
2077 gfc_init_se (&se, NULL);
2078 gfc_conv_expr_val (&se, c->iterator->step);
2079 gfc_add_block_to_block (pblock, &se.pre);
2080 step = gfc_evaluate_now (se.expr, pblock);
2081
2082 sym = c->iterator->var->symtree->n.sym;
2083 type = gfc_typenode_for_spec (&sym->ts);
beb64b4a
DF
2084
2085 shadow_loopvar = gfc_create_var (type, "shadow_loopvar");
2086 gfc_shadow_sym (sym, shadow_loopvar, &saved_loopvar);
2087 }
2088
6de9cd9a
DN
2089 gfc_start_block (&body);
2090
2091 if (c->expr->expr_type == EXPR_ARRAY)
2092 {
2093 /* Array constructors can be nested. */
ec25720b 2094 gfc_trans_array_constructor_value (&body, type, desc,
6de9cd9a 2095 c->expr->value.constructor,
ec25720b 2096 poffset, offsetvar, dynamic);
6de9cd9a
DN
2097 }
2098 else if (c->expr->rank > 0)
2099 {
ec25720b
RS
2100 gfc_trans_array_constructor_subarray (&body, type, desc, c->expr,
2101 poffset, offsetvar, dynamic);
6de9cd9a
DN
2102 }
2103 else
2104 {
2105 /* This code really upsets the gimplifier so don't bother for now. */
2106 gfc_constructor *p;
2107 HOST_WIDE_INT n;
2108 HOST_WIDE_INT size;
2109
2110 p = c;
2111 n = 0;
2112 while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
2113 {
b7e75771 2114 p = gfc_constructor_next (p);
6de9cd9a
DN
2115 n++;
2116 }
2117 if (n < 4)
2118 {
2119 /* Scalar values. */
2120 gfc_init_se (&se, NULL);
ec25720b
RS
2121 gfc_trans_array_ctor_element (&body, desc, *poffset,
2122 &se, c->expr);
6de9cd9a 2123
94471a56
TB
2124 *poffset = fold_build2_loc (input_location, PLUS_EXPR,
2125 gfc_array_index_type,
2126 *poffset, gfc_index_one_node);
6de9cd9a
DN
2127 }
2128 else
2129 {
2130 /* Collect multiple scalar constants into a constructor. */
9771b263 2131 vec<constructor_elt, va_gc> *v = NULL;
6de9cd9a
DN
2132 tree init;
2133 tree bound;
2134 tree tmptype;
81f5094d 2135 HOST_WIDE_INT idx = 0;
6de9cd9a
DN
2136
2137 p = c;
6de9cd9a
DN
2138 /* Count the number of consecutive scalar constants. */
2139 while (p && !(p->iterator
2140 || p->expr->expr_type != EXPR_CONSTANT))
2141 {
2142 gfc_init_se (&se, NULL);
2143 gfc_conv_constant (&se, p->expr);
d393bbd7 2144
110ea21a
PT
2145 if (c->expr->ts.type != BT_CHARACTER)
2146 se.expr = fold_convert (type, se.expr);
d393bbd7
FXC
2147 /* For constant character array constructors we build
2148 an array of pointers. */
110ea21a 2149 else if (POINTER_TYPE_P (type))
d393bbd7
FXC
2150 se.expr = gfc_build_addr_expr
2151 (gfc_get_pchar_type (p->expr->ts.kind),
2152 se.expr);
2153
8748ad99
NF
2154 CONSTRUCTOR_APPEND_ELT (v,
2155 build_int_cst (gfc_array_index_type,
2156 idx++),
2157 se.expr);
6de9cd9a 2158 c = p;
b7e75771 2159 p = gfc_constructor_next (p);
6de9cd9a
DN
2160 }
2161
df09d1d5 2162 bound = size_int (n - 1);
6de9cd9a
DN
2163 /* Create an array type to hold them. */
2164 tmptype = build_range_type (gfc_array_index_type,
7ab92584 2165 gfc_index_zero_node, bound);
6de9cd9a
DN
2166 tmptype = build_array_type (type, tmptype);
2167
8748ad99 2168 init = build_constructor (tmptype, v);
6de9cd9a 2169 TREE_CONSTANT (init) = 1;
6de9cd9a
DN
2170 TREE_STATIC (init) = 1;
2171 /* Create a static variable to hold the data. */
2172 tmp = gfc_create_var (tmptype, "data");
2173 TREE_STATIC (tmp) = 1;
2174 TREE_CONSTANT (tmp) = 1;
0f0707d1 2175 TREE_READONLY (tmp) = 1;
6de9cd9a
DN
2176 DECL_INITIAL (tmp) = init;
2177 init = tmp;
2178
2179 /* Use BUILTIN_MEMCPY to assign the values. */
ec25720b 2180 tmp = gfc_conv_descriptor_data_get (desc);
db3927fb
AH
2181 tmp = build_fold_indirect_ref_loc (input_location,
2182 tmp);
1d6b7f39 2183 tmp = gfc_build_array_ref (tmp, *poffset, NULL);
628c189e
RG
2184 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
2185 init = gfc_build_addr_expr (NULL_TREE, init);
6de9cd9a
DN
2186
2187 size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
df09d1d5 2188 bound = build_int_cst (size_type_node, n * size);
db3927fb 2189 tmp = build_call_expr_loc (input_location,
e79983f4
MM
2190 builtin_decl_explicit (BUILT_IN_MEMCPY),
2191 3, tmp, init, bound);
6de9cd9a
DN
2192 gfc_add_expr_to_block (&body, tmp);
2193
94471a56
TB
2194 *poffset = fold_build2_loc (input_location, PLUS_EXPR,
2195 gfc_array_index_type, *poffset,
ac816b02 2196 build_int_cst (gfc_array_index_type, n));
6de9cd9a
DN
2197 }
2198 if (!INTEGER_CST_P (*poffset))
2199 {
726a989a 2200 gfc_add_modify (&body, *offsetvar, *poffset);
6de9cd9a
DN
2201 *poffset = *offsetvar;
2202 }
2203 }
2204
63346ddb 2205 /* The frontend should already have done any expansions
86403f0f
TS
2206 at compile-time. */
2207 if (!c->iterator)
6de9cd9a 2208 {
86403f0f
TS
2209 /* Pass the code as is. */
2210 tmp = gfc_finish_block (&body);
2211 gfc_add_expr_to_block (pblock, tmp);
2212 }
2213 else
2214 {
2215 /* Build the implied do-loop. */
beb64b4a 2216 stmtblock_t implied_do_block;
86403f0f 2217 tree cond;
6de9cd9a 2218 tree exit_label;
86403f0f 2219 tree loopbody;
ec25720b 2220 tree tmp2;
6de9cd9a
DN
2221
2222 loopbody = gfc_finish_block (&body);
2223
beb64b4a
DF
2224 /* Create a new block that holds the implied-do loop. A temporary
2225 loop-variable is used. */
2226 gfc_start_block(&implied_do_block);
bfa7a1e9 2227
13413760 2228 /* Initialize the loop. */
b63b1f86 2229 gfc_add_modify (&implied_do_block, shadow_loopvar, start);
6de9cd9a 2230
ec25720b
RS
2231 /* If this array expands dynamically, and the number of iterations
2232 is not constant, we won't have allocated space for the static
2233 part of C->EXPR's size. Do that now. */
2234 if (dynamic && gfc_iterator_has_dynamic_bounds (c->iterator))
2235 {
2236 /* Get the number of iterations. */
beb64b4a 2237 tmp = gfc_get_iteration_count (shadow_loopvar, end, step);
ec25720b
RS
2238
2239 /* Get the static part of C->EXPR's size. */
2240 gfc_get_array_constructor_element_size (&size, c->expr);
2241 tmp2 = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
2242
2243 /* Grow the array by TMP * TMP2 elements. */
94471a56
TB
2244 tmp = fold_build2_loc (input_location, MULT_EXPR,
2245 gfc_array_index_type, tmp, tmp2);
beb64b4a 2246 gfc_grow_array (&implied_do_block, desc, tmp);
ec25720b
RS
2247 }
2248
6de9cd9a
DN
2249 /* Generate the loop body. */
2250 exit_label = gfc_build_label_decl (NULL_TREE);
2251 gfc_start_block (&body);
2252
86403f0f
TS
2253 /* Generate the exit condition. Depending on the sign of
2254 the step variable we have to generate the correct
2255 comparison. */
63ee5404 2256 tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
94471a56
TB
2257 step, build_int_cst (TREE_TYPE (step), 0));
2258 cond = fold_build3_loc (input_location, COND_EXPR,
63ee5404 2259 logical_type_node, tmp,
94471a56 2260 fold_build2_loc (input_location, GT_EXPR,
63ee5404 2261 logical_type_node, shadow_loopvar, end),
94471a56 2262 fold_build2_loc (input_location, LT_EXPR,
63ee5404 2263 logical_type_node, shadow_loopvar, end));
6de9cd9a
DN
2264 tmp = build1_v (GOTO_EXPR, exit_label);
2265 TREE_USED (exit_label) = 1;
c2255bc4
AH
2266 tmp = build3_v (COND_EXPR, cond, tmp,
2267 build_empty_stmt (input_location));
6de9cd9a
DN
2268 gfc_add_expr_to_block (&body, tmp);
2269
2270 /* The main loop body. */
2271 gfc_add_expr_to_block (&body, loopbody);
2272
86403f0f 2273 /* Increase loop variable by step. */
94471a56
TB
2274 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2275 TREE_TYPE (shadow_loopvar), shadow_loopvar,
2276 step);
beb64b4a 2277 gfc_add_modify (&body, shadow_loopvar, tmp);
6de9cd9a
DN
2278
2279 /* Finish the loop. */
2280 tmp = gfc_finish_block (&body);
923ab88c 2281 tmp = build1_v (LOOP_EXPR, tmp);
beb64b4a 2282 gfc_add_expr_to_block (&implied_do_block, tmp);
6de9cd9a
DN
2283
2284 /* Add the exit label. */
2285 tmp = build1_v (LABEL_EXPR, exit_label);
beb64b4a
DF
2286 gfc_add_expr_to_block (&implied_do_block, tmp);
2287
eea58adb 2288 /* Finish the implied-do loop. */
beb64b4a
DF
2289 tmp = gfc_finish_block(&implied_do_block);
2290 gfc_add_expr_to_block(pblock, tmp);
bfa7a1e9 2291
beb64b4a 2292 gfc_restore_sym (c->iterator->var->symtree->n.sym, &saved_loopvar);
6de9cd9a 2293 }
6de9cd9a 2294 }
ec25720b 2295 mpz_clear (size);
6de9cd9a
DN
2296}
2297
2298
d751beac
LK
2299/* The array constructor code can create a string length with an operand
2300 in the form of a temporary variable. This variable will retain its
2301 context (current_function_decl). If we store this length tree in a
2302 gfc_charlen structure which is shared by a variable in another
2303 context, the resulting gfc_charlen structure with a variable in a
2304 different context, we could trip the assertion in expand_expr_real_1
2305 when it sees that a variable has been created in one context and
2306 referenced in another.
2307
2308 If this might be the case, we create a new gfc_charlen structure and
2309 link it into the current namespace. */
2310
2311static void
2312store_backend_decl (gfc_charlen **clp, tree len, bool force_new_cl)
2313{
2314 if (force_new_cl)
2315 {
2316 gfc_charlen *new_cl = gfc_new_charlen (gfc_current_ns, *clp);
2317 *clp = new_cl;
2318 }
2319 (*clp)->backend_decl = len;
2320}
2321
eea58adb 2322/* A catch-all to obtain the string length for anything that is not
6c1b5781
PT
2323 a substring of non-constant length, a constant, array or variable. */
2324
2325static void
2326get_array_ctor_all_strlen (stmtblock_t *block, gfc_expr *e, tree *len)
2327{
2328 gfc_se se;
6c1b5781
PT
2329
2330 /* Don't bother if we already know the length is a constant. */
2331 if (*len && INTEGER_CST_P (*len))
2332 return;
2333
2334 if (!e->ref && e->ts.u.cl && e->ts.u.cl->length
2335 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2336 {
2337 /* This is easy. */
2338 gfc_conv_const_charlen (e->ts.u.cl);
2339 *len = e->ts.u.cl->backend_decl;
2340 }
2341 else
2342 {
2343 /* Otherwise, be brutal even if inefficient. */
6c1b5781
PT
2344 gfc_init_se (&se, NULL);
2345
2346 /* No function call, in case of side effects. */
2347 se.no_function_call = 1;
2960a368 2348 if (e->rank == 0)
6c1b5781
PT
2349 gfc_conv_expr (&se, e);
2350 else
2960a368 2351 gfc_conv_expr_descriptor (&se, e);
6c1b5781
PT
2352
2353 /* Fix the value. */
2354 *len = gfc_evaluate_now (se.string_length, &se.pre);
2355
2356 gfc_add_block_to_block (block, &se.pre);
2357 gfc_add_block_to_block (block, &se.post);
2358
d751beac 2359 store_backend_decl (&e->ts.u.cl, *len, true);
6c1b5781
PT
2360 }
2361}
2362
2363
40f20186
PB
2364/* Figure out the string length of a variable reference expression.
2365 Used by get_array_ctor_strlen. */
2366
2367static void
6c1b5781 2368get_array_ctor_var_strlen (stmtblock_t *block, gfc_expr * expr, tree * len)
40f20186
PB
2369{
2370 gfc_ref *ref;
2371 gfc_typespec *ts;
1855915a 2372 mpz_t char_len;
feae0af8 2373 gfc_se se;
40f20186
PB
2374
2375 /* Don't bother if we already know the length is a constant. */
2376 if (*len && INTEGER_CST_P (*len))
2377 return;
2378
2379 ts = &expr->symtree->n.sym->ts;
2380 for (ref = expr->ref; ref; ref = ref->next)
2381 {
2382 switch (ref->type)
2383 {
2384 case REF_ARRAY:
df7df328 2385 /* Array references don't change the string length. */
d5f48c7c
PT
2386 if (ts->deferred)
2387 get_array_ctor_all_strlen (block, expr, len);
40f20186
PB
2388 break;
2389
0e3e65bc 2390 case REF_COMPONENT:
f7b529fa 2391 /* Use the length of the component. */
40f20186
PB
2392 ts = &ref->u.c.component->ts;
2393 break;
2394
1855915a 2395 case REF_SUBSTRING:
d5f48c7c
PT
2396 if (ref->u.ss.end == NULL
2397 || ref->u.ss.start->expr_type != EXPR_CONSTANT
08ddab21 2398 || ref->u.ss.end->expr_type != EXPR_CONSTANT)
6c1b5781
PT
2399 {
2400 /* Note that this might evaluate expr. */
2401 get_array_ctor_all_strlen (block, expr, len);
2402 return;
2403 }
1855915a
PT
2404 mpz_init_set_ui (char_len, 1);
2405 mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
2406 mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
f622221a 2407 *len = gfc_conv_mpz_to_tree_type (char_len, gfc_charlen_type_node);
1855915a
PT
2408 mpz_clear (char_len);
2409 return;
2410
a5fbc2f3
PT
2411 case REF_INQUIRY:
2412 break;
2413
40f20186 2414 default:
6c1b5781 2415 gcc_unreachable ();
40f20186
PB
2416 }
2417 }
2418
feae0af8
PT
2419 /* A last ditch attempt that is sometimes needed for deferred characters. */
2420 if (!ts->u.cl->backend_decl)
2421 {
2422 gfc_init_se (&se, NULL);
2423 if (expr->rank)
2424 gfc_conv_expr_descriptor (&se, expr);
2425 else
2426 gfc_conv_expr (&se, expr);
2427 gcc_assert (se.string_length != NULL_TREE);
2428 gfc_add_block_to_block (block, &se.pre);
2429 ts->u.cl->backend_decl = se.string_length;
2430 }
2431
bc21d315 2432 *len = ts->u.cl->backend_decl;
40f20186
PB
2433}
2434
2435
2436/* Figure out the string length of a character array constructor.
88fec49f
DK
2437 If len is NULL, don't calculate the length; this happens for recursive calls
2438 when a sub-array-constructor is an element but not at the first position,
2439 so when we're not interested in the length.
40f20186
PB
2440 Returns TRUE if all elements are character constants. */
2441
636da744 2442bool
b7e75771 2443get_array_ctor_strlen (stmtblock_t *block, gfc_constructor_base base, tree * len)
40f20186 2444{
b7e75771 2445 gfc_constructor *c;
40f20186 2446 bool is_const;
b7e75771 2447
40f20186 2448 is_const = TRUE;
58fbb917 2449
b7e75771 2450 if (gfc_constructor_first (base) == NULL)
58fbb917 2451 {
88fec49f
DK
2452 if (len)
2453 *len = build_int_cstu (gfc_charlen_type_node, 0);
58fbb917
PT
2454 return is_const;
2455 }
2456
88fec49f
DK
2457 /* Loop over all constructor elements to find out is_const, but in len we
2458 want to store the length of the first, not the last, element. We can
2459 of course exit the loop as soon as is_const is found to be false. */
b7e75771
JD
2460 for (c = gfc_constructor_first (base);
2461 c && is_const; c = gfc_constructor_next (c))
40f20186
PB
2462 {
2463 switch (c->expr->expr_type)
2464 {
2465 case EXPR_CONSTANT:
88fec49f 2466 if (len && !(*len && INTEGER_CST_P (*len)))
d7177ab2 2467 *len = build_int_cstu (gfc_charlen_type_node,
40f20186
PB
2468 c->expr->value.character.length);
2469 break;
2470
2471 case EXPR_ARRAY:
0ee8e250 2472 if (!get_array_ctor_strlen (block, c->expr->value.constructor, len))
01201992 2473 is_const = false;
40f20186
PB
2474 break;
2475
2476 case EXPR_VARIABLE:
2477 is_const = false;
88fec49f 2478 if (len)
6c1b5781 2479 get_array_ctor_var_strlen (block, c->expr, len);
40f20186
PB
2480 break;
2481
2482 default:
01201992 2483 is_const = false;
88fec49f
DK
2484 if (len)
2485 get_array_ctor_all_strlen (block, c->expr, len);
40f20186
PB
2486 break;
2487 }
88fec49f
DK
2488
2489 /* After the first iteration, we don't want the length modified. */
2490 len = NULL;
40f20186
PB
2491 }
2492
2493 return is_const;
2494}
2495
62511fb1
RS
2496/* Check whether the array constructor C consists entirely of constant
2497 elements, and if so returns the number of those elements, otherwise
2498 return zero. Note, an empty or NULL array constructor returns zero. */
2499
b01e2f88 2500unsigned HOST_WIDE_INT
b7e75771 2501gfc_constant_array_constructor_p (gfc_constructor_base base)
62511fb1
RS
2502{
2503 unsigned HOST_WIDE_INT nelem = 0;
2504
b7e75771 2505 gfc_constructor *c = gfc_constructor_first (base);
62511fb1
RS
2506 while (c)
2507 {
2508 if (c->iterator
2509 || c->expr->rank > 0
2510 || c->expr->expr_type != EXPR_CONSTANT)
2511 return 0;
b7e75771 2512 c = gfc_constructor_next (c);
62511fb1
RS
2513 nelem++;
2514 }
2515 return nelem;
2516}
2517
2518
2519/* Given EXPR, the constant array constructor specified by an EXPR_ARRAY,
2520 and the tree type of it's elements, TYPE, return a static constant
2521 variable that is compile-time initialized. */
2522
b01e2f88 2523tree
62511fb1
RS
2524gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
2525{
8748ad99 2526 tree tmptype, init, tmp;
62511fb1
RS
2527 HOST_WIDE_INT nelem;
2528 gfc_constructor *c;
2529 gfc_array_spec as;
2530 gfc_se se;
61a04b5b 2531 int i;
9771b263 2532 vec<constructor_elt, va_gc> *v = NULL;
62511fb1
RS
2533
2534 /* First traverse the constructor list, converting the constants
2535 to tree to build an initializer. */
2536 nelem = 0;
b7e75771 2537 c = gfc_constructor_first (expr->value.constructor);
62511fb1
RS
2538 while (c)
2539 {
2540 gfc_init_se (&se, NULL);
2541 gfc_conv_constant (&se, c->expr);
110ea21a
PT
2542 if (c->expr->ts.type != BT_CHARACTER)
2543 se.expr = fold_convert (type, se.expr);
2544 else if (POINTER_TYPE_P (type))
d393bbd7
FXC
2545 se.expr = gfc_build_addr_expr (gfc_get_pchar_type (c->expr->ts.kind),
2546 se.expr);
8748ad99
NF
2547 CONSTRUCTOR_APPEND_ELT (v, build_int_cst (gfc_array_index_type, nelem),
2548 se.expr);
b7e75771 2549 c = gfc_constructor_next (c);
62511fb1
RS
2550 nelem++;
2551 }
2552
65de695f 2553 /* Next determine the tree type for the array. We use the gfortran
62511fb1
RS
2554 front-end's gfc_get_nodesc_array_type in order to create a suitable
2555 GFC_ARRAY_TYPE_P that may be used by the scalarizer. */
2556
2557 memset (&as, 0, sizeof (gfc_array_spec));
2558
61a04b5b 2559 as.rank = expr->rank;
62511fb1 2560 as.type = AS_EXPLICIT;
61a04b5b
RS
2561 if (!expr->shape)
2562 {
b7e75771
JD
2563 as.lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2564 as.upper[0] = gfc_get_int_expr (gfc_default_integer_kind,
2565 NULL, nelem - 1);
61a04b5b
RS
2566 }
2567 else
2568 for (i = 0; i < expr->rank; i++)
2569 {
2570 int tmp = (int) mpz_get_si (expr->shape[i]);
b7e75771
JD
2571 as.lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2572 as.upper[i] = gfc_get_int_expr (gfc_default_integer_kind,
2573 NULL, tmp - 1);
61a04b5b
RS
2574 }
2575
10174ddf 2576 tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
62511fb1 2577
1b4544b7
MM
2578 /* as is not needed anymore. */
2579 for (i = 0; i < as.rank + as.corank; i++)
2580 {
2581 gfc_free_expr (as.lower[i]);
2582 gfc_free_expr (as.upper[i]);
2583 }
2584
8748ad99 2585 init = build_constructor (tmptype, v);
62511fb1
RS
2586
2587 TREE_CONSTANT (init) = 1;
62511fb1
RS
2588 TREE_STATIC (init) = 1;
2589
059345ce
BS
2590 tmp = build_decl (input_location, VAR_DECL, create_tmp_var_name ("A"),
2591 tmptype);
2592 DECL_ARTIFICIAL (tmp) = 1;
2593 DECL_IGNORED_P (tmp) = 1;
62511fb1
RS
2594 TREE_STATIC (tmp) = 1;
2595 TREE_CONSTANT (tmp) = 1;
62511fb1
RS
2596 TREE_READONLY (tmp) = 1;
2597 DECL_INITIAL (tmp) = init;
059345ce 2598 pushdecl (tmp);
62511fb1
RS
2599
2600 return tmp;
2601}
2602
2603
2604/* Translate a constant EXPR_ARRAY array constructor for the scalarizer.
2605 This mostly initializes the scalarizer state info structure with the
2606 appropriate values to directly use the array created by the function
2607 gfc_build_constant_array_constructor. */
2608
2609static void
a13d9afe 2610trans_constant_array_constructor (gfc_ss * ss, tree type)
62511fb1 2611{
6d63e468 2612 gfc_array_info *info;
62511fb1 2613 tree tmp;
61a04b5b 2614 int i;
62511fb1 2615
f98cfd3c 2616 tmp = gfc_build_constant_array_constructor (ss->info->expr, type);
62511fb1 2617
1838afec 2618 info = &ss->info->data.array;
62511fb1
RS
2619
2620 info->descriptor = tmp;
628c189e 2621 info->data = gfc_build_addr_expr (NULL_TREE, tmp);
45bc572c 2622 info->offset = gfc_index_zero_node;
62511fb1 2623
cb4b9eae 2624 for (i = 0; i < ss->dimen; i++)
61a04b5b
RS
2625 {
2626 info->delta[i] = gfc_index_zero_node;
2627 info->start[i] = gfc_index_zero_node;
2628 info->end[i] = gfc_index_zero_node;
2629 info->stride[i] = gfc_index_one_node;
61a04b5b 2630 }
62511fb1
RS
2631}
2632
fa168d9f 2633
b2f82aaa
MM
2634static int
2635get_rank (gfc_loopinfo *loop)
2636{
2637 int rank;
2638
2639 rank = 0;
2640 for (; loop; loop = loop->parent)
2641 rank += loop->dimen;
2642
2643 return rank;
2644}
2645
2646
61a04b5b
RS
2647/* Helper routine of gfc_trans_array_constructor to determine if the
2648 bounds of the loop specified by LOOP are constant and simple enough
a13d9afe 2649 to use with trans_constant_array_constructor. Returns the
df2fba9e 2650 iteration count of the loop if suitable, and NULL_TREE otherwise. */
61a04b5b
RS
2651
2652static tree
f03077b0 2653constant_array_constructor_loop_size (gfc_loopinfo * l)
61a04b5b 2654{
f03077b0 2655 gfc_loopinfo *loop;
61a04b5b
RS
2656 tree size = gfc_index_one_node;
2657 tree tmp;
f03077b0 2658 int i, total_dim;
61a04b5b 2659
f03077b0
MM
2660 total_dim = get_rank (l);
2661
2662 for (loop = l; loop; loop = loop->parent)
61a04b5b 2663 {
f03077b0 2664 for (i = 0; i < loop->dimen; i++)
61a04b5b 2665 {
f03077b0
MM
2666 /* If the bounds aren't constant, return NULL_TREE. */
2667 if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
61a04b5b 2668 return NULL_TREE;
f03077b0
MM
2669 if (!integer_zerop (loop->from[i]))
2670 {
2671 /* Only allow nonzero "from" in one-dimensional arrays. */
2672 if (total_dim != 1)
2673 return NULL_TREE;
2674 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2675 gfc_array_index_type,
2676 loop->to[i], loop->from[i]);
2677 }
2678 else
2679 tmp = loop->to[i];
2680 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2681 gfc_array_index_type, tmp, gfc_index_one_node);
2682 size = fold_build2_loc (input_location, MULT_EXPR,
2683 gfc_array_index_type, size, tmp);
61a04b5b 2684 }
61a04b5b
RS
2685 }
2686
2687 return size;
2688}
2689
40f20186 2690
b2f82aaa
MM
2691static tree *
2692get_loop_upper_bound_for_array (gfc_ss *array, int array_dim)
2693{
2694 gfc_ss *ss;
2695 int n;
2696
2697 gcc_assert (array->nested_ss == NULL);
2698
2699 for (ss = array; ss; ss = ss->parent)
2700 for (n = 0; n < ss->loop->dimen; n++)
2701 if (array_dim == get_array_ref_dim_for_loop_dim (ss, n))
2702 return &(ss->loop->to[n]);
2703
2704 gcc_unreachable ();
2705}
2706
2707
d769d0df
MM
2708static gfc_loopinfo *
2709outermost_loop (gfc_loopinfo * loop)
2710{
2711 while (loop->parent != NULL)
2712 loop = loop->parent;
2713
2714 return loop;
2715}
2716
2717
6de9cd9a
DN
2718/* Array constructors are handled by constructing a temporary, then using that
2719 within the scalarization loop. This is not optimal, but seems by far the
2720 simplest method. */
2721
2722static void
6adbe654 2723trans_array_constructor (gfc_ss * ss, locus * where)
6de9cd9a 2724{
b7e75771 2725 gfc_constructor_base c;
6de9cd9a
DN
2726 tree offset;
2727 tree offsetvar;
2728 tree desc;
6de9cd9a 2729 tree type;
597553ab 2730 tree tmp;
b2f82aaa 2731 tree *loop_ubound0;
ec25720b 2732 bool dynamic;
4b7f8314
DK
2733 bool old_first_len, old_typespec_chararray_ctor;
2734 tree old_first_len_val;
d769d0df 2735 gfc_loopinfo *loop, *outer_loop;
a0add3be 2736 gfc_ss_info *ss_info;
f98cfd3c 2737 gfc_expr *expr;
fa168d9f 2738 gfc_ss *s;
90ee6453
EP
2739 tree neg_len;
2740 char *msg;
4b7f8314
DK
2741
2742 /* Save the old values for nested checking. */
2743 old_first_len = first_len;
2744 old_first_len_val = first_len_val;
2745 old_typespec_chararray_ctor = typespec_chararray_ctor;
6de9cd9a 2746
6adbe654 2747 loop = ss->loop;
d769d0df 2748 outer_loop = outermost_loop (loop);
a0add3be
MM
2749 ss_info = ss->info;
2750 expr = ss_info->expr;
f98cfd3c 2751
c03fc95d
DK
2752 /* Do bounds-checking here and in gfc_trans_array_ctor_element only if no
2753 typespec was given for the array constructor. */
3a146d46
JJ
2754 typespec_chararray_ctor = (expr->ts.type == BT_CHARACTER
2755 && expr->ts.u.cl
f98cfd3c 2756 && expr->ts.u.cl->length_from_typespec);
c03fc95d 2757
d3d3011f 2758 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
f98cfd3c 2759 && expr->ts.type == BT_CHARACTER && !typespec_chararray_ctor)
f04986a9 2760 {
32be9f94
PT
2761 first_len_val = gfc_create_var (gfc_charlen_type_node, "len");
2762 first_len = true;
2763 }
2764
b2f82aaa 2765 gcc_assert (ss->dimen == ss->loop->dimen);
40f20186 2766
f98cfd3c
MM
2767 c = expr->value.constructor;
2768 if (expr->ts.type == BT_CHARACTER)
40f20186 2769 {
c03fc95d 2770 bool const_string;
d751beac 2771 bool force_new_cl = false;
f04986a9 2772
c03fc95d
DK
2773 /* get_array_ctor_strlen walks the elements of the constructor, if a
2774 typespec was given, we already know the string length and want the one
2775 specified there. */
f98cfd3c
MM
2776 if (typespec_chararray_ctor && expr->ts.u.cl->length
2777 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
c03fc95d
DK
2778 {
2779 gfc_se length_se;
2780
2781 const_string = false;
2782 gfc_init_se (&length_se, NULL);
f98cfd3c 2783 gfc_conv_expr_type (&length_se, expr->ts.u.cl->length,
c03fc95d 2784 gfc_charlen_type_node);
a0add3be 2785 ss_info->string_length = length_se.expr;
90ee6453
EP
2786
2787 /* Check if the character length is negative. If it is, then
2788 set LEN = 0. */
2789 neg_len = fold_build2_loc (input_location, LT_EXPR,
63ee5404 2790 logical_type_node, ss_info->string_length,
f622221a
JB
2791 build_zero_cst (TREE_TYPE
2792 (ss_info->string_length)));
90ee6453
EP
2793 /* Print a warning if bounds checking is enabled. */
2794 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2795 {
2796 msg = xasprintf ("Negative character length treated as LEN = 0");
2797 gfc_trans_runtime_check (false, true, neg_len, &length_se.pre,
2798 where, msg);
2799 free (msg);
2800 }
2801
2802 ss_info->string_length
2803 = fold_build3_loc (input_location, COND_EXPR,
2804 gfc_charlen_type_node, neg_len,
f622221a
JB
2805 build_zero_cst
2806 (TREE_TYPE (ss_info->string_length)),
90ee6453
EP
2807 ss_info->string_length);
2808 ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
2809 &length_se.pre);
d769d0df
MM
2810 gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
2811 gfc_add_block_to_block (&outer_loop->post, &length_se.post);
c03fc95d
DK
2812 }
2813 else
d751beac
LK
2814 {
2815 const_string = get_array_ctor_strlen (&outer_loop->pre, c,
2816 &ss_info->string_length);
2817 force_new_cl = true;
2818 }
ca39e6f2
FXC
2819
2820 /* Complex character array constructors should have been taken care of
2821 and not end up here. */
a0add3be 2822 gcc_assert (ss_info->string_length);
40f20186 2823
d751beac 2824 store_backend_decl (&expr->ts.u.cl, ss_info->string_length, force_new_cl);
0ee8e250 2825
a0add3be 2826 type = gfc_get_character_type_len (expr->ts.kind, ss_info->string_length);
40f20186
PB
2827 if (const_string)
2828 type = build_pointer_type (type);
2829 }
2830 else
574284e9
AV
2831 type = gfc_typenode_for_spec (expr->ts.type == BT_CLASS
2832 ? &CLASS_DATA (expr)->ts : &expr->ts);
40f20186 2833
ec25720b
RS
2834 /* See if the constructor determines the loop bounds. */
2835 dynamic = false;
6a56381b 2836
b2f82aaa
MM
2837 loop_ubound0 = get_loop_upper_bound_for_array (ss, 0);
2838
2839 if (expr->shape && get_rank (loop) > 1 && *loop_ubound0 == NULL_TREE)
6a56381b
PT
2840 {
2841 /* We have a multidimensional parameter. */
fa168d9f
MM
2842 for (s = ss; s; s = s->parent)
2843 {
2844 int n;
2845 for (n = 0; n < s->loop->dimen; n++)
2846 {
2847 s->loop->from[n] = gfc_index_zero_node;
2848 s->loop->to[n] = gfc_conv_mpz_to_tree (expr->shape[s->dim[n]],
2849 gfc_index_integer_kind);
2850 s->loop->to[n] = fold_build2_loc (input_location, MINUS_EXPR,
2851 gfc_array_index_type,
2852 s->loop->to[n],
2853 gfc_index_one_node);
2854 }
2855 }
6a56381b
PT
2856 }
2857
b2f82aaa 2858 if (*loop_ubound0 == NULL_TREE)
ec25720b
RS
2859 {
2860 mpz_t size;
2861
2862 /* We should have a 1-dimensional, zero-based loop. */
4616ef9b 2863 gcc_assert (loop->parent == NULL && loop->nested == NULL);
ec25720b
RS
2864 gcc_assert (loop->dimen == 1);
2865 gcc_assert (integer_zerop (loop->from[0]));
2866
2867 /* Split the constructor size into a static part and a dynamic part.
2868 Allocate the static size up-front and record whether the dynamic
2869 size might be nonzero. */
2870 mpz_init (size);
2871 dynamic = gfc_get_array_constructor_size (&size, c);
2872 mpz_sub_ui (size, size, 1);
2873 loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
2874 mpz_clear (size);
2875 }
2876
62511fb1 2877 /* Special case constant array constructors. */
61a04b5b 2878 if (!dynamic)
62511fb1 2879 {
b01e2f88 2880 unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
62511fb1
RS
2881 if (nelem > 0)
2882 {
61a04b5b
RS
2883 tree size = constant_array_constructor_loop_size (loop);
2884 if (size && compare_tree_int (size, nelem) == 0)
62511fb1 2885 {
a13d9afe 2886 trans_constant_array_constructor (ss, type);
4b7f8314 2887 goto finish;
62511fb1
RS
2888 }
2889 }
2890 }
2891
d769d0df
MM
2892 gfc_trans_create_temp_array (&outer_loop->pre, &outer_loop->post, ss, type,
2893 NULL_TREE, dynamic, true, false, where);
6de9cd9a 2894
1838afec 2895 desc = ss_info->data.array.descriptor;
7ab92584 2896 offset = gfc_index_zero_node;
6de9cd9a 2897 offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
d5e69948 2898 suppress_warning (offsetvar);
6de9cd9a 2899 TREE_USED (offsetvar) = 0;
d769d0df 2900 gfc_trans_array_constructor_value (&outer_loop->pre, type, desc, c,
ec25720b
RS
2901 &offset, &offsetvar, dynamic);
2902
2903 /* If the array grows dynamically, the upper bound of the loop variable
2904 is determined by the array's final upper bound. */
2905 if (dynamic)
597553ab
PT
2906 {
2907 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2908 gfc_array_index_type,
2909 offsetvar, gfc_index_one_node);
d769d0df 2910 tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
597553ab 2911 gfc_conv_descriptor_ubound_set (&loop->pre, desc, gfc_rank_cst[0], tmp);
d168c883 2912 if (*loop_ubound0 && VAR_P (*loop_ubound0))
d769d0df 2913 gfc_add_modify (&outer_loop->pre, *loop_ubound0, tmp);
597553ab 2914 else
b2f82aaa 2915 *loop_ubound0 = tmp;
597553ab 2916 }
6de9cd9a
DN
2917
2918 if (TREE_USED (offsetvar))
2919 pushdecl (offsetvar);
2920 else
6e45f57b 2921 gcc_assert (INTEGER_CST_P (offset));
597553ab 2922
6de9cd9a 2923#if 0
dfc46c1f 2924 /* Disable bound checking for now because it's probably broken. */
d3d3011f 2925 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
6de9cd9a 2926 {
6e45f57b 2927 gcc_unreachable ();
6de9cd9a
DN
2928 }
2929#endif
4b7f8314
DK
2930
2931finish:
2932 /* Restore old values of globals. */
2933 first_len = old_first_len;
2934 first_len_val = old_first_len_val;
2935 typespec_chararray_ctor = old_typespec_chararray_ctor;
6de9cd9a
DN
2936}
2937
2938
7a70c12d
RS
2939/* INFO describes a GFC_SS_SECTION in loop LOOP, and this function is
2940 called after evaluating all of INFO's vector dimensions. Go through
2941 each such vector dimension and see if we can now fill in any missing
2942 loop bounds. */
2943
2944static void
84952a4e 2945set_vector_loop_bounds (gfc_ss * ss)
7a70c12d 2946{
d769d0df 2947 gfc_loopinfo *loop, *outer_loop;
6d63e468 2948 gfc_array_info *info;
7a70c12d
RS
2949 gfc_se se;
2950 tree tmp;
2951 tree desc;
2952 tree zero;
2953 int n;
2954 int dim;
2955
d769d0df
MM
2956 outer_loop = outermost_loop (ss->loop);
2957
1838afec 2958 info = &ss->info->data.array;
43e7d60b 2959
f49afcb0 2960 for (; ss; ss = ss->parent)
7a70c12d 2961 {
f49afcb0
MM
2962 loop = ss->loop;
2963
2964 for (n = 0; n < loop->dimen; n++)
7a70c12d 2965 {
f49afcb0
MM
2966 dim = ss->dim[n];
2967 if (info->ref->u.ar.dimen_type[dim] != DIMEN_VECTOR
2968 || loop->to[n] != NULL)
2969 continue;
2970
7a70c12d
RS
2971 /* Loop variable N indexes vector dimension DIM, and we don't
2972 yet know the upper bound of loop variable N. Set it to the
2973 difference between the vector's upper and lower bounds. */
2974 gcc_assert (loop->from[n] == gfc_index_zero_node);
2975 gcc_assert (info->subscript[dim]
bcc4d4e0 2976 && info->subscript[dim]->info->type == GFC_SS_VECTOR);
7a70c12d
RS
2977
2978 gfc_init_se (&se, NULL);
1838afec 2979 desc = info->subscript[dim]->info->data.array.descriptor;
7a70c12d 2980 zero = gfc_rank_cst[0];
94471a56
TB
2981 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2982 gfc_array_index_type,
568e8e1e
PT
2983 gfc_conv_descriptor_ubound_get (desc, zero),
2984 gfc_conv_descriptor_lbound_get (desc, zero));
d769d0df 2985 tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
7a70c12d
RS
2986 loop->to[n] = tmp;
2987 }
2988 }
2989}
2990
2991
14aeb3cd
MM
2992/* Tells whether a scalar argument to an elemental procedure is saved out
2993 of a scalarization loop as a value or as a reference. */
2994
2995bool
2996gfc_scalar_elemental_arg_saved_as_reference (gfc_ss_info * ss_info)
2997{
2998 if (ss_info->type != GFC_SS_REFERENCE)
2999 return false;
3000
4932364b
TK
3001 if (ss_info->data.scalar.needs_temporary)
3002 return false;
3003
14aeb3cd
MM
3004 /* If the actual argument can be absent (in other words, it can
3005 be a NULL reference), don't try to evaluate it; pass instead
3006 the reference directly. */
3007 if (ss_info->can_be_null_ref)
3008 return true;
3009
3010 /* If the expression is of polymorphic type, it's actual size is not known,
3011 so we avoid copying it anywhere. */
3012 if (ss_info->data.scalar.dummy_arg
5d9d16db
MM
3013 && gfc_dummy_arg_get_typespec (*ss_info->data.scalar.dummy_arg).type
3014 == BT_CLASS
14aeb3cd
MM
3015 && ss_info->expr->ts.type == BT_CLASS)
3016 return true;
3017
3018 /* If the expression is a data reference of aggregate type,
711d7c23 3019 and the data reference is not used on the left hand side,
14aeb3cd 3020 avoid a copy by saving a reference to the content. */
711d7c23 3021 if (!ss_info->data.scalar.needs_temporary
14aeb3cd 3022 && (ss_info->expr->ts.type == BT_DERIVED
711d7c23
MM
3023 || ss_info->expr->ts.type == BT_CLASS)
3024 && gfc_expr_is_variable (ss_info->expr))
14aeb3cd
MM
3025 return true;
3026
3027 /* Otherwise the expression is evaluated to a temporary variable before the
3028 scalarization loop. */
3029 return false;
3030}
3031
3032
6de9cd9a
DN
3033/* Add the pre and post chains for all the scalar expressions in a SS chain
3034 to loop. This is called after the loop parameters have been calculated,
3035 but before the actual scalarizing loops. */
6de9cd9a
DN
3036
3037static void
bdfd2ff0
TK
3038gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
3039 locus * where)
6de9cd9a 3040{
d769d0df 3041 gfc_loopinfo *nested_loop, *outer_loop;
6de9cd9a 3042 gfc_se se;
f98cfd3c 3043 gfc_ss_info *ss_info;
1838afec 3044 gfc_array_info *info;
f98cfd3c 3045 gfc_expr *expr;
6de9cd9a
DN
3046 int n;
3047
f391a855
TB
3048 /* Don't evaluate the arguments for realloc_lhs_loop_for_fcn_call; otherwise,
3049 arguments could get evaluated multiple times. */
3050 if (ss->is_alloc_lhs)
3051 return;
3052
d769d0df
MM
3053 outer_loop = outermost_loop (loop);
3054
df2fba9e
RW
3055 /* TODO: This can generate bad code if there are ordering dependencies,
3056 e.g., a callee allocated function and an unknown size constructor. */
6e45f57b 3057 gcc_assert (ss != NULL);
6de9cd9a
DN
3058
3059 for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
3060 {
6e45f57b 3061 gcc_assert (ss);
6de9cd9a 3062
30ae600f
MM
3063 /* Cross loop arrays are handled from within the most nested loop. */
3064 if (ss->nested_ss != NULL)
3065 continue;
3066
f98cfd3c
MM
3067 ss_info = ss->info;
3068 expr = ss_info->expr;
1838afec 3069 info = &ss_info->data.array;
f98cfd3c
MM
3070
3071 switch (ss_info->type)
6de9cd9a
DN
3072 {
3073 case GFC_SS_SCALAR:
3074 /* Scalar expression. Evaluate this now. This includes elemental
3075 dimension indices, but not array section bounds. */
3076 gfc_init_se (&se, NULL);
f98cfd3c 3077 gfc_conv_expr (&se, expr);
d769d0df 3078 gfc_add_block_to_block (&outer_loop->pre, &se.pre);
6de9cd9a 3079
43a68a9d
PT
3080 if (expr->ts.type != BT_CHARACTER
3081 && !gfc_is_alloc_class_scalar_function (expr))
ae772c2d
PT
3082 {
3083 /* Move the evaluation of scalar expressions outside the
3084 scalarization loop, except for WHERE assignments. */
3085 if (subscript)
3086 se.expr = convert(gfc_array_index_type, se.expr);
42d0058e 3087 if (!ss_info->where)
d769d0df
MM
3088 se.expr = gfc_evaluate_now (se.expr, &outer_loop->pre);
3089 gfc_add_block_to_block (&outer_loop->pre, &se.post);
ae772c2d
PT
3090 }
3091 else
d769d0df 3092 gfc_add_block_to_block (&outer_loop->post, &se.post);
6de9cd9a 3093
99dd5a29 3094 ss_info->data.scalar.value = se.expr;
a0add3be 3095 ss_info->string_length = se.string_length;
6de9cd9a
DN
3096 break;
3097
3098 case GFC_SS_REFERENCE:
0192ef20 3099 /* Scalar argument to elemental procedure. */
6de9cd9a 3100 gfc_init_se (&se, NULL);
14aeb3cd
MM
3101 if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
3102 gfc_conv_expr_reference (&se, expr);
0192ef20
MM
3103 else
3104 {
14aeb3cd 3105 /* Evaluate the argument outside the loop and pass
0192ef20
MM
3106 a reference to the value. */
3107 gfc_conv_expr (&se, expr);
3108 }
da78a067
PT
3109
3110 /* Ensure that a pointer to the string is stored. */
3111 if (expr->ts.type == BT_CHARACTER)
3112 gfc_conv_string_parameter (&se);
3113
d769d0df
MM
3114 gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3115 gfc_add_block_to_block (&outer_loop->post, &se.post);
c49ea23d
PT
3116 if (gfc_is_class_scalar_expr (expr))
3117 /* This is necessary because the dynamic type will always be
3118 large than the declared type. In consequence, assigning
3119 the value to a temporary could segfault.
3120 OOP-TODO: see if this is generally correct or is the value
3121 has to be written to an allocated temporary, whose address
3122 is passed via ss_info. */
3123 ss_info->data.scalar.value = se.expr;
3124 else
3125 ss_info->data.scalar.value = gfc_evaluate_now (se.expr,
3126 &outer_loop->pre);
6de9cd9a 3127
a0add3be 3128 ss_info->string_length = se.string_length;
6de9cd9a
DN
3129 break;
3130
3131 case GFC_SS_SECTION:
7a70c12d 3132 /* Add the expressions for scalar and vector subscripts. */
6de9cd9a 3133 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1838afec 3134 if (info->subscript[n])
573234ac 3135 gfc_add_loop_ss_code (loop, info->subscript[n], true, where);
7a70c12d 3136
84952a4e 3137 set_vector_loop_bounds (ss);
7a70c12d
RS
3138 break;
3139
3140 case GFC_SS_VECTOR:
3141 /* Get the vector's descriptor and store it in SS. */
3142 gfc_init_se (&se, NULL);
2960a368 3143 gfc_conv_expr_descriptor (&se, expr);
d769d0df
MM
3144 gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3145 gfc_add_block_to_block (&outer_loop->post, &se.post);
1838afec 3146 info->descriptor = se.expr;
6de9cd9a
DN
3147 break;
3148
3149 case GFC_SS_INTRINSIC:
3150 gfc_add_intrinsic_ss_code (loop, ss);
3151 break;
3152
3153 case GFC_SS_FUNCTION:
3154 /* Array function return value. We call the function and save its
3155 result in a temporary for use inside the loop. */
3156 gfc_init_se (&se, NULL);
3157 se.loop = loop;
3158 se.ss = ss;
a6b22eea
PT
3159 if (gfc_is_class_array_function (expr))
3160 expr->must_finalize = 1;
f98cfd3c 3161 gfc_conv_expr (&se, expr);
d769d0df
MM
3162 gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3163 gfc_add_block_to_block (&outer_loop->post, &se.post);
a0add3be 3164 ss_info->string_length = se.string_length;
6de9cd9a
DN
3165 break;
3166
3167 case GFC_SS_CONSTRUCTOR:
f98cfd3c 3168 if (expr->ts.type == BT_CHARACTER
a0add3be 3169 && ss_info->string_length == NULL
f98cfd3c 3170 && expr->ts.u.cl
d751beac
LK
3171 && expr->ts.u.cl->length
3172 && expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
f2d3cb25
PT
3173 {
3174 gfc_init_se (&se, NULL);
f98cfd3c 3175 gfc_conv_expr_type (&se, expr->ts.u.cl->length,
f2d3cb25 3176 gfc_charlen_type_node);
a0add3be 3177 ss_info->string_length = se.expr;
d769d0df
MM
3178 gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3179 gfc_add_block_to_block (&outer_loop->post, &se.post);
f2d3cb25 3180 }
6adbe654 3181 trans_array_constructor (ss, where);
6de9cd9a
DN
3182 break;
3183
fc90a8f2 3184 case GFC_SS_TEMP:
e9cfef64
PB
3185 case GFC_SS_COMPONENT:
3186 /* Do nothing. These are handled elsewhere. */
fc90a8f2
PB
3187 break;
3188
6de9cd9a 3189 default:
6e45f57b 3190 gcc_unreachable ();
6de9cd9a
DN
3191 }
3192 }
30ae600f 3193
573234ac 3194 if (!subscript)
30ae600f
MM
3195 for (nested_loop = loop->nested; nested_loop;
3196 nested_loop = nested_loop->next)
3197 gfc_add_loop_ss_code (nested_loop, nested_loop->ss, subscript, where);
6de9cd9a
DN
3198}
3199
3200
3201/* Translate expressions for the descriptor and data pointer of a SS. */
3202/*GCC ARRAYS*/
3203
3204static void
3205gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
3206{
3207 gfc_se se;
f98cfd3c 3208 gfc_ss_info *ss_info;
1838afec 3209 gfc_array_info *info;
6de9cd9a
DN
3210 tree tmp;
3211
f98cfd3c 3212 ss_info = ss->info;
1838afec 3213 info = &ss_info->data.array;
f98cfd3c 3214
6de9cd9a 3215 /* Get the descriptor for the array to be scalarized. */
f98cfd3c 3216 gcc_assert (ss_info->expr->expr_type == EXPR_VARIABLE);
6de9cd9a
DN
3217 gfc_init_se (&se, NULL);
3218 se.descriptor_only = 1;
f98cfd3c 3219 gfc_conv_expr_lhs (&se, ss_info->expr);
6de9cd9a 3220 gfc_add_block_to_block (block, &se.pre);
1838afec 3221 info->descriptor = se.expr;
a0add3be 3222 ss_info->string_length = se.string_length;
6de9cd9a
DN
3223
3224 if (base)
3225 {
cef026ec
AV
3226 if (ss_info->expr->ts.type == BT_CHARACTER && !ss_info->expr->ts.deferred
3227 && ss_info->expr->ts.u.cl->length == NULL)
3228 {
3229 /* Emit a DECL_EXPR for the variable sized array type in
3230 GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
3231 sizes works correctly. */
3232 tree arraytype = TREE_TYPE (
3233 GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (info->descriptor)));
3234 if (! TYPE_NAME (arraytype))
3235 TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
3236 NULL_TREE, arraytype);
3237 gfc_add_expr_to_block (block, build1 (DECL_EXPR, arraytype,
3238 TYPE_NAME (arraytype)));
3239 }
6de9cd9a
DN
3240 /* Also the data pointer. */
3241 tmp = gfc_conv_array_data (se.expr);
ce8dcc91 3242 /* If this is a variable or address or a class array, use it directly.
2054fc29 3243 Otherwise we must evaluate it now to avoid breaking dependency
6de9cd9a
DN
3244 analysis by pulling the expressions for elemental array indices
3245 inside the loop. */
3246 if (!(DECL_P (tmp)
3247 || (TREE_CODE (tmp) == ADDR_EXPR
ce8dcc91
PT
3248 && DECL_P (TREE_OPERAND (tmp, 0)))
3249 || (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
3250 && TREE_CODE (se.expr) == COMPONENT_REF
3251 && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (se.expr, 0))))))
6de9cd9a 3252 tmp = gfc_evaluate_now (tmp, block);
1838afec 3253 info->data = tmp;
6de9cd9a
DN
3254
3255 tmp = gfc_conv_array_offset (se.expr);
1838afec 3256 info->offset = gfc_evaluate_now (tmp, block);
597553ab
PT
3257
3258 /* Make absolutely sure that the saved_offset is indeed saved
3259 so that the variable is still accessible after the loops
3260 are translated. */
1838afec 3261 info->saved_offset = info->offset;
6de9cd9a
DN
3262 }
3263}
3264
3265
1f2959f0 3266/* Initialize a gfc_loopinfo structure. */
6de9cd9a
DN
3267
3268void
3269gfc_init_loopinfo (gfc_loopinfo * loop)
3270{
3271 int n;
3272
3273 memset (loop, 0, sizeof (gfc_loopinfo));
3274 gfc_init_block (&loop->pre);
3275 gfc_init_block (&loop->post);
3276
3d03ead0 3277 /* Initially scalarize in order and default to no loop reversal. */
6de9cd9a 3278 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
3d03ead0
PT
3279 {
3280 loop->order[n] = n;
aed5574e 3281 loop->reverse[n] = GFC_INHIBIT_REVERSE;
3d03ead0 3282 }
6de9cd9a
DN
3283
3284 loop->ss = gfc_ss_terminator;
3285}
3286
3287
e7dc5b4f 3288/* Copies the loop variable info to a gfc_se structure. Does not copy the SS
6de9cd9a
DN
3289 chain. */
3290
3291void
3292gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
3293{
3294 se->loop = loop;
3295}
3296
3297
3298/* Return an expression for the data pointer of an array. */
3299
3300tree
3301gfc_conv_array_data (tree descriptor)
3302{
3303 tree type;
3304
3305 type = TREE_TYPE (descriptor);
3306 if (GFC_ARRAY_TYPE_P (type))
3307 {
3308 if (TREE_CODE (type) == POINTER_TYPE)
3309 return descriptor;
3310 else
3311 {
13413760 3312 /* Descriptorless arrays. */
628c189e 3313 return gfc_build_addr_expr (NULL_TREE, descriptor);
6de9cd9a
DN
3314 }
3315 }
3316 else
4c73896d 3317 return gfc_conv_descriptor_data_get (descriptor);
6de9cd9a
DN
3318}
3319
3320
3321/* Return an expression for the base offset of an array. */
3322
3323tree
3324gfc_conv_array_offset (tree descriptor)
3325{
3326 tree type;
3327
3328 type = TREE_TYPE (descriptor);
3329 if (GFC_ARRAY_TYPE_P (type))
3330 return GFC_TYPE_ARRAY_OFFSET (type);
3331 else
568e8e1e 3332 return gfc_conv_descriptor_offset_get (descriptor);
6de9cd9a
DN
3333}
3334
3335
3336/* Get an expression for the array stride. */
3337
3338tree
3339gfc_conv_array_stride (tree descriptor, int dim)
3340{
3341 tree tmp;
3342 tree type;
3343
3344 type = TREE_TYPE (descriptor);
3345
3346 /* For descriptorless arrays use the array size. */
3347 tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
3348 if (tmp != NULL_TREE)
3349 return tmp;
3350
568e8e1e 3351 tmp = gfc_conv_descriptor_stride_get (descriptor, gfc_rank_cst[dim]);
6de9cd9a
DN
3352 return tmp;
3353}
3354
3355
3356/* Like gfc_conv_array_stride, but for the lower bound. */
3357
3358tree
3359gfc_conv_array_lbound (tree descriptor, int dim)
3360{
3361 tree tmp;
3362 tree type;
3363
3364 type = TREE_TYPE (descriptor);
3365
3366 tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
3367 if (tmp != NULL_TREE)
3368 return tmp;
3369
568e8e1e 3370 tmp = gfc_conv_descriptor_lbound_get (descriptor, gfc_rank_cst[dim]);
6de9cd9a
DN
3371 return tmp;
3372}
3373
3374
3375/* Like gfc_conv_array_stride, but for the upper bound. */
3376
3377tree
3378gfc_conv_array_ubound (tree descriptor, int dim)
3379{
3380 tree tmp;
3381 tree type;
3382
3383 type = TREE_TYPE (descriptor);
3384
3385 tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
3386 if (tmp != NULL_TREE)
3387 return tmp;
3388
3389 /* This should only ever happen when passing an assumed shape array
3390 as an actual parameter. The value will never be used. */
3391 if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
7ab92584 3392 return gfc_index_zero_node;
6de9cd9a 3393
568e8e1e 3394 tmp = gfc_conv_descriptor_ubound_get (descriptor, gfc_rank_cst[dim]);
6de9cd9a
DN
3395 return tmp;
3396}
3397
3398
6de9cd9a
DN
3399/* Generate code to perform an array index bound check. */
3400
3401static tree
36e783e3
MM
3402trans_array_bound_check (gfc_se * se, gfc_ss *ss, tree index, int n,
3403 locus * where, bool check_upper)
6de9cd9a 3404{
6de9cd9a 3405 tree fault;
c6ec7cc6 3406 tree tmp_lo, tmp_up;
36e783e3 3407 tree descriptor;
dd18a33b 3408 char *msg;
d19c0f4f 3409 const char * name = NULL;
6de9cd9a 3410
d3d3011f 3411 if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
6de9cd9a
DN
3412 return index;
3413
1838afec 3414 descriptor = ss->info->data.array.descriptor;
36e783e3 3415
6de9cd9a 3416 index = gfc_evaluate_now (index, &se->pre);
dd18a33b 3417
d19c0f4f 3418 /* We find a name for the error message. */
f98cfd3c 3419 name = ss->info->expr->symtree->n.sym->name;
14bf3267 3420 gcc_assert (name != NULL);
d19c0f4f 3421
d168c883 3422 if (VAR_P (descriptor))
e3e529d1
SK
3423 name = IDENTIFIER_POINTER (DECL_NAME (descriptor));
3424
c6ec7cc6 3425 /* If upper bound is present, include both bounds in the error message. */
c099916d
FXC
3426 if (check_upper)
3427 {
c6ec7cc6
DW
3428 tmp_lo = gfc_conv_array_lbound (descriptor, n);
3429 tmp_up = gfc_conv_array_ubound (descriptor, n);
3430
3431 if (name)
1a33dc9e
UB
3432 msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3433 "outside of expected range (%%ld:%%ld)", n+1, name);
c6ec7cc6 3434 else
1a33dc9e
UB
3435 msg = xasprintf ("Index '%%ld' of dimension %d "
3436 "outside of expected range (%%ld:%%ld)", n+1);
c6ec7cc6 3437
63ee5404 3438 fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
94471a56 3439 index, tmp_lo);
c6ec7cc6
DW
3440 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3441 fold_convert (long_integer_type_node, index),
3442 fold_convert (long_integer_type_node, tmp_lo),
3443 fold_convert (long_integer_type_node, tmp_up));
63ee5404 3444 fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
94471a56 3445 index, tmp_up);
c6ec7cc6
DW
3446 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3447 fold_convert (long_integer_type_node, index),
3448 fold_convert (long_integer_type_node, tmp_lo),
3449 fold_convert (long_integer_type_node, tmp_up));
cede9502 3450 free (msg);
c6ec7cc6
DW
3451 }
3452 else
3453 {
3454 tmp_lo = gfc_conv_array_lbound (descriptor, n);
3455
c099916d 3456 if (name)
1a33dc9e
UB
3457 msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3458 "below lower bound of %%ld", n+1, name);
c099916d 3459 else
1a33dc9e
UB
3460 msg = xasprintf ("Index '%%ld' of dimension %d "
3461 "below lower bound of %%ld", n+1);
c6ec7cc6 3462
63ee5404 3463 fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
94471a56 3464 index, tmp_lo);
0d52899f 3465 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
c8fe94c7 3466 fold_convert (long_integer_type_node, index),
c6ec7cc6 3467 fold_convert (long_integer_type_node, tmp_lo));
cede9502 3468 free (msg);
c099916d 3469 }
6de9cd9a
DN
3470
3471 return index;
3472}
3473
3474
6de9cd9a 3475/* Return the offset for an index. Performs bound checking for elemental
9157ccb2
MM
3476 dimensions. Single element references are processed separately.
3477 DIM is the array dimension, I is the loop dimension. */
6de9cd9a
DN
3478
3479static tree
36e783e3
MM
3480conv_array_index_offset (gfc_se * se, gfc_ss * ss, int dim, int i,
3481 gfc_array_ref * ar, tree stride)
6de9cd9a 3482{
6d63e468 3483 gfc_array_info *info;
6de9cd9a 3484 tree index;
7a70c12d
RS
3485 tree desc;
3486 tree data;
6de9cd9a 3487
1838afec 3488 info = &ss->info->data.array;
36e783e3 3489
6de9cd9a
DN
3490 /* Get the index into the array for this dimension. */
3491 if (ar)
3492 {
6e45f57b 3493 gcc_assert (ar->type != AR_ELEMENT);
7a70c12d 3494 switch (ar->dimen_type[dim])
6de9cd9a 3495 {
a3935ffc
TB
3496 case DIMEN_THIS_IMAGE:
3497 gcc_unreachable ();
3498 break;
7a70c12d 3499 case DIMEN_ELEMENT:
6de9cd9a 3500 /* Elemental dimension. */
6e45f57b 3501 gcc_assert (info->subscript[dim]
bcc4d4e0 3502 && info->subscript[dim]->info->type == GFC_SS_SCALAR);
6de9cd9a 3503 /* We've already translated this value outside the loop. */
99dd5a29 3504 index = info->subscript[dim]->info->data.scalar.value;
6de9cd9a 3505
36e783e3
MM
3506 index = trans_array_bound_check (se, ss, index, dim, &ar->where,
3507 ar->as->type != AS_ASSUMED_SIZE
3508 || dim < ar->dimen - 1);
7a70c12d
RS
3509 break;
3510
3511 case DIMEN_VECTOR:
3512 gcc_assert (info && se->loop);
3513 gcc_assert (info->subscript[dim]
bcc4d4e0 3514 && info->subscript[dim]->info->type == GFC_SS_VECTOR);
1838afec 3515 desc = info->subscript[dim]->info->data.array.descriptor;
7a70c12d
RS
3516
3517 /* Get a zero-based index into the vector. */
94471a56
TB
3518 index = fold_build2_loc (input_location, MINUS_EXPR,
3519 gfc_array_index_type,
3520 se->loop->loopvar[i], se->loop->from[i]);
7a70c12d
RS
3521
3522 /* Multiply the index by the stride. */
94471a56
TB
3523 index = fold_build2_loc (input_location, MULT_EXPR,
3524 gfc_array_index_type,
3525 index, gfc_conv_array_stride (desc, 0));
7a70c12d
RS
3526
3527 /* Read the vector to get an index into info->descriptor. */
db3927fb
AH
3528 data = build_fold_indirect_ref_loc (input_location,
3529 gfc_conv_array_data (desc));
1d6b7f39 3530 index = gfc_build_array_ref (data, index, NULL);
7a70c12d 3531 index = gfc_evaluate_now (index, &se->pre);
92375a20 3532 index = fold_convert (gfc_array_index_type, index);
7a70c12d
RS
3533
3534 /* Do any bounds checking on the final info->descriptor index. */
36e783e3
MM
3535 index = trans_array_bound_check (se, ss, index, dim, &ar->where,
3536 ar->as->type != AS_ASSUMED_SIZE
3537 || dim < ar->dimen - 1);
7a70c12d
RS
3538 break;
3539
3540 case DIMEN_RANGE:
6de9cd9a 3541 /* Scalarized dimension. */
6e45f57b 3542 gcc_assert (info && se->loop);
6de9cd9a 3543
9157ccb2 3544 /* Multiply the loop variable by the stride and delta. */
6de9cd9a 3545 index = se->loop->loopvar[i];
9157ccb2 3546 if (!integer_onep (info->stride[dim]))
94471a56
TB
3547 index = fold_build2_loc (input_location, MULT_EXPR,
3548 gfc_array_index_type, index,
3549 info->stride[dim]);
9157ccb2 3550 if (!integer_zerop (info->delta[dim]))
94471a56
TB
3551 index = fold_build2_loc (input_location, PLUS_EXPR,
3552 gfc_array_index_type, index,
3553 info->delta[dim]);
7a70c12d 3554 break;
6de9cd9a 3555
7a70c12d
RS
3556 default:
3557 gcc_unreachable ();
6de9cd9a
DN
3558 }
3559 }
3560 else
3561 {
e9cfef64 3562 /* Temporary array or derived type component. */
6e45f57b 3563 gcc_assert (se->loop);
6de9cd9a 3564 index = se->loop->loopvar[se->loop->order[i]];
30a390c8 3565
f04986a9 3566 /* Pointer functions can have stride[0] different from unity.
30a390c8 3567 Use the stride returned by the function call and stored in
f04986a9 3568 the descriptor for the temporary. */
bcc4d4e0 3569 if (se->ss && se->ss->info->type == GFC_SS_FUNCTION
f98cfd3c
MM
3570 && se->ss->info->expr
3571 && se->ss->info->expr->symtree
3572 && se->ss->info->expr->symtree->n.sym->result
3573 && se->ss->info->expr->symtree->n.sym->result->attr.pointer)
30a390c8
PT
3574 stride = gfc_conv_descriptor_stride_get (info->descriptor,
3575 gfc_rank_cst[dim]);
3576
43a68a9d 3577 if (info->delta[dim] && !integer_zerop (info->delta[dim]))
94471a56
TB
3578 index = fold_build2_loc (input_location, PLUS_EXPR,
3579 gfc_array_index_type, index, info->delta[dim]);
6de9cd9a
DN
3580 }
3581
3582 /* Multiply by the stride. */
2368eaf9 3583 if (stride != NULL && !integer_onep (stride))
94471a56
TB
3584 index = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
3585 index, stride);
6de9cd9a
DN
3586
3587 return index;
3588}
3589
3590
c49ea23d
PT
3591/* Build a scalarized array reference using the vptr 'size'. */
3592
3593static bool
3594build_class_array_ref (gfc_se *se, tree base, tree index)
3595{
c49ea23d 3596 tree size;
574284e9 3597 tree decl = NULL_TREE;
c49ea23d
PT
3598 tree tmp;
3599 gfc_expr *expr = se->ss->info->expr;
9a0e09f3 3600 gfc_expr *class_expr;
c49ea23d 3601 gfc_typespec *ts;
9a0e09f3 3602 gfc_symbol *sym;
c49ea23d 3603
9a0e09f3
PT
3604 tmp = !VAR_P (base) ? gfc_get_class_from_expr (base) : NULL_TREE;
3605
3606 if (tmp != NULL_TREE)
3607 decl = tmp;
c49ea23d 3608 else
c49ea23d 3609 {
9a0e09f3
PT
3610 /* The base expression does not contain a class component, either
3611 because it is a temporary array or array descriptor. Class
3612 array functions are correctly resolved above. */
3613 if (!expr
574284e9 3614 || (expr->ts.type != BT_CLASS
574284e9
AV
3615 && !gfc_is_class_array_ref (expr, NULL)))
3616 return false;
3617
9a0e09f3
PT
3618 /* Obtain the expression for the class entity or component that is
3619 followed by an array reference, which is not an element, so that
3620 the span of the array can be obtained. */
3621 class_expr = gfc_find_and_cut_at_last_class_ref (expr, false, &ts);
c49ea23d 3622
9a0e09f3 3623 if (!ts)
574284e9 3624 return false;
c49ea23d 3625
9a0e09f3
PT
3626 sym = (!class_expr && expr) ? expr->symtree->n.sym : NULL;
3627 if (sym && sym->attr.function
3628 && sym == sym->result
3629 && sym->backend_decl == current_function_decl)
3630 /* The temporary is the data field of the class data component
3631 of the current function. */
3632 decl = gfc_get_fake_result_decl (sym, 0);
3633 else if (sym)
3634 {
3635 if (decl == NULL_TREE)
3636 decl = expr->symtree->n.sym->backend_decl;
3637 /* For class arrays the tree containing the class is stored in
3638 GFC_DECL_SAVED_DESCRIPTOR of the sym's backend_decl.
3639 For all others it's sym's backend_decl directly. */
3640 if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
3641 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
43a68a9d 3642 }
9a0e09f3
PT
3643 else
3644 decl = gfc_get_class_from_gfc_expr (class_expr);
43a68a9d 3645
9a0e09f3
PT
3646 if (POINTER_TYPE_P (TREE_TYPE (decl)))
3647 decl = build_fold_indirect_ref_loc (input_location, decl);
a6b22eea 3648
9a0e09f3
PT
3649 if (!GFC_CLASS_TYPE_P (TREE_TYPE (decl)))
3650 return false;
c49ea23d
PT
3651 }
3652
9a0e09f3 3653 se->class_vptr = gfc_evaluate_now (gfc_class_vptr_get (decl), &se->pre);
43a68a9d 3654
34d9d749 3655 size = gfc_class_vtab_size_get (decl);
cef026ec 3656 /* For unlimited polymorphic entities then _len component needs to be
ce8dcc91
PT
3657 multiplied with the size. */
3658 size = gfc_resize_class_size_with_len (&se->pre, decl, size);
ce8dcc91 3659 size = fold_convert (TREE_TYPE (index), size);
cef026ec 3660
c49ea23d 3661 /* Return the element in the se expression. */
9a0e09f3 3662 se->expr = gfc_build_spanned_array_ref (base, index, size);
c49ea23d
PT
3663 return true;
3664}
3665
3666
7964ab6c
MM
3667/* Indicates that the tree EXPR is a reference to an array that can’t
3668 have any negative stride. */
3669
3670static bool
3671non_negative_strides_array_p (tree expr)
3672{
3673 if (expr == NULL_TREE)
3674 return false;
3675
3676 tree type = TREE_TYPE (expr);
3677 if (POINTER_TYPE_P (type))
3678 type = TREE_TYPE (type);
3679
3680 if (TYPE_LANG_SPECIFIC (type))
3681 {
3682 gfc_array_kind array_kind = GFC_TYPE_ARRAY_AKIND (type);
3683
3684 if (array_kind == GFC_ARRAY_ALLOCATABLE
3685 || array_kind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3686 return true;
3687 }
3688
3689 /* An array with descriptor can have negative strides.
3690 We try to be conservative and return false by default here
3691 if we don’t recognize a contiguous array instead of
3692 returning false if we can identify a non-contiguous one. */
3693 if (!GFC_ARRAY_TYPE_P (type))
3694 return false;
3695
3696 /* If the array was originally a dummy with a descriptor, strides can be
3697 negative. */
3698 if (DECL_P (expr)
3e0c9fdf
MM
3699 && DECL_LANG_SPECIFIC (expr)
3700 && GFC_DECL_SAVED_DESCRIPTOR (expr)
3701 && GFC_DECL_SAVED_DESCRIPTOR (expr) != expr)
3702 return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (expr));
7964ab6c
MM
3703
3704 return true;
3705}
3706
3707
6de9cd9a
DN
3708/* Build a scalarized reference to an array. */
3709
3710static void
7964ab6c
MM
3711gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar,
3712 bool tmp_array = false)
6de9cd9a 3713{
6d63e468 3714 gfc_array_info *info;
1d6b7f39 3715 tree decl = NULL_TREE;
6de9cd9a 3716 tree index;
b120c8b2 3717 tree base;
cb4b9eae 3718 gfc_ss *ss;
f98cfd3c 3719 gfc_expr *expr;
6de9cd9a
DN
3720 int n;
3721
cb4b9eae 3722 ss = se->ss;
f98cfd3c 3723 expr = ss->info->expr;
1838afec 3724 info = &ss->info->data.array;
6de9cd9a
DN
3725 if (ar)
3726 n = se->loop->order[0];
3727 else
3728 n = 0;
3729
cb4b9eae 3730 index = conv_array_index_offset (se, ss, ss->dim[n], n, ar, info->stride0);
6de9cd9a
DN
3731 /* Add the offset for this dimension to the stored offset for all other
3732 dimensions. */
43a68a9d 3733 if (info->offset && !integer_zerop (info->offset))
94471a56
TB
3734 index = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
3735 index, info->offset);
6de9cd9a 3736
b120c8b2
PT
3737 base = build_fold_indirect_ref_loc (input_location, info->data);
3738
3739 /* Use the vptr 'size' field to access the element of a class array. */
3740 if (build_class_array_ref (se, base, index))
3741 return;
3742
0d78e4aa 3743 if (get_CFI_desc (NULL, expr, &decl, ar))
94f3d11c 3744 decl = build_fold_indirect_ref_loc (input_location, decl);
d5f48c7c 3745
ff3598bc
PT
3746 /* A pointer array component can be detected from its field decl. Fix
3747 the descriptor, mark the resulting variable decl and pass it to
3748 gfc_build_array_ref. */
ba08c70a
PT
3749 if (is_pointer_array (info->descriptor)
3750 || (expr && expr->ts.deferred && info->descriptor
3751 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (info->descriptor))))
ff3598bc
PT
3752 {
3753 if (TREE_CODE (info->descriptor) == COMPONENT_REF)
4e227341 3754 decl = info->descriptor;
ff3598bc
PT
3755 else if (TREE_CODE (info->descriptor) == INDIRECT_REF)
3756 decl = TREE_OPERAND (info->descriptor, 0);
3757
3758 if (decl == NULL_TREE)
3759 decl = info->descriptor;
3760 }
3761
7964ab6c
MM
3762 bool non_negative_stride = tmp_array
3763 || non_negative_strides_array_p (info->descriptor);
3764 se->expr = gfc_build_array_ref (base, index, decl,
3765 non_negative_stride);
6de9cd9a
DN
3766}
3767
3768
3769/* Translate access of temporary array. */
3770
3771void
3772gfc_conv_tmp_array_ref (gfc_se * se)
3773{
a0add3be 3774 se->string_length = se->ss->info->string_length;
7964ab6c 3775 gfc_conv_scalarized_array_ref (se, NULL, true);
3db5d687 3776 gfc_advance_se_ss_chain (se);
6de9cd9a
DN
3777}
3778
428f80e6
RG
3779/* Add T to the offset pair *OFFSET, *CST_OFFSET. */
3780
3781static void
3782add_to_offset (tree *cst_offset, tree *offset, tree t)
3783{
3784 if (TREE_CODE (t) == INTEGER_CST)
3785 *cst_offset = int_const_binop (PLUS_EXPR, *cst_offset, t);
3786 else
3787 {
3788 if (!integer_zerop (*offset))
3789 *offset = fold_build2_loc (input_location, PLUS_EXPR,
3790 gfc_array_index_type, *offset, t);
3791 else
3792 *offset = t;
3793 }
3794}
6de9cd9a 3795
8f75db9f
PT
3796
3797static tree
f3b0bb7a 3798build_array_ref (tree desc, tree offset, tree decl, tree vptr)
8f75db9f
PT
3799{
3800 tree tmp;
f04986a9 3801 tree type;
ff3598bc 3802 tree cdesc;
f3b0bb7a
AV
3803
3804 /* For class arrays the class declaration is stored in the saved
3805 descriptor. */
3806 if (INDIRECT_REF_P (desc)
3807 && DECL_LANG_SPECIFIC (TREE_OPERAND (desc, 0))
3808 && GFC_DECL_SAVED_DESCRIPTOR (TREE_OPERAND (desc, 0)))
ff3598bc 3809 cdesc = gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (
f3b0bb7a
AV
3810 TREE_OPERAND (desc, 0)));
3811 else
ff3598bc 3812 cdesc = desc;
8f75db9f 3813
f04986a9
PT
3814 /* Class container types do not always have the GFC_CLASS_TYPE_P
3815 but the canonical type does. */
ff3598bc
PT
3816 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (cdesc))
3817 && TREE_CODE (cdesc) == COMPONENT_REF)
8f75db9f 3818 {
ff3598bc 3819 type = TREE_TYPE (TREE_OPERAND (cdesc, 0));
f04986a9
PT
3820 if (TYPE_CANONICAL (type)
3821 && GFC_CLASS_TYPE_P (TYPE_CANONICAL (type)))
ff3598bc 3822 vptr = gfc_class_vptr_get (TREE_OPERAND (cdesc, 0));
8f75db9f
PT
3823 }
3824
f04986a9
PT
3825 tmp = gfc_conv_array_data (desc);
3826 tmp = build_fold_indirect_ref_loc (input_location, tmp);
7964ab6c
MM
3827 tmp = gfc_build_array_ref (tmp, offset, decl,
3828 non_negative_strides_array_p (desc),
3829 vptr);
8f75db9f
PT
3830 return tmp;
3831}
3832
3833
6de9cd9a
DN
3834/* Build an array reference. se->expr already holds the array descriptor.
3835 This should be either a variable, indirect variable reference or component
3836 reference. For arrays which do not have a descriptor, se->expr will be
3837 the data pointer.
3838 a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
3839
3840void
31f02c77 3841gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_expr *expr,
dd18a33b 3842 locus * where)
6de9cd9a
DN
3843{
3844 int n;
428f80e6 3845 tree offset, cst_offset;
6de9cd9a
DN
3846 tree tmp;
3847 tree stride;
ff3598bc 3848 tree decl = NULL_TREE;
6de9cd9a 3849 gfc_se indexse;
59e36b72 3850 gfc_se tmpse;
31f02c77
TB
3851 gfc_symbol * sym = expr->symtree->n.sym;
3852 char *var_name = NULL;
6de9cd9a 3853
d3a9eea2 3854 if (ar->dimen == 0)
4409de24 3855 {
56b070e3
PT
3856 gcc_assert (ar->codimen || sym->attr.select_rank_temporary
3857 || (ar->as && ar->as->corank));
b8ff4e88 3858
badd9e69
TB
3859 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
3860 se->expr = build_fold_indirect_ref (gfc_conv_array_data (se->expr));
3861 else
3862 {
3863 if (GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr))
3864 && TREE_CODE (TREE_TYPE (se->expr)) == POINTER_TYPE)
3865 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
0c53708e 3866
1cc0e193 3867 /* Use the actual tree type and not the wrapped coarray. */
0c53708e
TB
3868 if (!se->want_pointer)
3869 se->expr = fold_convert (TYPE_MAIN_VARIANT (TREE_TYPE (se->expr)),
3870 se->expr);
badd9e69
TB
3871 }
3872
4409de24
TB
3873 return;
3874 }
d3a9eea2 3875
e7dc5b4f 3876 /* Handle scalarized references separately. */
6de9cd9a
DN
3877 if (ar->type != AR_ELEMENT)
3878 {
3879 gfc_conv_scalarized_array_ref (se, ar);
068e7338 3880 gfc_advance_se_ss_chain (se);
6de9cd9a
DN
3881 return;
3882 }
3883
31f02c77
TB
3884 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3885 {
3886 size_t len;
3887 gfc_ref *ref;
3888
3889 len = strlen (sym->name) + 1;
3890 for (ref = expr->ref; ref; ref = ref->next)
3891 {
3892 if (ref->type == REF_ARRAY && &ref->u.ar == ar)
3893 break;
3894 if (ref->type == REF_COMPONENT)
7b11fbb8 3895 len += 2 + strlen (ref->u.c.component->name);
31f02c77
TB
3896 }
3897
3898 var_name = XALLOCAVEC (char, len);
3899 strcpy (var_name, sym->name);
3900
3901 for (ref = expr->ref; ref; ref = ref->next)
3902 {
3903 if (ref->type == REF_ARRAY && &ref->u.ar == ar)
3904 break;
3905 if (ref->type == REF_COMPONENT)
3906 {
3907 strcat (var_name, "%%");
3908 strcat (var_name, ref->u.c.component->name);
3909 }
3910 }
3911 }
3912
2ee70f5d
JRFS
3913 decl = se->expr;
3914 if (IS_CLASS_ARRAY (sym) && sym->attr.dummy && ar->as->type != AS_DEFERRED)
3915 decl = sym->backend_decl;
3916
428f80e6 3917 cst_offset = offset = gfc_index_zero_node;
2ee70f5d 3918 add_to_offset (&cst_offset, &offset, gfc_conv_array_offset (decl));
6de9cd9a 3919
428f80e6
RG
3920 /* Calculate the offsets from all the dimensions. Make sure to associate
3921 the final offset so that we form a chain of loop invariant summands. */
3922 for (n = ar->dimen - 1; n >= 0; n--)
6de9cd9a 3923 {
1f2959f0 3924 /* Calculate the index for this dimension. */
068e7338 3925 gfc_init_se (&indexse, se);
6de9cd9a
DN
3926 gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
3927 gfc_add_block_to_block (&se->pre, &indexse.pre);
3928
980fa45e 3929 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && ! expr->no_bounds_check)
6de9cd9a
DN
3930 {
3931 /* Check array bounds. */
3932 tree cond;
dd18a33b 3933 char *msg;
6de9cd9a 3934
a90552d5
FXC
3935 /* Evaluate the indexse.expr only once. */
3936 indexse.expr = save_expr (indexse.expr);
3937
c099916d 3938 /* Lower bound. */
2ee70f5d 3939 tmp = gfc_conv_array_lbound (decl, n);
59e36b72
PT
3940 if (sym->attr.temporary)
3941 {
3942 gfc_init_se (&tmpse, se);
3943 gfc_conv_expr_type (&tmpse, ar->as->lower[n],
3944 gfc_array_index_type);
3945 gfc_add_block_to_block (&se->pre, &tmpse.pre);
3946 tmp = tmpse.expr;
3947 }
3948
63ee5404 3949 cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
94471a56 3950 indexse.expr, tmp);
1a33dc9e
UB
3951 msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3952 "below lower bound of %%ld", n+1, var_name);
0d52899f 3953 gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
c8fe94c7
FXC
3954 fold_convert (long_integer_type_node,
3955 indexse.expr),
3956 fold_convert (long_integer_type_node, tmp));
cede9502 3957 free (msg);
6de9cd9a 3958
c099916d
FXC
3959 /* Upper bound, but not for the last dimension of assumed-size
3960 arrays. */
b3aefde2 3961 if (n < ar->dimen - 1 || ar->as->type != AS_ASSUMED_SIZE)
c099916d 3962 {
2ee70f5d 3963 tmp = gfc_conv_array_ubound (decl, n);
59e36b72
PT
3964 if (sym->attr.temporary)
3965 {
3966 gfc_init_se (&tmpse, se);
3967 gfc_conv_expr_type (&tmpse, ar->as->upper[n],
3968 gfc_array_index_type);
3969 gfc_add_block_to_block (&se->pre, &tmpse.pre);
3970 tmp = tmpse.expr;
3971 }
3972
94471a56 3973 cond = fold_build2_loc (input_location, GT_EXPR,
63ee5404 3974 logical_type_node, indexse.expr, tmp);
1a33dc9e
UB
3975 msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3976 "above upper bound of %%ld", n+1, var_name);
0d52899f 3977 gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
c8fe94c7
FXC
3978 fold_convert (long_integer_type_node,
3979 indexse.expr),
3980 fold_convert (long_integer_type_node, tmp));
cede9502 3981 free (msg);
c099916d 3982 }
6de9cd9a
DN
3983 }
3984
3985 /* Multiply the index by the stride. */
2ee70f5d 3986 stride = gfc_conv_array_stride (decl, n);
94471a56
TB
3987 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
3988 indexse.expr, stride);
6de9cd9a
DN
3989
3990 /* And add it to the total. */
428f80e6 3991 add_to_offset (&cst_offset, &offset, tmp);
6de9cd9a
DN
3992 }
3993
428f80e6
RG
3994 if (!integer_zerop (cst_offset))
3995 offset = fold_build2_loc (input_location, PLUS_EXPR,
3996 gfc_array_index_type, offset, cst_offset);
1d6b7f39 3997
ff3598bc
PT
3998 /* A pointer array component can be detected from its field decl. Fix
3999 the descriptor, mark the resulting variable decl and pass it to
4000 build_array_ref. */
2ee70f5d 4001 decl = NULL_TREE;
0d78e4aa
PT
4002 if (get_CFI_desc (sym, expr, &decl, ar))
4003 decl = build_fold_indirect_ref_loc (input_location, decl);
ff3598bc
PT
4004 if (!expr->ts.deferred && !sym->attr.codimension
4005 && is_pointer_array (se->expr))
4006 {
4007 if (TREE_CODE (se->expr) == COMPONENT_REF)
4e227341 4008 decl = se->expr;
ff3598bc
PT
4009 else if (TREE_CODE (se->expr) == INDIRECT_REF)
4010 decl = TREE_OPERAND (se->expr, 0);
4011 else
4012 decl = se->expr;
4013 }
4014 else if (expr->ts.deferred
4015 || (sym->ts.type == BT_CHARACTER
4016 && sym->attr.select_type_temporary))
ba08c70a
PT
4017 {
4018 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
4019 {
4020 decl = se->expr;
4021 if (TREE_CODE (decl) == INDIRECT_REF)
4022 decl = TREE_OPERAND (decl, 0);
4023 }
4024 else
4025 decl = sym->backend_decl;
4026 }
ff3598bc 4027 else if (sym->ts.type == BT_CLASS)
fcc4891d
PT
4028 {
4029 if (UNLIMITED_POLY (sym))
4030 {
4031 gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
4032 gfc_init_se (&tmpse, NULL);
4033 gfc_conv_expr (&tmpse, class_expr);
4034 if (!se->class_vptr)
4035 se->class_vptr = gfc_class_vptr_get (tmpse.expr);
4036 gfc_free_expr (class_expr);
4037 decl = tmpse.expr;
4038 }
4039 else
4040 decl = NULL_TREE;
4041 }
ff3598bc
PT
4042
4043 se->expr = build_array_ref (se->expr, offset, decl, se->class_vptr);
6de9cd9a
DN
4044}
4045
4046
1190b611
MM
4047/* Add the offset corresponding to array's ARRAY_DIM dimension and loop's
4048 LOOP_DIM dimension (if any) to array's offset. */
4049
4050static void
4051add_array_offset (stmtblock_t *pblock, gfc_loopinfo *loop, gfc_ss *ss,
4052 gfc_array_ref *ar, int array_dim, int loop_dim)
4053{
4054 gfc_se se;
6d63e468 4055 gfc_array_info *info;
1190b611
MM
4056 tree stride, index;
4057
1838afec 4058 info = &ss->info->data.array;
1190b611
MM
4059
4060 gfc_init_se (&se, NULL);
4061 se.loop = loop;
4062 se.expr = info->descriptor;
4063 stride = gfc_conv_array_stride (info->descriptor, array_dim);
36e783e3 4064 index = conv_array_index_offset (&se, ss, array_dim, loop_dim, ar, stride);
1190b611
MM
4065 gfc_add_block_to_block (pblock, &se.pre);
4066
4067 info->offset = fold_build2_loc (input_location, PLUS_EXPR,
4068 gfc_array_index_type,
4069 info->offset, index);
4070 info->offset = gfc_evaluate_now (info->offset, pblock);
4071}
4072
4073
6de9cd9a
DN
4074/* Generate the code to be executed immediately before entering a
4075 scalarization loop. */
4076
4077static void
4078gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
4079 stmtblock_t * pblock)
4080{
6de9cd9a 4081 tree stride;
1838afec 4082 gfc_ss_info *ss_info;
6d63e468 4083 gfc_array_info *info;
bcc4d4e0 4084 gfc_ss_type ss_type;
8e24054b
MM
4085 gfc_ss *ss, *pss;
4086 gfc_loopinfo *ploop;
1fb35a90 4087 gfc_array_ref *ar;
6de9cd9a
DN
4088 int i;
4089
4090 /* This code will be executed before entering the scalarization loop
4091 for this dimension. */
4092 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4093 {
1838afec
MM
4094 ss_info = ss->info;
4095
7a412892 4096 if ((ss_info->useflags & flag) == 0)
6de9cd9a
DN
4097 continue;
4098
1838afec 4099 ss_type = ss_info->type;
bcc4d4e0
MM
4100 if (ss_type != GFC_SS_SECTION
4101 && ss_type != GFC_SS_FUNCTION
4102 && ss_type != GFC_SS_CONSTRUCTOR
4103 && ss_type != GFC_SS_COMPONENT)
6de9cd9a
DN
4104 continue;
4105
1838afec 4106 info = &ss_info->data.array;
6de9cd9a 4107
cb4b9eae
MM
4108 gcc_assert (dim < ss->dimen);
4109 gcc_assert (ss->dimen == loop->dimen);
6de9cd9a 4110
1fb35a90 4111 if (info->ref)
7f6d568e 4112 ar = &info->ref->u.ar;
1fb35a90 4113 else
7f6d568e
MM
4114 ar = NULL;
4115
8e24054b
MM
4116 if (dim == loop->dimen - 1 && loop->parent != NULL)
4117 {
4118 /* If we are in the outermost dimension of this loop, the previous
4119 dimension shall be in the parent loop. */
4120 gcc_assert (ss->parent != NULL);
4121
4122 pss = ss->parent;
4123 ploop = loop->parent;
4124
4125 /* ss and ss->parent are about the same array. */
4126 gcc_assert (ss_info == pss->info);
4127 }
4128 else
4129 {
4130 ploop = loop;
4131 pss = ss;
4132 }
4133
e2b3e6bd 4134 if (dim == loop->dimen - 1)
4f9a70fa
MM
4135 i = 0;
4136 else
4137 i = dim + 1;
1fb35a90 4138
7f6d568e 4139 /* For the time being, there is no loop reordering. */
8e24054b
MM
4140 gcc_assert (i == ploop->order[i]);
4141 i = ploop->order[i];
1fb35a90 4142
8e24054b 4143 if (dim == loop->dimen - 1 && loop->parent == NULL)
6de9cd9a 4144 {
8e24054b
MM
4145 stride = gfc_conv_array_stride (info->descriptor,
4146 innermost_ss (ss)->dim[i]);
bee1695c
MM
4147
4148 /* Calculate the stride of the innermost loop. Hopefully this will
4149 allow the backend optimizers to do their stuff more effectively.
4150 */
4151 info->stride0 = gfc_evaluate_now (stride, pblock);
4152
6de9cd9a
DN
4153 /* For the outermost loop calculate the offset due to any
4154 elemental dimensions. It will have been initialized with the
4155 base offset of the array. */
4156 if (info->ref)
4157 {
1fb35a90 4158 for (i = 0; i < ar->dimen; i++)
6de9cd9a 4159 {
1fb35a90 4160 if (ar->dimen_type[i] != DIMEN_ELEMENT)
6de9cd9a
DN
4161 continue;
4162
1190b611 4163 add_array_offset (pblock, loop, ss, ar, i, /* unused */ -1);
6de9cd9a 4164 }
6de9cd9a 4165 }
6de9cd9a
DN
4166 }
4167 else
1190b611 4168 /* Add the offset for the previous loop dimension. */
8e24054b 4169 add_array_offset (pblock, ploop, ss, ar, pss->dim[i], i);
6de9cd9a 4170
e7dc5b4f 4171 /* Remember this offset for the second loop. */
8e24054b 4172 if (dim == loop->temp_dim - 1 && loop->parent == NULL)
6de9cd9a
DN
4173 info->saved_offset = info->offset;
4174 }
4175}
4176
4177
4178/* Start a scalarized expression. Creates a scope and declares loop
4179 variables. */
4180
4181void
4182gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
4183{
4184 int dim;
4185 int n;
4186 int flags;
4187
6e45f57b 4188 gcc_assert (!loop->array_parameter);
6de9cd9a 4189
c6d741b8 4190 for (dim = loop->dimen - 1; dim >= 0; dim--)
6de9cd9a
DN
4191 {
4192 n = loop->order[dim];
4193
4194 gfc_start_block (&loop->code[n]);
4195
4196 /* Create the loop variable. */
4197 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
4198
4199 if (dim < loop->temp_dim)
4200 flags = 3;
4201 else
4202 flags = 1;
4203 /* Calculate values that will be constant within this loop. */
4204 gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
4205 }
4206 gfc_start_block (pbody);
4207}
4208
4209
4210/* Generates the actual loop code for a scalarization loop. */
4211
a470bfcc 4212static void
6de9cd9a
DN
4213gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
4214 stmtblock_t * pbody)
4215{
4216 stmtblock_t block;
4217 tree cond;
4218 tree tmp;
4219 tree loopbody;
4220 tree exit_label;
34d01e1d
VL
4221 tree stmt;
4222 tree init;
4223 tree incr;
6de9cd9a 4224
57bf3072
JJ
4225 if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS
4226 | OMPWS_SCALARIZER_BODY))
34d01e1d
VL
4227 == (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS)
4228 && n == loop->dimen - 1)
4229 {
4230 /* We create an OMP_FOR construct for the outermost scalarized loop. */
4231 init = make_tree_vec (1);
4232 cond = make_tree_vec (1);
4233 incr = make_tree_vec (1);
4234
4235 /* Cycle statement is implemented with a goto. Exit statement must not
4236 be present for this loop. */
4237 exit_label = gfc_build_label_decl (NULL_TREE);
4238 TREE_USED (exit_label) = 1;
4239
4240 /* Label for cycle statements (if needed). */
4241 tmp = build1_v (LABEL_EXPR, exit_label);
4242 gfc_add_expr_to_block (pbody, tmp);
4243
4244 stmt = make_node (OMP_FOR);
4245
4246 TREE_TYPE (stmt) = void_type_node;
4247 OMP_FOR_BODY (stmt) = loopbody = gfc_finish_block (pbody);
4248
c2255bc4
AH
4249 OMP_FOR_CLAUSES (stmt) = build_omp_clause (input_location,
4250 OMP_CLAUSE_SCHEDULE);
34d01e1d
VL
4251 OMP_CLAUSE_SCHEDULE_KIND (OMP_FOR_CLAUSES (stmt))
4252 = OMP_CLAUSE_SCHEDULE_STATIC;
4253 if (ompws_flags & OMPWS_NOWAIT)
4254 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (stmt))
c2255bc4 4255 = build_omp_clause (input_location, OMP_CLAUSE_NOWAIT);
34d01e1d
VL
4256
4257 /* Initialize the loopvar. */
4258 TREE_VEC_ELT (init, 0) = build2_v (MODIFY_EXPR, loop->loopvar[n],
4259 loop->from[n]);
4260 OMP_FOR_INIT (stmt) = init;
4261 /* The exit condition. */
5d44e5c8 4262 TREE_VEC_ELT (cond, 0) = build2_loc (input_location, LE_EXPR,
63ee5404 4263 logical_type_node,
5d44e5c8
TB
4264 loop->loopvar[n], loop->to[n]);
4265 SET_EXPR_LOCATION (TREE_VEC_ELT (cond, 0), input_location);
34d01e1d
VL
4266 OMP_FOR_COND (stmt) = cond;
4267 /* Increment the loopvar. */
5d44e5c8
TB
4268 tmp = build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4269 loop->loopvar[n], gfc_index_one_node);
94471a56 4270 TREE_VEC_ELT (incr, 0) = fold_build2_loc (input_location, MODIFY_EXPR,
34d01e1d
VL
4271 void_type_node, loop->loopvar[n], tmp);
4272 OMP_FOR_INCR (stmt) = incr;
4273
4274 ompws_flags &= ~OMPWS_CURR_SINGLEUNIT;
4275 gfc_add_expr_to_block (&loop->code[n], stmt);
4276 }
4277 else
4278 {
3d03ead0
PT
4279 bool reverse_loop = (loop->reverse[n] == GFC_REVERSE_SET)
4280 && (loop->temp_ss == NULL);
4281
34d01e1d 4282 loopbody = gfc_finish_block (pbody);
6de9cd9a 4283
3d03ead0 4284 if (reverse_loop)
fab27f52 4285 std::swap (loop->from[n], loop->to[n]);
3d03ead0 4286
34d01e1d 4287 /* Initialize the loopvar. */
80927a56
JJ
4288 if (loop->loopvar[n] != loop->from[n])
4289 gfc_add_modify (&loop->code[n], loop->loopvar[n], loop->from[n]);
6de9cd9a 4290
34d01e1d 4291 exit_label = gfc_build_label_decl (NULL_TREE);
6de9cd9a 4292
34d01e1d
VL
4293 /* Generate the loop body. */
4294 gfc_init_block (&block);
6de9cd9a 4295
34d01e1d 4296 /* The exit condition. */
94471a56 4297 cond = fold_build2_loc (input_location, reverse_loop ? LT_EXPR : GT_EXPR,
63ee5404 4298 logical_type_node, loop->loopvar[n], loop->to[n]);
34d01e1d
VL
4299 tmp = build1_v (GOTO_EXPR, exit_label);
4300 TREE_USED (exit_label) = 1;
c2255bc4 4301 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
34d01e1d 4302 gfc_add_expr_to_block (&block, tmp);
6de9cd9a 4303
34d01e1d
VL
4304 /* The main body. */
4305 gfc_add_expr_to_block (&block, loopbody);
6de9cd9a 4306
34d01e1d 4307 /* Increment the loopvar. */
94471a56
TB
4308 tmp = fold_build2_loc (input_location,
4309 reverse_loop ? MINUS_EXPR : PLUS_EXPR,
4310 gfc_array_index_type, loop->loopvar[n],
4311 gfc_index_one_node);
3d03ead0 4312
34d01e1d 4313 gfc_add_modify (&block, loop->loopvar[n], tmp);
6de9cd9a 4314
34d01e1d
VL
4315 /* Build the loop. */
4316 tmp = gfc_finish_block (&block);
4317 tmp = build1_v (LOOP_EXPR, tmp);
4318 gfc_add_expr_to_block (&loop->code[n], tmp);
4319
4320 /* Add the exit label. */
4321 tmp = build1_v (LABEL_EXPR, exit_label);
4322 gfc_add_expr_to_block (&loop->code[n], tmp);
4323 }
6de9cd9a 4324
6de9cd9a
DN
4325}
4326
4327
4328/* Finishes and generates the loops for a scalarized expression. */
4329
4330void
4331gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
4332{
4333 int dim;
4334 int n;
4335 gfc_ss *ss;
4336 stmtblock_t *pblock;
4337 tree tmp;
4338
4339 pblock = body;
4340 /* Generate the loops. */
c6d741b8 4341 for (dim = 0; dim < loop->dimen; dim++)
6de9cd9a
DN
4342 {
4343 n = loop->order[dim];
4344 gfc_trans_scalarized_loop_end (loop, n, pblock);
4345 loop->loopvar[n] = NULL_TREE;
4346 pblock = &loop->code[n];
4347 }
4348
4349 tmp = gfc_finish_block (pblock);
4350 gfc_add_expr_to_block (&loop->pre, tmp);
4351
4352 /* Clear all the used flags. */
39abb03c 4353 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2eace29a
MM
4354 if (ss->parent == NULL)
4355 ss->info->useflags = 0;
6de9cd9a
DN
4356}
4357
4358
4359/* Finish the main body of a scalarized expression, and start the secondary
4360 copying body. */
4361
4362void
4363gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
4364{
4365 int dim;
4366 int n;
4367 stmtblock_t *pblock;
4368 gfc_ss *ss;
4369
4370 pblock = body;
4371 /* We finish as many loops as are used by the temporary. */
4372 for (dim = 0; dim < loop->temp_dim - 1; dim++)
4373 {
4374 n = loop->order[dim];
4375 gfc_trans_scalarized_loop_end (loop, n, pblock);
4376 loop->loopvar[n] = NULL_TREE;
4377 pblock = &loop->code[n];
4378 }
4379
4380 /* We don't want to finish the outermost loop entirely. */
4381 n = loop->order[loop->temp_dim - 1];
4382 gfc_trans_scalarized_loop_end (loop, n, pblock);
4383
4384 /* Restore the initial offsets. */
4385 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4386 {
bcc4d4e0 4387 gfc_ss_type ss_type;
1838afec
MM
4388 gfc_ss_info *ss_info;
4389
4390 ss_info = ss->info;
bcc4d4e0 4391
7a412892 4392 if ((ss_info->useflags & 2) == 0)
6de9cd9a
DN
4393 continue;
4394
1838afec 4395 ss_type = ss_info->type;
bcc4d4e0
MM
4396 if (ss_type != GFC_SS_SECTION
4397 && ss_type != GFC_SS_FUNCTION
4398 && ss_type != GFC_SS_CONSTRUCTOR
4399 && ss_type != GFC_SS_COMPONENT)
6de9cd9a
DN
4400 continue;
4401
1838afec 4402 ss_info->data.array.offset = ss_info->data.array.saved_offset;
6de9cd9a
DN
4403 }
4404
4405 /* Restart all the inner loops we just finished. */
4406 for (dim = loop->temp_dim - 2; dim >= 0; dim--)
4407 {
4408 n = loop->order[dim];
4409
4410 gfc_start_block (&loop->code[n]);
4411
4412 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
4413
4414 gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
4415 }
4416
4417 /* Start a block for the secondary copying code. */
4418 gfc_start_block (body);
4419}
4420
4421
287b3dd2
MM
4422/* Precalculate (either lower or upper) bound of an array section.
4423 BLOCK: Block in which the (pre)calculation code will go.
4424 BOUNDS[DIM]: Where the bound value will be stored once evaluated.
4425 VALUES[DIM]: Specified bound (NULL <=> unspecified).
4426 DESC: Array descriptor from which the bound will be picked if unspecified
4427 (either lower or upper bound according to LBOUND). */
4428
4429static void
4430evaluate_bound (stmtblock_t *block, tree *bounds, gfc_expr ** values,
97561cdc 4431 tree desc, int dim, bool lbound, bool deferred)
287b3dd2
MM
4432{
4433 gfc_se se;
4434 gfc_expr * input_val = values[dim];
4435 tree *output = &bounds[dim];
4436
4437
4438 if (input_val)
4439 {
4440 /* Specified section bound. */
4441 gfc_init_se (&se, NULL);
4442 gfc_conv_expr_type (&se, input_val, gfc_array_index_type);
4443 gfc_add_block_to_block (block, &se.pre);
4444 *output = se.expr;
4445 }
591bb5e4 4446 else if (deferred && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
97561cdc
AV
4447 {
4448 /* The gfc_conv_array_lbound () routine returns a constant zero for
591bb5e4 4449 deferred length arrays, which in the scalarizer wreaks havoc, when
97561cdc
AV
4450 copying to a (newly allocated) one-based array.
4451 Keep returning the actual result in sync for both bounds. */
4452 *output = lbound ? gfc_conv_descriptor_lbound_get (desc,
4453 gfc_rank_cst[dim]):
4454 gfc_conv_descriptor_ubound_get (desc,
4455 gfc_rank_cst[dim]);
4456 }
287b3dd2
MM
4457 else
4458 {
4459 /* No specific bound specified so use the bound of the array. */
4460 *output = lbound ? gfc_conv_array_lbound (desc, dim) :
4461 gfc_conv_array_ubound (desc, dim);
4462 }
4463 *output = gfc_evaluate_now (*output, block);
4464}
4465
4466
6de9cd9a
DN
4467/* Calculate the lower bound of an array section. */
4468
4469static void
cf664522 4470gfc_conv_section_startstride (stmtblock_t * block, gfc_ss * ss, int dim)
6de9cd9a 4471{
a3935ffc 4472 gfc_expr *stride = NULL;
6de9cd9a
DN
4473 tree desc;
4474 gfc_se se;
6d63e468 4475 gfc_array_info *info;
3ca39858 4476 gfc_array_ref *ar;
6de9cd9a 4477
bcc4d4e0 4478 gcc_assert (ss->info->type == GFC_SS_SECTION);
6de9cd9a 4479
1838afec 4480 info = &ss->info->data.array;
3ca39858 4481 ar = &info->ref->u.ar;
6de9cd9a 4482
3ca39858 4483 if (ar->dimen_type[dim] == DIMEN_VECTOR)
6de9cd9a 4484 {
7a70c12d 4485 /* We use a zero-based index to access the vector. */
9157ccb2 4486 info->start[dim] = gfc_index_zero_node;
9157ccb2 4487 info->end[dim] = NULL;
065c6f9d 4488 info->stride[dim] = gfc_index_one_node;
7a70c12d 4489 return;
6de9cd9a
DN
4490 }
4491
b0ac6998
MM
4492 gcc_assert (ar->dimen_type[dim] == DIMEN_RANGE
4493 || ar->dimen_type[dim] == DIMEN_THIS_IMAGE);
7a70c12d 4494 desc = info->descriptor;
065c6f9d 4495 stride = ar->stride[dim];
6de9cd9a 4496
97561cdc 4497
6de9cd9a
DN
4498 /* Calculate the start of the range. For vector subscripts this will
4499 be the range of the vector. */
97561cdc
AV
4500 evaluate_bound (block, info->start, ar->start, desc, dim, true,
4501 ar->as->type == AS_DEFERRED);
6de9cd9a 4502
8424e0d8
PT
4503 /* Similarly calculate the end. Although this is not used in the
4504 scalarizer, it is needed when checking bounds and where the end
4505 is an expression with side-effects. */
97561cdc
AV
4506 evaluate_bound (block, info->end, ar->end, desc, dim, false,
4507 ar->as->type == AS_DEFERRED);
4508
8424e0d8 4509
6de9cd9a 4510 /* Calculate the stride. */
065c6f9d 4511 if (stride == NULL)
9157ccb2 4512 info->stride[dim] = gfc_index_one_node;
065c6f9d 4513 else
6de9cd9a
DN
4514 {
4515 gfc_init_se (&se, NULL);
4516 gfc_conv_expr_type (&se, stride, gfc_array_index_type);
cf664522
MM
4517 gfc_add_block_to_block (block, &se.pre);
4518 info->stride[dim] = gfc_evaluate_now (se.expr, block);
6de9cd9a
DN
4519 }
4520}
4521
4522
4523/* Calculates the range start and stride for a SS chain. Also gets the
4524 descriptor and data pointer. The range of vector subscripts is the size
4525 of the vector. Array bounds are also checked. */
4526
4527void
4528gfc_conv_ss_startstride (gfc_loopinfo * loop)
4529{
4530 int n;
4531 tree tmp;
4532 gfc_ss *ss;
6de9cd9a
DN
4533 tree desc;
4534
1f65468a
MM
4535 gfc_loopinfo * const outer_loop = outermost_loop (loop);
4536
6de9cd9a
DN
4537 loop->dimen = 0;
4538 /* Determine the rank of the loop. */
199c387d 4539 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
6de9cd9a 4540 {
bcc4d4e0 4541 switch (ss->info->type)
6de9cd9a
DN
4542 {
4543 case GFC_SS_SECTION:
4544 case GFC_SS_CONSTRUCTOR:
4545 case GFC_SS_FUNCTION:
e9cfef64 4546 case GFC_SS_COMPONENT:
cb4b9eae 4547 loop->dimen = ss->dimen;
199c387d 4548 goto done;
6de9cd9a 4549
f5f701ad
PT
4550 /* As usual, lbound and ubound are exceptions!. */
4551 case GFC_SS_INTRINSIC:
f98cfd3c 4552 switch (ss->info->expr->value.function.isym->id)
f5f701ad
PT
4553 {
4554 case GFC_ISYM_LBOUND:
4555 case GFC_ISYM_UBOUND:
a3935ffc
TB
4556 case GFC_ISYM_LCOBOUND:
4557 case GFC_ISYM_UCOBOUND:
1af78e73 4558 case GFC_ISYM_SHAPE:
a3935ffc 4559 case GFC_ISYM_THIS_IMAGE:
cb4b9eae 4560 loop->dimen = ss->dimen;
199c387d 4561 goto done;
f5f701ad
PT
4562
4563 default:
4564 break;
4565 }
4566
6de9cd9a
DN
4567 default:
4568 break;
4569 }
4570 }
4571
ca39e6f2
FXC
4572 /* We should have determined the rank of the expression by now. If
4573 not, that's bad news. */
199c387d 4574 gcc_unreachable ();
6de9cd9a 4575
199c387d 4576done:
13413760 4577 /* Loop over all the SS in the chain. */
6de9cd9a
DN
4578 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4579 {
f98cfd3c 4580 gfc_ss_info *ss_info;
08dcec61 4581 gfc_array_info *info;
f98cfd3c 4582 gfc_expr *expr;
08dcec61 4583
f98cfd3c
MM
4584 ss_info = ss->info;
4585 expr = ss_info->expr;
1838afec 4586 info = &ss_info->data.array;
08dcec61 4587
f98cfd3c
MM
4588 if (expr && expr->shape && !info->shape)
4589 info->shape = expr->shape;
e9cfef64 4590
f98cfd3c 4591 switch (ss_info->type)
6de9cd9a
DN
4592 {
4593 case GFC_SS_SECTION:
30ae600f
MM
4594 /* Get the descriptor for the array. If it is a cross loops array,
4595 we got the descriptor already in the outermost loop. */
4596 if (ss->parent == NULL)
1f65468a
MM
4597 gfc_conv_ss_descriptor (&outer_loop->pre, ss,
4598 !loop->array_parameter);
6de9cd9a 4599
cb4b9eae 4600 for (n = 0; n < ss->dimen; n++)
1f65468a 4601 gfc_conv_section_startstride (&outer_loop->pre, ss, ss->dim[n]);
6de9cd9a
DN
4602 break;
4603
f5f701ad 4604 case GFC_SS_INTRINSIC:
f98cfd3c 4605 switch (expr->value.function.isym->id)
f5f701ad
PT
4606 {
4607 /* Fall through to supply start and stride. */
4608 case GFC_ISYM_LBOUND:
4609 case GFC_ISYM_UBOUND:
1af78e73
SL
4610 /* This is the variant without DIM=... */
4611 gcc_assert (expr->value.function.actual->next->expr == NULL);
4612 /* Fall through. */
4613
4614 case GFC_ISYM_SHAPE:
e5a24119
MM
4615 {
4616 gfc_expr *arg;
4617
e5a24119
MM
4618 arg = expr->value.function.actual->expr;
4619 if (arg->rank == -1)
4620 {
4621 gfc_se se;
4622 tree rank, tmp;
4623
4624 /* The rank (hence the return value's shape) is unknown,
4625 we have to retrieve it. */
4626 gfc_init_se (&se, NULL);
4627 se.descriptor_only = 1;
4628 gfc_conv_expr (&se, arg);
4629 /* This is a bare variable, so there is no preliminary
4630 or cleanup code. */
4631 gcc_assert (se.pre.head == NULL_TREE
4632 && se.post.head == NULL_TREE);
4633 rank = gfc_conv_descriptor_rank (se.expr);
4634 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4635 gfc_array_index_type,
4636 fold_convert (gfc_array_index_type,
4637 rank),
4638 gfc_index_one_node);
1f65468a 4639 info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
e5a24119
MM
4640 info->start[0] = gfc_index_zero_node;
4641 info->stride[0] = gfc_index_one_node;
4642 continue;
4643 }
4644 /* Otherwise fall through GFC_SS_FUNCTION. */
81fea426 4645 gcc_fallthrough ();
e5a24119 4646 }
a3935ffc
TB
4647 case GFC_ISYM_LCOBOUND:
4648 case GFC_ISYM_UCOBOUND:
4649 case GFC_ISYM_THIS_IMAGE:
f5f701ad 4650 break;
a3935ffc 4651
f5f701ad
PT
4652 default:
4653 continue;
4654 }
4655
191816a3 4656 /* FALLTHRU */
6de9cd9a
DN
4657 case GFC_SS_CONSTRUCTOR:
4658 case GFC_SS_FUNCTION:
cb4b9eae 4659 for (n = 0; n < ss->dimen; n++)
6de9cd9a 4660 {
cb4b9eae 4661 int dim = ss->dim[n];
ae9054ba 4662
1838afec
MM
4663 info->start[dim] = gfc_index_zero_node;
4664 info->end[dim] = gfc_index_zero_node;
4665 info->stride[dim] = gfc_index_one_node;
6de9cd9a
DN
4666 }
4667 break;
4668
4669 default:
4670 break;
4671 }
4672 }
4673
d1ecece9 4674 /* The rest is just runtime bounds checking. */
d3d3011f 4675 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
6de9cd9a
DN
4676 {
4677 stmtblock_t block;
ef31fe62 4678 tree lbound, ubound;
6de9cd9a
DN
4679 tree end;
4680 tree size[GFC_MAX_DIMENSIONS];
c6ec7cc6 4681 tree stride_pos, stride_neg, non_zerosized, tmp2, tmp3;
6d63e468 4682 gfc_array_info *info;
dd18a33b 4683 char *msg;
6de9cd9a
DN
4684 int dim;
4685
4686 gfc_start_block (&block);
4687
6de9cd9a
DN
4688 for (n = 0; n < loop->dimen; n++)
4689 size[n] = NULL_TREE;
4690
4691 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4692 {
ba4698e1 4693 stmtblock_t inner;
f98cfd3c
MM
4694 gfc_ss_info *ss_info;
4695 gfc_expr *expr;
4696 locus *expr_loc;
4697 const char *expr_name;
ba4698e1 4698
f98cfd3c
MM
4699 ss_info = ss->info;
4700 if (ss_info->type != GFC_SS_SECTION)
6de9cd9a
DN
4701 continue;
4702
597553ab 4703 /* Catch allocatable lhs in f2003. */
d1ecece9 4704 if (flag_realloc_lhs && ss->no_bounds_check)
597553ab
PT
4705 continue;
4706
f98cfd3c
MM
4707 expr = ss_info->expr;
4708 expr_loc = &expr->where;
4709 expr_name = expr->symtree->name;
4710
ba4698e1
FXC
4711 gfc_start_block (&inner);
4712
6de9cd9a 4713 /* TODO: range checking for mapped dimensions. */
1838afec 4714 info = &ss_info->data.array;
6de9cd9a 4715
7a70c12d
RS
4716 /* This code only checks ranges. Elemental and vector
4717 dimensions are checked later. */
6de9cd9a
DN
4718 for (n = 0; n < loop->dimen; n++)
4719 {
c099916d
FXC
4720 bool check_upper;
4721
cb4b9eae 4722 dim = ss->dim[n];
7a70c12d
RS
4723 if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
4724 continue;
c099916d 4725
1954a27b 4726 if (dim == info->ref->u.ar.dimen - 1
b3aefde2 4727 && info->ref->u.ar.as->type == AS_ASSUMED_SIZE)
c099916d
FXC
4728 check_upper = false;
4729 else
4730 check_upper = true;
ef31fe62
FXC
4731
4732 /* Zero stride is not allowed. */
63ee5404 4733 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
94471a56 4734 info->stride[dim], gfc_index_zero_node);
1a33dc9e
UB
4735 msg = xasprintf ("Zero stride is not allowed, for dimension %d "
4736 "of array '%s'", dim + 1, expr_name);
0d52899f 4737 gfc_trans_runtime_check (true, false, tmp, &inner,
f98cfd3c 4738 expr_loc, msg);
cede9502 4739 free (msg);
ef31fe62 4740
1838afec 4741 desc = info->descriptor;
c099916d 4742
e53b6e56 4743 /* This is the run-time equivalent of resolve.cc's
9157ccb2
MM
4744 check_dimension(). The logical is more readable there
4745 than it is here, with all the trees. */
c099916d 4746 lbound = gfc_conv_array_lbound (desc, dim);
9157ccb2 4747 end = info->end[dim];
c099916d
FXC
4748 if (check_upper)
4749 ubound = gfc_conv_array_ubound (desc, dim);
4750 else
4751 ubound = NULL;
4752
ef31fe62 4753 /* non_zerosized is true when the selected range is not
9157ccb2 4754 empty. */
94471a56 4755 stride_pos = fold_build2_loc (input_location, GT_EXPR,
63ee5404 4756 logical_type_node, info->stride[dim],
94471a56 4757 gfc_index_zero_node);
63ee5404 4758 tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
94471a56
TB
4759 info->start[dim], end);
4760 stride_pos = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404 4761 logical_type_node, stride_pos, tmp);
94471a56
TB
4762
4763 stride_neg = fold_build2_loc (input_location, LT_EXPR,
63ee5404 4764 logical_type_node,
94471a56 4765 info->stride[dim], gfc_index_zero_node);
63ee5404 4766 tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
94471a56
TB
4767 info->start[dim], end);
4768 stride_neg = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404 4769 logical_type_node,
94471a56
TB
4770 stride_neg, tmp);
4771 non_zerosized = fold_build2_loc (input_location, TRUTH_OR_EXPR,
63ee5404 4772 logical_type_node,
94471a56 4773 stride_pos, stride_neg);
ef31fe62
FXC
4774
4775 /* Check the start of the range against the lower and upper
f04986a9
PT
4776 bounds of the array, if the range is not empty.
4777 If upper bound is present, include both bounds in the
c6ec7cc6 4778 error message. */
c099916d
FXC
4779 if (check_upper)
4780 {
94471a56 4781 tmp = fold_build2_loc (input_location, LT_EXPR,
63ee5404 4782 logical_type_node,
94471a56
TB
4783 info->start[dim], lbound);
4784 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404 4785 logical_type_node,
94471a56
TB
4786 non_zerosized, tmp);
4787 tmp2 = fold_build2_loc (input_location, GT_EXPR,
63ee5404 4788 logical_type_node,
94471a56
TB
4789 info->start[dim], ubound);
4790 tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404 4791 logical_type_node,
94471a56 4792 non_zerosized, tmp2);
1a33dc9e
UB
4793 msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4794 "outside of expected range (%%ld:%%ld)",
4795 dim + 1, expr_name);
9157ccb2 4796 gfc_trans_runtime_check (true, false, tmp, &inner,
f98cfd3c 4797 expr_loc, msg,
9157ccb2
MM
4798 fold_convert (long_integer_type_node, info->start[dim]),
4799 fold_convert (long_integer_type_node, lbound),
c6ec7cc6 4800 fold_convert (long_integer_type_node, ubound));
9157ccb2 4801 gfc_trans_runtime_check (true, false, tmp2, &inner,
f98cfd3c 4802 expr_loc, msg,
9157ccb2
MM
4803 fold_convert (long_integer_type_node, info->start[dim]),
4804 fold_convert (long_integer_type_node, lbound),
c6ec7cc6 4805 fold_convert (long_integer_type_node, ubound));
cede9502 4806 free (msg);
c099916d 4807 }
c6ec7cc6
DW
4808 else
4809 {
94471a56 4810 tmp = fold_build2_loc (input_location, LT_EXPR,
63ee5404 4811 logical_type_node,
94471a56
TB
4812 info->start[dim], lbound);
4813 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404 4814 logical_type_node, non_zerosized, tmp);
1a33dc9e
UB
4815 msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4816 "below lower bound of %%ld",
4817 dim + 1, expr_name);
9157ccb2 4818 gfc_trans_runtime_check (true, false, tmp, &inner,
f98cfd3c 4819 expr_loc, msg,
9157ccb2 4820 fold_convert (long_integer_type_node, info->start[dim]),
c6ec7cc6 4821 fold_convert (long_integer_type_node, lbound));
cede9502 4822 free (msg);
c6ec7cc6 4823 }
f04986a9 4824
ef31fe62
FXC
4825 /* Compute the last element of the range, which is not
4826 necessarily "end" (think 0:5:3, which doesn't contain 5)
4827 and check it against both lower and upper bounds. */
c6ec7cc6 4828
94471a56
TB
4829 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4830 gfc_array_index_type, end,
4831 info->start[dim]);
4832 tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
4833 gfc_array_index_type, tmp,
4834 info->stride[dim]);
4835 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4836 gfc_array_index_type, end, tmp);
4837 tmp2 = fold_build2_loc (input_location, LT_EXPR,
63ee5404 4838 logical_type_node, tmp, lbound);
94471a56 4839 tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404 4840 logical_type_node, non_zerosized, tmp2);
c099916d
FXC
4841 if (check_upper)
4842 {
94471a56 4843 tmp3 = fold_build2_loc (input_location, GT_EXPR,
63ee5404 4844 logical_type_node, tmp, ubound);
94471a56 4845 tmp3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404 4846 logical_type_node, non_zerosized, tmp3);
1a33dc9e
UB
4847 msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4848 "outside of expected range (%%ld:%%ld)",
4849 dim + 1, expr_name);
c6ec7cc6 4850 gfc_trans_runtime_check (true, false, tmp2, &inner,
f98cfd3c 4851 expr_loc, msg,
c6ec7cc6 4852 fold_convert (long_integer_type_node, tmp),
f04986a9 4853 fold_convert (long_integer_type_node, ubound),
c6ec7cc6
DW
4854 fold_convert (long_integer_type_node, lbound));
4855 gfc_trans_runtime_check (true, false, tmp3, &inner,
f98cfd3c 4856 expr_loc, msg,
c6ec7cc6 4857 fold_convert (long_integer_type_node, tmp),
f04986a9 4858 fold_convert (long_integer_type_node, ubound),
c6ec7cc6 4859 fold_convert (long_integer_type_node, lbound));
cede9502 4860 free (msg);
c099916d 4861 }
c6ec7cc6
DW
4862 else
4863 {
1a33dc9e
UB
4864 msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4865 "below lower bound of %%ld",
4866 dim + 1, expr_name);
c6ec7cc6 4867 gfc_trans_runtime_check (true, false, tmp2, &inner,
f98cfd3c 4868 expr_loc, msg,
c6ec7cc6
DW
4869 fold_convert (long_integer_type_node, tmp),
4870 fold_convert (long_integer_type_node, lbound));
cede9502 4871 free (msg);
c6ec7cc6 4872 }
9157ccb2 4873
6de9cd9a 4874 /* Check the section sizes match. */
94471a56
TB
4875 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4876 gfc_array_index_type, end,
4877 info->start[dim]);
4878 tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
4879 gfc_array_index_type, tmp,
4880 info->stride[dim]);
4881 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4882 gfc_array_index_type,
4883 gfc_index_one_node, tmp);
4884 tmp = fold_build2_loc (input_location, MAX_EXPR,
4885 gfc_array_index_type, tmp,
4886 build_int_cst (gfc_array_index_type, 0));
6de9cd9a 4887 /* We remember the size of the first section, and check all the
9157ccb2 4888 others against this. */
6de9cd9a
DN
4889 if (size[n])
4890 {
94471a56 4891 tmp3 = fold_build2_loc (input_location, NE_EXPR,
63ee5404 4892 logical_type_node, tmp, size[n]);
1a33dc9e
UB
4893 msg = xasprintf ("Array bound mismatch for dimension %d "
4894 "of array '%s' (%%ld/%%ld)",
4895 dim + 1, expr_name);
6c559604 4896
0d52899f 4897 gfc_trans_runtime_check (true, false, tmp3, &inner,
f98cfd3c 4898 expr_loc, msg,
c8fe94c7
FXC
4899 fold_convert (long_integer_type_node, tmp),
4900 fold_convert (long_integer_type_node, size[n]));
6c559604 4901
cede9502 4902 free (msg);
6de9cd9a
DN
4903 }
4904 else
ba4698e1 4905 size[n] = gfc_evaluate_now (tmp, &inner);
6de9cd9a 4906 }
ba4698e1
FXC
4907
4908 tmp = gfc_finish_block (&inner);
4909
4910 /* For optional arguments, only check bounds if the argument is
4911 present. */
9d3a953e
HA
4912 if ((expr->symtree->n.sym->attr.optional
4913 || expr->symtree->n.sym->attr.not_always_present)
4914 && expr->symtree->n.sym->attr.dummy)
ba4698e1 4915 tmp = build3_v (COND_EXPR,
f98cfd3c 4916 gfc_conv_expr_present (expr->symtree->n.sym),
c2255bc4 4917 tmp, build_empty_stmt (input_location));
ba4698e1
FXC
4918
4919 gfc_add_expr_to_block (&block, tmp);
4920
6de9cd9a 4921 }
6de9cd9a
DN
4922
4923 tmp = gfc_finish_block (&block);
1f65468a 4924 gfc_add_expr_to_block (&outer_loop->pre, tmp);
6de9cd9a 4925 }
30ae600f
MM
4926
4927 for (loop = loop->nested; loop; loop = loop->next)
4928 gfc_conv_ss_startstride (loop);
6de9cd9a
DN
4929}
4930
ecb3baaa
TK
4931/* Return true if both symbols could refer to the same data object. Does
4932 not take account of aliasing due to equivalence statements. */
4933
4934static int
4935symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym, bool lsym_pointer,
4936 bool lsym_target, bool rsym_pointer, bool rsym_target)
4937{
4938 /* Aliasing isn't possible if the symbols have different base types. */
4939 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
4940 return 0;
4941
4942 /* Pointers can point to other pointers and target objects. */
4943
4944 if ((lsym_pointer && (rsym_pointer || rsym_target))
4945 || (rsym_pointer && (lsym_pointer || lsym_target)))
4946 return 1;
4947
4948 /* Special case: Argument association, cf. F90 12.4.1.6, F2003 12.4.1.7
4949 and F2008 12.5.2.13 items 3b and 4b. The pointer case (a) is already
4950 checked above. */
4951 if (lsym_target && rsym_target
4952 && ((lsym->attr.dummy && !lsym->attr.contiguous
4953 && (!lsym->attr.dimension || lsym->as->type == AS_ASSUMED_SHAPE))
4954 || (rsym->attr.dummy && !rsym->attr.contiguous
4955 && (!rsym->attr.dimension
4956 || rsym->as->type == AS_ASSUMED_SHAPE))))
4957 return 1;
4958
4959 return 0;
4960}
4961
6de9cd9a 4962
13795658 4963/* Return true if the two SS could be aliased, i.e. both point to the same data
6de9cd9a
DN
4964 object. */
4965/* TODO: resolve aliases based on frontend expressions. */
4966
4967static int
4968gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
4969{
4970 gfc_ref *lref;
4971 gfc_ref *rref;
f98cfd3c 4972 gfc_expr *lexpr, *rexpr;
6de9cd9a
DN
4973 gfc_symbol *lsym;
4974 gfc_symbol *rsym;
ecb3baaa 4975 bool lsym_pointer, lsym_target, rsym_pointer, rsym_target;
6de9cd9a 4976
f98cfd3c
MM
4977 lexpr = lss->info->expr;
4978 rexpr = rss->info->expr;
4979
4980 lsym = lexpr->symtree->n.sym;
4981 rsym = rexpr->symtree->n.sym;
ecb3baaa
TK
4982
4983 lsym_pointer = lsym->attr.pointer;
4984 lsym_target = lsym->attr.target;
4985 rsym_pointer = rsym->attr.pointer;
4986 rsym_target = rsym->attr.target;
4987
4988 if (symbols_could_alias (lsym, rsym, lsym_pointer, lsym_target,
4989 rsym_pointer, rsym_target))
6de9cd9a
DN
4990 return 1;
4991
272cec5d
TK
4992 if (rsym->ts.type != BT_DERIVED && rsym->ts.type != BT_CLASS
4993 && lsym->ts.type != BT_DERIVED && lsym->ts.type != BT_CLASS)
6de9cd9a
DN
4994 return 0;
4995
13413760 4996 /* For derived types we must check all the component types. We can ignore
6de9cd9a
DN
4997 array references as these will have the same base type as the previous
4998 component ref. */
1838afec 4999 for (lref = lexpr->ref; lref != lss->info->data.array.ref; lref = lref->next)
6de9cd9a
DN
5000 {
5001 if (lref->type != REF_COMPONENT)
5002 continue;
5003
ecb3baaa
TK
5004 lsym_pointer = lsym_pointer || lref->u.c.sym->attr.pointer;
5005 lsym_target = lsym_target || lref->u.c.sym->attr.target;
5006
5007 if (symbols_could_alias (lref->u.c.sym, rsym, lsym_pointer, lsym_target,
5008 rsym_pointer, rsym_target))
6de9cd9a
DN
5009 return 1;
5010
ecb3baaa
TK
5011 if ((lsym_pointer && (rsym_pointer || rsym_target))
5012 || (rsym_pointer && (lsym_pointer || lsym_target)))
5013 {
5014 if (gfc_compare_types (&lref->u.c.component->ts,
5015 &rsym->ts))
5016 return 1;
5017 }
5018
1838afec 5019 for (rref = rexpr->ref; rref != rss->info->data.array.ref;
6de9cd9a
DN
5020 rref = rref->next)
5021 {
5022 if (rref->type != REF_COMPONENT)
5023 continue;
5024
ecb3baaa
TK
5025 rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
5026 rsym_target = lsym_target || rref->u.c.sym->attr.target;
5027
5028 if (symbols_could_alias (lref->u.c.sym, rref->u.c.sym,
5029 lsym_pointer, lsym_target,
5030 rsym_pointer, rsym_target))
6de9cd9a 5031 return 1;
ecb3baaa
TK
5032
5033 if ((lsym_pointer && (rsym_pointer || rsym_target))
5034 || (rsym_pointer && (lsym_pointer || lsym_target)))
5035 {
5036 if (gfc_compare_types (&lref->u.c.component->ts,
5037 &rref->u.c.sym->ts))
5038 return 1;
5039 if (gfc_compare_types (&lref->u.c.sym->ts,
5040 &rref->u.c.component->ts))
5041 return 1;
5042 if (gfc_compare_types (&lref->u.c.component->ts,
5043 &rref->u.c.component->ts))
5044 return 1;
5045 }
6de9cd9a
DN
5046 }
5047 }
5048
ecb3baaa
TK
5049 lsym_pointer = lsym->attr.pointer;
5050 lsym_target = lsym->attr.target;
ecb3baaa 5051
1838afec 5052 for (rref = rexpr->ref; rref != rss->info->data.array.ref; rref = rref->next)
6de9cd9a
DN
5053 {
5054 if (rref->type != REF_COMPONENT)
5055 break;
5056
ecb3baaa
TK
5057 rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
5058 rsym_target = lsym_target || rref->u.c.sym->attr.target;
5059
5060 if (symbols_could_alias (rref->u.c.sym, lsym,
5061 lsym_pointer, lsym_target,
5062 rsym_pointer, rsym_target))
6de9cd9a 5063 return 1;
ecb3baaa
TK
5064
5065 if ((lsym_pointer && (rsym_pointer || rsym_target))
5066 || (rsym_pointer && (lsym_pointer || lsym_target)))
5067 {
5068 if (gfc_compare_types (&lsym->ts, &rref->u.c.component->ts))
5069 return 1;
5070 }
6de9cd9a
DN
5071 }
5072
5073 return 0;
5074}
5075
5076
5077/* Resolve array data dependencies. Creates a temporary if required. */
5078/* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
e53b6e56 5079 dependency.cc. */
6de9cd9a
DN
5080
5081void
5082gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
5083 gfc_ss * rss)
5084{
5085 gfc_ss *ss;
5086 gfc_ref *lref;
5087 gfc_ref *rref;
711d7c23 5088 gfc_ss_info *ss_info;
f98cfd3c
MM
5089 gfc_expr *dest_expr;
5090 gfc_expr *ss_expr;
6de9cd9a 5091 int nDepend = 0;
af804603 5092 int i, j;
6de9cd9a
DN
5093
5094 loop->temp_ss = NULL;
f98cfd3c 5095 dest_expr = dest->info->expr;
6de9cd9a
DN
5096
5097 for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
5098 {
711d7c23
MM
5099 ss_info = ss->info;
5100 ss_expr = ss_info->expr;
343ab492 5101
711d7c23 5102 if (ss_info->array_outer_dependency)
30c931de
PT
5103 {
5104 nDepend = 1;
5105 break;
5106 }
5107
711d7c23 5108 if (ss_info->type != GFC_SS_SECTION)
343ab492 5109 {
203c7ebf 5110 if (flag_realloc_lhs
343ab492
PT
5111 && dest_expr != ss_expr
5112 && gfc_is_reallocatable_lhs (dest_expr)
5113 && ss_expr->rank)
5114 nDepend = gfc_check_dependency (dest_expr, ss_expr, true);
6de9cd9a 5115
502b97e4
TK
5116 /* Check for cases like c(:)(1:2) = c(2)(2:3) */
5117 if (!nDepend && dest_expr->rank > 0
5118 && dest_expr->ts.type == BT_CHARACTER
5119 && ss_expr->expr_type == EXPR_VARIABLE)
1b961de9 5120
502b97e4
TK
5121 nDepend = gfc_check_dependency (dest_expr, ss_expr, false);
5122
711d7c23
MM
5123 if (ss_info->type == GFC_SS_REFERENCE
5124 && gfc_check_dependency (dest_expr, ss_expr, false))
5125 ss_info->data.scalar.needs_temporary = 1;
5126
213c3b7b
TK
5127 if (nDepend)
5128 break;
5129 else
5130 continue;
343ab492 5131 }
f98cfd3c
MM
5132
5133 if (dest_expr->symtree->n.sym != ss_expr->symtree->n.sym)
6de9cd9a 5134 {
7d1f1e61 5135 if (gfc_could_be_alias (dest, ss)
f98cfd3c 5136 || gfc_are_equivalenced_arrays (dest_expr, ss_expr))
7d1f1e61
PT
5137 {
5138 nDepend = 1;
5139 break;
5140 }
6de9cd9a 5141 }
7d1f1e61 5142 else
6de9cd9a 5143 {
f98cfd3c
MM
5144 lref = dest_expr->ref;
5145 rref = ss_expr->ref;
6de9cd9a 5146
3d03ead0
PT
5147 nDepend = gfc_dep_resolver (lref, rref, &loop->reverse[0]);
5148
4f06d65b
PT
5149 if (nDepend == 1)
5150 break;
af804603 5151
cb4b9eae
MM
5152 for (i = 0; i < dest->dimen; i++)
5153 for (j = 0; j < ss->dimen; j++)
af804603 5154 if (i != j
cb4b9eae 5155 && dest->dim[i] == ss->dim[j])
af804603
MM
5156 {
5157 /* If we don't access array elements in the same order,
5158 there is a dependency. */
5159 nDepend = 1;
5160 goto temporary;
5161 }
6de9cd9a
DN
5162#if 0
5163 /* TODO : loop shifting. */
5164 if (nDepend == 1)
5165 {
5166 /* Mark the dimensions for LOOP SHIFTING */
5167 for (n = 0; n < loop->dimen; n++)
5168 {
5169 int dim = dest->data.info.dim[n];
5170
5171 if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
5172 depends[n] = 2;
5173 else if (! gfc_is_same_range (&lref->u.ar,
5174 &rref->u.ar, dim, 0))
5175 depends[n] = 1;
5176 }
5177
13413760 5178 /* Put all the dimensions with dependencies in the
6de9cd9a
DN
5179 innermost loops. */
5180 dim = 0;
5181 for (n = 0; n < loop->dimen; n++)
5182 {
6e45f57b 5183 gcc_assert (loop->order[n] == n);
6de9cd9a
DN
5184 if (depends[n])
5185 loop->order[dim++] = n;
5186 }
6de9cd9a
DN
5187 for (n = 0; n < loop->dimen; n++)
5188 {
5189 if (! depends[n])
5190 loop->order[dim++] = n;
5191 }
5192
6e45f57b 5193 gcc_assert (dim == loop->dimen);
6de9cd9a
DN
5194 break;
5195 }
5196#endif
5197 }
5198 }
5199
af804603
MM
5200temporary:
5201
6de9cd9a
DN
5202 if (nDepend == 1)
5203 {
f98cfd3c 5204 tree base_type = gfc_typenode_for_spec (&dest_expr->ts);
eca18fb4
AP
5205 if (GFC_ARRAY_TYPE_P (base_type)
5206 || GFC_DESCRIPTOR_TYPE_P (base_type))
5207 base_type = gfc_get_element_type (base_type);
a0add3be 5208 loop->temp_ss = gfc_get_temp_ss (base_type, dest->info->string_length,
a1ae4f43 5209 loop->dimen);
6de9cd9a
DN
5210 gfc_add_ss_to_loop (loop, loop->temp_ss);
5211 }
5212 else
5213 loop->temp_ss = NULL;
5214}
5215
5216
1d9370e9
MM
5217/* Browse through each array's information from the scalarizer and set the loop
5218 bounds according to the "best" one (per dimension), i.e. the one which
eea58adb 5219 provides the most information (constant bounds, shape, etc.). */
6de9cd9a 5220
1d9370e9
MM
5221static void
5222set_loop_bounds (gfc_loopinfo *loop)
6de9cd9a 5223{
9157ccb2 5224 int n, dim, spec_dim;
6d63e468
MM
5225 gfc_array_info *info;
5226 gfc_array_info *specinfo;
1d9370e9 5227 gfc_ss *ss;
6de9cd9a 5228 tree tmp;
1d9370e9 5229 gfc_ss **loopspec;
ec25720b 5230 bool dynamic[GFC_MAX_DIMENSIONS];
6de9cd9a
DN
5231 mpz_t *cshape;
5232 mpz_t i;
478ad83d 5233 bool nonoptional_arr;
6de9cd9a 5234
1f65468a
MM
5235 gfc_loopinfo * const outer_loop = outermost_loop (loop);
5236
1d9370e9
MM
5237 loopspec = loop->specloop;
5238
6de9cd9a 5239 mpz_init (i);
c6d741b8 5240 for (n = 0; n < loop->dimen; n++)
6de9cd9a
DN
5241 {
5242 loopspec[n] = NULL;
ec25720b 5243 dynamic[n] = false;
478ad83d
TB
5244
5245 /* If there are both optional and nonoptional array arguments, scalarize
5246 over the nonoptional; otherwise, it does not matter as then all
5247 (optional) arrays have to be present per F2008, 125.2.12p3(6). */
5248
5249 nonoptional_arr = false;
5250
5251 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5252 if (ss->info->type != GFC_SS_SCALAR && ss->info->type != GFC_SS_TEMP
5253 && ss->info->type != GFC_SS_REFERENCE && !ss->info->can_be_null_ref)
502af491
PCC
5254 {
5255 nonoptional_arr = true;
5256 break;
5257 }
478ad83d 5258
6de9cd9a 5259 /* We use one SS term, and use that to determine the bounds of the
9157ccb2 5260 loop for this dimension. We try to pick the simplest term. */
6de9cd9a
DN
5261 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5262 {
596a9579
MM
5263 gfc_ss_type ss_type;
5264
bcc4d4e0 5265 ss_type = ss->info->type;
596a9579
MM
5266 if (ss_type == GFC_SS_SCALAR
5267 || ss_type == GFC_SS_TEMP
478ad83d
TB
5268 || ss_type == GFC_SS_REFERENCE
5269 || (ss->info->can_be_null_ref && nonoptional_arr))
9157ccb2
MM
5270 continue;
5271
1838afec 5272 info = &ss->info->data.array;
cb4b9eae 5273 dim = ss->dim[n];
9157ccb2
MM
5274
5275 if (loopspec[n] != NULL)
5276 {
1838afec 5277 specinfo = &loopspec[n]->info->data.array;
cb4b9eae 5278 spec_dim = loopspec[n]->dim[n];
9157ccb2
MM
5279 }
5280 else
5281 {
eea58adb 5282 /* Silence uninitialized warnings. */
9157ccb2
MM
5283 specinfo = NULL;
5284 spec_dim = 0;
5285 }
5286
08dcec61 5287 if (info->shape)
6de9cd9a
DN
5288 {
5289 /* The frontend has worked out the size for us. */
9157ccb2 5290 if (!loopspec[n]
08dcec61 5291 || !specinfo->shape
9157ccb2 5292 || !integer_zerop (specinfo->start[spec_dim]))
45bc572c
MM
5293 /* Prefer zero-based descriptors if possible. */
5294 loopspec[n] = ss;
6de9cd9a
DN
5295 continue;
5296 }
5297
bcc4d4e0 5298 if (ss_type == GFC_SS_CONSTRUCTOR)
6de9cd9a 5299 {
b7e75771 5300 gfc_constructor_base base;
e9cfef64 5301 /* An unknown size constructor will always be rank one.
40f20186 5302 Higher rank constructors will either have known shape,
e9cfef64 5303 or still be wrapped in a call to reshape. */
6e45f57b 5304 gcc_assert (loop->dimen == 1);
ec25720b
RS
5305
5306 /* Always prefer to use the constructor bounds if the size
5307 can be determined at compile time. Prefer not to otherwise,
5308 since the general case involves realloc, and it's better to
5309 avoid that overhead if possible. */
f98cfd3c 5310 base = ss->info->expr->value.constructor;
b7e75771 5311 dynamic[n] = gfc_get_array_constructor_size (&i, base);
ec25720b
RS
5312 if (!dynamic[n] || !loopspec[n])
5313 loopspec[n] = ss;
6de9cd9a
DN
5314 continue;
5315 }
5316
597553ab
PT
5317 /* Avoid using an allocatable lhs in an assignment, since
5318 there might be a reallocation coming. */
5319 if (loopspec[n] && ss->is_alloc_lhs)
5320 continue;
5321
9157ccb2 5322 if (!loopspec[n])
ec25720b 5323 loopspec[n] = ss;
6de9cd9a 5324 /* Criteria for choosing a loop specifier (most important first):
ec25720b 5325 doesn't need realloc
6de9cd9a
DN
5326 stride of one
5327 known stride
5328 known lower bound
5329 known upper bound
5330 */
96b2ffe1 5331 else if (loopspec[n]->info->type == GFC_SS_CONSTRUCTOR && dynamic[n])
6de9cd9a 5332 loopspec[n] = ss;
9157ccb2
MM
5333 else if (integer_onep (info->stride[dim])
5334 && !integer_onep (specinfo->stride[spec_dim]))
ec25720b 5335 loopspec[n] = ss;
9157ccb2
MM
5336 else if (INTEGER_CST_P (info->stride[dim])
5337 && !INTEGER_CST_P (specinfo->stride[spec_dim]))
ec25720b 5338 loopspec[n] = ss;
9157ccb2 5339 else if (INTEGER_CST_P (info->start[dim])
96b2ffe1
MM
5340 && !INTEGER_CST_P (specinfo->start[spec_dim])
5341 && integer_onep (info->stride[dim])
8f96b844 5342 == integer_onep (specinfo->stride[spec_dim])
96b2ffe1 5343 && INTEGER_CST_P (info->stride[dim])
8f96b844 5344 == INTEGER_CST_P (specinfo->stride[spec_dim]))
ec25720b
RS
5345 loopspec[n] = ss;
5346 /* We don't work out the upper bound.
5347 else if (INTEGER_CST_P (info->finish[n])
5348 && ! INTEGER_CST_P (specinfo->finish[n]))
5349 loopspec[n] = ss; */
6de9cd9a
DN
5350 }
5351
ca39e6f2
FXC
5352 /* We should have found the scalarization loop specifier. If not,
5353 that's bad news. */
5354 gcc_assert (loopspec[n]);
6de9cd9a 5355
1838afec 5356 info = &loopspec[n]->info->data.array;
cb4b9eae 5357 dim = loopspec[n]->dim[n];
6de9cd9a
DN
5358
5359 /* Set the extents of this range. */
08dcec61 5360 cshape = info->shape;
c6d741b8 5361 if (cshape && INTEGER_CST_P (info->start[dim])
9157ccb2 5362 && INTEGER_CST_P (info->stride[dim]))
6de9cd9a 5363 {
9157ccb2 5364 loop->from[n] = info->start[dim];
d6b3a0d7 5365 mpz_set (i, cshape[get_array_ref_dim_for_loop_dim (loopspec[n], n)]);
6de9cd9a
DN
5366 mpz_sub_ui (i, i, 1);
5367 /* To = from + (size - 1) * stride. */
5368 tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
9157ccb2 5369 if (!integer_onep (info->stride[dim]))
94471a56
TB
5370 tmp = fold_build2_loc (input_location, MULT_EXPR,
5371 gfc_array_index_type, tmp,
5372 info->stride[dim]);
5373 loop->to[n] = fold_build2_loc (input_location, PLUS_EXPR,
5374 gfc_array_index_type,
5375 loop->from[n], tmp);
6de9cd9a
DN
5376 }
5377 else
5378 {
9157ccb2 5379 loop->from[n] = info->start[dim];
bcc4d4e0 5380 switch (loopspec[n]->info->type)
6de9cd9a
DN
5381 {
5382 case GFC_SS_CONSTRUCTOR:
ec25720b
RS
5383 /* The upper bound is calculated when we expand the
5384 constructor. */
5385 gcc_assert (loop->to[n] == NULL_TREE);
6de9cd9a
DN
5386 break;
5387
5388 case GFC_SS_SECTION:
993ac38b
PT
5389 /* Use the end expression if it exists and is not constant,
5390 so that it is only evaluated once. */
9157ccb2 5391 loop->to[n] = info->end[dim];
6de9cd9a
DN
5392 break;
5393
859b6600 5394 case GFC_SS_FUNCTION:
fc90a8f2 5395 /* The loop bound will be set when we generate the call. */
859b6600
MM
5396 gcc_assert (loop->to[n] == NULL_TREE);
5397 break;
fc90a8f2 5398
e5a24119
MM
5399 case GFC_SS_INTRINSIC:
5400 {
5401 gfc_expr *expr = loopspec[n]->info->expr;
5402
5403 /* The {l,u}bound of an assumed rank. */
1af78e73
SL
5404 if (expr->value.function.isym->id == GFC_ISYM_SHAPE)
5405 gcc_assert (expr->value.function.actual->expr->rank == -1);
5406 else
5407 gcc_assert ((expr->value.function.isym->id == GFC_ISYM_LBOUND
5408 || expr->value.function.isym->id == GFC_ISYM_UBOUND)
5409 && expr->value.function.actual->next->expr == NULL
5410 && expr->value.function.actual->expr->rank == -1);
e5a24119
MM
5411
5412 loop->to[n] = info->end[dim];
5413 break;
5414 }
5415
276515e6
PT
5416 case GFC_SS_COMPONENT:
5417 {
5418 if (info->end[dim] != NULL_TREE)
5419 {
5420 loop->to[n] = info->end[dim];
5421 break;
5422 }
5423 else
5424 gcc_unreachable ();
5425 }
5426
6de9cd9a 5427 default:
6e45f57b 5428 gcc_unreachable ();
6de9cd9a
DN
5429 }
5430 }
5431
5432 /* Transform everything so we have a simple incrementing variable. */
3120f511 5433 if (integer_onep (info->stride[dim]))
9157ccb2 5434 info->delta[dim] = gfc_index_zero_node;
3120f511 5435 else
6de9cd9a
DN
5436 {
5437 /* Set the delta for this section. */
1f65468a 5438 info->delta[dim] = gfc_evaluate_now (loop->from[n], &outer_loop->pre);
6de9cd9a
DN
5439 /* Number of iterations is (end - start + step) / step.
5440 with start = 0, this simplifies to
5441 last = end / step;
5442 for (i = 0; i<=last; i++){...}; */
94471a56
TB
5443 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5444 gfc_array_index_type, loop->to[n],
5445 loop->from[n]);
5446 tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
5447 gfc_array_index_type, tmp, info->stride[dim]);
5448 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
5449 tmp, build_int_cst (gfc_array_index_type, -1));
1f65468a 5450 loop->to[n] = gfc_evaluate_now (tmp, &outer_loop->pre);
6de9cd9a 5451 /* Make the loop variable start at 0. */
7ab92584 5452 loop->from[n] = gfc_index_zero_node;
6de9cd9a
DN
5453 }
5454 }
1d9370e9 5455 mpz_clear (i);
30ae600f
MM
5456
5457 for (loop = loop->nested; loop; loop = loop->next)
5458 set_loop_bounds (loop);
1d9370e9
MM
5459}
5460
5461
1d9370e9
MM
5462/* Initialize the scalarization loop. Creates the loop variables. Determines
5463 the range of the loop variables. Creates a temporary if required.
5464 Also generates code for scalar expressions which have been
5465 moved outside the loop. */
5466
5467void
5468gfc_conv_loop_setup (gfc_loopinfo * loop, locus * where)
5469{
5470 gfc_ss *tmp_ss;
5471 tree tmp;
1d9370e9
MM
5472
5473 set_loop_bounds (loop);
6de9cd9a 5474
fc90a8f2
PB
5475 /* Add all the scalar code that can be taken out of the loops.
5476 This may include calculating the loop bounds, so do it before
5477 allocating the temporary. */
bdfd2ff0 5478 gfc_add_loop_ss_code (loop, loop->ss, false, where);
fc90a8f2 5479
cb4b9eae 5480 tmp_ss = loop->temp_ss;
6de9cd9a 5481 /* If we want a temporary then create it. */
cb4b9eae 5482 if (tmp_ss != NULL)
6de9cd9a 5483 {
bcc4d4e0
MM
5484 gfc_ss_info *tmp_ss_info;
5485
5486 tmp_ss_info = tmp_ss->info;
5487 gcc_assert (tmp_ss_info->type == GFC_SS_TEMP);
4616ef9b 5488 gcc_assert (loop->parent == NULL);
640670c7
PT
5489
5490 /* Make absolutely sure that this is a complete type. */
a0add3be 5491 if (tmp_ss_info->string_length)
961e73ac 5492 tmp_ss_info->data.temp.type
d393bbd7 5493 = gfc_get_character_type_len_for_eltype
961e73ac 5494 (TREE_TYPE (tmp_ss_info->data.temp.type),
a0add3be 5495 tmp_ss_info->string_length);
640670c7 5496
961e73ac 5497 tmp = tmp_ss_info->data.temp.type;
1838afec 5498 memset (&tmp_ss_info->data.array, 0, sizeof (gfc_array_info));
bcc4d4e0 5499 tmp_ss_info->type = GFC_SS_SECTION;
ffc3bba4 5500
cb4b9eae 5501 gcc_assert (tmp_ss->dimen != 0);
ffc3bba4 5502
41645793
MM
5503 gfc_trans_create_temp_array (&loop->pre, &loop->post, tmp_ss, tmp,
5504 NULL_TREE, false, true, false, where);
6de9cd9a
DN
5505 }
5506
6de9cd9a
DN
5507 /* For array parameters we don't have loop variables, so don't calculate the
5508 translations. */
121c82c9
MM
5509 if (!loop->array_parameter)
5510 gfc_set_delta (loop);
1d9370e9
MM
5511}
5512
5513
5514/* Calculates how to transform from loop variables to array indices for each
5515 array: once loop bounds are chosen, sets the difference (DELTA field) between
5516 loop bounds and array reference bounds, for each array info. */
5517
121c82c9
MM
5518void
5519gfc_set_delta (gfc_loopinfo *loop)
1d9370e9
MM
5520{
5521 gfc_ss *ss, **loopspec;
5522 gfc_array_info *info;
5523 tree tmp;
5524 int n, dim;
5525
1f65468a
MM
5526 gfc_loopinfo * const outer_loop = outermost_loop (loop);
5527
1d9370e9
MM
5528 loopspec = loop->specloop;
5529
6de9cd9a
DN
5530 /* Calculate the translation from loop variables to array indices. */
5531 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5532 {
bcc4d4e0 5533 gfc_ss_type ss_type;
45bc572c 5534
bcc4d4e0
MM
5535 ss_type = ss->info->type;
5536 if (ss_type != GFC_SS_SECTION
5537 && ss_type != GFC_SS_COMPONENT
5538 && ss_type != GFC_SS_CONSTRUCTOR)
6de9cd9a
DN
5539 continue;
5540
1838afec 5541 info = &ss->info->data.array;
6de9cd9a 5542
cb4b9eae 5543 for (n = 0; n < ss->dimen; n++)
6de9cd9a 5544 {
e9cfef64 5545 /* If we are specifying the range the delta is already set. */
6de9cd9a
DN
5546 if (loopspec[n] != ss)
5547 {
cb4b9eae 5548 dim = ss->dim[n];
9157ccb2 5549
6de9cd9a 5550 /* Calculate the offset relative to the loop variable.
9157ccb2 5551 First multiply by the stride. */
c96111c0 5552 tmp = loop->from[n];
9157ccb2 5553 if (!integer_onep (info->stride[dim]))
94471a56
TB
5554 tmp = fold_build2_loc (input_location, MULT_EXPR,
5555 gfc_array_index_type,
5556 tmp, info->stride[dim]);
6de9cd9a
DN
5557
5558 /* Then subtract this from our starting value. */
94471a56
TB
5559 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5560 gfc_array_index_type,
5561 info->start[dim], tmp);
6de9cd9a 5562
1f65468a 5563 info->delta[dim] = gfc_evaluate_now (tmp, &outer_loop->pre);
6de9cd9a
DN
5564 }
5565 }
5566 }
30ae600f
MM
5567
5568 for (loop = loop->nested; loop; loop = loop->next)
121c82c9 5569 gfc_set_delta (loop);
6de9cd9a
DN
5570}
5571
5572
99d821c0
DK
5573/* Calculate the size of a given array dimension from the bounds. This
5574 is simply (ubound - lbound + 1) if this expression is positive
5575 or 0 if it is negative (pick either one if it is zero). Optionally
5576 (if or_expr is present) OR the (expression != 0) condition to it. */
5577
5578tree
5579gfc_conv_array_extent_dim (tree lbound, tree ubound, tree* or_expr)
5580{
5581 tree res;
5582 tree cond;
5583
5584 /* Calculate (ubound - lbound + 1). */
94471a56
TB
5585 res = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5586 ubound, lbound);
5587 res = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, res,
5588 gfc_index_one_node);
99d821c0
DK
5589
5590 /* Check whether the size for this dimension is negative. */
63ee5404 5591 cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, res,
94471a56
TB
5592 gfc_index_zero_node);
5593 res = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
5594 gfc_index_zero_node, res);
99d821c0
DK
5595
5596 /* Build OR expression. */
5597 if (or_expr)
94471a56 5598 *or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
63ee5404 5599 logical_type_node, *or_expr, cond);
99d821c0
DK
5600
5601 return res;
5602}
5603
5604
5605/* For an array descriptor, get the total number of elements. This is just
155e5d5f 5606 the product of the extents along from_dim to to_dim. */
99d821c0 5607
155e5d5f
TB
5608static tree
5609gfc_conv_descriptor_size_1 (tree desc, int from_dim, int to_dim)
99d821c0
DK
5610{
5611 tree res;
5612 int dim;
5613
5614 res = gfc_index_one_node;
5615
155e5d5f 5616 for (dim = from_dim; dim < to_dim; ++dim)
99d821c0
DK
5617 {
5618 tree lbound;
5619 tree ubound;
5620 tree extent;
5621
5622 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
5623 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
5624
5625 extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
94471a56
TB
5626 res = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5627 res, extent);
99d821c0
DK
5628 }
5629
5630 return res;
5631}
5632
5633
155e5d5f
TB
5634/* Full size of an array. */
5635
5636tree
5637gfc_conv_descriptor_size (tree desc, int rank)
5638{
5639 return gfc_conv_descriptor_size_1 (desc, 0, rank);
5640}
5641
5642
5643/* Size of a coarray for all dimensions but the last. */
5644
5645tree
5646gfc_conv_descriptor_cosize (tree desc, int rank, int corank)
5647{
5648 return gfc_conv_descriptor_size_1 (desc, rank, rank + corank - 1);
5649}
5650
5651
1ab3acf4
JB
5652/* Fills in an array descriptor, and returns the size of the array.
5653 The size will be a simple_val, ie a variable or a constant. Also
5654 calculates the offset of the base. The pointer argument overflow,
5655 which should be of integer type, will increase in value if overflow
5656 occurs during the size calculation. Returns the size of the array.
6de9cd9a
DN
5657 {
5658 stride = 1;
5659 offset = 0;
5660 for (n = 0; n < rank; n++)
5661 {
99d821c0
DK
5662 a.lbound[n] = specified_lower_bound;
5663 offset = offset + a.lbond[n] * stride;
5664 size = 1 - lbound;
5665 a.ubound[n] = specified_upper_bound;
5666 a.stride[n] = stride;
4f13e17f 5667 size = size >= 0 ? ubound + size : 0; //size = ubound + 1 - lbound
1ab3acf4 5668 overflow += size == 0 ? 0: (MAX/size < stride ? 1: 0);
99d821c0 5669 stride = stride * size;
6de9cd9a 5670 }
badd9e69
TB
5671 for (n = rank; n < rank+corank; n++)
5672 (Set lcobound/ucobound as above.)
1ab3acf4 5673 element_size = sizeof (array element);
badd9e69
TB
5674 if (!rank)
5675 return element_size
1ab3acf4
JB
5676 stride = (size_t) stride;
5677 overflow += element_size == 0 ? 0: (MAX/element_size < stride ? 1: 0);
5678 stride = stride * element_size;
6de9cd9a
DN
5679 return (stride);
5680 } */
5681/*GCC ARRAYS*/
5682
5683static tree
f33beee9 5684gfc_array_init_size (tree descriptor, int rank, int corank, tree * poffset,
4f13e17f 5685 gfc_expr ** lower, gfc_expr ** upper, stmtblock_t * pblock,
c49ea23d 5686 stmtblock_t * descriptor_block, tree * overflow,
1792349b 5687 tree expr3_elem_size, tree *nelems, gfc_expr *expr3,
da46c08e
PT
5688 tree expr3_desc, bool e3_has_nodescriptor, gfc_expr *expr,
5689 tree *element_size)
6de9cd9a
DN
5690{
5691 tree type;
5692 tree tmp;
5693 tree size;
5694 tree offset;
5695 tree stride;
3c86fb4e
TK
5696 tree or_expr;
5697 tree thencase;
5698 tree elsecase;
79cae72e 5699 tree cond;
3c86fb4e
TK
5700 tree var;
5701 stmtblock_t thenblock;
5702 stmtblock_t elseblock;
6de9cd9a
DN
5703 gfc_expr *ubound;
5704 gfc_se se;
5705 int n;
5706
5707 type = TREE_TYPE (descriptor);
5708
7ab92584
SB
5709 stride = gfc_index_one_node;
5710 offset = gfc_index_zero_node;
6de9cd9a 5711
3c9f5092
AV
5712 /* Set the dtype before the alloc, because registration of coarrays needs
5713 it initialized. */
d168c883
JJ
5714 if (expr->ts.type == BT_CHARACTER
5715 && expr->ts.deferred
5716 && VAR_P (expr->ts.u.cl->backend_decl))
afbc5ae8
PT
5717 {
5718 type = gfc_typenode_for_spec (&expr->ts);
5719 tmp = gfc_conv_descriptor_dtype (descriptor);
3c9f5092 5720 gfc_add_modify (pblock, tmp, gfc_get_dtype_rank_type (rank, type));
afbc5ae8 5721 }
9d44426f
PT
5722 else if (expr->ts.type == BT_CHARACTER
5723 && expr->ts.deferred
5724 && TREE_CODE (descriptor) == COMPONENT_REF)
5725 {
5726 /* Deferred character components have their string length tucked away
5727 in a hidden field of the derived type. Obtain that and use it to
5728 set the dtype. The charlen backend decl is zero because the field
5729 type is zero length. */
5730 gfc_ref *ref;
5731 tmp = NULL_TREE;
5732 for (ref = expr->ref; ref; ref = ref->next)
5733 if (ref->type == REF_COMPONENT
5734 && gfc_deferred_strlen (ref->u.c.component, &tmp))
5735 break;
5736 gcc_assert (tmp != NULL_TREE);
5737 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
5738 TREE_OPERAND (descriptor, 0), tmp, NULL_TREE);
5739 tmp = fold_convert (gfc_charlen_type_node, tmp);
5740 type = gfc_get_character_type_len (expr->ts.kind, tmp);
5741 tmp = gfc_conv_descriptor_dtype (descriptor);
5742 gfc_add_modify (pblock, tmp, gfc_get_dtype_rank_type (rank, type));
5743 }
afbc5ae8
PT
5744 else
5745 {
950ab3f1
PT
5746 tmp = gfc_conv_descriptor_dtype (descriptor);
5747 gfc_add_modify (pblock, tmp, gfc_get_dtype (type));
afbc5ae8 5748 }
6de9cd9a 5749
63ee5404 5750 or_expr = logical_false_node;
3c86fb4e 5751
6de9cd9a
DN
5752 for (n = 0; n < rank; n++)
5753 {
99d821c0
DK
5754 tree conv_lbound;
5755 tree conv_ubound;
5756
6de9cd9a 5757 /* We have 3 possibilities for determining the size of the array:
99d821c0
DK
5758 lower == NULL => lbound = 1, ubound = upper[n]
5759 upper[n] = NULL => lbound = 1, ubound = lower[n]
5760 upper[n] != NULL => lbound = lower[n], ubound = upper[n] */
6de9cd9a
DN
5761 ubound = upper[n];
5762
5763 /* Set lower bound. */
5764 gfc_init_se (&se, NULL);
1792349b
AV
5765 if (expr3_desc != NULL_TREE)
5766 {
c1525930
TB
5767 if (e3_has_nodescriptor)
5768 /* The lbound of nondescriptor arrays like array constructors,
5769 nonallocatable/nonpointer function results/variables,
5770 start at zero, but when allocating it, the standard expects
5771 the array to start at one. */
1792349b
AV
5772 se.expr = gfc_index_one_node;
5773 else
5774 se.expr = gfc_conv_descriptor_lbound_get (expr3_desc,
5775 gfc_rank_cst[n]);
5776 }
5777 else if (lower == NULL)
7ab92584 5778 se.expr = gfc_index_one_node;
6de9cd9a
DN
5779 else
5780 {
6e45f57b 5781 gcc_assert (lower[n]);
99d821c0
DK
5782 if (ubound)
5783 {
6de9cd9a
DN
5784 gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
5785 gfc_add_block_to_block (pblock, &se.pre);
99d821c0
DK
5786 }
5787 else
5788 {
5789 se.expr = gfc_index_one_node;
5790 ubound = lower[n];
5791 }
6de9cd9a 5792 }
f04986a9 5793 gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
4f13e17f 5794 gfc_rank_cst[n], se.expr);
99d821c0 5795 conv_lbound = se.expr;
6de9cd9a
DN
5796
5797 /* Work out the offset for this component. */
94471a56
TB
5798 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5799 se.expr, stride);
5800 offset = fold_build2_loc (input_location, MINUS_EXPR,
5801 gfc_array_index_type, offset, tmp);
6de9cd9a 5802
6de9cd9a
DN
5803 /* Set upper bound. */
5804 gfc_init_se (&se, NULL);
1792349b
AV
5805 if (expr3_desc != NULL_TREE)
5806 {
c1525930 5807 if (e3_has_nodescriptor)
1792349b 5808 {
c1525930
TB
5809 /* The lbound of nondescriptor arrays like array constructors,
5810 nonallocatable/nonpointer function results/variables,
5811 start at zero, but when allocating it, the standard expects
5812 the array to start at one. Therefore fix the upper bound to be
5813 (desc.ubound - desc.lbound) + 1. */
1792349b
AV
5814 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5815 gfc_array_index_type,
5816 gfc_conv_descriptor_ubound_get (
5817 expr3_desc, gfc_rank_cst[n]),
5818 gfc_conv_descriptor_lbound_get (
5819 expr3_desc, gfc_rank_cst[n]));
5820 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5821 gfc_array_index_type, tmp,
5822 gfc_index_one_node);
5823 se.expr = gfc_evaluate_now (tmp, pblock);
5824 }
5825 else
5826 se.expr = gfc_conv_descriptor_ubound_get (expr3_desc,
5827 gfc_rank_cst[n]);
5828 }
5829 else
5830 {
5831 gcc_assert (ubound);
5832 gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
5833 gfc_add_block_to_block (pblock, &se.pre);
3e4d188a
AV
5834 if (ubound->expr_type == EXPR_FUNCTION)
5835 se.expr = gfc_evaluate_now (se.expr, pblock);
1792349b 5836 }
4f13e17f 5837 gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
99d821c0
DK
5838 gfc_rank_cst[n], se.expr);
5839 conv_ubound = se.expr;
6de9cd9a
DN
5840
5841 /* Store the stride. */
4f13e17f 5842 gfc_conv_descriptor_stride_set (descriptor_block, descriptor,
99d821c0 5843 gfc_rank_cst[n], stride);
3c86fb4e 5844
99d821c0
DK
5845 /* Calculate size and check whether extent is negative. */
5846 size = gfc_conv_array_extent_dim (conv_lbound, conv_ubound, &or_expr);
1ab3acf4
JB
5847 size = gfc_evaluate_now (size, pblock);
5848
5849 /* Check whether multiplying the stride by the number of
5850 elements in this dimension would overflow. We must also check
5851 whether the current dimension has zero size in order to avoid
f04986a9 5852 division by zero.
1ab3acf4 5853 */
f04986a9
PT
5854 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
5855 gfc_array_index_type,
5856 fold_convert (gfc_array_index_type,
1ab3acf4
JB
5857 TYPE_MAX_VALUE (gfc_array_index_type)),
5858 size);
79cae72e 5859 cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
63ee5404 5860 logical_type_node, tmp, stride),
ed9c79e1 5861 PRED_FORTRAN_OVERFLOW);
79cae72e
JJ
5862 tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
5863 integer_one_node, integer_zero_node);
5864 cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
63ee5404 5865 logical_type_node, size,
ed9c79e1
JJ
5866 gfc_index_zero_node),
5867 PRED_FORTRAN_SIZE_ZERO);
79cae72e
JJ
5868 tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
5869 integer_zero_node, tmp);
1ab3acf4
JB
5870 tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
5871 *overflow, tmp);
5872 *overflow = gfc_evaluate_now (tmp, pblock);
f04986a9 5873
6de9cd9a 5874 /* Multiply the stride by the number of elements in this dimension. */
94471a56
TB
5875 stride = fold_build2_loc (input_location, MULT_EXPR,
5876 gfc_array_index_type, stride, size);
6de9cd9a
DN
5877 stride = gfc_evaluate_now (stride, pblock);
5878 }
5879
f33beee9
TB
5880 for (n = rank; n < rank + corank; n++)
5881 {
5882 ubound = upper[n];
5883
5884 /* Set lower bound. */
5885 gfc_init_se (&se, NULL);
5886 if (lower == NULL || lower[n] == NULL)
5887 {
5888 gcc_assert (n == rank + corank - 1);
5889 se.expr = gfc_index_one_node;
5890 }
5891 else
5892 {
99d821c0
DK
5893 if (ubound || n == rank + corank - 1)
5894 {
f33beee9
TB
5895 gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
5896 gfc_add_block_to_block (pblock, &se.pre);
99d821c0
DK
5897 }
5898 else
5899 {
5900 se.expr = gfc_index_one_node;
5901 ubound = lower[n];
5902 }
f33beee9 5903 }
f04986a9 5904 gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
4f13e17f 5905 gfc_rank_cst[n], se.expr);
f33beee9
TB
5906
5907 if (n < rank + corank - 1)
5908 {
5909 gfc_init_se (&se, NULL);
5910 gcc_assert (ubound);
5911 gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
5912 gfc_add_block_to_block (pblock, &se.pre);
4f13e17f 5913 gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
99d821c0 5914 gfc_rank_cst[n], se.expr);
f33beee9
TB
5915 }
5916 }
5917
6de9cd9a 5918 /* The stride is the number of elements in the array, so multiply by the
eea58adb 5919 size of an element to get the total size. Obviously, if there is a
c49ea23d 5920 SOURCE expression (expr3) we must use its element size. */
4daa71b0
PT
5921 if (expr3_elem_size != NULL_TREE)
5922 tmp = expr3_elem_size;
5923 else if (expr3 != NULL)
c49ea23d
PT
5924 {
5925 if (expr3->ts.type == BT_CLASS)
5926 {
5927 gfc_se se_sz;
5928 gfc_expr *sz = gfc_copy_expr (expr3);
5929 gfc_add_vptr_component (sz);
5930 gfc_add_size_component (sz);
5931 gfc_init_se (&se_sz, NULL);
5932 gfc_conv_expr (&se_sz, sz);
5933 gfc_free_expr (sz);
5934 tmp = se_sz.expr;
5935 }
5936 else
5937 {
5938 tmp = gfc_typenode_for_spec (&expr3->ts);
5939 tmp = TYPE_SIZE_UNIT (tmp);
5940 }
5941 }
5942 else
5943 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
5944
1ab3acf4 5945 /* Convert to size_t. */
da46c08e 5946 *element_size = fold_convert (size_type_node, tmp);
badd9e69
TB
5947
5948 if (rank == 0)
da46c08e 5949 return *element_size;
badd9e69 5950
4daa71b0 5951 *nelems = gfc_evaluate_now (stride, pblock);
79cae72e 5952 stride = fold_convert (size_type_node, stride);
1ab3acf4
JB
5953
5954 /* First check for overflow. Since an array of type character can
5955 have zero element_size, we must check for that before
5956 dividing. */
f04986a9 5957 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
79cae72e 5958 size_type_node,
da46c08e 5959 TYPE_MAX_VALUE (size_type_node), *element_size);
79cae72e 5960 cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
63ee5404 5961 logical_type_node, tmp, stride),
ed9c79e1 5962 PRED_FORTRAN_OVERFLOW);
79cae72e 5963 tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
1ab3acf4 5964 integer_one_node, integer_zero_node);
79cae72e 5965 cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
da46c08e 5966 logical_type_node, *element_size,
ed9c79e1
JJ
5967 build_int_cst (size_type_node, 0)),
5968 PRED_FORTRAN_SIZE_ZERO);
79cae72e 5969 tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
1ab3acf4
JB
5970 integer_zero_node, tmp);
5971 tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
5972 *overflow, tmp);
5973 *overflow = gfc_evaluate_now (tmp, pblock);
5974
79cae72e 5975 size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
da46c08e 5976 stride, *element_size);
6de9cd9a
DN
5977
5978 if (poffset != NULL)
5979 {
5980 offset = gfc_evaluate_now (offset, pblock);
5981 *poffset = offset;
5982 }
5983
fcac9229
RS
5984 if (integer_zerop (or_expr))
5985 return size;
5986 if (integer_onep (or_expr))
79cae72e 5987 return build_int_cst (size_type_node, 0);
fcac9229 5988
3c86fb4e
TK
5989 var = gfc_create_var (TREE_TYPE (size), "size");
5990 gfc_start_block (&thenblock);
79cae72e 5991 gfc_add_modify (&thenblock, var, build_int_cst (size_type_node, 0));
3c86fb4e
TK
5992 thencase = gfc_finish_block (&thenblock);
5993
5994 gfc_start_block (&elseblock);
726a989a 5995 gfc_add_modify (&elseblock, var, size);
3c86fb4e
TK
5996 elsecase = gfc_finish_block (&elseblock);
5997
5998 tmp = gfc_evaluate_now (or_expr, pblock);
5999 tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
6000 gfc_add_expr_to_block (pblock, tmp);
6001
6002 return var;
6de9cd9a
DN
6003}
6004
6005
1792349b
AV
6006/* Retrieve the last ref from the chain. This routine is specific to
6007 gfc_array_allocate ()'s needs. */
6008
6009bool
6010retrieve_last_ref (gfc_ref **ref_in, gfc_ref **prev_ref_in)
6011{
6012 gfc_ref *ref, *prev_ref;
6013
6014 ref = *ref_in;
6015 /* Prevent warnings for uninitialized variables. */
6016 prev_ref = *prev_ref_in;
6017 while (ref && ref->next != NULL)
6018 {
6019 gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT
6020 || (ref->u.ar.dimen == 0 && ref->u.ar.codimen > 0));
6021 prev_ref = ref;
6022 ref = ref->next;
6023 }
6024
6025 if (ref == NULL || ref->type != REF_ARRAY)
6026 return false;
6027
6028 *ref_in = ref;
6029 *prev_ref_in = prev_ref;
6030 return true;
6031}
6032
1f2959f0 6033/* Initializes the descriptor and generates a call to _gfor_allocate. Does
6de9cd9a
DN
6034 the work for an ALLOCATE statement. */
6035/*GCC ARRAYS*/
6036
5b725b8d 6037bool
8f992d64 6038gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree status, tree errmsg,
4daa71b0 6039 tree errlen, tree label_finish, tree expr3_elem_size,
1792349b 6040 tree *nelems, gfc_expr *expr3, tree e3_arr_desc,
c1525930 6041 bool e3_has_nodescriptor)
6de9cd9a
DN
6042{
6043 tree tmp;
6044 tree pointer;
badd9e69 6045 tree offset = NULL_TREE;
979d4598 6046 tree token = NULL_TREE;
6de9cd9a 6047 tree size;
1ab3acf4 6048 tree msg;
badd9e69 6049 tree error = NULL_TREE;
1ab3acf4 6050 tree overflow; /* Boolean storing whether size calculation overflows. */
badd9e69 6051 tree var_overflow = NULL_TREE;
1ab3acf4 6052 tree cond;
4f13e17f 6053 tree set_descriptor;
6090f915 6054 tree not_prev_allocated = NULL_TREE;
da46c08e 6055 tree element_size = NULL_TREE;
4f13e17f 6056 stmtblock_t set_descriptor_block;
1ab3acf4 6057 stmtblock_t elseblock;
6de9cd9a
DN
6058 gfc_expr **lower;
6059 gfc_expr **upper;
3c9f5092 6060 gfc_ref *ref, *prev_ref = NULL, *coref;
de91486c
AV
6061 bool allocatable, coarray, dimension, alloc_w_e3_arr_spec = false,
6062 non_ulimate_coarray_ptr_comp;
5b725b8d
TK
6063
6064 ref = expr->ref;
6065
6066 /* Find the last reference in the chain. */
1792349b
AV
6067 if (!retrieve_last_ref (&ref, &prev_ref))
6068 return false;
6069
e457a6fc
AV
6070 /* Take the allocatable and coarray properties solely from the expr-ref's
6071 attributes and not from source=-expression. */
f33beee9 6072 if (!prev_ref)
d3a9eea2 6073 {
ea6363a3 6074 allocatable = expr->symtree->n.sym->attr.allocatable;
badd9e69 6075 dimension = expr->symtree->n.sym->attr.dimension;
de91486c 6076 non_ulimate_coarray_ptr_comp = false;
d3a9eea2 6077 }
f33beee9 6078 else
d3a9eea2 6079 {
ea6363a3 6080 allocatable = prev_ref->u.c.component->attr.allocatable;
de91486c
AV
6081 /* Pointer components in coarrayed derived types must be treated
6082 specially in that they are registered without a check if the are
6083 already associated. This does not hold for ultimate coarray
6084 pointers. */
6085 non_ulimate_coarray_ptr_comp = (prev_ref->u.c.component->attr.pointer
6086 && !prev_ref->u.c.component->attr.codimension);
badd9e69 6087 dimension = prev_ref->u.c.component->attr.dimension;
d3a9eea2
TB
6088 }
6089
3c9f5092
AV
6090 /* For allocatable/pointer arrays in derived types, one of the refs has to be
6091 a coarray. In this case it does not matter whether we are on this_image
6092 or not. */
6093 coarray = false;
6094 for (coref = expr->ref; coref; coref = coref->next)
6095 if (coref->type == REF_ARRAY && coref->u.ar.codimen > 0)
6096 {
6097 coarray = true;
6098 break;
6099 }
6100
badd9e69
TB
6101 if (!dimension)
6102 gcc_assert (coarray);
5046aff5 6103
e457a6fc
AV
6104 if (ref->u.ar.type == AR_FULL && expr3 != NULL)
6105 {
7090cac9 6106 gfc_ref *old_ref = ref;
e457a6fc
AV
6107 /* F08:C633: Array shape from expr3. */
6108 ref = expr3->ref;
6109
6110 /* Find the last reference in the chain. */
6111 if (!retrieve_last_ref (&ref, &prev_ref))
7090cac9
AV
6112 {
6113 if (expr3->expr_type == EXPR_FUNCTION
6114 && gfc_expr_attr (expr3).dimension)
6115 ref = old_ref;
6116 else
6117 return false;
6118 }
e457a6fc
AV
6119 alloc_w_e3_arr_spec = true;
6120 }
6121
6de9cd9a
DN
6122 /* Figure out the size of the array. */
6123 switch (ref->u.ar.type)
6124 {
6125 case AR_ELEMENT:
f33beee9
TB
6126 if (!coarray)
6127 {
6128 lower = NULL;
6129 upper = ref->u.ar.start;
6130 break;
6131 }
6132 /* Fall through. */
6133
6134 case AR_SECTION:
6135 lower = ref->u.ar.start;
6136 upper = ref->u.ar.end;
6de9cd9a
DN
6137 break;
6138
6139 case AR_FULL:
1792349b
AV
6140 gcc_assert (ref->u.ar.as->type == AS_EXPLICIT
6141 || alloc_w_e3_arr_spec);
6de9cd9a
DN
6142
6143 lower = ref->u.ar.as->lower;
6144 upper = ref->u.ar.as->upper;
6145 break;
6146
6de9cd9a 6147 default:
6e45f57b 6148 gcc_unreachable ();
6de9cd9a
DN
6149 break;
6150 }
6151
1ab3acf4 6152 overflow = integer_zero_node;
4f13e17f 6153
ba08c70a
PT
6154 if (expr->ts.type == BT_CHARACTER
6155 && TREE_CODE (se->string_length) == COMPONENT_REF
9d44426f
PT
6156 && expr->ts.u.cl->backend_decl != se->string_length
6157 && VAR_P (expr->ts.u.cl->backend_decl))
6158 gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
6159 fold_convert (TREE_TYPE (expr->ts.u.cl->backend_decl),
6160 se->string_length));
ba08c70a 6161
4f13e17f 6162 gfc_init_block (&set_descriptor_block);
3c9f5092
AV
6163 /* Take the corank only from the actual ref and not from the coref. The
6164 later will mislead the generation of the array dimensions for allocatable/
6165 pointer components in derived types. */
1792349b
AV
6166 size = gfc_array_init_size (se->expr, alloc_w_e3_arr_spec ? expr->rank
6167 : ref->u.ar.as->rank,
e457a6fc
AV
6168 coarray ? ref->u.ar.as->corank : 0,
6169 &offset, lower, upper,
c49ea23d 6170 &se->pre, &set_descriptor_block, &overflow,
1792349b 6171 expr3_elem_size, nelems, expr3, e3_arr_desc,
da46c08e 6172 e3_has_nodescriptor, expr, &element_size);
4f13e17f 6173
81fa8ab2 6174 if (dimension)
badd9e69 6175 {
badd9e69
TB
6176 var_overflow = gfc_create_var (integer_type_node, "overflow");
6177 gfc_add_modify (&se->pre, var_overflow, overflow);
1ab3acf4 6178
81fa8ab2
TB
6179 if (status == NULL_TREE)
6180 {
6181 /* Generate the block of code handling overflow. */
6182 msg = gfc_build_addr_expr (pchar_type_node,
6183 gfc_build_localized_cstring_const
1ab3acf4
JB
6184 ("Integer overflow when calculating the amount of "
6185 "memory to allocate"));
81fa8ab2
TB
6186 error = build_call_expr_loc (input_location,
6187 gfor_fndecl_runtime_error, 1, msg);
6188 }
6189 else
6190 {
6191 tree status_type = TREE_TYPE (status);
6192 stmtblock_t set_status_block;
1ab3acf4 6193
81fa8ab2
TB
6194 gfc_start_block (&set_status_block);
6195 gfc_add_modify (&set_status_block, status,
6196 build_int_cst (status_type, LIBERROR_ALLOCATION));
6197 error = gfc_finish_block (&set_status_block);
6198 }
1ab3acf4 6199 }
6de9cd9a
DN
6200
6201 /* Allocate memory to store the data. */
4daa71b0
PT
6202 if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
6203 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
6204
f19626cf 6205 if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
3c9f5092 6206 {
de91486c
AV
6207 pointer = non_ulimate_coarray_ptr_comp ? se->expr
6208 : gfc_conv_descriptor_data_get (se->expr);
26f391e8 6209 token = gfc_conv_descriptor_token (se->expr);
3c9f5092
AV
6210 token = gfc_build_addr_expr (NULL_TREE, token);
6211 }
de91486c
AV
6212 else
6213 pointer = gfc_conv_descriptor_data_get (se->expr);
6214 STRIP_NOPS (pointer);
979d4598 6215
6090f915
TK
6216 if (allocatable)
6217 {
6218 not_prev_allocated = gfc_create_var (logical_type_node,
6219 "not_prev_allocated");
6220 tmp = fold_build2_loc (input_location, EQ_EXPR,
6221 logical_type_node, pointer,
6222 build_int_cst (TREE_TYPE (pointer), 0));
6223
6224 gfc_add_modify (&se->pre, not_prev_allocated, tmp);
6225 }
6226
6227 gfc_start_block (&elseblock);
6228
8f992d64 6229 /* The allocatable variant takes the old pointer as first argument. */
ea6363a3 6230 if (allocatable)
979d4598 6231 gfc_allocate_allocatable (&elseblock, pointer, size, token,
3c9f5092
AV
6232 status, errmsg, errlen, label_finish, expr,
6233 coref != NULL ? coref->u.ar.as->corank : 0);
de91486c
AV
6234 else if (non_ulimate_coarray_ptr_comp && token)
6235 /* The token is set only for GFC_FCOARRAY_LIB mode. */
6236 gfc_allocate_using_caf_lib (&elseblock, pointer, size, token, status,
6237 errmsg, errlen,
6238 GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY);
5039610b 6239 else
4f13e17f 6240 gfc_allocate_using_malloc (&elseblock, pointer, size, status);
1ab3acf4 6241
badd9e69
TB
6242 if (dimension)
6243 {
6244 cond = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
63ee5404 6245 logical_type_node, var_overflow, integer_zero_node),
ed9c79e1 6246 PRED_FORTRAN_OVERFLOW);
f04986a9 6247 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
badd9e69
TB
6248 error, gfc_finish_block (&elseblock));
6249 }
6250 else
6251 tmp = gfc_finish_block (&elseblock);
1ab3acf4 6252
6de9cd9a
DN
6253 gfc_add_expr_to_block (&se->pre, tmp);
6254
da46c08e 6255 /* Update the array descriptor with the offset and the span. */
badd9e69 6256 if (dimension)
ff3598bc 6257 {
da46c08e
PT
6258 gfc_conv_descriptor_offset_set (&set_descriptor_block, se->expr, offset);
6259 tmp = fold_convert (gfc_array_index_type, element_size);
ff3598bc
PT
6260 gfc_conv_descriptor_span_set (&set_descriptor_block, se->expr, tmp);
6261 }
6262
4f13e17f
DC
6263 set_descriptor = gfc_finish_block (&set_descriptor_block);
6264 if (status != NULL_TREE)
6265 {
6266 cond = fold_build2_loc (input_location, EQ_EXPR,
63ee5404 6267 logical_type_node, status,
4f13e17f 6268 build_int_cst (TREE_TYPE (status), 0));
6090f915
TK
6269
6270 if (not_prev_allocated != NULL_TREE)
6271 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
6272 logical_type_node, cond, not_prev_allocated);
6273
4f13e17f
DC
6274 gfc_add_expr_to_block (&se->pre,
6275 fold_build3_loc (input_location, COND_EXPR, void_type_node,
7f7fa20f 6276 cond,
ed9c79e1 6277 set_descriptor,
f04986a9 6278 build_empty_stmt (input_location)));
4f13e17f
DC
6279 }
6280 else
6281 gfc_add_expr_to_block (&se->pre, set_descriptor);
5b725b8d
TK
6282
6283 return true;
6de9cd9a
DN
6284}
6285
6286
6de9cd9a
DN
6287/* Create an array constructor from an initialization expression.
6288 We assume the frontend already did any expansions and conversions. */
6289
6290tree
6291gfc_conv_array_initializer (tree type, gfc_expr * expr)
6292{
6293 gfc_constructor *c;
6de9cd9a 6294 tree tmp;
6de9cd9a 6295 gfc_se se;
21ea4922 6296 tree index, range;
9771b263 6297 vec<constructor_elt, va_gc> *v = NULL;
6de9cd9a 6298
c3f34952
TB
6299 if (expr->expr_type == EXPR_VARIABLE
6300 && expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6301 && expr->symtree->n.sym->value)
6302 expr = expr->symtree->n.sym->value;
6303
6de9cd9a
DN
6304 switch (expr->expr_type)
6305 {
6306 case EXPR_CONSTANT:
6307 case EXPR_STRUCTURE:
6308 /* A single scalar or derived type value. Create an array with all
6309 elements equal to that value. */
6310 gfc_init_se (&se, NULL);
f04986a9 6311
e9cfef64
PB
6312 if (expr->expr_type == EXPR_CONSTANT)
6313 gfc_conv_constant (&se, expr);
6314 else
6315 gfc_conv_structure (&se, expr, 1);
6de9cd9a 6316
45e955b0
JJ
6317 if (tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
6318 TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
6319 break;
6320 else if (tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
6321 TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
6322 range = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
6323 else
6324 range = build2 (RANGE_EXPR, gfc_array_index_type,
6325 TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
6326 TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
6327 CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
6de9cd9a
DN
6328 break;
6329
6330 case EXPR_ARRAY:
4038c495 6331 /* Create a vector of all the elements. */
b7e75771 6332 for (c = gfc_constructor_first (expr->value.constructor);
8c21bc66 6333 c && c->expr; c = gfc_constructor_next (c))
6de9cd9a
DN
6334 {
6335 if (c->iterator)
6336 {
6337 /* Problems occur when we get something like
63346ddb 6338 integer :: a(lots) = (/(i, i=1, lots)/) */
29e0597e
TB
6339 gfc_fatal_error ("The number of elements in the array "
6340 "constructor at %L requires an increase of "
6341 "the allowed %d upper limit. See "
6342 "%<-fmax-array-constructor%> option",
c61819ff 6343 &expr->where, flag_max_array_constructor);
63346ddb 6344 return NULL_TREE;
6de9cd9a 6345 }
b7e75771
JD
6346 if (mpz_cmp_si (c->offset, 0) != 0)
6347 index = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
6de9cd9a
DN
6348 else
6349 index = NULL_TREE;
6de9cd9a 6350
21ea4922
JJ
6351 if (mpz_cmp_si (c->repeat, 1) > 0)
6352 {
6353 tree tmp1, tmp2;
6354 mpz_t maxval;
6355
6356 mpz_init (maxval);
6357 mpz_add (maxval, c->offset, c->repeat);
6358 mpz_sub_ui (maxval, maxval, 1);
6359 tmp2 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
6360 if (mpz_cmp_si (c->offset, 0) != 0)
6361 {
6362 mpz_add_ui (maxval, c->offset, 1);
6363 tmp1 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
6364 }
6365 else
6366 tmp1 = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
6367
6368 range = fold_build2 (RANGE_EXPR, gfc_array_index_type, tmp1, tmp2);
6369 mpz_clear (maxval);
6370 }
6371 else
6372 range = NULL;
6373
6de9cd9a
DN
6374 gfc_init_se (&se, NULL);
6375 switch (c->expr->expr_type)
6376 {
6377 case EXPR_CONSTANT:
6378 gfc_conv_constant (&se, c->expr);
8b393e9f
BE
6379
6380 /* See gfortran.dg/charlen_15.f90 for instance. */
6381 if (TREE_CODE (se.expr) == STRING_CST
6382 && TREE_CODE (type) == ARRAY_TYPE)
6383 {
6384 tree atype = type;
6385 while (TREE_CODE (TREE_TYPE (atype)) == ARRAY_TYPE)
6386 atype = TREE_TYPE (atype);
22cd0312
BE
6387 gcc_checking_assert (TREE_CODE (TREE_TYPE (atype))
6388 == INTEGER_TYPE);
6389 gcc_checking_assert (TREE_TYPE (TREE_TYPE (se.expr))
6390 == TREE_TYPE (atype));
6391 if (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (se.expr)))
6392 > tree_to_uhwi (TYPE_SIZE_UNIT (atype)))
8b393e9f
BE
6393 {
6394 unsigned HOST_WIDE_INT size
6395 = tree_to_uhwi (TYPE_SIZE_UNIT (atype));
6396 const char *p = TREE_STRING_POINTER (se.expr);
6397
6398 se.expr = build_string (size, p);
8b393e9f 6399 }
22cd0312 6400 TREE_TYPE (se.expr) = atype;
8b393e9f 6401 }
6de9cd9a
DN
6402 break;
6403
6404 case EXPR_STRUCTURE:
6405 gfc_conv_structure (&se, c->expr, 1);
6de9cd9a
DN
6406 break;
6407
6408 default:
c1cfed03
PT
6409 /* Catch those occasional beasts that do not simplify
6410 for one reason or another, assuming that if they are
6411 standard defying the frontend will catch them. */
6412 gfc_conv_expr (&se, c->expr);
c1cfed03 6413 break;
6de9cd9a 6414 }
21ea4922
JJ
6415
6416 if (range == NULL_TREE)
6417 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
6418 else
6419 {
6420 if (index != NULL_TREE)
6421 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
6422 CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
6423 }
6de9cd9a 6424 }
6de9cd9a
DN
6425 break;
6426
5046aff5
PT
6427 case EXPR_NULL:
6428 return gfc_build_null_descriptor (type);
6429
6de9cd9a 6430 default:
6e45f57b 6431 gcc_unreachable ();
6de9cd9a
DN
6432 }
6433
6434 /* Create a constructor from the list of elements. */
4038c495 6435 tmp = build_constructor (type, v);
6de9cd9a 6436 TREE_CONSTANT (tmp) = 1;
6de9cd9a
DN
6437 return tmp;
6438}
6439
6440
9f3761c5
TB
6441/* Generate code to evaluate non-constant coarray cobounds. */
6442
6443void
6444gfc_trans_array_cobounds (tree type, stmtblock_t * pblock,
6445 const gfc_symbol *sym)
6446{
6447 int dim;
6448 tree ubound;
6449 tree lbound;
6450 gfc_se se;
6451 gfc_array_spec *as;
6452
f3b0bb7a 6453 as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
9f3761c5
TB
6454
6455 for (dim = as->rank; dim < as->rank + as->corank; dim++)
6456 {
6457 /* Evaluate non-constant array bound expressions. */
6458 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
6459 if (as->lower[dim] && !INTEGER_CST_P (lbound))
6460 {
6461 gfc_init_se (&se, NULL);
6462 gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
6463 gfc_add_block_to_block (pblock, &se.pre);
6464 gfc_add_modify (pblock, lbound, se.expr);
6465 }
6466 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
6467 if (as->upper[dim] && !INTEGER_CST_P (ubound))
6468 {
6469 gfc_init_se (&se, NULL);
6470 gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
6471 gfc_add_block_to_block (pblock, &se.pre);
6472 gfc_add_modify (pblock, ubound, se.expr);
6473 }
6474 }
6475}
6476
6477
6de9cd9a
DN
6478/* Generate code to evaluate non-constant array bounds. Sets *poffset and
6479 returns the size (in elements) of the array. */
6480
64f96237 6481tree
6de9cd9a
DN
6482gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
6483 stmtblock_t * pblock)
6484{
6485 gfc_array_spec *as;
6486 tree size;
6487 tree stride;
6488 tree offset;
6489 tree ubound;
6490 tree lbound;
6491 tree tmp;
6492 gfc_se se;
6493
6494 int dim;
6495
f3b0bb7a 6496 as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
6de9cd9a 6497
7ab92584
SB
6498 size = gfc_index_one_node;
6499 offset = gfc_index_zero_node;
6de9cd9a
DN
6500 for (dim = 0; dim < as->rank; dim++)
6501 {
6502 /* Evaluate non-constant array bound expressions. */
6503 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
6504 if (as->lower[dim] && !INTEGER_CST_P (lbound))
6505 {
6506 gfc_init_se (&se, NULL);
6507 gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
6508 gfc_add_block_to_block (pblock, &se.pre);
726a989a 6509 gfc_add_modify (pblock, lbound, se.expr);
6de9cd9a
DN
6510 }
6511 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
6512 if (as->upper[dim] && !INTEGER_CST_P (ubound))
6513 {
6514 gfc_init_se (&se, NULL);
6515 gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
6516 gfc_add_block_to_block (pblock, &se.pre);
726a989a 6517 gfc_add_modify (pblock, ubound, se.expr);
6de9cd9a 6518 }
f7b529fa 6519 /* The offset of this dimension. offset = offset - lbound * stride. */
94471a56
TB
6520 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6521 lbound, size);
6522 offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6523 offset, tmp);
6de9cd9a
DN
6524
6525 /* The size of this dimension, and the stride of the next. */
6526 if (dim + 1 < as->rank)
6527 stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
6528 else
417ab240 6529 stride = GFC_TYPE_ARRAY_SIZE (type);
6de9cd9a
DN
6530
6531 if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
6532 {
6533 /* Calculate stride = size * (ubound + 1 - lbound). */
94471a56
TB
6534 tmp = fold_build2_loc (input_location, MINUS_EXPR,
6535 gfc_array_index_type,
6536 gfc_index_one_node, lbound);
6537 tmp = fold_build2_loc (input_location, PLUS_EXPR,
6538 gfc_array_index_type, ubound, tmp);
6539 tmp = fold_build2_loc (input_location, MULT_EXPR,
6540 gfc_array_index_type, size, tmp);
6de9cd9a 6541 if (stride)
726a989a 6542 gfc_add_modify (pblock, stride, tmp);
6de9cd9a
DN
6543 else
6544 stride = gfc_evaluate_now (tmp, pblock);
5b440a1c
PT
6545
6546 /* Make sure that negative size arrays are translated
6547 to being zero size. */
63ee5404 6548 tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
94471a56
TB
6549 stride, gfc_index_zero_node);
6550 tmp = fold_build3_loc (input_location, COND_EXPR,
6551 gfc_array_index_type, tmp,
6552 stride, gfc_index_zero_node);
726a989a 6553 gfc_add_modify (pblock, stride, tmp);
6de9cd9a
DN
6554 }
6555
6556 size = stride;
6557 }
9f3761c5
TB
6558
6559 gfc_trans_array_cobounds (type, pblock, sym);
417ab240
JJ
6560 gfc_trans_vla_type_sizes (sym, pblock);
6561
6de9cd9a
DN
6562 *poffset = offset;
6563 return size;
6564}
6565
6566
6567/* Generate code to initialize/allocate an array variable. */
6568
0019d498
DK
6569void
6570gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym,
6571 gfc_wrapped_block * block)
6de9cd9a 6572{
0019d498 6573 stmtblock_t init;
6de9cd9a 6574 tree type;
c76f8d52 6575 tree tmp = NULL_TREE;
6de9cd9a
DN
6576 tree size;
6577 tree offset;
c76f8d52
MM
6578 tree space;
6579 tree inittree;
6de9cd9a
DN
6580 bool onstack;
6581
6e45f57b 6582 gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
6de9cd9a
DN
6583
6584 /* Do nothing for USEd variables. */
6585 if (sym->attr.use_assoc)
0019d498 6586 return;
6de9cd9a
DN
6587
6588 type = TREE_TYPE (decl);
6e45f57b 6589 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a
DN
6590 onstack = TREE_CODE (type) != POINTER_TYPE;
6591
f315a6b4 6592 gfc_init_block (&init);
6de9cd9a
DN
6593
6594 /* Evaluate character string length. */
6595 if (sym->ts.type == BT_CHARACTER
bc21d315 6596 && onstack && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
6de9cd9a 6597 {
0019d498 6598 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
6de9cd9a 6599
0019d498 6600 gfc_trans_vla_type_sizes (sym, &init);
417ab240 6601
1a186ec5 6602 /* Emit a DECL_EXPR for this variable, which will cause the
13795658 6603 gimplifier to allocate storage, and all that good stuff. */
94471a56 6604 tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
0019d498 6605 gfc_add_expr_to_block (&init, tmp);
6de9cd9a
DN
6606 }
6607
6608 if (onstack)
6609 {
0019d498
DK
6610 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
6611 return;
6de9cd9a
DN
6612 }
6613
6614 type = TREE_TYPE (type);
6615
6e45f57b 6616 gcc_assert (!sym->attr.use_assoc);
cb9e4f55 6617 gcc_assert (!sym->module);
6de9cd9a
DN
6618
6619 if (sym->ts.type == BT_CHARACTER
bc21d315 6620 && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
0019d498 6621 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
6de9cd9a 6622
0019d498 6623 size = gfc_trans_array_bounds (type, sym, &offset, &init);
6de9cd9a 6624
83d890b9
AL
6625 /* Don't actually allocate space for Cray Pointees. */
6626 if (sym->attr.cray_pointee)
6627 {
d168c883 6628 if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
0019d498
DK
6629 gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
6630
6631 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
6632 return;
83d890b9
AL
6633 }
6634
203c7ebf 6635 if (flag_stack_arrays)
c76f8d52
MM
6636 {
6637 gcc_assert (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE);
9c81750c 6638 space = build_decl (gfc_get_location (&sym->declared_at),
c76f8d52
MM
6639 VAR_DECL, create_tmp_var_name ("A"),
6640 TREE_TYPE (TREE_TYPE (decl)));
6641 gfc_trans_vla_type_sizes (sym, &init);
6642 }
6643 else
6644 {
6645 /* The size is the number of elements in the array, so multiply by the
6646 size of an element to get the total size. */
6647 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
6648 size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6649 size, fold_convert (gfc_array_index_type, tmp));
6650
6651 /* Allocate memory to hold the data. */
6652 tmp = gfc_call_malloc (&init, TREE_TYPE (decl), size);
6653 gfc_add_modify (&init, decl, tmp);
6de9cd9a 6654
c76f8d52 6655 /* Free the temporary. */
107051a5 6656 tmp = gfc_call_free (decl);
c76f8d52
MM
6657 space = NULL_TREE;
6658 }
6de9cd9a
DN
6659
6660 /* Set offset of the array. */
d168c883 6661 if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
0019d498 6662 gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
6de9cd9a
DN
6663
6664 /* Automatic arrays should not have initializers. */
6e45f57b 6665 gcc_assert (!sym->value);
6de9cd9a 6666
c76f8d52 6667 inittree = gfc_finish_block (&init);
6de9cd9a 6668
c76f8d52
MM
6669 if (space)
6670 {
6671 tree addr;
6672 pushdecl (space);
6673
6674 /* Don't create new scope, emit the DECL_EXPR in exactly the scope
6675 where also space is located. */
6676 gfc_init_block (&init);
6677 tmp = fold_build1_loc (input_location, DECL_EXPR,
6678 TREE_TYPE (space), space);
6679 gfc_add_expr_to_block (&init, tmp);
9c81750c 6680 addr = fold_build1_loc (gfc_get_location (&sym->declared_at),
c76f8d52
MM
6681 ADDR_EXPR, TREE_TYPE (decl), space);
6682 gfc_add_modify (&init, decl, addr);
6683 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
6684 tmp = NULL_TREE;
6685 }
6686 gfc_add_init_cleanup (block, inittree, tmp);
6de9cd9a
DN
6687}
6688
6689
6690/* Generate entry and exit code for g77 calling convention arrays. */
6691
0019d498
DK
6692void
6693gfc_trans_g77_array (gfc_symbol * sym, gfc_wrapped_block * block)
6de9cd9a
DN
6694{
6695 tree parm;
6696 tree type;
6697 locus loc;
6698 tree offset;
6699 tree tmp;
363aab21 6700 tree stmt;
0019d498 6701 stmtblock_t init;
6de9cd9a 6702
363aab21 6703 gfc_save_backend_locus (&loc);
6de9cd9a
DN
6704 gfc_set_backend_locus (&sym->declared_at);
6705
6706 /* Descriptor type. */
6707 parm = sym->backend_decl;
6708 type = TREE_TYPE (parm);
6e45f57b 6709 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a 6710
0019d498 6711 gfc_start_block (&init);
6de9cd9a
DN
6712
6713 if (sym->ts.type == BT_CHARACTER
d168c883 6714 && VAR_P (sym->ts.u.cl->backend_decl))
0019d498 6715 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
6de9cd9a
DN
6716
6717 /* Evaluate the bounds of the array. */
0019d498 6718 gfc_trans_array_bounds (type, sym, &offset, &init);
6de9cd9a
DN
6719
6720 /* Set the offset. */
d168c883 6721 if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
0019d498 6722 gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
6de9cd9a 6723
1f2959f0 6724 /* Set the pointer itself if we aren't using the parameter directly. */
6de9cd9a
DN
6725 if (TREE_CODE (parm) != PARM_DECL)
6726 {
a2c59300
PT
6727 tmp = GFC_DECL_SAVED_DESCRIPTOR (parm);
6728 if (sym->ts.type == BT_CLASS)
6729 {
6730 tmp = build_fold_indirect_ref_loc (input_location, tmp);
6731 tmp = gfc_class_data_get (tmp);
6732 tmp = gfc_conv_descriptor_data_get (tmp);
6733 }
6734 tmp = convert (TREE_TYPE (parm), tmp);
0019d498 6735 gfc_add_modify (&init, parm, tmp);
6de9cd9a 6736 }
0019d498 6737 stmt = gfc_finish_block (&init);
6de9cd9a 6738
363aab21 6739 gfc_restore_backend_locus (&loc);
6de9cd9a 6740
6de9cd9a 6741 /* Add the initialization code to the start of the function. */
54129a64 6742
eb92cd57
TB
6743 if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
6744 || sym->attr.optional
6745 || sym->attr.not_always_present)
54129a64 6746 {
cb3c3d63
TB
6747 tree nullify;
6748 if (TREE_CODE (parm) != PARM_DECL)
6749 nullify = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6750 parm, null_pointer_node);
6751 else
6752 nullify = build_empty_stmt (input_location);
6753 tmp = gfc_conv_expr_present (sym, true);
6754 stmt = build3_v (COND_EXPR, tmp, stmt, nullify);
54129a64 6755 }
f04986a9 6756
0019d498 6757 gfc_add_init_cleanup (block, stmt, NULL_TREE);
6de9cd9a
DN
6758}
6759
6760
6761/* Modify the descriptor of an array parameter so that it has the
6762 correct lower bound. Also move the upper bound accordingly.
6763 If the array is not packed, it will be copied into a temporary.
6764 For each dimension we set the new lower and upper bounds. Then we copy the
6765 stride and calculate the offset for this dimension. We also work out
6766 what the stride of a packed array would be, and see it the two match.
6767 If the array need repacking, we set the stride to the values we just
6768 calculated, recalculate the offset and copy the array data.
6769 Code is also added to copy the data back at the end of the function.
6770 */
6771
0019d498
DK
6772void
6773gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc,
6774 gfc_wrapped_block * block)
6de9cd9a
DN
6775{
6776 tree size;
6777 tree type;
6778 tree offset;
6779 locus loc;
0019d498
DK
6780 stmtblock_t init;
6781 tree stmtInit, stmtCleanup;
6de9cd9a
DN
6782 tree lbound;
6783 tree ubound;
6784 tree dubound;
6785 tree dlbound;
6786 tree dumdesc;
6787 tree tmp;
e8300d6e 6788 tree stride, stride2;
6de9cd9a
DN
6789 tree stmt_packed;
6790 tree stmt_unpacked;
6791 tree partial;
6792 gfc_se se;
6793 int n;
6794 int checkparm;
6795 int no_repack;
3d79abbd 6796 bool optional_arg;
f3b0bb7a
AV
6797 gfc_array_spec *as;
6798 bool is_classarray = IS_CLASS_ARRAY (sym);
6de9cd9a 6799
fc90a8f2 6800 /* Do nothing for pointer and allocatable arrays. */
f3b0bb7a
AV
6801 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
6802 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
6803 || sym->attr.allocatable
6804 || (is_classarray && CLASS_DATA (sym)->attr.allocatable))
0019d498 6805 return;
fc90a8f2 6806
f3b0bb7a 6807 if (!is_classarray && sym->attr.dummy && gfc_is_nodesc_array (sym))
0019d498
DK
6808 {
6809 gfc_trans_g77_array (sym, block);
6810 return;
6811 }
6de9cd9a 6812
8e9218f2 6813 loc.nextc = NULL;
363aab21 6814 gfc_save_backend_locus (&loc);
8e9218f2
AV
6815 /* loc.nextc is not set by save_backend_locus but the location routines
6816 depend on it. */
6817 if (loc.nextc == NULL)
6818 loc.nextc = loc.lb->line;
6de9cd9a
DN
6819 gfc_set_backend_locus (&sym->declared_at);
6820
6821 /* Descriptor type. */
6822 type = TREE_TYPE (tmpdesc);
6e45f57b 6823 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a 6824 dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
f3b0bb7a
AV
6825 if (is_classarray)
6826 /* For a class array the dummy array descriptor is in the _class
6827 component. */
6828 dumdesc = gfc_class_data_get (dumdesc);
6829 else
6830 dumdesc = build_fold_indirect_ref_loc (input_location, dumdesc);
6831 as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
0019d498 6832 gfc_start_block (&init);
6de9cd9a
DN
6833
6834 if (sym->ts.type == BT_CHARACTER
d168c883 6835 && VAR_P (sym->ts.u.cl->backend_decl))
0019d498 6836 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
6de9cd9a 6837
a2c59300
PT
6838 /* TODO: Fix the exclusion of class arrays from extent checking. */
6839 checkparm = (as->type == AS_EXPLICIT && !is_classarray
d3d3011f 6840 && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS));
6de9cd9a
DN
6841
6842 no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
0019d498 6843 || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
6de9cd9a
DN
6844
6845 if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
6846 {
6847 /* For non-constant shape arrays we only check if the first dimension
0019d498
DK
6848 is contiguous. Repacking higher dimensions wouldn't gain us
6849 anything as we still don't know the array stride. */
63ee5404 6850 partial = gfc_create_var (logical_type_node, "partial");
6de9cd9a 6851 TREE_USED (partial) = 1;
568e8e1e 6852 tmp = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
63ee5404 6853 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
94471a56 6854 gfc_index_one_node);
0019d498 6855 gfc_add_modify (&init, partial, tmp);
6de9cd9a
DN
6856 }
6857 else
0019d498 6858 partial = NULL_TREE;
6de9cd9a
DN
6859
6860 /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
6861 here, however I think it does the right thing. */
6862 if (no_repack)
6863 {
6864 /* Set the first stride. */
568e8e1e 6865 stride = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
0019d498 6866 stride = gfc_evaluate_now (stride, &init);
6de9cd9a 6867
63ee5404 6868 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
94471a56
TB
6869 stride, gfc_index_zero_node);
6870 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
6871 tmp, gfc_index_one_node, stride);
6de9cd9a 6872 stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
0019d498 6873 gfc_add_modify (&init, stride, tmp);
6de9cd9a
DN
6874
6875 /* Allow the user to disable array repacking. */
6876 stmt_unpacked = NULL_TREE;
6877 }
6878 else
6879 {
6e45f57b 6880 gcc_assert (integer_onep (GFC_TYPE_ARRAY_STRIDE (type, 0)));
1f2959f0 6881 /* A library call to repack the array if necessary. */
6de9cd9a 6882 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
db3927fb
AH
6883 stmt_unpacked = build_call_expr_loc (input_location,
6884 gfor_fndecl_in_pack, 1, tmp);
6de9cd9a 6885
7ab92584 6886 stride = gfc_index_one_node;
bdfd2ff0 6887
73e42eef 6888 if (warn_array_temporaries)
48749dbc
MLI
6889 gfc_warning (OPT_Warray_temporaries,
6890 "Creating array temporary at %L", &loc);
6de9cd9a
DN
6891 }
6892
6893 /* This is for the case where the array data is used directly without
6894 calling the repack function. */
6895 if (no_repack || partial != NULL_TREE)
4c73896d 6896 stmt_packed = gfc_conv_descriptor_data_get (dumdesc);
6de9cd9a
DN
6897 else
6898 stmt_packed = NULL_TREE;
6899
6900 /* Assign the data pointer. */
6901 if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
6902 {
6903 /* Don't repack unknown shape arrays when the first stride is 1. */
94471a56
TB
6904 tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (stmt_packed),
6905 partial, stmt_packed, stmt_unpacked);
6de9cd9a
DN
6906 }
6907 else
6908 tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
0019d498 6909 gfc_add_modify (&init, tmpdesc, fold_convert (type, tmp));
6de9cd9a 6910
7ab92584
SB
6911 offset = gfc_index_zero_node;
6912 size = gfc_index_one_node;
6de9cd9a
DN
6913
6914 /* Evaluate the bounds of the array. */
f3b0bb7a 6915 for (n = 0; n < as->rank; n++)
6de9cd9a 6916 {
f3b0bb7a 6917 if (checkparm || !as->upper[n])
6de9cd9a
DN
6918 {
6919 /* Get the bounds of the actual parameter. */
568e8e1e
PT
6920 dubound = gfc_conv_descriptor_ubound_get (dumdesc, gfc_rank_cst[n]);
6921 dlbound = gfc_conv_descriptor_lbound_get (dumdesc, gfc_rank_cst[n]);
6de9cd9a
DN
6922 }
6923 else
0019d498 6924 {
6de9cd9a
DN
6925 dubound = NULL_TREE;
6926 dlbound = NULL_TREE;
0019d498 6927 }
6de9cd9a
DN
6928
6929 lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
6930 if (!INTEGER_CST_P (lbound))
0019d498
DK
6931 {
6932 gfc_init_se (&se, NULL);
f3b0bb7a 6933 gfc_conv_expr_type (&se, as->lower[n],
0019d498
DK
6934 gfc_array_index_type);
6935 gfc_add_block_to_block (&init, &se.pre);
6936 gfc_add_modify (&init, lbound, se.expr);
6937 }
6de9cd9a
DN
6938
6939 ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
6940 /* Set the desired upper bound. */
f3b0bb7a 6941 if (as->upper[n])
6de9cd9a
DN
6942 {
6943 /* We know what we want the upper bound to be. */
0019d498
DK
6944 if (!INTEGER_CST_P (ubound))
6945 {
6de9cd9a 6946 gfc_init_se (&se, NULL);
f3b0bb7a 6947 gfc_conv_expr_type (&se, as->upper[n],
0019d498
DK
6948 gfc_array_index_type);
6949 gfc_add_block_to_block (&init, &se.pre);
6950 gfc_add_modify (&init, ubound, se.expr);
6951 }
6de9cd9a
DN
6952
6953 /* Check the sizes match. */
6954 if (checkparm)
6955 {
6956 /* Check (ubound(a) - lbound(a) == ubound(b) - lbound(b)). */
dd18a33b 6957 char * msg;
6c559604 6958 tree temp;
6de9cd9a 6959
94471a56
TB
6960 temp = fold_build2_loc (input_location, MINUS_EXPR,
6961 gfc_array_index_type, ubound, lbound);
6962 temp = fold_build2_loc (input_location, PLUS_EXPR,
6963 gfc_array_index_type,
6964 gfc_index_one_node, temp);
6965 stride2 = fold_build2_loc (input_location, MINUS_EXPR,
6966 gfc_array_index_type, dubound,
6967 dlbound);
6968 stride2 = fold_build2_loc (input_location, PLUS_EXPR,
6969 gfc_array_index_type,
6970 gfc_index_one_node, stride2);
6971 tmp = fold_build2_loc (input_location, NE_EXPR,
6972 gfc_array_index_type, temp, stride2);
1a33dc9e
UB
6973 msg = xasprintf ("Dimension %d of array '%s' has extent "
6974 "%%ld instead of %%ld", n+1, sym->name);
6c559604 6975
f04986a9 6976 gfc_trans_runtime_check (true, false, tmp, &init, &loc, msg,
6c559604
SK
6977 fold_convert (long_integer_type_node, temp),
6978 fold_convert (long_integer_type_node, stride2));
6979
cede9502 6980 free (msg);
6de9cd9a
DN
6981 }
6982 }
6983 else
6984 {
6985 /* For assumed shape arrays move the upper bound by the same amount
6986 as the lower bound. */
94471a56
TB
6987 tmp = fold_build2_loc (input_location, MINUS_EXPR,
6988 gfc_array_index_type, dubound, dlbound);
6989 tmp = fold_build2_loc (input_location, PLUS_EXPR,
6990 gfc_array_index_type, tmp, lbound);
0019d498 6991 gfc_add_modify (&init, ubound, tmp);
6de9cd9a 6992 }
f7b529fa 6993 /* The offset of this dimension. offset = offset - lbound * stride. */
94471a56
TB
6994 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6995 lbound, stride);
6996 offset = fold_build2_loc (input_location, MINUS_EXPR,
6997 gfc_array_index_type, offset, tmp);
6de9cd9a
DN
6998
6999 /* The size of this dimension, and the stride of the next. */
f3b0bb7a 7000 if (n + 1 < as->rank)
0019d498
DK
7001 {
7002 stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
6de9cd9a 7003
0019d498
DK
7004 if (no_repack || partial != NULL_TREE)
7005 stmt_unpacked =
7006 gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[n+1]);
6de9cd9a 7007
0019d498
DK
7008 /* Figure out the stride if not a known constant. */
7009 if (!INTEGER_CST_P (stride))
7010 {
7011 if (no_repack)
7012 stmt_packed = NULL_TREE;
7013 else
7014 {
7015 /* Calculate stride = size * (ubound + 1 - lbound). */
94471a56
TB
7016 tmp = fold_build2_loc (input_location, MINUS_EXPR,
7017 gfc_array_index_type,
7018 gfc_index_one_node, lbound);
7019 tmp = fold_build2_loc (input_location, PLUS_EXPR,
7020 gfc_array_index_type, ubound, tmp);
7021 size = fold_build2_loc (input_location, MULT_EXPR,
7022 gfc_array_index_type, size, tmp);
0019d498
DK
7023 stmt_packed = size;
7024 }
6de9cd9a 7025
0019d498
DK
7026 /* Assign the stride. */
7027 if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
94471a56
TB
7028 tmp = fold_build3_loc (input_location, COND_EXPR,
7029 gfc_array_index_type, partial,
7030 stmt_unpacked, stmt_packed);
0019d498
DK
7031 else
7032 tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
7033 gfc_add_modify (&init, stride, tmp);
7034 }
7035 }
417ab240
JJ
7036 else
7037 {
7038 stride = GFC_TYPE_ARRAY_SIZE (type);
7039
7040 if (stride && !INTEGER_CST_P (stride))
7041 {
7042 /* Calculate size = stride * (ubound + 1 - lbound). */
94471a56
TB
7043 tmp = fold_build2_loc (input_location, MINUS_EXPR,
7044 gfc_array_index_type,
7045 gfc_index_one_node, lbound);
7046 tmp = fold_build2_loc (input_location, PLUS_EXPR,
7047 gfc_array_index_type,
7048 ubound, tmp);
7049 tmp = fold_build2_loc (input_location, MULT_EXPR,
7050 gfc_array_index_type,
7051 GFC_TYPE_ARRAY_STRIDE (type, n), tmp);
0019d498 7052 gfc_add_modify (&init, stride, tmp);
417ab240
JJ
7053 }
7054 }
6de9cd9a
DN
7055 }
7056
d73b65b6
TB
7057 gfc_trans_array_cobounds (type, &init, sym);
7058
6de9cd9a 7059 /* Set the offset. */
d168c883 7060 if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
0019d498 7061 gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
6de9cd9a 7062
0019d498 7063 gfc_trans_vla_type_sizes (sym, &init);
417ab240 7064
0019d498 7065 stmtInit = gfc_finish_block (&init);
6de9cd9a
DN
7066
7067 /* Only do the entry/initialization code if the arg is present. */
7068 dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
d198b59a
JJ
7069 optional_arg = (sym->attr.optional
7070 || (sym->ns->proc_name->attr.entry_master
7071 && sym->attr.dummy));
3d79abbd 7072 if (optional_arg)
6de9cd9a 7073 {
892c7427
TB
7074 tree zero_init = fold_convert (TREE_TYPE (tmpdesc), null_pointer_node);
7075 zero_init = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7076 tmpdesc, zero_init);
7077 tmp = gfc_conv_expr_present (sym, true);
7078 stmtInit = build3_v (COND_EXPR, tmp, stmtInit, zero_init);
6de9cd9a 7079 }
6de9cd9a
DN
7080
7081 /* Cleanup code. */
0019d498
DK
7082 if (no_repack)
7083 stmtCleanup = NULL_TREE;
7084 else
6de9cd9a 7085 {
0019d498 7086 stmtblock_t cleanup;
6de9cd9a 7087 gfc_start_block (&cleanup);
0019d498 7088
6de9cd9a
DN
7089 if (sym->attr.intent != INTENT_IN)
7090 {
7091 /* Copy the data back. */
db3927fb
AH
7092 tmp = build_call_expr_loc (input_location,
7093 gfor_fndecl_in_unpack, 2, dumdesc, tmpdesc);
6de9cd9a
DN
7094 gfc_add_expr_to_block (&cleanup, tmp);
7095 }
7096
7097 /* Free the temporary. */
1529b8d9 7098 tmp = gfc_call_free (tmpdesc);
6de9cd9a
DN
7099 gfc_add_expr_to_block (&cleanup, tmp);
7100
0019d498 7101 stmtCleanup = gfc_finish_block (&cleanup);
f04986a9 7102
6de9cd9a 7103 /* Only do the cleanup if the array was repacked. */
b2d83bd2
AV
7104 if (is_classarray)
7105 /* For a class array the dummy array descriptor is in the _class
7106 component. */
7107 tmp = gfc_class_data_get (dumdesc);
7108 else
7109 tmp = build_fold_indirect_ref_loc (input_location, dumdesc);
4c73896d 7110 tmp = gfc_conv_descriptor_data_get (tmp);
63ee5404 7111 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
94471a56 7112 tmp, tmpdesc);
0019d498
DK
7113 stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
7114 build_empty_stmt (input_location));
6de9cd9a 7115
3d79abbd 7116 if (optional_arg)
0019d498
DK
7117 {
7118 tmp = gfc_conv_expr_present (sym);
7119 stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
7120 build_empty_stmt (input_location));
7121 }
6de9cd9a 7122 }
0019d498 7123
6de9cd9a
DN
7124 /* We don't need to free any memory allocated by internal_pack as it will
7125 be freed at the end of the function by pop_context. */
0019d498 7126 gfc_add_init_cleanup (block, stmtInit, stmtCleanup);
363aab21
MM
7127
7128 gfc_restore_backend_locus (&loc);
6de9cd9a
DN
7129}
7130
7131
1d6b7f39 7132/* Calculate the overall offset, including subreferences. */
bbf18dc5 7133void
1d6b7f39
PT
7134gfc_get_dataptr_offset (stmtblock_t *block, tree parm, tree desc, tree offset,
7135 bool subref, gfc_expr *expr)
7136{
7137 tree tmp;
7138 tree field;
7139 tree stride;
7140 tree index;
7141 gfc_ref *ref;
7142 gfc_se start;
7143 int n;
7144
7145 /* If offset is NULL and this is not a subreferenced array, there is
7146 nothing to do. */
7147 if (offset == NULL_TREE)
7148 {
7149 if (subref)
7150 offset = gfc_index_zero_node;
7151 else
7152 return;
7153 }
7154
f3b0bb7a 7155 tmp = build_array_ref (desc, offset, NULL, NULL);
1d6b7f39
PT
7156
7157 /* Offset the data pointer for pointer assignments from arrays with
df2fba9e 7158 subreferences; e.g. my_integer => my_type(:)%integer_component. */
1d6b7f39
PT
7159 if (subref)
7160 {
7161 /* Go past the array reference. */
7162 for (ref = expr->ref; ref; ref = ref->next)
7163 if (ref->type == REF_ARRAY &&
7164 ref->u.ar.type != AR_ELEMENT)
7165 {
7166 ref = ref->next;
7167 break;
7168 }
7169
7170 /* Calculate the offset for each subsequent subreference. */
7171 for (; ref; ref = ref->next)
7172 {
7173 switch (ref->type)
7174 {
7175 case REF_COMPONENT:
7176 field = ref->u.c.component->backend_decl;
7177 gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
94471a56
TB
7178 tmp = fold_build3_loc (input_location, COMPONENT_REF,
7179 TREE_TYPE (field),
7180 tmp, field, NULL_TREE);
1d6b7f39
PT
7181 break;
7182
7183 case REF_SUBSTRING:
7184 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
7185 gfc_init_se (&start, NULL);
7186 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
7187 gfc_add_block_to_block (block, &start.pre);
7188 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
7189 break;
7190
7191 case REF_ARRAY:
7192 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE
7193 && ref->u.ar.type == AR_ELEMENT);
7194
7195 /* TODO - Add bounds checking. */
7196 stride = gfc_index_one_node;
7197 index = gfc_index_zero_node;
7198 for (n = 0; n < ref->u.ar.dimen; n++)
7199 {
7200 tree itmp;
7201 tree jtmp;
7202
7203 /* Update the index. */
7204 gfc_init_se (&start, NULL);
7205 gfc_conv_expr_type (&start, ref->u.ar.start[n], gfc_array_index_type);
7206 itmp = gfc_evaluate_now (start.expr, block);
7207 gfc_init_se (&start, NULL);
7208 gfc_conv_expr_type (&start, ref->u.ar.as->lower[n], gfc_array_index_type);
7209 jtmp = gfc_evaluate_now (start.expr, block);
94471a56
TB
7210 itmp = fold_build2_loc (input_location, MINUS_EXPR,
7211 gfc_array_index_type, itmp, jtmp);
7212 itmp = fold_build2_loc (input_location, MULT_EXPR,
7213 gfc_array_index_type, itmp, stride);
7214 index = fold_build2_loc (input_location, PLUS_EXPR,
7215 gfc_array_index_type, itmp, index);
1d6b7f39
PT
7216 index = gfc_evaluate_now (index, block);
7217
7218 /* Update the stride. */
7219 gfc_init_se (&start, NULL);
7220 gfc_conv_expr_type (&start, ref->u.ar.as->upper[n], gfc_array_index_type);
94471a56
TB
7221 itmp = fold_build2_loc (input_location, MINUS_EXPR,
7222 gfc_array_index_type, start.expr,
7223 jtmp);
7224 itmp = fold_build2_loc (input_location, PLUS_EXPR,
7225 gfc_array_index_type,
7226 gfc_index_one_node, itmp);
7227 stride = fold_build2_loc (input_location, MULT_EXPR,
7228 gfc_array_index_type, stride, itmp);
1d6b7f39
PT
7229 stride = gfc_evaluate_now (stride, block);
7230 }
7231
7232 /* Apply the index to obtain the array element. */
7233 tmp = gfc_build_array_ref (tmp, index, NULL);
7234 break;
7235
9de42a8e
PT
7236 case REF_INQUIRY:
7237 switch (ref->u.i)
7238 {
7239 case INQUIRY_RE:
7240 tmp = fold_build1_loc (input_location, REALPART_EXPR,
7241 TREE_TYPE (TREE_TYPE (tmp)), tmp);
7242 break;
7243
7244 case INQUIRY_IM:
7245 tmp = fold_build1_loc (input_location, IMAGPART_EXPR,
7246 TREE_TYPE (TREE_TYPE (tmp)), tmp);
7247 break;
7248
7249 default:
7250 break;
7251 }
7252 break;
7253
1d6b7f39
PT
7254 default:
7255 gcc_unreachable ();
7256 break;
7257 }
7258 }
7259 }
7260
7261 /* Set the target data pointer. */
7262 offset = gfc_build_addr_expr (gfc_array_dataptr_type (desc), tmp);
7263 gfc_conv_descriptor_data_set (block, parm, offset);
7264}
7265
7266
5d63a35f
PT
7267/* gfc_conv_expr_descriptor needs the string length an expression
7268 so that the size of the temporary can be obtained. This is done
7269 by adding up the string lengths of all the elements in the
7270 expression. Function with non-constant expressions have their
7271 string lengths mapped onto the actual arguments using the
e53b6e56 7272 interface mapping machinery in trans-expr.cc. */
0a164a3c 7273static void
5d63a35f 7274get_array_charlen (gfc_expr *expr, gfc_se *se)
0a164a3c
PT
7275{
7276 gfc_interface_mapping mapping;
7277 gfc_formal_arglist *formal;
7278 gfc_actual_arglist *arg;
7279 gfc_se tse;
d5f48c7c 7280 gfc_expr *e;
0a164a3c 7281
bc21d315
JW
7282 if (expr->ts.u.cl->length
7283 && gfc_is_constant_expr (expr->ts.u.cl->length))
0a164a3c 7284 {
bc21d315
JW
7285 if (!expr->ts.u.cl->backend_decl)
7286 gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
5d63a35f 7287 return;
0a164a3c
PT
7288 }
7289
5d63a35f
PT
7290 switch (expr->expr_type)
7291 {
d5f48c7c
PT
7292 case EXPR_ARRAY:
7293
7294 /* This is somewhat brutal. The expression for the first
7295 element of the array is evaluated and assigned to a
7296 new string length for the original expression. */
7297 e = gfc_constructor_first (expr->value.constructor)->expr;
7298
7299 gfc_init_se (&tse, NULL);
300ef2fc
PT
7300
7301 /* Avoid evaluating trailing array references since all we need is
7302 the string length. */
d5f48c7c 7303 if (e->rank)
300ef2fc
PT
7304 tse.descriptor_only = 1;
7305 if (e->rank && e->expr_type != EXPR_VARIABLE)
d5f48c7c
PT
7306 gfc_conv_expr_descriptor (&tse, e);
7307 else
7308 gfc_conv_expr (&tse, e);
7309
7310 gfc_add_block_to_block (&se->pre, &tse.pre);
7311 gfc_add_block_to_block (&se->post, &tse.post);
7312
7313 if (!expr->ts.u.cl->backend_decl || !VAR_P (expr->ts.u.cl->backend_decl))
7314 {
7315 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7316 expr->ts.u.cl->backend_decl =
7317 gfc_create_var (gfc_charlen_type_node, "sln");
7318 }
7319
7320 gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
7321 tse.string_length);
7322
300ef2fc
PT
7323 /* Make sure that deferred length components point to the hidden
7324 string_length component. */
7325 if (TREE_CODE (tse.expr) == COMPONENT_REF
7326 && TREE_CODE (tse.string_length) == COMPONENT_REF
7327 && TREE_OPERAND (tse.expr, 0) == TREE_OPERAND (tse.string_length, 0))
7328 e->ts.u.cl->backend_decl = expr->ts.u.cl->backend_decl;
7329
d5f48c7c
PT
7330 return;
7331
5d63a35f
PT
7332 case EXPR_OP:
7333 get_array_charlen (expr->value.op.op1, se);
7334
300ef2fc 7335 /* For parentheses the expression ts.u.cl should be identical. */
5d63a35f 7336 if (expr->value.op.op == INTRINSIC_PARENTHESES)
300ef2fc
PT
7337 {
7338 if (expr->value.op.op1->ts.u.cl != expr->ts.u.cl)
7339 expr->ts.u.cl->backend_decl
7340 = expr->value.op.op1->ts.u.cl->backend_decl;
7341 return;
7342 }
5d63a35f 7343
d5f48c7c 7344 expr->ts.u.cl->backend_decl =
5d63a35f
PT
7345 gfc_create_var (gfc_charlen_type_node, "sln");
7346
7347 if (expr->value.op.op2)
7348 {
7349 get_array_charlen (expr->value.op.op2, se);
7350
71a7778c
PT
7351 gcc_assert (expr->value.op.op == INTRINSIC_CONCAT);
7352
5d63a35f
PT
7353 /* Add the string lengths and assign them to the expression
7354 string length backend declaration. */
bc21d315 7355 gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
94471a56
TB
7356 fold_build2_loc (input_location, PLUS_EXPR,
7357 gfc_charlen_type_node,
bc21d315
JW
7358 expr->value.op.op1->ts.u.cl->backend_decl,
7359 expr->value.op.op2->ts.u.cl->backend_decl));
5d63a35f
PT
7360 }
7361 else
bc21d315
JW
7362 gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
7363 expr->value.op.op1->ts.u.cl->backend_decl);
5d63a35f
PT
7364 break;
7365
7366 case EXPR_FUNCTION:
7367 if (expr->value.function.esym == NULL
bc21d315 7368 || expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
5d63a35f 7369 {
bc21d315 7370 gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
5d63a35f
PT
7371 break;
7372 }
7373
7374 /* Map expressions involving the dummy arguments onto the actual
7375 argument expressions. */
7376 gfc_init_interface_mapping (&mapping);
4cbc9039 7377 formal = gfc_sym_get_dummy_args (expr->symtree->n.sym);
5d63a35f
PT
7378 arg = expr->value.function.actual;
7379
7380 /* Set se = NULL in the calls to the interface mapping, to suppress any
7381 backend stuff. */
7382 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
7383 {
7384 if (!arg->expr)
7385 continue;
7386 if (formal->sym)
7387 gfc_add_interface_mapping (&mapping, formal->sym, NULL, arg->expr);
7388 }
7389
7390 gfc_init_se (&tse, NULL);
7391
7392 /* Build the expression for the character length and convert it. */
bc21d315 7393 gfc_apply_interface_mapping (&mapping, &tse, expr->ts.u.cl->length);
0a164a3c 7394
5d63a35f
PT
7395 gfc_add_block_to_block (&se->pre, &tse.pre);
7396 gfc_add_block_to_block (&se->post, &tse.post);
7397 tse.expr = fold_convert (gfc_charlen_type_node, tse.expr);
94471a56 7398 tse.expr = fold_build2_loc (input_location, MAX_EXPR,
f622221a
JB
7399 TREE_TYPE (tse.expr), tse.expr,
7400 build_zero_cst (TREE_TYPE (tse.expr)));
bc21d315 7401 expr->ts.u.cl->backend_decl = tse.expr;
5d63a35f
PT
7402 gfc_free_interface_mapping (&mapping);
7403 break;
0a164a3c 7404
5d63a35f 7405 default:
bc21d315 7406 gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
5d63a35f
PT
7407 break;
7408 }
0a164a3c
PT
7409}
7410
cb4b9eae 7411
b4e9d41d
MM
7412/* Helper function to check dimensions. */
7413static bool
a7fb208d 7414transposed_dims (gfc_ss *ss)
b4e9d41d
MM
7415{
7416 int n;
a7fb208d 7417
cb4b9eae
MM
7418 for (n = 0; n < ss->dimen; n++)
7419 if (ss->dim[n] != n)
a7fb208d
MM
7420 return true;
7421 return false;
b4e9d41d 7422}
0a164a3c 7423
2960a368
TB
7424
7425/* Convert the last ref of a scalar coarray from an AR_ELEMENT to an
7426 AR_FULL, suitable for the scalarizer. */
7427
7428static gfc_ss *
7429walk_coarray (gfc_expr *e)
7430{
7431 gfc_ss *ss;
7432
7433 gcc_assert (gfc_get_corank (e) > 0);
7434
7435 ss = gfc_walk_expr (e);
7436
7437 /* Fix scalar coarray. */
7438 if (ss == gfc_ss_terminator)
7439 {
7440 gfc_ref *ref;
7441
7442 ref = e->ref;
7443 while (ref)
7444 {
7445 if (ref->type == REF_ARRAY
7446 && ref->u.ar.codimen > 0)
7447 break;
7448
7449 ref = ref->next;
7450 }
7451
7452 gcc_assert (ref != NULL);
7453 if (ref->u.ar.type == AR_ELEMENT)
7454 ref->u.ar.type = AR_SECTION;
7455 ss = gfc_reverse_ss (gfc_walk_array_ref (ss, e, ref));
7456 }
7457
7458 return ss;
7459}
7460
7461
7a70c12d 7462/* Convert an array for passing as an actual argument. Expressions and
7ab92584 7463 vector subscripts are evaluated and stored in a temporary, which is then
6de9cd9a
DN
7464 passed. For whole arrays the descriptor is passed. For array sections
7465 a modified copy of the descriptor is passed, but using the original data.
7a70c12d
RS
7466
7467 This function is also used for array pointer assignments, and there
7468 are three cases:
7469
3e90ac4e 7470 - se->want_pointer && !se->direct_byref
7a70c12d
RS
7471 EXPR is an actual argument. On exit, se->expr contains a
7472 pointer to the array descriptor.
7473
3e90ac4e 7474 - !se->want_pointer && !se->direct_byref
7a70c12d
RS
7475 EXPR is an actual argument to an intrinsic function or the
7476 left-hand side of a pointer assignment. On exit, se->expr
7477 contains the descriptor for EXPR.
7478
3e90ac4e 7479 - !se->want_pointer && se->direct_byref
7a70c12d
RS
7480 EXPR is the right-hand side of a pointer assignment and
7481 se->expr is the descriptor for the previously-evaluated
7482 left-hand side. The function creates an assignment from
f04986a9 7483 EXPR to se->expr.
0b4f2770
MM
7484
7485
7486 The se->force_tmp flag disables the non-copying descriptor optimization
7487 that is used for transpose. It may be used in cases where there is an
7488 alias between the transpose argument and another argument in the same
7489 function call. */
6de9cd9a
DN
7490
7491void
2960a368 7492gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr)
6de9cd9a 7493{
2960a368 7494 gfc_ss *ss;
bcc4d4e0 7495 gfc_ss_type ss_type;
f98cfd3c 7496 gfc_ss_info *ss_info;
6de9cd9a 7497 gfc_loopinfo loop;
6d63e468 7498 gfc_array_info *info;
6de9cd9a
DN
7499 int need_tmp;
7500 int n;
7501 tree tmp;
7502 tree desc;
7503 stmtblock_t block;
7504 tree start;
6de9cd9a 7505 int full;
1d6b7f39 7506 bool subref_array_target = false;
9d44426f 7507 bool deferred_array_component = false;
f98cfd3c 7508 gfc_expr *arg, *ss_expr;
6de9cd9a 7509
2960a368
TB
7510 if (se->want_coarray)
7511 ss = walk_coarray (expr);
7512 else
7513 ss = gfc_walk_expr (expr);
7514
0b4f2770 7515 gcc_assert (ss != NULL);
6e45f57b 7516 gcc_assert (ss != gfc_ss_terminator);
6de9cd9a 7517
f98cfd3c
MM
7518 ss_info = ss->info;
7519 ss_type = ss_info->type;
7520 ss_expr = ss_info->expr;
bcc4d4e0 7521
2960a368
TB
7522 /* Special case: TRANSPOSE which needs no temporary. */
7523 while (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym
01512446 7524 && (arg = gfc_get_noncopying_intrinsic_argument (expr)) != NULL)
2960a368
TB
7525 {
7526 /* This is a call to transpose which has already been handled by the
7527 scalarizer, so that we just need to get its argument's descriptor. */
7528 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
7529 expr = expr->value.function.actual->expr;
7530 }
7531
d514626e
JRFS
7532 if (!se->direct_byref)
7533 se->unlimited_polymorphic = UNLIMITED_POLY (expr);
7534
fc90a8f2
PB
7535 /* Special case things we know we can pass easily. */
7536 switch (expr->expr_type)
6de9cd9a 7537 {
fc90a8f2
PB
7538 case EXPR_VARIABLE:
7539 /* If we have a linear array section, we can pass it directly.
7540 Otherwise we need to copy it into a temporary. */
6de9cd9a 7541
bcc4d4e0 7542 gcc_assert (ss_type == GFC_SS_SECTION);
f98cfd3c 7543 gcc_assert (ss_expr == expr);
1838afec 7544 info = &ss_info->data.array;
6de9cd9a
DN
7545
7546 /* Get the descriptor for the array. */
0b4f2770 7547 gfc_conv_ss_descriptor (&se->pre, ss, 0);
6de9cd9a 7548 desc = info->descriptor;
7a70c12d 7549
9d44426f
PT
7550 /* The charlen backend decl for deferred character components cannot
7551 be used because it is fixed at zero. Instead, the hidden string
7552 length component is used. */
7553 if (expr->ts.type == BT_CHARACTER
7554 && expr->ts.deferred
7555 && TREE_CODE (desc) == COMPONENT_REF)
7556 deferred_array_component = true;
7557
d514626e
JRFS
7558 subref_array_target = (is_subref_array (expr)
7559 && (se->direct_byref
7560 || expr->ts.type == BT_CHARACTER));
7561 need_tmp = (gfc_ref_needs_temporary_p (expr->ref)
7562 && !subref_array_target);
1d6b7f39 7563
0b4f2770
MM
7564 if (se->force_tmp)
7565 need_tmp = 1;
0a524296
PT
7566 else if (se->force_no_tmp)
7567 need_tmp = 0;
0b4f2770 7568
7a70c12d
RS
7569 if (need_tmp)
7570 full = 0;
7571 else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
6de9cd9a
DN
7572 {
7573 /* Create a new descriptor if the array doesn't have one. */
7574 full = 0;
7575 }
2960a368 7576 else if (info->ref->u.ar.type == AR_FULL || se->descriptor_only)
6de9cd9a
DN
7577 full = 1;
7578 else if (se->direct_byref)
7579 full = 0;
2ff0f488
JRFS
7580 else if (info->ref->u.ar.dimen == 0 && !info->ref->next)
7581 full = 1;
7582 else if (info->ref->u.ar.type == AR_SECTION && se->want_pointer)
7583 full = 0;
6de9cd9a 7584 else
a61a36ab 7585 full = gfc_full_array_ref_p (info->ref, NULL);
ca2940c3 7586
a7fb208d 7587 if (full && !transposed_dims (ss))
6de9cd9a 7588 {
99d821c0 7589 if (se->direct_byref && !se->byref_noassign)
6de9cd9a
DN
7590 {
7591 /* Copy the descriptor for pointer assignments. */
726a989a 7592 gfc_add_modify (&se->pre, se->expr, desc);
1d6b7f39
PT
7593
7594 /* Add any offsets from subreferences. */
7595 gfc_get_dataptr_offset (&se->pre, se->expr, desc, NULL_TREE,
7596 subref_array_target, expr);
ff3598bc
PT
7597
7598 /* ....and set the span field. */
d514626e
JRFS
7599 tmp = gfc_conv_descriptor_span_get (desc);
7600 gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
6de9cd9a
DN
7601 }
7602 else if (se->want_pointer)
7603 {
7604 /* We pass full arrays directly. This means that pointers and
fc90a8f2 7605 allocatable arrays should also work. */
628c189e 7606 se->expr = gfc_build_addr_expr (NULL_TREE, desc);
6de9cd9a
DN
7607 }
7608 else
7609 {
7610 se->expr = desc;
7611 }
ca2940c3 7612
9d44426f 7613 if (expr->ts.type == BT_CHARACTER && !deferred_array_component)
ca2940c3 7614 se->string_length = gfc_get_expr_charlen (expr);
9d44426f
PT
7615 /* The ss_info string length is returned set to the value of the
7616 hidden string length component. */
7617 else if (deferred_array_component)
7618 se->string_length = ss_info->string_length;
ca2940c3 7619
2960a368 7620 gfc_free_ss_chain (ss);
6de9cd9a
DN
7621 return;
7622 }
fc90a8f2 7623 break;
f04986a9 7624
fc90a8f2
PB
7625 case EXPR_FUNCTION:
7626 /* A transformational function return value will be a temporary
7627 array descriptor. We still need to go through the scalarizer
eea58adb 7628 to create the descriptor. Elemental functions are handled as
e7dc5b4f 7629 arbitrary expressions, i.e. copy to a temporary. */
fc90a8f2
PB
7630
7631 if (se->direct_byref)
7632 {
f98cfd3c 7633 gcc_assert (ss_type == GFC_SS_FUNCTION && ss_expr == expr);
fc90a8f2
PB
7634
7635 /* For pointer assignments pass the descriptor directly. */
0b4f2770
MM
7636 if (se->ss == NULL)
7637 se->ss = ss;
7638 else
7639 gcc_assert (se->ss == ss);
ff3598bc
PT
7640
7641 if (!is_pointer_array (se->expr))
7642 {
7643 tmp = gfc_get_element_type (TREE_TYPE (se->expr));
7644 tmp = fold_convert (gfc_array_index_type,
7645 size_in_bytes (tmp));
7646 gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
7647 }
7648
628c189e 7649 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
fc90a8f2 7650 gfc_conv_expr (se, expr);
ff3598bc 7651
2960a368 7652 gfc_free_ss_chain (ss);
fc90a8f2
PB
7653 return;
7654 }
7655
f98cfd3c 7656 if (ss_expr != expr || ss_type != GFC_SS_FUNCTION)
fc90a8f2 7657 {
f98cfd3c 7658 if (ss_expr != expr)
bef6486a
MM
7659 /* Elemental function. */
7660 gcc_assert ((expr->value.function.esym != NULL
7661 && expr->value.function.esym->attr.elemental)
7662 || (expr->value.function.isym != NULL
0c08de8f 7663 && expr->value.function.isym->elemental)
003f0414
PT
7664 || (gfc_expr_attr (expr).proc_pointer
7665 && gfc_expr_attr (expr).elemental)
0c08de8f 7666 || gfc_inline_intrinsic_function_p (expr));
bef6486a 7667
fc90a8f2 7668 need_tmp = 1;
0a164a3c 7669 if (expr->ts.type == BT_CHARACTER
bc21d315 7670 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5d63a35f 7671 get_array_charlen (expr, se);
0a164a3c 7672
fc90a8f2
PB
7673 info = NULL;
7674 }
7675 else
7676 {
7677 /* Transformational function. */
1838afec 7678 info = &ss_info->data.array;
fc90a8f2
PB
7679 need_tmp = 0;
7680 }
7681 break;
7682
114e4d10
RS
7683 case EXPR_ARRAY:
7684 /* Constant array constructors don't need a temporary. */
bcc4d4e0 7685 if (ss_type == GFC_SS_CONSTRUCTOR
114e4d10
RS
7686 && expr->ts.type != BT_CHARACTER
7687 && gfc_constant_array_constructor_p (expr->value.constructor))
7688 {
7689 need_tmp = 0;
1838afec 7690 info = &ss_info->data.array;
114e4d10
RS
7691 }
7692 else
7693 {
7694 need_tmp = 1;
114e4d10
RS
7695 info = NULL;
7696 }
7697 break;
7698
fc90a8f2
PB
7699 default:
7700 /* Something complicated. Copy it into a temporary. */
6de9cd9a 7701 need_tmp = 1;
6de9cd9a 7702 info = NULL;
fc90a8f2 7703 break;
6de9cd9a
DN
7704 }
7705
0b4f2770
MM
7706 /* If we are creating a temporary, we don't need to bother about aliases
7707 anymore. */
7708 if (need_tmp)
7709 se->force_tmp = 0;
7710
6de9cd9a
DN
7711 gfc_init_loopinfo (&loop);
7712
7713 /* Associate the SS with the loop. */
7714 gfc_add_ss_to_loop (&loop, ss);
7715
13413760 7716 /* Tell the scalarizer not to bother creating loop variables, etc. */
6de9cd9a
DN
7717 if (!need_tmp)
7718 loop.array_parameter = 1;
7719 else
7a70c12d
RS
7720 /* The right-hand side of a pointer assignment mustn't use a temporary. */
7721 gcc_assert (!se->direct_byref);
6de9cd9a 7722
980fa45e
TK
7723 /* Do we need bounds checking or not? */
7724 ss->no_bounds_check = expr->no_bounds_check;
7725
6de9cd9a
DN
7726 /* Setup the scalarizing loops and bounds. */
7727 gfc_conv_ss_startstride (&loop);
7728
7729 if (need_tmp)
7730 {
d5f48c7c
PT
7731 if (expr->ts.type == BT_CHARACTER
7732 && (!expr->ts.u.cl->backend_decl || expr->expr_type == EXPR_ARRAY))
5d63a35f 7733 get_array_charlen (expr, se);
07368af0 7734
a1ae4f43
MM
7735 /* Tell the scalarizer to make a temporary. */
7736 loop.temp_ss = gfc_get_temp_ss (gfc_typenode_for_spec (&expr->ts),
7737 ((expr->ts.type == BT_CHARACTER)
7738 ? expr->ts.u.cl->backend_decl
7739 : NULL),
7740 loop.dimen);
07368af0 7741
a0add3be 7742 se->string_length = loop.temp_ss->info->string_length;
cb4b9eae 7743 gcc_assert (loop.temp_ss->dimen == loop.dimen);
6de9cd9a
DN
7744 gfc_add_ss_to_loop (&loop, loop.temp_ss);
7745 }
7746
bdfd2ff0 7747 gfc_conv_loop_setup (&loop, & expr->where);
6de9cd9a
DN
7748
7749 if (need_tmp)
7750 {
7751 /* Copy into a temporary and pass that. We don't need to copy the data
7752 back because expressions and vector subscripts must be INTENT_IN. */
7753 /* TODO: Optimize passing function return values. */
7754 gfc_se lse;
7755 gfc_se rse;
4ee822df 7756 bool deep_copy;
6de9cd9a
DN
7757
7758 /* Start the copying loops. */
7759 gfc_mark_ss_chain_used (loop.temp_ss, 1);
7760 gfc_mark_ss_chain_used (ss, 1);
7761 gfc_start_scalarized_body (&loop, &block);
7762
7763 /* Copy each data element. */
7764 gfc_init_se (&lse, NULL);
7765 gfc_copy_loopinfo_to_se (&lse, &loop);
7766 gfc_init_se (&rse, NULL);
7767 gfc_copy_loopinfo_to_se (&rse, &loop);
7768
7769 lse.ss = loop.temp_ss;
7770 rse.ss = ss;
7771
761dda57 7772 gfc_conv_tmp_array_ref (&lse);
2b052ce2
PT
7773 if (expr->ts.type == BT_CHARACTER)
7774 {
7775 gfc_conv_expr (&rse, expr);
20b1cbc3 7776 if (POINTER_TYPE_P (TREE_TYPE (rse.expr)))
db3927fb
AH
7777 rse.expr = build_fold_indirect_ref_loc (input_location,
7778 rse.expr);
2b052ce2
PT
7779 }
7780 else
7781 gfc_conv_expr_val (&rse, expr);
6de9cd9a
DN
7782
7783 gfc_add_block_to_block (&block, &rse.pre);
7784 gfc_add_block_to_block (&block, &lse.pre);
7785
129c14bd 7786 lse.string_length = rse.string_length;
4ee822df
LK
7787
7788 deep_copy = !se->data_not_needed
7789 && (expr->expr_type == EXPR_VARIABLE
7790 || expr->expr_type == EXPR_ARRAY);
ed673c00 7791 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
4ee822df 7792 deep_copy, false);
129c14bd 7793 gfc_add_expr_to_block (&block, tmp);
6de9cd9a
DN
7794
7795 /* Finish the copying loops. */
7796 gfc_trans_scalarizing_loops (&loop, &block);
7797
1838afec 7798 desc = loop.temp_ss->info->data.array.descriptor;
6de9cd9a 7799 }
a7fb208d 7800 else if (expr->expr_type == EXPR_FUNCTION && !transposed_dims (ss))
fc90a8f2
PB
7801 {
7802 desc = info->descriptor;
a0add3be 7803 se->string_length = ss_info->string_length;
fc90a8f2 7804 }
6de9cd9a
DN
7805 else
7806 {
fc90a8f2
PB
7807 /* We pass sections without copying to a temporary. Make a new
7808 descriptor and point it at the section we want. The loop variable
7809 limits will be the limits of the section.
7810 A function may decide to repack the array to speed up access, but
7811 we're not bothered about that here. */
a3935ffc 7812 int dim, ndim, codim;
6de9cd9a
DN
7813 tree parm;
7814 tree parmtype;
d514626e 7815 tree dtype;
6de9cd9a
DN
7816 tree stride;
7817 tree from;
7818 tree to;
7819 tree base;
2ff0f488 7820 tree offset;
6de9cd9a 7821
cb4b9eae 7822 ndim = info->ref ? info->ref->u.ar.dimen : ss->dimen;
c2558afc 7823
23c3d0f9 7824 if (se->want_coarray)
6bd0ce7b 7825 {
7c5950bd
MM
7826 gfc_array_ref *ar = &info->ref->u.ar;
7827
6bd0ce7b 7828 codim = gfc_get_corank (expr);
a04b23d8 7829 for (n = 0; n < codim - 1; n++)
6bd0ce7b 7830 {
065c6f9d 7831 /* Make sure we are not lost somehow. */
a04b23d8 7832 gcc_assert (ar->dimen_type[n + ndim] == DIMEN_THIS_IMAGE);
065c6f9d 7833
621babd8 7834 /* Make sure the call to gfc_conv_section_startstride won't
cf664522 7835 generate unnecessary code to calculate stride. */
a04b23d8 7836 gcc_assert (ar->stride[n + ndim] == NULL);
065c6f9d 7837
cf664522 7838 gfc_conv_section_startstride (&loop.pre, ss, n + ndim);
a04b23d8
MM
7839 loop.from[n + loop.dimen] = info->start[n + ndim];
7840 loop.to[n + loop.dimen] = info->end[n + ndim];
6bd0ce7b
MM
7841 }
7842
a04b23d8 7843 gcc_assert (n == codim - 1);
7c5950bd 7844 evaluate_bound (&loop.pre, info->start, ar->start,
97561cdc
AV
7845 info->descriptor, n + ndim, true,
7846 ar->as->type == AS_DEFERRED);
a04b23d8 7847 loop.from[n + loop.dimen] = info->start[n + ndim];
6bd0ce7b 7848 }
23c3d0f9
MM
7849 else
7850 codim = 0;
7851
fc90a8f2 7852 /* Set the string_length for a character array. */
20c9dc8a 7853 if (expr->ts.type == BT_CHARACTER)
d5f48c7c 7854 {
7987beec
PT
7855 if (deferred_array_component)
7856 se->string_length = ss_info->string_length;
7857 else
7858 se->string_length = gfc_get_expr_charlen (expr);
7859
d5f48c7c
PT
7860 if (VAR_P (se->string_length)
7861 && expr->ts.u.cl->backend_decl == se->string_length)
7862 tmp = ss_info->string_length;
7863 else
7864 tmp = se->string_length;
7865
7987beec 7866 if (expr->ts.deferred && VAR_P (expr->ts.u.cl->backend_decl))
d5f48c7c 7867 gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl, tmp);
7987beec
PT
7868 else
7869 expr->ts.u.cl->backend_decl = tmp;
d5f48c7c 7870 }
20c9dc8a 7871
2ff0f488
JRFS
7872 /* If we have an array section, are assigning or passing an array
7873 section argument make sure that the lower bound is 1. References
7874 to the full array should otherwise keep the original bounds. */
7875 if (!info->ref || info->ref->u.ar.type != AR_FULL)
3244f4cd
AV
7876 for (dim = 0; dim < loop.dimen; dim++)
7877 if (!integer_onep (loop.from[dim]))
7878 {
7879 tmp = fold_build2_loc (input_location, MINUS_EXPR,
7880 gfc_array_index_type, gfc_index_one_node,
7881 loop.from[dim]);
7882 loop.to[dim] = fold_build2_loc (input_location, PLUS_EXPR,
7883 gfc_array_index_type,
7884 loop.to[dim], tmp);
7885 loop.from[dim] = gfc_index_one_node;
7886 }
7887
6de9cd9a 7888 desc = info->descriptor;
99d821c0 7889 if (se->direct_byref && !se->byref_noassign)
6de9cd9a 7890 {
e8db6cd5 7891 /* For pointer assignments we fill in the destination. */
6de9cd9a
DN
7892 parm = se->expr;
7893 parmtype = TREE_TYPE (parm);
7894 }
7895 else
7896 {
7897 /* Otherwise make a new one. */
d514626e 7898 if (expr->ts.type == BT_CHARACTER)
d5ace305
PT
7899 parmtype = gfc_typenode_for_spec (&expr->ts);
7900 else
7901 parmtype = gfc_get_element_type (TREE_TYPE (desc));
7902
a7525708
MM
7903 parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, codim,
7904 loop.from, loop.to, 0,
10174ddf 7905 GFC_ARRAY_UNKNOWN, false);
6de9cd9a 7906 parm = gfc_create_var (parmtype, "parm");
574284e9
AV
7907
7908 /* When expression is a class object, then add the class' handle to
7909 the parm_decl. */
7910 if (expr->ts.type == BT_CLASS && expr->expr_type == EXPR_VARIABLE)
7911 {
7912 gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
7913 gfc_se classse;
7914
7915 /* class_expr can be NULL, when no _class ref is in expr.
7916 We must not fix this here with a gfc_fix_class_ref (). */
7917 if (class_expr)
7918 {
7919 gfc_init_se (&classse, NULL);
7920 gfc_conv_expr (&classse, class_expr);
7921 gfc_free_expr (class_expr);
7922
7923 gcc_assert (classse.pre.head == NULL_TREE
7924 && classse.post.head == NULL_TREE);
7925 gfc_allocate_lang_decl (parm);
7926 GFC_DECL_SAVED_DESCRIPTOR (parm) = classse.expr;
7927 }
7928 }
6de9cd9a
DN
7929 }
7930
e8db6cd5 7931 /* Set the span field. */
d514626e
JRFS
7932 tmp = gfc_get_array_span (desc, expr);
7933 if (tmp)
e8db6cd5
PT
7934 gfc_conv_descriptor_span_set (&loop.pre, parm, tmp);
7935
6de9cd9a
DN
7936 /* The following can be somewhat confusing. We have two
7937 descriptors, a new one and the original array.
7938 {parm, parmtype, dim} refer to the new one.
0b4f2770 7939 {desc, type, n, loop} refer to the original, which maybe
6de9cd9a 7940 a descriptorless array.
e7dc5b4f 7941 The bounds of the scalarization are the bounds of the section.
6de9cd9a
DN
7942 We don't have to worry about numeric overflows when calculating
7943 the offsets because all elements are within the array data. */
7944
7945 /* Set the dtype. */
7946 tmp = gfc_conv_descriptor_dtype (parm);
d514626e
JRFS
7947 if (se->unlimited_polymorphic)
7948 dtype = gfc_get_dtype (TREE_TYPE (desc), &loop.dimen);
64f96237
TB
7949 else if (expr->ts.type == BT_ASSUMED)
7950 {
7951 tree tmp2 = desc;
7952 if (DECL_LANG_SPECIFIC (tmp2) && GFC_DECL_SAVED_DESCRIPTOR (tmp2))
7953 tmp2 = GFC_DECL_SAVED_DESCRIPTOR (tmp2);
7954 if (POINTER_TYPE_P (TREE_TYPE (tmp2)))
7955 tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
7956 dtype = gfc_conv_descriptor_dtype (tmp2);
7957 }
d514626e
JRFS
7958 else
7959 dtype = gfc_get_dtype (parmtype);
7960 gfc_add_modify (&loop.pre, tmp, dtype);
6de9cd9a 7961
2ff0f488
JRFS
7962 /* The 1st element in the section. */
7963 base = gfc_index_zero_node;
7964
7965 /* The offset from the 1st element in the section. */
7966 offset = gfc_index_zero_node;
6de9cd9a 7967
114e4d10 7968 for (n = 0; n < ndim; n++)
6de9cd9a
DN
7969 {
7970 stride = gfc_conv_array_stride (desc, n);
7971
2ff0f488 7972 /* Work out the 1st element in the section. */
114e4d10
RS
7973 if (info->ref
7974 && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
6de9cd9a 7975 {
6e45f57b 7976 gcc_assert (info->subscript[n]
bcc4d4e0 7977 && info->subscript[n]->info->type == GFC_SS_SCALAR);
99dd5a29 7978 start = info->subscript[n]->info->data.scalar.value;
6de9cd9a
DN
7979 }
7980 else
7981 {
6de9cd9a 7982 /* Evaluate and remember the start of the section. */
9157ccb2 7983 start = info->start[n];
6de9cd9a
DN
7984 stride = gfc_evaluate_now (stride, &loop.pre);
7985 }
7986
7987 tmp = gfc_conv_array_lbound (desc, n);
94471a56
TB
7988 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
7989 start, tmp);
7990 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
7991 tmp, stride);
2ff0f488
JRFS
7992 base = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
7993 base, tmp);
6de9cd9a 7994
114e4d10
RS
7995 if (info->ref
7996 && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
6de9cd9a 7997 {
2ff0f488
JRFS
7998 /* For elemental dimensions, we only need the 1st
7999 element in the section. */
6de9cd9a
DN
8000 continue;
8001 }
8002
8003 /* Vector subscripts need copying and are handled elsewhere. */
114e4d10
RS
8004 if (info->ref)
8005 gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
f04986a9 8006
0b4f2770
MM
8007 /* look for the corresponding scalarizer dimension: dim. */
8008 for (dim = 0; dim < ndim; dim++)
cb4b9eae 8009 if (ss->dim[dim] == n)
0b4f2770
MM
8010 break;
8011
8012 /* loop exited early: the DIM being looked for has been found. */
8013 gcc_assert (dim < ndim);
6de9cd9a
DN
8014
8015 /* Set the new lower bound. */
8016 from = loop.from[dim];
8017 to = loop.to[dim];
4fd9a813 8018
568e8e1e
PT
8019 gfc_conv_descriptor_lbound_set (&loop.pre, parm,
8020 gfc_rank_cst[dim], from);
6de9cd9a
DN
8021
8022 /* Set the new upper bound. */
568e8e1e
PT
8023 gfc_conv_descriptor_ubound_set (&loop.pre, parm,
8024 gfc_rank_cst[dim], to);
6de9cd9a
DN
8025
8026 /* Multiply the stride by the section stride to get the
8027 total stride. */
94471a56
TB
8028 stride = fold_build2_loc (input_location, MULT_EXPR,
8029 gfc_array_index_type,
8030 stride, info->stride[n]);
6de9cd9a 8031
2ff0f488
JRFS
8032 tmp = fold_build2_loc (input_location, MULT_EXPR,
8033 TREE_TYPE (offset), stride, from);
8034 offset = fold_build2_loc (input_location, MINUS_EXPR,
8035 TREE_TYPE (offset), offset, tmp);
6de9cd9a
DN
8036
8037 /* Store the new stride. */
568e8e1e
PT
8038 gfc_conv_descriptor_stride_set (&loop.pre, parm,
8039 gfc_rank_cst[dim], stride);
6de9cd9a
DN
8040 }
8041
700535b7 8042 for (n = loop.dimen; n < loop.dimen + codim; n++)
a3935ffc 8043 {
bb033c9a
MM
8044 from = loop.from[n];
8045 to = loop.to[n];
a3935ffc 8046 gfc_conv_descriptor_lbound_set (&loop.pre, parm,
bb033c9a 8047 gfc_rank_cst[n], from);
700535b7 8048 if (n < loop.dimen + codim - 1)
a3935ffc 8049 gfc_conv_descriptor_ubound_set (&loop.pre, parm,
bb033c9a 8050 gfc_rank_cst[n], to);
a3935ffc
TB
8051 }
8052
ad5dd90d 8053 if (se->data_not_needed)
568e8e1e
PT
8054 gfc_conv_descriptor_data_set (&loop.pre, parm,
8055 gfc_index_zero_node);
ad5dd90d 8056 else
568e8e1e 8057 /* Point the data pointer at the 1st element in the section. */
2ff0f488 8058 gfc_get_dataptr_offset (&loop.pre, parm, desc, base,
1d6b7f39 8059 subref_array_target, expr);
6de9cd9a 8060
2ff0f488
JRFS
8061 gfc_conv_descriptor_offset_set (&loop.pre, parm, offset);
8062
7a70c12d
RS
8063 desc = parm;
8064 }
6de9cd9a 8065
1792349b
AV
8066 /* For class arrays add the class tree into the saved descriptor to
8067 enable getting of _vptr and the like. */
8068 if (expr->expr_type == EXPR_VARIABLE && VAR_P (desc)
b8ac4f3b 8069 && IS_CLASS_ARRAY (expr->symtree->n.sym))
1792349b
AV
8070 {
8071 gfc_allocate_lang_decl (desc);
8072 GFC_DECL_SAVED_DESCRIPTOR (desc) =
b8ac4f3b
AV
8073 DECL_LANG_SPECIFIC (expr->symtree->n.sym->backend_decl) ?
8074 GFC_DECL_SAVED_DESCRIPTOR (expr->symtree->n.sym->backend_decl)
8075 : expr->symtree->n.sym->backend_decl;
1792349b 8076 }
574284e9
AV
8077 else if (expr->expr_type == EXPR_ARRAY && VAR_P (desc)
8078 && IS_CLASS_ARRAY (expr))
8079 {
8080 tree vtype;
8081 gfc_allocate_lang_decl (desc);
8082 tmp = gfc_create_var (expr->ts.u.derived->backend_decl, "class");
8083 GFC_DECL_SAVED_DESCRIPTOR (desc) = tmp;
8084 vtype = gfc_class_vptr_get (tmp);
8085 gfc_add_modify (&se->pre, vtype,
8086 gfc_build_addr_expr (TREE_TYPE (vtype),
8087 gfc_find_vtab (&expr->ts)->backend_decl));
8088 }
99d821c0 8089 if (!se->direct_byref || se->byref_noassign)
7a70c12d
RS
8090 {
8091 /* Get a pointer to the new descriptor. */
8092 if (se->want_pointer)
628c189e 8093 se->expr = gfc_build_addr_expr (NULL_TREE, desc);
7a70c12d
RS
8094 else
8095 se->expr = desc;
6de9cd9a
DN
8096 }
8097
8098 gfc_add_block_to_block (&se->pre, &loop.pre);
8099 gfc_add_block_to_block (&se->post, &loop.post);
8100
8101 /* Cleanup the scalarizer. */
8102 gfc_cleanup_loop (&loop);
8103}
8104
00f6de9c
TB
8105
8106/* Calculate the array size (number of elements); if dim != NULL_TREE,
8107 return size for that dim (dim=0..rank-1; only for GFC_DESCRIPTOR_TYPE_P). */
8108tree
8109gfc_tree_array_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree dim)
8110{
8111 if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
8112 {
8113 gcc_assert (dim == NULL_TREE);
8114 return GFC_TYPE_ARRAY_SIZE (TREE_TYPE (desc));
8115 }
8116 tree size, tmp, rank = NULL_TREE, cond = NULL_TREE;
8117 symbol_attribute attr = gfc_expr_attr (expr);
8118 gfc_array_spec *as = gfc_get_full_arrayspec_from_expr (expr);
8119 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
8120 if ((!attr.pointer && !attr.allocatable && as && as->type == AS_ASSUMED_RANK)
8121 || !dim)
8122 {
8123 if (expr->rank < 0)
8124 rank = fold_convert (signed_char_type_node,
8125 gfc_conv_descriptor_rank (desc));
8126 else
8127 rank = build_int_cst (signed_char_type_node, expr->rank);
8128 }
8129
8130 if (dim || expr->rank == 1)
8131 {
8132 if (!dim)
8133 dim = gfc_index_zero_node;
8134 tree ubound = gfc_conv_descriptor_ubound_get (desc, dim);
8135 tree lbound = gfc_conv_descriptor_lbound_get (desc, dim);
8136
8137 size = fold_build2_loc (input_location, MINUS_EXPR,
8138 gfc_array_index_type, ubound, lbound);
8139 size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8140 size, gfc_index_one_node);
8141 /* if (!allocatable && !pointer && assumed rank)
8142 size = (idx == rank && ubound[rank-1] == -1 ? -1 : size;
8143 else
8144 size = max (0, size); */
8145 size = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
8146 size, gfc_index_zero_node);
8147 if (!attr.pointer && !attr.allocatable
8148 && as && as->type == AS_ASSUMED_RANK)
8149 {
8150 tmp = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
8151 rank, build_int_cst (signed_char_type_node, 1));
8152 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8153 fold_convert (signed_char_type_node, dim),
8154 tmp);
8155 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8156 gfc_conv_descriptor_ubound_get (desc, dim),
8157 build_int_cst (gfc_array_index_type, -1));
8158 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
8159 cond, tmp);
8160 tmp = build_int_cst (gfc_array_index_type, -1);
8161 size = build3_loc (input_location, COND_EXPR, gfc_array_index_type,
8162 cond, tmp, size);
8163 }
8164 return size;
8165 }
8166
8167 /* size = 1. */
8168 size = gfc_create_var (gfc_array_index_type, "size");
8169 gfc_add_modify (block, size, build_int_cst (TREE_TYPE (size), 1));
8170 tree extent = gfc_create_var (gfc_array_index_type, "extent");
8171
8172 stmtblock_t cond_block, loop_body;
8173 gfc_init_block (&cond_block);
8174 gfc_init_block (&loop_body);
8175
8176 /* Loop: for (i = 0; i < rank; ++i). */
8177 tree idx = gfc_create_var (signed_char_type_node, "idx");
8178 /* Loop body. */
8179 /* #if (assumed-rank + !allocatable && !pointer)
8180 if (idx == rank - 1 && dim[idx].ubound == -1)
8181 extent = -1;
8182 else
8183 #endif
8184 extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1
8185 if (extent < 0)
8186 extent = 0
8187 size *= extent. */
8188 cond = NULL_TREE;
8189 if (!attr.pointer && !attr.allocatable && as && as->type == AS_ASSUMED_RANK)
8190 {
8191 tmp = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
8192 rank, build_int_cst (signed_char_type_node, 1));
8193 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8194 idx, tmp);
8195 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8196 gfc_conv_descriptor_ubound_get (desc, idx),
8197 build_int_cst (gfc_array_index_type, -1));
8198 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
8199 cond, tmp);
8200 }
8201 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8202 gfc_conv_descriptor_ubound_get (desc, idx),
8203 gfc_conv_descriptor_lbound_get (desc, idx));
8204 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8205 tmp, gfc_index_one_node);
8206 gfc_add_modify (&cond_block, extent, tmp);
8207 tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
8208 extent, gfc_index_zero_node);
8209 tmp = build3_v (COND_EXPR, tmp,
8210 fold_build2_loc (input_location, MODIFY_EXPR,
8211 gfc_array_index_type,
8212 extent, gfc_index_zero_node),
8213 build_empty_stmt (input_location));
8214 gfc_add_expr_to_block (&cond_block, tmp);
8215 tmp = gfc_finish_block (&cond_block);
8216 if (cond)
8217 tmp = build3_v (COND_EXPR, cond,
8218 fold_build2_loc (input_location, MODIFY_EXPR,
8219 gfc_array_index_type, extent,
8220 build_int_cst (gfc_array_index_type, -1)),
8221 tmp);
8222 gfc_add_expr_to_block (&loop_body, tmp);
8223 /* size *= extent. */
8224 gfc_add_modify (&loop_body, size,
8225 fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8226 size, extent));
8227 /* Generate loop. */
8228 gfc_simple_for_loop (block, idx, build_int_cst (TREE_TYPE (idx), 0), rank, LT_EXPR,
8229 build_int_cst (TREE_TYPE (idx), 1),
8230 gfc_finish_block (&loop_body));
8231 return size;
8232}
8233
7e279142
JJ
8234/* Helper function for gfc_conv_array_parameter if array size needs to be
8235 computed. */
8236
8237static void
00f6de9c 8238array_parameter_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree *size)
7e279142
JJ
8239{
8240 tree elem;
00f6de9c 8241 *size = gfc_tree_array_size (block, desc, expr, NULL);
7e279142 8242 elem = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
94471a56
TB
8243 *size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8244 *size, fold_convert (gfc_array_index_type, elem));
7e279142 8245}
6de9cd9a 8246
bf09e559 8247/* Helper function - return true if the argument is a pointer. */
94f3d11c 8248
bf09e559
TK
8249static bool
8250is_pointer (gfc_expr *e)
8251{
8252 gfc_symbol *sym;
8253
8254 if (e->expr_type != EXPR_VARIABLE || e->symtree == NULL)
8255 return false;
8256
8257 sym = e->symtree->n.sym;
8258 if (sym == NULL)
8259 return false;
8260
8261 return sym->attr.pointer || sym->attr.proc_pointer;
8262}
8263
6de9cd9a 8264/* Convert an array for passing as an actual parameter. */
6de9cd9a
DN
8265
8266void
2960a368 8267gfc_conv_array_parameter (gfc_se * se, gfc_expr * expr, bool g77,
7e279142
JJ
8268 const gfc_symbol *fsym, const char *proc_name,
8269 tree *size)
6de9cd9a
DN
8270{
8271 tree ptr;
8272 tree desc;
bd075cf2 8273 tree tmp = NULL_TREE;
6de9cd9a 8274 tree stmt;
b2b247f9 8275 tree parent = DECL_CONTEXT (current_function_decl);
17555e7e
PT
8276 bool full_array_var;
8277 bool this_array_result;
8278 bool contiguous;
f7172b55 8279 bool no_pack;
2542496c
PT
8280 bool array_constructor;
8281 bool good_allocatable;
ba461991
PT
8282 bool ultimate_ptr_comp;
8283 bool ultimate_alloc_comp;
6de9cd9a
DN
8284 gfc_symbol *sym;
8285 stmtblock_t block;
17555e7e
PT
8286 gfc_ref *ref;
8287
ba461991
PT
8288 ultimate_ptr_comp = false;
8289 ultimate_alloc_comp = false;
fe4e525c 8290
17555e7e 8291 for (ref = expr->ref; ref; ref = ref->next)
ba461991
PT
8292 {
8293 if (ref->next == NULL)
8294 break;
8295
8296 if (ref->type == REF_COMPONENT)
8297 {
8298 ultimate_ptr_comp = ref->u.c.component->attr.pointer;
8299 ultimate_alloc_comp = ref->u.c.component->attr.allocatable;
8300 }
8301 }
17555e7e
PT
8302
8303 full_array_var = false;
8304 contiguous = false;
8305
ba461991 8306 if (expr->expr_type == EXPR_VARIABLE && ref && !ultimate_ptr_comp)
17555e7e 8307 full_array_var = gfc_full_array_ref_p (ref, &contiguous);
6de9cd9a 8308
b2b247f9
PT
8309 sym = full_array_var ? expr->symtree->n.sym : NULL;
8310
18b0679f 8311 /* The symbol should have an array specification. */
17555e7e 8312 gcc_assert (!sym || sym->as || ref->u.ar.as);
18b0679f 8313
0ee8e250
PT
8314 if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_CHARACTER)
8315 {
8316 get_array_ctor_strlen (&se->pre, expr->value.constructor, &tmp);
bc21d315 8317 expr->ts.u.cl->backend_decl = tmp;
f2d3cb25 8318 se->string_length = tmp;
0ee8e250
PT
8319 }
8320
b2b247f9
PT
8321 /* Is this the result of the enclosing procedure? */
8322 this_array_result = (full_array_var && sym->attr.flavor == FL_PROCEDURE);
8323 if (this_array_result
8324 && (sym->backend_decl != current_function_decl)
8325 && (sym->backend_decl != parent))
8326 this_array_result = false;
8327
6de9cd9a 8328 /* Passing address of the array if it is not pointer or assumed-shape. */
ea73447a
JW
8329 if (full_array_var && g77 && !this_array_result
8330 && sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
6de9cd9a 8331 {
b122dc6a 8332 tmp = gfc_get_symbol_decl (sym);
83d890b9 8333
20c9dc8a 8334 if (sym->ts.type == BT_CHARACTER)
bc21d315 8335 se->string_length = sym->ts.u.cl->backend_decl;
17555e7e 8336
f7172b55 8337 if (!sym->attr.pointer
c62c6622 8338 && sym->as
f04986a9 8339 && sym->as->type != AS_ASSUMED_SHAPE
2d98d2b4 8340 && sym->as->type != AS_DEFERRED
f04986a9 8341 && sym->as->type != AS_ASSUMED_RANK
c62c6622 8342 && !sym->attr.allocatable)
6de9cd9a 8343 {
346d5977 8344 /* Some variables are declared directly, others are declared as
841b0c1f
PB
8345 pointers and allocated on the heap. */
8346 if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
8347 se->expr = tmp;
6de9cd9a 8348 else
628c189e 8349 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
7e279142 8350 if (size)
00f6de9c 8351 array_parameter_size (&se->pre, tmp, expr, size);
6de9cd9a
DN
8352 return;
8353 }
17555e7e 8354
6de9cd9a
DN
8355 if (sym->attr.allocatable)
8356 {
237b2f1b 8357 if (sym->attr.dummy || sym->attr.result)
7f0d6da9 8358 {
2960a368 8359 gfc_conv_expr_descriptor (se, expr);
7e279142 8360 tmp = se->expr;
7f0d6da9 8361 }
7e279142 8362 if (size)
00f6de9c 8363 array_parameter_size (&se->pre, tmp, expr, size);
7e279142 8364 se->expr = gfc_conv_array_data (tmp);
6de9cd9a
DN
8365 return;
8366 }
8367 }
8368
ba461991
PT
8369 /* A convenient reduction in scope. */
8370 contiguous = g77 && !this_array_result && contiguous;
8371
2542496c 8372 /* There is no need to pack and unpack the array, if it is contiguous
fe4e525c
TB
8373 and not a deferred- or assumed-shape array, or if it is simply
8374 contiguous. */
f7172b55
PT
8375 no_pack = ((sym && sym->as
8376 && !sym->attr.pointer
8377 && sym->as->type != AS_DEFERRED
c62c6622 8378 && sym->as->type != AS_ASSUMED_RANK
f7172b55
PT
8379 && sym->as->type != AS_ASSUMED_SHAPE)
8380 ||
8381 (ref && ref->u.ar.as
8382 && ref->u.ar.as->type != AS_DEFERRED
c62c6622 8383 && ref->u.ar.as->type != AS_ASSUMED_RANK
fe4e525c
TB
8384 && ref->u.ar.as->type != AS_ASSUMED_SHAPE)
8385 ||
460263d0 8386 gfc_is_simply_contiguous (expr, false, true));
f7172b55 8387
ba461991 8388 no_pack = contiguous && no_pack;
f7172b55 8389
5f8865c3
TK
8390 /* If we have an EXPR_OP or a function returning an explicit-shaped
8391 or allocatable array, an array temporary will be generated which
8392 does not need to be packed / unpacked if passed to an
8393 explicit-shape dummy array. */
7dc3df08 8394
5f8865c3
TK
8395 if (g77)
8396 {
8397 if (expr->expr_type == EXPR_OP)
8398 no_pack = 1;
8399 else if (expr->expr_type == EXPR_FUNCTION && expr->value.function.esym)
8400 {
8401 gfc_symbol *result = expr->value.function.esym->result;
8402 if (result->attr.dimension
8ef8fa9a
TK
8403 && (result->as->type == AS_EXPLICIT
8404 || result->attr.allocatable
8405 || result->attr.contiguous))
5f8865c3
TK
8406 no_pack = 1;
8407 }
8408 }
7dc3df08 8409
2542496c
PT
8410 /* Array constructors are always contiguous and do not need packing. */
8411 array_constructor = g77 && !this_array_result && expr->expr_type == EXPR_ARRAY;
8412
8413 /* Same is true of contiguous sections from allocatable variables. */
ba461991
PT
8414 good_allocatable = contiguous
8415 && expr->symtree
8416 && expr->symtree->n.sym->attr.allocatable;
8417
8418 /* Or ultimate allocatable components. */
f04986a9 8419 ultimate_alloc_comp = contiguous && ultimate_alloc_comp;
f7172b55 8420
ba461991 8421 if (no_pack || array_constructor || good_allocatable || ultimate_alloc_comp)
17555e7e 8422 {
2960a368 8423 gfc_conv_expr_descriptor (se, expr);
1b961de9
PT
8424 /* Deallocate the allocatable components of structures that are
8425 not variable. */
8426 if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
8427 && expr->ts.u.derived->attr.alloc_comp
8428 && expr->expr_type != EXPR_VARIABLE)
8429 {
8430 tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, se->expr, expr->rank);
8431
8432 /* The components shall be deallocated before their containing entity. */
8433 gfc_prepend_expr_to_block (&se->post, tmp);
8434 }
6b7a9826 8435 if (expr->ts.type == BT_CHARACTER && expr->expr_type != EXPR_FUNCTION)
17555e7e
PT
8436 se->string_length = expr->ts.u.cl->backend_decl;
8437 if (size)
00f6de9c 8438 array_parameter_size (&se->pre, se->expr, expr, size);
17555e7e
PT
8439 se->expr = gfc_conv_array_data (se->expr);
8440 return;
8441 }
8442
b2b247f9
PT
8443 if (this_array_result)
8444 {
8445 /* Result of the enclosing function. */
2960a368 8446 gfc_conv_expr_descriptor (se, expr);
7e279142 8447 if (size)
00f6de9c 8448 array_parameter_size (&se->pre, se->expr, expr, size);
628c189e 8449 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
b2b247f9
PT
8450
8451 if (g77 && TREE_TYPE (TREE_TYPE (se->expr)) != NULL_TREE
8452 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
db3927fb
AH
8453 se->expr = gfc_conv_array_data (build_fold_indirect_ref_loc (input_location,
8454 se->expr));
b2b247f9
PT
8455
8456 return;
8457 }
8458 else
8459 {
8460 /* Every other type of array. */
8461 se->want_pointer = 1;
2960a368 8462 gfc_conv_expr_descriptor (se, expr);
ff3598bc 8463
7e279142 8464 if (size)
00f6de9c
TB
8465 array_parameter_size (&se->pre,
8466 build_fold_indirect_ref_loc (input_location,
8467 se->expr),
8468 expr, size);
b2b247f9
PT
8469 }
8470
5046aff5 8471 /* Deallocate the allocatable components of structures that are
0e1f8c6a
MM
8472 not variable, for descriptorless arguments.
8473 Arguments with a descriptor are handled in gfc_conv_procedure_call. */
8474 if (g77 && (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
8475 && expr->ts.u.derived->attr.alloc_comp
8476 && expr->expr_type != EXPR_VARIABLE)
5046aff5 8477 {
46b2c440 8478 tmp = build_fold_indirect_ref_loc (input_location, se->expr);
bc21d315 8479 tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
46b2c440
MM
8480
8481 /* The components shall be deallocated before their containing entity. */
8482 gfc_prepend_expr_to_block (&se->post, tmp);
5046aff5
PT
8483 }
8484
fe4e525c 8485 if (g77 || (fsym && fsym->attr.contiguous
460263d0 8486 && !gfc_is_simply_contiguous (expr, false, true)))
6de9cd9a 8487 {
fe4e525c
TB
8488 tree origptr = NULL_TREE;
8489
6de9cd9a 8490 desc = se->expr;
fe4e525c
TB
8491
8492 /* For contiguous arrays, save the original value of the descriptor. */
8493 if (!g77)
8494 {
8495 origptr = gfc_create_var (pvoid_type_node, "origptr");
8496 tmp = build_fold_indirect_ref_loc (input_location, desc);
8497 tmp = gfc_conv_array_data (tmp);
94471a56
TB
8498 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8499 TREE_TYPE (origptr), origptr,
8500 fold_convert (TREE_TYPE (origptr), tmp));
fe4e525c
TB
8501 gfc_add_expr_to_block (&se->pre, tmp);
8502 }
8503
6de9cd9a 8504 /* Repack the array. */
73e42eef 8505 if (warn_array_temporaries)
0d52899f
TB
8506 {
8507 if (fsym)
48749dbc
MLI
8508 gfc_warning (OPT_Warray_temporaries,
8509 "Creating array temporary at %L for argument %qs",
0d52899f
TB
8510 &expr->where, fsym->name);
8511 else
48749dbc
MLI
8512 gfc_warning (OPT_Warray_temporaries,
8513 "Creating array temporary at %L", &expr->where);
0d52899f 8514 }
bdfd2ff0 8515
bf09e559
TK
8516 /* When optmizing, we can use gfc_conv_subref_array_arg for
8517 making the packing and unpacking operation visible to the
8518 optimizers. */
8519
95d27703 8520 if (g77 && flag_inline_arg_packing && expr->expr_type == EXPR_VARIABLE
1585b483 8521 && !is_pointer (expr) && ! gfc_has_dimen_vector_ref (expr)
0cc063af
TK
8522 && !(expr->symtree->n.sym->as
8523 && expr->symtree->n.sym->as->type == AS_ASSUMED_RANK)
1585b483 8524 && (fsym == NULL || fsym->ts.type != BT_ASSUMED))
bf09e559
TK
8525 {
8526 gfc_conv_subref_array_arg (se, expr, g77,
8527 fsym ? fsym->attr.intent : INTENT_INOUT,
1585b483 8528 false, fsym, proc_name, sym, true);
bf09e559
TK
8529 return;
8530 }
8531
db3927fb
AH
8532 ptr = build_call_expr_loc (input_location,
8533 gfor_fndecl_in_pack, 1, desc);
0d52899f
TB
8534
8535 if (fsym && fsym->attr.optional && sym && sym->attr.optional)
8536 {
8537 tmp = gfc_conv_expr_present (sym);
5d44e5c8
TB
8538 ptr = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
8539 tmp, fold_convert (TREE_TYPE (se->expr), ptr),
6e1b67b3 8540 fold_convert (TREE_TYPE (se->expr), null_pointer_node));
0d52899f
TB
8541 }
8542
6de9cd9a 8543 ptr = gfc_evaluate_now (ptr, &se->pre);
0d52899f 8544
fe4e525c
TB
8545 /* Use the packed data for the actual argument, except for contiguous arrays,
8546 where the descriptor's data component is set. */
8547 if (g77)
8548 se->expr = ptr;
8549 else
8550 {
8551 tmp = build_fold_indirect_ref_loc (input_location, desc);
88719f2d
MM
8552
8553 gfc_ss * ss = gfc_walk_expr (expr);
8554 if (!transposed_dims (ss))
8555 gfc_conv_descriptor_data_set (&se->pre, tmp, ptr);
8556 else
8557 {
8558 tree old_field, new_field;
8559
8560 /* The original descriptor has transposed dims so we can't reuse
8561 it directly; we have to create a new one. */
8562 tree old_desc = tmp;
8563 tree new_desc = gfc_create_var (TREE_TYPE (old_desc), "arg_desc");
8564
8565 old_field = gfc_conv_descriptor_dtype (old_desc);
8566 new_field = gfc_conv_descriptor_dtype (new_desc);
8567 gfc_add_modify (&se->pre, new_field, old_field);
8568
8569 old_field = gfc_conv_descriptor_offset (old_desc);
8570 new_field = gfc_conv_descriptor_offset (new_desc);
8571 gfc_add_modify (&se->pre, new_field, old_field);
8572
8573 for (int i = 0; i < expr->rank; i++)
8574 {
8575 old_field = gfc_conv_descriptor_dimension (old_desc,
8576 gfc_rank_cst[get_array_ref_dim_for_loop_dim (ss, i)]);
8577 new_field = gfc_conv_descriptor_dimension (new_desc,
8578 gfc_rank_cst[i]);
8579 gfc_add_modify (&se->pre, new_field, old_field);
8580 }
8581
f19626cf 8582 if (flag_coarray == GFC_FCOARRAY_LIB
88719f2d
MM
8583 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (old_desc))
8584 && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (old_desc))
8585 == GFC_ARRAY_ALLOCATABLE)
8586 {
8587 old_field = gfc_conv_descriptor_token (old_desc);
8588 new_field = gfc_conv_descriptor_token (new_desc);
8589 gfc_add_modify (&se->pre, new_field, old_field);
8590 }
8591
8592 gfc_conv_descriptor_data_set (&se->pre, new_desc, ptr);
8593 se->expr = gfc_build_addr_expr (NULL_TREE, new_desc);
8594 }
8595 gfc_free_ss (ss);
fe4e525c 8596 }
6de9cd9a 8597
d3d3011f 8598 if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
0d52899f
TB
8599 {
8600 char * msg;
8601
8602 if (fsym && proc_name)
1a33dc9e
UB
8603 msg = xasprintf ("An array temporary was created for argument "
8604 "'%s' of procedure '%s'", fsym->name, proc_name);
0d52899f 8605 else
1a33dc9e 8606 msg = xasprintf ("An array temporary was created");
0d52899f 8607
db3927fb
AH
8608 tmp = build_fold_indirect_ref_loc (input_location,
8609 desc);
0d52899f 8610 tmp = gfc_conv_array_data (tmp);
63ee5404 8611 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
94471a56 8612 fold_convert (TREE_TYPE (tmp), ptr), tmp);
0d52899f
TB
8613
8614 if (fsym && fsym->attr.optional && sym && sym->attr.optional)
94471a56 8615 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404 8616 logical_type_node,
94471a56 8617 gfc_conv_expr_present (sym), tmp);
0d52899f
TB
8618
8619 gfc_trans_runtime_check (false, true, tmp, &se->pre,
8620 &expr->where, msg);
cede9502 8621 free (msg);
0d52899f
TB
8622 }
8623
6de9cd9a
DN
8624 gfc_start_block (&block);
8625
8626 /* Copy the data back. */
0d52899f
TB
8627 if (fsym == NULL || fsym->attr.intent != INTENT_IN)
8628 {
db3927fb
AH
8629 tmp = build_call_expr_loc (input_location,
8630 gfor_fndecl_in_unpack, 2, desc, ptr);
0d52899f
TB
8631 gfc_add_expr_to_block (&block, tmp);
8632 }
6de9cd9a
DN
8633
8634 /* Free the temporary. */
107051a5 8635 tmp = gfc_call_free (ptr);
6de9cd9a
DN
8636 gfc_add_expr_to_block (&block, tmp);
8637
8638 stmt = gfc_finish_block (&block);
8639
8640 gfc_init_block (&block);
8641 /* Only if it was repacked. This code needs to be executed before the
8642 loop cleanup code. */
db3927fb
AH
8643 tmp = build_fold_indirect_ref_loc (input_location,
8644 desc);
6de9cd9a 8645 tmp = gfc_conv_array_data (tmp);
63ee5404 8646 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
94471a56 8647 fold_convert (TREE_TYPE (tmp), ptr), tmp);
0d52899f
TB
8648
8649 if (fsym && fsym->attr.optional && sym && sym->attr.optional)
94471a56 8650 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404 8651 logical_type_node,
94471a56 8652 gfc_conv_expr_present (sym), tmp);
0d52899f 8653
c2255bc4 8654 tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
6de9cd9a
DN
8655
8656 gfc_add_expr_to_block (&block, tmp);
8657 gfc_add_block_to_block (&block, &se->post);
8658
8659 gfc_init_block (&se->post);
fe4e525c
TB
8660
8661 /* Reset the descriptor pointer. */
8662 if (!g77)
8663 {
8664 tmp = build_fold_indirect_ref_loc (input_location, desc);
8665 gfc_conv_descriptor_data_set (&se->post, tmp, origptr);
8666 }
8667
6de9cd9a
DN
8668 gfc_add_block_to_block (&se->post, &block);
8669 }
8670}
8671
8672
5046aff5
PT
8673/* This helper function calculates the size in words of a full array. */
8674
92d28cbb
JJ
8675tree
8676gfc_full_array_size (stmtblock_t *block, tree decl, int rank)
5046aff5
PT
8677{
8678 tree idx;
8679 tree nelems;
8680 tree tmp;
8681 idx = gfc_rank_cst[rank - 1];
568e8e1e
PT
8682 nelems = gfc_conv_descriptor_ubound_get (decl, idx);
8683 tmp = gfc_conv_descriptor_lbound_get (decl, idx);
94471a56
TB
8684 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8685 nelems, tmp);
8686 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8687 tmp, gfc_index_one_node);
5046aff5
PT
8688 tmp = gfc_evaluate_now (tmp, block);
8689
568e8e1e 8690 nelems = gfc_conv_descriptor_stride_get (decl, idx);
94471a56
TB
8691 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8692 nelems, tmp);
5046aff5
PT
8693 return gfc_evaluate_now (tmp, block);
8694}
42a0e16c 8695
5046aff5 8696
40c32948
PT
8697/* Allocate dest to the same size as src, and copy src -> dest.
8698 If no_malloc is set, only the copy is done. */
5046aff5 8699
40c32948 8700static tree
94471a56 8701duplicate_allocatable (tree dest, tree src, tree type, int rank,
fc7d0afb
AV
8702 bool no_malloc, bool no_memcpy, tree str_sz,
8703 tree add_when_allocated)
5046aff5
PT
8704{
8705 tree tmp;
8706 tree size;
8707 tree nelems;
5046aff5
PT
8708 tree null_cond;
8709 tree null_data;
8710 stmtblock_t block;
8711
40c32948
PT
8712 /* If the source is null, set the destination to null. Then,
8713 allocate memory to the destination. */
5046aff5 8714 gfc_init_block (&block);
5046aff5 8715
14c96bca 8716 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
40c32948 8717 {
ba85c8c3 8718 gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
40c32948
PT
8719 null_data = gfc_finish_block (&block);
8720
8721 gfc_init_block (&block);
2b3dc0db
PT
8722 if (str_sz != NULL_TREE)
8723 size = str_sz;
8724 else
8725 size = TYPE_SIZE_UNIT (TREE_TYPE (type));
8726
40c32948
PT
8727 if (!no_malloc)
8728 {
8729 tmp = gfc_call_malloc (&block, type, size);
ba85c8c3 8730 gfc_add_modify (&block, dest, fold_convert (type, tmp));
40c32948
PT
8731 }
8732
92d28cbb
JJ
8733 if (!no_memcpy)
8734 {
8735 tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
8736 tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
8737 fold_convert (size_type_node, size));
8738 gfc_add_expr_to_block (&block, tmp);
8739 }
40c32948
PT
8740 }
8741 else
8742 {
8743 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
8744 null_data = gfc_finish_block (&block);
8745
8746 gfc_init_block (&block);
14c96bca 8747 if (rank)
92d28cbb 8748 nelems = gfc_full_array_size (&block, src, rank);
14c96bca
TB
8749 else
8750 nelems = gfc_index_one_node;
8751
2b3dc0db
PT
8752 if (str_sz != NULL_TREE)
8753 tmp = fold_convert (gfc_array_index_type, str_sz);
8754 else
8755 tmp = fold_convert (gfc_array_index_type,
8756 TYPE_SIZE_UNIT (gfc_get_element_type (type)));
94471a56
TB
8757 size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8758 nelems, tmp);
40c32948
PT
8759 if (!no_malloc)
8760 {
8761 tmp = TREE_TYPE (gfc_conv_descriptor_data_get (src));
8762 tmp = gfc_call_malloc (&block, tmp, size);
8763 gfc_conv_descriptor_data_set (&block, dest, tmp);
8764 }
8765
8766 /* We know the temporary and the value will be the same length,
8767 so can use memcpy. */
92d28cbb
JJ
8768 if (!no_memcpy)
8769 {
8770 tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
8771 tmp = build_call_expr_loc (input_location, tmp, 3,
8772 gfc_conv_descriptor_data_get (dest),
8773 gfc_conv_descriptor_data_get (src),
8774 fold_convert (size_type_node, size));
8775 gfc_add_expr_to_block (&block, tmp);
8776 }
40c32948 8777 }
5046aff5 8778
fc7d0afb 8779 gfc_add_expr_to_block (&block, add_when_allocated);
42a0e16c
PT
8780 tmp = gfc_finish_block (&block);
8781
5046aff5
PT
8782 /* Null the destination if the source is null; otherwise do
8783 the allocate and copy. */
14c96bca 8784 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
40c32948
PT
8785 null_cond = src;
8786 else
8787 null_cond = gfc_conv_descriptor_data_get (src);
8788
5046aff5 8789 null_cond = convert (pvoid_type_node, null_cond);
63ee5404 8790 null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
94471a56 8791 null_cond, null_pointer_node);
5046aff5
PT
8792 return build3_v (COND_EXPR, null_cond, tmp, null_data);
8793}
8794
8795
40c32948
PT
8796/* Allocate dest to the same size as src, and copy data src -> dest. */
8797
8798tree
fc7d0afb
AV
8799gfc_duplicate_allocatable (tree dest, tree src, tree type, int rank,
8800 tree add_when_allocated)
40c32948 8801{
92d28cbb 8802 return duplicate_allocatable (dest, src, type, rank, false, false,
fc7d0afb 8803 NULL_TREE, add_when_allocated);
40c32948
PT
8804}
8805
8806
8807/* Copy data src -> dest. */
8808
8809tree
8810gfc_copy_allocatable_data (tree dest, tree src, tree type, int rank)
8811{
92d28cbb 8812 return duplicate_allocatable (dest, src, type, rank, true, false,
fc7d0afb 8813 NULL_TREE, NULL_TREE);
92d28cbb
JJ
8814}
8815
8816/* Allocate dest to the same size as src, but don't copy anything. */
8817
8818tree
8819gfc_duplicate_allocatable_nocopy (tree dest, tree src, tree type, int rank)
8820{
fc7d0afb
AV
8821 return duplicate_allocatable (dest, src, type, rank, false, true,
8822 NULL_TREE, NULL_TREE);
40c32948
PT
8823}
8824
8825
ba85c8c3
AV
8826static tree
8827duplicate_allocatable_coarray (tree dest, tree dest_tok, tree src,
8828 tree type, int rank)
8829{
8830 tree tmp;
8831 tree size;
8832 tree nelems;
8833 tree null_cond;
8834 tree null_data;
8835 stmtblock_t block, globalblock;
8836
8837 /* If the source is null, set the destination to null. Then,
8838 allocate memory to the destination. */
8839 gfc_init_block (&block);
8840 gfc_init_block (&globalblock);
8841
8842 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
8843 {
8844 gfc_se se;
8845 symbol_attribute attr;
8846 tree dummy_desc;
8847
8848 gfc_init_se (&se, NULL);
e0396d77
AV
8849 gfc_clear_attr (&attr);
8850 attr.allocatable = 1;
ba85c8c3
AV
8851 dummy_desc = gfc_conv_scalar_to_descriptor (&se, dest, attr);
8852 gfc_add_block_to_block (&globalblock, &se.pre);
8853 size = TYPE_SIZE_UNIT (TREE_TYPE (type));
8854
8855 gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
8856 gfc_allocate_using_caf_lib (&block, dummy_desc, size,
8857 gfc_build_addr_expr (NULL_TREE, dest_tok),
8858 NULL_TREE, NULL_TREE, NULL_TREE,
8859 GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
8860 null_data = gfc_finish_block (&block);
8861
8862 gfc_init_block (&block);
8863
8864 gfc_allocate_using_caf_lib (&block, dummy_desc,
8865 fold_convert (size_type_node, size),
8866 gfc_build_addr_expr (NULL_TREE, dest_tok),
8867 NULL_TREE, NULL_TREE, NULL_TREE,
8868 GFC_CAF_COARRAY_ALLOC);
8869
8870 tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
8871 tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
8872 fold_convert (size_type_node, size));
8873 gfc_add_expr_to_block (&block, tmp);
8874 }
8875 else
8876 {
8877 /* Set the rank or unitialized memory access may be reported. */
7fb43006 8878 tmp = gfc_conv_descriptor_rank (dest);
ba85c8c3
AV
8879 gfc_add_modify (&globalblock, tmp, build_int_cst (TREE_TYPE (tmp), rank));
8880
8881 if (rank)
8882 nelems = gfc_full_array_size (&block, src, rank);
8883 else
8884 nelems = integer_one_node;
8885
8886 tmp = fold_convert (size_type_node,
8887 TYPE_SIZE_UNIT (gfc_get_element_type (type)));
8888 size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
8889 fold_convert (size_type_node, nelems), tmp);
8890
8891 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
8892 gfc_allocate_using_caf_lib (&block, dest, fold_convert (size_type_node,
8893 size),
8894 gfc_build_addr_expr (NULL_TREE, dest_tok),
8895 NULL_TREE, NULL_TREE, NULL_TREE,
8896 GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
8897 null_data = gfc_finish_block (&block);
8898
8899 gfc_init_block (&block);
8900 gfc_allocate_using_caf_lib (&block, dest,
8901 fold_convert (size_type_node, size),
8902 gfc_build_addr_expr (NULL_TREE, dest_tok),
8903 NULL_TREE, NULL_TREE, NULL_TREE,
8904 GFC_CAF_COARRAY_ALLOC);
8905
8906 tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
8907 tmp = build_call_expr_loc (input_location, tmp, 3,
8908 gfc_conv_descriptor_data_get (dest),
8909 gfc_conv_descriptor_data_get (src),
8910 fold_convert (size_type_node, size));
8911 gfc_add_expr_to_block (&block, tmp);
8912 }
8913
8914 tmp = gfc_finish_block (&block);
8915
8916 /* Null the destination if the source is null; otherwise do
8917 the register and copy. */
8918 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
8919 null_cond = src;
8920 else
8921 null_cond = gfc_conv_descriptor_data_get (src);
8922
8923 null_cond = convert (pvoid_type_node, null_cond);
63ee5404 8924 null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
ba85c8c3
AV
8925 null_cond, null_pointer_node);
8926 gfc_add_expr_to_block (&globalblock, build3_v (COND_EXPR, null_cond, tmp,
8927 null_data));
8928 return gfc_finish_block (&globalblock);
8929}
8930
8931
8932/* Helper function to abstract whether coarray processing is enabled. */
8933
8934static bool
8935caf_enabled (int caf_mode)
8936{
8937 return (caf_mode & GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY)
8938 == GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY;
8939}
8940
8941
8942/* Helper function to abstract whether coarray processing is enabled
8943 and we are in a derived type coarray. */
8944
8945static bool
8946caf_in_coarray (int caf_mode)
8947{
8948 static const int pat = GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
8949 | GFC_STRUCTURE_CAF_MODE_IN_COARRAY;
8950 return (caf_mode & pat) == pat;
8951}
8952
8953
8954/* Helper function to abstract whether coarray is to deallocate only. */
8955
8956bool
8957gfc_caf_is_dealloc_only (int caf_mode)
8958{
8959 return (caf_mode & GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY)
8960 == GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY;
8961}
8962
8963
5046aff5
PT
8964/* Recursively traverse an object of derived type, generating code to
8965 deallocate, nullify or copy allocatable components. This is the work horse
8966 function for the functions named in this enum. */
8967
ba85c8c3 8968enum {DEALLOCATE_ALLOC_COMP = 1, NULLIFY_ALLOC_COMP,
5bab4c96 8969 COPY_ALLOC_COMP, COPY_ONLY_ALLOC_COMP, REASSIGN_CAF_COMP,
c78d3425
AF
8970 ALLOCATE_PDT_COMP, DEALLOCATE_PDT_COMP, CHECK_PDT_DUMMY,
8971 BCAST_ALLOC_COMP};
5bab4c96
PT
8972
8973static gfc_actual_arglist *pdt_param_list;
5046aff5
PT
8974
8975static tree
8976structure_alloc_comps (gfc_symbol * der_type, tree decl,
c78d3425
AF
8977 tree dest, int rank, int purpose, int caf_mode,
8978 gfc_co_subroutines_args *args)
5046aff5
PT
8979{
8980 gfc_component *c;
8981 gfc_loopinfo loop;
8982 stmtblock_t fnblock;
8983 stmtblock_t loopbody;
d6430d9a 8984 stmtblock_t tmpblock;
546a65d9 8985 tree decl_type;
5046aff5
PT
8986 tree tmp;
8987 tree comp;
8988 tree dcmp;
8989 tree nelems;
8990 tree index;
8991 tree var;
8992 tree cdecl;
8993 tree ctype;
8994 tree vref, dref;
8995 tree null_cond = NULL_TREE;
fc7d0afb 8996 tree add_when_allocated;
bf9f15ee 8997 tree dealloc_fndecl;
39da5866 8998 tree caf_token;
bf9f15ee 8999 gfc_symbol *vtab;
39da5866
AV
9000 int caf_dereg_mode;
9001 symbol_attribute *attr;
9002 bool deallocate_called;
5046aff5
PT
9003
9004 gfc_init_block (&fnblock);
9005
546a65d9
PT
9006 decl_type = TREE_TYPE (decl);
9007
fc7d0afb 9008 if ((POINTER_TYPE_P (decl_type))
546a65d9 9009 || (TREE_CODE (decl_type) == REFERENCE_TYPE && rank == 0))
fc7d0afb
AV
9010 {
9011 decl = build_fold_indirect_ref_loc (input_location, decl);
9012 /* Deref dest in sync with decl, but only when it is not NULL. */
9013 if (dest)
9014 dest = build_fold_indirect_ref_loc (input_location, dest);
7114edca 9015
ba85c8c3
AV
9016 /* Update the decl_type because it got dereferenced. */
9017 decl_type = TREE_TYPE (decl);
9018 }
546a65d9 9019
fc7d0afb 9020 /* If this is an array of derived types with allocatable components
5046aff5 9021 build a loop and recursively call this function. */
546a65d9 9022 if (TREE_CODE (decl_type) == ARRAY_TYPE
2be13164 9023 || (GFC_DESCRIPTOR_TYPE_P (decl_type) && rank != 0))
5046aff5
PT
9024 {
9025 tmp = gfc_conv_array_data (decl);
fc7d0afb 9026 var = build_fold_indirect_ref_loc (input_location, tmp);
f04986a9 9027
5046aff5 9028 /* Get the number of elements - 1 and set the counter. */
546a65d9 9029 if (GFC_DESCRIPTOR_TYPE_P (decl_type))
5046aff5
PT
9030 {
9031 /* Use the descriptor for an allocatable array. Since this
9032 is a full array reference, we only need the descriptor
9033 information from dimension = rank. */
92d28cbb 9034 tmp = gfc_full_array_size (&fnblock, decl, rank);
94471a56
TB
9035 tmp = fold_build2_loc (input_location, MINUS_EXPR,
9036 gfc_array_index_type, tmp,
9037 gfc_index_one_node);
5046aff5
PT
9038
9039 null_cond = gfc_conv_descriptor_data_get (decl);
94471a56 9040 null_cond = fold_build2_loc (input_location, NE_EXPR,
63ee5404 9041 logical_type_node, null_cond,
94471a56 9042 build_int_cst (TREE_TYPE (null_cond), 0));
5046aff5
PT
9043 }
9044 else
9045 {
9046 /* Otherwise use the TYPE_DOMAIN information. */
fc7d0afb 9047 tmp = array_type_nelts (decl_type);
5046aff5
PT
9048 tmp = fold_convert (gfc_array_index_type, tmp);
9049 }
9050
9051 /* Remember that this is, in fact, the no. of elements - 1. */
9052 nelems = gfc_evaluate_now (tmp, &fnblock);
9053 index = gfc_create_var (gfc_array_index_type, "S");
9054
9055 /* Build the body of the loop. */
9056 gfc_init_block (&loopbody);
9057
1d6b7f39 9058 vref = gfc_build_array_ref (var, index, NULL);
5046aff5 9059
e00464a5 9060 if (purpose == COPY_ALLOC_COMP || purpose == COPY_ONLY_ALLOC_COMP)
ba85c8c3 9061 {
40c32948 9062 tmp = build_fold_indirect_ref_loc (input_location,
c78d3425 9063 gfc_conv_array_data (dest));
40c32948
PT
9064 dref = gfc_build_array_ref (tmp, index, NULL);
9065 tmp = structure_alloc_comps (der_type, vref, dref, rank,
e00464a5 9066 COPY_ALLOC_COMP, caf_mode, args);
40c32948 9067 }
5046aff5 9068 else
ba85c8c3 9069 tmp = structure_alloc_comps (der_type, vref, NULL_TREE, rank, purpose,
c78d3425 9070 caf_mode, args);
5046aff5
PT
9071
9072 gfc_add_expr_to_block (&loopbody, tmp);
9073
66e4ab31 9074 /* Build the loop and return. */
5046aff5
PT
9075 gfc_init_loopinfo (&loop);
9076 loop.dimen = 1;
9077 loop.from[0] = gfc_index_zero_node;
9078 loop.loopvar[0] = index;
9079 loop.to[0] = nelems;
9080 gfc_trans_scalarizing_loops (&loop, &loopbody);
9081 gfc_add_block_to_block (&fnblock, &loop.pre);
9082
9083 tmp = gfc_finish_block (&fnblock);
fc7d0afb
AV
9084 /* When copying allocateable components, the above implements the
9085 deep copy. Nevertheless is a deep copy only allowed, when the current
9086 component is allocated, for which code will be generated in
9087 gfc_duplicate_allocatable (), where the deep copy code is just added
9088 into the if's body, by adding tmp (the deep copy code) as last
9089 argument to gfc_duplicate_allocatable (). */
9090 if (purpose == COPY_ALLOC_COMP
9091 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
9092 tmp = gfc_duplicate_allocatable (dest, decl, decl_type, rank,
9093 tmp);
9094 else if (null_cond != NULL_TREE)
c2255bc4
AH
9095 tmp = build3_v (COND_EXPR, null_cond, tmp,
9096 build_empty_stmt (input_location));
5046aff5
PT
9097
9098 return tmp;
9099 }
9100
2fcd5884
PT
9101 if (purpose == DEALLOCATE_ALLOC_COMP && der_type->attr.pdt_type)
9102 {
9103 tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
c78d3425 9104 DEALLOCATE_PDT_COMP, 0, args);
2fcd5884
PT
9105 gfc_add_expr_to_block (&fnblock, tmp);
9106 }
9107 else if (purpose == ALLOCATE_PDT_COMP && der_type->attr.alloc_comp)
9108 {
9109 tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
c78d3425 9110 NULLIFY_ALLOC_COMP, 0, args);
2fcd5884
PT
9111 gfc_add_expr_to_block (&fnblock, tmp);
9112 }
9113
5046aff5 9114 /* Otherwise, act on the components or recursively call self to
66e4ab31 9115 act on a chain of components. */
5046aff5
PT
9116 for (c = der_type->components; c; c = c->next)
9117 {
272cec5d
TK
9118 bool cmp_has_alloc_comps = (c->ts.type == BT_DERIVED
9119 || c->ts.type == BT_CLASS)
bc21d315 9120 && c->ts.u.derived->attr.alloc_comp;
39da5866
AV
9121 bool same_type = (c->ts.type == BT_DERIVED && der_type == c->ts.u.derived)
9122 || (c->ts.type == BT_CLASS && der_type == CLASS_DATA (c)->ts.u.derived);
bf9f15ee 9123
0b627b58
PT
9124 bool is_pdt_type = c->ts.type == BT_DERIVED
9125 && c->ts.u.derived->attr.pdt_type;
9126
5046aff5
PT
9127 cdecl = c->backend_decl;
9128 ctype = TREE_TYPE (cdecl);
9129
9130 switch (purpose)
9131 {
c78d3425
AF
9132
9133 case BCAST_ALLOC_COMP:
9134
9135 tree ubound;
9136 tree cdesc;
9137 stmtblock_t derived_type_block;
9138
9139 gfc_init_block (&tmpblock);
9140
9141 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9142 decl, cdecl, NULL_TREE);
9143
9144 /* Shortcut to get the attributes of the component. */
9145 if (c->ts.type == BT_CLASS)
9146 {
9147 attr = &CLASS_DATA (c)->attr;
9148 if (attr->class_pointer)
9149 continue;
9150 }
9151 else
9152 {
9153 attr = &c->attr;
9154 if (attr->pointer)
9155 continue;
9156 }
9157
26e237fb
AV
9158 /* Do not broadcast a caf_token. These are local to the image. */
9159 if (attr->caf_token)
9160 continue;
9161
c78d3425
AF
9162 add_when_allocated = NULL_TREE;
9163 if (cmp_has_alloc_comps
9164 && !c->attr.pointer && !c->attr.proc_pointer)
9165 {
9166 if (c->ts.type == BT_CLASS)
9167 {
9168 rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
9169 add_when_allocated
9170 = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
9171 comp, NULL_TREE, rank, purpose,
9172 caf_mode, args);
9173 }
9174 else
9175 {
9176 rank = c->as ? c->as->rank : 0;
9177 add_when_allocated = structure_alloc_comps (c->ts.u.derived,
9178 comp, NULL_TREE,
9179 rank, purpose,
9180 caf_mode, args);
9181 }
9182 }
9183
9184 gfc_init_block (&derived_type_block);
9185 if (add_when_allocated)
9186 gfc_add_expr_to_block (&derived_type_block, add_when_allocated);
9187 tmp = gfc_finish_block (&derived_type_block);
9188 gfc_add_expr_to_block (&tmpblock, tmp);
9189
9190 /* Convert the component into a rank 1 descriptor type. */
9191 if (attr->dimension)
9192 {
9193 tmp = gfc_get_element_type (TREE_TYPE (comp));
26e237fb
AV
9194 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
9195 ubound = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (comp));
9196 else
9197 ubound = gfc_full_array_size (&tmpblock, comp,
9198 c->ts.type == BT_CLASS
9199 ? CLASS_DATA (c)->as->rank
9200 : c->as->rank);
c78d3425
AF
9201 }
9202 else
9203 {
9204 tmp = TREE_TYPE (comp);
9205 ubound = build_int_cst (gfc_array_index_type, 1);
9206 }
9207
26e237fb
AV
9208 /* Treat strings like arrays. Or the other way around, do not
9209 * generate an additional array layer for scalar components. */
9210 if (attr->dimension || c->ts.type == BT_CHARACTER)
9211 {
9212 cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
9213 &ubound, 1,
9214 GFC_ARRAY_ALLOCATABLE, false);
9215
9216 cdesc = gfc_create_var (cdesc, "cdesc");
9217 DECL_ARTIFICIAL (cdesc) = 1;
9218
9219 gfc_add_modify (&tmpblock, gfc_conv_descriptor_dtype (cdesc),
9220 gfc_get_dtype_rank_type (1, tmp));
9221 gfc_conv_descriptor_lbound_set (&tmpblock, cdesc,
9222 gfc_index_zero_node,
9223 gfc_index_one_node);
9224 gfc_conv_descriptor_stride_set (&tmpblock, cdesc,
9225 gfc_index_zero_node,
9226 gfc_index_one_node);
9227 gfc_conv_descriptor_ubound_set (&tmpblock, cdesc,
9228 gfc_index_zero_node, ubound);
9229 }
9230 else
9231 /* Prevent warning. */
9232 cdesc = NULL_TREE;
61c8d9e4 9233
c78d3425 9234 if (attr->dimension)
26e237fb
AV
9235 {
9236 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
9237 comp = gfc_conv_descriptor_data_get (comp);
9238 else
9239 comp = gfc_build_addr_expr (NULL_TREE, comp);
9240 }
c78d3425
AF
9241 else
9242 {
9243 gfc_se se;
9244
9245 gfc_init_se (&se, NULL);
9246
9247 comp = gfc_conv_scalar_to_descriptor (&se, comp,
26e237fb
AV
9248 c->ts.type == BT_CLASS
9249 ? CLASS_DATA (c)->attr
9250 : c->attr);
9251 if (c->ts.type == BT_CHARACTER)
9252 comp = gfc_build_addr_expr (NULL_TREE, comp);
c78d3425
AF
9253 gfc_add_block_to_block (&tmpblock, &se.pre);
9254 }
9255
26e237fb
AV
9256 if (attr->dimension || c->ts.type == BT_CHARACTER)
9257 gfc_conv_descriptor_data_set (&tmpblock, cdesc, comp);
9258 else
9259 cdesc = comp;
c78d3425
AF
9260
9261 tree fndecl;
9262
9263 fndecl = build_call_expr_loc (input_location,
9264 gfor_fndecl_co_broadcast, 5,
9265 gfc_build_addr_expr (pvoid_type_node,cdesc),
9266 args->image_index,
9267 null_pointer_node, null_pointer_node,
9268 null_pointer_node);
9269
9270 gfc_add_expr_to_block (&tmpblock, fndecl);
9271 gfc_add_block_to_block (&fnblock, &tmpblock);
9272
9273 break;
9274
5046aff5 9275 case DEALLOCATE_ALLOC_COMP:
d6430d9a 9276
d6430d9a 9277 gfc_init_block (&tmpblock);
dbb7247b 9278
39da5866
AV
9279 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9280 decl, cdecl, NULL_TREE);
9281
9282 /* Shortcut to get the attributes of the component. */
9283 if (c->ts.type == BT_CLASS)
4b9c80d8
AV
9284 {
9285 attr = &CLASS_DATA (c)->attr;
9286 if (attr->class_pointer)
9287 continue;
9288 }
39da5866 9289 else
4b9c80d8
AV
9290 {
9291 attr = &c->attr;
9292 if (attr->pointer)
9293 continue;
9294 }
39da5866 9295
895a0c2d 9296 if ((c->ts.type == BT_DERIVED && !c->attr.pointer)
39da5866
AV
9297 || (c->ts.type == BT_CLASS && !CLASS_DATA (c)->attr.class_pointer))
9298 /* Call the finalizer, which will free the memory and nullify the
9299 pointer of an array. */
9300 deallocate_called = gfc_add_comp_finalizer_call (&tmpblock, comp, c,
9301 caf_enabled (caf_mode))
9302 && attr->dimension;
9303 else
9304 deallocate_called = false;
9305
9306 /* Add the _class ref for classes. */
9307 if (c->ts.type == BT_CLASS && attr->allocatable)
9308 comp = gfc_class_data_get (comp);
895a0c2d 9309
39da5866
AV
9310 add_when_allocated = NULL_TREE;
9311 if (cmp_has_alloc_comps
9312 && !c->attr.pointer && !c->attr.proc_pointer
9313 && !same_type
9314 && !deallocate_called)
9315 {
9316 /* Add checked deallocation of the components. This code is
9317 obviously added because the finalizer is not trusted to free
9318 all memory. */
9319 if (c->ts.type == BT_CLASS)
9320 {
9321 rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
9322 add_when_allocated
9323 = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
9324 comp, NULL_TREE, rank, purpose,
c78d3425 9325 caf_mode, args);
39da5866
AV
9326 }
9327 else
9328 {
9329 rank = c->as ? c->as->rank : 0;
9330 add_when_allocated = structure_alloc_comps (c->ts.u.derived,
9331 comp, NULL_TREE,
9332 rank, purpose,
c78d3425 9333 caf_mode, args);
39da5866 9334 }
895a0c2d 9335 }
895a0c2d 9336
39da5866
AV
9337 if (attr->allocatable && !same_type
9338 && (!attr->codimension || caf_enabled (caf_mode)))
895a0c2d 9339 {
39da5866
AV
9340 /* Handle all types of components besides components of the
9341 same_type as the current one, because those would create an
9342 endless loop. */
9343 caf_dereg_mode
9344 = (caf_in_coarray (caf_mode) || attr->codimension)
ba85c8c3
AV
9345 ? (gfc_caf_is_dealloc_only (caf_mode)
9346 ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
9347 : GFC_CAF_COARRAY_DEREGISTER)
9348 : GFC_CAF_COARRAY_NOCOARRAY;
ba85c8c3 9349
39da5866
AV
9350 caf_token = NULL_TREE;
9351 /* Coarray components are handled directly by
9352 deallocate_with_status. */
9353 if (!attr->codimension
9354 && caf_dereg_mode != GFC_CAF_COARRAY_NOCOARRAY)
ba85c8c3 9355 {
39da5866
AV
9356 if (c->caf_token)
9357 caf_token = fold_build3_loc (input_location, COMPONENT_REF,
9358 TREE_TYPE (c->caf_token),
9359 decl, c->caf_token, NULL_TREE);
9360 else if (attr->dimension && !attr->proc_pointer)
9361 caf_token = gfc_conv_descriptor_token (comp);
ba85c8c3 9362 }
39da5866
AV
9363 if (attr->dimension && !attr->codimension && !attr->proc_pointer)
9364 /* When this is an array but not in conjunction with a coarray
9365 then add the data-ref. For coarray'ed arrays the data-ref
9366 is added by deallocate_with_status. */
9367 comp = gfc_conv_descriptor_data_get (comp);
ba85c8c3 9368
39da5866
AV
9369 tmp = gfc_deallocate_with_status (comp, NULL_TREE, NULL_TREE,
9370 NULL_TREE, NULL_TREE, true,
9371 NULL, caf_dereg_mode,
9372 add_when_allocated, caf_token);
1517fd57 9373
d6430d9a 9374 gfc_add_expr_to_block (&tmpblock, tmp);
1517fd57 9375 }
39da5866
AV
9376 else if (attr->allocatable && !attr->codimension
9377 && !deallocate_called)
bf9f15ee
PT
9378 {
9379 /* Case of recursive allocatable derived types. */
9380 tree is_allocated;
9381 tree ubound;
9382 tree cdesc;
bf9f15ee
PT
9383 stmtblock_t dealloc_block;
9384
9385 gfc_init_block (&dealloc_block);
39da5866
AV
9386 if (add_when_allocated)
9387 gfc_add_expr_to_block (&dealloc_block, add_when_allocated);
bf9f15ee
PT
9388
9389 /* Convert the component into a rank 1 descriptor type. */
39da5866 9390 if (attr->dimension)
bf9f15ee
PT
9391 {
9392 tmp = gfc_get_element_type (TREE_TYPE (comp));
39da5866
AV
9393 ubound = gfc_full_array_size (&dealloc_block, comp,
9394 c->ts.type == BT_CLASS
9395 ? CLASS_DATA (c)->as->rank
9396 : c->as->rank);
bf9f15ee
PT
9397 }
9398 else
9399 {
9400 tmp = TREE_TYPE (comp);
9401 ubound = build_int_cst (gfc_array_index_type, 1);
9402 }
9403
ba85c8c3
AV
9404 cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
9405 &ubound, 1,
bf9f15ee
PT
9406 GFC_ARRAY_ALLOCATABLE, false);
9407
9408 cdesc = gfc_create_var (cdesc, "cdesc");
9409 DECL_ARTIFICIAL (cdesc) = 1;
9410
9411 gfc_add_modify (&dealloc_block, gfc_conv_descriptor_dtype (cdesc),
9412 gfc_get_dtype_rank_type (1, tmp));
9413 gfc_conv_descriptor_lbound_set (&dealloc_block, cdesc,
ba85c8c3
AV
9414 gfc_index_zero_node,
9415 gfc_index_one_node);
bf9f15ee 9416 gfc_conv_descriptor_stride_set (&dealloc_block, cdesc,
ba85c8c3
AV
9417 gfc_index_zero_node,
9418 gfc_index_one_node);
bf9f15ee 9419 gfc_conv_descriptor_ubound_set (&dealloc_block, cdesc,
ba85c8c3 9420 gfc_index_zero_node, ubound);
bf9f15ee 9421
39da5866
AV
9422 if (attr->dimension)
9423 comp = gfc_conv_descriptor_data_get (comp);
bf9f15ee 9424
39da5866 9425 gfc_conv_descriptor_data_set (&dealloc_block, cdesc, comp);
bf9f15ee
PT
9426
9427 /* Now call the deallocator. */
9428 vtab = gfc_find_vtab (&c->ts);
9429 if (vtab->backend_decl == NULL)
9430 gfc_get_symbol_decl (vtab);
9431 tmp = gfc_build_addr_expr (NULL_TREE, vtab->backend_decl);
9432 dealloc_fndecl = gfc_vptr_deallocate_get (tmp);
9433 dealloc_fndecl = build_fold_indirect_ref_loc (input_location,
9434 dealloc_fndecl);
39da5866 9435 tmp = build_int_cst (TREE_TYPE (comp), 0);
bf9f15ee 9436 is_allocated = fold_build2_loc (input_location, NE_EXPR,
63ee5404 9437 logical_type_node, tmp,
39da5866 9438 comp);
bf9f15ee
PT
9439 cdesc = gfc_build_addr_expr (NULL_TREE, cdesc);
9440
9441 tmp = build_call_expr_loc (input_location,
9442 dealloc_fndecl, 1,
9443 cdesc);
9444 gfc_add_expr_to_block (&dealloc_block, tmp);
9445
9446 tmp = gfc_finish_block (&dealloc_block);
9447
9448 tmp = fold_build3_loc (input_location, COND_EXPR,
9449 void_type_node, is_allocated, tmp,
9450 build_empty_stmt (input_location));
9451
9452 gfc_add_expr_to_block (&tmpblock, tmp);
bf9f15ee 9453 }
39da5866
AV
9454 else if (add_when_allocated)
9455 gfc_add_expr_to_block (&tmpblock, add_when_allocated);
bf9f15ee 9456
39da5866
AV
9457 if (c->ts.type == BT_CLASS && attr->allocatable
9458 && (!attr->codimension || !caf_enabled (caf_mode)))
1517fd57 9459 {
6a4236ce
PT
9460 /* Finally, reset the vptr to the declared type vtable and, if
9461 necessary reset the _len field.
9462
9463 First recover the reference to the component and obtain
9464 the vptr. */
9465 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
39da5866 9466 decl, cdecl, NULL_TREE);
6a4236ce
PT
9467 tmp = gfc_class_vptr_get (comp);
9468
9469 if (UNLIMITED_POLY (c))
9470 {
9471 /* Both vptr and _len field should be nulled. */
9472 gfc_add_modify (&tmpblock, tmp,
9473 build_int_cst (TREE_TYPE (tmp), 0));
9474 tmp = gfc_class_len_get (comp);
9475 gfc_add_modify (&tmpblock, tmp,
9476 build_int_cst (TREE_TYPE (tmp), 0));
9477 }
9478 else
9479 {
9480 /* Build the vtable address and set the vptr with it. */
9481 tree vtab;
9482 gfc_symbol *vtable;
9483 vtable = gfc_find_derived_vtab (c->ts.u.derived);
9484 vtab = vtable->backend_decl;
9485 if (vtab == NULL_TREE)
9486 vtab = gfc_get_symbol_decl (vtable);
9487 vtab = gfc_build_addr_expr (NULL, vtab);
9488 vtab = fold_convert (TREE_TYPE (tmp), vtab);
9489 gfc_add_modify (&tmpblock, tmp, vtab);
9490 }
d6430d9a
PT
9491 }
9492
d6430d9a
PT
9493 /* Now add the deallocation of this component. */
9494 gfc_add_block_to_block (&fnblock, &tmpblock);
5046aff5
PT
9495 break;
9496
9497 case NULLIFY_ALLOC_COMP:
de91486c
AV
9498 /* Nullify
9499 - allocatable components (regular or in class)
9500 - components that have allocatable components
9501 - pointer components when in a coarray.
9502 Skip everything else especially proc_pointers, which may come
9503 coupled with the regular pointer attribute. */
9504 if (c->attr.proc_pointer
ba85c8c3
AV
9505 || !(c->attr.allocatable || (c->ts.type == BT_CLASS
9506 && CLASS_DATA (c)->attr.allocatable)
de91486c
AV
9507 || (cmp_has_alloc_comps
9508 && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
9509 || (c->ts.type == BT_CLASS
9510 && !CLASS_DATA (c)->attr.class_pointer)))
9511 || (caf_in_coarray (caf_mode) && c->attr.pointer)))
5046aff5 9512 continue;
ba85c8c3 9513
de91486c
AV
9514 /* Process class components first, because they always have the
9515 pointer-attribute set which would be caught wrong else. */
9516 if (c->ts.type == BT_CLASS
9517 && (CLASS_DATA (c)->attr.allocatable
9518 || CLASS_DATA (c)->attr.class_pointer))
1517fd57 9519 {
61c8d9e4
PT
9520 tree vptr_decl;
9521
de91486c 9522 /* Allocatable CLASS components. */
94471a56
TB
9523 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9524 decl, cdecl, NULL_TREE);
de91486c 9525
61c8d9e4
PT
9526 vptr_decl = gfc_class_vptr_get (comp);
9527
de91486c
AV
9528 comp = gfc_class_data_get (comp);
9529 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
9530 gfc_conv_descriptor_data_set (&fnblock, comp,
9531 null_pointer_node);
9532 else
2b3dc0db 9533 {
2b3dc0db 9534 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
de91486c 9535 void_type_node, comp,
2b3dc0db
PT
9536 build_int_cst (TREE_TYPE (comp), 0));
9537 gfc_add_expr_to_block (&fnblock, tmp);
9538 }
61c8d9e4
PT
9539
9540 /* The dynamic type of a disassociated pointer or unallocated
9541 allocatable variable is its declared type. An unlimited
9542 polymorphic entity has no declared type. */
9543 if (!UNLIMITED_POLY (c))
9544 {
9545 vtab = gfc_find_derived_vtab (c->ts.u.derived);
9546 if (!vtab->backend_decl)
9547 gfc_get_symbol_decl (vtab);
9548 tmp = gfc_build_addr_expr (NULL_TREE, vtab->backend_decl);
9549 }
9550 else
9551 tmp = build_int_cst (TREE_TYPE (vptr_decl), 0);
9552
9553 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
9554 void_type_node, vptr_decl, tmp);
9555 gfc_add_expr_to_block (&fnblock, tmp);
9556
ba85c8c3 9557 cmp_has_alloc_comps = false;
1517fd57 9558 }
de91486c
AV
9559 /* Coarrays need the component to be nulled before the api-call
9560 is made. */
9561 else if (c->attr.pointer || c->attr.allocatable)
1517fd57 9562 {
94471a56
TB
9563 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9564 decl, cdecl, NULL_TREE);
de91486c
AV
9565 if (c->attr.dimension || c->attr.codimension)
9566 gfc_conv_descriptor_data_set (&fnblock, comp,
9567 null_pointer_node);
c49ea23d 9568 else
de91486c
AV
9569 gfc_add_modify (&fnblock, comp,
9570 build_int_cst (TREE_TYPE (comp), 0));
9571 if (gfc_deferred_strlen (c, &comp))
c49ea23d 9572 {
de91486c
AV
9573 comp = fold_build3_loc (input_location, COMPONENT_REF,
9574 TREE_TYPE (comp),
9575 decl, comp, NULL_TREE);
c49ea23d 9576 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
de91486c 9577 TREE_TYPE (comp), comp,
c49ea23d
PT
9578 build_int_cst (TREE_TYPE (comp), 0));
9579 gfc_add_expr_to_block (&fnblock, tmp);
9580 }
ba85c8c3
AV
9581 cmp_has_alloc_comps = false;
9582 }
9583
61fad608 9584 if (flag_coarray == GFC_FCOARRAY_LIB && caf_in_coarray (caf_mode))
ba85c8c3 9585 {
61fad608
AV
9586 /* Register a component of a derived type coarray with the
9587 coarray library. Do not register ultimate component
9588 coarrays here. They are treated like regular coarrays and
9589 are either allocated on all images or on none. */
ba85c8c3
AV
9590 tree token;
9591
9592 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9593 decl, cdecl, NULL_TREE);
61fad608 9594 if (c->attr.dimension)
ba85c8c3 9595 {
de91486c
AV
9596 /* Set the dtype, because caf_register needs it. */
9597 gfc_add_modify (&fnblock, gfc_conv_descriptor_dtype (comp),
9598 gfc_get_dtype (TREE_TYPE (comp)));
ba85c8c3
AV
9599 tmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9600 decl, cdecl, NULL_TREE);
9601 token = gfc_conv_descriptor_token (tmp);
9602 }
9603 else
9604 {
9605 gfc_se se;
ba85c8c3
AV
9606
9607 gfc_init_se (&se, NULL);
ba85c8c3
AV
9608 token = fold_build3_loc (input_location, COMPONENT_REF,
9609 pvoid_type_node, decl, c->caf_token,
9610 NULL_TREE);
e0396d77
AV
9611 comp = gfc_conv_scalar_to_descriptor (&se, comp,
9612 c->ts.type == BT_CLASS
9613 ? CLASS_DATA (c)->attr
9614 : c->attr);
ba85c8c3
AV
9615 gfc_add_block_to_block (&fnblock, &se.pre);
9616 }
9617
ba85c8c3
AV
9618 gfc_allocate_using_caf_lib (&fnblock, comp, size_zero_node,
9619 gfc_build_addr_expr (NULL_TREE,
9620 token),
9621 NULL_TREE, NULL_TREE, NULL_TREE,
9622 GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
1517fd57 9623 }
ba85c8c3
AV
9624
9625 if (cmp_has_alloc_comps)
5046aff5 9626 {
94471a56
TB
9627 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9628 decl, cdecl, NULL_TREE);
5046aff5 9629 rank = c->as ? c->as->rank : 0;
bc21d315 9630 tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE,
c78d3425 9631 rank, purpose, caf_mode, args);
5046aff5
PT
9632 gfc_add_expr_to_block (&fnblock, tmp);
9633 }
9634 break;
9635
ba85c8c3
AV
9636 case REASSIGN_CAF_COMP:
9637 if (caf_enabled (caf_mode)
9638 && (c->attr.codimension
9639 || (c->ts.type == BT_CLASS
9640 && (CLASS_DATA (c)->attr.coarray_comp
9641 || caf_in_coarray (caf_mode)))
9642 || (c->ts.type == BT_DERIVED
9643 && (c->ts.u.derived->attr.coarray_comp
9644 || caf_in_coarray (caf_mode))))
9645 && !same_type)
558f3755 9646 {
ba85c8c3
AV
9647 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9648 decl, cdecl, NULL_TREE);
9649 dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9650 dest, cdecl, NULL_TREE);
9651
9652 if (c->attr.codimension)
558f3755 9653 {
ba85c8c3
AV
9654 if (c->ts.type == BT_CLASS)
9655 {
9656 comp = gfc_class_data_get (comp);
9657 dcmp = gfc_class_data_get (dcmp);
9658 }
9659 gfc_conv_descriptor_data_set (&fnblock, dcmp,
558f3755 9660 gfc_conv_descriptor_data_get (comp));
ba85c8c3
AV
9661 }
9662 else
9663 {
9664 tmp = structure_alloc_comps (c->ts.u.derived, comp, dcmp,
9665 rank, purpose, caf_mode
c78d3425
AF
9666 | GFC_STRUCTURE_CAF_MODE_IN_COARRAY,
9667 args);
ba85c8c3
AV
9668 gfc_add_expr_to_block (&fnblock, tmp);
9669 }
abc2d807
TB
9670 }
9671 break;
9672
5046aff5 9673 case COPY_ALLOC_COMP:
e057d3e5 9674 if (c->attr.pointer || c->attr.proc_pointer)
5046aff5
PT
9675 continue;
9676
9677 /* We need source and destination components. */
94471a56
TB
9678 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype, decl,
9679 cdecl, NULL_TREE);
9680 dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype, dest,
9681 cdecl, NULL_TREE);
5046aff5
PT
9682 dcmp = fold_convert (TREE_TYPE (comp), dcmp);
9683
4ed1b019
TB
9684 if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
9685 {
9686 tree ftn_tree;
9687 tree size;
9688 tree dst_data;
9689 tree src_data;
9690 tree null_data;
9691
9692 dst_data = gfc_class_data_get (dcmp);
9693 src_data = gfc_class_data_get (comp);
34d9d749
AV
9694 size = fold_convert (size_type_node,
9695 gfc_class_vtab_size_get (comp));
4ed1b019
TB
9696
9697 if (CLASS_DATA (c)->attr.dimension)
9698 {
9699 nelems = gfc_conv_descriptor_size (src_data,
9700 CLASS_DATA (c)->as->rank);
16023efc
TB
9701 size = fold_build2_loc (input_location, MULT_EXPR,
9702 size_type_node, size,
9703 fold_convert (size_type_node,
9704 nelems));
4ed1b019
TB
9705 }
9706 else
9707 nelems = build_int_cst (size_type_node, 1);
9708
abc2d807
TB
9709 if (CLASS_DATA (c)->attr.dimension
9710 || CLASS_DATA (c)->attr.codimension)
9711 {
9712 src_data = gfc_conv_descriptor_data_get (src_data);
9713 dst_data = gfc_conv_descriptor_data_get (dst_data);
9714 }
9715
4ed1b019
TB
9716 gfc_init_block (&tmpblock);
9717
26219cee
PT
9718 gfc_add_modify (&tmpblock, gfc_class_vptr_get (dcmp),
9719 gfc_class_vptr_get (comp));
9720
9721 /* Copy the unlimited '_len' field. If it is greater than zero
9722 (ie. a character(_len)), multiply it by size and use this
9723 for the malloc call. */
9724 if (UNLIMITED_POLY (c))
9725 {
26219cee
PT
9726 gfc_add_modify (&tmpblock, gfc_class_len_get (dcmp),
9727 gfc_class_len_get (comp));
ce8dcc91 9728 size = gfc_resize_class_size_with_len (&tmpblock, comp, size);
26219cee
PT
9729 }
9730
abc2d807
TB
9731 /* Coarray component have to have the same allocation status and
9732 shape/type-parameter/effective-type on the LHS and RHS of an
9733 intrinsic assignment. Hence, we did not deallocated them - and
9734 do not allocate them here. */
9735 if (!CLASS_DATA (c)->attr.codimension)
9736 {
9737 ftn_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
9738 tmp = build_call_expr_loc (input_location, ftn_tree, 1, size);
9739 gfc_add_modify (&tmpblock, dst_data,
9740 fold_convert (TREE_TYPE (dst_data), tmp));
9741 }
4ed1b019 9742
34d9d749
AV
9743 tmp = gfc_copy_class_to_class (comp, dcmp, nelems,
9744 UNLIMITED_POLY (c));
4ed1b019
TB
9745 gfc_add_expr_to_block (&tmpblock, tmp);
9746 tmp = gfc_finish_block (&tmpblock);
9747
9748 gfc_init_block (&tmpblock);
9749 gfc_add_modify (&tmpblock, dst_data,
9750 fold_convert (TREE_TYPE (dst_data),
9751 null_pointer_node));
9752 null_data = gfc_finish_block (&tmpblock);
9753
9754 null_cond = fold_build2_loc (input_location, NE_EXPR,
63ee5404 9755 logical_type_node, src_data,
f04986a9 9756 null_pointer_node);
4ed1b019
TB
9757
9758 gfc_add_expr_to_block (&fnblock, build3_v (COND_EXPR, null_cond,
9759 tmp, null_data));
9760 continue;
9761 }
9762
fc7d0afb
AV
9763 /* To implement guarded deep copy, i.e., deep copy only allocatable
9764 components that are really allocated, the deep copy code has to
9765 be generated first and then added to the if-block in
9766 gfc_duplicate_allocatable (). */
0b627b58 9767 if (cmp_has_alloc_comps && !c->attr.proc_pointer && !same_type)
fc7d0afb
AV
9768 {
9769 rank = c->as ? c->as->rank : 0;
9770 tmp = fold_convert (TREE_TYPE (dcmp), comp);
9771 gfc_add_modify (&fnblock, dcmp, tmp);
9772 add_when_allocated = structure_alloc_comps (c->ts.u.derived,
9773 comp, dcmp,
ba85c8c3 9774 rank, purpose,
c78d3425 9775 caf_mode, args);
fc7d0afb
AV
9776 }
9777 else
9778 add_when_allocated = NULL_TREE;
9779
2b3dc0db
PT
9780 if (gfc_deferred_strlen (c, &tmp))
9781 {
9782 tree len, size;
9783 len = tmp;
9784 tmp = fold_build3_loc (input_location, COMPONENT_REF,
9785 TREE_TYPE (len),
9786 decl, len, NULL_TREE);
9787 len = fold_build3_loc (input_location, COMPONENT_REF,
9788 TREE_TYPE (len),
9789 dest, len, NULL_TREE);
9790 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
9791 TREE_TYPE (len), len, tmp);
9792 gfc_add_expr_to_block (&fnblock, tmp);
9793 size = size_of_string_in_bytes (c->ts.kind, len);
67914693 9794 /* This component cannot have allocatable components,
fc7d0afb
AV
9795 therefore add_when_allocated of duplicate_allocatable ()
9796 is always NULL. */
2b3dc0db 9797 tmp = duplicate_allocatable (dcmp, comp, ctype, rank,
fc7d0afb 9798 false, false, size, NULL_TREE);
2b3dc0db
PT
9799 gfc_add_expr_to_block (&fnblock, tmp);
9800 }
0b627b58
PT
9801 else if (c->attr.pdt_array)
9802 {
9803 tmp = duplicate_allocatable (dcmp, comp, ctype,
9804 c->as ? c->as->rank : 0,
9805 false, false, NULL_TREE, NULL_TREE);
9806 gfc_add_expr_to_block (&fnblock, tmp);
9807 }
9808 else if ((c->attr.allocatable)
9809 && !c->attr.proc_pointer && !same_type
9810 && (!(cmp_has_alloc_comps && c->as) || c->attr.codimension
9811 || caf_in_coarray (caf_mode)))
5046aff5 9812 {
40c32948 9813 rank = c->as ? c->as->rank : 0;
abc2d807
TB
9814 if (c->attr.codimension)
9815 tmp = gfc_copy_allocatable_data (dcmp, comp, ctype, rank);
ba85c8c3
AV
9816 else if (flag_coarray == GFC_FCOARRAY_LIB
9817 && caf_in_coarray (caf_mode))
9818 {
e00464a5
AV
9819 tree dst_tok;
9820 if (c->as)
9821 dst_tok = gfc_conv_descriptor_token (dcmp);
9822 else
9823 {
9824 /* For a scalar allocatable component the caf_token is
9825 the next component. */
9826 if (!c->caf_token)
9827 c->caf_token = c->next->backend_decl;
9828 dst_tok = fold_build3_loc (input_location,
9829 COMPONENT_REF,
9830 pvoid_type_node, dest,
9831 c->caf_token,
9832 NULL_TREE);
9833 }
ba85c8c3
AV
9834 tmp = duplicate_allocatable_coarray (dcmp, dst_tok, comp,
9835 ctype, rank);
9836 }
abc2d807 9837 else
fc7d0afb
AV
9838 tmp = gfc_duplicate_allocatable (dcmp, comp, ctype, rank,
9839 add_when_allocated);
5046aff5
PT
9840 gfc_add_expr_to_block (&fnblock, tmp);
9841 }
fc7d0afb 9842 else
0b627b58 9843 if (cmp_has_alloc_comps || is_pdt_type)
fc7d0afb 9844 gfc_add_expr_to_block (&fnblock, add_when_allocated);
5046aff5 9845
5046aff5
PT
9846 break;
9847
5bab4c96
PT
9848 case ALLOCATE_PDT_COMP:
9849
9850 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9851 decl, cdecl, NULL_TREE);
9852
9853 /* Set the PDT KIND and LEN fields. */
9854 if (c->attr.pdt_kind || c->attr.pdt_len)
9855 {
9856 gfc_se tse;
9857 gfc_expr *c_expr = NULL;
9858 gfc_actual_arglist *param = pdt_param_list;
9859 gfc_init_se (&tse, NULL);
9860 for (; param; param = param->next)
276515e6 9861 if (param->name && !strcmp (c->name, param->name))
5bab4c96
PT
9862 c_expr = param->expr;
9863
9864 if (!c_expr)
9865 c_expr = c->initializer;
9866
9867 if (c_expr)
9868 {
9869 gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
9870 gfc_add_modify (&fnblock, comp, tse.expr);
9871 }
9872 }
9873
9874 if (c->attr.pdt_string)
9875 {
9876 gfc_se tse;
9877 gfc_init_se (&tse, NULL);
276515e6
PT
9878 tree strlen = NULL_TREE;
9879 gfc_expr *e = gfc_copy_expr (c->ts.u.cl->length);
5bab4c96
PT
9880 /* Convert the parameterized string length to its value. The
9881 string length is stored in a hidden field in the same way as
9882 deferred string lengths. */
276515e6 9883 gfc_insert_parameter_exprs (e, pdt_param_list);
5bab4c96
PT
9884 if (gfc_deferred_strlen (c, &strlen) && strlen != NULL_TREE)
9885 {
276515e6 9886 gfc_conv_expr_type (&tse, e,
5bab4c96
PT
9887 TREE_TYPE (strlen));
9888 strlen = fold_build3_loc (input_location, COMPONENT_REF,
9889 TREE_TYPE (strlen),
9890 decl, strlen, NULL_TREE);
9891 gfc_add_modify (&fnblock, strlen, tse.expr);
9892 c->ts.u.cl->backend_decl = strlen;
9893 }
276515e6
PT
9894 gfc_free_expr (e);
9895
0b627b58 9896 /* Scalar parameterized strings can be allocated now. */
5bab4c96
PT
9897 if (!c->as)
9898 {
9899 tmp = fold_convert (gfc_array_index_type, strlen);
9900 tmp = size_of_string_in_bytes (c->ts.kind, tmp);
9901 tmp = gfc_evaluate_now (tmp, &fnblock);
9902 tmp = gfc_call_malloc (&fnblock, TREE_TYPE (comp), tmp);
9903 gfc_add_modify (&fnblock, comp, tmp);
9904 }
9905 }
9906
0b627b58 9907 /* Allocate parameterized arrays of parameterized derived types. */
5bab4c96
PT
9908 if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
9909 && !((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9910 && (c->ts.u.derived && c->ts.u.derived->attr.pdt_type)))
9911 continue;
9912
9913 if (c->ts.type == BT_CLASS)
9914 comp = gfc_class_data_get (comp);
9915
9916 if (c->attr.pdt_array)
9917 {
9918 gfc_se tse;
9919 int i;
9920 tree size = gfc_index_one_node;
9921 tree offset = gfc_index_zero_node;
9922 tree lower, upper;
9923 gfc_expr *e;
9924
9925 /* This chunk takes the expressions for 'lower' and 'upper'
9926 in the arrayspec and substitutes in the expressions for
9927 the parameters from 'pdt_param_list'. The descriptor
9928 fields can then be filled from the values so obtained. */
9929 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)));
9930 for (i = 0; i < c->as->rank; i++)
9931 {
9932 gfc_init_se (&tse, NULL);
9933 e = gfc_copy_expr (c->as->lower[i]);
9934 gfc_insert_parameter_exprs (e, pdt_param_list);
9935 gfc_conv_expr_type (&tse, e, gfc_array_index_type);
9936 gfc_free_expr (e);
9937 lower = tse.expr;
9938 gfc_conv_descriptor_lbound_set (&fnblock, comp,
9939 gfc_rank_cst[i],
9940 lower);
9941 e = gfc_copy_expr (c->as->upper[i]);
9942 gfc_insert_parameter_exprs (e, pdt_param_list);
9943 gfc_conv_expr_type (&tse, e, gfc_array_index_type);
9944 gfc_free_expr (e);
9945 upper = tse.expr;
9946 gfc_conv_descriptor_ubound_set (&fnblock, comp,
9947 gfc_rank_cst[i],
9948 upper);
9949 gfc_conv_descriptor_stride_set (&fnblock, comp,
9950 gfc_rank_cst[i],
9951 size);
9952 size = gfc_evaluate_now (size, &fnblock);
9953 offset = fold_build2_loc (input_location,
9954 MINUS_EXPR,
9955 gfc_array_index_type,
9956 offset, size);
9957 offset = gfc_evaluate_now (offset, &fnblock);
9958 tmp = fold_build2_loc (input_location, MINUS_EXPR,
9959 gfc_array_index_type,
9960 upper, lower);
9961 tmp = fold_build2_loc (input_location, PLUS_EXPR,
9962 gfc_array_index_type,
9963 tmp, gfc_index_one_node);
9964 size = fold_build2_loc (input_location, MULT_EXPR,
9965 gfc_array_index_type, size, tmp);
9966 }
9967 gfc_conv_descriptor_offset_set (&fnblock, comp, offset);
9968 if (c->ts.type == BT_CLASS)
9969 {
9970 tmp = gfc_get_vptr_from_expr (comp);
9971 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
9972 tmp = build_fold_indirect_ref_loc (input_location, tmp);
9973 tmp = gfc_vptr_size_get (tmp);
9974 }
9975 else
9976 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (ctype));
9977 tmp = fold_convert (gfc_array_index_type, tmp);
9978 size = fold_build2_loc (input_location, MULT_EXPR,
9979 gfc_array_index_type, size, tmp);
9980 size = gfc_evaluate_now (size, &fnblock);
9981 tmp = gfc_call_malloc (&fnblock, NULL, size);
9982 gfc_conv_descriptor_data_set (&fnblock, comp, tmp);
9983 tmp = gfc_conv_descriptor_dtype (comp);
9984 gfc_add_modify (&fnblock, tmp, gfc_get_dtype (ctype));
0b627b58
PT
9985
9986 if (c->initializer && c->initializer->rank)
9987 {
9988 gfc_init_se (&tse, NULL);
9989 e = gfc_copy_expr (c->initializer);
9990 gfc_insert_parameter_exprs (e, pdt_param_list);
9991 gfc_conv_expr_descriptor (&tse, e);
9992 gfc_add_block_to_block (&fnblock, &tse.pre);
9993 gfc_free_expr (e);
9994 tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9995 tmp = build_call_expr_loc (input_location, tmp, 3,
9996 gfc_conv_descriptor_data_get (comp),
9997 gfc_conv_descriptor_data_get (tse.expr),
9998 fold_convert (size_type_node, size));
9999 gfc_add_expr_to_block (&fnblock, tmp);
10000 gfc_add_block_to_block (&fnblock, &tse.post);
10001 }
5bab4c96
PT
10002 }
10003
10004 /* Recurse in to PDT components. */
10005 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2fcd5884
PT
10006 && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
10007 && !(c->attr.pointer || c->attr.allocatable))
5bab4c96
PT
10008 {
10009 bool is_deferred = false;
10010 gfc_actual_arglist *tail = c->param_list;
10011
10012 for (; tail; tail = tail->next)
10013 if (!tail->expr)
10014 is_deferred = true;
10015
10016 tail = is_deferred ? pdt_param_list : c->param_list;
10017 tmp = gfc_allocate_pdt_comp (c->ts.u.derived, comp,
10018 c->as ? c->as->rank : 0,
10019 tail);
10020 gfc_add_expr_to_block (&fnblock, tmp);
10021 }
10022
10023 break;
10024
10025 case DEALLOCATE_PDT_COMP:
10026 /* Deallocate array or parameterized string length components
10027 of parameterized derived types. */
10028 if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
10029 && !c->attr.pdt_string
10030 && !((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10031 && (c->ts.u.derived && c->ts.u.derived->attr.pdt_type)))
10032 continue;
10033
10034 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10035 decl, cdecl, NULL_TREE);
10036 if (c->ts.type == BT_CLASS)
10037 comp = gfc_class_data_get (comp);
10038
10039 /* Recurse in to PDT components. */
10040 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2fcd5884
PT
10041 && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
10042 && (!c->attr.pointer && !c->attr.allocatable))
5bab4c96
PT
10043 {
10044 tmp = gfc_deallocate_pdt_comp (c->ts.u.derived, comp,
10045 c->as ? c->as->rank : 0);
10046 gfc_add_expr_to_block (&fnblock, tmp);
10047 }
10048
10049 if (c->attr.pdt_array)
10050 {
10051 tmp = gfc_conv_descriptor_data_get (comp);
2fcd5884 10052 null_cond = fold_build2_loc (input_location, NE_EXPR,
63ee5404 10053 logical_type_node, tmp,
2fcd5884 10054 build_int_cst (TREE_TYPE (tmp), 0));
5bab4c96 10055 tmp = gfc_call_free (tmp);
2fcd5884
PT
10056 tmp = build3_v (COND_EXPR, null_cond, tmp,
10057 build_empty_stmt (input_location));
5bab4c96
PT
10058 gfc_add_expr_to_block (&fnblock, tmp);
10059 gfc_conv_descriptor_data_set (&fnblock, comp, null_pointer_node);
10060 }
10061 else if (c->attr.pdt_string)
10062 {
2fcd5884 10063 null_cond = fold_build2_loc (input_location, NE_EXPR,
63ee5404 10064 logical_type_node, comp,
2fcd5884 10065 build_int_cst (TREE_TYPE (comp), 0));
5bab4c96 10066 tmp = gfc_call_free (comp);
2fcd5884
PT
10067 tmp = build3_v (COND_EXPR, null_cond, tmp,
10068 build_empty_stmt (input_location));
5bab4c96
PT
10069 gfc_add_expr_to_block (&fnblock, tmp);
10070 tmp = fold_convert (TREE_TYPE (comp), null_pointer_node);
10071 gfc_add_modify (&fnblock, comp, tmp);
10072 }
10073
10074 break;
10075
10076 case CHECK_PDT_DUMMY:
10077
10078 comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10079 decl, cdecl, NULL_TREE);
10080 if (c->ts.type == BT_CLASS)
10081 comp = gfc_class_data_get (comp);
10082
10083 /* Recurse in to PDT components. */
10084 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10085 && c->ts.u.derived && c->ts.u.derived->attr.pdt_type)
10086 {
10087 tmp = gfc_check_pdt_dummy (c->ts.u.derived, comp,
10088 c->as ? c->as->rank : 0,
10089 pdt_param_list);
10090 gfc_add_expr_to_block (&fnblock, tmp);
10091 }
10092
10093 if (!c->attr.pdt_len)
10094 continue;
10095 else
10096 {
10097 gfc_se tse;
10098 gfc_expr *c_expr = NULL;
10099 gfc_actual_arglist *param = pdt_param_list;
10100
10101 gfc_init_se (&tse, NULL);
10102 for (; param; param = param->next)
0b627b58
PT
10103 if (!strcmp (c->name, param->name)
10104 && param->spec_type == SPEC_EXPLICIT)
5bab4c96
PT
10105 c_expr = param->expr;
10106
10107 if (c_expr)
10108 {
10109 tree error, cond, cname;
10110 gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
10111 cond = fold_build2_loc (input_location, NE_EXPR,
63ee5404 10112 logical_type_node,
5bab4c96
PT
10113 comp, tse.expr);
10114 cname = gfc_build_cstring_const (c->name);
10115 cname = gfc_build_addr_expr (pchar_type_node, cname);
10116 error = gfc_trans_runtime_error (true, NULL,
10117 "The value of the PDT LEN "
10118 "parameter '%s' does not "
10119 "agree with that in the "
10120 "dummy declaration",
10121 cname);
10122 tmp = fold_build3_loc (input_location, COND_EXPR,
10123 void_type_node, cond, error,
10124 build_empty_stmt (input_location));
10125 gfc_add_expr_to_block (&fnblock, tmp);
10126 }
10127 }
10128 break;
10129
5046aff5
PT
10130 default:
10131 gcc_unreachable ();
10132 break;
10133 }
10134 }
10135
10136 return gfc_finish_block (&fnblock);
10137}
10138
10139/* Recursively traverse an object of derived type, generating code to
10140 nullify allocatable components. */
10141
10142tree
de91486c
AV
10143gfc_nullify_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
10144 int caf_mode)
5046aff5
PT
10145{
10146 return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
ba85c8c3 10147 NULLIFY_ALLOC_COMP,
c78d3425 10148 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode, NULL);
42a0e16c
PT
10149}
10150
10151
5046aff5
PT
10152/* Recursively traverse an object of derived type, generating code to
10153 deallocate allocatable components. */
10154
10155tree
ba85c8c3
AV
10156gfc_deallocate_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
10157 int caf_mode)
5046aff5
PT
10158{
10159 return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
ba85c8c3 10160 DEALLOCATE_ALLOC_COMP,
c78d3425 10161 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode, NULL);
5046aff5
PT
10162}
10163
c78d3425
AF
10164tree
10165gfc_bcast_alloc_comp (gfc_symbol *derived, gfc_expr *expr, int rank,
10166 tree image_index, tree stat, tree errmsg,
10167 tree errmsg_len)
10168{
10169 tree tmp, array;
10170 gfc_se argse;
10171 stmtblock_t block, post_block;
10172 gfc_co_subroutines_args args;
10173
10174 args.image_index = image_index;
10175 args.stat = stat;
10176 args.errmsg = errmsg;
05814dde 10177 args.errmsg_len = errmsg_len;
c78d3425
AF
10178
10179 if (rank == 0)
10180 {
10181 gfc_start_block (&block);
10182 gfc_init_block (&post_block);
10183 gfc_init_se (&argse, NULL);
10184 gfc_conv_expr (&argse, expr);
10185 gfc_add_block_to_block (&block, &argse.pre);
10186 gfc_add_block_to_block (&post_block, &argse.post);
10187 array = argse.expr;
10188 }
10189 else
10190 {
10191 gfc_init_se (&argse, NULL);
10192 argse.want_pointer = 1;
10193 gfc_conv_expr_descriptor (&argse, expr);
10194 array = argse.expr;
10195 }
10196
10197 tmp = structure_alloc_comps (derived, array, NULL_TREE, rank,
10198 BCAST_ALLOC_COMP,
10199 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY, &args);
10200 return tmp;
10201}
5046aff5 10202
abc2d807
TB
10203/* Recursively traverse an object of derived type, generating code to
10204 deallocate allocatable components. But do not deallocate coarrays.
10205 To be used for intrinsic assignment, which may not change the allocation
10206 status of coarrays. */
10207
10208tree
10209gfc_deallocate_alloc_comp_no_caf (gfc_symbol * der_type, tree decl, int rank)
10210{
10211 return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
c78d3425 10212 DEALLOCATE_ALLOC_COMP, 0, NULL);
abc2d807
TB
10213}
10214
10215
10216tree
10217gfc_reassign_alloc_comp_caf (gfc_symbol *der_type, tree decl, tree dest)
10218{
ba85c8c3 10219 return structure_alloc_comps (der_type, decl, dest, 0, REASSIGN_CAF_COMP,
c78d3425 10220 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY, NULL);
abc2d807
TB
10221}
10222
10223
5046aff5 10224/* Recursively traverse an object of derived type, generating code to
40c32948 10225 copy it and its allocatable components. */
5046aff5
PT
10226
10227tree
ba85c8c3
AV
10228gfc_copy_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank,
10229 int caf_mode)
5046aff5 10230{
ba85c8c3 10231 return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
c78d3425 10232 caf_mode, NULL);
5046aff5
PT
10233}
10234
10235
40c32948
PT
10236/* Recursively traverse an object of derived type, generating code to
10237 copy only its allocatable components. */
10238
10239tree
10240gfc_copy_only_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
10241{
ba85c8c3 10242 return structure_alloc_comps (der_type, decl, dest, rank,
c78d3425 10243 COPY_ONLY_ALLOC_COMP, 0, NULL);
40c32948
PT
10244}
10245
10246
71837f64 10247/* Recursively traverse an object of parameterized derived type, generating
5bab4c96
PT
10248 code to allocate parameterized components. */
10249
10250tree
10251gfc_allocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank,
10252 gfc_actual_arglist *param_list)
10253{
10254 tree res;
10255 gfc_actual_arglist *old_param_list = pdt_param_list;
10256 pdt_param_list = param_list;
10257 res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
c78d3425 10258 ALLOCATE_PDT_COMP, 0, NULL);
5bab4c96
PT
10259 pdt_param_list = old_param_list;
10260 return res;
10261}
10262
71837f64 10263/* Recursively traverse an object of parameterized derived type, generating
5bab4c96
PT
10264 code to deallocate parameterized components. */
10265
10266tree
10267gfc_deallocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank)
10268{
10269 return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
c78d3425 10270 DEALLOCATE_PDT_COMP, 0, NULL);
5bab4c96
PT
10271}
10272
10273
71837f64 10274/* Recursively traverse a dummy of parameterized derived type to check the
5bab4c96
PT
10275 values of LEN parameters. */
10276
10277tree
10278gfc_check_pdt_dummy (gfc_symbol * der_type, tree decl, int rank,
10279 gfc_actual_arglist *param_list)
10280{
10281 tree res;
10282 gfc_actual_arglist *old_param_list = pdt_param_list;
10283 pdt_param_list = param_list;
10284 res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
c78d3425 10285 CHECK_PDT_DUMMY, 0, NULL);
5bab4c96
PT
10286 pdt_param_list = old_param_list;
10287 return res;
10288}
10289
10290
597553ab
PT
10291/* Returns the value of LBOUND for an expression. This could be broken out
10292 from gfc_conv_intrinsic_bound but this seemed to be simpler. This is
10293 called by gfc_alloc_allocatable_for_assignment. */
10294static tree
10295get_std_lbound (gfc_expr *expr, tree desc, int dim, bool assumed_size)
10296{
10297 tree lbound;
10298 tree ubound;
10299 tree stride;
10300 tree cond, cond1, cond3, cond4;
10301 tree tmp;
99ee0251
PT
10302 gfc_ref *ref;
10303
597553ab
PT
10304 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
10305 {
10306 tmp = gfc_rank_cst[dim];
10307 lbound = gfc_conv_descriptor_lbound_get (desc, tmp);
10308 ubound = gfc_conv_descriptor_ubound_get (desc, tmp);
10309 stride = gfc_conv_descriptor_stride_get (desc, tmp);
63ee5404 10310 cond1 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
597553ab 10311 ubound, lbound);
63ee5404 10312 cond3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
597553ab
PT
10313 stride, gfc_index_zero_node);
10314 cond3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
63ee5404
JB
10315 logical_type_node, cond3, cond1);
10316 cond4 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
597553ab
PT
10317 stride, gfc_index_zero_node);
10318 if (assumed_size)
63ee5404 10319 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
597553ab
PT
10320 tmp, build_int_cst (gfc_array_index_type,
10321 expr->rank - 1));
10322 else
63ee5404 10323 cond = logical_false_node;
597553ab
PT
10324
10325 cond1 = fold_build2_loc (input_location, TRUTH_OR_EXPR,
63ee5404 10326 logical_type_node, cond3, cond4);
597553ab 10327 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
63ee5404 10328 logical_type_node, cond, cond1);
597553ab
PT
10329
10330 return fold_build3_loc (input_location, COND_EXPR,
10331 gfc_array_index_type, cond,
10332 lbound, gfc_index_one_node);
10333 }
e48cc391
TB
10334
10335 if (expr->expr_type == EXPR_FUNCTION)
10336 {
10337 /* A conversion function, so use the argument. */
10338 gcc_assert (expr->value.function.isym
10339 && expr->value.function.isym->conversion);
10340 expr = expr->value.function.actual->expr;
10341 }
10342
10343 if (expr->expr_type == EXPR_VARIABLE)
597553ab
PT
10344 {
10345 tmp = TREE_TYPE (expr->symtree->n.sym->backend_decl);
99ee0251
PT
10346 for (ref = expr->ref; ref; ref = ref->next)
10347 {
10348 if (ref->type == REF_COMPONENT
10349 && ref->u.c.component->as
10350 && ref->next
10351 && ref->next->u.ar.type == AR_FULL)
10352 tmp = TREE_TYPE (ref->u.c.component->backend_decl);
10353 }
597553ab
PT
10354 return GFC_TYPE_ARRAY_LBOUND(tmp, dim);
10355 }
597553ab
PT
10356
10357 return gfc_index_one_node;
10358}
10359
10360
10361/* Returns true if an expression represents an lhs that can be reallocated
10362 on assignment. */
10363
10364bool
10365gfc_is_reallocatable_lhs (gfc_expr *expr)
10366{
10367 gfc_ref * ref;
a8399af8 10368 gfc_symbol *sym;
597553ab
PT
10369
10370 if (!expr->ref)
10371 return false;
10372
a8399af8
PT
10373 sym = expr->symtree->n.sym;
10374
a086078b 10375 if (sym->attr.associate_var && !expr->ref)
ca32d61b
PT
10376 return false;
10377
574284e9 10378 /* An allocatable class variable with no reference. */
a8399af8 10379 if (sym->ts.type == BT_CLASS
12b537b9 10380 && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
a8399af8 10381 && CLASS_DATA (sym)->attr.allocatable
d0477233
TB
10382 && expr->ref
10383 && ((expr->ref->type == REF_ARRAY && expr->ref->u.ar.type == AR_FULL
10384 && expr->ref->next == NULL)
10385 || (expr->ref->type == REF_COMPONENT
10386 && strcmp (expr->ref->u.c.component->name, "_data") == 0
10387 && (expr->ref->next == NULL
10388 || (expr->ref->next->type == REF_ARRAY
10389 && expr->ref->next->u.ar.type == AR_FULL
10390 && expr->ref->next->next == NULL)))))
574284e9
AV
10391 return true;
10392
597553ab 10393 /* An allocatable variable. */
a8399af8 10394 if (sym->attr.allocatable
12b537b9 10395 && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
a086078b
PT
10396 && expr->ref
10397 && expr->ref->type == REF_ARRAY
10398 && expr->ref->u.ar.type == AR_FULL)
597553ab
PT
10399 return true;
10400
10401 /* All that can be left are allocatable components. */
a8399af8
PT
10402 if ((sym->ts.type != BT_DERIVED
10403 && sym->ts.type != BT_CLASS)
10404 || !sym->ts.u.derived->attr.alloc_comp)
597553ab
PT
10405 return false;
10406
10407 /* Find a component ref followed by an array reference. */
10408 for (ref = expr->ref; ref; ref = ref->next)
10409 if (ref->next
10410 && ref->type == REF_COMPONENT
10411 && ref->next->type == REF_ARRAY
10412 && !ref->next->next)
10413 break;
10414
10415 if (!ref)
10416 return false;
10417
10418 /* Return true if valid reallocatable lhs. */
10419 if (ref->u.c.component->attr.allocatable
10420 && ref->next->u.ar.type == AR_FULL)
10421 return true;
10422
10423 return false;
10424}
10425
10426
78ab5260
PT
10427static tree
10428concat_str_length (gfc_expr* expr)
10429{
10430 tree type;
10431 tree len1;
10432 tree len2;
10433 gfc_se se;
10434
10435 type = gfc_typenode_for_spec (&expr->value.op.op1->ts);
10436 len1 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
10437 if (len1 == NULL_TREE)
10438 {
10439 if (expr->value.op.op1->expr_type == EXPR_OP)
10440 len1 = concat_str_length (expr->value.op.op1);
10441 else if (expr->value.op.op1->expr_type == EXPR_CONSTANT)
10442 len1 = build_int_cst (gfc_charlen_type_node,
10443 expr->value.op.op1->value.character.length);
10444 else if (expr->value.op.op1->ts.u.cl->length)
10445 {
10446 gfc_init_se (&se, NULL);
10447 gfc_conv_expr (&se, expr->value.op.op1->ts.u.cl->length);
10448 len1 = se.expr;
10449 }
10450 else
10451 {
10452 /* Last resort! */
10453 gfc_init_se (&se, NULL);
10454 se.want_pointer = 1;
10455 se.descriptor_only = 1;
10456 gfc_conv_expr (&se, expr->value.op.op1);
10457 len1 = se.string_length;
10458 }
10459 }
10460
10461 type = gfc_typenode_for_spec (&expr->value.op.op2->ts);
10462 len2 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
10463 if (len2 == NULL_TREE)
10464 {
10465 if (expr->value.op.op2->expr_type == EXPR_OP)
10466 len2 = concat_str_length (expr->value.op.op2);
10467 else if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
10468 len2 = build_int_cst (gfc_charlen_type_node,
10469 expr->value.op.op2->value.character.length);
10470 else if (expr->value.op.op2->ts.u.cl->length)
10471 {
10472 gfc_init_se (&se, NULL);
10473 gfc_conv_expr (&se, expr->value.op.op2->ts.u.cl->length);
10474 len2 = se.expr;
10475 }
10476 else
10477 {
10478 /* Last resort! */
10479 gfc_init_se (&se, NULL);
10480 se.want_pointer = 1;
10481 se.descriptor_only = 1;
10482 gfc_conv_expr (&se, expr->value.op.op2);
10483 len2 = se.string_length;
10484 }
10485 }
10486
10487 gcc_assert(len1 && len2);
10488 len1 = fold_convert (gfc_charlen_type_node, len1);
10489 len2 = fold_convert (gfc_charlen_type_node, len2);
10490
10491 return fold_build2_loc (input_location, PLUS_EXPR,
10492 gfc_charlen_type_node, len1, len2);
10493}
10494
10495
597553ab
PT
10496/* Allocate the lhs of an assignment to an allocatable array, otherwise
10497 reallocate it. */
10498
10499tree
10500gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop,
10501 gfc_expr *expr1,
10502 gfc_expr *expr2)
10503{
10504 stmtblock_t realloc_block;
10505 stmtblock_t alloc_block;
10506 stmtblock_t fblock;
10507 gfc_ss *rss;
10508 gfc_ss *lss;
1838afec 10509 gfc_array_info *linfo;
597553ab
PT
10510 tree realloc_expr;
10511 tree alloc_expr;
10512 tree size1;
10513 tree size2;
ce8dcc91
PT
10514 tree elemsize1;
10515 tree elemsize2;
597553ab 10516 tree array1;
d700518b 10517 tree cond_null;
597553ab
PT
10518 tree cond;
10519 tree tmp;
10520 tree tmp2;
10521 tree lbound;
10522 tree ubound;
10523 tree desc;
16e24756 10524 tree old_desc;
597553ab
PT
10525 tree desc2;
10526 tree offset;
10527 tree jump_label1;
10528 tree jump_label2;
597553ab 10529 tree lbd;
0175d45d 10530 tree class_expr2 = NULL_TREE;
597553ab
PT
10531 int n;
10532 int dim;
10533 gfc_array_spec * as;
3c9f5092
AV
10534 bool coarray = (flag_coarray == GFC_FCOARRAY_LIB
10535 && gfc_caf_attr (expr1, true).codimension);
10536 tree token;
10537 gfc_se caf_se;
597553ab
PT
10538
10539 /* x = f(...) with x allocatable. In this case, expr1 is the rhs.
10540 Find the lhs expression in the loop chain and set expr1 and
10541 expr2 accordingly. */
10542 if (expr1->expr_type == EXPR_FUNCTION && expr2 == NULL)
10543 {
10544 expr2 = expr1;
10545 /* Find the ss for the lhs. */
10546 lss = loop->ss;
10547 for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
f98cfd3c 10548 if (lss->info->expr && lss->info->expr->expr_type == EXPR_VARIABLE)
597553ab
PT
10549 break;
10550 if (lss == gfc_ss_terminator)
10551 return NULL_TREE;
f98cfd3c 10552 expr1 = lss->info->expr;
597553ab
PT
10553 }
10554
10555 /* Bail out if this is not a valid allocate on assignment. */
10556 if (!gfc_is_reallocatable_lhs (expr1)
10557 || (expr2 && !expr2->rank))
10558 return NULL_TREE;
10559
10560 /* Find the ss for the lhs. */
10561 lss = loop->ss;
10562 for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
f98cfd3c 10563 if (lss->info->expr == expr1)
597553ab
PT
10564 break;
10565
10566 if (lss == gfc_ss_terminator)
10567 return NULL_TREE;
10568
1838afec
MM
10569 linfo = &lss->info->data.array;
10570
597553ab
PT
10571 /* Find an ss for the rhs. For operator expressions, we see the
10572 ss's for the operands. Any one of these will do. */
10573 rss = loop->ss;
10574 for (; rss && rss != gfc_ss_terminator; rss = rss->loop_chain)
f98cfd3c 10575 if (rss->info->expr != expr1 && rss != loop->temp_ss)
597553ab
PT
10576 break;
10577
10578 if (expr2 && rss == gfc_ss_terminator)
10579 return NULL_TREE;
10580
dc32bc72
PT
10581 /* Ensure that the string length from the current scope is used. */
10582 if (expr2->ts.type == BT_CHARACTER
10583 && expr2->expr_type == EXPR_FUNCTION
10584 && !expr2->value.function.isym)
10585 expr2->ts.u.cl->backend_decl = rss->info->string_length;
10586
597553ab
PT
10587 gfc_start_block (&fblock);
10588
10589 /* Since the lhs is allocatable, this must be a descriptor type.
10590 Get the data and array size. */
1838afec 10591 desc = linfo->descriptor;
597553ab
PT
10592 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
10593 array1 = gfc_conv_descriptor_data_get (desc);
597553ab 10594
ce8dcc91
PT
10595 if (expr2)
10596 desc2 = rss->info->data.array.descriptor;
10597 else
10598 desc2 = NULL_TREE;
10599
10600 /* Get the old lhs element size for deferred character and class expr1. */
10601 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
10602 {
10603 if (expr1->ts.u.cl->backend_decl
10604 && VAR_P (expr1->ts.u.cl->backend_decl))
10605 elemsize1 = expr1->ts.u.cl->backend_decl;
10606 else
10607 elemsize1 = lss->info->string_length;
071d00e0
TB
10608 tree unit_size = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
10609 elemsize1 = fold_build2_loc (input_location, MULT_EXPR,
10610 TREE_TYPE (elemsize1), elemsize1,
10611 fold_convert (TREE_TYPE (elemsize1), unit_size));
10612
ce8dcc91
PT
10613 }
10614 else if (expr1->ts.type == BT_CLASS)
10615 {
9a0e09f3
PT
10616 /* Unfortunately, the lhs vptr is set too early in many cases.
10617 Play it safe by using the descriptor element length. */
10618 tmp = gfc_conv_descriptor_elem_len (desc);
10619 elemsize1 = fold_convert (gfc_array_index_type, tmp);
ce8dcc91
PT
10620 }
10621 else
10622 elemsize1 = NULL_TREE;
10623 if (elemsize1 != NULL_TREE)
10624 elemsize1 = gfc_evaluate_now (elemsize1, &fblock);
10625
10626 /* Get the new lhs size in bytes. */
10627 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
10628 {
10629 if (expr2->ts.deferred)
10630 {
10631 if (expr2->ts.u.cl->backend_decl
10632 && VAR_P (expr2->ts.u.cl->backend_decl))
10633 tmp = expr2->ts.u.cl->backend_decl;
10634 else
10635 tmp = rss->info->string_length;
10636 }
10637 else
10638 {
10639 tmp = expr2->ts.u.cl->backend_decl;
10640 if (!tmp && expr2->expr_type == EXPR_OP
10641 && expr2->value.op.op == INTRINSIC_CONCAT)
10642 {
10643 tmp = concat_str_length (expr2);
10644 expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
10645 }
10646 else if (!tmp && expr2->ts.u.cl->length)
10647 {
10648 gfc_se tmpse;
10649 gfc_init_se (&tmpse, NULL);
10650 gfc_conv_expr_type (&tmpse, expr2->ts.u.cl->length,
10651 gfc_charlen_type_node);
10652 tmp = tmpse.expr;
10653 expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
10654 }
10655 tmp = fold_convert (TREE_TYPE (expr1->ts.u.cl->backend_decl), tmp);
10656 }
10657
10658 if (expr1->ts.u.cl->backend_decl
10659 && VAR_P (expr1->ts.u.cl->backend_decl))
10660 gfc_add_modify (&fblock, expr1->ts.u.cl->backend_decl, tmp);
10661 else
10662 gfc_add_modify (&fblock, lss->info->string_length, tmp);
10663
10664 if (expr1->ts.kind > 1)
10665 tmp = fold_build2_loc (input_location, MULT_EXPR,
10666 TREE_TYPE (tmp),
10667 tmp, build_int_cst (TREE_TYPE (tmp),
10668 expr1->ts.kind));
10669 }
10670 else if (expr1->ts.type == BT_CHARACTER && expr1->ts.u.cl->backend_decl)
10671 {
10672 tmp = TYPE_SIZE_UNIT (TREE_TYPE (gfc_typenode_for_spec (&expr1->ts)));
10673 tmp = fold_build2_loc (input_location, MULT_EXPR,
10674 gfc_array_index_type, tmp,
10675 expr1->ts.u.cl->backend_decl);
10676 }
10677 else if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
10678 tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
10679 else if (expr1->ts.type == BT_CLASS && expr2->ts.type == BT_CLASS)
10680 {
10681 tmp = expr2->rank ? gfc_get_class_from_expr (desc2) : NULL_TREE;
0175d45d
PT
10682 if (tmp == NULL_TREE && expr2->expr_type == EXPR_VARIABLE)
10683 tmp = class_expr2 = gfc_get_class_from_gfc_expr (expr2);
10684
ce8dcc91
PT
10685 if (tmp != NULL_TREE)
10686 tmp = gfc_class_vtab_size_get (tmp);
10687 else
10688 tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
10689 }
10690 else
10691 tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
10692 elemsize2 = fold_convert (gfc_array_index_type, tmp);
10693 elemsize2 = gfc_evaluate_now (elemsize2, &fblock);
10694
93c3bf47
PT
10695 /* 7.4.1.3 "If variable is an allocated allocatable variable, it is
10696 deallocated if expr is an array of different shape or any of the
10697 corresponding length type parameter values of variable and expr
10698 differ." This assures F95 compatibility. */
597553ab
PT
10699 jump_label1 = gfc_build_label_decl (NULL_TREE);
10700 jump_label2 = gfc_build_label_decl (NULL_TREE);
10701
10702 /* Allocate if data is NULL. */
63ee5404 10703 cond_null = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
597553ab 10704 array1, build_int_cst (TREE_TYPE (array1), 0));
071d00e0 10705 cond_null= gfc_evaluate_now (cond_null, &fblock);
78ab5260 10706
d700518b 10707 tmp = build3_v (COND_EXPR, cond_null,
597553ab
PT
10708 build1_v (GOTO_EXPR, jump_label1),
10709 build_empty_stmt (input_location));
10710 gfc_add_expr_to_block (&fblock, tmp);
10711
93c3bf47 10712 /* Get arrayspec if expr is a full array. */
597553ab
PT
10713 if (expr2 && expr2->expr_type == EXPR_FUNCTION
10714 && expr2->value.function.isym
10715 && expr2->value.function.isym->conversion)
10716 {
10717 /* For conversion functions, take the arg. */
10718 gfc_expr *arg = expr2->value.function.actual->expr;
10719 as = gfc_get_full_arrayspec_from_expr (arg);
10720 }
10721 else if (expr2)
10722 as = gfc_get_full_arrayspec_from_expr (expr2);
10723 else
10724 as = NULL;
10725
93c3bf47 10726 /* If the lhs shape is not the same as the rhs jump to setting the
f04986a9 10727 bounds and doing the reallocation....... */
93c3bf47 10728 for (n = 0; n < expr1->rank; n++)
597553ab 10729 {
93c3bf47
PT
10730 /* Check the shape. */
10731 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
10732 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
10733 tmp = fold_build2_loc (input_location, MINUS_EXPR,
10734 gfc_array_index_type,
10735 loop->to[n], loop->from[n]);
10736 tmp = fold_build2_loc (input_location, PLUS_EXPR,
10737 gfc_array_index_type,
10738 tmp, lbound);
10739 tmp = fold_build2_loc (input_location, MINUS_EXPR,
10740 gfc_array_index_type,
10741 tmp, ubound);
10742 cond = fold_build2_loc (input_location, NE_EXPR,
63ee5404 10743 logical_type_node,
93c3bf47
PT
10744 tmp, gfc_index_zero_node);
10745 tmp = build3_v (COND_EXPR, cond,
10746 build1_v (GOTO_EXPR, jump_label1),
10747 build_empty_stmt (input_location));
f04986a9 10748 gfc_add_expr_to_block (&fblock, tmp);
93c3bf47
PT
10749 }
10750
ce8dcc91
PT
10751 /* ...else if the element lengths are not the same also go to
10752 setting the bounds and doing the reallocation.... */
10753 if (elemsize1 != NULL_TREE)
10754 {
10755 cond = fold_build2_loc (input_location, NE_EXPR,
10756 logical_type_node,
10757 elemsize1, elemsize2);
10758 tmp = build3_v (COND_EXPR, cond,
10759 build1_v (GOTO_EXPR, jump_label1),
10760 build_empty_stmt (input_location));
10761 gfc_add_expr_to_block (&fblock, tmp);
10762 }
10763
93c3bf47
PT
10764 /* ....else jump past the (re)alloc code. */
10765 tmp = build1_v (GOTO_EXPR, jump_label2);
10766 gfc_add_expr_to_block (&fblock, tmp);
f04986a9 10767
93c3bf47
PT
10768 /* Add the label to start automatic (re)allocation. */
10769 tmp = build1_v (LABEL_EXPR, jump_label1);
10770 gfc_add_expr_to_block (&fblock, tmp);
597553ab 10771
d700518b 10772 /* Get the rhs size and fix it. */
93c3bf47
PT
10773 size2 = gfc_index_one_node;
10774 for (n = 0; n < expr2->rank; n++)
10775 {
10776 tmp = fold_build2_loc (input_location, MINUS_EXPR,
10777 gfc_array_index_type,
10778 loop->to[n], loop->from[n]);
10779 tmp = fold_build2_loc (input_location, PLUS_EXPR,
10780 gfc_array_index_type,
10781 tmp, gfc_index_one_node);
10782 size2 = fold_build2_loc (input_location, MULT_EXPR,
10783 gfc_array_index_type,
10784 tmp, size2);
597553ab 10785 }
93c3bf47
PT
10786 size2 = gfc_evaluate_now (size2, &fblock);
10787
16e24756
PT
10788 /* Deallocation of allocatable components will have to occur on
10789 reallocation. Fix the old descriptor now. */
10790 if ((expr1->ts.type == BT_DERIVED)
10791 && expr1->ts.u.derived->attr.alloc_comp)
10792 old_desc = gfc_evaluate_now (desc, &fblock);
10793 else
10794 old_desc = NULL_TREE;
597553ab
PT
10795
10796 /* Now modify the lhs descriptor and the associated scalarizer
93c3bf47
PT
10797 variables. F2003 7.4.1.3: "If variable is or becomes an
10798 unallocated allocatable variable, then it is allocated with each
10799 deferred type parameter equal to the corresponding type parameters
10800 of expr , with the shape of expr , and with each lower bound equal
f04986a9 10801 to the corresponding element of LBOUND(expr)."
93c3bf47
PT
10802 Reuse size1 to keep a dimension-by-dimension track of the
10803 stride of the new array. */
597553ab
PT
10804 size1 = gfc_index_one_node;
10805 offset = gfc_index_zero_node;
10806
10807 for (n = 0; n < expr2->rank; n++)
10808 {
10809 tmp = fold_build2_loc (input_location, MINUS_EXPR,
10810 gfc_array_index_type,
10811 loop->to[n], loop->from[n]);
10812 tmp = fold_build2_loc (input_location, PLUS_EXPR,
10813 gfc_array_index_type,
10814 tmp, gfc_index_one_node);
10815
10816 lbound = gfc_index_one_node;
10817 ubound = tmp;
10818
10819 if (as)
10820 {
10821 lbd = get_std_lbound (expr2, desc2, n,
10822 as->type == AS_ASSUMED_SIZE);
10823 ubound = fold_build2_loc (input_location,
10824 MINUS_EXPR,
10825 gfc_array_index_type,
10826 ubound, lbound);
10827 ubound = fold_build2_loc (input_location,
10828 PLUS_EXPR,
10829 gfc_array_index_type,
10830 ubound, lbd);
10831 lbound = lbd;
10832 }
10833
10834 gfc_conv_descriptor_lbound_set (&fblock, desc,
10835 gfc_rank_cst[n],
10836 lbound);
10837 gfc_conv_descriptor_ubound_set (&fblock, desc,
10838 gfc_rank_cst[n],
10839 ubound);
10840 gfc_conv_descriptor_stride_set (&fblock, desc,
10841 gfc_rank_cst[n],
10842 size1);
10843 lbound = gfc_conv_descriptor_lbound_get (desc,
10844 gfc_rank_cst[n]);
10845 tmp2 = fold_build2_loc (input_location, MULT_EXPR,
10846 gfc_array_index_type,
10847 lbound, size1);
10848 offset = fold_build2_loc (input_location, MINUS_EXPR,
10849 gfc_array_index_type,
10850 offset, tmp2);
10851 size1 = fold_build2_loc (input_location, MULT_EXPR,
10852 gfc_array_index_type,
10853 tmp, size1);
10854 }
10855
10856 /* Set the lhs descriptor and scalarizer offsets. For rank > 1,
10857 the array offset is saved and the info.offset is used for a
10858 running offset. Use the saved_offset instead. */
10859 tmp = gfc_conv_descriptor_offset (desc);
10860 gfc_add_modify (&fblock, tmp, offset);
1838afec 10861 if (linfo->saved_offset
d168c883 10862 && VAR_P (linfo->saved_offset))
1838afec 10863 gfc_add_modify (&fblock, linfo->saved_offset, tmp);
597553ab
PT
10864
10865 /* Now set the deltas for the lhs. */
10866 for (n = 0; n < expr1->rank; n++)
10867 {
10868 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
cb4b9eae 10869 dim = lss->dim[n];
597553ab
PT
10870 tmp = fold_build2_loc (input_location, MINUS_EXPR,
10871 gfc_array_index_type, tmp,
10872 loop->from[dim]);
d168c883 10873 if (linfo->delta[dim] && VAR_P (linfo->delta[dim]))
1838afec 10874 gfc_add_modify (&fblock, linfo->delta[dim], tmp);
597553ab
PT
10875 }
10876
9d44426f 10877 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
ce8dcc91 10878 gfc_conv_descriptor_span_set (&fblock, desc, elemsize2);
9d44426f 10879
597553ab
PT
10880 size2 = fold_build2_loc (input_location, MULT_EXPR,
10881 gfc_array_index_type,
ce8dcc91 10882 elemsize2, size2);
597553ab 10883 size2 = fold_convert (size_type_node, size2);
6f556b07
TB
10884 size2 = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
10885 size2, size_one_node);
597553ab
PT
10886 size2 = gfc_evaluate_now (size2, &fblock);
10887
78ab5260
PT
10888 /* For deferred character length, the 'size' field of the dtype might
10889 have changed so set the dtype. */
10890 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
10891 && expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
10892 {
10893 tree type;
10894 tmp = gfc_conv_descriptor_dtype (desc);
10895 if (expr2->ts.u.cl->backend_decl)
10896 type = gfc_typenode_for_spec (&expr2->ts);
10897 else
10898 type = gfc_typenode_for_spec (&expr1->ts);
10899
10900 gfc_add_modify (&fblock, tmp,
10901 gfc_get_dtype_rank_type (expr1->rank,type));
10902 }
ce8dcc91 10903 else if (expr1->ts.type == BT_CLASS)
75382a96
PT
10904 {
10905 tree type;
10906 tmp = gfc_conv_descriptor_dtype (desc);
ce8dcc91
PT
10907
10908 if (expr2->ts.type != BT_CLASS)
10909 type = gfc_typenode_for_spec (&expr2->ts);
10910 else
10911 type = gfc_get_character_type_len (1, elemsize2);
10912
75382a96
PT
10913 gfc_add_modify (&fblock, tmp,
10914 gfc_get_dtype_rank_type (expr2->rank,type));
10915 /* Set the _len field as well... */
ce8dcc91
PT
10916 if (UNLIMITED_POLY (expr1))
10917 {
10918 tmp = gfc_class_len_get (TREE_OPERAND (desc, 0));
10919 if (expr2->ts.type == BT_CHARACTER)
10920 gfc_add_modify (&fblock, tmp,
10921 fold_convert (TREE_TYPE (tmp),
10922 TYPE_SIZE_UNIT (type)));
10923 else
10924 gfc_add_modify (&fblock, tmp,
10925 build_int_cst (TREE_TYPE (tmp), 0));
10926 }
75382a96
PT
10927 /* ...and the vptr. */
10928 tmp = gfc_class_vptr_get (TREE_OPERAND (desc, 0));
ce8dcc91
PT
10929 if (expr2->ts.type == BT_CLASS && !VAR_P (desc2)
10930 && TREE_CODE (desc2) == COMPONENT_REF)
10931 {
10932 tmp2 = gfc_get_class_from_expr (desc2);
10933 tmp2 = gfc_class_vptr_get (tmp2);
10934 }
0175d45d
PT
10935 else if (expr2->ts.type == BT_CLASS && class_expr2 != NULL_TREE)
10936 tmp2 = gfc_class_vptr_get (class_expr2);
ce8dcc91
PT
10937 else
10938 {
10939 tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
10940 tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
10941 }
10942
10943 gfc_add_modify (&fblock, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
75382a96 10944 }
3c9f5092
AV
10945 else if (coarray && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
10946 {
10947 gfc_add_modify (&fblock, gfc_conv_descriptor_dtype (desc),
10948 gfc_get_dtype (TREE_TYPE (desc)));
10949 }
78ab5260 10950
597553ab 10951 /* Realloc expression. Note that the scalarizer uses desc.data
1cc0e193 10952 in the array reference - (*desc.data)[<element>]. */
597553ab 10953 gfc_init_block (&realloc_block);
3c9f5092 10954 gfc_init_se (&caf_se, NULL);
16e24756 10955
3c9f5092
AV
10956 if (coarray)
10957 {
10958 token = gfc_get_ultimate_alloc_ptr_comps_caf_token (&caf_se, expr1);
10959 if (token == NULL_TREE)
10960 {
10961 tmp = gfc_get_tree_for_caf_expr (expr1);
6479f45b
AV
10962 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
10963 tmp = build_fold_indirect_ref (tmp);
3c9f5092
AV
10964 gfc_get_caf_token_offset (&caf_se, &token, NULL, tmp, NULL_TREE,
10965 expr1);
10966 token = gfc_build_addr_expr (NULL_TREE, token);
10967 }
10968
10969 gfc_add_block_to_block (&realloc_block, &caf_se.pre);
10970 }
16e24756
PT
10971 if ((expr1->ts.type == BT_DERIVED)
10972 && expr1->ts.u.derived->attr.alloc_comp)
10973 {
abc2d807
TB
10974 tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, old_desc,
10975 expr1->rank);
16e24756
PT
10976 gfc_add_expr_to_block (&realloc_block, tmp);
10977 }
10978
3c9f5092
AV
10979 if (!coarray)
10980 {
10981 tmp = build_call_expr_loc (input_location,
10982 builtin_decl_explicit (BUILT_IN_REALLOC), 2,
10983 fold_convert (pvoid_type_node, array1),
10984 size2);
10985 gfc_conv_descriptor_data_set (&realloc_block,
10986 desc, tmp);
10987 }
10988 else
10989 {
10990 tmp = build_call_expr_loc (input_location,
ba85c8c3
AV
10991 gfor_fndecl_caf_deregister, 5, token,
10992 build_int_cst (integer_type_node,
10993 GFC_CAF_COARRAY_DEALLOCATE_ONLY),
10994 null_pointer_node, null_pointer_node,
10995 integer_zero_node);
3c9f5092
AV
10996 gfc_add_expr_to_block (&realloc_block, tmp);
10997 tmp = build_call_expr_loc (input_location,
10998 gfor_fndecl_caf_register,
10999 7, size2,
11000 build_int_cst (integer_type_node,
ba85c8c3 11001 GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY),
3c9f5092
AV
11002 token, gfc_build_addr_expr (NULL_TREE, desc),
11003 null_pointer_node, null_pointer_node,
11004 integer_zero_node);
11005 gfc_add_expr_to_block (&realloc_block, tmp);
11006 }
16e24756
PT
11007
11008 if ((expr1->ts.type == BT_DERIVED)
11009 && expr1->ts.u.derived->attr.alloc_comp)
11010 {
11011 tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
11012 expr1->rank);
11013 gfc_add_expr_to_block (&realloc_block, tmp);
11014 }
11015
3c9f5092 11016 gfc_add_block_to_block (&realloc_block, &caf_se.post);
597553ab
PT
11017 realloc_expr = gfc_finish_block (&realloc_block);
11018
597553ab
PT
11019 /* Malloc expression. */
11020 gfc_init_block (&alloc_block);
3c9f5092
AV
11021 if (!coarray)
11022 {
11023 tmp = build_call_expr_loc (input_location,
11024 builtin_decl_explicit (BUILT_IN_MALLOC),
11025 1, size2);
11026 gfc_conv_descriptor_data_set (&alloc_block,
11027 desc, tmp);
11028 }
11029 else
11030 {
11031 tmp = build_call_expr_loc (input_location,
11032 gfor_fndecl_caf_register,
11033 7, size2,
11034 build_int_cst (integer_type_node,
11035 GFC_CAF_COARRAY_ALLOC),
11036 token, gfc_build_addr_expr (NULL_TREE, desc),
11037 null_pointer_node, null_pointer_node,
11038 integer_zero_node);
11039 gfc_add_expr_to_block (&alloc_block, tmp);
11040 }
11041
78ab5260
PT
11042
11043 /* We already set the dtype in the case of deferred character
9a0e09f3 11044 length arrays and class lvalues. */
78ab5260 11045 if (!(GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
3c9f5092 11046 && ((expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
75382a96 11047 || coarray))
9a0e09f3 11048 && expr1->ts.type != BT_CLASS)
78ab5260
PT
11049 {
11050 tmp = gfc_conv_descriptor_dtype (desc);
11051 gfc_add_modify (&alloc_block, tmp, gfc_get_dtype (TREE_TYPE (desc)));
11052 }
11053
16e24756
PT
11054 if ((expr1->ts.type == BT_DERIVED)
11055 && expr1->ts.u.derived->attr.alloc_comp)
11056 {
11057 tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
11058 expr1->rank);
11059 gfc_add_expr_to_block (&alloc_block, tmp);
11060 }
597553ab
PT
11061 alloc_expr = gfc_finish_block (&alloc_block);
11062
11063 /* Malloc if not allocated; realloc otherwise. */
ce8dcc91 11064 tmp = build3_v (COND_EXPR, cond_null, alloc_expr, realloc_expr);
597553ab
PT
11065 gfc_add_expr_to_block (&fblock, tmp);
11066
11067 /* Make sure that the scalarizer data pointer is updated. */
d168c883 11068 if (linfo->data && VAR_P (linfo->data))
597553ab
PT
11069 {
11070 tmp = gfc_conv_descriptor_data_get (desc);
1838afec 11071 gfc_add_modify (&fblock, linfo->data, tmp);
597553ab
PT
11072 }
11073
ce8dcc91 11074 /* Add the label for same shape lhs and rhs. */
597553ab
PT
11075 tmp = build1_v (LABEL_EXPR, jump_label2);
11076 gfc_add_expr_to_block (&fblock, tmp);
11077
11078 return gfc_finish_block (&fblock);
11079}
11080
11081
4cfdaeb2
JRFS
11082/* Initialize class descriptor's TKR infomation. */
11083
11084void
11085gfc_trans_class_array (gfc_symbol * sym, gfc_wrapped_block * block)
11086{
11087 tree type, etype;
11088 tree tmp;
11089 tree descriptor;
11090 stmtblock_t init;
11091 locus loc;
11092 int rank;
11093
11094 /* Make sure the frontend gets these right. */
11095 gcc_assert (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
11096 && (CLASS_DATA (sym)->attr.class_pointer
11097 || CLASS_DATA (sym)->attr.allocatable));
11098
11099 gcc_assert (VAR_P (sym->backend_decl)
11100 || TREE_CODE (sym->backend_decl) == PARM_DECL);
11101
11102 if (sym->attr.dummy)
11103 return;
11104
11105 descriptor = gfc_class_data_get (sym->backend_decl);
11106 type = TREE_TYPE (descriptor);
11107
11108 if (type == NULL || !GFC_DESCRIPTOR_TYPE_P (type))
11109 return;
11110
11111 gfc_save_backend_locus (&loc);
11112 gfc_set_backend_locus (&sym->declared_at);
11113 gfc_init_block (&init);
11114
11115 rank = CLASS_DATA (sym)->as ? (CLASS_DATA (sym)->as->rank) : (0);
11116 gcc_assert (rank>=0);
11117 tmp = gfc_conv_descriptor_dtype (descriptor);
11118 etype = gfc_get_element_type (type);
11119 tmp = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp), tmp,
11120 gfc_get_dtype_rank_type (rank, etype));
11121 gfc_add_expr_to_block (&init, tmp);
11122
11123 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
11124 gfc_restore_backend_locus (&loc);
11125}
11126
11127
5046aff5
PT
11128/* NULLIFY an allocatable/pointer array on function entry, free it on exit.
11129 Do likewise, recursively if necessary, with the allocatable components of
62ede14d
TB
11130 derived types. This function is also called for assumed-rank arrays, which
11131 are always dummy arguments. */
6de9cd9a 11132
0019d498
DK
11133void
11134gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
6de9cd9a
DN
11135{
11136 tree type;
11137 tree tmp;
11138 tree descriptor;
0019d498
DK
11139 stmtblock_t init;
11140 stmtblock_t cleanup;
6de9cd9a 11141 locus loc;
5046aff5 11142 int rank;
ef292537 11143 bool sym_has_alloc_comp, has_finalizer;
5046aff5 11144
272cec5d
TK
11145 sym_has_alloc_comp = (sym->ts.type == BT_DERIVED
11146 || sym->ts.type == BT_CLASS)
bc21d315 11147 && sym->ts.u.derived->attr.alloc_comp;
ea8b72e6
TB
11148 has_finalizer = sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED
11149 ? gfc_is_finalizable (sym->ts.u.derived, NULL) : false;
6de9cd9a
DN
11150
11151 /* Make sure the frontend gets these right. */
ea8b72e6 11152 gcc_assert (sym->attr.pointer || sym->attr.allocatable || sym_has_alloc_comp
62ede14d
TB
11153 || has_finalizer
11154 || (sym->as->type == AS_ASSUMED_RANK && sym->attr.dummy));
6de9cd9a 11155
ceccaacf
TB
11156 gfc_save_backend_locus (&loc);
11157 gfc_set_backend_locus (&sym->declared_at);
0019d498 11158 gfc_init_block (&init);
6de9cd9a 11159
d168c883
JJ
11160 gcc_assert (VAR_P (sym->backend_decl)
11161 || TREE_CODE (sym->backend_decl) == PARM_DECL);
99c7ab42 11162
6de9cd9a 11163 if (sym->ts.type == BT_CHARACTER
bc21d315 11164 && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
417ab240 11165 {
0019d498
DK
11166 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
11167 gfc_trans_vla_type_sizes (sym, &init);
417ab240 11168 }
6de9cd9a 11169
bafc96b4
PT
11170 /* Dummy, use associated and result variables don't need anything special. */
11171 if (sym->attr.dummy || sym->attr.use_assoc || sym->attr.result)
6de9cd9a 11172 {
0019d498 11173 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
ceccaacf 11174 gfc_restore_backend_locus (&loc);
0019d498 11175 return;
6de9cd9a
DN
11176 }
11177
6de9cd9a
DN
11178 descriptor = sym->backend_decl;
11179
b2a43373 11180 /* Although static, derived types with default initializers and
5046aff5
PT
11181 allocatable components must not be nulled wholesale; instead they
11182 are treated component by component. */
ea8b72e6 11183 if (TREE_STATIC (descriptor) && !sym_has_alloc_comp && !has_finalizer)
6de9cd9a
DN
11184 {
11185 /* SAVEd variables are not freed on exit. */
11186 gfc_trans_static_array_pointer (sym);
0019d498
DK
11187
11188 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
363aab21 11189 gfc_restore_backend_locus (&loc);
0019d498 11190 return;
6de9cd9a
DN
11191 }
11192
11193 /* Get the descriptor type. */
11194 type = TREE_TYPE (sym->backend_decl);
2b56d6a4 11195
ea8b72e6
TB
11196 if ((sym_has_alloc_comp || (has_finalizer && sym->ts.type != BT_CLASS))
11197 && !(sym->attr.pointer || sym->attr.allocatable))
5046aff5 11198 {
2b56d6a4
TB
11199 if (!sym->attr.save
11200 && !(TREE_STATIC (sym->backend_decl) && sym->attr.is_main_program))
36d3fb4c 11201 {
16e520b6
DF
11202 if (sym->value == NULL
11203 || !gfc_has_default_initializer (sym->ts.u.derived))
2b56d6a4
TB
11204 {
11205 rank = sym->as ? sym->as->rank : 0;
0019d498
DK
11206 tmp = gfc_nullify_alloc_comp (sym->ts.u.derived,
11207 descriptor, rank);
11208 gfc_add_expr_to_block (&init, tmp);
2b56d6a4
TB
11209 }
11210 else
0019d498 11211 gfc_init_default_dt (sym, &init, false);
36d3fb4c 11212 }
5046aff5
PT
11213 }
11214 else if (!GFC_DESCRIPTOR_TYPE_P (type))
f5f701ad
PT
11215 {
11216 /* If the backend_decl is not a descriptor, we must have a pointer
11217 to one. */
db3927fb 11218 descriptor = build_fold_indirect_ref_loc (input_location,
0019d498 11219 sym->backend_decl);
f5f701ad 11220 type = TREE_TYPE (descriptor);
f5f701ad 11221 }
f04986a9 11222
727dc121
JV
11223 /* NULLIFY the data pointer, for non-saved allocatables. */
11224 if (GFC_DESCRIPTOR_TYPE_P (type) && !sym->attr.save && sym->attr.allocatable)
ba85c8c3
AV
11225 {
11226 gfc_conv_descriptor_data_set (&init, descriptor, null_pointer_node);
11227 if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
11228 {
11229 /* Declare the variable static so its array descriptor stays present
11230 after leaving the scope. It may still be accessed through another
11231 image. This may happen, for example, with the caf_mpi
11232 implementation. */
11233 TREE_STATIC (descriptor) = 1;
11234 tmp = gfc_conv_descriptor_token (descriptor);
11235 gfc_add_modify (&init, tmp, fold_convert (TREE_TYPE (tmp),
11236 null_pointer_node));
11237 }
11238 }
6de9cd9a 11239
c1c86ab9
JRFS
11240 /* Set initial TKR for pointers and allocatables */
11241 if (GFC_DESCRIPTOR_TYPE_P (type)
11242 && (sym->attr.pointer || sym->attr.allocatable))
11243 {
11244 tree etype;
11245
11246 gcc_assert (sym->as && sym->as->rank>=0);
11247 tmp = gfc_conv_descriptor_dtype (descriptor);
11248 etype = gfc_get_element_type (type);
11249 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
11250 TREE_TYPE (tmp), tmp,
11251 gfc_get_dtype_rank_type (sym->as->rank, etype));
11252 gfc_add_expr_to_block (&init, tmp);
11253 }
363aab21 11254 gfc_restore_backend_locus (&loc);
ceccaacf 11255 gfc_init_block (&cleanup);
5046aff5
PT
11256
11257 /* Allocatable arrays need to be freed when they go out of scope.
11258 The allocatable components of pointers must not be touched. */
ea8b72e6
TB
11259 if (!sym->attr.allocatable && has_finalizer && sym->ts.type != BT_CLASS
11260 && !sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
11261 && !sym->ns->proc_name->attr.is_main_program)
11262 {
11263 gfc_expr *e;
11264 sym->attr.referenced = 1;
11265 e = gfc_lval_expr_from_sym (sym);
11266 gfc_add_finalizer_call (&cleanup, e);
11267 gfc_free_expr (e);
11268 }
11269 else if ((!sym->attr.allocatable || !has_finalizer)
ef292537
TB
11270 && sym_has_alloc_comp && !(sym->attr.function || sym->attr.result)
11271 && !sym->attr.pointer && !sym->attr.save
11272 && !sym->ns->proc_name->attr.is_main_program)
5046aff5
PT
11273 {
11274 int rank;
11275 rank = sym->as ? sym->as->rank : 0;
bc21d315 11276 tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank);
0019d498 11277 gfc_add_expr_to_block (&cleanup, tmp);
5046aff5
PT
11278 }
11279
badd9e69 11280 if (sym->attr.allocatable && (sym->attr.dimension || sym->attr.codimension)
ef292537
TB
11281 && !sym->attr.save && !sym->attr.result
11282 && !sym->ns->proc_name->attr.is_main_program)
6de9cd9a 11283 {
6a2bf10f
TB
11284 gfc_expr *e;
11285 e = has_finalizer ? gfc_lval_expr_from_sym (sym) : NULL;
39da5866
AV
11286 tmp = gfc_deallocate_with_status (sym->backend_decl, NULL_TREE, NULL_TREE,
11287 NULL_TREE, NULL_TREE, true, e,
11288 sym->attr.codimension
11289 ? GFC_CAF_COARRAY_DEREGISTER
11290 : GFC_CAF_COARRAY_NOCOARRAY);
6a2bf10f
TB
11291 if (e)
11292 gfc_free_expr (e);
0019d498 11293 gfc_add_expr_to_block (&cleanup, tmp);
6de9cd9a
DN
11294 }
11295
0019d498
DK
11296 gfc_add_init_cleanup (block, gfc_finish_block (&init),
11297 gfc_finish_block (&cleanup));
6de9cd9a
DN
11298}
11299
11300/************ Expression Walking Functions ******************/
11301
11302/* Walk a variable reference.
11303
11304 Possible extension - multiple component subscripts.
11305 x(:,:) = foo%a(:)%b(:)
11306 Transforms to
11307 forall (i=..., j=...)
11308 x(i,j) = foo%a(j)%b(i)
11309 end forall
735dfed7 11310 This adds a fair amount of complexity because you need to deal with more
6de9cd9a
DN
11311 than one ref. Maybe handle in a similar manner to vector subscripts.
11312 Maybe not worth the effort. */
11313
11314
11315static gfc_ss *
11316gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
11317{
11318 gfc_ref *ref;
6de9cd9a 11319
4932364b
TK
11320 gfc_fix_class_refs (expr);
11321
6de9cd9a 11322 for (ref = expr->ref; ref; ref = ref->next)
068e7338
RS
11323 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
11324 break;
11325
42ac5ee1
MM
11326 return gfc_walk_array_ref (ss, expr, ref);
11327}
11328
11329
11330gfc_ss *
11331gfc_walk_array_ref (gfc_ss * ss, gfc_expr * expr, gfc_ref * ref)
11332{
11333 gfc_array_ref *ar;
11334 gfc_ss *newss;
11335 int n;
11336
068e7338 11337 for (; ref; ref = ref->next)
6de9cd9a 11338 {
068e7338
RS
11339 if (ref->type == REF_SUBSTRING)
11340 {
26f77530 11341 ss = gfc_get_scalar_ss (ss, ref->u.ss.start);
c2e99836
ME
11342 if (ref->u.ss.end)
11343 ss = gfc_get_scalar_ss (ss, ref->u.ss.end);
068e7338
RS
11344 }
11345
11346 /* We're only interested in array sections from now on. */
6de9cd9a
DN
11347 if (ref->type != REF_ARRAY)
11348 continue;
11349
11350 ar = &ref->u.ar;
d3a9eea2 11351
6de9cd9a
DN
11352 switch (ar->type)
11353 {
11354 case AR_ELEMENT:
a7c61416 11355 for (n = ar->dimen - 1; n >= 0; n--)
26f77530 11356 ss = gfc_get_scalar_ss (ss, ar->start[n]);
6de9cd9a
DN
11357 break;
11358
11359 case AR_FULL:
66877276 11360 newss = gfc_get_array_ss (ss, expr, ar->as->rank, GFC_SS_SECTION);
1838afec 11361 newss->info->data.array.ref = ref;
6de9cd9a
DN
11362
11363 /* Make sure array is the same as array(:,:), this way
11364 we don't need to special case all the time. */
11365 ar->dimen = ar->as->rank;
11366 for (n = 0; n < ar->dimen; n++)
11367 {
6de9cd9a
DN
11368 ar->dimen_type[n] = DIMEN_RANGE;
11369
6e45f57b
PB
11370 gcc_assert (ar->start[n] == NULL);
11371 gcc_assert (ar->end[n] == NULL);
11372 gcc_assert (ar->stride[n] == NULL);
6de9cd9a 11373 }
068e7338
RS
11374 ss = newss;
11375 break;
6de9cd9a
DN
11376
11377 case AR_SECTION:
66877276 11378 newss = gfc_get_array_ss (ss, expr, 0, GFC_SS_SECTION);
1838afec 11379 newss->info->data.array.ref = ref;
6de9cd9a 11380
66877276 11381 /* We add SS chains for all the subscripts in the section. */
d7baf647 11382 for (n = 0; n < ar->dimen; n++)
6de9cd9a
DN
11383 {
11384 gfc_ss *indexss;
11385
11386 switch (ar->dimen_type[n])
11387 {
11388 case DIMEN_ELEMENT:
11389 /* Add SS for elemental (scalar) subscripts. */
6e45f57b 11390 gcc_assert (ar->start[n]);
26f77530 11391 indexss = gfc_get_scalar_ss (gfc_ss_terminator, ar->start[n]);
6de9cd9a 11392 indexss->loop_chain = gfc_ss_terminator;
1838afec 11393 newss->info->data.array.subscript[n] = indexss;
6de9cd9a
DN
11394 break;
11395
11396 case DIMEN_RANGE:
11397 /* We don't add anything for sections, just remember this
11398 dimension for later. */
cb4b9eae
MM
11399 newss->dim[newss->dimen] = n;
11400 newss->dimen++;
6de9cd9a
DN
11401 break;
11402
11403 case DIMEN_VECTOR:
7a70c12d
RS
11404 /* Create a GFC_SS_VECTOR index in which we can store
11405 the vector's descriptor. */
66877276
MM
11406 indexss = gfc_get_array_ss (gfc_ss_terminator, ar->start[n],
11407 1, GFC_SS_VECTOR);
7a70c12d 11408 indexss->loop_chain = gfc_ss_terminator;
1838afec 11409 newss->info->data.array.subscript[n] = indexss;
cb4b9eae
MM
11410 newss->dim[newss->dimen] = n;
11411 newss->dimen++;
6de9cd9a
DN
11412 break;
11413
11414 default:
11415 /* We should know what sort of section it is by now. */
6e45f57b 11416 gcc_unreachable ();
6de9cd9a
DN
11417 }
11418 }
6b81e94d
MM
11419 /* We should have at least one non-elemental dimension,
11420 unless we are creating a descriptor for a (scalar) coarray. */
cb4b9eae 11421 gcc_assert (newss->dimen > 0
1838afec 11422 || newss->info->data.array.ref->u.ar.as->corank > 0);
068e7338 11423 ss = newss;
6de9cd9a
DN
11424 break;
11425
11426 default:
11427 /* We should know what sort of section it is by now. */
6e45f57b 11428 gcc_unreachable ();
6de9cd9a
DN
11429 }
11430
11431 }
11432 return ss;
11433}
11434
11435
11436/* Walk an expression operator. If only one operand of a binary expression is
11437 scalar, we must also add the scalar term to the SS chain. */
11438
11439static gfc_ss *
11440gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
11441{
11442 gfc_ss *head;
11443 gfc_ss *head2;
6de9cd9a 11444
58b03ab2
TS
11445 head = gfc_walk_subexpr (ss, expr->value.op.op1);
11446 if (expr->value.op.op2 == NULL)
6de9cd9a
DN
11447 head2 = head;
11448 else
58b03ab2 11449 head2 = gfc_walk_subexpr (head, expr->value.op.op2);
6de9cd9a
DN
11450
11451 /* All operands are scalar. Pass back and let the caller deal with it. */
11452 if (head2 == ss)
11453 return head2;
11454
f7b529fa 11455 /* All operands require scalarization. */
58b03ab2 11456 if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
6de9cd9a
DN
11457 return head2;
11458
11459 /* One of the operands needs scalarization, the other is scalar.
11460 Create a gfc_ss for the scalar expression. */
6de9cd9a
DN
11461 if (head == ss)
11462 {
11463 /* First operand is scalar. We build the chain in reverse order, so
df2fba9e 11464 add the scalar SS after the second operand. */
6de9cd9a
DN
11465 head = head2;
11466 while (head && head->next != ss)
11467 head = head->next;
11468 /* Check we haven't somehow broken the chain. */
6e45f57b 11469 gcc_assert (head);
26f77530 11470 head->next = gfc_get_scalar_ss (ss, expr->value.op.op1);
6de9cd9a
DN
11471 }
11472 else /* head2 == head */
11473 {
6e45f57b 11474 gcc_assert (head2 == head);
6de9cd9a 11475 /* Second operand is scalar. */
26f77530 11476 head2 = gfc_get_scalar_ss (head2, expr->value.op.op2);
6de9cd9a
DN
11477 }
11478
11479 return head2;
11480}
11481
11482
11483/* Reverse a SS chain. */
11484
48474141 11485gfc_ss *
6de9cd9a
DN
11486gfc_reverse_ss (gfc_ss * ss)
11487{
11488 gfc_ss *next;
11489 gfc_ss *head;
11490
6e45f57b 11491 gcc_assert (ss != NULL);
6de9cd9a
DN
11492
11493 head = gfc_ss_terminator;
11494 while (ss != gfc_ss_terminator)
11495 {
11496 next = ss->next;
6e45f57b
PB
11497 /* Check we didn't somehow break the chain. */
11498 gcc_assert (next != NULL);
6de9cd9a
DN
11499 ss->next = head;
11500 head = ss;
11501 ss = next;
11502 }
11503
11504 return (head);
11505}
11506
11507
eea58adb 11508/* Given an expression referring to a procedure, return the symbol of its
58b29fa3
MM
11509 interface. We can't get the procedure symbol directly as we have to handle
11510 the case of (deferred) type-bound procedures. */
11511
11512gfc_symbol *
11513gfc_get_proc_ifc_for_expr (gfc_expr *procedure_ref)
11514{
11515 gfc_symbol *sym;
11516 gfc_ref *ref;
11517
11518 if (procedure_ref == NULL)
11519 return NULL;
11520
11521 /* Normal procedure case. */
252207bd
MM
11522 if (procedure_ref->expr_type == EXPR_FUNCTION
11523 && procedure_ref->value.function.esym)
11524 sym = procedure_ref->value.function.esym;
11525 else
11526 sym = procedure_ref->symtree->n.sym;
58b29fa3
MM
11527
11528 /* Typebound procedure case. */
11529 for (ref = procedure_ref->ref; ref; ref = ref->next)
11530 {
11531 if (ref->type == REF_COMPONENT
11532 && ref->u.c.component->attr.proc_pointer)
11533 sym = ref->u.c.component->ts.interface;
11534 else
11535 sym = NULL;
11536 }
11537
11538 return sym;
11539}
11540
11541
68d62cb2
MM
11542/* Given an expression referring to an intrinsic function call,
11543 return the intrinsic symbol. */
11544
11545gfc_intrinsic_sym *
11546gfc_get_intrinsic_for_expr (gfc_expr *call)
11547{
11548 if (call == NULL)
11549 return NULL;
11550
11551 /* Normal procedure case. */
11552 if (call->expr_type == EXPR_FUNCTION)
11553 return call->value.function.isym;
11554 else
11555 return NULL;
11556}
11557
11558
11559/* Indicates whether an argument to an intrinsic function should be used in
11560 scalarization. It is usually the case, except for some intrinsics
11561 requiring the value to be constant, and using the value at compile time only.
11562 As the value is not used at runtime in those cases, we don’t produce code
11563 for it, and it should not be visible to the scalarizer.
11564 FUNCTION is the intrinsic function being called, ACTUAL_ARG is the actual
11565 argument being examined in that call, and ARG_NUM the index number
11566 of ACTUAL_ARG in the list of arguments.
11567 The intrinsic procedure’s dummy argument associated with ACTUAL_ARG is
11568 identified using the name in ACTUAL_ARG if it is present (that is: if it’s
11569 a keyword argument), otherwise using ARG_NUM. */
11570
11571static bool
11572arg_evaluated_for_scalarization (gfc_intrinsic_sym *function,
48a8c5be 11573 gfc_dummy_arg *dummy_arg)
68d62cb2 11574{
48a8c5be 11575 if (function != NULL && dummy_arg != NULL)
68d62cb2
MM
11576 {
11577 switch (function->id)
11578 {
11579 case GFC_ISYM_INDEX:
721d8b9e 11580 case GFC_ISYM_LEN_TRIM:
c1c17a43
MM
11581 case GFC_ISYM_MASKL:
11582 case GFC_ISYM_MASKR:
11583 case GFC_ISYM_SCAN:
11584 case GFC_ISYM_VERIFY:
48a8c5be 11585 if (strcmp ("kind", gfc_dummy_arg_get_name (*dummy_arg)) == 0)
68d62cb2
MM
11586 return false;
11587 /* Fallthrough. */
11588
11589 default:
11590 break;
11591 }
11592 }
11593
11594 return true;
11595}
11596
11597
17d038cd
MM
11598/* Walk the arguments of an elemental function.
11599 PROC_EXPR is used to check whether an argument is permitted to be absent. If
11600 it is NULL, we don't do the check and the argument is assumed to be present.
11601*/
6de9cd9a
DN
11602
11603gfc_ss *
48474141 11604gfc_walk_elemental_function_args (gfc_ss * ss, gfc_actual_arglist *arg,
68d62cb2 11605 gfc_intrinsic_sym *intrinsic_sym,
5d9d16db 11606 gfc_ss_type type)
6de9cd9a 11607{
6de9cd9a
DN
11608 int scalar;
11609 gfc_ss *head;
11610 gfc_ss *tail;
11611 gfc_ss *newss;
11612
11613 head = gfc_ss_terminator;
11614 tail = NULL;
17d038cd 11615
6de9cd9a 11616 scalar = 1;
48474141 11617 for (; arg; arg = arg->next)
6de9cd9a 11618 {
5d9d16db 11619 gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
68d62cb2
MM
11620 if (!arg->expr
11621 || arg->expr->expr_type == EXPR_NULL
48a8c5be
MM
11622 || !arg_evaluated_for_scalarization (intrinsic_sym, dummy_arg))
11623 continue;
6de9cd9a
DN
11624
11625 newss = gfc_walk_subexpr (head, arg->expr);
11626 if (newss == head)
11627 {
1f2959f0 11628 /* Scalar argument. */
26f77530
MM
11629 gcc_assert (type == GFC_SS_SCALAR || type == GFC_SS_REFERENCE);
11630 newss = gfc_get_scalar_ss (head, arg->expr);
bcc4d4e0 11631 newss->info->type = type;
14aeb3cd 11632 if (dummy_arg)
5d9d16db 11633 newss->info->data.scalar.dummy_arg = dummy_arg;
6de9cd9a
DN
11634 }
11635 else
11636 scalar = 0;
11637
9bcf7121 11638 if (dummy_arg != NULL
5d9d16db 11639 && gfc_dummy_arg_is_optional (*dummy_arg)
9bcf7121
MM
11640 && arg->expr->expr_type == EXPR_VARIABLE
11641 && (gfc_expr_attr (arg->expr).optional
11642 || gfc_expr_attr (arg->expr).allocatable
11643 || gfc_expr_attr (arg->expr).pointer))
11644 newss->info->can_be_null_ref = true;
11645
6de9cd9a
DN
11646 head = newss;
11647 if (!tail)
11648 {
11649 tail = head;
11650 while (tail->next != gfc_ss_terminator)
11651 tail = tail->next;
11652 }
11653 }
11654
11655 if (scalar)
11656 {
11657 /* If all the arguments are scalar we don't need the argument SS. */
11658 gfc_free_ss_chain (head);
11659 /* Pass it back. */
11660 return ss;
11661 }
11662
11663 /* Add it onto the existing chain. */
11664 tail->next = ss;
11665 return head;
11666}
11667
11668
11669/* Walk a function call. Scalar functions are passed back, and taken out of
11670 scalarization loops. For elemental functions we walk their arguments.
11671 The result of functions returning arrays is stored in a temporary outside
11672 the loop, so that the function is only called once. Hence we do not need
11673 to walk their arguments. */
11674
11675static gfc_ss *
11676gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
11677{
6de9cd9a
DN
11678 gfc_intrinsic_sym *isym;
11679 gfc_symbol *sym;
c74b74a8 11680 gfc_component *comp = NULL;
6de9cd9a
DN
11681
11682 isym = expr->value.function.isym;
11683
13413760 11684 /* Handle intrinsic functions separately. */
6de9cd9a
DN
11685 if (isym)
11686 return gfc_walk_intrinsic_function (ss, expr, isym);
11687
11688 sym = expr->value.function.esym;
11689 if (!sym)
1b26c26b 11690 sym = expr->symtree->n.sym;
6de9cd9a 11691
a6b22eea 11692 if (gfc_is_class_array_function (expr))
43a68a9d
PT
11693 return gfc_get_array_ss (ss, expr,
11694 CLASS_DATA (expr->value.function.esym->result)->as->rank,
11695 GFC_SS_FUNCTION);
11696
6de9cd9a 11697 /* A function that returns arrays. */
2a573572 11698 comp = gfc_get_proc_ptr_comp (expr);
c74b74a8
JW
11699 if ((!comp && gfc_return_by_reference (sym) && sym->result->attr.dimension)
11700 || (comp && comp->attr.dimension))
66877276 11701 return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
6de9cd9a
DN
11702
11703 /* Walk the parameters of an elemental function. For now we always pass
11704 by reference. */
1b26c26b 11705 if (sym->attr.elemental || (comp && comp->attr.elemental))
30c931de
PT
11706 {
11707 gfc_ss *old_ss = ss;
11708
11709 ss = gfc_walk_elemental_function_args (old_ss,
11710 expr->value.function.actual,
68d62cb2 11711 gfc_get_intrinsic_for_expr (expr),
dec131b6 11712 GFC_SS_REFERENCE);
30c931de
PT
11713 if (ss != old_ss
11714 && (comp
11715 || sym->attr.proc_pointer
11716 || sym->attr.if_source != IFSRC_DECL
11717 || sym->attr.array_outer_dependency))
11718 ss->info->array_outer_dependency = 1;
11719 }
6de9cd9a 11720
e7dc5b4f 11721 /* Scalar functions are OK as these are evaluated outside the scalarization
6de9cd9a
DN
11722 loop. Pass back and let the caller deal with it. */
11723 return ss;
11724}
11725
11726
11727/* An array temporary is constructed for array constructors. */
11728
11729static gfc_ss *
11730gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
11731{
66877276 11732 return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_CONSTRUCTOR);
6de9cd9a
DN
11733}
11734
11735
1f2959f0 11736/* Walk an expression. Add walked expressions to the head of the SS chain.
aa9c57ec 11737 A wholly scalar expression will not be added. */
6de9cd9a 11738
712efae1 11739gfc_ss *
6de9cd9a
DN
11740gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
11741{
11742 gfc_ss *head;
11743
11744 switch (expr->expr_type)
11745 {
11746 case EXPR_VARIABLE:
11747 head = gfc_walk_variable_expr (ss, expr);
11748 return head;
11749
11750 case EXPR_OP:
11751 head = gfc_walk_op_expr (ss, expr);
11752 return head;
11753
11754 case EXPR_FUNCTION:
11755 head = gfc_walk_function_expr (ss, expr);
11756 return head;
11757
11758 case EXPR_CONSTANT:
11759 case EXPR_NULL:
11760 case EXPR_STRUCTURE:
11761 /* Pass back and let the caller deal with it. */
11762 break;
11763
11764 case EXPR_ARRAY:
11765 head = gfc_walk_array_constructor (ss, expr);
11766 return head;
11767
11768 case EXPR_SUBSTRING:
11769 /* Pass back and let the caller deal with it. */
11770 break;
11771
11772 default:
17d5d49f 11773 gfc_internal_error ("bad expression type during walk (%d)",
6de9cd9a
DN
11774 expr->expr_type);
11775 }
11776 return ss;
11777}
11778
11779
11780/* Entry point for expression walking.
11781 A return value equal to the passed chain means this is
11782 a scalar expression. It is up to the caller to take whatever action is
1f2959f0 11783 necessary to translate these. */
6de9cd9a
DN
11784
11785gfc_ss *
11786gfc_walk_expr (gfc_expr * expr)
11787{
11788 gfc_ss *res;
11789
11790 res = gfc_walk_subexpr (gfc_ss_terminator, expr);
11791 return gfc_reverse_ss (res);
11792}