]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/trans-array.c
spu.c (reg_names): Remove prototype.
[thirdparty/gcc.git] / gcc / fortran / trans-array.c
CommitLineData
6de9cd9a 1/* Array translation routines
fa502cb2 2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
114e4d10 3 Free Software Foundation, Inc.
6de9cd9a
DN
4 Contributed by Paul Brook <paul@nowt.org>
5 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6
9fc4d79b 7This file is part of GCC.
6de9cd9a 8
9fc4d79b
TS
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
d234d788 11Software Foundation; either version 3, or (at your option) any later
9fc4d79b 12version.
6de9cd9a 13
9fc4d79b
TS
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
6de9cd9a
DN
18
19You should have received a copy of the GNU General Public License
d234d788
NC
20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
22
23/* trans-array.c-- Various array related code, including scalarization,
24 allocation, initialization and other support routines. */
25
26/* How the scalarizer works.
27 In gfortran, array expressions use the same core routines as scalar
28 expressions.
29 First, a Scalarization State (SS) chain is built. This is done by walking
30 the expression tree, and building a linear list of the terms in the
31 expression. As the tree is walked, scalar subexpressions are translated.
32
33 The scalarization parameters are stored in a gfc_loopinfo structure.
34 First the start and stride of each term is calculated by
35 gfc_conv_ss_startstride. During this process the expressions for the array
36 descriptors and data pointers are also translated.
37
38 If the expression is an assignment, we must then resolve any dependencies.
39 In fortran all the rhs values of an assignment must be evaluated before
40 any assignments take place. This can require a temporary array to store the
41 values. We also require a temporary when we are passing array expressions
df2fba9e 42 or vector subscripts as procedure parameters.
6de9cd9a
DN
43
44 Array sections are passed without copying to a temporary. These use the
45 scalarizer to determine the shape of the section. The flag
46 loop->array_parameter tells the scalarizer that the actual values and loop
47 variables will not be required.
48
49 The function gfc_conv_loop_setup generates the scalarization setup code.
50 It determines the range of the scalarizing loop variables. If a temporary
51 is required, this is created and initialized. Code for scalar expressions
52 taken outside the loop is also generated at this time. Next the offset and
53 scaling required to translate from loop variables to array indices for each
54 term is calculated.
55
56 A call to gfc_start_scalarized_body marks the start of the scalarized
57 expression. This creates a scope and declares the loop variables. Before
58 calling this gfc_make_ss_chain_used must be used to indicate which terms
59 will be used inside this loop.
60
61 The scalar gfc_conv_* functions are then used to build the main body of the
62 scalarization loop. Scalarization loop variables and precalculated scalar
1f2959f0 63 values are automatically substituted. Note that gfc_advance_se_ss_chain
6de9cd9a
DN
64 must be used, rather than changing the se->ss directly.
65
66 For assignment expressions requiring a temporary two sub loops are
67 generated. The first stores the result of the expression in the temporary,
68 the second copies it to the result. A call to
69 gfc_trans_scalarized_loop_boundary marks the end of the main loop code and
70 the start of the copying loop. The temporary may be less than full rank.
71
72 Finally gfc_trans_scalarizing_loops is called to generate the implicit do
73 loops. The loops are added to the pre chain of the loopinfo. The post
74 chain may still contain cleanup code.
75
76 After the loop code has been added into its parent scope gfc_cleanup_loop
77 is called to free all the SS allocated by the scalarizer. */
78
79#include "config.h"
80#include "system.h"
81#include "coretypes.h"
82#include "tree.h"
c829d016 83#include "diagnostic-core.h" /* For internal_error/fatal_error. */
6de9cd9a 84#include "flags.h"
6de9cd9a 85#include "gfortran.h"
b7e75771 86#include "constructor.h"
6de9cd9a
DN
87#include "trans.h"
88#include "trans-stmt.h"
89#include "trans-types.h"
90#include "trans-array.h"
91#include "trans-const.h"
92#include "dependency.h"
93
94static gfc_ss *gfc_walk_subexpr (gfc_ss *, gfc_expr *);
b7e75771 95static bool gfc_get_array_constructor_size (mpz_t *, gfc_constructor_base);
6de9cd9a 96
13413760 97/* The contents of this structure aren't actually used, just the address. */
6de9cd9a
DN
98static gfc_ss gfc_ss_terminator_var;
99gfc_ss * const gfc_ss_terminator = &gfc_ss_terminator_var;
100
6de9cd9a
DN
101
102static tree
103gfc_array_dataptr_type (tree desc)
104{
105 return (GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc)));
106}
107
108
109/* Build expressions to access the members of an array descriptor.
110 It's surprisingly easy to mess up here, so never access
111 an array descriptor by "brute force", always use these
112 functions. This also avoids problems if we change the format
113 of an array descriptor.
114
115 To understand these magic numbers, look at the comments
116 before gfc_build_array_type() in trans-types.c.
117
118 The code within these defines should be the only code which knows the format
119 of an array descriptor.
120
121 Any code just needing to read obtain the bounds of an array should use
122 gfc_conv_array_* rather than the following functions as these will return
123 know constant values, and work with arrays which do not have descriptors.
124
125 Don't forget to #undef these! */
126
127#define DATA_FIELD 0
128#define OFFSET_FIELD 1
129#define DTYPE_FIELD 2
130#define DIMENSION_FIELD 3
131
132#define STRIDE_SUBFIELD 0
133#define LBOUND_SUBFIELD 1
134#define UBOUND_SUBFIELD 2
135
4c73896d
RH
136/* This provides READ-ONLY access to the data field. The field itself
137 doesn't have the proper type. */
138
6de9cd9a 139tree
4c73896d 140gfc_conv_descriptor_data_get (tree desc)
6de9cd9a 141{
4c73896d 142 tree field, type, t;
6de9cd9a
DN
143
144 type = TREE_TYPE (desc);
6e45f57b 145 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
6de9cd9a
DN
146
147 field = TYPE_FIELDS (type);
6e45f57b 148 gcc_assert (DATA_FIELD == 0);
6de9cd9a 149
44855d8c 150 t = fold_build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
4c73896d
RH
151 t = fold_convert (GFC_TYPE_ARRAY_DATAPTR_TYPE (type), t);
152
153 return t;
154}
155
07beea0d
AH
156/* This provides WRITE access to the data field.
157
158 TUPLES_P is true if we are generating tuples.
159
160 This function gets called through the following macros:
161 gfc_conv_descriptor_data_set
726a989a 162 gfc_conv_descriptor_data_set. */
4c73896d
RH
163
164void
726a989a 165gfc_conv_descriptor_data_set (stmtblock_t *block, tree desc, tree value)
4c73896d
RH
166{
167 tree field, type, t;
168
169 type = TREE_TYPE (desc);
170 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
171
172 field = TYPE_FIELDS (type);
173 gcc_assert (DATA_FIELD == 0);
174
44855d8c 175 t = fold_build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
726a989a 176 gfc_add_modify (block, t, fold_convert (TREE_TYPE (field), value));
4c73896d
RH
177}
178
179
180/* This provides address access to the data field. This should only be
181 used by array allocation, passing this on to the runtime. */
182
183tree
184gfc_conv_descriptor_data_addr (tree desc)
185{
186 tree field, type, t;
187
188 type = TREE_TYPE (desc);
189 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
190
191 field = TYPE_FIELDS (type);
192 gcc_assert (DATA_FIELD == 0);
193
44855d8c 194 t = fold_build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
628c189e 195 return gfc_build_addr_expr (NULL_TREE, t);
6de9cd9a
DN
196}
197
568e8e1e 198static tree
6de9cd9a
DN
199gfc_conv_descriptor_offset (tree desc)
200{
201 tree type;
202 tree field;
203
204 type = TREE_TYPE (desc);
6e45f57b 205 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
6de9cd9a
DN
206
207 field = gfc_advance_chain (TYPE_FIELDS (type), OFFSET_FIELD);
6e45f57b 208 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
6de9cd9a 209
44855d8c
TS
210 return fold_build3 (COMPONENT_REF, TREE_TYPE (field),
211 desc, field, NULL_TREE);
6de9cd9a
DN
212}
213
568e8e1e
PT
214tree
215gfc_conv_descriptor_offset_get (tree desc)
216{
217 return gfc_conv_descriptor_offset (desc);
218}
219
220void
221gfc_conv_descriptor_offset_set (stmtblock_t *block, tree desc,
222 tree value)
223{
224 tree t = gfc_conv_descriptor_offset (desc);
225 gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
226}
227
228
6de9cd9a
DN
229tree
230gfc_conv_descriptor_dtype (tree desc)
231{
232 tree field;
233 tree type;
234
235 type = TREE_TYPE (desc);
6e45f57b 236 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
6de9cd9a
DN
237
238 field = gfc_advance_chain (TYPE_FIELDS (type), DTYPE_FIELD);
6e45f57b 239 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
6de9cd9a 240
44855d8c
TS
241 return fold_build3 (COMPONENT_REF, TREE_TYPE (field),
242 desc, field, NULL_TREE);
6de9cd9a
DN
243}
244
245static tree
246gfc_conv_descriptor_dimension (tree desc, tree dim)
247{
248 tree field;
249 tree type;
250 tree tmp;
251
252 type = TREE_TYPE (desc);
6e45f57b 253 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
6de9cd9a
DN
254
255 field = gfc_advance_chain (TYPE_FIELDS (type), DIMENSION_FIELD);
6e45f57b 256 gcc_assert (field != NULL_TREE
6de9cd9a
DN
257 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
258 && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == RECORD_TYPE);
259
44855d8c
TS
260 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
261 desc, field, NULL_TREE);
1d6b7f39 262 tmp = gfc_build_array_ref (tmp, dim, NULL);
6de9cd9a
DN
263 return tmp;
264}
265
568e8e1e 266static tree
6de9cd9a
DN
267gfc_conv_descriptor_stride (tree desc, tree dim)
268{
269 tree tmp;
270 tree field;
271
272 tmp = gfc_conv_descriptor_dimension (desc, dim);
273 field = TYPE_FIELDS (TREE_TYPE (tmp));
274 field = gfc_advance_chain (field, STRIDE_SUBFIELD);
6e45f57b 275 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
6de9cd9a 276
44855d8c
TS
277 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
278 tmp, field, NULL_TREE);
6de9cd9a
DN
279 return tmp;
280}
281
282tree
568e8e1e
PT
283gfc_conv_descriptor_stride_get (tree desc, tree dim)
284{
a3788c44
MM
285 tree type = TREE_TYPE (desc);
286 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
287 if (integer_zerop (dim)
fe4e525c
TB
288 && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
289 ||GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT
290 ||GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT))
a3788c44
MM
291 return gfc_index_one_node;
292
568e8e1e
PT
293 return gfc_conv_descriptor_stride (desc, dim);
294}
295
296void
297gfc_conv_descriptor_stride_set (stmtblock_t *block, tree desc,
298 tree dim, tree value)
299{
300 tree t = gfc_conv_descriptor_stride (desc, dim);
301 gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
302}
303
304static tree
6de9cd9a
DN
305gfc_conv_descriptor_lbound (tree desc, tree dim)
306{
307 tree tmp;
308 tree field;
309
310 tmp = gfc_conv_descriptor_dimension (desc, dim);
311 field = TYPE_FIELDS (TREE_TYPE (tmp));
312 field = gfc_advance_chain (field, LBOUND_SUBFIELD);
6e45f57b 313 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
6de9cd9a 314
44855d8c
TS
315 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
316 tmp, field, NULL_TREE);
6de9cd9a
DN
317 return tmp;
318}
319
320tree
568e8e1e
PT
321gfc_conv_descriptor_lbound_get (tree desc, tree dim)
322{
323 return gfc_conv_descriptor_lbound (desc, dim);
324}
325
326void
327gfc_conv_descriptor_lbound_set (stmtblock_t *block, tree desc,
328 tree dim, tree value)
329{
330 tree t = gfc_conv_descriptor_lbound (desc, dim);
331 gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
332}
333
334static tree
6de9cd9a
DN
335gfc_conv_descriptor_ubound (tree desc, tree dim)
336{
337 tree tmp;
338 tree field;
339
340 tmp = gfc_conv_descriptor_dimension (desc, dim);
341 field = TYPE_FIELDS (TREE_TYPE (tmp));
342 field = gfc_advance_chain (field, UBOUND_SUBFIELD);
6e45f57b 343 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
6de9cd9a 344
44855d8c
TS
345 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
346 tmp, field, NULL_TREE);
6de9cd9a
DN
347 return tmp;
348}
349
568e8e1e
PT
350tree
351gfc_conv_descriptor_ubound_get (tree desc, tree dim)
352{
353 return gfc_conv_descriptor_ubound (desc, dim);
354}
355
356void
357gfc_conv_descriptor_ubound_set (stmtblock_t *block, tree desc,
358 tree dim, tree value)
359{
360 tree t = gfc_conv_descriptor_ubound (desc, dim);
361 gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
362}
6de9cd9a 363
49de9e73 364/* Build a null array descriptor constructor. */
6de9cd9a 365
331c72f3
PB
366tree
367gfc_build_null_descriptor (tree type)
6de9cd9a 368{
6de9cd9a 369 tree field;
331c72f3 370 tree tmp;
6de9cd9a 371
6e45f57b
PB
372 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
373 gcc_assert (DATA_FIELD == 0);
6de9cd9a
DN
374 field = TYPE_FIELDS (type);
375
331c72f3 376 /* Set a NULL data pointer. */
4038c495 377 tmp = build_constructor_single (type, field, null_pointer_node);
6de9cd9a 378 TREE_CONSTANT (tmp) = 1;
331c72f3
PB
379 /* All other fields are ignored. */
380
381 return tmp;
6de9cd9a
DN
382}
383
384
385/* Cleanup those #defines. */
386
387#undef DATA_FIELD
388#undef OFFSET_FIELD
389#undef DTYPE_FIELD
390#undef DIMENSION_FIELD
391#undef STRIDE_SUBFIELD
392#undef LBOUND_SUBFIELD
393#undef UBOUND_SUBFIELD
394
395
396/* Mark a SS chain as used. Flags specifies in which loops the SS is used.
397 flags & 1 = Main loop body.
398 flags & 2 = temp copy loop. */
399
400void
401gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
402{
403 for (; ss != gfc_ss_terminator; ss = ss->next)
404 ss->useflags = flags;
405}
406
407static void gfc_free_ss (gfc_ss *);
408
409
410/* Free a gfc_ss chain. */
411
412static void
413gfc_free_ss_chain (gfc_ss * ss)
414{
415 gfc_ss *next;
416
417 while (ss != gfc_ss_terminator)
418 {
6e45f57b 419 gcc_assert (ss != NULL);
6de9cd9a
DN
420 next = ss->next;
421 gfc_free_ss (ss);
422 ss = next;
423 }
424}
425
426
427/* Free a SS. */
428
429static void
430gfc_free_ss (gfc_ss * ss)
431{
432 int n;
433
434 switch (ss->type)
435 {
436 case GFC_SS_SECTION:
6de9cd9a
DN
437 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
438 {
439 if (ss->data.info.subscript[n])
440 gfc_free_ss_chain (ss->data.info.subscript[n]);
441 }
442 break;
443
444 default:
445 break;
446 }
447
448 gfc_free (ss);
449}
450
451
452/* Free all the SS associated with a loop. */
453
454void
455gfc_cleanup_loop (gfc_loopinfo * loop)
456{
457 gfc_ss *ss;
458 gfc_ss *next;
459
460 ss = loop->ss;
461 while (ss != gfc_ss_terminator)
462 {
6e45f57b 463 gcc_assert (ss != NULL);
6de9cd9a
DN
464 next = ss->loop_chain;
465 gfc_free_ss (ss);
466 ss = next;
467 }
468}
469
470
471/* Associate a SS chain with a loop. */
472
473void
474gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
475{
476 gfc_ss *ss;
477
478 if (head == gfc_ss_terminator)
479 return;
480
481 ss = head;
482 for (; ss && ss != gfc_ss_terminator; ss = ss->next)
483 {
484 if (ss->next == gfc_ss_terminator)
485 ss->loop_chain = loop->ss;
486 else
487 ss->loop_chain = ss->next;
488 }
6e45f57b 489 gcc_assert (ss == gfc_ss_terminator);
6de9cd9a
DN
490 loop->ss = head;
491}
492
493
331c72f3
PB
494/* Generate an initializer for a static pointer or allocatable array. */
495
496void
497gfc_trans_static_array_pointer (gfc_symbol * sym)
498{
499 tree type;
500
6e45f57b 501 gcc_assert (TREE_STATIC (sym->backend_decl));
331c72f3
PB
502 /* Just zero the data member. */
503 type = TREE_TYPE (sym->backend_decl);
df7df328 504 DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
331c72f3
PB
505}
506
507
62ab4a54
RS
508/* If the bounds of SE's loop have not yet been set, see if they can be
509 determined from array spec AS, which is the array spec of a called
510 function. MAPPING maps the callee's dummy arguments to the values
511 that the caller is passing. Add any initialization and finalization
512 code to SE. */
513
514void
515gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
516 gfc_se * se, gfc_array_spec * as)
517{
518 int n, dim;
519 gfc_se tmpse;
520 tree lower;
521 tree upper;
522 tree tmp;
523
524 if (as && as->type == AS_EXPLICIT)
525 for (dim = 0; dim < se->loop->dimen; dim++)
526 {
527 n = se->loop->order[dim];
528 if (se->loop->to[n] == NULL_TREE)
529 {
530 /* Evaluate the lower bound. */
531 gfc_init_se (&tmpse, NULL);
532 gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
533 gfc_add_block_to_block (&se->pre, &tmpse.pre);
534 gfc_add_block_to_block (&se->post, &tmpse.post);
795dc587 535 lower = fold_convert (gfc_array_index_type, tmpse.expr);
62ab4a54
RS
536
537 /* ...and the upper bound. */
538 gfc_init_se (&tmpse, NULL);
539 gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
540 gfc_add_block_to_block (&se->pre, &tmpse.pre);
541 gfc_add_block_to_block (&se->post, &tmpse.post);
795dc587 542 upper = fold_convert (gfc_array_index_type, tmpse.expr);
62ab4a54
RS
543
544 /* Set the upper bound of the loop to UPPER - LOWER. */
545 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, upper, lower);
546 tmp = gfc_evaluate_now (tmp, &se->pre);
547 se->loop->to[n] = tmp;
548 }
549 }
550}
551
552
6de9cd9a 553/* Generate code to allocate an array temporary, or create a variable to
5b0b7251
EE
554 hold the data. If size is NULL, zero the descriptor so that the
555 callee will allocate the array. If DEALLOC is true, also generate code to
556 free the array afterwards.
ec25720b 557
12f681a0
DK
558 If INITIAL is not NULL, it is packed using internal_pack and the result used
559 as data instead of allocating a fresh, unitialized area of memory.
560
62ab4a54 561 Initialization code is added to PRE and finalization code to POST.
ec25720b
RS
562 DYNAMIC is true if the caller may want to extend the array later
563 using realloc. This prevents us from putting the array on the stack. */
6de9cd9a
DN
564
565static void
62ab4a54 566gfc_trans_allocate_array_storage (stmtblock_t * pre, stmtblock_t * post,
12f681a0
DK
567 gfc_ss_info * info, tree size, tree nelem,
568 tree initial, bool dynamic, bool dealloc)
6de9cd9a
DN
569{
570 tree tmp;
6de9cd9a 571 tree desc;
6de9cd9a
DN
572 bool onstack;
573
574 desc = info->descriptor;
4c73896d 575 info->offset = gfc_index_zero_node;
ec25720b 576 if (size == NULL_TREE || integer_zerop (size))
6de9cd9a 577 {
fc90a8f2 578 /* A callee allocated array. */
62ab4a54 579 gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
fc90a8f2 580 onstack = FALSE;
6de9cd9a
DN
581 }
582 else
583 {
fc90a8f2 584 /* Allocate the temporary. */
12f681a0
DK
585 onstack = !dynamic && initial == NULL_TREE
586 && gfc_can_put_var_on_stack (size);
fc90a8f2
PB
587
588 if (onstack)
589 {
590 /* Make a temporary variable to hold the data. */
10c7a96f 591 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (nelem), nelem,
37da9343 592 gfc_index_one_node);
fc90a8f2
PB
593 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
594 tmp);
595 tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
596 tmp);
597 tmp = gfc_create_var (tmp, "A");
628c189e 598 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
62ab4a54 599 gfc_conv_descriptor_data_set (pre, desc, tmp);
fc90a8f2 600 }
6de9cd9a 601 else
fc90a8f2 602 {
12f681a0
DK
603 /* Allocate memory to hold the data or call internal_pack. */
604 if (initial == NULL_TREE)
605 {
606 tmp = gfc_call_malloc (pre, NULL, size);
607 tmp = gfc_evaluate_now (tmp, pre);
608 }
609 else
610 {
611 tree packed;
612 tree source_data;
613 tree was_packed;
614 stmtblock_t do_copying;
615
616 tmp = TREE_TYPE (initial); /* Pointer to descriptor. */
617 gcc_assert (TREE_CODE (tmp) == POINTER_TYPE);
618 tmp = TREE_TYPE (tmp); /* The descriptor itself. */
619 tmp = gfc_get_element_type (tmp);
620 gcc_assert (tmp == gfc_get_element_type (TREE_TYPE (desc)));
621 packed = gfc_create_var (build_pointer_type (tmp), "data");
622
db3927fb
AH
623 tmp = build_call_expr_loc (input_location,
624 gfor_fndecl_in_pack, 1, initial);
12f681a0
DK
625 tmp = fold_convert (TREE_TYPE (packed), tmp);
626 gfc_add_modify (pre, packed, tmp);
627
db3927fb
AH
628 tmp = build_fold_indirect_ref_loc (input_location,
629 initial);
12f681a0
DK
630 source_data = gfc_conv_descriptor_data_get (tmp);
631
632 /* internal_pack may return source->data without any allocation
633 or copying if it is already packed. If that's the case, we
634 need to allocate and copy manually. */
635
636 gfc_start_block (&do_copying);
637 tmp = gfc_call_malloc (&do_copying, NULL, size);
638 tmp = fold_convert (TREE_TYPE (packed), tmp);
639 gfc_add_modify (&do_copying, packed, tmp);
640 tmp = gfc_build_memcpy_call (packed, source_data, size);
641 gfc_add_expr_to_block (&do_copying, tmp);
642
643 was_packed = fold_build2 (EQ_EXPR, boolean_type_node,
644 packed, source_data);
645 tmp = gfc_finish_block (&do_copying);
c2255bc4
AH
646 tmp = build3_v (COND_EXPR, was_packed, tmp,
647 build_empty_stmt (input_location));
12f681a0
DK
648 gfc_add_expr_to_block (pre, tmp);
649
650 tmp = fold_convert (pvoid_type_node, packed);
651 }
652
62ab4a54 653 gfc_conv_descriptor_data_set (pre, desc, tmp);
fc90a8f2 654 }
6de9cd9a 655 }
4c73896d 656 info->data = gfc_conv_descriptor_data_get (desc);
6de9cd9a
DN
657
658 /* The offset is zero because we create temporaries with a zero
659 lower bound. */
568e8e1e 660 gfc_conv_descriptor_offset_set (pre, desc, gfc_index_zero_node);
6de9cd9a 661
5b0b7251 662 if (dealloc && !onstack)
6de9cd9a
DN
663 {
664 /* Free the temporary. */
4c73896d 665 tmp = gfc_conv_descriptor_data_get (desc);
1529b8d9 666 tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
62ab4a54 667 gfc_add_expr_to_block (post, tmp);
6de9cd9a
DN
668 }
669}
670
671
8e119f1b 672/* Generate code to create and initialize the descriptor for a temporary
e7dc5b4f 673 array. This is used for both temporaries needed by the scalarizer, and
8e119f1b
EE
674 functions returning arrays. Adjusts the loop variables to be
675 zero-based, and calculates the loop bounds for callee allocated arrays.
676 Allocate the array unless it's callee allocated (we have a callee
677 allocated array if 'callee_alloc' is true, or if loop->to[n] is
678 NULL_TREE for any n). Also fills in the descriptor, data and offset
679 fields of info if known. Returns the size of the array, or NULL for a
680 callee allocated array.
ec25720b 681
12f681a0
DK
682 PRE, POST, INITIAL, DYNAMIC and DEALLOC are as for
683 gfc_trans_allocate_array_storage.
5b0b7251 684 */
6de9cd9a
DN
685
686tree
8e119f1b
EE
687gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post,
688 gfc_loopinfo * loop, gfc_ss_info * info,
12f681a0
DK
689 tree eltype, tree initial, bool dynamic,
690 bool dealloc, bool callee_alloc, locus * where)
6de9cd9a
DN
691{
692 tree type;
693 tree desc;
694 tree tmp;
695 tree size;
696 tree nelem;
da4340a1
TK
697 tree cond;
698 tree or_expr;
6de9cd9a
DN
699 int n;
700 int dim;
701
6e45f57b 702 gcc_assert (info->dimen > 0);
bdfd2ff0
TK
703
704 if (gfc_option.warn_array_temp && where)
705 gfc_warning ("Creating array temporary at %L", where);
706
6de9cd9a
DN
707 /* Set the lower bound to zero. */
708 for (dim = 0; dim < info->dimen; dim++)
709 {
710 n = loop->order[dim];
22089905
PT
711 /* Callee allocated arrays may not have a known bound yet. */
712 if (loop->to[n])
713 loop->to[n] = gfc_evaluate_now (fold_build2 (MINUS_EXPR,
714 gfc_array_index_type,
715 loop->to[n], loop->from[n]), pre);
716 loop->from[n] = gfc_index_zero_node;
6de9cd9a 717
7ab92584
SB
718 info->delta[dim] = gfc_index_zero_node;
719 info->start[dim] = gfc_index_zero_node;
8424e0d8 720 info->end[dim] = gfc_index_zero_node;
7ab92584 721 info->stride[dim] = gfc_index_one_node;
6de9cd9a
DN
722 info->dim[dim] = dim;
723 }
724
13413760 725 /* Initialize the descriptor. */
6de9cd9a 726 type =
f33beee9 727 gfc_get_array_type_bounds (eltype, info->dimen, 0, loop->from, loop->to, 1,
10174ddf 728 GFC_ARRAY_UNKNOWN, true);
6de9cd9a
DN
729 desc = gfc_create_var (type, "atmp");
730 GFC_DECL_PACKED_ARRAY (desc) = 1;
731
732 info->descriptor = desc;
7ab92584 733 size = gfc_index_one_node;
6de9cd9a
DN
734
735 /* Fill in the array dtype. */
736 tmp = gfc_conv_descriptor_dtype (desc);
726a989a 737 gfc_add_modify (pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
6de9cd9a 738
7ab92584
SB
739 /*
740 Fill in the bounds and stride. This is a packed array, so:
741
6de9cd9a
DN
742 size = 1;
743 for (n = 0; n < rank; n++)
7ab92584
SB
744 {
745 stride[n] = size
746 delta = ubound[n] + 1 - lbound[n];
12f681a0 747 size = size * delta;
7ab92584
SB
748 }
749 size = size * sizeof(element);
750 */
751
da4340a1
TK
752 or_expr = NULL_TREE;
753
45bc572c
MM
754 /* If there is at least one null loop->to[n], it is a callee allocated
755 array. */
6de9cd9a 756 for (n = 0; n < info->dimen; n++)
45bc572c
MM
757 if (loop->to[n] == NULL_TREE)
758 {
759 size = NULL_TREE;
760 break;
761 }
762
763 for (n = 0; n < info->dimen; n++)
764 {
765 if (size == NULL_TREE)
12f681a0 766 {
fc90a8f2
PB
767 /* For a callee allocated array express the loop bounds in terms
768 of the descriptor fields. */
12f681a0 769 tmp =
44855d8c 770 fold_build2 (MINUS_EXPR, gfc_array_index_type,
568e8e1e
PT
771 gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]),
772 gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]));
12f681a0
DK
773 loop->to[n] = tmp;
774 continue;
775 }
776
6de9cd9a 777 /* Store the stride and bound components in the descriptor. */
568e8e1e 778 gfc_conv_descriptor_stride_set (pre, desc, gfc_rank_cst[n], size);
6de9cd9a 779
568e8e1e
PT
780 gfc_conv_descriptor_lbound_set (pre, desc, gfc_rank_cst[n],
781 gfc_index_zero_node);
6de9cd9a 782
568e8e1e 783 gfc_conv_descriptor_ubound_set (pre, desc, gfc_rank_cst[n], loop->to[n]);
6de9cd9a 784
10c7a96f
SB
785 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
786 loop->to[n], gfc_index_one_node);
6de9cd9a 787
999ffb1a
FXC
788 /* Check whether the size for this dimension is negative. */
789 cond = fold_build2 (LE_EXPR, boolean_type_node, tmp,
da4340a1 790 gfc_index_zero_node);
999ffb1a 791 cond = gfc_evaluate_now (cond, pre);
da4340a1 792
999ffb1a
FXC
793 if (n == 0)
794 or_expr = cond;
795 else
796 or_expr = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, or_expr, cond);
da4340a1 797
10c7a96f 798 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
62ab4a54 799 size = gfc_evaluate_now (size, pre);
6de9cd9a
DN
800 }
801
6de9cd9a 802 /* Get the size of the array. */
da4340a1 803
8e119f1b 804 if (size && !callee_alloc)
da4340a1 805 {
999ffb1a
FXC
806 /* If or_expr is true, then the extent in at least one
807 dimension is zero and the size is set to zero. */
808 size = fold_build3 (COND_EXPR, gfc_array_index_type,
809 or_expr, gfc_index_zero_node, size);
da4340a1 810
fcac9229 811 nelem = size;
da4340a1 812 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size,
7c57b2f1
FXC
813 fold_convert (gfc_array_index_type,
814 TYPE_SIZE_UNIT (gfc_get_element_type (type))));
da4340a1 815 }
8e119f1b 816 else
da4340a1
TK
817 {
818 nelem = size;
819 size = NULL_TREE;
820 }
6de9cd9a 821
12f681a0
DK
822 gfc_trans_allocate_array_storage (pre, post, info, size, nelem, initial,
823 dynamic, dealloc);
6de9cd9a
DN
824
825 if (info->dimen > loop->temp_dim)
826 loop->temp_dim = info->dimen;
827
828 return size;
829}
830
831
8a6c4339 832/* Generate code to transpose array EXPR by creating a new descriptor
1524f80b
RS
833 in which the dimension specifications have been reversed. */
834
835void
836gfc_conv_array_transpose (gfc_se * se, gfc_expr * expr)
837{
838 tree dest, src, dest_index, src_index;
839 gfc_loopinfo *loop;
c4fae39e 840 gfc_ss_info *dest_info;
1524f80b
RS
841 gfc_ss *dest_ss, *src_ss;
842 gfc_se src_se;
843 int n;
844
845 loop = se->loop;
846
847 src_ss = gfc_walk_expr (expr);
848 dest_ss = se->ss;
849
1524f80b 850 dest_info = &dest_ss->data.info;
c69a7fb7 851 gcc_assert (dest_info->dimen == 2);
1524f80b
RS
852
853 /* Get a descriptor for EXPR. */
854 gfc_init_se (&src_se, NULL);
855 gfc_conv_expr_descriptor (&src_se, expr, src_ss);
856 gfc_add_block_to_block (&se->pre, &src_se.pre);
857 gfc_add_block_to_block (&se->post, &src_se.post);
858 src = src_se.expr;
859
860 /* Allocate a new descriptor for the return value. */
861 dest = gfc_create_var (TREE_TYPE (src), "atmp");
862 dest_info->descriptor = dest;
863 se->expr = dest;
864
865 /* Copy across the dtype field. */
726a989a 866 gfc_add_modify (&se->pre,
1524f80b
RS
867 gfc_conv_descriptor_dtype (dest),
868 gfc_conv_descriptor_dtype (src));
869
870 /* Copy the dimension information, renumbering dimension 1 to 0 and
871 0 to 1. */
1524f80b
RS
872 for (n = 0; n < 2; n++)
873 {
37da9343
RS
874 dest_info->delta[n] = gfc_index_zero_node;
875 dest_info->start[n] = gfc_index_zero_node;
8424e0d8 876 dest_info->end[n] = gfc_index_zero_node;
37da9343 877 dest_info->stride[n] = gfc_index_one_node;
1524f80b
RS
878 dest_info->dim[n] = n;
879
880 dest_index = gfc_rank_cst[n];
881 src_index = gfc_rank_cst[1 - n];
882
568e8e1e
PT
883 gfc_conv_descriptor_stride_set (&se->pre, dest, dest_index,
884 gfc_conv_descriptor_stride_get (src, src_index));
1524f80b 885
568e8e1e
PT
886 gfc_conv_descriptor_lbound_set (&se->pre, dest, dest_index,
887 gfc_conv_descriptor_lbound_get (src, src_index));
1524f80b 888
568e8e1e
PT
889 gfc_conv_descriptor_ubound_set (&se->pre, dest, dest_index,
890 gfc_conv_descriptor_ubound_get (src, src_index));
1524f80b
RS
891
892 if (!loop->to[n])
893 {
894 gcc_assert (integer_zerop (loop->from[n]));
44855d8c
TS
895 loop->to[n] =
896 fold_build2 (MINUS_EXPR, gfc_array_index_type,
568e8e1e
PT
897 gfc_conv_descriptor_ubound_get (dest, dest_index),
898 gfc_conv_descriptor_lbound_get (dest, dest_index));
1524f80b
RS
899 }
900 }
901
902 /* Copy the data pointer. */
903 dest_info->data = gfc_conv_descriptor_data_get (src);
904 gfc_conv_descriptor_data_set (&se->pre, dest, dest_info->data);
905
1229ece0
PT
906 /* Copy the offset. This is not changed by transposition; the top-left
907 element is still at the same offset as before, except where the loop
908 starts at zero. */
909 if (!integer_zerop (loop->from[0]))
568e8e1e 910 dest_info->offset = gfc_conv_descriptor_offset_get (src);
1229ece0
PT
911 else
912 dest_info->offset = gfc_index_zero_node;
913
568e8e1e
PT
914 gfc_conv_descriptor_offset_set (&se->pre, dest,
915 dest_info->offset);
1229ece0 916
1524f80b
RS
917 if (dest_info->dimen > loop->temp_dim)
918 loop->temp_dim = dest_info->dimen;
919}
920
921
ec25720b
RS
922/* Return the number of iterations in a loop that starts at START,
923 ends at END, and has step STEP. */
924
925static tree
926gfc_get_iteration_count (tree start, tree end, tree step)
927{
928 tree tmp;
929 tree type;
930
931 type = TREE_TYPE (step);
932 tmp = fold_build2 (MINUS_EXPR, type, end, start);
933 tmp = fold_build2 (FLOOR_DIV_EXPR, type, tmp, step);
934 tmp = fold_build2 (PLUS_EXPR, type, tmp, build_int_cst (type, 1));
935 tmp = fold_build2 (MAX_EXPR, type, tmp, build_int_cst (type, 0));
936 return fold_convert (gfc_array_index_type, tmp);
937}
938
939
940/* Extend the data in array DESC by EXTRA elements. */
941
942static void
943gfc_grow_array (stmtblock_t * pblock, tree desc, tree extra)
944{
5039610b 945 tree arg0, arg1;
ec25720b
RS
946 tree tmp;
947 tree size;
948 tree ubound;
949
950 if (integer_zerop (extra))
951 return;
952
568e8e1e 953 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[0]);
ec25720b
RS
954
955 /* Add EXTRA to the upper bound. */
44855d8c 956 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, ubound, extra);
568e8e1e 957 gfc_conv_descriptor_ubound_set (pblock, desc, gfc_rank_cst[0], tmp);
ec25720b
RS
958
959 /* Get the value of the current data pointer. */
5039610b 960 arg0 = gfc_conv_descriptor_data_get (desc);
ec25720b
RS
961
962 /* Calculate the new array size. */
963 size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
44855d8c
TS
964 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
965 ubound, gfc_index_one_node);
966 arg1 = fold_build2 (MULT_EXPR, size_type_node,
967 fold_convert (size_type_node, tmp),
968 fold_convert (size_type_node, size));
ec25720b 969
4376b7cf
FXC
970 /* Call the realloc() function. */
971 tmp = gfc_call_realloc (pblock, arg0, arg1);
ec25720b
RS
972 gfc_conv_descriptor_data_set (pblock, desc, tmp);
973}
974
975
976/* Return true if the bounds of iterator I can only be determined
977 at run time. */
978
979static inline bool
980gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
981{
982 return (i->start->expr_type != EXPR_CONSTANT
983 || i->end->expr_type != EXPR_CONSTANT
984 || i->step->expr_type != EXPR_CONSTANT);
985}
986
987
988/* Split the size of constructor element EXPR into the sum of two terms,
989 one of which can be determined at compile time and one of which must
990 be calculated at run time. Set *SIZE to the former and return true
991 if the latter might be nonzero. */
992
993static bool
994gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
995{
996 if (expr->expr_type == EXPR_ARRAY)
997 return gfc_get_array_constructor_size (size, expr->value.constructor);
998 else if (expr->rank > 0)
999 {
1000 /* Calculate everything at run time. */
1001 mpz_set_ui (*size, 0);
1002 return true;
1003 }
1004 else
1005 {
1006 /* A single element. */
1007 mpz_set_ui (*size, 1);
1008 return false;
1009 }
1010}
1011
1012
1013/* Like gfc_get_array_constructor_element_size, but applied to the whole
1014 of array constructor C. */
1015
1016static bool
b7e75771 1017gfc_get_array_constructor_size (mpz_t * size, gfc_constructor_base base)
ec25720b 1018{
b7e75771 1019 gfc_constructor *c;
ec25720b
RS
1020 gfc_iterator *i;
1021 mpz_t val;
1022 mpz_t len;
1023 bool dynamic;
1024
1025 mpz_set_ui (*size, 0);
1026 mpz_init (len);
1027 mpz_init (val);
1028
1029 dynamic = false;
b7e75771 1030 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
ec25720b
RS
1031 {
1032 i = c->iterator;
1033 if (i && gfc_iterator_has_dynamic_bounds (i))
1034 dynamic = true;
1035 else
1036 {
1037 dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
1038 if (i)
1039 {
1040 /* Multiply the static part of the element size by the
1041 number of iterations. */
1042 mpz_sub (val, i->end->value.integer, i->start->value.integer);
1043 mpz_fdiv_q (val, val, i->step->value.integer);
1044 mpz_add_ui (val, val, 1);
1045 if (mpz_sgn (val) > 0)
1046 mpz_mul (len, len, val);
1047 else
1048 mpz_set_ui (len, 0);
1049 }
1050 mpz_add (*size, *size, len);
1051 }
1052 }
1053 mpz_clear (len);
1054 mpz_clear (val);
1055 return dynamic;
1056}
1057
1058
6de9cd9a
DN
1059/* Make sure offset is a variable. */
1060
1061static void
1062gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
1063 tree * offsetvar)
1064{
1065 /* We should have already created the offset variable. We cannot
13413760 1066 create it here because we may be in an inner scope. */
6e45f57b 1067 gcc_assert (*offsetvar != NULL_TREE);
726a989a 1068 gfc_add_modify (pblock, *offsetvar, *poffset);
6de9cd9a
DN
1069 *poffset = *offsetvar;
1070 TREE_USED (*offsetvar) = 1;
1071}
1072
1073
c03fc95d 1074/* Variables needed for bounds-checking. */
32be9f94
PT
1075static bool first_len;
1076static tree first_len_val;
c03fc95d 1077static bool typespec_chararray_ctor;
40f20186
PB
1078
1079static void
ec25720b 1080gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
40f20186
PB
1081 tree offset, gfc_se * se, gfc_expr * expr)
1082{
1083 tree tmp;
40f20186
PB
1084
1085 gfc_conv_expr (se, expr);
1086
1087 /* Store the value. */
db3927fb
AH
1088 tmp = build_fold_indirect_ref_loc (input_location,
1089 gfc_conv_descriptor_data_get (desc));
1d6b7f39 1090 tmp = gfc_build_array_ref (tmp, offset, NULL);
32be9f94 1091
40f20186
PB
1092 if (expr->ts.type == BT_CHARACTER)
1093 {
691da334
FXC
1094 int i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
1095 tree esize;
1096
1097 esize = size_in_bytes (gfc_get_element_type (TREE_TYPE (desc)));
1098 esize = fold_convert (gfc_charlen_type_node, esize);
1099 esize = fold_build2 (TRUNC_DIV_EXPR, gfc_charlen_type_node, esize,
1100 build_int_cst (gfc_charlen_type_node,
1101 gfc_character_kinds[i].bit_size / 8));
1102
40f20186
PB
1103 gfc_conv_string_parameter (se);
1104 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
1105 {
1106 /* The temporary is an array of pointers. */
1107 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
726a989a 1108 gfc_add_modify (&se->pre, tmp, se->expr);
40f20186
PB
1109 }
1110 else
1111 {
1112 /* The temporary is an array of string values. */
d393bbd7 1113 tmp = gfc_build_addr_expr (gfc_get_pchar_type (expr->ts.kind), tmp);
40f20186
PB
1114 /* We know the temporary and the value will be the same length,
1115 so can use memcpy. */
d393bbd7
FXC
1116 gfc_trans_string_copy (&se->pre, esize, tmp, expr->ts.kind,
1117 se->string_length, se->expr, expr->ts.kind);
32be9f94 1118 }
d3d3011f 1119 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !typespec_chararray_ctor)
32be9f94
PT
1120 {
1121 if (first_len)
1122 {
726a989a 1123 gfc_add_modify (&se->pre, first_len_val,
32be9f94
PT
1124 se->string_length);
1125 first_len = false;
1126 }
1127 else
1128 {
1129 /* Verify that all constructor elements are of the same
1130 length. */
1131 tree cond = fold_build2 (NE_EXPR, boolean_type_node,
1132 first_len_val, se->string_length);
1133 gfc_trans_runtime_check
0d52899f 1134 (true, false, cond, &se->pre, &expr->where,
32be9f94
PT
1135 "Different CHARACTER lengths (%ld/%ld) in array constructor",
1136 fold_convert (long_integer_type_node, first_len_val),
1137 fold_convert (long_integer_type_node, se->string_length));
1138 }
40f20186
PB
1139 }
1140 }
1141 else
1142 {
1143 /* TODO: Should the frontend already have done this conversion? */
1144 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
726a989a 1145 gfc_add_modify (&se->pre, tmp, se->expr);
40f20186
PB
1146 }
1147
1148 gfc_add_block_to_block (pblock, &se->pre);
1149 gfc_add_block_to_block (pblock, &se->post);
1150}
1151
1152
ec25720b
RS
1153/* Add the contents of an array to the constructor. DYNAMIC is as for
1154 gfc_trans_array_constructor_value. */
6de9cd9a
DN
1155
1156static void
1157gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
1158 tree type ATTRIBUTE_UNUSED,
ec25720b
RS
1159 tree desc, gfc_expr * expr,
1160 tree * poffset, tree * offsetvar,
1161 bool dynamic)
6de9cd9a
DN
1162{
1163 gfc_se se;
1164 gfc_ss *ss;
1165 gfc_loopinfo loop;
1166 stmtblock_t body;
1167 tree tmp;
ec25720b
RS
1168 tree size;
1169 int n;
6de9cd9a
DN
1170
1171 /* We need this to be a variable so we can increment it. */
1172 gfc_put_offset_into_var (pblock, poffset, offsetvar);
1173
1174 gfc_init_se (&se, NULL);
1175
1176 /* Walk the array expression. */
1177 ss = gfc_walk_expr (expr);
6e45f57b 1178 gcc_assert (ss != gfc_ss_terminator);
6de9cd9a
DN
1179
1180 /* Initialize the scalarizer. */
1181 gfc_init_loopinfo (&loop);
1182 gfc_add_ss_to_loop (&loop, ss);
1183
1184 /* Initialize the loop. */
1185 gfc_conv_ss_startstride (&loop);
bdfd2ff0 1186 gfc_conv_loop_setup (&loop, &expr->where);
6de9cd9a 1187
ec25720b
RS
1188 /* Make sure the constructed array has room for the new data. */
1189 if (dynamic)
1190 {
1191 /* Set SIZE to the total number of elements in the subarray. */
1192 size = gfc_index_one_node;
1193 for (n = 0; n < loop.dimen; n++)
1194 {
1195 tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
1196 gfc_index_one_node);
1197 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
1198 }
1199
1200 /* Grow the constructed array by SIZE elements. */
1201 gfc_grow_array (&loop.pre, desc, size);
1202 }
1203
6de9cd9a
DN
1204 /* Make the loop body. */
1205 gfc_mark_ss_chain_used (ss, 1);
1206 gfc_start_scalarized_body (&loop, &body);
1207 gfc_copy_loopinfo_to_se (&se, &loop);
1208 se.ss = ss;
1209
ec25720b 1210 gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
6e45f57b 1211 gcc_assert (se.ss == gfc_ss_terminator);
6de9cd9a
DN
1212
1213 /* Increment the offset. */
44855d8c
TS
1214 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1215 *poffset, gfc_index_one_node);
726a989a 1216 gfc_add_modify (&body, *poffset, tmp);
6de9cd9a
DN
1217
1218 /* Finish the loop. */
6de9cd9a
DN
1219 gfc_trans_scalarizing_loops (&loop, &body);
1220 gfc_add_block_to_block (&loop.pre, &loop.post);
1221 tmp = gfc_finish_block (&loop.pre);
1222 gfc_add_expr_to_block (pblock, tmp);
1223
1224 gfc_cleanup_loop (&loop);
1225}
1226
1227
ec25720b
RS
1228/* Assign the values to the elements of an array constructor. DYNAMIC
1229 is true if descriptor DESC only contains enough data for the static
1230 size calculated by gfc_get_array_constructor_size. When true, memory
1231 for the dynamic parts must be allocated using realloc. */
6de9cd9a
DN
1232
1233static void
1234gfc_trans_array_constructor_value (stmtblock_t * pblock, tree type,
b7e75771 1235 tree desc, gfc_constructor_base base,
ec25720b
RS
1236 tree * poffset, tree * offsetvar,
1237 bool dynamic)
6de9cd9a
DN
1238{
1239 tree tmp;
6de9cd9a 1240 stmtblock_t body;
6de9cd9a 1241 gfc_se se;
ec25720b 1242 mpz_t size;
b7e75771 1243 gfc_constructor *c;
6de9cd9a 1244
beb64b4a
DF
1245 tree shadow_loopvar = NULL_TREE;
1246 gfc_saved_var saved_loopvar;
1247
ec25720b 1248 mpz_init (size);
b7e75771 1249 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
6de9cd9a
DN
1250 {
1251 /* If this is an iterator or an array, the offset must be a variable. */
1252 if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
1253 gfc_put_offset_into_var (pblock, poffset, offsetvar);
1254
beb64b4a
DF
1255 /* Shadowing the iterator avoids changing its value and saves us from
1256 keeping track of it. Further, it makes sure that there's always a
1257 backend-decl for the symbol, even if there wasn't one before,
1258 e.g. in the case of an iterator that appears in a specification
1259 expression in an interface mapping. */
1260 if (c->iterator)
1261 {
1262 gfc_symbol *sym = c->iterator->var->symtree->n.sym;
1263 tree type = gfc_typenode_for_spec (&sym->ts);
1264
1265 shadow_loopvar = gfc_create_var (type, "shadow_loopvar");
1266 gfc_shadow_sym (sym, shadow_loopvar, &saved_loopvar);
1267 }
1268
6de9cd9a
DN
1269 gfc_start_block (&body);
1270
1271 if (c->expr->expr_type == EXPR_ARRAY)
1272 {
1273 /* Array constructors can be nested. */
ec25720b 1274 gfc_trans_array_constructor_value (&body, type, desc,
6de9cd9a 1275 c->expr->value.constructor,
ec25720b 1276 poffset, offsetvar, dynamic);
6de9cd9a
DN
1277 }
1278 else if (c->expr->rank > 0)
1279 {
ec25720b
RS
1280 gfc_trans_array_constructor_subarray (&body, type, desc, c->expr,
1281 poffset, offsetvar, dynamic);
6de9cd9a
DN
1282 }
1283 else
1284 {
1285 /* This code really upsets the gimplifier so don't bother for now. */
1286 gfc_constructor *p;
1287 HOST_WIDE_INT n;
1288 HOST_WIDE_INT size;
1289
1290 p = c;
1291 n = 0;
1292 while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
1293 {
b7e75771 1294 p = gfc_constructor_next (p);
6de9cd9a
DN
1295 n++;
1296 }
1297 if (n < 4)
1298 {
1299 /* Scalar values. */
1300 gfc_init_se (&se, NULL);
ec25720b
RS
1301 gfc_trans_array_ctor_element (&body, desc, *poffset,
1302 &se, c->expr);
6de9cd9a 1303
10c7a96f
SB
1304 *poffset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1305 *poffset, gfc_index_one_node);
6de9cd9a
DN
1306 }
1307 else
1308 {
1309 /* Collect multiple scalar constants into a constructor. */
8748ad99 1310 VEC(constructor_elt,gc) *v = NULL;
6de9cd9a
DN
1311 tree init;
1312 tree bound;
1313 tree tmptype;
81f5094d 1314 HOST_WIDE_INT idx = 0;
6de9cd9a
DN
1315
1316 p = c;
6de9cd9a
DN
1317 /* Count the number of consecutive scalar constants. */
1318 while (p && !(p->iterator
1319 || p->expr->expr_type != EXPR_CONSTANT))
1320 {
1321 gfc_init_se (&se, NULL);
1322 gfc_conv_constant (&se, p->expr);
d393bbd7 1323
110ea21a
PT
1324 if (c->expr->ts.type != BT_CHARACTER)
1325 se.expr = fold_convert (type, se.expr);
d393bbd7
FXC
1326 /* For constant character array constructors we build
1327 an array of pointers. */
110ea21a 1328 else if (POINTER_TYPE_P (type))
d393bbd7
FXC
1329 se.expr = gfc_build_addr_expr
1330 (gfc_get_pchar_type (p->expr->ts.kind),
1331 se.expr);
1332
8748ad99
NF
1333 CONSTRUCTOR_APPEND_ELT (v,
1334 build_int_cst (gfc_array_index_type,
1335 idx++),
1336 se.expr);
6de9cd9a 1337 c = p;
b7e75771 1338 p = gfc_constructor_next (p);
6de9cd9a
DN
1339 }
1340
7d60be94 1341 bound = build_int_cst (NULL_TREE, n - 1);
6de9cd9a
DN
1342 /* Create an array type to hold them. */
1343 tmptype = build_range_type (gfc_array_index_type,
7ab92584 1344 gfc_index_zero_node, bound);
6de9cd9a
DN
1345 tmptype = build_array_type (type, tmptype);
1346
8748ad99 1347 init = build_constructor (tmptype, v);
6de9cd9a 1348 TREE_CONSTANT (init) = 1;
6de9cd9a
DN
1349 TREE_STATIC (init) = 1;
1350 /* Create a static variable to hold the data. */
1351 tmp = gfc_create_var (tmptype, "data");
1352 TREE_STATIC (tmp) = 1;
1353 TREE_CONSTANT (tmp) = 1;
0f0707d1 1354 TREE_READONLY (tmp) = 1;
6de9cd9a
DN
1355 DECL_INITIAL (tmp) = init;
1356 init = tmp;
1357
1358 /* Use BUILTIN_MEMCPY to assign the values. */
ec25720b 1359 tmp = gfc_conv_descriptor_data_get (desc);
db3927fb
AH
1360 tmp = build_fold_indirect_ref_loc (input_location,
1361 tmp);
1d6b7f39 1362 tmp = gfc_build_array_ref (tmp, *poffset, NULL);
628c189e
RG
1363 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1364 init = gfc_build_addr_expr (NULL_TREE, init);
6de9cd9a
DN
1365
1366 size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
7d60be94 1367 bound = build_int_cst (NULL_TREE, n * size);
db3927fb
AH
1368 tmp = build_call_expr_loc (input_location,
1369 built_in_decls[BUILT_IN_MEMCPY], 3,
5039610b 1370 tmp, init, bound);
6de9cd9a
DN
1371 gfc_add_expr_to_block (&body, tmp);
1372
10c7a96f 1373 *poffset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
ac816b02
RG
1374 *poffset,
1375 build_int_cst (gfc_array_index_type, n));
6de9cd9a
DN
1376 }
1377 if (!INTEGER_CST_P (*poffset))
1378 {
726a989a 1379 gfc_add_modify (&body, *offsetvar, *poffset);
6de9cd9a
DN
1380 *poffset = *offsetvar;
1381 }
1382 }
1383
63346ddb 1384 /* The frontend should already have done any expansions
86403f0f
TS
1385 at compile-time. */
1386 if (!c->iterator)
6de9cd9a 1387 {
86403f0f
TS
1388 /* Pass the code as is. */
1389 tmp = gfc_finish_block (&body);
1390 gfc_add_expr_to_block (pblock, tmp);
1391 }
1392 else
1393 {
1394 /* Build the implied do-loop. */
beb64b4a 1395 stmtblock_t implied_do_block;
86403f0f 1396 tree cond;
6de9cd9a
DN
1397 tree end;
1398 tree step;
6de9cd9a 1399 tree exit_label;
86403f0f 1400 tree loopbody;
ec25720b 1401 tree tmp2;
6de9cd9a
DN
1402
1403 loopbody = gfc_finish_block (&body);
1404
beb64b4a
DF
1405 /* Create a new block that holds the implied-do loop. A temporary
1406 loop-variable is used. */
1407 gfc_start_block(&implied_do_block);
bfa7a1e9 1408
13413760 1409 /* Initialize the loop. */
6de9cd9a
DN
1410 gfc_init_se (&se, NULL);
1411 gfc_conv_expr_val (&se, c->iterator->start);
beb64b4a
DF
1412 gfc_add_block_to_block (&implied_do_block, &se.pre);
1413 gfc_add_modify (&implied_do_block, shadow_loopvar, se.expr);
6de9cd9a
DN
1414
1415 gfc_init_se (&se, NULL);
1416 gfc_conv_expr_val (&se, c->iterator->end);
beb64b4a
DF
1417 gfc_add_block_to_block (&implied_do_block, &se.pre);
1418 end = gfc_evaluate_now (se.expr, &implied_do_block);
6de9cd9a
DN
1419
1420 gfc_init_se (&se, NULL);
1421 gfc_conv_expr_val (&se, c->iterator->step);
beb64b4a
DF
1422 gfc_add_block_to_block (&implied_do_block, &se.pre);
1423 step = gfc_evaluate_now (se.expr, &implied_do_block);
6de9cd9a 1424
ec25720b
RS
1425 /* If this array expands dynamically, and the number of iterations
1426 is not constant, we won't have allocated space for the static
1427 part of C->EXPR's size. Do that now. */
1428 if (dynamic && gfc_iterator_has_dynamic_bounds (c->iterator))
1429 {
1430 /* Get the number of iterations. */
beb64b4a 1431 tmp = gfc_get_iteration_count (shadow_loopvar, end, step);
ec25720b
RS
1432
1433 /* Get the static part of C->EXPR's size. */
1434 gfc_get_array_constructor_element_size (&size, c->expr);
1435 tmp2 = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
1436
1437 /* Grow the array by TMP * TMP2 elements. */
1438 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, tmp, tmp2);
beb64b4a 1439 gfc_grow_array (&implied_do_block, desc, tmp);
ec25720b
RS
1440 }
1441
6de9cd9a
DN
1442 /* Generate the loop body. */
1443 exit_label = gfc_build_label_decl (NULL_TREE);
1444 gfc_start_block (&body);
1445
86403f0f
TS
1446 /* Generate the exit condition. Depending on the sign of
1447 the step variable we have to generate the correct
1448 comparison. */
1449 tmp = fold_build2 (GT_EXPR, boolean_type_node, step,
1450 build_int_cst (TREE_TYPE (step), 0));
1451 cond = fold_build3 (COND_EXPR, boolean_type_node, tmp,
44855d8c 1452 fold_build2 (GT_EXPR, boolean_type_node,
beb64b4a 1453 shadow_loopvar, end),
44855d8c 1454 fold_build2 (LT_EXPR, boolean_type_node,
beb64b4a 1455 shadow_loopvar, end));
6de9cd9a
DN
1456 tmp = build1_v (GOTO_EXPR, exit_label);
1457 TREE_USED (exit_label) = 1;
c2255bc4
AH
1458 tmp = build3_v (COND_EXPR, cond, tmp,
1459 build_empty_stmt (input_location));
6de9cd9a
DN
1460 gfc_add_expr_to_block (&body, tmp);
1461
1462 /* The main loop body. */
1463 gfc_add_expr_to_block (&body, loopbody);
1464
86403f0f 1465 /* Increase loop variable by step. */
beb64b4a
DF
1466 tmp = fold_build2 (PLUS_EXPR, TREE_TYPE (shadow_loopvar), shadow_loopvar, step);
1467 gfc_add_modify (&body, shadow_loopvar, tmp);
6de9cd9a
DN
1468
1469 /* Finish the loop. */
1470 tmp = gfc_finish_block (&body);
923ab88c 1471 tmp = build1_v (LOOP_EXPR, tmp);
beb64b4a 1472 gfc_add_expr_to_block (&implied_do_block, tmp);
6de9cd9a
DN
1473
1474 /* Add the exit label. */
1475 tmp = build1_v (LABEL_EXPR, exit_label);
beb64b4a
DF
1476 gfc_add_expr_to_block (&implied_do_block, tmp);
1477
1478 /* Finishe the implied-do loop. */
1479 tmp = gfc_finish_block(&implied_do_block);
1480 gfc_add_expr_to_block(pblock, tmp);
bfa7a1e9 1481
beb64b4a 1482 gfc_restore_sym (c->iterator->var->symtree->n.sym, &saved_loopvar);
6de9cd9a 1483 }
6de9cd9a 1484 }
ec25720b 1485 mpz_clear (size);
6de9cd9a
DN
1486}
1487
1488
40f20186
PB
1489/* Figure out the string length of a variable reference expression.
1490 Used by get_array_ctor_strlen. */
1491
1492static void
1493get_array_ctor_var_strlen (gfc_expr * expr, tree * len)
1494{
1495 gfc_ref *ref;
1496 gfc_typespec *ts;
1855915a 1497 mpz_t char_len;
40f20186
PB
1498
1499 /* Don't bother if we already know the length is a constant. */
1500 if (*len && INTEGER_CST_P (*len))
1501 return;
1502
1503 ts = &expr->symtree->n.sym->ts;
1504 for (ref = expr->ref; ref; ref = ref->next)
1505 {
1506 switch (ref->type)
1507 {
1508 case REF_ARRAY:
df7df328 1509 /* Array references don't change the string length. */
40f20186
PB
1510 break;
1511
0e3e65bc 1512 case REF_COMPONENT:
f7b529fa 1513 /* Use the length of the component. */
40f20186
PB
1514 ts = &ref->u.c.component->ts;
1515 break;
1516
1855915a
PT
1517 case REF_SUBSTRING:
1518 if (ref->u.ss.start->expr_type != EXPR_CONSTANT
08ddab21 1519 || ref->u.ss.end->expr_type != EXPR_CONSTANT)
1855915a
PT
1520 break;
1521 mpz_init_set_ui (char_len, 1);
1522 mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
1523 mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
d393bbd7 1524 *len = gfc_conv_mpz_to_tree (char_len, gfc_default_integer_kind);
1855915a
PT
1525 *len = convert (gfc_charlen_type_node, *len);
1526 mpz_clear (char_len);
1527 return;
1528
40f20186
PB
1529 default:
1530 /* TODO: Substrings are tricky because we can't evaluate the
1531 expression more than once. For now we just give up, and hope
1532 we can figure it out elsewhere. */
1533 return;
1534 }
1535 }
1536
bc21d315 1537 *len = ts->u.cl->backend_decl;
40f20186
PB
1538}
1539
1540
0ee8e250
PT
1541/* A catch-all to obtain the string length for anything that is not a
1542 constant, array or variable. */
1543static void
1544get_array_ctor_all_strlen (stmtblock_t *block, gfc_expr *e, tree *len)
1545{
1546 gfc_se se;
1547 gfc_ss *ss;
1548
1549 /* Don't bother if we already know the length is a constant. */
1550 if (*len && INTEGER_CST_P (*len))
1551 return;
1552
bc21d315
JW
1553 if (!e->ref && e->ts.u.cl && e->ts.u.cl->length
1554 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
0ee8e250
PT
1555 {
1556 /* This is easy. */
bc21d315
JW
1557 gfc_conv_const_charlen (e->ts.u.cl);
1558 *len = e->ts.u.cl->backend_decl;
0ee8e250
PT
1559 }
1560 else
1561 {
1562 /* Otherwise, be brutal even if inefficient. */
1563 ss = gfc_walk_expr (e);
1564 gfc_init_se (&se, NULL);
1565
1566 /* No function call, in case of side effects. */
1567 se.no_function_call = 1;
1568 if (ss == gfc_ss_terminator)
1569 gfc_conv_expr (&se, e);
1570 else
1571 gfc_conv_expr_descriptor (&se, e, ss);
1572
1573 /* Fix the value. */
1574 *len = gfc_evaluate_now (se.string_length, &se.pre);
1575
1576 gfc_add_block_to_block (block, &se.pre);
1577 gfc_add_block_to_block (block, &se.post);
1578
bc21d315 1579 e->ts.u.cl->backend_decl = *len;
0ee8e250
PT
1580 }
1581}
1582
1583
40f20186 1584/* Figure out the string length of a character array constructor.
88fec49f
DK
1585 If len is NULL, don't calculate the length; this happens for recursive calls
1586 when a sub-array-constructor is an element but not at the first position,
1587 so when we're not interested in the length.
40f20186
PB
1588 Returns TRUE if all elements are character constants. */
1589
636da744 1590bool
b7e75771 1591get_array_ctor_strlen (stmtblock_t *block, gfc_constructor_base base, tree * len)
40f20186 1592{
b7e75771 1593 gfc_constructor *c;
40f20186 1594 bool is_const;
b7e75771 1595
40f20186 1596 is_const = TRUE;
58fbb917 1597
b7e75771 1598 if (gfc_constructor_first (base) == NULL)
58fbb917 1599 {
88fec49f
DK
1600 if (len)
1601 *len = build_int_cstu (gfc_charlen_type_node, 0);
58fbb917
PT
1602 return is_const;
1603 }
1604
88fec49f
DK
1605 /* Loop over all constructor elements to find out is_const, but in len we
1606 want to store the length of the first, not the last, element. We can
1607 of course exit the loop as soon as is_const is found to be false. */
b7e75771
JD
1608 for (c = gfc_constructor_first (base);
1609 c && is_const; c = gfc_constructor_next (c))
40f20186
PB
1610 {
1611 switch (c->expr->expr_type)
1612 {
1613 case EXPR_CONSTANT:
88fec49f 1614 if (len && !(*len && INTEGER_CST_P (*len)))
d7177ab2 1615 *len = build_int_cstu (gfc_charlen_type_node,
40f20186
PB
1616 c->expr->value.character.length);
1617 break;
1618
1619 case EXPR_ARRAY:
0ee8e250 1620 if (!get_array_ctor_strlen (block, c->expr->value.constructor, len))
01201992 1621 is_const = false;
40f20186
PB
1622 break;
1623
1624 case EXPR_VARIABLE:
1625 is_const = false;
88fec49f
DK
1626 if (len)
1627 get_array_ctor_var_strlen (c->expr, len);
40f20186
PB
1628 break;
1629
1630 default:
01201992 1631 is_const = false;
88fec49f
DK
1632 if (len)
1633 get_array_ctor_all_strlen (block, c->expr, len);
40f20186
PB
1634 break;
1635 }
88fec49f
DK
1636
1637 /* After the first iteration, we don't want the length modified. */
1638 len = NULL;
40f20186
PB
1639 }
1640
1641 return is_const;
1642}
1643
62511fb1
RS
1644/* Check whether the array constructor C consists entirely of constant
1645 elements, and if so returns the number of those elements, otherwise
1646 return zero. Note, an empty or NULL array constructor returns zero. */
1647
b01e2f88 1648unsigned HOST_WIDE_INT
b7e75771 1649gfc_constant_array_constructor_p (gfc_constructor_base base)
62511fb1
RS
1650{
1651 unsigned HOST_WIDE_INT nelem = 0;
1652
b7e75771 1653 gfc_constructor *c = gfc_constructor_first (base);
62511fb1
RS
1654 while (c)
1655 {
1656 if (c->iterator
1657 || c->expr->rank > 0
1658 || c->expr->expr_type != EXPR_CONSTANT)
1659 return 0;
b7e75771 1660 c = gfc_constructor_next (c);
62511fb1
RS
1661 nelem++;
1662 }
1663 return nelem;
1664}
1665
1666
1667/* Given EXPR, the constant array constructor specified by an EXPR_ARRAY,
1668 and the tree type of it's elements, TYPE, return a static constant
1669 variable that is compile-time initialized. */
1670
b01e2f88 1671tree
62511fb1
RS
1672gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
1673{
8748ad99 1674 tree tmptype, init, tmp;
62511fb1
RS
1675 HOST_WIDE_INT nelem;
1676 gfc_constructor *c;
1677 gfc_array_spec as;
1678 gfc_se se;
61a04b5b 1679 int i;
8748ad99 1680 VEC(constructor_elt,gc) *v = NULL;
62511fb1
RS
1681
1682 /* First traverse the constructor list, converting the constants
1683 to tree to build an initializer. */
1684 nelem = 0;
b7e75771 1685 c = gfc_constructor_first (expr->value.constructor);
62511fb1
RS
1686 while (c)
1687 {
1688 gfc_init_se (&se, NULL);
1689 gfc_conv_constant (&se, c->expr);
110ea21a
PT
1690 if (c->expr->ts.type != BT_CHARACTER)
1691 se.expr = fold_convert (type, se.expr);
1692 else if (POINTER_TYPE_P (type))
d393bbd7
FXC
1693 se.expr = gfc_build_addr_expr (gfc_get_pchar_type (c->expr->ts.kind),
1694 se.expr);
8748ad99
NF
1695 CONSTRUCTOR_APPEND_ELT (v, build_int_cst (gfc_array_index_type, nelem),
1696 se.expr);
b7e75771 1697 c = gfc_constructor_next (c);
62511fb1
RS
1698 nelem++;
1699 }
1700
65de695f 1701 /* Next determine the tree type for the array. We use the gfortran
62511fb1
RS
1702 front-end's gfc_get_nodesc_array_type in order to create a suitable
1703 GFC_ARRAY_TYPE_P that may be used by the scalarizer. */
1704
1705 memset (&as, 0, sizeof (gfc_array_spec));
1706
61a04b5b 1707 as.rank = expr->rank;
62511fb1 1708 as.type = AS_EXPLICIT;
61a04b5b
RS
1709 if (!expr->shape)
1710 {
b7e75771
JD
1711 as.lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
1712 as.upper[0] = gfc_get_int_expr (gfc_default_integer_kind,
1713 NULL, nelem - 1);
61a04b5b
RS
1714 }
1715 else
1716 for (i = 0; i < expr->rank; i++)
1717 {
1718 int tmp = (int) mpz_get_si (expr->shape[i]);
b7e75771
JD
1719 as.lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
1720 as.upper[i] = gfc_get_int_expr (gfc_default_integer_kind,
1721 NULL, tmp - 1);
61a04b5b
RS
1722 }
1723
10174ddf 1724 tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
62511fb1 1725
8748ad99 1726 init = build_constructor (tmptype, v);
62511fb1
RS
1727
1728 TREE_CONSTANT (init) = 1;
62511fb1
RS
1729 TREE_STATIC (init) = 1;
1730
1731 tmp = gfc_create_var (tmptype, "A");
1732 TREE_STATIC (tmp) = 1;
1733 TREE_CONSTANT (tmp) = 1;
62511fb1
RS
1734 TREE_READONLY (tmp) = 1;
1735 DECL_INITIAL (tmp) = init;
1736
1737 return tmp;
1738}
1739
1740
1741/* Translate a constant EXPR_ARRAY array constructor for the scalarizer.
1742 This mostly initializes the scalarizer state info structure with the
1743 appropriate values to directly use the array created by the function
1744 gfc_build_constant_array_constructor. */
1745
1746static void
1747gfc_trans_constant_array_constructor (gfc_loopinfo * loop,
1748 gfc_ss * ss, tree type)
1749{
1750 gfc_ss_info *info;
1751 tree tmp;
61a04b5b 1752 int i;
62511fb1
RS
1753
1754 tmp = gfc_build_constant_array_constructor (ss->expr, type);
1755
1756 info = &ss->data.info;
1757
1758 info->descriptor = tmp;
628c189e 1759 info->data = gfc_build_addr_expr (NULL_TREE, tmp);
45bc572c 1760 info->offset = gfc_index_zero_node;
62511fb1 1761
61a04b5b
RS
1762 for (i = 0; i < info->dimen; i++)
1763 {
1764 info->delta[i] = gfc_index_zero_node;
1765 info->start[i] = gfc_index_zero_node;
1766 info->end[i] = gfc_index_zero_node;
1767 info->stride[i] = gfc_index_one_node;
1768 info->dim[i] = i;
1769 }
62511fb1
RS
1770
1771 if (info->dimen > loop->temp_dim)
1772 loop->temp_dim = info->dimen;
1773}
1774
61a04b5b
RS
1775/* Helper routine of gfc_trans_array_constructor to determine if the
1776 bounds of the loop specified by LOOP are constant and simple enough
1777 to use with gfc_trans_constant_array_constructor. Returns the
df2fba9e 1778 iteration count of the loop if suitable, and NULL_TREE otherwise. */
61a04b5b
RS
1779
1780static tree
1781constant_array_constructor_loop_size (gfc_loopinfo * loop)
1782{
1783 tree size = gfc_index_one_node;
1784 tree tmp;
1785 int i;
1786
1787 for (i = 0; i < loop->dimen; i++)
1788 {
1789 /* If the bounds aren't constant, return NULL_TREE. */
1790 if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
1791 return NULL_TREE;
1792 if (!integer_zerop (loop->from[i]))
1793 {
86bf520d 1794 /* Only allow nonzero "from" in one-dimensional arrays. */
61a04b5b
RS
1795 if (loop->dimen != 1)
1796 return NULL_TREE;
1797 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1798 loop->to[i], loop->from[i]);
1799 }
1800 else
1801 tmp = loop->to[i];
1802 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1803 tmp, gfc_index_one_node);
1804 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
1805 }
1806
1807 return size;
1808}
1809
40f20186 1810
6de9cd9a
DN
1811/* Array constructors are handled by constructing a temporary, then using that
1812 within the scalarization loop. This is not optimal, but seems by far the
1813 simplest method. */
1814
1815static void
bdfd2ff0 1816gfc_trans_array_constructor (gfc_loopinfo * loop, gfc_ss * ss, locus * where)
6de9cd9a 1817{
b7e75771 1818 gfc_constructor_base c;
6de9cd9a
DN
1819 tree offset;
1820 tree offsetvar;
1821 tree desc;
6de9cd9a 1822 tree type;
ec25720b 1823 bool dynamic;
4b7f8314
DK
1824 bool old_first_len, old_typespec_chararray_ctor;
1825 tree old_first_len_val;
1826
1827 /* Save the old values for nested checking. */
1828 old_first_len = first_len;
1829 old_first_len_val = first_len_val;
1830 old_typespec_chararray_ctor = typespec_chararray_ctor;
6de9cd9a 1831
c03fc95d
DK
1832 /* Do bounds-checking here and in gfc_trans_array_ctor_element only if no
1833 typespec was given for the array constructor. */
bc21d315
JW
1834 typespec_chararray_ctor = (ss->expr->ts.u.cl
1835 && ss->expr->ts.u.cl->length_from_typespec);
c03fc95d 1836
d3d3011f
FXC
1837 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1838 && ss->expr->ts.type == BT_CHARACTER && !typespec_chararray_ctor)
32be9f94
PT
1839 {
1840 first_len_val = gfc_create_var (gfc_charlen_type_node, "len");
1841 first_len = true;
1842 }
1843
6de9cd9a 1844 ss->data.info.dimen = loop->dimen;
40f20186 1845
ec25720b 1846 c = ss->expr->value.constructor;
40f20186
PB
1847 if (ss->expr->ts.type == BT_CHARACTER)
1848 {
c03fc95d
DK
1849 bool const_string;
1850
1851 /* get_array_ctor_strlen walks the elements of the constructor, if a
1852 typespec was given, we already know the string length and want the one
1853 specified there. */
bc21d315
JW
1854 if (typespec_chararray_ctor && ss->expr->ts.u.cl->length
1855 && ss->expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
c03fc95d
DK
1856 {
1857 gfc_se length_se;
1858
1859 const_string = false;
1860 gfc_init_se (&length_se, NULL);
bc21d315 1861 gfc_conv_expr_type (&length_se, ss->expr->ts.u.cl->length,
c03fc95d
DK
1862 gfc_charlen_type_node);
1863 ss->string_length = length_se.expr;
1864 gfc_add_block_to_block (&loop->pre, &length_se.pre);
1865 gfc_add_block_to_block (&loop->post, &length_se.post);
1866 }
1867 else
1868 const_string = get_array_ctor_strlen (&loop->pre, c,
1869 &ss->string_length);
ca39e6f2
FXC
1870
1871 /* Complex character array constructors should have been taken care of
1872 and not end up here. */
1873 gcc_assert (ss->string_length);
40f20186 1874
bc21d315 1875 ss->expr->ts.u.cl->backend_decl = ss->string_length;
0ee8e250 1876
40f20186
PB
1877 type = gfc_get_character_type_len (ss->expr->ts.kind, ss->string_length);
1878 if (const_string)
1879 type = build_pointer_type (type);
1880 }
1881 else
62511fb1 1882 type = gfc_typenode_for_spec (&ss->expr->ts);
40f20186 1883
ec25720b
RS
1884 /* See if the constructor determines the loop bounds. */
1885 dynamic = false;
6a56381b
PT
1886
1887 if (ss->expr->shape && loop->dimen > 1 && loop->to[0] == NULL_TREE)
1888 {
1889 /* We have a multidimensional parameter. */
1890 int n;
1891 for (n = 0; n < ss->expr->rank; n++)
1892 {
1893 loop->from[n] = gfc_index_zero_node;
1894 loop->to[n] = gfc_conv_mpz_to_tree (ss->expr->shape [n],
1895 gfc_index_integer_kind);
1896 loop->to[n] = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1897 loop->to[n], gfc_index_one_node);
1898 }
1899 }
1900
ec25720b
RS
1901 if (loop->to[0] == NULL_TREE)
1902 {
1903 mpz_t size;
1904
1905 /* We should have a 1-dimensional, zero-based loop. */
1906 gcc_assert (loop->dimen == 1);
1907 gcc_assert (integer_zerop (loop->from[0]));
1908
1909 /* Split the constructor size into a static part and a dynamic part.
1910 Allocate the static size up-front and record whether the dynamic
1911 size might be nonzero. */
1912 mpz_init (size);
1913 dynamic = gfc_get_array_constructor_size (&size, c);
1914 mpz_sub_ui (size, size, 1);
1915 loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
1916 mpz_clear (size);
1917 }
1918
62511fb1 1919 /* Special case constant array constructors. */
61a04b5b 1920 if (!dynamic)
62511fb1 1921 {
b01e2f88 1922 unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
62511fb1
RS
1923 if (nelem > 0)
1924 {
61a04b5b
RS
1925 tree size = constant_array_constructor_loop_size (loop);
1926 if (size && compare_tree_int (size, nelem) == 0)
62511fb1
RS
1927 {
1928 gfc_trans_constant_array_constructor (loop, ss, type);
4b7f8314 1929 goto finish;
62511fb1
RS
1930 }
1931 }
1932 }
1933
8e119f1b 1934 gfc_trans_create_temp_array (&loop->pre, &loop->post, loop, &ss->data.info,
12f681a0 1935 type, NULL_TREE, dynamic, true, false, where);
6de9cd9a
DN
1936
1937 desc = ss->data.info.descriptor;
7ab92584 1938 offset = gfc_index_zero_node;
6de9cd9a 1939 offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
01306727 1940 TREE_NO_WARNING (offsetvar) = 1;
6de9cd9a 1941 TREE_USED (offsetvar) = 0;
ec25720b
RS
1942 gfc_trans_array_constructor_value (&loop->pre, type, desc, c,
1943 &offset, &offsetvar, dynamic);
1944
1945 /* If the array grows dynamically, the upper bound of the loop variable
1946 is determined by the array's final upper bound. */
1947 if (dynamic)
568e8e1e 1948 loop->to[0] = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[0]);
6de9cd9a
DN
1949
1950 if (TREE_USED (offsetvar))
1951 pushdecl (offsetvar);
1952 else
6e45f57b 1953 gcc_assert (INTEGER_CST_P (offset));
6de9cd9a 1954#if 0
dfc46c1f 1955 /* Disable bound checking for now because it's probably broken. */
d3d3011f 1956 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
6de9cd9a 1957 {
6e45f57b 1958 gcc_unreachable ();
6de9cd9a
DN
1959 }
1960#endif
4b7f8314
DK
1961
1962finish:
1963 /* Restore old values of globals. */
1964 first_len = old_first_len;
1965 first_len_val = old_first_len_val;
1966 typespec_chararray_ctor = old_typespec_chararray_ctor;
6de9cd9a
DN
1967}
1968
1969
7a70c12d
RS
1970/* INFO describes a GFC_SS_SECTION in loop LOOP, and this function is
1971 called after evaluating all of INFO's vector dimensions. Go through
1972 each such vector dimension and see if we can now fill in any missing
1973 loop bounds. */
1974
1975static void
1976gfc_set_vector_loop_bounds (gfc_loopinfo * loop, gfc_ss_info * info)
1977{
1978 gfc_se se;
1979 tree tmp;
1980 tree desc;
1981 tree zero;
1982 int n;
1983 int dim;
1984
1985 for (n = 0; n < loop->dimen; n++)
1986 {
1987 dim = info->dim[n];
1988 if (info->ref->u.ar.dimen_type[dim] == DIMEN_VECTOR
1989 && loop->to[n] == NULL)
1990 {
1991 /* Loop variable N indexes vector dimension DIM, and we don't
1992 yet know the upper bound of loop variable N. Set it to the
1993 difference between the vector's upper and lower bounds. */
1994 gcc_assert (loop->from[n] == gfc_index_zero_node);
1995 gcc_assert (info->subscript[dim]
1996 && info->subscript[dim]->type == GFC_SS_VECTOR);
1997
1998 gfc_init_se (&se, NULL);
1999 desc = info->subscript[dim]->data.info.descriptor;
2000 zero = gfc_rank_cst[0];
2001 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
568e8e1e
PT
2002 gfc_conv_descriptor_ubound_get (desc, zero),
2003 gfc_conv_descriptor_lbound_get (desc, zero));
7a70c12d
RS
2004 tmp = gfc_evaluate_now (tmp, &loop->pre);
2005 loop->to[n] = tmp;
2006 }
2007 }
2008}
2009
2010
6de9cd9a
DN
2011/* Add the pre and post chains for all the scalar expressions in a SS chain
2012 to loop. This is called after the loop parameters have been calculated,
2013 but before the actual scalarizing loops. */
6de9cd9a
DN
2014
2015static void
bdfd2ff0
TK
2016gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
2017 locus * where)
6de9cd9a
DN
2018{
2019 gfc_se se;
2020 int n;
2021
df2fba9e
RW
2022 /* TODO: This can generate bad code if there are ordering dependencies,
2023 e.g., a callee allocated function and an unknown size constructor. */
6e45f57b 2024 gcc_assert (ss != NULL);
6de9cd9a
DN
2025
2026 for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
2027 {
6e45f57b 2028 gcc_assert (ss);
6de9cd9a
DN
2029
2030 switch (ss->type)
2031 {
2032 case GFC_SS_SCALAR:
2033 /* Scalar expression. Evaluate this now. This includes elemental
2034 dimension indices, but not array section bounds. */
2035 gfc_init_se (&se, NULL);
ae772c2d
PT
2036 gfc_conv_expr (&se, ss->expr);
2037 gfc_add_block_to_block (&loop->pre, &se.pre);
6de9cd9a 2038
ae772c2d
PT
2039 if (ss->expr->ts.type != BT_CHARACTER)
2040 {
2041 /* Move the evaluation of scalar expressions outside the
2042 scalarization loop, except for WHERE assignments. */
2043 if (subscript)
2044 se.expr = convert(gfc_array_index_type, se.expr);
2045 if (!ss->where)
2046 se.expr = gfc_evaluate_now (se.expr, &loop->pre);
2047 gfc_add_block_to_block (&loop->pre, &se.post);
2048 }
2049 else
2050 gfc_add_block_to_block (&loop->post, &se.post);
6de9cd9a
DN
2051
2052 ss->data.scalar.expr = se.expr;
40f20186 2053 ss->string_length = se.string_length;
6de9cd9a
DN
2054 break;
2055
2056 case GFC_SS_REFERENCE:
991b4da1
PT
2057 /* Scalar argument to elemental procedure. Evaluate this
2058 now. */
6de9cd9a 2059 gfc_init_se (&se, NULL);
991b4da1 2060 gfc_conv_expr (&se, ss->expr);
6de9cd9a
DN
2061 gfc_add_block_to_block (&loop->pre, &se.pre);
2062 gfc_add_block_to_block (&loop->post, &se.post);
2063
2064 ss->data.scalar.expr = gfc_evaluate_now (se.expr, &loop->pre);
40f20186 2065 ss->string_length = se.string_length;
6de9cd9a
DN
2066 break;
2067
2068 case GFC_SS_SECTION:
7a70c12d 2069 /* Add the expressions for scalar and vector subscripts. */
6de9cd9a 2070 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
7a70c12d 2071 if (ss->data.info.subscript[n])
bdfd2ff0
TK
2072 gfc_add_loop_ss_code (loop, ss->data.info.subscript[n], true,
2073 where);
7a70c12d
RS
2074
2075 gfc_set_vector_loop_bounds (loop, &ss->data.info);
2076 break;
2077
2078 case GFC_SS_VECTOR:
2079 /* Get the vector's descriptor and store it in SS. */
2080 gfc_init_se (&se, NULL);
2081 gfc_conv_expr_descriptor (&se, ss->expr, gfc_walk_expr (ss->expr));
2082 gfc_add_block_to_block (&loop->pre, &se.pre);
2083 gfc_add_block_to_block (&loop->post, &se.post);
2084 ss->data.info.descriptor = se.expr;
6de9cd9a
DN
2085 break;
2086
2087 case GFC_SS_INTRINSIC:
2088 gfc_add_intrinsic_ss_code (loop, ss);
2089 break;
2090
2091 case GFC_SS_FUNCTION:
2092 /* Array function return value. We call the function and save its
2093 result in a temporary for use inside the loop. */
2094 gfc_init_se (&se, NULL);
2095 se.loop = loop;
2096 se.ss = ss;
2097 gfc_conv_expr (&se, ss->expr);
2098 gfc_add_block_to_block (&loop->pre, &se.pre);
2099 gfc_add_block_to_block (&loop->post, &se.post);
0348d6fd 2100 ss->string_length = se.string_length;
6de9cd9a
DN
2101 break;
2102
2103 case GFC_SS_CONSTRUCTOR:
f2d3cb25 2104 if (ss->expr->ts.type == BT_CHARACTER
fe8edd0c 2105 && ss->string_length == NULL
bc21d315
JW
2106 && ss->expr->ts.u.cl
2107 && ss->expr->ts.u.cl->length)
f2d3cb25
PT
2108 {
2109 gfc_init_se (&se, NULL);
bc21d315 2110 gfc_conv_expr_type (&se, ss->expr->ts.u.cl->length,
f2d3cb25
PT
2111 gfc_charlen_type_node);
2112 ss->string_length = se.expr;
2113 gfc_add_block_to_block (&loop->pre, &se.pre);
2114 gfc_add_block_to_block (&loop->post, &se.post);
2115 }
bdfd2ff0 2116 gfc_trans_array_constructor (loop, ss, where);
6de9cd9a
DN
2117 break;
2118
fc90a8f2 2119 case GFC_SS_TEMP:
e9cfef64
PB
2120 case GFC_SS_COMPONENT:
2121 /* Do nothing. These are handled elsewhere. */
fc90a8f2
PB
2122 break;
2123
6de9cd9a 2124 default:
6e45f57b 2125 gcc_unreachable ();
6de9cd9a
DN
2126 }
2127 }
2128}
2129
2130
2131/* Translate expressions for the descriptor and data pointer of a SS. */
2132/*GCC ARRAYS*/
2133
2134static void
2135gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
2136{
2137 gfc_se se;
2138 tree tmp;
2139
2140 /* Get the descriptor for the array to be scalarized. */
6e45f57b 2141 gcc_assert (ss->expr->expr_type == EXPR_VARIABLE);
6de9cd9a
DN
2142 gfc_init_se (&se, NULL);
2143 se.descriptor_only = 1;
2144 gfc_conv_expr_lhs (&se, ss->expr);
2145 gfc_add_block_to_block (block, &se.pre);
2146 ss->data.info.descriptor = se.expr;
40f20186 2147 ss->string_length = se.string_length;
6de9cd9a
DN
2148
2149 if (base)
2150 {
2151 /* Also the data pointer. */
2152 tmp = gfc_conv_array_data (se.expr);
2153 /* If this is a variable or address of a variable we use it directly.
2054fc29 2154 Otherwise we must evaluate it now to avoid breaking dependency
6de9cd9a
DN
2155 analysis by pulling the expressions for elemental array indices
2156 inside the loop. */
2157 if (!(DECL_P (tmp)
2158 || (TREE_CODE (tmp) == ADDR_EXPR
2159 && DECL_P (TREE_OPERAND (tmp, 0)))))
2160 tmp = gfc_evaluate_now (tmp, block);
2161 ss->data.info.data = tmp;
2162
2163 tmp = gfc_conv_array_offset (se.expr);
2164 ss->data.info.offset = gfc_evaluate_now (tmp, block);
2165 }
2166}
2167
2168
1f2959f0 2169/* Initialize a gfc_loopinfo structure. */
6de9cd9a
DN
2170
2171void
2172gfc_init_loopinfo (gfc_loopinfo * loop)
2173{
2174 int n;
2175
2176 memset (loop, 0, sizeof (gfc_loopinfo));
2177 gfc_init_block (&loop->pre);
2178 gfc_init_block (&loop->post);
2179
13413760 2180 /* Initially scalarize in order. */
6de9cd9a
DN
2181 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
2182 loop->order[n] = n;
2183
2184 loop->ss = gfc_ss_terminator;
2185}
2186
2187
e7dc5b4f 2188/* Copies the loop variable info to a gfc_se structure. Does not copy the SS
6de9cd9a
DN
2189 chain. */
2190
2191void
2192gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
2193{
2194 se->loop = loop;
2195}
2196
2197
2198/* Return an expression for the data pointer of an array. */
2199
2200tree
2201gfc_conv_array_data (tree descriptor)
2202{
2203 tree type;
2204
2205 type = TREE_TYPE (descriptor);
2206 if (GFC_ARRAY_TYPE_P (type))
2207 {
2208 if (TREE_CODE (type) == POINTER_TYPE)
2209 return descriptor;
2210 else
2211 {
13413760 2212 /* Descriptorless arrays. */
628c189e 2213 return gfc_build_addr_expr (NULL_TREE, descriptor);
6de9cd9a
DN
2214 }
2215 }
2216 else
4c73896d 2217 return gfc_conv_descriptor_data_get (descriptor);
6de9cd9a
DN
2218}
2219
2220
2221/* Return an expression for the base offset of an array. */
2222
2223tree
2224gfc_conv_array_offset (tree descriptor)
2225{
2226 tree type;
2227
2228 type = TREE_TYPE (descriptor);
2229 if (GFC_ARRAY_TYPE_P (type))
2230 return GFC_TYPE_ARRAY_OFFSET (type);
2231 else
568e8e1e 2232 return gfc_conv_descriptor_offset_get (descriptor);
6de9cd9a
DN
2233}
2234
2235
2236/* Get an expression for the array stride. */
2237
2238tree
2239gfc_conv_array_stride (tree descriptor, int dim)
2240{
2241 tree tmp;
2242 tree type;
2243
2244 type = TREE_TYPE (descriptor);
2245
2246 /* For descriptorless arrays use the array size. */
2247 tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
2248 if (tmp != NULL_TREE)
2249 return tmp;
2250
568e8e1e 2251 tmp = gfc_conv_descriptor_stride_get (descriptor, gfc_rank_cst[dim]);
6de9cd9a
DN
2252 return tmp;
2253}
2254
2255
2256/* Like gfc_conv_array_stride, but for the lower bound. */
2257
2258tree
2259gfc_conv_array_lbound (tree descriptor, int dim)
2260{
2261 tree tmp;
2262 tree type;
2263
2264 type = TREE_TYPE (descriptor);
2265
2266 tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
2267 if (tmp != NULL_TREE)
2268 return tmp;
2269
568e8e1e 2270 tmp = gfc_conv_descriptor_lbound_get (descriptor, gfc_rank_cst[dim]);
6de9cd9a
DN
2271 return tmp;
2272}
2273
2274
2275/* Like gfc_conv_array_stride, but for the upper bound. */
2276
2277tree
2278gfc_conv_array_ubound (tree descriptor, int dim)
2279{
2280 tree tmp;
2281 tree type;
2282
2283 type = TREE_TYPE (descriptor);
2284
2285 tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
2286 if (tmp != NULL_TREE)
2287 return tmp;
2288
2289 /* This should only ever happen when passing an assumed shape array
2290 as an actual parameter. The value will never be used. */
2291 if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
7ab92584 2292 return gfc_index_zero_node;
6de9cd9a 2293
568e8e1e 2294 tmp = gfc_conv_descriptor_ubound_get (descriptor, gfc_rank_cst[dim]);
6de9cd9a
DN
2295 return tmp;
2296}
2297
2298
6de9cd9a
DN
2299/* Generate code to perform an array index bound check. */
2300
2301static tree
d16b57df 2302gfc_trans_array_bound_check (gfc_se * se, tree descriptor, tree index, int n,
c099916d 2303 locus * where, bool check_upper)
6de9cd9a 2304{
6de9cd9a 2305 tree fault;
c6ec7cc6 2306 tree tmp_lo, tmp_up;
dd18a33b 2307 char *msg;
d19c0f4f 2308 const char * name = NULL;
6de9cd9a 2309
d3d3011f 2310 if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
6de9cd9a
DN
2311 return index;
2312
2313 index = gfc_evaluate_now (index, &se->pre);
dd18a33b 2314
d19c0f4f
FXC
2315 /* We find a name for the error message. */
2316 if (se->ss)
2317 name = se->ss->expr->symtree->name;
2318
2319 if (!name && se->loop && se->loop->ss && se->loop->ss->expr
2320 && se->loop->ss->expr->symtree)
2321 name = se->loop->ss->expr->symtree->name;
2322
2323 if (!name && se->loop && se->loop->ss && se->loop->ss->loop_chain
2324 && se->loop->ss->loop_chain->expr
2325 && se->loop->ss->loop_chain->expr->symtree)
2326 name = se->loop->ss->loop_chain->expr->symtree->name;
2327
d19c0f4f
FXC
2328 if (!name && se->loop && se->loop->ss && se->loop->ss->expr)
2329 {
2330 if (se->loop->ss->expr->expr_type == EXPR_FUNCTION
2331 && se->loop->ss->expr->value.function.name)
2332 name = se->loop->ss->expr->value.function.name;
2333 else
2334 if (se->loop->ss->type == GFC_SS_CONSTRUCTOR
2335 || se->loop->ss->type == GFC_SS_SCALAR)
2336 name = "unnamed constant";
2337 }
2338
8583a512 2339 if (TREE_CODE (descriptor) == VAR_DECL)
e3e529d1
SK
2340 name = IDENTIFIER_POINTER (DECL_NAME (descriptor));
2341
c6ec7cc6 2342 /* If upper bound is present, include both bounds in the error message. */
c099916d
FXC
2343 if (check_upper)
2344 {
c6ec7cc6
DW
2345 tmp_lo = gfc_conv_array_lbound (descriptor, n);
2346 tmp_up = gfc_conv_array_ubound (descriptor, n);
2347
2348 if (name)
2349 asprintf (&msg, "Index '%%ld' of dimension %d of array '%s' "
2350 "outside of expected range (%%ld:%%ld)", n+1, name);
2351 else
2352 asprintf (&msg, "Index '%%ld' of dimension %d "
2353 "outside of expected range (%%ld:%%ld)", n+1);
2354
2355 fault = fold_build2 (LT_EXPR, boolean_type_node, index, tmp_lo);
2356 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2357 fold_convert (long_integer_type_node, index),
2358 fold_convert (long_integer_type_node, tmp_lo),
2359 fold_convert (long_integer_type_node, tmp_up));
2360 fault = fold_build2 (GT_EXPR, boolean_type_node, index, tmp_up);
2361 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2362 fold_convert (long_integer_type_node, index),
2363 fold_convert (long_integer_type_node, tmp_lo),
2364 fold_convert (long_integer_type_node, tmp_up));
2365 gfc_free (msg);
2366 }
2367 else
2368 {
2369 tmp_lo = gfc_conv_array_lbound (descriptor, n);
2370
c099916d 2371 if (name)
c6ec7cc6
DW
2372 asprintf (&msg, "Index '%%ld' of dimension %d of array '%s' "
2373 "below lower bound of %%ld", n+1, name);
c099916d 2374 else
c6ec7cc6
DW
2375 asprintf (&msg, "Index '%%ld' of dimension %d "
2376 "below lower bound of %%ld", n+1);
2377
2378 fault = fold_build2 (LT_EXPR, boolean_type_node, index, tmp_lo);
0d52899f 2379 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
c8fe94c7 2380 fold_convert (long_integer_type_node, index),
c6ec7cc6 2381 fold_convert (long_integer_type_node, tmp_lo));
c099916d
FXC
2382 gfc_free (msg);
2383 }
6de9cd9a
DN
2384
2385 return index;
2386}
2387
2388
6de9cd9a 2389/* Return the offset for an index. Performs bound checking for elemental
e7dc5b4f 2390 dimensions. Single element references are processed separately. */
6de9cd9a
DN
2391
2392static tree
2393gfc_conv_array_index_offset (gfc_se * se, gfc_ss_info * info, int dim, int i,
2394 gfc_array_ref * ar, tree stride)
2395{
2396 tree index;
7a70c12d
RS
2397 tree desc;
2398 tree data;
6de9cd9a
DN
2399
2400 /* Get the index into the array for this dimension. */
2401 if (ar)
2402 {
6e45f57b 2403 gcc_assert (ar->type != AR_ELEMENT);
7a70c12d 2404 switch (ar->dimen_type[dim])
6de9cd9a 2405 {
7a70c12d 2406 case DIMEN_ELEMENT:
6de9cd9a 2407 /* Elemental dimension. */
6e45f57b 2408 gcc_assert (info->subscript[dim]
7a70c12d 2409 && info->subscript[dim]->type == GFC_SS_SCALAR);
6de9cd9a
DN
2410 /* We've already translated this value outside the loop. */
2411 index = info->subscript[dim]->data.scalar.expr;
2412
c099916d
FXC
2413 index = gfc_trans_array_bound_check (se, info->descriptor,
2414 index, dim, &ar->where,
b3aefde2
TB
2415 ar->as->type != AS_ASSUMED_SIZE
2416 || dim < ar->dimen - 1);
7a70c12d
RS
2417 break;
2418
2419 case DIMEN_VECTOR:
2420 gcc_assert (info && se->loop);
2421 gcc_assert (info->subscript[dim]
2422 && info->subscript[dim]->type == GFC_SS_VECTOR);
2423 desc = info->subscript[dim]->data.info.descriptor;
2424
2425 /* Get a zero-based index into the vector. */
2426 index = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2427 se->loop->loopvar[i], se->loop->from[i]);
2428
2429 /* Multiply the index by the stride. */
2430 index = fold_build2 (MULT_EXPR, gfc_array_index_type,
2431 index, gfc_conv_array_stride (desc, 0));
2432
2433 /* Read the vector to get an index into info->descriptor. */
db3927fb
AH
2434 data = build_fold_indirect_ref_loc (input_location,
2435 gfc_conv_array_data (desc));
1d6b7f39 2436 index = gfc_build_array_ref (data, index, NULL);
7a70c12d 2437 index = gfc_evaluate_now (index, &se->pre);
92375a20 2438 index = fold_convert (gfc_array_index_type, index);
7a70c12d
RS
2439
2440 /* Do any bounds checking on the final info->descriptor index. */
c099916d
FXC
2441 index = gfc_trans_array_bound_check (se, info->descriptor,
2442 index, dim, &ar->where,
b3aefde2
TB
2443 ar->as->type != AS_ASSUMED_SIZE
2444 || dim < ar->dimen - 1);
7a70c12d
RS
2445 break;
2446
2447 case DIMEN_RANGE:
6de9cd9a 2448 /* Scalarized dimension. */
6e45f57b 2449 gcc_assert (info && se->loop);
6de9cd9a 2450
df7df328 2451 /* Multiply the loop variable by the stride and delta. */
6de9cd9a 2452 index = se->loop->loopvar[i];
ecc54e6e
RS
2453 if (!integer_onep (info->stride[i]))
2454 index = fold_build2 (MULT_EXPR, gfc_array_index_type, index,
2455 info->stride[i]);
2456 if (!integer_zerop (info->delta[i]))
2457 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index,
2458 info->delta[i]);
7a70c12d 2459 break;
6de9cd9a 2460
7a70c12d
RS
2461 default:
2462 gcc_unreachable ();
6de9cd9a
DN
2463 }
2464 }
2465 else
2466 {
e9cfef64 2467 /* Temporary array or derived type component. */
6e45f57b 2468 gcc_assert (se->loop);
6de9cd9a 2469 index = se->loop->loopvar[se->loop->order[i]];
e9cfef64 2470 if (!integer_zerop (info->delta[i]))
10c7a96f
SB
2471 index = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2472 index, info->delta[i]);
6de9cd9a
DN
2473 }
2474
2475 /* Multiply by the stride. */
ecc54e6e
RS
2476 if (!integer_onep (stride))
2477 index = fold_build2 (MULT_EXPR, gfc_array_index_type, index, stride);
6de9cd9a
DN
2478
2479 return index;
2480}
2481
2482
2483/* Build a scalarized reference to an array. */
2484
2485static void
2486gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar)
2487{
2488 gfc_ss_info *info;
1d6b7f39 2489 tree decl = NULL_TREE;
6de9cd9a
DN
2490 tree index;
2491 tree tmp;
2492 int n;
2493
2494 info = &se->ss->data.info;
2495 if (ar)
2496 n = se->loop->order[0];
2497 else
2498 n = 0;
2499
2500 index = gfc_conv_array_index_offset (se, info, info->dim[n], n, ar,
2501 info->stride0);
2502 /* Add the offset for this dimension to the stored offset for all other
2503 dimensions. */
62511fb1
RS
2504 if (!integer_zerop (info->offset))
2505 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, info->offset);
6de9cd9a 2506
1d6b7f39
PT
2507 if (se->ss->expr && is_subref_array (se->ss->expr))
2508 decl = se->ss->expr->symtree->n.sym->backend_decl;
2509
db3927fb
AH
2510 tmp = build_fold_indirect_ref_loc (input_location,
2511 info->data);
1d6b7f39 2512 se->expr = gfc_build_array_ref (tmp, index, decl);
6de9cd9a
DN
2513}
2514
2515
2516/* Translate access of temporary array. */
2517
2518void
2519gfc_conv_tmp_array_ref (gfc_se * se)
2520{
40f20186 2521 se->string_length = se->ss->string_length;
6de9cd9a
DN
2522 gfc_conv_scalarized_array_ref (se, NULL);
2523}
2524
2525
2526/* Build an array reference. se->expr already holds the array descriptor.
2527 This should be either a variable, indirect variable reference or component
2528 reference. For arrays which do not have a descriptor, se->expr will be
2529 the data pointer.
2530 a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
2531
2532void
dd18a33b
FXC
2533gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_symbol * sym,
2534 locus * where)
6de9cd9a
DN
2535{
2536 int n;
2537 tree index;
2538 tree tmp;
2539 tree stride;
6de9cd9a 2540 gfc_se indexse;
59e36b72 2541 gfc_se tmpse;
6de9cd9a 2542
d3a9eea2
TB
2543 if (ar->dimen == 0)
2544 return;
2545
e7dc5b4f 2546 /* Handle scalarized references separately. */
6de9cd9a
DN
2547 if (ar->type != AR_ELEMENT)
2548 {
2549 gfc_conv_scalarized_array_ref (se, ar);
068e7338 2550 gfc_advance_se_ss_chain (se);
6de9cd9a
DN
2551 return;
2552 }
2553
7ab92584 2554 index = gfc_index_zero_node;
6de9cd9a 2555
6de9cd9a
DN
2556 /* Calculate the offsets from all the dimensions. */
2557 for (n = 0; n < ar->dimen; n++)
2558 {
1f2959f0 2559 /* Calculate the index for this dimension. */
068e7338 2560 gfc_init_se (&indexse, se);
6de9cd9a
DN
2561 gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
2562 gfc_add_block_to_block (&se->pre, &indexse.pre);
2563
d3d3011f 2564 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
6de9cd9a
DN
2565 {
2566 /* Check array bounds. */
2567 tree cond;
dd18a33b 2568 char *msg;
6de9cd9a 2569
a90552d5
FXC
2570 /* Evaluate the indexse.expr only once. */
2571 indexse.expr = save_expr (indexse.expr);
2572
c099916d 2573 /* Lower bound. */
6de9cd9a 2574 tmp = gfc_conv_array_lbound (se->expr, n);
59e36b72
PT
2575 if (sym->attr.temporary)
2576 {
2577 gfc_init_se (&tmpse, se);
2578 gfc_conv_expr_type (&tmpse, ar->as->lower[n],
2579 gfc_array_index_type);
2580 gfc_add_block_to_block (&se->pre, &tmpse.pre);
2581 tmp = tmpse.expr;
2582 }
2583
10c7a96f
SB
2584 cond = fold_build2 (LT_EXPR, boolean_type_node,
2585 indexse.expr, tmp);
c6ec7cc6
DW
2586 asprintf (&msg, "Index '%%ld' of dimension %d of array '%s' "
2587 "below lower bound of %%ld", n+1, sym->name);
0d52899f 2588 gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
c8fe94c7
FXC
2589 fold_convert (long_integer_type_node,
2590 indexse.expr),
2591 fold_convert (long_integer_type_node, tmp));
dd18a33b 2592 gfc_free (msg);
6de9cd9a 2593
c099916d
FXC
2594 /* Upper bound, but not for the last dimension of assumed-size
2595 arrays. */
b3aefde2 2596 if (n < ar->dimen - 1 || ar->as->type != AS_ASSUMED_SIZE)
c099916d
FXC
2597 {
2598 tmp = gfc_conv_array_ubound (se->expr, n);
59e36b72
PT
2599 if (sym->attr.temporary)
2600 {
2601 gfc_init_se (&tmpse, se);
2602 gfc_conv_expr_type (&tmpse, ar->as->upper[n],
2603 gfc_array_index_type);
2604 gfc_add_block_to_block (&se->pre, &tmpse.pre);
2605 tmp = tmpse.expr;
2606 }
2607
c099916d
FXC
2608 cond = fold_build2 (GT_EXPR, boolean_type_node,
2609 indexse.expr, tmp);
c6ec7cc6
DW
2610 asprintf (&msg, "Index '%%ld' of dimension %d of array '%s' "
2611 "above upper bound of %%ld", n+1, sym->name);
0d52899f 2612 gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
c8fe94c7
FXC
2613 fold_convert (long_integer_type_node,
2614 indexse.expr),
2615 fold_convert (long_integer_type_node, tmp));
c099916d
FXC
2616 gfc_free (msg);
2617 }
6de9cd9a
DN
2618 }
2619
2620 /* Multiply the index by the stride. */
2621 stride = gfc_conv_array_stride (se->expr, n);
10c7a96f
SB
2622 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, indexse.expr,
2623 stride);
6de9cd9a
DN
2624
2625 /* And add it to the total. */
10c7a96f 2626 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, tmp);
6de9cd9a
DN
2627 }
2628
6de9cd9a
DN
2629 tmp = gfc_conv_array_offset (se->expr);
2630 if (!integer_zerop (tmp))
10c7a96f 2631 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, tmp);
1d6b7f39 2632
6de9cd9a
DN
2633 /* Access the calculated element. */
2634 tmp = gfc_conv_array_data (se->expr);
38611275 2635 tmp = build_fold_indirect_ref (tmp);
1d6b7f39 2636 se->expr = gfc_build_array_ref (tmp, index, sym->backend_decl);
6de9cd9a
DN
2637}
2638
2639
2640/* Generate the code to be executed immediately before entering a
2641 scalarization loop. */
2642
2643static void
2644gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
2645 stmtblock_t * pblock)
2646{
2647 tree index;
2648 tree stride;
2649 gfc_ss_info *info;
2650 gfc_ss *ss;
2651 gfc_se se;
2652 int i;
2653
2654 /* This code will be executed before entering the scalarization loop
2655 for this dimension. */
2656 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2657 {
2658 if ((ss->useflags & flag) == 0)
2659 continue;
2660
2661 if (ss->type != GFC_SS_SECTION
e9cfef64
PB
2662 && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
2663 && ss->type != GFC_SS_COMPONENT)
6de9cd9a
DN
2664 continue;
2665
2666 info = &ss->data.info;
2667
2668 if (dim >= info->dimen)
2669 continue;
2670
2671 if (dim == info->dimen - 1)
2672 {
2673 /* For the outermost loop calculate the offset due to any
2674 elemental dimensions. It will have been initialized with the
2675 base offset of the array. */
2676 if (info->ref)
2677 {
2678 for (i = 0; i < info->ref->u.ar.dimen; i++)
2679 {
2680 if (info->ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2681 continue;
2682
2683 gfc_init_se (&se, NULL);
2684 se.loop = loop;
2685 se.expr = info->descriptor;
2686 stride = gfc_conv_array_stride (info->descriptor, i);
2687 index = gfc_conv_array_index_offset (&se, info, i, -1,
2688 &info->ref->u.ar,
2689 stride);
2690 gfc_add_block_to_block (pblock, &se.pre);
2691
10c7a96f
SB
2692 info->offset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2693 info->offset, index);
6de9cd9a
DN
2694 info->offset = gfc_evaluate_now (info->offset, pblock);
2695 }
2696
2697 i = loop->order[0];
2698 stride = gfc_conv_array_stride (info->descriptor, info->dim[i]);
2699 }
2700 else
2701 stride = gfc_conv_array_stride (info->descriptor, 0);
2702
2703 /* Calculate the stride of the innermost loop. Hopefully this will
2704 allow the backend optimizers to do their stuff more effectively.
2705 */
2706 info->stride0 = gfc_evaluate_now (stride, pblock);
2707 }
2708 else
2709 {
2710 /* Add the offset for the previous loop dimension. */
2711 gfc_array_ref *ar;
2712
2713 if (info->ref)
2714 {
2715 ar = &info->ref->u.ar;
2716 i = loop->order[dim + 1];
2717 }
2718 else
2719 {
2720 ar = NULL;
2721 i = dim + 1;
2722 }
2723
2724 gfc_init_se (&se, NULL);
2725 se.loop = loop;
2726 se.expr = info->descriptor;
2727 stride = gfc_conv_array_stride (info->descriptor, info->dim[i]);
2728 index = gfc_conv_array_index_offset (&se, info, info->dim[i], i,
2729 ar, stride);
2730 gfc_add_block_to_block (pblock, &se.pre);
10c7a96f
SB
2731 info->offset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2732 info->offset, index);
6de9cd9a
DN
2733 info->offset = gfc_evaluate_now (info->offset, pblock);
2734 }
2735
e7dc5b4f 2736 /* Remember this offset for the second loop. */
6de9cd9a
DN
2737 if (dim == loop->temp_dim - 1)
2738 info->saved_offset = info->offset;
2739 }
2740}
2741
2742
2743/* Start a scalarized expression. Creates a scope and declares loop
2744 variables. */
2745
2746void
2747gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
2748{
2749 int dim;
2750 int n;
2751 int flags;
2752
6e45f57b 2753 gcc_assert (!loop->array_parameter);
6de9cd9a
DN
2754
2755 for (dim = loop->dimen - 1; dim >= 0; dim--)
2756 {
2757 n = loop->order[dim];
2758
2759 gfc_start_block (&loop->code[n]);
2760
2761 /* Create the loop variable. */
2762 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
2763
2764 if (dim < loop->temp_dim)
2765 flags = 3;
2766 else
2767 flags = 1;
2768 /* Calculate values that will be constant within this loop. */
2769 gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
2770 }
2771 gfc_start_block (pbody);
2772}
2773
2774
2775/* Generates the actual loop code for a scalarization loop. */
2776
80927a56 2777void
6de9cd9a
DN
2778gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
2779 stmtblock_t * pbody)
2780{
2781 stmtblock_t block;
2782 tree cond;
2783 tree tmp;
2784 tree loopbody;
2785 tree exit_label;
34d01e1d
VL
2786 tree stmt;
2787 tree init;
2788 tree incr;
6de9cd9a 2789
34d01e1d
VL
2790 if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS))
2791 == (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS)
2792 && n == loop->dimen - 1)
2793 {
2794 /* We create an OMP_FOR construct for the outermost scalarized loop. */
2795 init = make_tree_vec (1);
2796 cond = make_tree_vec (1);
2797 incr = make_tree_vec (1);
2798
2799 /* Cycle statement is implemented with a goto. Exit statement must not
2800 be present for this loop. */
2801 exit_label = gfc_build_label_decl (NULL_TREE);
2802 TREE_USED (exit_label) = 1;
2803
2804 /* Label for cycle statements (if needed). */
2805 tmp = build1_v (LABEL_EXPR, exit_label);
2806 gfc_add_expr_to_block (pbody, tmp);
2807
2808 stmt = make_node (OMP_FOR);
2809
2810 TREE_TYPE (stmt) = void_type_node;
2811 OMP_FOR_BODY (stmt) = loopbody = gfc_finish_block (pbody);
2812
c2255bc4
AH
2813 OMP_FOR_CLAUSES (stmt) = build_omp_clause (input_location,
2814 OMP_CLAUSE_SCHEDULE);
34d01e1d
VL
2815 OMP_CLAUSE_SCHEDULE_KIND (OMP_FOR_CLAUSES (stmt))
2816 = OMP_CLAUSE_SCHEDULE_STATIC;
2817 if (ompws_flags & OMPWS_NOWAIT)
2818 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (stmt))
c2255bc4 2819 = build_omp_clause (input_location, OMP_CLAUSE_NOWAIT);
34d01e1d
VL
2820
2821 /* Initialize the loopvar. */
2822 TREE_VEC_ELT (init, 0) = build2_v (MODIFY_EXPR, loop->loopvar[n],
2823 loop->from[n]);
2824 OMP_FOR_INIT (stmt) = init;
2825 /* The exit condition. */
2826 TREE_VEC_ELT (cond, 0) = build2 (LE_EXPR, boolean_type_node,
2827 loop->loopvar[n], loop->to[n]);
2828 OMP_FOR_COND (stmt) = cond;
2829 /* Increment the loopvar. */
2830 tmp = build2 (PLUS_EXPR, gfc_array_index_type,
2831 loop->loopvar[n], gfc_index_one_node);
2832 TREE_VEC_ELT (incr, 0) = fold_build2 (MODIFY_EXPR,
2833 void_type_node, loop->loopvar[n], tmp);
2834 OMP_FOR_INCR (stmt) = incr;
2835
2836 ompws_flags &= ~OMPWS_CURR_SINGLEUNIT;
2837 gfc_add_expr_to_block (&loop->code[n], stmt);
2838 }
2839 else
2840 {
2841 loopbody = gfc_finish_block (pbody);
6de9cd9a 2842
34d01e1d 2843 /* Initialize the loopvar. */
80927a56
JJ
2844 if (loop->loopvar[n] != loop->from[n])
2845 gfc_add_modify (&loop->code[n], loop->loopvar[n], loop->from[n]);
6de9cd9a 2846
34d01e1d 2847 exit_label = gfc_build_label_decl (NULL_TREE);
6de9cd9a 2848
34d01e1d
VL
2849 /* Generate the loop body. */
2850 gfc_init_block (&block);
6de9cd9a 2851
34d01e1d
VL
2852 /* The exit condition. */
2853 cond = fold_build2 (GT_EXPR, boolean_type_node,
2854 loop->loopvar[n], loop->to[n]);
2855 tmp = build1_v (GOTO_EXPR, exit_label);
2856 TREE_USED (exit_label) = 1;
c2255bc4 2857 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
34d01e1d 2858 gfc_add_expr_to_block (&block, tmp);
6de9cd9a 2859
34d01e1d
VL
2860 /* The main body. */
2861 gfc_add_expr_to_block (&block, loopbody);
6de9cd9a 2862
34d01e1d
VL
2863 /* Increment the loopvar. */
2864 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2865 loop->loopvar[n], gfc_index_one_node);
2866 gfc_add_modify (&block, loop->loopvar[n], tmp);
6de9cd9a 2867
34d01e1d
VL
2868 /* Build the loop. */
2869 tmp = gfc_finish_block (&block);
2870 tmp = build1_v (LOOP_EXPR, tmp);
2871 gfc_add_expr_to_block (&loop->code[n], tmp);
2872
2873 /* Add the exit label. */
2874 tmp = build1_v (LABEL_EXPR, exit_label);
2875 gfc_add_expr_to_block (&loop->code[n], tmp);
2876 }
6de9cd9a 2877
6de9cd9a
DN
2878}
2879
2880
2881/* Finishes and generates the loops for a scalarized expression. */
2882
2883void
2884gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
2885{
2886 int dim;
2887 int n;
2888 gfc_ss *ss;
2889 stmtblock_t *pblock;
2890 tree tmp;
2891
2892 pblock = body;
2893 /* Generate the loops. */
2894 for (dim = 0; dim < loop->dimen; dim++)
2895 {
2896 n = loop->order[dim];
2897 gfc_trans_scalarized_loop_end (loop, n, pblock);
2898 loop->loopvar[n] = NULL_TREE;
2899 pblock = &loop->code[n];
2900 }
2901
2902 tmp = gfc_finish_block (pblock);
2903 gfc_add_expr_to_block (&loop->pre, tmp);
2904
2905 /* Clear all the used flags. */
2906 for (ss = loop->ss; ss; ss = ss->loop_chain)
2907 ss->useflags = 0;
2908}
2909
2910
2911/* Finish the main body of a scalarized expression, and start the secondary
2912 copying body. */
2913
2914void
2915gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
2916{
2917 int dim;
2918 int n;
2919 stmtblock_t *pblock;
2920 gfc_ss *ss;
2921
2922 pblock = body;
2923 /* We finish as many loops as are used by the temporary. */
2924 for (dim = 0; dim < loop->temp_dim - 1; dim++)
2925 {
2926 n = loop->order[dim];
2927 gfc_trans_scalarized_loop_end (loop, n, pblock);
2928 loop->loopvar[n] = NULL_TREE;
2929 pblock = &loop->code[n];
2930 }
2931
2932 /* We don't want to finish the outermost loop entirely. */
2933 n = loop->order[loop->temp_dim - 1];
2934 gfc_trans_scalarized_loop_end (loop, n, pblock);
2935
2936 /* Restore the initial offsets. */
2937 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2938 {
2939 if ((ss->useflags & 2) == 0)
2940 continue;
2941
2942 if (ss->type != GFC_SS_SECTION
e9cfef64
PB
2943 && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
2944 && ss->type != GFC_SS_COMPONENT)
6de9cd9a
DN
2945 continue;
2946
2947 ss->data.info.offset = ss->data.info.saved_offset;
2948 }
2949
2950 /* Restart all the inner loops we just finished. */
2951 for (dim = loop->temp_dim - 2; dim >= 0; dim--)
2952 {
2953 n = loop->order[dim];
2954
2955 gfc_start_block (&loop->code[n]);
2956
2957 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
2958
2959 gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
2960 }
2961
2962 /* Start a block for the secondary copying code. */
2963 gfc_start_block (body);
2964}
2965
2966
2967/* Calculate the upper bound of an array section. */
2968
2969static tree
2970gfc_conv_section_upper_bound (gfc_ss * ss, int n, stmtblock_t * pblock)
2971{
2972 int dim;
6de9cd9a
DN
2973 gfc_expr *end;
2974 tree desc;
2975 tree bound;
2976 gfc_se se;
7a70c12d 2977 gfc_ss_info *info;
6de9cd9a 2978
6e45f57b 2979 gcc_assert (ss->type == GFC_SS_SECTION);
6de9cd9a 2980
7a70c12d
RS
2981 info = &ss->data.info;
2982 dim = info->dim[n];
6de9cd9a 2983
7a70c12d
RS
2984 if (info->ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
2985 /* We'll calculate the upper bound once we have access to the
2986 vector's descriptor. */
2987 return NULL;
2988
2989 gcc_assert (info->ref->u.ar.dimen_type[dim] == DIMEN_RANGE);
2990 desc = info->descriptor;
2991 end = info->ref->u.ar.end[dim];
6de9cd9a
DN
2992
2993 if (end)
2994 {
2995 /* The upper bound was specified. */
2996 gfc_init_se (&se, NULL);
2997 gfc_conv_expr_type (&se, end, gfc_array_index_type);
2998 gfc_add_block_to_block (pblock, &se.pre);
2999 bound = se.expr;
3000 }
3001 else
3002 {
f7b529fa 3003 /* No upper bound was specified, so use the bound of the array. */
6de9cd9a
DN
3004 bound = gfc_conv_array_ubound (desc, dim);
3005 }
3006
3007 return bound;
3008}
3009
3010
3011/* Calculate the lower bound of an array section. */
3012
3013static void
3014gfc_conv_section_startstride (gfc_loopinfo * loop, gfc_ss * ss, int n)
3015{
3016 gfc_expr *start;
8424e0d8 3017 gfc_expr *end;
6de9cd9a 3018 gfc_expr *stride;
6de9cd9a
DN
3019 tree desc;
3020 gfc_se se;
3021 gfc_ss_info *info;
3022 int dim;
3023
7a70c12d 3024 gcc_assert (ss->type == GFC_SS_SECTION);
6de9cd9a 3025
7a70c12d 3026 info = &ss->data.info;
6de9cd9a
DN
3027 dim = info->dim[n];
3028
7a70c12d 3029 if (info->ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
6de9cd9a 3030 {
7a70c12d
RS
3031 /* We use a zero-based index to access the vector. */
3032 info->start[n] = gfc_index_zero_node;
8424e0d8 3033 info->end[n] = gfc_index_zero_node;
7a70c12d
RS
3034 info->stride[n] = gfc_index_one_node;
3035 return;
6de9cd9a
DN
3036 }
3037
7a70c12d
RS
3038 gcc_assert (info->ref->u.ar.dimen_type[dim] == DIMEN_RANGE);
3039 desc = info->descriptor;
3040 start = info->ref->u.ar.start[dim];
8424e0d8 3041 end = info->ref->u.ar.end[dim];
7a70c12d 3042 stride = info->ref->u.ar.stride[dim];
6de9cd9a
DN
3043
3044 /* Calculate the start of the range. For vector subscripts this will
3045 be the range of the vector. */
3046 if (start)
3047 {
3048 /* Specified section start. */
3049 gfc_init_se (&se, NULL);
3050 gfc_conv_expr_type (&se, start, gfc_array_index_type);
3051 gfc_add_block_to_block (&loop->pre, &se.pre);
3052 info->start[n] = se.expr;
3053 }
3054 else
3055 {
3056 /* No lower bound specified so use the bound of the array. */
3057 info->start[n] = gfc_conv_array_lbound (desc, dim);
3058 }
3059 info->start[n] = gfc_evaluate_now (info->start[n], &loop->pre);
3060
8424e0d8
PT
3061 /* Similarly calculate the end. Although this is not used in the
3062 scalarizer, it is needed when checking bounds and where the end
3063 is an expression with side-effects. */
3064 if (end)
3065 {
3066 /* Specified section start. */
3067 gfc_init_se (&se, NULL);
3068 gfc_conv_expr_type (&se, end, gfc_array_index_type);
3069 gfc_add_block_to_block (&loop->pre, &se.pre);
3070 info->end[n] = se.expr;
3071 }
3072 else
3073 {
3074 /* No upper bound specified so use the bound of the array. */
3075 info->end[n] = gfc_conv_array_ubound (desc, dim);
3076 }
3077 info->end[n] = gfc_evaluate_now (info->end[n], &loop->pre);
3078
6de9cd9a
DN
3079 /* Calculate the stride. */
3080 if (stride == NULL)
7ab92584 3081 info->stride[n] = gfc_index_one_node;
6de9cd9a
DN
3082 else
3083 {
3084 gfc_init_se (&se, NULL);
3085 gfc_conv_expr_type (&se, stride, gfc_array_index_type);
3086 gfc_add_block_to_block (&loop->pre, &se.pre);
3087 info->stride[n] = gfc_evaluate_now (se.expr, &loop->pre);
3088 }
3089}
3090
3091
3092/* Calculates the range start and stride for a SS chain. Also gets the
3093 descriptor and data pointer. The range of vector subscripts is the size
3094 of the vector. Array bounds are also checked. */
3095
3096void
3097gfc_conv_ss_startstride (gfc_loopinfo * loop)
3098{
3099 int n;
3100 tree tmp;
3101 gfc_ss *ss;
6de9cd9a
DN
3102 tree desc;
3103
3104 loop->dimen = 0;
3105 /* Determine the rank of the loop. */
3106 for (ss = loop->ss;
3107 ss != gfc_ss_terminator && loop->dimen == 0; ss = ss->loop_chain)
3108 {
3109 switch (ss->type)
3110 {
3111 case GFC_SS_SECTION:
3112 case GFC_SS_CONSTRUCTOR:
3113 case GFC_SS_FUNCTION:
e9cfef64 3114 case GFC_SS_COMPONENT:
6de9cd9a
DN
3115 loop->dimen = ss->data.info.dimen;
3116 break;
3117
f5f701ad
PT
3118 /* As usual, lbound and ubound are exceptions!. */
3119 case GFC_SS_INTRINSIC:
cd5ecab6 3120 switch (ss->expr->value.function.isym->id)
f5f701ad
PT
3121 {
3122 case GFC_ISYM_LBOUND:
3123 case GFC_ISYM_UBOUND:
3124 loop->dimen = ss->data.info.dimen;
3125
3126 default:
3127 break;
3128 }
3129
6de9cd9a
DN
3130 default:
3131 break;
3132 }
3133 }
3134
ca39e6f2
FXC
3135 /* We should have determined the rank of the expression by now. If
3136 not, that's bad news. */
3137 gcc_assert (loop->dimen != 0);
6de9cd9a 3138
13413760 3139 /* Loop over all the SS in the chain. */
6de9cd9a
DN
3140 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
3141 {
e9cfef64
PB
3142 if (ss->expr && ss->expr->shape && !ss->shape)
3143 ss->shape = ss->expr->shape;
3144
6de9cd9a
DN
3145 switch (ss->type)
3146 {
3147 case GFC_SS_SECTION:
3148 /* Get the descriptor for the array. */
3149 gfc_conv_ss_descriptor (&loop->pre, ss, !loop->array_parameter);
3150
3151 for (n = 0; n < ss->data.info.dimen; n++)
3152 gfc_conv_section_startstride (loop, ss, n);
3153 break;
3154
f5f701ad 3155 case GFC_SS_INTRINSIC:
cd5ecab6 3156 switch (ss->expr->value.function.isym->id)
f5f701ad
PT
3157 {
3158 /* Fall through to supply start and stride. */
3159 case GFC_ISYM_LBOUND:
3160 case GFC_ISYM_UBOUND:
3161 break;
3162 default:
3163 continue;
3164 }
3165
6de9cd9a
DN
3166 case GFC_SS_CONSTRUCTOR:
3167 case GFC_SS_FUNCTION:
3168 for (n = 0; n < ss->data.info.dimen; n++)
3169 {
7ab92584 3170 ss->data.info.start[n] = gfc_index_zero_node;
8424e0d8 3171 ss->data.info.end[n] = gfc_index_zero_node;
7ab92584 3172 ss->data.info.stride[n] = gfc_index_one_node;
6de9cd9a
DN
3173 }
3174 break;
3175
3176 default:
3177 break;
3178 }
3179 }
3180
3181 /* The rest is just runtime bound checking. */
d3d3011f 3182 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
6de9cd9a
DN
3183 {
3184 stmtblock_t block;
ef31fe62 3185 tree lbound, ubound;
6de9cd9a
DN
3186 tree end;
3187 tree size[GFC_MAX_DIMENSIONS];
c6ec7cc6 3188 tree stride_pos, stride_neg, non_zerosized, tmp2, tmp3;
6de9cd9a 3189 gfc_ss_info *info;
dd18a33b 3190 char *msg;
6de9cd9a
DN
3191 int dim;
3192
3193 gfc_start_block (&block);
3194
6de9cd9a
DN
3195 for (n = 0; n < loop->dimen; n++)
3196 size[n] = NULL_TREE;
3197
3198 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
3199 {
ba4698e1
FXC
3200 stmtblock_t inner;
3201
6de9cd9a
DN
3202 if (ss->type != GFC_SS_SECTION)
3203 continue;
3204
ba4698e1
FXC
3205 gfc_start_block (&inner);
3206
6de9cd9a
DN
3207 /* TODO: range checking for mapped dimensions. */
3208 info = &ss->data.info;
3209
7a70c12d
RS
3210 /* This code only checks ranges. Elemental and vector
3211 dimensions are checked later. */
6de9cd9a
DN
3212 for (n = 0; n < loop->dimen; n++)
3213 {
c099916d
FXC
3214 bool check_upper;
3215
6de9cd9a 3216 dim = info->dim[n];
7a70c12d
RS
3217 if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
3218 continue;
c099916d 3219
1954a27b 3220 if (dim == info->ref->u.ar.dimen - 1
b3aefde2 3221 && info->ref->u.ar.as->type == AS_ASSUMED_SIZE)
c099916d
FXC
3222 check_upper = false;
3223 else
3224 check_upper = true;
ef31fe62
FXC
3225
3226 /* Zero stride is not allowed. */
3227 tmp = fold_build2 (EQ_EXPR, boolean_type_node, info->stride[n],
3228 gfc_index_zero_node);
3229 asprintf (&msg, "Zero stride is not allowed, for dimension %d "
3230 "of array '%s'", info->dim[n]+1,
3231 ss->expr->symtree->name);
0d52899f
TB
3232 gfc_trans_runtime_check (true, false, tmp, &inner,
3233 &ss->expr->where, msg);
ef31fe62
FXC
3234 gfc_free (msg);
3235
c099916d
FXC
3236 desc = ss->data.info.descriptor;
3237
3238 /* This is the run-time equivalent of resolve.c's
3239 check_dimension(). The logical is more readable there
3240 than it is here, with all the trees. */
3241 lbound = gfc_conv_array_lbound (desc, dim);
3242 end = info->end[n];
3243 if (check_upper)
3244 ubound = gfc_conv_array_ubound (desc, dim);
3245 else
3246 ubound = NULL;
3247
ef31fe62
FXC
3248 /* non_zerosized is true when the selected range is not
3249 empty. */
3250 stride_pos = fold_build2 (GT_EXPR, boolean_type_node,
3251 info->stride[n], gfc_index_zero_node);
3252 tmp = fold_build2 (LE_EXPR, boolean_type_node, info->start[n],
3253 end);
3254 stride_pos = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3255 stride_pos, tmp);
3256
3257 stride_neg = fold_build2 (LT_EXPR, boolean_type_node,
3258 info->stride[n], gfc_index_zero_node);
3259 tmp = fold_build2 (GE_EXPR, boolean_type_node, info->start[n],
3260 end);
3261 stride_neg = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3262 stride_neg, tmp);
3263 non_zerosized = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
3264 stride_pos, stride_neg);
3265
3266 /* Check the start of the range against the lower and upper
c6ec7cc6
DW
3267 bounds of the array, if the range is not empty.
3268 If upper bound is present, include both bounds in the
3269 error message. */
c099916d
FXC
3270 if (check_upper)
3271 {
c6ec7cc6
DW
3272 tmp = fold_build2 (LT_EXPR, boolean_type_node,
3273 info->start[n], lbound);
c099916d
FXC
3274 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3275 non_zerosized, tmp);
c6ec7cc6
DW
3276 tmp2 = fold_build2 (GT_EXPR, boolean_type_node,
3277 info->start[n], ubound);
3278 tmp2 = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3279 non_zerosized, tmp2);
3280 asprintf (&msg, "Index '%%ld' of dimension %d of array '%s' "
3281 "outside of expected range (%%ld:%%ld)",
1954a27b 3282 info->dim[n]+1, ss->expr->symtree->name);
c6ec7cc6
DW
3283 gfc_trans_runtime_check (true, false, tmp, &inner,
3284 &ss->expr->where, msg,
3285 fold_convert (long_integer_type_node, info->start[n]),
3286 fold_convert (long_integer_type_node, lbound),
3287 fold_convert (long_integer_type_node, ubound));
3288 gfc_trans_runtime_check (true, false, tmp2, &inner,
3289 &ss->expr->where, msg,
3290 fold_convert (long_integer_type_node, info->start[n]),
3291 fold_convert (long_integer_type_node, lbound),
3292 fold_convert (long_integer_type_node, ubound));
c099916d
FXC
3293 gfc_free (msg);
3294 }
c6ec7cc6
DW
3295 else
3296 {
3297 tmp = fold_build2 (LT_EXPR, boolean_type_node,
3298 info->start[n], lbound);
3299 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3300 non_zerosized, tmp);
3301 asprintf (&msg, "Index '%%ld' of dimension %d of array '%s' "
3302 "below lower bound of %%ld",
3303 info->dim[n]+1, ss->expr->symtree->name);
3304 gfc_trans_runtime_check (true, false, tmp, &inner,
3305 &ss->expr->where, msg,
3306 fold_convert (long_integer_type_node, info->start[n]),
3307 fold_convert (long_integer_type_node, lbound));
3308 gfc_free (msg);
3309 }
3310
ef31fe62
FXC
3311 /* Compute the last element of the range, which is not
3312 necessarily "end" (think 0:5:3, which doesn't contain 5)
3313 and check it against both lower and upper bounds. */
c6ec7cc6
DW
3314
3315 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
ef31fe62 3316 info->start[n]);
c6ec7cc6 3317 tmp = fold_build2 (TRUNC_MOD_EXPR, gfc_array_index_type, tmp,
ef31fe62 3318 info->stride[n]);
c6ec7cc6
DW
3319 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
3320 tmp);
3321 tmp2 = fold_build2 (LT_EXPR, boolean_type_node, tmp, lbound);
3322 tmp2 = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3323 non_zerosized, tmp2);
c099916d
FXC
3324 if (check_upper)
3325 {
c6ec7cc6
DW
3326 tmp3 = fold_build2 (GT_EXPR, boolean_type_node, tmp, ubound);
3327 tmp3 = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3328 non_zerosized, tmp3);
3329 asprintf (&msg, "Index '%%ld' of dimension %d of array '%s' "
3330 "outside of expected range (%%ld:%%ld)",
1954a27b 3331 info->dim[n]+1, ss->expr->symtree->name);
c6ec7cc6
DW
3332 gfc_trans_runtime_check (true, false, tmp2, &inner,
3333 &ss->expr->where, msg,
3334 fold_convert (long_integer_type_node, tmp),
3335 fold_convert (long_integer_type_node, ubound),
3336 fold_convert (long_integer_type_node, lbound));
3337 gfc_trans_runtime_check (true, false, tmp3, &inner,
3338 &ss->expr->where, msg,
3339 fold_convert (long_integer_type_node, tmp),
3340 fold_convert (long_integer_type_node, ubound),
3341 fold_convert (long_integer_type_node, lbound));
c099916d
FXC
3342 gfc_free (msg);
3343 }
c6ec7cc6
DW
3344 else
3345 {
3346 asprintf (&msg, "Index '%%ld' of dimension %d of array '%s' "
3347 "below lower bound of %%ld",
3348 info->dim[n]+1, ss->expr->symtree->name);
3349 gfc_trans_runtime_check (true, false, tmp2, &inner,
3350 &ss->expr->where, msg,
3351 fold_convert (long_integer_type_node, tmp),
3352 fold_convert (long_integer_type_node, lbound));
3353 gfc_free (msg);
3354 }
3355
6de9cd9a 3356 /* Check the section sizes match. */
10c7a96f
SB
3357 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
3358 info->start[n]);
3359 tmp = fold_build2 (FLOOR_DIV_EXPR, gfc_array_index_type, tmp,
3360 info->stride[n]);
7ac61957
JD
3361 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
3362 gfc_index_one_node, tmp);
4c7382bb
FXC
3363 tmp = fold_build2 (MAX_EXPR, gfc_array_index_type, tmp,
3364 build_int_cst (gfc_array_index_type, 0));
6de9cd9a
DN
3365 /* We remember the size of the first section, and check all the
3366 others against this. */
3367 if (size[n])
3368 {
a50ba82d 3369 tmp3 = fold_build2 (NE_EXPR, boolean_type_node, tmp, size[n]);
6c559604
SK
3370 asprintf (&msg, "Array bound mismatch for dimension %d "
3371 "of array '%s' (%%ld/%%ld)",
c8fe94c7 3372 info->dim[n]+1, ss->expr->symtree->name);
6c559604 3373
0d52899f
TB
3374 gfc_trans_runtime_check (true, false, tmp3, &inner,
3375 &ss->expr->where, msg,
c8fe94c7
FXC
3376 fold_convert (long_integer_type_node, tmp),
3377 fold_convert (long_integer_type_node, size[n]));
6c559604 3378
dd18a33b 3379 gfc_free (msg);
6de9cd9a
DN
3380 }
3381 else
ba4698e1 3382 size[n] = gfc_evaluate_now (tmp, &inner);
6de9cd9a 3383 }
ba4698e1
FXC
3384
3385 tmp = gfc_finish_block (&inner);
3386
3387 /* For optional arguments, only check bounds if the argument is
3388 present. */
3389 if (ss->expr->symtree->n.sym->attr.optional
3390 || ss->expr->symtree->n.sym->attr.not_always_present)
3391 tmp = build3_v (COND_EXPR,
3392 gfc_conv_expr_present (ss->expr->symtree->n.sym),
c2255bc4 3393 tmp, build_empty_stmt (input_location));
ba4698e1
FXC
3394
3395 gfc_add_expr_to_block (&block, tmp);
3396
6de9cd9a 3397 }
6de9cd9a
DN
3398
3399 tmp = gfc_finish_block (&block);
3400 gfc_add_expr_to_block (&loop->pre, tmp);
3401 }
3402}
3403
3404
13795658 3405/* Return true if the two SS could be aliased, i.e. both point to the same data
6de9cd9a
DN
3406 object. */
3407/* TODO: resolve aliases based on frontend expressions. */
3408
3409static int
3410gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
3411{
3412 gfc_ref *lref;
3413 gfc_ref *rref;
3414 gfc_symbol *lsym;
3415 gfc_symbol *rsym;
3416
3417 lsym = lss->expr->symtree->n.sym;
3418 rsym = rss->expr->symtree->n.sym;
3419 if (gfc_symbols_could_alias (lsym, rsym))
3420 return 1;
3421
3422 if (rsym->ts.type != BT_DERIVED
3423 && lsym->ts.type != BT_DERIVED)
3424 return 0;
3425
13413760 3426 /* For derived types we must check all the component types. We can ignore
6de9cd9a
DN
3427 array references as these will have the same base type as the previous
3428 component ref. */
3429 for (lref = lss->expr->ref; lref != lss->data.info.ref; lref = lref->next)
3430 {
3431 if (lref->type != REF_COMPONENT)
3432 continue;
3433
3434 if (gfc_symbols_could_alias (lref->u.c.sym, rsym))
3435 return 1;
3436
3437 for (rref = rss->expr->ref; rref != rss->data.info.ref;
3438 rref = rref->next)
3439 {
3440 if (rref->type != REF_COMPONENT)
3441 continue;
3442
3443 if (gfc_symbols_could_alias (lref->u.c.sym, rref->u.c.sym))
3444 return 1;
3445 }
3446 }
3447
3448 for (rref = rss->expr->ref; rref != rss->data.info.ref; rref = rref->next)
3449 {
3450 if (rref->type != REF_COMPONENT)
3451 break;
3452
3453 if (gfc_symbols_could_alias (rref->u.c.sym, lsym))
3454 return 1;
3455 }
3456
3457 return 0;
3458}
3459
3460
3461/* Resolve array data dependencies. Creates a temporary if required. */
3462/* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
3463 dependency.c. */
3464
3465void
3466gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
3467 gfc_ss * rss)
3468{
3469 gfc_ss *ss;
3470 gfc_ref *lref;
3471 gfc_ref *rref;
6de9cd9a 3472 int nDepend = 0;
6de9cd9a
DN
3473
3474 loop->temp_ss = NULL;
6de9cd9a
DN
3475
3476 for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
3477 {
3478 if (ss->type != GFC_SS_SECTION)
3479 continue;
3480
7d1f1e61 3481 if (dest->expr->symtree->n.sym != ss->expr->symtree->n.sym)
6de9cd9a 3482 {
7d1f1e61
PT
3483 if (gfc_could_be_alias (dest, ss)
3484 || gfc_are_equivalenced_arrays (dest->expr, ss->expr))
3485 {
3486 nDepend = 1;
3487 break;
3488 }
6de9cd9a 3489 }
7d1f1e61 3490 else
6de9cd9a
DN
3491 {
3492 lref = dest->expr->ref;
3493 rref = ss->expr->ref;
3494
3495 nDepend = gfc_dep_resolver (lref, rref);
4f06d65b
PT
3496 if (nDepend == 1)
3497 break;
6de9cd9a
DN
3498#if 0
3499 /* TODO : loop shifting. */
3500 if (nDepend == 1)
3501 {
3502 /* Mark the dimensions for LOOP SHIFTING */
3503 for (n = 0; n < loop->dimen; n++)
3504 {
3505 int dim = dest->data.info.dim[n];
3506
3507 if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
3508 depends[n] = 2;
3509 else if (! gfc_is_same_range (&lref->u.ar,
3510 &rref->u.ar, dim, 0))
3511 depends[n] = 1;
3512 }
3513
13413760 3514 /* Put all the dimensions with dependencies in the
6de9cd9a
DN
3515 innermost loops. */
3516 dim = 0;
3517 for (n = 0; n < loop->dimen; n++)
3518 {
6e45f57b 3519 gcc_assert (loop->order[n] == n);
6de9cd9a
DN
3520 if (depends[n])
3521 loop->order[dim++] = n;
3522 }
6de9cd9a
DN
3523 for (n = 0; n < loop->dimen; n++)
3524 {
3525 if (! depends[n])
3526 loop->order[dim++] = n;
3527 }
3528
6e45f57b 3529 gcc_assert (dim == loop->dimen);
6de9cd9a
DN
3530 break;
3531 }
3532#endif
3533 }
3534 }
3535
3536 if (nDepend == 1)
3537 {
eca18fb4
AP
3538 tree base_type = gfc_typenode_for_spec (&dest->expr->ts);
3539 if (GFC_ARRAY_TYPE_P (base_type)
3540 || GFC_DESCRIPTOR_TYPE_P (base_type))
3541 base_type = gfc_get_element_type (base_type);
6de9cd9a
DN
3542 loop->temp_ss = gfc_get_ss ();
3543 loop->temp_ss->type = GFC_SS_TEMP;
eca18fb4 3544 loop->temp_ss->data.temp.type = base_type;
72caba17 3545 loop->temp_ss->string_length = dest->string_length;
6de9cd9a
DN
3546 loop->temp_ss->data.temp.dimen = loop->dimen;
3547 loop->temp_ss->next = gfc_ss_terminator;
3548 gfc_add_ss_to_loop (loop, loop->temp_ss);
3549 }
3550 else
3551 loop->temp_ss = NULL;
3552}
3553
3554
1f2959f0 3555/* Initialize the scalarization loop. Creates the loop variables. Determines
6de9cd9a
DN
3556 the range of the loop variables. Creates a temporary if required.
3557 Calculates how to transform from loop variables to array indices for each
3558 expression. Also generates code for scalar expressions which have been
f7b529fa 3559 moved outside the loop. */
6de9cd9a
DN
3560
3561void
bdfd2ff0 3562gfc_conv_loop_setup (gfc_loopinfo * loop, locus * where)
6de9cd9a
DN
3563{
3564 int n;
6de9cd9a
DN
3565 gfc_ss_info *info;
3566 gfc_ss_info *specinfo;
3567 gfc_ss *ss;
3568 tree tmp;
6de9cd9a 3569 gfc_ss *loopspec[GFC_MAX_DIMENSIONS];
ec25720b 3570 bool dynamic[GFC_MAX_DIMENSIONS];
6de9cd9a
DN
3571 mpz_t *cshape;
3572 mpz_t i;
3573
3574 mpz_init (i);
3575 for (n = 0; n < loop->dimen; n++)
3576 {
3577 loopspec[n] = NULL;
ec25720b 3578 dynamic[n] = false;
6de9cd9a
DN
3579 /* We use one SS term, and use that to determine the bounds of the
3580 loop for this dimension. We try to pick the simplest term. */
3581 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
3582 {
e9cfef64 3583 if (ss->shape)
6de9cd9a
DN
3584 {
3585 /* The frontend has worked out the size for us. */
45bc572c
MM
3586 if (!loopspec[n] || !loopspec[n]->shape
3587 || !integer_zerop (loopspec[n]->data.info.start[n]))
3588 /* Prefer zero-based descriptors if possible. */
3589 loopspec[n] = ss;
6de9cd9a
DN
3590 continue;
3591 }
3592
3593 if (ss->type == GFC_SS_CONSTRUCTOR)
3594 {
b7e75771 3595 gfc_constructor_base base;
e9cfef64 3596 /* An unknown size constructor will always be rank one.
40f20186 3597 Higher rank constructors will either have known shape,
e9cfef64 3598 or still be wrapped in a call to reshape. */
6e45f57b 3599 gcc_assert (loop->dimen == 1);
ec25720b
RS
3600
3601 /* Always prefer to use the constructor bounds if the size
3602 can be determined at compile time. Prefer not to otherwise,
3603 since the general case involves realloc, and it's better to
3604 avoid that overhead if possible. */
b7e75771
JD
3605 base = ss->expr->value.constructor;
3606 dynamic[n] = gfc_get_array_constructor_size (&i, base);
ec25720b
RS
3607 if (!dynamic[n] || !loopspec[n])
3608 loopspec[n] = ss;
6de9cd9a
DN
3609 continue;
3610 }
3611
fc90a8f2 3612 /* TODO: Pick the best bound if we have a choice between a
e9cfef64 3613 function and something else. */
fc90a8f2
PB
3614 if (ss->type == GFC_SS_FUNCTION)
3615 {
3616 loopspec[n] = ss;
3617 continue;
3618 }
3619
6de9cd9a
DN
3620 if (ss->type != GFC_SS_SECTION)
3621 continue;
3622
6de9cd9a
DN
3623 if (loopspec[n])
3624 specinfo = &loopspec[n]->data.info;
3625 else
3626 specinfo = NULL;
3627 info = &ss->data.info;
3628
ec25720b
RS
3629 if (!specinfo)
3630 loopspec[n] = ss;
6de9cd9a 3631 /* Criteria for choosing a loop specifier (most important first):
ec25720b 3632 doesn't need realloc
6de9cd9a
DN
3633 stride of one
3634 known stride
3635 known lower bound
3636 known upper bound
3637 */
ec25720b 3638 else if (loopspec[n]->type == GFC_SS_CONSTRUCTOR && dynamic[n])
6de9cd9a 3639 loopspec[n] = ss;
ec25720b
RS
3640 else if (integer_onep (info->stride[n])
3641 && !integer_onep (specinfo->stride[n]))
3642 loopspec[n] = ss;
3643 else if (INTEGER_CST_P (info->stride[n])
3644 && !INTEGER_CST_P (specinfo->stride[n]))
3645 loopspec[n] = ss;
3646 else if (INTEGER_CST_P (info->start[n])
3647 && !INTEGER_CST_P (specinfo->start[n]))
3648 loopspec[n] = ss;
3649 /* We don't work out the upper bound.
3650 else if (INTEGER_CST_P (info->finish[n])
3651 && ! INTEGER_CST_P (specinfo->finish[n]))
3652 loopspec[n] = ss; */
6de9cd9a
DN
3653 }
3654
ca39e6f2
FXC
3655 /* We should have found the scalarization loop specifier. If not,
3656 that's bad news. */
3657 gcc_assert (loopspec[n]);
6de9cd9a
DN
3658
3659 info = &loopspec[n]->data.info;
3660
3661 /* Set the extents of this range. */
e9cfef64 3662 cshape = loopspec[n]->shape;
6de9cd9a
DN
3663 if (cshape && INTEGER_CST_P (info->start[n])
3664 && INTEGER_CST_P (info->stride[n]))
3665 {
3666 loop->from[n] = info->start[n];
3667 mpz_set (i, cshape[n]);
3668 mpz_sub_ui (i, i, 1);
3669 /* To = from + (size - 1) * stride. */
3670 tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
3671 if (!integer_onep (info->stride[n]))
10c7a96f
SB
3672 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
3673 tmp, info->stride[n]);
3674 loop->to[n] = fold_build2 (PLUS_EXPR, gfc_array_index_type,
3675 loop->from[n], tmp);
6de9cd9a
DN
3676 }
3677 else
3678 {
3679 loop->from[n] = info->start[n];
3680 switch (loopspec[n]->type)
3681 {
3682 case GFC_SS_CONSTRUCTOR:
ec25720b
RS
3683 /* The upper bound is calculated when we expand the
3684 constructor. */
3685 gcc_assert (loop->to[n] == NULL_TREE);
6de9cd9a
DN
3686 break;
3687
3688 case GFC_SS_SECTION:
993ac38b
PT
3689 /* Use the end expression if it exists and is not constant,
3690 so that it is only evaluated once. */
3691 if (info->end[n] && !INTEGER_CST_P (info->end[n]))
3692 loop->to[n] = info->end[n];
3693 else
3694 loop->to[n] = gfc_conv_section_upper_bound (loopspec[n], n,
3695 &loop->pre);
6de9cd9a
DN
3696 break;
3697
fc90a8f2
PB
3698 case GFC_SS_FUNCTION:
3699 /* The loop bound will be set when we generate the call. */
6e45f57b 3700 gcc_assert (loop->to[n] == NULL_TREE);
fc90a8f2
PB
3701 break;
3702
6de9cd9a 3703 default:
6e45f57b 3704 gcc_unreachable ();
6de9cd9a
DN
3705 }
3706 }
3707
3708 /* Transform everything so we have a simple incrementing variable. */
3709 if (integer_onep (info->stride[n]))
7ab92584 3710 info->delta[n] = gfc_index_zero_node;
6de9cd9a
DN
3711 else
3712 {
3713 /* Set the delta for this section. */
3714 info->delta[n] = gfc_evaluate_now (loop->from[n], &loop->pre);
3715 /* Number of iterations is (end - start + step) / step.
3716 with start = 0, this simplifies to
3717 last = end / step;
3718 for (i = 0; i<=last; i++){...}; */
10c7a96f
SB
3719 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3720 loop->to[n], loop->from[n]);
4c7382bb 3721 tmp = fold_build2 (FLOOR_DIV_EXPR, gfc_array_index_type,
10c7a96f 3722 tmp, info->stride[n]);
4c7382bb
FXC
3723 tmp = fold_build2 (MAX_EXPR, gfc_array_index_type, tmp,
3724 build_int_cst (gfc_array_index_type, -1));
6de9cd9a
DN
3725 loop->to[n] = gfc_evaluate_now (tmp, &loop->pre);
3726 /* Make the loop variable start at 0. */
7ab92584 3727 loop->from[n] = gfc_index_zero_node;
6de9cd9a
DN
3728 }
3729 }
3730
fc90a8f2
PB
3731 /* Add all the scalar code that can be taken out of the loops.
3732 This may include calculating the loop bounds, so do it before
3733 allocating the temporary. */
bdfd2ff0 3734 gfc_add_loop_ss_code (loop, loop->ss, false, where);
fc90a8f2 3735
6de9cd9a
DN
3736 /* If we want a temporary then create it. */
3737 if (loop->temp_ss != NULL)
3738 {
6e45f57b 3739 gcc_assert (loop->temp_ss->type == GFC_SS_TEMP);
640670c7
PT
3740
3741 /* Make absolutely sure that this is a complete type. */
3742 if (loop->temp_ss->string_length)
3743 loop->temp_ss->data.temp.type
d393bbd7
FXC
3744 = gfc_get_character_type_len_for_eltype
3745 (TREE_TYPE (loop->temp_ss->data.temp.type),
3746 loop->temp_ss->string_length);
640670c7 3747
6de9cd9a 3748 tmp = loop->temp_ss->data.temp.type;
6de9cd9a
DN
3749 n = loop->temp_ss->data.temp.dimen;
3750 memset (&loop->temp_ss->data.info, 0, sizeof (gfc_ss_info));
3751 loop->temp_ss->type = GFC_SS_SECTION;
3752 loop->temp_ss->data.info.dimen = n;
8e119f1b 3753 gfc_trans_create_temp_array (&loop->pre, &loop->post, loop,
12f681a0
DK
3754 &loop->temp_ss->data.info, tmp, NULL_TREE,
3755 false, true, false, where);
6de9cd9a
DN
3756 }
3757
6de9cd9a
DN
3758 for (n = 0; n < loop->temp_dim; n++)
3759 loopspec[loop->order[n]] = NULL;
3760
3761 mpz_clear (i);
3762
3763 /* For array parameters we don't have loop variables, so don't calculate the
3764 translations. */
3765 if (loop->array_parameter)
3766 return;
3767
3768 /* Calculate the translation from loop variables to array indices. */
3769 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
3770 {
45bc572c
MM
3771 if (ss->type != GFC_SS_SECTION && ss->type != GFC_SS_COMPONENT
3772 && ss->type != GFC_SS_CONSTRUCTOR)
3773
6de9cd9a
DN
3774 continue;
3775
3776 info = &ss->data.info;
3777
3778 for (n = 0; n < info->dimen; n++)
3779 {
e9cfef64 3780 /* If we are specifying the range the delta is already set. */
6de9cd9a
DN
3781 if (loopspec[n] != ss)
3782 {
3783 /* Calculate the offset relative to the loop variable.
3784 First multiply by the stride. */
c96111c0
RS
3785 tmp = loop->from[n];
3786 if (!integer_onep (info->stride[n]))
3787 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
3788 tmp, info->stride[n]);
6de9cd9a
DN
3789
3790 /* Then subtract this from our starting value. */
10c7a96f
SB
3791 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3792 info->start[n], tmp);
6de9cd9a
DN
3793
3794 info->delta[n] = gfc_evaluate_now (tmp, &loop->pre);
3795 }
3796 }
3797 }
3798}
3799
3800
3801/* Fills in an array descriptor, and returns the size of the array. The size
3802 will be a simple_val, ie a variable or a constant. Also calculates the
1f2959f0 3803 offset of the base. Returns the size of the array.
6de9cd9a
DN
3804 {
3805 stride = 1;
3806 offset = 0;
3807 for (n = 0; n < rank; n++)
3808 {
3809 a.lbound[n] = specified_lower_bound;
3810 offset = offset + a.lbond[n] * stride;
3811 size = 1 - lbound;
3812 a.ubound[n] = specified_upper_bound;
3813 a.stride[n] = stride;
067feae3 3814 size = siz >= 0 ? ubound + size : 0; //size = ubound + 1 - lbound
6de9cd9a
DN
3815 stride = stride * size;
3816 }
3817 return (stride);
3818 } */
3819/*GCC ARRAYS*/
3820
3821static tree
f33beee9 3822gfc_array_init_size (tree descriptor, int rank, int corank, tree * poffset,
6de9cd9a
DN
3823 gfc_expr ** lower, gfc_expr ** upper,
3824 stmtblock_t * pblock)
3825{
3826 tree type;
3827 tree tmp;
3828 tree size;
3829 tree offset;
3830 tree stride;
3c86fb4e
TK
3831 tree cond;
3832 tree or_expr;
3833 tree thencase;
3834 tree elsecase;
3835 tree var;
3836 stmtblock_t thenblock;
3837 stmtblock_t elseblock;
6de9cd9a
DN
3838 gfc_expr *ubound;
3839 gfc_se se;
3840 int n;
3841
3842 type = TREE_TYPE (descriptor);
3843
7ab92584
SB
3844 stride = gfc_index_one_node;
3845 offset = gfc_index_zero_node;
6de9cd9a
DN
3846
3847 /* Set the dtype. */
3848 tmp = gfc_conv_descriptor_dtype (descriptor);
726a989a 3849 gfc_add_modify (pblock, tmp, gfc_get_dtype (TREE_TYPE (descriptor)));
6de9cd9a 3850
3c86fb4e
TK
3851 or_expr = NULL_TREE;
3852
6de9cd9a
DN
3853 for (n = 0; n < rank; n++)
3854 {
3855 /* We have 3 possibilities for determining the size of the array:
3856 lower == NULL => lbound = 1, ubound = upper[n]
3857 upper[n] = NULL => lbound = 1, ubound = lower[n]
3858 upper[n] != NULL => lbound = lower[n], ubound = upper[n] */
3859 ubound = upper[n];
3860
3861 /* Set lower bound. */
3862 gfc_init_se (&se, NULL);
3863 if (lower == NULL)
7ab92584 3864 se.expr = gfc_index_one_node;
6de9cd9a
DN
3865 else
3866 {
6e45f57b 3867 gcc_assert (lower[n]);
6de9cd9a
DN
3868 if (ubound)
3869 {
3870 gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
3871 gfc_add_block_to_block (pblock, &se.pre);
3872 }
3873 else
3874 {
7ab92584 3875 se.expr = gfc_index_one_node;
6de9cd9a
DN
3876 ubound = lower[n];
3877 }
3878 }
568e8e1e
PT
3879 gfc_conv_descriptor_lbound_set (pblock, descriptor, gfc_rank_cst[n],
3880 se.expr);
6de9cd9a
DN
3881
3882 /* Work out the offset for this component. */
10c7a96f
SB
3883 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, se.expr, stride);
3884 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
6de9cd9a
DN
3885
3886 /* Start the calculation for the size of this dimension. */
44855d8c
TS
3887 size = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3888 gfc_index_one_node, se.expr);
6de9cd9a
DN
3889
3890 /* Set upper bound. */
3891 gfc_init_se (&se, NULL);
6e45f57b 3892 gcc_assert (ubound);
6de9cd9a
DN
3893 gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
3894 gfc_add_block_to_block (pblock, &se.pre);
3895
568e8e1e 3896 gfc_conv_descriptor_ubound_set (pblock, descriptor, gfc_rank_cst[n], se.expr);
6de9cd9a
DN
3897
3898 /* Store the stride. */
568e8e1e 3899 gfc_conv_descriptor_stride_set (pblock, descriptor, gfc_rank_cst[n], stride);
6de9cd9a
DN
3900
3901 /* Calculate the size of this dimension. */
10c7a96f 3902 size = fold_build2 (PLUS_EXPR, gfc_array_index_type, se.expr, size);
6de9cd9a 3903
dbfd1e01 3904 /* Check whether the size for this dimension is negative. */
3c86fb4e
TK
3905 cond = fold_build2 (LE_EXPR, boolean_type_node, size,
3906 gfc_index_zero_node);
3907 if (n == 0)
3908 or_expr = cond;
3909 else
3910 or_expr = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, or_expr, cond);
3911
067feae3
PT
3912 size = fold_build3 (COND_EXPR, gfc_array_index_type, cond,
3913 gfc_index_zero_node, size);
3914
6de9cd9a 3915 /* Multiply the stride by the number of elements in this dimension. */
10c7a96f 3916 stride = fold_build2 (MULT_EXPR, gfc_array_index_type, stride, size);
6de9cd9a
DN
3917 stride = gfc_evaluate_now (stride, pblock);
3918 }
3919
f33beee9
TB
3920 for (n = rank; n < rank + corank; n++)
3921 {
3922 ubound = upper[n];
3923
3924 /* Set lower bound. */
3925 gfc_init_se (&se, NULL);
3926 if (lower == NULL || lower[n] == NULL)
3927 {
3928 gcc_assert (n == rank + corank - 1);
3929 se.expr = gfc_index_one_node;
3930 }
3931 else
3932 {
3933 if (ubound || n == rank + corank - 1)
3934 {
3935 gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
3936 gfc_add_block_to_block (pblock, &se.pre);
3937 }
3938 else
3939 {
3940 se.expr = gfc_index_one_node;
3941 ubound = lower[n];
3942 }
3943 }
3944 gfc_conv_descriptor_lbound_set (pblock, descriptor, gfc_rank_cst[n],
3945 se.expr);
3946
3947 if (n < rank + corank - 1)
3948 {
3949 gfc_init_se (&se, NULL);
3950 gcc_assert (ubound);
3951 gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
3952 gfc_add_block_to_block (pblock, &se.pre);
3953 gfc_conv_descriptor_ubound_set (pblock, descriptor, gfc_rank_cst[n], se.expr);
3954 }
3955 }
3956
6de9cd9a
DN
3957 /* The stride is the number of elements in the array, so multiply by the
3958 size of an element to get the total size. */
3959 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
7c57b2f1
FXC
3960 size = fold_build2 (MULT_EXPR, gfc_array_index_type, stride,
3961 fold_convert (gfc_array_index_type, tmp));
6de9cd9a
DN
3962
3963 if (poffset != NULL)
3964 {
3965 offset = gfc_evaluate_now (offset, pblock);
3966 *poffset = offset;
3967 }
3968
fcac9229
RS
3969 if (integer_zerop (or_expr))
3970 return size;
3971 if (integer_onep (or_expr))
3972 return gfc_index_zero_node;
3973
3c86fb4e
TK
3974 var = gfc_create_var (TREE_TYPE (size), "size");
3975 gfc_start_block (&thenblock);
726a989a 3976 gfc_add_modify (&thenblock, var, gfc_index_zero_node);
3c86fb4e
TK
3977 thencase = gfc_finish_block (&thenblock);
3978
3979 gfc_start_block (&elseblock);
726a989a 3980 gfc_add_modify (&elseblock, var, size);
3c86fb4e
TK
3981 elsecase = gfc_finish_block (&elseblock);
3982
3983 tmp = gfc_evaluate_now (or_expr, pblock);
3984 tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
3985 gfc_add_expr_to_block (pblock, tmp);
3986
3987 return var;
6de9cd9a
DN
3988}
3989
3990
1f2959f0 3991/* Initializes the descriptor and generates a call to _gfor_allocate. Does
6de9cd9a
DN
3992 the work for an ALLOCATE statement. */
3993/*GCC ARRAYS*/
3994
5b725b8d
TK
3995bool
3996gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree pstat)
6de9cd9a
DN
3997{
3998 tree tmp;
3999 tree pointer;
6de9cd9a
DN
4000 tree offset;
4001 tree size;
4002 gfc_expr **lower;
4003 gfc_expr **upper;
5046aff5 4004 gfc_ref *ref, *prev_ref = NULL;
f33beee9 4005 bool allocatable_array, coarray;
5b725b8d
TK
4006
4007 ref = expr->ref;
4008
4009 /* Find the last reference in the chain. */
4010 while (ref && ref->next != NULL)
4011 {
d3a9eea2
TB
4012 gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT
4013 || (ref->u.ar.dimen == 0 && ref->u.ar.codimen > 0));
5046aff5 4014 prev_ref = ref;
5b725b8d
TK
4015 ref = ref->next;
4016 }
4017
4018 if (ref == NULL || ref->type != REF_ARRAY)
4019 return false;
6de9cd9a 4020
f33beee9 4021 if (!prev_ref)
d3a9eea2 4022 {
f33beee9
TB
4023 allocatable_array = expr->symtree->n.sym->attr.allocatable;
4024 coarray = expr->symtree->n.sym->attr.codimension;
d3a9eea2 4025 }
f33beee9 4026 else
d3a9eea2 4027 {
f33beee9
TB
4028 allocatable_array = prev_ref->u.c.component->attr.allocatable;
4029 coarray = prev_ref->u.c.component->attr.codimension;
d3a9eea2
TB
4030 }
4031
f33beee9
TB
4032 /* Return if this is a scalar coarray. */
4033 if ((!prev_ref && !expr->symtree->n.sym->attr.dimension)
4034 || (prev_ref && !prev_ref->u.c.component->attr.dimension))
4035 {
4036 gcc_assert (coarray);
4037 return false;
4038 }
5046aff5 4039
6de9cd9a
DN
4040 /* Figure out the size of the array. */
4041 switch (ref->u.ar.type)
4042 {
4043 case AR_ELEMENT:
f33beee9
TB
4044 if (!coarray)
4045 {
4046 lower = NULL;
4047 upper = ref->u.ar.start;
4048 break;
4049 }
4050 /* Fall through. */
4051
4052 case AR_SECTION:
4053 lower = ref->u.ar.start;
4054 upper = ref->u.ar.end;
6de9cd9a
DN
4055 break;
4056
4057 case AR_FULL:
6e45f57b 4058 gcc_assert (ref->u.ar.as->type == AS_EXPLICIT);
6de9cd9a
DN
4059
4060 lower = ref->u.ar.as->lower;
4061 upper = ref->u.ar.as->upper;
4062 break;
4063
6de9cd9a 4064 default:
6e45f57b 4065 gcc_unreachable ();
6de9cd9a
DN
4066 break;
4067 }
4068
f33beee9
TB
4069 size = gfc_array_init_size (se->expr, ref->u.ar.as->rank,
4070 ref->u.ar.as->corank, &offset, lower, upper,
4071 &se->pre);
6de9cd9a
DN
4072
4073 /* Allocate memory to store the data. */
54200abb
RG
4074 pointer = gfc_conv_descriptor_data_get (se->expr);
4075 STRIP_NOPS (pointer);
6de9cd9a 4076
54200abb
RG
4077 /* The allocate_array variants take the old pointer as first argument. */
4078 if (allocatable_array)
f25a62a5 4079 tmp = gfc_allocate_array_with_status (&se->pre, pointer, size, pstat, expr);
5039610b 4080 else
4376b7cf 4081 tmp = gfc_allocate_with_status (&se->pre, size, pstat);
44855d8c 4082 tmp = fold_build2 (MODIFY_EXPR, void_type_node, pointer, tmp);
6de9cd9a
DN
4083 gfc_add_expr_to_block (&se->pre, tmp);
4084
568e8e1e 4085 gfc_conv_descriptor_offset_set (&se->pre, se->expr, offset);
5b725b8d 4086
5046aff5 4087 if (expr->ts.type == BT_DERIVED
bc21d315 4088 && expr->ts.u.derived->attr.alloc_comp)
5046aff5 4089 {
bc21d315 4090 tmp = gfc_nullify_alloc_comp (expr->ts.u.derived, se->expr,
5046aff5
PT
4091 ref->u.ar.as->rank);
4092 gfc_add_expr_to_block (&se->pre, tmp);
4093 }
4094
5b725b8d 4095 return true;
6de9cd9a
DN
4096}
4097
4098
4099/* Deallocate an array variable. Also used when an allocated variable goes
4100 out of scope. */
4101/*GCC ARRAYS*/
4102
4103tree
f25a62a5 4104gfc_array_deallocate (tree descriptor, tree pstat, gfc_expr* expr)
6de9cd9a
DN
4105{
4106 tree var;
4107 tree tmp;
4108 stmtblock_t block;
4109
4110 gfc_start_block (&block);
4111 /* Get a pointer to the data. */
54200abb
RG
4112 var = gfc_conv_descriptor_data_get (descriptor);
4113 STRIP_NOPS (var);
6de9cd9a
DN
4114
4115 /* Parameter is the address of the data component. */
f25a62a5 4116 tmp = gfc_deallocate_with_status (var, pstat, false, expr);
6de9cd9a
DN
4117 gfc_add_expr_to_block (&block, tmp);
4118
54200abb 4119 /* Zero the data pointer. */
44855d8c
TS
4120 tmp = fold_build2 (MODIFY_EXPR, void_type_node,
4121 var, build_int_cst (TREE_TYPE (var), 0));
54200abb
RG
4122 gfc_add_expr_to_block (&block, tmp);
4123
6de9cd9a
DN
4124 return gfc_finish_block (&block);
4125}
4126
4127
4128/* Create an array constructor from an initialization expression.
4129 We assume the frontend already did any expansions and conversions. */
4130
4131tree
4132gfc_conv_array_initializer (tree type, gfc_expr * expr)
4133{
4134 gfc_constructor *c;
6de9cd9a 4135 tree tmp;
6de9cd9a
DN
4136 gfc_se se;
4137 HOST_WIDE_INT hi;
4138 unsigned HOST_WIDE_INT lo;
e5880243 4139 tree index;
4038c495 4140 VEC(constructor_elt,gc) *v = NULL;
6de9cd9a 4141
6de9cd9a
DN
4142 switch (expr->expr_type)
4143 {
4144 case EXPR_CONSTANT:
4145 case EXPR_STRUCTURE:
4146 /* A single scalar or derived type value. Create an array with all
4147 elements equal to that value. */
4148 gfc_init_se (&se, NULL);
e9cfef64
PB
4149
4150 if (expr->expr_type == EXPR_CONSTANT)
4151 gfc_conv_constant (&se, expr);
4152 else
4153 gfc_conv_structure (&se, expr, 1);
6de9cd9a
DN
4154
4155 tmp = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
6e45f57b 4156 gcc_assert (tmp && INTEGER_CST_P (tmp));
6de9cd9a
DN
4157 hi = TREE_INT_CST_HIGH (tmp);
4158 lo = TREE_INT_CST_LOW (tmp);
4159 lo++;
4160 if (lo == 0)
4161 hi++;
4162 /* This will probably eat buckets of memory for large arrays. */
4163 while (hi != 0 || lo != 0)
4164 {
4038c495 4165 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, se.expr);
6de9cd9a
DN
4166 if (lo == 0)
4167 hi--;
4168 lo--;
4169 }
4170 break;
4171
4172 case EXPR_ARRAY:
4038c495 4173 /* Create a vector of all the elements. */
b7e75771
JD
4174 for (c = gfc_constructor_first (expr->value.constructor);
4175 c; c = gfc_constructor_next (c))
6de9cd9a
DN
4176 {
4177 if (c->iterator)
4178 {
4179 /* Problems occur when we get something like
63346ddb 4180 integer :: a(lots) = (/(i, i=1, lots)/) */
f2ff577a
JD
4181 gfc_fatal_error ("The number of elements in the array constructor "
4182 "at %L requires an increase of the allowed %d "
4183 "upper limit. See -fmax-array-constructor "
4184 "option", &expr->where,
4185 gfc_option.flag_max_array_constructor);
63346ddb 4186 return NULL_TREE;
6de9cd9a 4187 }
b7e75771
JD
4188 if (mpz_cmp_si (c->offset, 0) != 0)
4189 index = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
6de9cd9a
DN
4190 else
4191 index = NULL_TREE;
6de9cd9a
DN
4192
4193 gfc_init_se (&se, NULL);
4194 switch (c->expr->expr_type)
4195 {
4196 case EXPR_CONSTANT:
4197 gfc_conv_constant (&se, c->expr);
e5880243 4198 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
6de9cd9a
DN
4199 break;
4200
4201 case EXPR_STRUCTURE:
4202 gfc_conv_structure (&se, c->expr, 1);
4038c495 4203 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
6de9cd9a
DN
4204 break;
4205
c1cfed03 4206
6de9cd9a 4207 default:
c1cfed03
PT
4208 /* Catch those occasional beasts that do not simplify
4209 for one reason or another, assuming that if they are
4210 standard defying the frontend will catch them. */
4211 gfc_conv_expr (&se, c->expr);
e5880243 4212 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
c1cfed03 4213 break;
6de9cd9a
DN
4214 }
4215 }
6de9cd9a
DN
4216 break;
4217
5046aff5
PT
4218 case EXPR_NULL:
4219 return gfc_build_null_descriptor (type);
4220
6de9cd9a 4221 default:
6e45f57b 4222 gcc_unreachable ();
6de9cd9a
DN
4223 }
4224
4225 /* Create a constructor from the list of elements. */
4038c495 4226 tmp = build_constructor (type, v);
6de9cd9a 4227 TREE_CONSTANT (tmp) = 1;
6de9cd9a
DN
4228 return tmp;
4229}
4230
4231
4232/* Generate code to evaluate non-constant array bounds. Sets *poffset and
4233 returns the size (in elements) of the array. */
4234
4235static tree
4236gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
4237 stmtblock_t * pblock)
4238{
4239 gfc_array_spec *as;
4240 tree size;
4241 tree stride;
4242 tree offset;
4243 tree ubound;
4244 tree lbound;
4245 tree tmp;
4246 gfc_se se;
4247
4248 int dim;
4249
4250 as = sym->as;
4251
7ab92584
SB
4252 size = gfc_index_one_node;
4253 offset = gfc_index_zero_node;
6de9cd9a
DN
4254 for (dim = 0; dim < as->rank; dim++)
4255 {
4256 /* Evaluate non-constant array bound expressions. */
4257 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
4258 if (as->lower[dim] && !INTEGER_CST_P (lbound))
4259 {
4260 gfc_init_se (&se, NULL);
4261 gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
4262 gfc_add_block_to_block (pblock, &se.pre);
726a989a 4263 gfc_add_modify (pblock, lbound, se.expr);
6de9cd9a
DN
4264 }
4265 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
4266 if (as->upper[dim] && !INTEGER_CST_P (ubound))
4267 {
4268 gfc_init_se (&se, NULL);
4269 gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
4270 gfc_add_block_to_block (pblock, &se.pre);
726a989a 4271 gfc_add_modify (pblock, ubound, se.expr);
6de9cd9a 4272 }
f7b529fa 4273 /* The offset of this dimension. offset = offset - lbound * stride. */
10c7a96f
SB
4274 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, lbound, size);
4275 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
6de9cd9a
DN
4276
4277 /* The size of this dimension, and the stride of the next. */
4278 if (dim + 1 < as->rank)
4279 stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
4280 else
417ab240 4281 stride = GFC_TYPE_ARRAY_SIZE (type);
6de9cd9a
DN
4282
4283 if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
4284 {
4285 /* Calculate stride = size * (ubound + 1 - lbound). */
10c7a96f
SB
4286 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4287 gfc_index_one_node, lbound);
4288 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, ubound, tmp);
4289 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
6de9cd9a 4290 if (stride)
726a989a 4291 gfc_add_modify (pblock, stride, tmp);
6de9cd9a
DN
4292 else
4293 stride = gfc_evaluate_now (tmp, pblock);
5b440a1c
PT
4294
4295 /* Make sure that negative size arrays are translated
4296 to being zero size. */
44855d8c
TS
4297 tmp = fold_build2 (GE_EXPR, boolean_type_node,
4298 stride, gfc_index_zero_node);
4299 tmp = fold_build3 (COND_EXPR, gfc_array_index_type, tmp,
4300 stride, gfc_index_zero_node);
726a989a 4301 gfc_add_modify (pblock, stride, tmp);
6de9cd9a
DN
4302 }
4303
4304 size = stride;
4305 }
4306
417ab240
JJ
4307 gfc_trans_vla_type_sizes (sym, pblock);
4308
6de9cd9a
DN
4309 *poffset = offset;
4310 return size;
4311}
4312
4313
4314/* Generate code to initialize/allocate an array variable. */
4315
4316tree
4317gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym, tree fnbody)
4318{
4319 stmtblock_t block;
4320 tree type;
4321 tree tmp;
6de9cd9a
DN
4322 tree size;
4323 tree offset;
6de9cd9a
DN
4324 bool onstack;
4325
6e45f57b 4326 gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
6de9cd9a
DN
4327
4328 /* Do nothing for USEd variables. */
4329 if (sym->attr.use_assoc)
4330 return fnbody;
4331
4332 type = TREE_TYPE (decl);
6e45f57b 4333 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a
DN
4334 onstack = TREE_CODE (type) != POINTER_TYPE;
4335
6de9cd9a
DN
4336 gfc_start_block (&block);
4337
4338 /* Evaluate character string length. */
4339 if (sym->ts.type == BT_CHARACTER
bc21d315 4340 && onstack && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
6de9cd9a 4341 {
bc21d315 4342 gfc_conv_string_length (sym->ts.u.cl, NULL, &block);
6de9cd9a 4343
417ab240
JJ
4344 gfc_trans_vla_type_sizes (sym, &block);
4345
1a186ec5 4346 /* Emit a DECL_EXPR for this variable, which will cause the
13795658 4347 gimplifier to allocate storage, and all that good stuff. */
44855d8c 4348 tmp = fold_build1 (DECL_EXPR, TREE_TYPE (decl), decl);
6de9cd9a
DN
4349 gfc_add_expr_to_block (&block, tmp);
4350 }
4351
4352 if (onstack)
4353 {
6de9cd9a
DN
4354 gfc_add_expr_to_block (&block, fnbody);
4355 return gfc_finish_block (&block);
4356 }
4357
4358 type = TREE_TYPE (type);
4359
6e45f57b
PB
4360 gcc_assert (!sym->attr.use_assoc);
4361 gcc_assert (!TREE_STATIC (decl));
cb9e4f55 4362 gcc_assert (!sym->module);
6de9cd9a
DN
4363
4364 if (sym->ts.type == BT_CHARACTER
bc21d315
JW
4365 && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
4366 gfc_conv_string_length (sym->ts.u.cl, NULL, &block);
6de9cd9a
DN
4367
4368 size = gfc_trans_array_bounds (type, sym, &offset, &block);
4369
83d890b9
AL
4370 /* Don't actually allocate space for Cray Pointees. */
4371 if (sym->attr.cray_pointee)
4372 {
4373 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
726a989a 4374 gfc_add_modify (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
83d890b9
AL
4375 gfc_add_expr_to_block (&block, fnbody);
4376 return gfc_finish_block (&block);
4377 }
4378
6de9cd9a
DN
4379 /* The size is the number of elements in the array, so multiply by the
4380 size of an element to get the total size. */
4381 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
61f70bf2
FXC
4382 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size,
4383 fold_convert (gfc_array_index_type, tmp));
6de9cd9a
DN
4384
4385 /* Allocate memory to hold the data. */
1529b8d9 4386 tmp = gfc_call_malloc (&block, TREE_TYPE (decl), size);
726a989a 4387 gfc_add_modify (&block, decl, tmp);
6de9cd9a
DN
4388
4389 /* Set offset of the array. */
4390 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
726a989a 4391 gfc_add_modify (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
6de9cd9a
DN
4392
4393
4394 /* Automatic arrays should not have initializers. */
6e45f57b 4395 gcc_assert (!sym->value);
6de9cd9a
DN
4396
4397 gfc_add_expr_to_block (&block, fnbody);
4398
4399 /* Free the temporary. */
1529b8d9 4400 tmp = gfc_call_free (convert (pvoid_type_node, decl));
6de9cd9a
DN
4401 gfc_add_expr_to_block (&block, tmp);
4402
4403 return gfc_finish_block (&block);
4404}
4405
4406
4407/* Generate entry and exit code for g77 calling convention arrays. */
4408
4409tree
4410gfc_trans_g77_array (gfc_symbol * sym, tree body)
4411{
4412 tree parm;
4413 tree type;
4414 locus loc;
4415 tree offset;
4416 tree tmp;
54129a64 4417 tree stmt;
6de9cd9a
DN
4418 stmtblock_t block;
4419
4420 gfc_get_backend_locus (&loc);
4421 gfc_set_backend_locus (&sym->declared_at);
4422
4423 /* Descriptor type. */
4424 parm = sym->backend_decl;
4425 type = TREE_TYPE (parm);
6e45f57b 4426 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a
DN
4427
4428 gfc_start_block (&block);
4429
4430 if (sym->ts.type == BT_CHARACTER
bc21d315
JW
4431 && TREE_CODE (sym->ts.u.cl->backend_decl) == VAR_DECL)
4432 gfc_conv_string_length (sym->ts.u.cl, NULL, &block);
6de9cd9a
DN
4433
4434 /* Evaluate the bounds of the array. */
4435 gfc_trans_array_bounds (type, sym, &offset, &block);
4436
4437 /* Set the offset. */
4438 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
726a989a 4439 gfc_add_modify (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
6de9cd9a 4440
1f2959f0 4441 /* Set the pointer itself if we aren't using the parameter directly. */
6de9cd9a
DN
4442 if (TREE_CODE (parm) != PARM_DECL)
4443 {
4444 tmp = convert (TREE_TYPE (parm), GFC_DECL_SAVED_DESCRIPTOR (parm));
726a989a 4445 gfc_add_modify (&block, parm, tmp);
6de9cd9a 4446 }
54129a64 4447 stmt = gfc_finish_block (&block);
6de9cd9a
DN
4448
4449 gfc_set_backend_locus (&loc);
4450
4451 gfc_start_block (&block);
54129a64 4452
6de9cd9a 4453 /* Add the initialization code to the start of the function. */
54129a64
PT
4454
4455 if (sym->attr.optional || sym->attr.not_always_present)
4456 {
4457 tmp = gfc_conv_expr_present (sym);
c2255bc4 4458 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
54129a64
PT
4459 }
4460
4461 gfc_add_expr_to_block (&block, stmt);
6de9cd9a
DN
4462 gfc_add_expr_to_block (&block, body);
4463
4464 return gfc_finish_block (&block);
4465}
4466
4467
4468/* Modify the descriptor of an array parameter so that it has the
4469 correct lower bound. Also move the upper bound accordingly.
4470 If the array is not packed, it will be copied into a temporary.
4471 For each dimension we set the new lower and upper bounds. Then we copy the
4472 stride and calculate the offset for this dimension. We also work out
4473 what the stride of a packed array would be, and see it the two match.
4474 If the array need repacking, we set the stride to the values we just
4475 calculated, recalculate the offset and copy the array data.
4476 Code is also added to copy the data back at the end of the function.
4477 */
4478
4479tree
4480gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc, tree body)
4481{
4482 tree size;
4483 tree type;
4484 tree offset;
4485 locus loc;
4486 stmtblock_t block;
4487 stmtblock_t cleanup;
4488 tree lbound;
4489 tree ubound;
4490 tree dubound;
4491 tree dlbound;
4492 tree dumdesc;
4493 tree tmp;
4494 tree stmt;
e8300d6e 4495 tree stride, stride2;
6de9cd9a
DN
4496 tree stmt_packed;
4497 tree stmt_unpacked;
4498 tree partial;
4499 gfc_se se;
4500 int n;
4501 int checkparm;
4502 int no_repack;
3d79abbd 4503 bool optional_arg;
6de9cd9a 4504
fc90a8f2
PB
4505 /* Do nothing for pointer and allocatable arrays. */
4506 if (sym->attr.pointer || sym->attr.allocatable)
4507 return body;
4508
6de9cd9a
DN
4509 if (sym->attr.dummy && gfc_is_nodesc_array (sym))
4510 return gfc_trans_g77_array (sym, body);
4511
4512 gfc_get_backend_locus (&loc);
4513 gfc_set_backend_locus (&sym->declared_at);
4514
4515 /* Descriptor type. */
4516 type = TREE_TYPE (tmpdesc);
6e45f57b 4517 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a 4518 dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
db3927fb
AH
4519 dumdesc = build_fold_indirect_ref_loc (input_location,
4520 dumdesc);
6de9cd9a
DN
4521 gfc_start_block (&block);
4522
4523 if (sym->ts.type == BT_CHARACTER
bc21d315
JW
4524 && TREE_CODE (sym->ts.u.cl->backend_decl) == VAR_DECL)
4525 gfc_conv_string_length (sym->ts.u.cl, NULL, &block);
6de9cd9a 4526
d3d3011f
FXC
4527 checkparm = (sym->as->type == AS_EXPLICIT
4528 && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS));
6de9cd9a
DN
4529
4530 no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
4531 || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
4532
4533 if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
4534 {
4535 /* For non-constant shape arrays we only check if the first dimension
4536 is contiguous. Repacking higher dimensions wouldn't gain us
4537 anything as we still don't know the array stride. */
4538 partial = gfc_create_var (boolean_type_node, "partial");
4539 TREE_USED (partial) = 1;
568e8e1e 4540 tmp = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
37da9343 4541 tmp = fold_build2 (EQ_EXPR, boolean_type_node, tmp, gfc_index_one_node);
726a989a 4542 gfc_add_modify (&block, partial, tmp);
6de9cd9a
DN
4543 }
4544 else
4545 {
4546 partial = NULL_TREE;
4547 }
4548
4549 /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
4550 here, however I think it does the right thing. */
4551 if (no_repack)
4552 {
4553 /* Set the first stride. */
568e8e1e 4554 stride = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
6de9cd9a
DN
4555 stride = gfc_evaluate_now (stride, &block);
4556
44855d8c
TS
4557 tmp = fold_build2 (EQ_EXPR, boolean_type_node,
4558 stride, gfc_index_zero_node);
4559 tmp = fold_build3 (COND_EXPR, gfc_array_index_type, tmp,
4560 gfc_index_one_node, stride);
6de9cd9a 4561 stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
726a989a 4562 gfc_add_modify (&block, stride, tmp);
6de9cd9a
DN
4563
4564 /* Allow the user to disable array repacking. */
4565 stmt_unpacked = NULL_TREE;
4566 }
4567 else
4568 {
6e45f57b 4569 gcc_assert (integer_onep (GFC_TYPE_ARRAY_STRIDE (type, 0)));
1f2959f0 4570 /* A library call to repack the array if necessary. */
6de9cd9a 4571 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
db3927fb
AH
4572 stmt_unpacked = build_call_expr_loc (input_location,
4573 gfor_fndecl_in_pack, 1, tmp);
6de9cd9a 4574
7ab92584 4575 stride = gfc_index_one_node;
bdfd2ff0
TK
4576
4577 if (gfc_option.warn_array_temp)
4578 gfc_warning ("Creating array temporary at %L", &loc);
6de9cd9a
DN
4579 }
4580
4581 /* This is for the case where the array data is used directly without
4582 calling the repack function. */
4583 if (no_repack || partial != NULL_TREE)
4c73896d 4584 stmt_packed = gfc_conv_descriptor_data_get (dumdesc);
6de9cd9a
DN
4585 else
4586 stmt_packed = NULL_TREE;
4587
4588 /* Assign the data pointer. */
4589 if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
4590 {
4591 /* Don't repack unknown shape arrays when the first stride is 1. */
44855d8c
TS
4592 tmp = fold_build3 (COND_EXPR, TREE_TYPE (stmt_packed),
4593 partial, stmt_packed, stmt_unpacked);
6de9cd9a
DN
4594 }
4595 else
4596 tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
726a989a 4597 gfc_add_modify (&block, tmpdesc, fold_convert (type, tmp));
6de9cd9a 4598
7ab92584
SB
4599 offset = gfc_index_zero_node;
4600 size = gfc_index_one_node;
6de9cd9a
DN
4601
4602 /* Evaluate the bounds of the array. */
4603 for (n = 0; n < sym->as->rank; n++)
4604 {
4605 if (checkparm || !sym->as->upper[n])
4606 {
4607 /* Get the bounds of the actual parameter. */
568e8e1e
PT
4608 dubound = gfc_conv_descriptor_ubound_get (dumdesc, gfc_rank_cst[n]);
4609 dlbound = gfc_conv_descriptor_lbound_get (dumdesc, gfc_rank_cst[n]);
6de9cd9a
DN
4610 }
4611 else
4612 {
4613 dubound = NULL_TREE;
4614 dlbound = NULL_TREE;
4615 }
4616
4617 lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
4618 if (!INTEGER_CST_P (lbound))
4619 {
4620 gfc_init_se (&se, NULL);
0df3cf7f 4621 gfc_conv_expr_type (&se, sym->as->lower[n],
6de9cd9a
DN
4622 gfc_array_index_type);
4623 gfc_add_block_to_block (&block, &se.pre);
726a989a 4624 gfc_add_modify (&block, lbound, se.expr);
6de9cd9a
DN
4625 }
4626
4627 ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
4628 /* Set the desired upper bound. */
4629 if (sym->as->upper[n])
4630 {
4631 /* We know what we want the upper bound to be. */
4632 if (!INTEGER_CST_P (ubound))
4633 {
4634 gfc_init_se (&se, NULL);
4635 gfc_conv_expr_type (&se, sym->as->upper[n],
4636 gfc_array_index_type);
4637 gfc_add_block_to_block (&block, &se.pre);
726a989a 4638 gfc_add_modify (&block, ubound, se.expr);
6de9cd9a
DN
4639 }
4640
4641 /* Check the sizes match. */
4642 if (checkparm)
4643 {
4644 /* Check (ubound(a) - lbound(a) == ubound(b) - lbound(b)). */
dd18a33b 4645 char * msg;
6c559604 4646 tree temp;
6de9cd9a 4647
6c559604
SK
4648 temp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4649 ubound, lbound);
4650 temp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
4651 gfc_index_one_node, temp);
4652
4653 stride2 = fold_build2 (MINUS_EXPR, gfc_array_index_type,
44855d8c 4654 dubound, dlbound);
6c559604
SK
4655 stride2 = fold_build2 (PLUS_EXPR, gfc_array_index_type,
4656 gfc_index_one_node, stride2);
4657
4658 tmp = fold_build2 (NE_EXPR, gfc_array_index_type, temp, stride2);
4659 asprintf (&msg, "Dimension %d of array '%s' has extent "
4660 "%%ld instead of %%ld", n+1, sym->name);
4661
4662 gfc_trans_runtime_check (true, false, tmp, &block, &loc, msg,
4663 fold_convert (long_integer_type_node, temp),
4664 fold_convert (long_integer_type_node, stride2));
4665
dd18a33b 4666 gfc_free (msg);
6de9cd9a
DN
4667 }
4668 }
4669 else
4670 {
4671 /* For assumed shape arrays move the upper bound by the same amount
4672 as the lower bound. */
44855d8c
TS
4673 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4674 dubound, dlbound);
10c7a96f 4675 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, lbound);
726a989a 4676 gfc_add_modify (&block, ubound, tmp);
6de9cd9a 4677 }
f7b529fa 4678 /* The offset of this dimension. offset = offset - lbound * stride. */
10c7a96f
SB
4679 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, lbound, stride);
4680 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
6de9cd9a
DN
4681
4682 /* The size of this dimension, and the stride of the next. */
4683 if (n + 1 < sym->as->rank)
4684 {
4685 stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
4686
4687 if (no_repack || partial != NULL_TREE)
4688 {
4689 stmt_unpacked =
568e8e1e 4690 gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[n+1]);
6de9cd9a
DN
4691 }
4692
4693 /* Figure out the stride if not a known constant. */
4694 if (!INTEGER_CST_P (stride))
4695 {
4696 if (no_repack)
4697 stmt_packed = NULL_TREE;
4698 else
4699 {
4700 /* Calculate stride = size * (ubound + 1 - lbound). */
10c7a96f
SB
4701 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4702 gfc_index_one_node, lbound);
4703 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
4704 ubound, tmp);
4705 size = fold_build2 (MULT_EXPR, gfc_array_index_type,
4706 size, tmp);
6de9cd9a
DN
4707 stmt_packed = size;
4708 }
4709
4710 /* Assign the stride. */
4711 if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
44855d8c
TS
4712 tmp = fold_build3 (COND_EXPR, gfc_array_index_type, partial,
4713 stmt_unpacked, stmt_packed);
6de9cd9a
DN
4714 else
4715 tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
726a989a 4716 gfc_add_modify (&block, stride, tmp);
6de9cd9a
DN
4717 }
4718 }
417ab240
JJ
4719 else
4720 {
4721 stride = GFC_TYPE_ARRAY_SIZE (type);
4722
4723 if (stride && !INTEGER_CST_P (stride))
4724 {
4725 /* Calculate size = stride * (ubound + 1 - lbound). */
4726 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4727 gfc_index_one_node, lbound);
4728 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
4729 ubound, tmp);
4730 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
4731 GFC_TYPE_ARRAY_STRIDE (type, n), tmp);
726a989a 4732 gfc_add_modify (&block, stride, tmp);
417ab240
JJ
4733 }
4734 }
6de9cd9a
DN
4735 }
4736
4737 /* Set the offset. */
4738 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
726a989a 4739 gfc_add_modify (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
6de9cd9a 4740
417ab240
JJ
4741 gfc_trans_vla_type_sizes (sym, &block);
4742
6de9cd9a
DN
4743 stmt = gfc_finish_block (&block);
4744
4745 gfc_start_block (&block);
4746
4747 /* Only do the entry/initialization code if the arg is present. */
4748 dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
d198b59a
JJ
4749 optional_arg = (sym->attr.optional
4750 || (sym->ns->proc_name->attr.entry_master
4751 && sym->attr.dummy));
3d79abbd 4752 if (optional_arg)
6de9cd9a
DN
4753 {
4754 tmp = gfc_conv_expr_present (sym);
c2255bc4 4755 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
6de9cd9a
DN
4756 }
4757 gfc_add_expr_to_block (&block, stmt);
4758
4759 /* Add the main function body. */
4760 gfc_add_expr_to_block (&block, body);
4761
4762 /* Cleanup code. */
4763 if (!no_repack)
4764 {
4765 gfc_start_block (&cleanup);
4766
4767 if (sym->attr.intent != INTENT_IN)
4768 {
4769 /* Copy the data back. */
db3927fb
AH
4770 tmp = build_call_expr_loc (input_location,
4771 gfor_fndecl_in_unpack, 2, dumdesc, tmpdesc);
6de9cd9a
DN
4772 gfc_add_expr_to_block (&cleanup, tmp);
4773 }
4774
4775 /* Free the temporary. */
1529b8d9 4776 tmp = gfc_call_free (tmpdesc);
6de9cd9a
DN
4777 gfc_add_expr_to_block (&cleanup, tmp);
4778
4779 stmt = gfc_finish_block (&cleanup);
4780
4781 /* Only do the cleanup if the array was repacked. */
db3927fb
AH
4782 tmp = build_fold_indirect_ref_loc (input_location,
4783 dumdesc);
4c73896d 4784 tmp = gfc_conv_descriptor_data_get (tmp);
44855d8c 4785 tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, tmpdesc);
c2255bc4 4786 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
6de9cd9a 4787
3d79abbd 4788 if (optional_arg)
6de9cd9a
DN
4789 {
4790 tmp = gfc_conv_expr_present (sym);
c2255bc4
AH
4791 stmt = build3_v (COND_EXPR, tmp, stmt,
4792 build_empty_stmt (input_location));
6de9cd9a
DN
4793 }
4794 gfc_add_expr_to_block (&block, stmt);
4795 }
4796 /* We don't need to free any memory allocated by internal_pack as it will
4797 be freed at the end of the function by pop_context. */
4798 return gfc_finish_block (&block);
4799}
4800
4801
1d6b7f39
PT
4802/* Calculate the overall offset, including subreferences. */
4803static void
4804gfc_get_dataptr_offset (stmtblock_t *block, tree parm, tree desc, tree offset,
4805 bool subref, gfc_expr *expr)
4806{
4807 tree tmp;
4808 tree field;
4809 tree stride;
4810 tree index;
4811 gfc_ref *ref;
4812 gfc_se start;
4813 int n;
4814
4815 /* If offset is NULL and this is not a subreferenced array, there is
4816 nothing to do. */
4817 if (offset == NULL_TREE)
4818 {
4819 if (subref)
4820 offset = gfc_index_zero_node;
4821 else
4822 return;
4823 }
4824
4825 tmp = gfc_conv_array_data (desc);
db3927fb
AH
4826 tmp = build_fold_indirect_ref_loc (input_location,
4827 tmp);
1d6b7f39
PT
4828 tmp = gfc_build_array_ref (tmp, offset, NULL);
4829
4830 /* Offset the data pointer for pointer assignments from arrays with
df2fba9e 4831 subreferences; e.g. my_integer => my_type(:)%integer_component. */
1d6b7f39
PT
4832 if (subref)
4833 {
4834 /* Go past the array reference. */
4835 for (ref = expr->ref; ref; ref = ref->next)
4836 if (ref->type == REF_ARRAY &&
4837 ref->u.ar.type != AR_ELEMENT)
4838 {
4839 ref = ref->next;
4840 break;
4841 }
4842
4843 /* Calculate the offset for each subsequent subreference. */
4844 for (; ref; ref = ref->next)
4845 {
4846 switch (ref->type)
4847 {
4848 case REF_COMPONENT:
4849 field = ref->u.c.component->backend_decl;
4850 gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
44855d8c
TS
4851 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
4852 tmp, field, NULL_TREE);
1d6b7f39
PT
4853 break;
4854
4855 case REF_SUBSTRING:
4856 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
4857 gfc_init_se (&start, NULL);
4858 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
4859 gfc_add_block_to_block (block, &start.pre);
4860 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
4861 break;
4862
4863 case REF_ARRAY:
4864 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE
4865 && ref->u.ar.type == AR_ELEMENT);
4866
4867 /* TODO - Add bounds checking. */
4868 stride = gfc_index_one_node;
4869 index = gfc_index_zero_node;
4870 for (n = 0; n < ref->u.ar.dimen; n++)
4871 {
4872 tree itmp;
4873 tree jtmp;
4874
4875 /* Update the index. */
4876 gfc_init_se (&start, NULL);
4877 gfc_conv_expr_type (&start, ref->u.ar.start[n], gfc_array_index_type);
4878 itmp = gfc_evaluate_now (start.expr, block);
4879 gfc_init_se (&start, NULL);
4880 gfc_conv_expr_type (&start, ref->u.ar.as->lower[n], gfc_array_index_type);
4881 jtmp = gfc_evaluate_now (start.expr, block);
4882 itmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, itmp, jtmp);
4883 itmp = fold_build2 (MULT_EXPR, gfc_array_index_type, itmp, stride);
4884 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, itmp, index);
4885 index = gfc_evaluate_now (index, block);
4886
4887 /* Update the stride. */
4888 gfc_init_se (&start, NULL);
4889 gfc_conv_expr_type (&start, ref->u.ar.as->upper[n], gfc_array_index_type);
4890 itmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, start.expr, jtmp);
4891 itmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
4892 gfc_index_one_node, itmp);
4893 stride = fold_build2 (MULT_EXPR, gfc_array_index_type, stride, itmp);
4894 stride = gfc_evaluate_now (stride, block);
4895 }
4896
4897 /* Apply the index to obtain the array element. */
4898 tmp = gfc_build_array_ref (tmp, index, NULL);
4899 break;
4900
4901 default:
4902 gcc_unreachable ();
4903 break;
4904 }
4905 }
4906 }
4907
4908 /* Set the target data pointer. */
4909 offset = gfc_build_addr_expr (gfc_array_dataptr_type (desc), tmp);
4910 gfc_conv_descriptor_data_set (block, parm, offset);
4911}
4912
4913
5d63a35f
PT
4914/* gfc_conv_expr_descriptor needs the string length an expression
4915 so that the size of the temporary can be obtained. This is done
4916 by adding up the string lengths of all the elements in the
4917 expression. Function with non-constant expressions have their
4918 string lengths mapped onto the actual arguments using the
4919 interface mapping machinery in trans-expr.c. */
0a164a3c 4920static void
5d63a35f 4921get_array_charlen (gfc_expr *expr, gfc_se *se)
0a164a3c
PT
4922{
4923 gfc_interface_mapping mapping;
4924 gfc_formal_arglist *formal;
4925 gfc_actual_arglist *arg;
4926 gfc_se tse;
4927
bc21d315
JW
4928 if (expr->ts.u.cl->length
4929 && gfc_is_constant_expr (expr->ts.u.cl->length))
0a164a3c 4930 {
bc21d315
JW
4931 if (!expr->ts.u.cl->backend_decl)
4932 gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
5d63a35f 4933 return;
0a164a3c
PT
4934 }
4935
5d63a35f
PT
4936 switch (expr->expr_type)
4937 {
4938 case EXPR_OP:
4939 get_array_charlen (expr->value.op.op1, se);
4940
bc21d315 4941 /* For parentheses the expression ts.u.cl is identical. */
5d63a35f
PT
4942 if (expr->value.op.op == INTRINSIC_PARENTHESES)
4943 return;
4944
bc21d315 4945 expr->ts.u.cl->backend_decl =
5d63a35f
PT
4946 gfc_create_var (gfc_charlen_type_node, "sln");
4947
4948 if (expr->value.op.op2)
4949 {
4950 get_array_charlen (expr->value.op.op2, se);
4951
71a7778c
PT
4952 gcc_assert (expr->value.op.op == INTRINSIC_CONCAT);
4953
5d63a35f
PT
4954 /* Add the string lengths and assign them to the expression
4955 string length backend declaration. */
bc21d315 4956 gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
5d63a35f 4957 fold_build2 (PLUS_EXPR, gfc_charlen_type_node,
bc21d315
JW
4958 expr->value.op.op1->ts.u.cl->backend_decl,
4959 expr->value.op.op2->ts.u.cl->backend_decl));
5d63a35f
PT
4960 }
4961 else
bc21d315
JW
4962 gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
4963 expr->value.op.op1->ts.u.cl->backend_decl);
5d63a35f
PT
4964 break;
4965
4966 case EXPR_FUNCTION:
4967 if (expr->value.function.esym == NULL
bc21d315 4968 || expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
5d63a35f 4969 {
bc21d315 4970 gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
5d63a35f
PT
4971 break;
4972 }
4973
4974 /* Map expressions involving the dummy arguments onto the actual
4975 argument expressions. */
4976 gfc_init_interface_mapping (&mapping);
4977 formal = expr->symtree->n.sym->formal;
4978 arg = expr->value.function.actual;
4979
4980 /* Set se = NULL in the calls to the interface mapping, to suppress any
4981 backend stuff. */
4982 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
4983 {
4984 if (!arg->expr)
4985 continue;
4986 if (formal->sym)
4987 gfc_add_interface_mapping (&mapping, formal->sym, NULL, arg->expr);
4988 }
4989
4990 gfc_init_se (&tse, NULL);
4991
4992 /* Build the expression for the character length and convert it. */
bc21d315 4993 gfc_apply_interface_mapping (&mapping, &tse, expr->ts.u.cl->length);
0a164a3c 4994
5d63a35f
PT
4995 gfc_add_block_to_block (&se->pre, &tse.pre);
4996 gfc_add_block_to_block (&se->post, &tse.post);
4997 tse.expr = fold_convert (gfc_charlen_type_node, tse.expr);
4998 tse.expr = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tse.expr,
4999 build_int_cst (gfc_charlen_type_node, 0));
bc21d315 5000 expr->ts.u.cl->backend_decl = tse.expr;
5d63a35f
PT
5001 gfc_free_interface_mapping (&mapping);
5002 break;
0a164a3c 5003
5d63a35f 5004 default:
bc21d315 5005 gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
5d63a35f
PT
5006 break;
5007 }
0a164a3c
PT
5008}
5009
5010
5d63a35f 5011
7a70c12d 5012/* Convert an array for passing as an actual argument. Expressions and
7ab92584 5013 vector subscripts are evaluated and stored in a temporary, which is then
6de9cd9a
DN
5014 passed. For whole arrays the descriptor is passed. For array sections
5015 a modified copy of the descriptor is passed, but using the original data.
7a70c12d
RS
5016
5017 This function is also used for array pointer assignments, and there
5018 are three cases:
5019
3e90ac4e 5020 - se->want_pointer && !se->direct_byref
7a70c12d
RS
5021 EXPR is an actual argument. On exit, se->expr contains a
5022 pointer to the array descriptor.
5023
3e90ac4e 5024 - !se->want_pointer && !se->direct_byref
7a70c12d
RS
5025 EXPR is an actual argument to an intrinsic function or the
5026 left-hand side of a pointer assignment. On exit, se->expr
5027 contains the descriptor for EXPR.
5028
3e90ac4e 5029 - !se->want_pointer && se->direct_byref
7a70c12d
RS
5030 EXPR is the right-hand side of a pointer assignment and
5031 se->expr is the descriptor for the previously-evaluated
5032 left-hand side. The function creates an assignment from
5033 EXPR to se->expr. */
6de9cd9a
DN
5034
5035void
5036gfc_conv_expr_descriptor (gfc_se * se, gfc_expr * expr, gfc_ss * ss)
5037{
5038 gfc_loopinfo loop;
5039 gfc_ss *secss;
5040 gfc_ss_info *info;
5041 int need_tmp;
5042 int n;
5043 tree tmp;
5044 tree desc;
5045 stmtblock_t block;
5046 tree start;
5047 tree offset;
5048 int full;
1d6b7f39 5049 bool subref_array_target = false;
6de9cd9a 5050
6e45f57b 5051 gcc_assert (ss != gfc_ss_terminator);
6de9cd9a 5052
fc90a8f2
PB
5053 /* Special case things we know we can pass easily. */
5054 switch (expr->expr_type)
6de9cd9a 5055 {
fc90a8f2
PB
5056 case EXPR_VARIABLE:
5057 /* If we have a linear array section, we can pass it directly.
5058 Otherwise we need to copy it into a temporary. */
6de9cd9a
DN
5059
5060 /* Find the SS for the array section. */
5061 secss = ss;
5062 while (secss != gfc_ss_terminator && secss->type != GFC_SS_SECTION)
5063 secss = secss->next;
5064
6e45f57b 5065 gcc_assert (secss != gfc_ss_terminator);
6de9cd9a
DN
5066 info = &secss->data.info;
5067
5068 /* Get the descriptor for the array. */
5069 gfc_conv_ss_descriptor (&se->pre, secss, 0);
5070 desc = info->descriptor;
7a70c12d 5071
1d6b7f39
PT
5072 subref_array_target = se->direct_byref && is_subref_array (expr);
5073 need_tmp = gfc_ref_needs_temporary_p (expr->ref)
5074 && !subref_array_target;
5075
7a70c12d
RS
5076 if (need_tmp)
5077 full = 0;
5078 else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
6de9cd9a
DN
5079 {
5080 /* Create a new descriptor if the array doesn't have one. */
5081 full = 0;
5082 }
5083 else if (info->ref->u.ar.type == AR_FULL)
5084 full = 1;
5085 else if (se->direct_byref)
5086 full = 0;
5087 else
a61a36ab 5088 full = gfc_full_array_ref_p (info->ref, NULL);
ca2940c3 5089
6de9cd9a
DN
5090 if (full)
5091 {
5092 if (se->direct_byref)
5093 {
5094 /* Copy the descriptor for pointer assignments. */
726a989a 5095 gfc_add_modify (&se->pre, se->expr, desc);
1d6b7f39
PT
5096
5097 /* Add any offsets from subreferences. */
5098 gfc_get_dataptr_offset (&se->pre, se->expr, desc, NULL_TREE,
5099 subref_array_target, expr);
6de9cd9a
DN
5100 }
5101 else if (se->want_pointer)
5102 {
5103 /* We pass full arrays directly. This means that pointers and
fc90a8f2 5104 allocatable arrays should also work. */
628c189e 5105 se->expr = gfc_build_addr_expr (NULL_TREE, desc);
6de9cd9a
DN
5106 }
5107 else
5108 {
5109 se->expr = desc;
5110 }
ca2940c3 5111
20c9dc8a 5112 if (expr->ts.type == BT_CHARACTER)
ca2940c3
TS
5113 se->string_length = gfc_get_expr_charlen (expr);
5114
6de9cd9a
DN
5115 return;
5116 }
fc90a8f2
PB
5117 break;
5118
5119 case EXPR_FUNCTION:
5120 /* A transformational function return value will be a temporary
5121 array descriptor. We still need to go through the scalarizer
5122 to create the descriptor. Elemental functions ar handled as
e7dc5b4f 5123 arbitrary expressions, i.e. copy to a temporary. */
fc90a8f2
PB
5124 secss = ss;
5125 /* Look for the SS for this function. */
5126 while (secss != gfc_ss_terminator
5127 && (secss->type != GFC_SS_FUNCTION || secss->expr != expr))
5128 secss = secss->next;
5129
5130 if (se->direct_byref)
5131 {
6e45f57b 5132 gcc_assert (secss != gfc_ss_terminator);
fc90a8f2
PB
5133
5134 /* For pointer assignments pass the descriptor directly. */
5135 se->ss = secss;
628c189e 5136 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
fc90a8f2
PB
5137 gfc_conv_expr (se, expr);
5138 return;
5139 }
5140
5141 if (secss == gfc_ss_terminator)
5142 {
5143 /* Elemental function. */
5144 need_tmp = 1;
0a164a3c 5145 if (expr->ts.type == BT_CHARACTER
bc21d315 5146 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5d63a35f 5147 get_array_charlen (expr, se);
0a164a3c 5148
fc90a8f2
PB
5149 info = NULL;
5150 }
5151 else
5152 {
5153 /* Transformational function. */
5154 info = &secss->data.info;
5155 need_tmp = 0;
5156 }
5157 break;
5158
114e4d10
RS
5159 case EXPR_ARRAY:
5160 /* Constant array constructors don't need a temporary. */
5161 if (ss->type == GFC_SS_CONSTRUCTOR
5162 && expr->ts.type != BT_CHARACTER
5163 && gfc_constant_array_constructor_p (expr->value.constructor))
5164 {
5165 need_tmp = 0;
5166 info = &ss->data.info;
5167 secss = ss;
5168 }
5169 else
5170 {
5171 need_tmp = 1;
5172 secss = NULL;
5173 info = NULL;
5174 }
5175 break;
5176
fc90a8f2
PB
5177 default:
5178 /* Something complicated. Copy it into a temporary. */
6de9cd9a
DN
5179 need_tmp = 1;
5180 secss = NULL;
5181 info = NULL;
fc90a8f2 5182 break;
6de9cd9a
DN
5183 }
5184
5185 gfc_init_loopinfo (&loop);
5186
5187 /* Associate the SS with the loop. */
5188 gfc_add_ss_to_loop (&loop, ss);
5189
13413760 5190 /* Tell the scalarizer not to bother creating loop variables, etc. */
6de9cd9a
DN
5191 if (!need_tmp)
5192 loop.array_parameter = 1;
5193 else
7a70c12d
RS
5194 /* The right-hand side of a pointer assignment mustn't use a temporary. */
5195 gcc_assert (!se->direct_byref);
6de9cd9a
DN
5196
5197 /* Setup the scalarizing loops and bounds. */
5198 gfc_conv_ss_startstride (&loop);
5199
5200 if (need_tmp)
5201 {
5202 /* Tell the scalarizer to make a temporary. */
5203 loop.temp_ss = gfc_get_ss ();
5204 loop.temp_ss->type = GFC_SS_TEMP;
5205 loop.temp_ss->next = gfc_ss_terminator;
07368af0 5206
5d63a35f 5207 if (expr->ts.type == BT_CHARACTER
bc21d315 5208 && !expr->ts.u.cl->backend_decl)
5d63a35f 5209 get_array_charlen (expr, se);
07368af0
PT
5210
5211 loop.temp_ss->data.temp.type = gfc_typenode_for_spec (&expr->ts);
5212
2b052ce2 5213 if (expr->ts.type == BT_CHARACTER)
bc21d315 5214 loop.temp_ss->string_length = expr->ts.u.cl->backend_decl;
20c9dc8a 5215 else
07368af0
PT
5216 loop.temp_ss->string_length = NULL;
5217
5218 se->string_length = loop.temp_ss->string_length;
6de9cd9a
DN
5219 loop.temp_ss->data.temp.dimen = loop.dimen;
5220 gfc_add_ss_to_loop (&loop, loop.temp_ss);
5221 }
5222
bdfd2ff0 5223 gfc_conv_loop_setup (&loop, & expr->where);
6de9cd9a
DN
5224
5225 if (need_tmp)
5226 {
5227 /* Copy into a temporary and pass that. We don't need to copy the data
5228 back because expressions and vector subscripts must be INTENT_IN. */
5229 /* TODO: Optimize passing function return values. */
5230 gfc_se lse;
5231 gfc_se rse;
5232
5233 /* Start the copying loops. */
5234 gfc_mark_ss_chain_used (loop.temp_ss, 1);
5235 gfc_mark_ss_chain_used (ss, 1);
5236 gfc_start_scalarized_body (&loop, &block);
5237
5238 /* Copy each data element. */
5239 gfc_init_se (&lse, NULL);
5240 gfc_copy_loopinfo_to_se (&lse, &loop);
5241 gfc_init_se (&rse, NULL);
5242 gfc_copy_loopinfo_to_se (&rse, &loop);
5243
5244 lse.ss = loop.temp_ss;
5245 rse.ss = ss;
5246
5247 gfc_conv_scalarized_array_ref (&lse, NULL);
2b052ce2
PT
5248 if (expr->ts.type == BT_CHARACTER)
5249 {
5250 gfc_conv_expr (&rse, expr);
20b1cbc3 5251 if (POINTER_TYPE_P (TREE_TYPE (rse.expr)))
db3927fb
AH
5252 rse.expr = build_fold_indirect_ref_loc (input_location,
5253 rse.expr);
2b052ce2
PT
5254 }
5255 else
5256 gfc_conv_expr_val (&rse, expr);
6de9cd9a
DN
5257
5258 gfc_add_block_to_block (&block, &rse.pre);
5259 gfc_add_block_to_block (&block, &lse.pre);
5260
129c14bd
PT
5261 lse.string_length = rse.string_length;
5262 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true,
2b56d6a4 5263 expr->expr_type == EXPR_VARIABLE, true);
129c14bd 5264 gfc_add_expr_to_block (&block, tmp);
6de9cd9a
DN
5265
5266 /* Finish the copying loops. */
5267 gfc_trans_scalarizing_loops (&loop, &block);
5268
6de9cd9a 5269 desc = loop.temp_ss->data.info.descriptor;
6de9cd9a 5270 }
fc90a8f2
PB
5271 else if (expr->expr_type == EXPR_FUNCTION)
5272 {
5273 desc = info->descriptor;
7823229b 5274 se->string_length = ss->string_length;
fc90a8f2 5275 }
6de9cd9a
DN
5276 else
5277 {
fc90a8f2
PB
5278 /* We pass sections without copying to a temporary. Make a new
5279 descriptor and point it at the section we want. The loop variable
5280 limits will be the limits of the section.
5281 A function may decide to repack the array to speed up access, but
5282 we're not bothered about that here. */
114e4d10 5283 int dim, ndim;
6de9cd9a
DN
5284 tree parm;
5285 tree parmtype;
5286 tree stride;
5287 tree from;
5288 tree to;
5289 tree base;
5290
fc90a8f2 5291 /* Set the string_length for a character array. */
20c9dc8a 5292 if (expr->ts.type == BT_CHARACTER)
ca2940c3 5293 se->string_length = gfc_get_expr_charlen (expr);
20c9dc8a 5294
6de9cd9a 5295 desc = info->descriptor;
6e45f57b 5296 gcc_assert (secss && secss != gfc_ss_terminator);
6de9cd9a
DN
5297 if (se->direct_byref)
5298 {
5299 /* For pointer assignments we fill in the destination. */
5300 parm = se->expr;
5301 parmtype = TREE_TYPE (parm);
5302 }
5303 else
5304 {
5305 /* Otherwise make a new one. */
5306 parmtype = gfc_get_element_type (TREE_TYPE (desc));
f33beee9 5307 parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, 0,
fad0afd7 5308 loop.from, loop.to, 0,
10174ddf 5309 GFC_ARRAY_UNKNOWN, false);
6de9cd9a
DN
5310 parm = gfc_create_var (parmtype, "parm");
5311 }
5312
7ab92584 5313 offset = gfc_index_zero_node;
6de9cd9a
DN
5314 dim = 0;
5315
5316 /* The following can be somewhat confusing. We have two
5317 descriptors, a new one and the original array.
5318 {parm, parmtype, dim} refer to the new one.
5319 {desc, type, n, secss, loop} refer to the original, which maybe
5320 a descriptorless array.
e7dc5b4f 5321 The bounds of the scalarization are the bounds of the section.
6de9cd9a
DN
5322 We don't have to worry about numeric overflows when calculating
5323 the offsets because all elements are within the array data. */
5324
5325 /* Set the dtype. */
5326 tmp = gfc_conv_descriptor_dtype (parm);
726a989a 5327 gfc_add_modify (&loop.pre, tmp, gfc_get_dtype (parmtype));
6de9cd9a 5328
a7d318ea
TB
5329 /* Set offset for assignments to pointer only to zero if it is not
5330 the full array. */
5331 if (se->direct_byref
5332 && info->ref && info->ref->u.ar.type != AR_FULL)
7ab92584 5333 base = gfc_index_zero_node;
c4ba8848
PT
5334 else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
5335 base = gfc_evaluate_now (gfc_conv_array_offset (desc), &loop.pre);
6de9cd9a
DN
5336 else
5337 base = NULL_TREE;
5338
114e4d10
RS
5339 ndim = info->ref ? info->ref->u.ar.dimen : info->dimen;
5340 for (n = 0; n < ndim; n++)
6de9cd9a
DN
5341 {
5342 stride = gfc_conv_array_stride (desc, n);
5343
5344 /* Work out the offset. */
114e4d10
RS
5345 if (info->ref
5346 && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
6de9cd9a 5347 {
6e45f57b 5348 gcc_assert (info->subscript[n]
6de9cd9a
DN
5349 && info->subscript[n]->type == GFC_SS_SCALAR);
5350 start = info->subscript[n]->data.scalar.expr;
5351 }
5352 else
5353 {
5354 /* Check we haven't somehow got out of sync. */
6e45f57b 5355 gcc_assert (info->dim[dim] == n);
6de9cd9a
DN
5356
5357 /* Evaluate and remember the start of the section. */
5358 start = info->start[dim];
5359 stride = gfc_evaluate_now (stride, &loop.pre);
5360 }
5361
5362 tmp = gfc_conv_array_lbound (desc, n);
10c7a96f 5363 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), start, tmp);
6de9cd9a 5364
10c7a96f
SB
5365 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (tmp), tmp, stride);
5366 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (tmp), offset, tmp);
6de9cd9a 5367
114e4d10
RS
5368 if (info->ref
5369 && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
6de9cd9a
DN
5370 {
5371 /* For elemental dimensions, we only need the offset. */
5372 continue;
5373 }
5374
5375 /* Vector subscripts need copying and are handled elsewhere. */
114e4d10
RS
5376 if (info->ref)
5377 gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
6de9cd9a
DN
5378
5379 /* Set the new lower bound. */
5380 from = loop.from[dim];
5381 to = loop.to[dim];
4fd9a813 5382
a7d318ea
TB
5383 /* If we have an array section or are assigning make sure that
5384 the lower bound is 1. References to the full
4fd9a813 5385 array should otherwise keep the original bounds. */
114e4d10 5386 if ((!info->ref
a7d318ea 5387 || info->ref->u.ar.type != AR_FULL)
4fd9a813 5388 && !integer_onep (from))
6de9cd9a 5389 {
10c7a96f
SB
5390 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
5391 gfc_index_one_node, from);
5392 to = fold_build2 (PLUS_EXPR, gfc_array_index_type, to, tmp);
7ab92584 5393 from = gfc_index_one_node;
6de9cd9a 5394 }
568e8e1e
PT
5395 gfc_conv_descriptor_lbound_set (&loop.pre, parm,
5396 gfc_rank_cst[dim], from);
6de9cd9a
DN
5397
5398 /* Set the new upper bound. */
568e8e1e
PT
5399 gfc_conv_descriptor_ubound_set (&loop.pre, parm,
5400 gfc_rank_cst[dim], to);
6de9cd9a
DN
5401
5402 /* Multiply the stride by the section stride to get the
5403 total stride. */
10c7a96f
SB
5404 stride = fold_build2 (MULT_EXPR, gfc_array_index_type,
5405 stride, info->stride[dim]);
6de9cd9a 5406
568e8e1e
PT
5407 if (se->direct_byref
5408 && info->ref
5409 && info->ref->u.ar.type != AR_FULL)
c4ba8848
PT
5410 {
5411 base = fold_build2 (MINUS_EXPR, TREE_TYPE (base),
5412 base, stride);
5413 }
5414 else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
5415 {
5416 tmp = gfc_conv_array_lbound (desc, n);
5417 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (base),
5418 tmp, loop.from[dim]);
5419 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (base),
5420 tmp, gfc_conv_array_stride (desc, n));
5421 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base),
5422 tmp, base);
5423 }
6de9cd9a
DN
5424
5425 /* Store the new stride. */
568e8e1e
PT
5426 gfc_conv_descriptor_stride_set (&loop.pre, parm,
5427 gfc_rank_cst[dim], stride);
6de9cd9a
DN
5428
5429 dim++;
5430 }
5431
ad5dd90d 5432 if (se->data_not_needed)
568e8e1e
PT
5433 gfc_conv_descriptor_data_set (&loop.pre, parm,
5434 gfc_index_zero_node);
ad5dd90d 5435 else
568e8e1e 5436 /* Point the data pointer at the 1st element in the section. */
1d6b7f39
PT
5437 gfc_get_dataptr_offset (&loop.pre, parm, desc, offset,
5438 subref_array_target, expr);
6de9cd9a 5439
c4ba8848 5440 if ((se->direct_byref || GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
a7d318ea 5441 && !se->data_not_needed)
6de9cd9a
DN
5442 {
5443 /* Set the offset. */
568e8e1e 5444 gfc_conv_descriptor_offset_set (&loop.pre, parm, base);
6de9cd9a
DN
5445 }
5446 else
5447 {
5448 /* Only the callee knows what the correct offset it, so just set
5449 it to zero here. */
568e8e1e 5450 gfc_conv_descriptor_offset_set (&loop.pre, parm, gfc_index_zero_node);
6de9cd9a 5451 }
7a70c12d
RS
5452 desc = parm;
5453 }
6de9cd9a 5454
7a70c12d
RS
5455 if (!se->direct_byref)
5456 {
5457 /* Get a pointer to the new descriptor. */
5458 if (se->want_pointer)
628c189e 5459 se->expr = gfc_build_addr_expr (NULL_TREE, desc);
7a70c12d
RS
5460 else
5461 se->expr = desc;
6de9cd9a
DN
5462 }
5463
5464 gfc_add_block_to_block (&se->pre, &loop.pre);
5465 gfc_add_block_to_block (&se->post, &loop.post);
5466
5467 /* Cleanup the scalarizer. */
5468 gfc_cleanup_loop (&loop);
5469}
5470
7e279142
JJ
5471/* Helper function for gfc_conv_array_parameter if array size needs to be
5472 computed. */
5473
5474static void
5475array_parameter_size (tree desc, gfc_expr *expr, tree *size)
5476{
5477 tree elem;
5478 if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
5479 *size = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (desc));
5480 else if (expr->rank > 1)
db3927fb
AH
5481 *size = build_call_expr_loc (input_location,
5482 gfor_fndecl_size0, 1,
7e279142
JJ
5483 gfc_build_addr_expr (NULL, desc));
5484 else
5485 {
568e8e1e
PT
5486 tree ubound = gfc_conv_descriptor_ubound_get (desc, gfc_index_zero_node);
5487 tree lbound = gfc_conv_descriptor_lbound_get (desc, gfc_index_zero_node);
7e279142
JJ
5488
5489 *size = fold_build2 (MINUS_EXPR, gfc_array_index_type, ubound, lbound);
5490 *size = fold_build2 (PLUS_EXPR, gfc_array_index_type, *size,
5491 gfc_index_one_node);
5492 *size = fold_build2 (MAX_EXPR, gfc_array_index_type, *size,
5493 gfc_index_zero_node);
5494 }
5495 elem = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
5496 *size = fold_build2 (MULT_EXPR, gfc_array_index_type, *size,
5497 fold_convert (gfc_array_index_type, elem));
5498}
6de9cd9a
DN
5499
5500/* Convert an array for passing as an actual parameter. */
5501/* TODO: Optimize passing g77 arrays. */
5502
5503void
f7172b55 5504gfc_conv_array_parameter (gfc_se * se, gfc_expr * expr, gfc_ss * ss, bool g77,
7e279142
JJ
5505 const gfc_symbol *fsym, const char *proc_name,
5506 tree *size)
6de9cd9a
DN
5507{
5508 tree ptr;
5509 tree desc;
bd075cf2 5510 tree tmp = NULL_TREE;
6de9cd9a 5511 tree stmt;
b2b247f9 5512 tree parent = DECL_CONTEXT (current_function_decl);
17555e7e
PT
5513 bool full_array_var;
5514 bool this_array_result;
5515 bool contiguous;
f7172b55 5516 bool no_pack;
2542496c
PT
5517 bool array_constructor;
5518 bool good_allocatable;
ba461991
PT
5519 bool ultimate_ptr_comp;
5520 bool ultimate_alloc_comp;
6de9cd9a
DN
5521 gfc_symbol *sym;
5522 stmtblock_t block;
17555e7e
PT
5523 gfc_ref *ref;
5524
ba461991
PT
5525 ultimate_ptr_comp = false;
5526 ultimate_alloc_comp = false;
fe4e525c 5527
17555e7e 5528 for (ref = expr->ref; ref; ref = ref->next)
ba461991
PT
5529 {
5530 if (ref->next == NULL)
5531 break;
5532
5533 if (ref->type == REF_COMPONENT)
5534 {
5535 ultimate_ptr_comp = ref->u.c.component->attr.pointer;
5536 ultimate_alloc_comp = ref->u.c.component->attr.allocatable;
5537 }
5538 }
17555e7e
PT
5539
5540 full_array_var = false;
5541 contiguous = false;
5542
ba461991 5543 if (expr->expr_type == EXPR_VARIABLE && ref && !ultimate_ptr_comp)
17555e7e 5544 full_array_var = gfc_full_array_ref_p (ref, &contiguous);
6de9cd9a 5545
b2b247f9
PT
5546 sym = full_array_var ? expr->symtree->n.sym : NULL;
5547
18b0679f 5548 /* The symbol should have an array specification. */
17555e7e 5549 gcc_assert (!sym || sym->as || ref->u.ar.as);
18b0679f 5550
0ee8e250
PT
5551 if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_CHARACTER)
5552 {
5553 get_array_ctor_strlen (&se->pre, expr->value.constructor, &tmp);
bc21d315 5554 expr->ts.u.cl->backend_decl = tmp;
f2d3cb25 5555 se->string_length = tmp;
0ee8e250
PT
5556 }
5557
b2b247f9
PT
5558 /* Is this the result of the enclosing procedure? */
5559 this_array_result = (full_array_var && sym->attr.flavor == FL_PROCEDURE);
5560 if (this_array_result
5561 && (sym->backend_decl != current_function_decl)
5562 && (sym->backend_decl != parent))
5563 this_array_result = false;
5564
6de9cd9a 5565 /* Passing address of the array if it is not pointer or assumed-shape. */
b2b247f9 5566 if (full_array_var && g77 && !this_array_result)
6de9cd9a 5567 {
b122dc6a 5568 tmp = gfc_get_symbol_decl (sym);
83d890b9 5569
20c9dc8a 5570 if (sym->ts.type == BT_CHARACTER)
bc21d315 5571 se->string_length = sym->ts.u.cl->backend_decl;
17555e7e 5572
2542496c 5573 if (sym->ts.type == BT_DERIVED)
17555e7e
PT
5574 {
5575 gfc_conv_expr_descriptor (se, expr, ss);
5576 se->expr = gfc_conv_array_data (se->expr);
5577 return;
5578 }
5579
f7172b55
PT
5580 if (!sym->attr.pointer
5581 && sym->as
5582 && sym->as->type != AS_ASSUMED_SHAPE
5583 && !sym->attr.allocatable)
6de9cd9a 5584 {
346d5977 5585 /* Some variables are declared directly, others are declared as
841b0c1f
PB
5586 pointers and allocated on the heap. */
5587 if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
5588 se->expr = tmp;
6de9cd9a 5589 else
628c189e 5590 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
7e279142
JJ
5591 if (size)
5592 array_parameter_size (tmp, expr, size);
6de9cd9a
DN
5593 return;
5594 }
17555e7e 5595
6de9cd9a
DN
5596 if (sym->attr.allocatable)
5597 {
237b2f1b 5598 if (sym->attr.dummy || sym->attr.result)
7f0d6da9
EE
5599 {
5600 gfc_conv_expr_descriptor (se, expr, ss);
7e279142 5601 tmp = se->expr;
7f0d6da9 5602 }
7e279142
JJ
5603 if (size)
5604 array_parameter_size (tmp, expr, size);
5605 se->expr = gfc_conv_array_data (tmp);
6de9cd9a
DN
5606 return;
5607 }
5608 }
5609
ba461991
PT
5610 /* A convenient reduction in scope. */
5611 contiguous = g77 && !this_array_result && contiguous;
5612
2542496c 5613 /* There is no need to pack and unpack the array, if it is contiguous
fe4e525c
TB
5614 and not a deferred- or assumed-shape array, or if it is simply
5615 contiguous. */
f7172b55
PT
5616 no_pack = ((sym && sym->as
5617 && !sym->attr.pointer
5618 && sym->as->type != AS_DEFERRED
5619 && sym->as->type != AS_ASSUMED_SHAPE)
5620 ||
5621 (ref && ref->u.ar.as
5622 && ref->u.ar.as->type != AS_DEFERRED
fe4e525c
TB
5623 && ref->u.ar.as->type != AS_ASSUMED_SHAPE)
5624 ||
5625 gfc_is_simply_contiguous (expr, false));
f7172b55 5626
ba461991 5627 no_pack = contiguous && no_pack;
f7172b55 5628
2542496c
PT
5629 /* Array constructors are always contiguous and do not need packing. */
5630 array_constructor = g77 && !this_array_result && expr->expr_type == EXPR_ARRAY;
5631
5632 /* Same is true of contiguous sections from allocatable variables. */
ba461991
PT
5633 good_allocatable = contiguous
5634 && expr->symtree
5635 && expr->symtree->n.sym->attr.allocatable;
5636
5637 /* Or ultimate allocatable components. */
5638 ultimate_alloc_comp = contiguous && ultimate_alloc_comp;
f7172b55 5639
ba461991 5640 if (no_pack || array_constructor || good_allocatable || ultimate_alloc_comp)
17555e7e
PT
5641 {
5642 gfc_conv_expr_descriptor (se, expr, ss);
5643 if (expr->ts.type == BT_CHARACTER)
5644 se->string_length = expr->ts.u.cl->backend_decl;
5645 if (size)
5646 array_parameter_size (se->expr, expr, size);
5647 se->expr = gfc_conv_array_data (se->expr);
5648 return;
5649 }
5650
b2b247f9
PT
5651 if (this_array_result)
5652 {
5653 /* Result of the enclosing function. */
5654 gfc_conv_expr_descriptor (se, expr, ss);
7e279142
JJ
5655 if (size)
5656 array_parameter_size (se->expr, expr, size);
628c189e 5657 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
b2b247f9
PT
5658
5659 if (g77 && TREE_TYPE (TREE_TYPE (se->expr)) != NULL_TREE
5660 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
db3927fb
AH
5661 se->expr = gfc_conv_array_data (build_fold_indirect_ref_loc (input_location,
5662 se->expr));
b2b247f9
PT
5663
5664 return;
5665 }
5666 else
5667 {
5668 /* Every other type of array. */
5669 se->want_pointer = 1;
5670 gfc_conv_expr_descriptor (se, expr, ss);
7e279142 5671 if (size)
db3927fb
AH
5672 array_parameter_size (build_fold_indirect_ref_loc (input_location,
5673 se->expr),
7e279142 5674 expr, size);
b2b247f9
PT
5675 }
5676
5046aff5
PT
5677 /* Deallocate the allocatable components of structures that are
5678 not variable. */
5679 if (expr->ts.type == BT_DERIVED
bc21d315 5680 && expr->ts.u.derived->attr.alloc_comp
5046aff5
PT
5681 && expr->expr_type != EXPR_VARIABLE)
5682 {
db3927fb
AH
5683 tmp = build_fold_indirect_ref_loc (input_location,
5684 se->expr);
bc21d315 5685 tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
5046aff5
PT
5686 gfc_add_expr_to_block (&se->post, tmp);
5687 }
5688
fe4e525c
TB
5689 if (g77 || (fsym && fsym->attr.contiguous
5690 && !gfc_is_simply_contiguous (expr, false)))
6de9cd9a 5691 {
fe4e525c
TB
5692 tree origptr = NULL_TREE;
5693
6de9cd9a 5694 desc = se->expr;
fe4e525c
TB
5695
5696 /* For contiguous arrays, save the original value of the descriptor. */
5697 if (!g77)
5698 {
5699 origptr = gfc_create_var (pvoid_type_node, "origptr");
5700 tmp = build_fold_indirect_ref_loc (input_location, desc);
5701 tmp = gfc_conv_array_data (tmp);
5702 tmp = fold_build2 (MODIFY_EXPR, TREE_TYPE (origptr), origptr,
5703 fold_convert (TREE_TYPE (origptr), tmp));
5704 gfc_add_expr_to_block (&se->pre, tmp);
5705 }
5706
6de9cd9a 5707 /* Repack the array. */
bdfd2ff0 5708 if (gfc_option.warn_array_temp)
0d52899f
TB
5709 {
5710 if (fsym)
5711 gfc_warning ("Creating array temporary at %L for argument '%s'",
5712 &expr->where, fsym->name);
5713 else
5714 gfc_warning ("Creating array temporary at %L", &expr->where);
5715 }
bdfd2ff0 5716
db3927fb
AH
5717 ptr = build_call_expr_loc (input_location,
5718 gfor_fndecl_in_pack, 1, desc);
0d52899f
TB
5719
5720 if (fsym && fsym->attr.optional && sym && sym->attr.optional)
5721 {
5722 tmp = gfc_conv_expr_present (sym);
6e1b67b3
RG
5723 ptr = build3 (COND_EXPR, TREE_TYPE (se->expr), tmp,
5724 fold_convert (TREE_TYPE (se->expr), ptr),
5725 fold_convert (TREE_TYPE (se->expr), null_pointer_node));
0d52899f
TB
5726 }
5727
6de9cd9a 5728 ptr = gfc_evaluate_now (ptr, &se->pre);
0d52899f 5729
fe4e525c
TB
5730 /* Use the packed data for the actual argument, except for contiguous arrays,
5731 where the descriptor's data component is set. */
5732 if (g77)
5733 se->expr = ptr;
5734 else
5735 {
5736 tmp = build_fold_indirect_ref_loc (input_location, desc);
5737 gfc_conv_descriptor_data_set (&se->pre, tmp, ptr);
5738 }
6de9cd9a 5739
d3d3011f 5740 if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
0d52899f
TB
5741 {
5742 char * msg;
5743
5744 if (fsym && proc_name)
5745 asprintf (&msg, "An array temporary was created for argument "
5746 "'%s' of procedure '%s'", fsym->name, proc_name);
5747 else
5748 asprintf (&msg, "An array temporary was created");
5749
db3927fb
AH
5750 tmp = build_fold_indirect_ref_loc (input_location,
5751 desc);
0d52899f
TB
5752 tmp = gfc_conv_array_data (tmp);
5753 tmp = fold_build2 (NE_EXPR, boolean_type_node,
5754 fold_convert (TREE_TYPE (tmp), ptr), tmp);
5755
5756 if (fsym && fsym->attr.optional && sym && sym->attr.optional)
5757 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
5758 gfc_conv_expr_present (sym), tmp);
5759
5760 gfc_trans_runtime_check (false, true, tmp, &se->pre,
5761 &expr->where, msg);
5762 gfc_free (msg);
5763 }
5764
6de9cd9a
DN
5765 gfc_start_block (&block);
5766
5767 /* Copy the data back. */
0d52899f
TB
5768 if (fsym == NULL || fsym->attr.intent != INTENT_IN)
5769 {
db3927fb
AH
5770 tmp = build_call_expr_loc (input_location,
5771 gfor_fndecl_in_unpack, 2, desc, ptr);
0d52899f
TB
5772 gfc_add_expr_to_block (&block, tmp);
5773 }
6de9cd9a
DN
5774
5775 /* Free the temporary. */
1529b8d9 5776 tmp = gfc_call_free (convert (pvoid_type_node, ptr));
6de9cd9a
DN
5777 gfc_add_expr_to_block (&block, tmp);
5778
5779 stmt = gfc_finish_block (&block);
5780
5781 gfc_init_block (&block);
5782 /* Only if it was repacked. This code needs to be executed before the
5783 loop cleanup code. */
db3927fb
AH
5784 tmp = build_fold_indirect_ref_loc (input_location,
5785 desc);
6de9cd9a 5786 tmp = gfc_conv_array_data (tmp);
44855d8c
TS
5787 tmp = fold_build2 (NE_EXPR, boolean_type_node,
5788 fold_convert (TREE_TYPE (tmp), ptr), tmp);
0d52899f
TB
5789
5790 if (fsym && fsym->attr.optional && sym && sym->attr.optional)
5791 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
5792 gfc_conv_expr_present (sym), tmp);
5793
c2255bc4 5794 tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
6de9cd9a
DN
5795
5796 gfc_add_expr_to_block (&block, tmp);
5797 gfc_add_block_to_block (&block, &se->post);
5798
5799 gfc_init_block (&se->post);
fe4e525c
TB
5800
5801 /* Reset the descriptor pointer. */
5802 if (!g77)
5803 {
5804 tmp = build_fold_indirect_ref_loc (input_location, desc);
5805 gfc_conv_descriptor_data_set (&se->post, tmp, origptr);
5806 }
5807
6de9cd9a
DN
5808 gfc_add_block_to_block (&se->post, &block);
5809 }
5810}
5811
5812
763ccd45 5813/* Generate code to deallocate an array, if it is allocated. */
42a0e16c
PT
5814
5815tree
763ccd45 5816gfc_trans_dealloc_allocated (tree descriptor)
42a0e16c
PT
5817{
5818 tree tmp;
5046aff5 5819 tree var;
42a0e16c
PT
5820 stmtblock_t block;
5821
42a0e16c 5822 gfc_start_block (&block);
42a0e16c 5823
54200abb
RG
5824 var = gfc_conv_descriptor_data_get (descriptor);
5825 STRIP_NOPS (var);
5046aff5 5826
4376b7cf 5827 /* Call array_deallocate with an int * present in the second argument.
5046aff5
PT
5828 Although it is ignored here, it's presence ensures that arrays that
5829 are already deallocated are ignored. */
f25a62a5 5830 tmp = gfc_deallocate_with_status (var, NULL_TREE, true, NULL);
42a0e16c 5831 gfc_add_expr_to_block (&block, tmp);
54200abb
RG
5832
5833 /* Zero the data pointer. */
44855d8c
TS
5834 tmp = fold_build2 (MODIFY_EXPR, void_type_node,
5835 var, build_int_cst (TREE_TYPE (var), 0));
54200abb
RG
5836 gfc_add_expr_to_block (&block, tmp);
5837
5046aff5
PT
5838 return gfc_finish_block (&block);
5839}
5840
5841
5842/* This helper function calculates the size in words of a full array. */
5843
5844static tree
5845get_full_array_size (stmtblock_t *block, tree decl, int rank)
5846{
5847 tree idx;
5848 tree nelems;
5849 tree tmp;
5850 idx = gfc_rank_cst[rank - 1];
568e8e1e
PT
5851 nelems = gfc_conv_descriptor_ubound_get (decl, idx);
5852 tmp = gfc_conv_descriptor_lbound_get (decl, idx);
44855d8c
TS
5853 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, nelems, tmp);
5854 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
5855 tmp, gfc_index_one_node);
5046aff5
PT
5856 tmp = gfc_evaluate_now (tmp, block);
5857
568e8e1e 5858 nelems = gfc_conv_descriptor_stride_get (decl, idx);
44855d8c 5859 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, nelems, tmp);
5046aff5
PT
5860 return gfc_evaluate_now (tmp, block);
5861}
42a0e16c 5862
5046aff5 5863
40c32948
PT
5864/* Allocate dest to the same size as src, and copy src -> dest.
5865 If no_malloc is set, only the copy is done. */
5046aff5 5866
40c32948
PT
5867static tree
5868duplicate_allocatable(tree dest, tree src, tree type, int rank,
5869 bool no_malloc)
5046aff5
PT
5870{
5871 tree tmp;
5872 tree size;
5873 tree nelems;
5046aff5
PT
5874 tree null_cond;
5875 tree null_data;
5876 stmtblock_t block;
5877
40c32948
PT
5878 /* If the source is null, set the destination to null. Then,
5879 allocate memory to the destination. */
5046aff5 5880 gfc_init_block (&block);
5046aff5 5881
40c32948
PT
5882 if (rank == 0)
5883 {
5884 tmp = null_pointer_node;
5885 tmp = fold_build2 (MODIFY_EXPR, type, dest, tmp);
5886 gfc_add_expr_to_block (&block, tmp);
5887 null_data = gfc_finish_block (&block);
5888
5889 gfc_init_block (&block);
5890 size = TYPE_SIZE_UNIT (type);
5891 if (!no_malloc)
5892 {
5893 tmp = gfc_call_malloc (&block, type, size);
5894 tmp = fold_build2 (MODIFY_EXPR, void_type_node, dest,
5895 fold_convert (type, tmp));
5896 gfc_add_expr_to_block (&block, tmp);
5897 }
5898
5899 tmp = built_in_decls[BUILT_IN_MEMCPY];
5900 tmp = build_call_expr_loc (input_location, tmp, 3,
5901 dest, src, size);
5902 }
5903 else
5904 {
5905 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
5906 null_data = gfc_finish_block (&block);
5907
5908 gfc_init_block (&block);
5909 nelems = get_full_array_size (&block, src, rank);
5910 tmp = fold_convert (gfc_array_index_type,
5911 TYPE_SIZE_UNIT (gfc_get_element_type (type)));
5912 size = fold_build2 (MULT_EXPR, gfc_array_index_type, nelems, tmp);
5913 if (!no_malloc)
5914 {
5915 tmp = TREE_TYPE (gfc_conv_descriptor_data_get (src));
5916 tmp = gfc_call_malloc (&block, tmp, size);
5917 gfc_conv_descriptor_data_set (&block, dest, tmp);
5918 }
5919
5920 /* We know the temporary and the value will be the same length,
5921 so can use memcpy. */
5922 tmp = built_in_decls[BUILT_IN_MEMCPY];
5923 tmp = build_call_expr_loc (input_location,
5924 tmp, 3, gfc_conv_descriptor_data_get (dest),
5925 gfc_conv_descriptor_data_get (src), size);
5926 }
5046aff5 5927
5046aff5 5928 gfc_add_expr_to_block (&block, tmp);
42a0e16c
PT
5929 tmp = gfc_finish_block (&block);
5930
5046aff5
PT
5931 /* Null the destination if the source is null; otherwise do
5932 the allocate and copy. */
40c32948
PT
5933 if (rank == 0)
5934 null_cond = src;
5935 else
5936 null_cond = gfc_conv_descriptor_data_get (src);
5937
5046aff5 5938 null_cond = convert (pvoid_type_node, null_cond);
44855d8c
TS
5939 null_cond = fold_build2 (NE_EXPR, boolean_type_node,
5940 null_cond, null_pointer_node);
5046aff5
PT
5941 return build3_v (COND_EXPR, null_cond, tmp, null_data);
5942}
5943
5944
40c32948
PT
5945/* Allocate dest to the same size as src, and copy data src -> dest. */
5946
5947tree
5948gfc_duplicate_allocatable (tree dest, tree src, tree type, int rank)
5949{
5950 return duplicate_allocatable(dest, src, type, rank, false);
5951}
5952
5953
5954/* Copy data src -> dest. */
5955
5956tree
5957gfc_copy_allocatable_data (tree dest, tree src, tree type, int rank)
5958{
5959 return duplicate_allocatable(dest, src, type, rank, true);
5960}
5961
5962
5046aff5
PT
5963/* Recursively traverse an object of derived type, generating code to
5964 deallocate, nullify or copy allocatable components. This is the work horse
5965 function for the functions named in this enum. */
5966
40c32948
PT
5967enum {DEALLOCATE_ALLOC_COMP = 1, NULLIFY_ALLOC_COMP, COPY_ALLOC_COMP,
5968 COPY_ONLY_ALLOC_COMP};
5046aff5
PT
5969
5970static tree
5971structure_alloc_comps (gfc_symbol * der_type, tree decl,
5972 tree dest, int rank, int purpose)
5973{
5974 gfc_component *c;
5975 gfc_loopinfo loop;
5976 stmtblock_t fnblock;
5977 stmtblock_t loopbody;
546a65d9 5978 tree decl_type;
5046aff5
PT
5979 tree tmp;
5980 tree comp;
5981 tree dcmp;
5982 tree nelems;
5983 tree index;
5984 tree var;
5985 tree cdecl;
5986 tree ctype;
5987 tree vref, dref;
5988 tree null_cond = NULL_TREE;
5989
5990 gfc_init_block (&fnblock);
5991
546a65d9
PT
5992 decl_type = TREE_TYPE (decl);
5993
5994 if ((POINTER_TYPE_P (decl_type) && rank != 0)
5995 || (TREE_CODE (decl_type) == REFERENCE_TYPE && rank == 0))
5996
db3927fb
AH
5997 decl = build_fold_indirect_ref_loc (input_location,
5998 decl);
7114edca 5999
546a65d9
PT
6000 /* Just in case in gets dereferenced. */
6001 decl_type = TREE_TYPE (decl);
6002
5046aff5
PT
6003 /* If this an array of derived types with allocatable components
6004 build a loop and recursively call this function. */
546a65d9
PT
6005 if (TREE_CODE (decl_type) == ARRAY_TYPE
6006 || GFC_DESCRIPTOR_TYPE_P (decl_type))
5046aff5
PT
6007 {
6008 tmp = gfc_conv_array_data (decl);
db3927fb
AH
6009 var = build_fold_indirect_ref_loc (input_location,
6010 tmp);
5046aff5
PT
6011
6012 /* Get the number of elements - 1 and set the counter. */
546a65d9 6013 if (GFC_DESCRIPTOR_TYPE_P (decl_type))
5046aff5
PT
6014 {
6015 /* Use the descriptor for an allocatable array. Since this
6016 is a full array reference, we only need the descriptor
6017 information from dimension = rank. */
6018 tmp = get_full_array_size (&fnblock, decl, rank);
44855d8c
TS
6019 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
6020 tmp, gfc_index_one_node);
5046aff5
PT
6021
6022 null_cond = gfc_conv_descriptor_data_get (decl);
44855d8c
TS
6023 null_cond = fold_build2 (NE_EXPR, boolean_type_node, null_cond,
6024 build_int_cst (TREE_TYPE (null_cond), 0));
5046aff5
PT
6025 }
6026 else
6027 {
6028 /* Otherwise use the TYPE_DOMAIN information. */
546a65d9 6029 tmp = array_type_nelts (decl_type);
5046aff5
PT
6030 tmp = fold_convert (gfc_array_index_type, tmp);
6031 }
6032
6033 /* Remember that this is, in fact, the no. of elements - 1. */
6034 nelems = gfc_evaluate_now (tmp, &fnblock);
6035 index = gfc_create_var (gfc_array_index_type, "S");
6036
6037 /* Build the body of the loop. */
6038 gfc_init_block (&loopbody);
6039
1d6b7f39 6040 vref = gfc_build_array_ref (var, index, NULL);
5046aff5
PT
6041
6042 if (purpose == COPY_ALLOC_COMP)
6043 {
b945f9f3
PT
6044 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
6045 {
546a65d9 6046 tmp = gfc_duplicate_allocatable (dest, decl, decl_type, rank);
b945f9f3
PT
6047 gfc_add_expr_to_block (&fnblock, tmp);
6048 }
db3927fb
AH
6049 tmp = build_fold_indirect_ref_loc (input_location,
6050 gfc_conv_array_data (dest));
1d6b7f39 6051 dref = gfc_build_array_ref (tmp, index, NULL);
5046aff5
PT
6052 tmp = structure_alloc_comps (der_type, vref, dref, rank, purpose);
6053 }
40c32948
PT
6054 else if (purpose == COPY_ONLY_ALLOC_COMP)
6055 {
6056 tmp = build_fold_indirect_ref_loc (input_location,
6057 gfc_conv_array_data (dest));
6058 dref = gfc_build_array_ref (tmp, index, NULL);
6059 tmp = structure_alloc_comps (der_type, vref, dref, rank,
6060 COPY_ALLOC_COMP);
6061 }
5046aff5
PT
6062 else
6063 tmp = structure_alloc_comps (der_type, vref, NULL_TREE, rank, purpose);
6064
6065 gfc_add_expr_to_block (&loopbody, tmp);
6066
66e4ab31 6067 /* Build the loop and return. */
5046aff5
PT
6068 gfc_init_loopinfo (&loop);
6069 loop.dimen = 1;
6070 loop.from[0] = gfc_index_zero_node;
6071 loop.loopvar[0] = index;
6072 loop.to[0] = nelems;
6073 gfc_trans_scalarizing_loops (&loop, &loopbody);
6074 gfc_add_block_to_block (&fnblock, &loop.pre);
6075
6076 tmp = gfc_finish_block (&fnblock);
6077 if (null_cond != NULL_TREE)
c2255bc4
AH
6078 tmp = build3_v (COND_EXPR, null_cond, tmp,
6079 build_empty_stmt (input_location));
5046aff5
PT
6080
6081 return tmp;
6082 }
6083
6084 /* Otherwise, act on the components or recursively call self to
66e4ab31 6085 act on a chain of components. */
5046aff5
PT
6086 for (c = der_type->components; c; c = c->next)
6087 {
6088 bool cmp_has_alloc_comps = (c->ts.type == BT_DERIVED)
bc21d315 6089 && c->ts.u.derived->attr.alloc_comp;
5046aff5
PT
6090 cdecl = c->backend_decl;
6091 ctype = TREE_TYPE (cdecl);
6092
6093 switch (purpose)
6094 {
6095 case DEALLOCATE_ALLOC_COMP:
6096 /* Do not deallocate the components of ultimate pointer
6097 components. */
d4b7d0f0 6098 if (cmp_has_alloc_comps && !c->attr.pointer)
5046aff5 6099 {
44855d8c
TS
6100 comp = fold_build3 (COMPONENT_REF, ctype,
6101 decl, cdecl, NULL_TREE);
5046aff5 6102 rank = c->as ? c->as->rank : 0;
bc21d315 6103 tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE,
5046aff5
PT
6104 rank, purpose);
6105 gfc_add_expr_to_block (&fnblock, tmp);
6106 }
6107
cf2b3c22 6108 if (c->attr.allocatable && c->attr.dimension)
5046aff5 6109 {
44855d8c
TS
6110 comp = fold_build3 (COMPONENT_REF, ctype,
6111 decl, cdecl, NULL_TREE);
5046aff5
PT
6112 tmp = gfc_trans_dealloc_allocated (comp);
6113 gfc_add_expr_to_block (&fnblock, tmp);
6114 }
1517fd57
JW
6115 else if (c->attr.allocatable)
6116 {
6117 /* Allocatable scalar components. */
6118 comp = fold_build3 (COMPONENT_REF, ctype, decl, cdecl, NULL_TREE);
6119
6120 tmp = gfc_deallocate_with_status (comp, NULL_TREE, true, NULL);
6121 gfc_add_expr_to_block (&fnblock, tmp);
6122
6123 tmp = fold_build2 (MODIFY_EXPR, void_type_node, comp,
6124 build_int_cst (TREE_TYPE (comp), 0));
6125 gfc_add_expr_to_block (&fnblock, tmp);
6126 }
7a08eda1 6127 else if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
1517fd57
JW
6128 {
6129 /* Allocatable scalar CLASS components. */
6130 comp = fold_build3 (COMPONENT_REF, ctype, decl, cdecl, NULL_TREE);
6131
6132 /* Add reference to '$data' component. */
7a08eda1 6133 tmp = CLASS_DATA (c)->backend_decl;
1517fd57
JW
6134 comp = fold_build3 (COMPONENT_REF, TREE_TYPE (tmp),
6135 comp, tmp, NULL_TREE);
6136
6137 tmp = gfc_deallocate_with_status (comp, NULL_TREE, true, NULL);
6138 gfc_add_expr_to_block (&fnblock, tmp);
6139
6140 tmp = fold_build2 (MODIFY_EXPR, void_type_node, comp,
6141 build_int_cst (TREE_TYPE (comp), 0));
6142 gfc_add_expr_to_block (&fnblock, tmp);
6143 }
5046aff5
PT
6144 break;
6145
6146 case NULLIFY_ALLOC_COMP:
d4b7d0f0 6147 if (c->attr.pointer)
5046aff5 6148 continue;
cf2b3c22 6149 else if (c->attr.allocatable && c->attr.dimension)
5046aff5 6150 {
44855d8c
TS
6151 comp = fold_build3 (COMPONENT_REF, ctype,
6152 decl, cdecl, NULL_TREE);
5046aff5
PT
6153 gfc_conv_descriptor_data_set (&fnblock, comp, null_pointer_node);
6154 }
1517fd57
JW
6155 else if (c->attr.allocatable)
6156 {
6157 /* Allocatable scalar components. */
6158 comp = fold_build3 (COMPONENT_REF, ctype, decl, cdecl, NULL_TREE);
6159 tmp = fold_build2 (MODIFY_EXPR, void_type_node, comp,
6160 build_int_cst (TREE_TYPE (comp), 0));
6161 gfc_add_expr_to_block (&fnblock, tmp);
6162 }
7a08eda1 6163 else if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
1517fd57
JW
6164 {
6165 /* Allocatable scalar CLASS components. */
6166 comp = fold_build3 (COMPONENT_REF, ctype, decl, cdecl, NULL_TREE);
6167 /* Add reference to '$data' component. */
7a08eda1 6168 tmp = CLASS_DATA (c)->backend_decl;
1517fd57
JW
6169 comp = fold_build3 (COMPONENT_REF, TREE_TYPE (tmp),
6170 comp, tmp, NULL_TREE);
6171 tmp = fold_build2 (MODIFY_EXPR, void_type_node, comp,
6172 build_int_cst (TREE_TYPE (comp), 0));
6173 gfc_add_expr_to_block (&fnblock, tmp);
6174 }
5046aff5
PT
6175 else if (cmp_has_alloc_comps)
6176 {
44855d8c
TS
6177 comp = fold_build3 (COMPONENT_REF, ctype,
6178 decl, cdecl, NULL_TREE);
5046aff5 6179 rank = c->as ? c->as->rank : 0;
bc21d315 6180 tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE,
5046aff5
PT
6181 rank, purpose);
6182 gfc_add_expr_to_block (&fnblock, tmp);
6183 }
6184 break;
6185
6186 case COPY_ALLOC_COMP:
d4b7d0f0 6187 if (c->attr.pointer)
5046aff5
PT
6188 continue;
6189
6190 /* We need source and destination components. */
44855d8c
TS
6191 comp = fold_build3 (COMPONENT_REF, ctype, decl, cdecl, NULL_TREE);
6192 dcmp = fold_build3 (COMPONENT_REF, ctype, dest, cdecl, NULL_TREE);
5046aff5
PT
6193 dcmp = fold_convert (TREE_TYPE (comp), dcmp);
6194
d4b7d0f0 6195 if (c->attr.allocatable && !cmp_has_alloc_comps)
5046aff5 6196 {
40c32948
PT
6197 rank = c->as ? c->as->rank : 0;
6198 tmp = gfc_duplicate_allocatable(dcmp, comp, ctype, rank);
5046aff5
PT
6199 gfc_add_expr_to_block (&fnblock, tmp);
6200 }
6201
6202 if (cmp_has_alloc_comps)
6203 {
6204 rank = c->as ? c->as->rank : 0;
6205 tmp = fold_convert (TREE_TYPE (dcmp), comp);
726a989a 6206 gfc_add_modify (&fnblock, dcmp, tmp);
bc21d315 6207 tmp = structure_alloc_comps (c->ts.u.derived, comp, dcmp,
5046aff5
PT
6208 rank, purpose);
6209 gfc_add_expr_to_block (&fnblock, tmp);
6210 }
6211 break;
6212
6213 default:
6214 gcc_unreachable ();
6215 break;
6216 }
6217 }
6218
6219 return gfc_finish_block (&fnblock);
6220}
6221
6222/* Recursively traverse an object of derived type, generating code to
6223 nullify allocatable components. */
6224
6225tree
6226gfc_nullify_alloc_comp (gfc_symbol * der_type, tree decl, int rank)
6227{
6228 return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
6229 NULLIFY_ALLOC_COMP);
42a0e16c
PT
6230}
6231
6232
5046aff5
PT
6233/* Recursively traverse an object of derived type, generating code to
6234 deallocate allocatable components. */
6235
6236tree
6237gfc_deallocate_alloc_comp (gfc_symbol * der_type, tree decl, int rank)
6238{
6239 return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
6240 DEALLOCATE_ALLOC_COMP);
6241}
6242
6243
6244/* Recursively traverse an object of derived type, generating code to
40c32948 6245 copy it and its allocatable components. */
5046aff5
PT
6246
6247tree
6248gfc_copy_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
6249{
6250 return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP);
6251}
6252
6253
40c32948
PT
6254/* Recursively traverse an object of derived type, generating code to
6255 copy only its allocatable components. */
6256
6257tree
6258gfc_copy_only_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
6259{
6260 return structure_alloc_comps (der_type, decl, dest, rank, COPY_ONLY_ALLOC_COMP);
6261}
6262
6263
5046aff5
PT
6264/* NULLIFY an allocatable/pointer array on function entry, free it on exit.
6265 Do likewise, recursively if necessary, with the allocatable components of
6266 derived types. */
6de9cd9a
DN
6267
6268tree
6269gfc_trans_deferred_array (gfc_symbol * sym, tree body)
6270{
6271 tree type;
6272 tree tmp;
6273 tree descriptor;
6de9cd9a
DN
6274 stmtblock_t fnblock;
6275 locus loc;
5046aff5
PT
6276 int rank;
6277 bool sym_has_alloc_comp;
6278
6279 sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
bc21d315 6280 && sym->ts.u.derived->attr.alloc_comp;
6de9cd9a
DN
6281
6282 /* Make sure the frontend gets these right. */
5046aff5
PT
6283 if (!(sym->attr.pointer || sym->attr.allocatable || sym_has_alloc_comp))
6284 fatal_error ("Possible frontend bug: Deferred array size without pointer, "
6285 "allocatable attribute or derived type without allocatable "
6286 "components.");
6de9cd9a
DN
6287
6288 gfc_init_block (&fnblock);
6289
99c7ab42 6290 gcc_assert (TREE_CODE (sym->backend_decl) == VAR_DECL
5046aff5 6291 || TREE_CODE (sym->backend_decl) == PARM_DECL);
99c7ab42 6292
6de9cd9a 6293 if (sym->ts.type == BT_CHARACTER
bc21d315 6294 && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
417ab240 6295 {
bc21d315 6296 gfc_conv_string_length (sym->ts.u.cl, NULL, &fnblock);
417ab240
JJ
6297 gfc_trans_vla_type_sizes (sym, &fnblock);
6298 }
6de9cd9a 6299
bafc96b4
PT
6300 /* Dummy, use associated and result variables don't need anything special. */
6301 if (sym->attr.dummy || sym->attr.use_assoc || sym->attr.result)
6de9cd9a
DN
6302 {
6303 gfc_add_expr_to_block (&fnblock, body);
6304
6305 return gfc_finish_block (&fnblock);
6306 }
6307
6308 gfc_get_backend_locus (&loc);
6309 gfc_set_backend_locus (&sym->declared_at);
6310 descriptor = sym->backend_decl;
6311
b2a43373 6312 /* Although static, derived types with default initializers and
5046aff5
PT
6313 allocatable components must not be nulled wholesale; instead they
6314 are treated component by component. */
6315 if (TREE_STATIC (descriptor) && !sym_has_alloc_comp)
6de9cd9a
DN
6316 {
6317 /* SAVEd variables are not freed on exit. */
6318 gfc_trans_static_array_pointer (sym);
6319 return body;
6320 }
6321
6322 /* Get the descriptor type. */
6323 type = TREE_TYPE (sym->backend_decl);
2b56d6a4 6324
5046aff5
PT
6325 if (sym_has_alloc_comp && !(sym->attr.pointer || sym->attr.allocatable))
6326 {
2b56d6a4
TB
6327 if (!sym->attr.save
6328 && !(TREE_STATIC (sym->backend_decl) && sym->attr.is_main_program))
36d3fb4c 6329 {
16e520b6
DF
6330 if (sym->value == NULL
6331 || !gfc_has_default_initializer (sym->ts.u.derived))
2b56d6a4
TB
6332 {
6333 rank = sym->as ? sym->as->rank : 0;
6334 tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, descriptor, rank);
6335 gfc_add_expr_to_block (&fnblock, tmp);
6336 }
6337 else
f40eccb0 6338 {
2b56d6a4 6339 tmp = gfc_init_default_dt (sym, NULL, false);
f40eccb0
PT
6340 gfc_add_expr_to_block (&fnblock, tmp);
6341 }
36d3fb4c 6342 }
5046aff5
PT
6343 }
6344 else if (!GFC_DESCRIPTOR_TYPE_P (type))
f5f701ad
PT
6345 {
6346 /* If the backend_decl is not a descriptor, we must have a pointer
6347 to one. */
db3927fb
AH
6348 descriptor = build_fold_indirect_ref_loc (input_location,
6349 sym->backend_decl);
f5f701ad 6350 type = TREE_TYPE (descriptor);
f5f701ad 6351 }
5046aff5 6352
6de9cd9a 6353 /* NULLIFY the data pointer. */
3672065a 6354 if (GFC_DESCRIPTOR_TYPE_P (type) && !sym->attr.save)
5046aff5 6355 gfc_conv_descriptor_data_set (&fnblock, descriptor, null_pointer_node);
6de9cd9a
DN
6356
6357 gfc_add_expr_to_block (&fnblock, body);
6358
6359 gfc_set_backend_locus (&loc);
5046aff5
PT
6360
6361 /* Allocatable arrays need to be freed when they go out of scope.
6362 The allocatable components of pointers must not be touched. */
6363 if (sym_has_alloc_comp && !(sym->attr.function || sym->attr.result)
36d3fb4c 6364 && !sym->attr.pointer && !sym->attr.save)
5046aff5
PT
6365 {
6366 int rank;
6367 rank = sym->as ? sym->as->rank : 0;
bc21d315 6368 tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank);
5046aff5
PT
6369 gfc_add_expr_to_block (&fnblock, tmp);
6370 }
6371
cf2b3c22
TB
6372 if (sym->attr.allocatable && sym->attr.dimension
6373 && !sym->attr.save && !sym->attr.result)
6de9cd9a 6374 {
763ccd45 6375 tmp = gfc_trans_dealloc_allocated (sym->backend_decl);
6de9cd9a
DN
6376 gfc_add_expr_to_block (&fnblock, tmp);
6377 }
6378
6379 return gfc_finish_block (&fnblock);
6380}
6381
6382/************ Expression Walking Functions ******************/
6383
6384/* Walk a variable reference.
6385
6386 Possible extension - multiple component subscripts.
6387 x(:,:) = foo%a(:)%b(:)
6388 Transforms to
6389 forall (i=..., j=...)
6390 x(i,j) = foo%a(j)%b(i)
6391 end forall
735dfed7 6392 This adds a fair amount of complexity because you need to deal with more
6de9cd9a
DN
6393 than one ref. Maybe handle in a similar manner to vector subscripts.
6394 Maybe not worth the effort. */
6395
6396
6397static gfc_ss *
6398gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
6399{
6400 gfc_ref *ref;
6401 gfc_array_ref *ar;
6402 gfc_ss *newss;
6de9cd9a
DN
6403 int n;
6404
6405 for (ref = expr->ref; ref; ref = ref->next)
068e7338
RS
6406 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
6407 break;
6408
6409 for (; ref; ref = ref->next)
6de9cd9a 6410 {
068e7338
RS
6411 if (ref->type == REF_SUBSTRING)
6412 {
6413 newss = gfc_get_ss ();
6414 newss->type = GFC_SS_SCALAR;
6415 newss->expr = ref->u.ss.start;
6416 newss->next = ss;
6417 ss = newss;
6418
6419 newss = gfc_get_ss ();
6420 newss->type = GFC_SS_SCALAR;
6421 newss->expr = ref->u.ss.end;
6422 newss->next = ss;
6423 ss = newss;
6424 }
6425
6426 /* We're only interested in array sections from now on. */
6de9cd9a
DN
6427 if (ref->type != REF_ARRAY)
6428 continue;
6429
6430 ar = &ref->u.ar;
d3a9eea2
TB
6431
6432 if (ar->as->rank == 0)
6433 {
6434 /* Scalar coarray. */
6435 continue;
6436 }
6437
6de9cd9a
DN
6438 switch (ar->type)
6439 {
6440 case AR_ELEMENT:
068e7338
RS
6441 for (n = 0; n < ar->dimen; n++)
6442 {
6443 newss = gfc_get_ss ();
6444 newss->type = GFC_SS_SCALAR;
6445 newss->expr = ar->start[n];
6446 newss->next = ss;
6447 ss = newss;
6448 }
6de9cd9a
DN
6449 break;
6450
6451 case AR_FULL:
6452 newss = gfc_get_ss ();
6453 newss->type = GFC_SS_SECTION;
6454 newss->expr = expr;
6455 newss->next = ss;
6456 newss->data.info.dimen = ar->as->rank;
6457 newss->data.info.ref = ref;
6458
6459 /* Make sure array is the same as array(:,:), this way
6460 we don't need to special case all the time. */
6461 ar->dimen = ar->as->rank;
6462 for (n = 0; n < ar->dimen; n++)
6463 {
6464 newss->data.info.dim[n] = n;
6465 ar->dimen_type[n] = DIMEN_RANGE;
6466
6e45f57b
PB
6467 gcc_assert (ar->start[n] == NULL);
6468 gcc_assert (ar->end[n] == NULL);
6469 gcc_assert (ar->stride[n] == NULL);
6de9cd9a 6470 }
068e7338
RS
6471 ss = newss;
6472 break;
6de9cd9a
DN
6473
6474 case AR_SECTION:
6475 newss = gfc_get_ss ();
6476 newss->type = GFC_SS_SECTION;
6477 newss->expr = expr;
6478 newss->next = ss;
6479 newss->data.info.dimen = 0;
6480 newss->data.info.ref = ref;
6481
6de9cd9a
DN
6482 /* We add SS chains for all the subscripts in the section. */
6483 for (n = 0; n < ar->dimen; n++)
6484 {
6485 gfc_ss *indexss;
6486
6487 switch (ar->dimen_type[n])
6488 {
6489 case DIMEN_ELEMENT:
6490 /* Add SS for elemental (scalar) subscripts. */
6e45f57b 6491 gcc_assert (ar->start[n]);
6de9cd9a
DN
6492 indexss = gfc_get_ss ();
6493 indexss->type = GFC_SS_SCALAR;
6494 indexss->expr = ar->start[n];
6495 indexss->next = gfc_ss_terminator;
6496 indexss->loop_chain = gfc_ss_terminator;
6497 newss->data.info.subscript[n] = indexss;
6498 break;
6499
6500 case DIMEN_RANGE:
6501 /* We don't add anything for sections, just remember this
6502 dimension for later. */
6503 newss->data.info.dim[newss->data.info.dimen] = n;
6504 newss->data.info.dimen++;
6505 break;
6506
6507 case DIMEN_VECTOR:
7a70c12d
RS
6508 /* Create a GFC_SS_VECTOR index in which we can store
6509 the vector's descriptor. */
6510 indexss = gfc_get_ss ();
6de9cd9a 6511 indexss->type = GFC_SS_VECTOR;
7a70c12d
RS
6512 indexss->expr = ar->start[n];
6513 indexss->next = gfc_ss_terminator;
6514 indexss->loop_chain = gfc_ss_terminator;
6de9cd9a 6515 newss->data.info.subscript[n] = indexss;
6de9cd9a
DN
6516 newss->data.info.dim[newss->data.info.dimen] = n;
6517 newss->data.info.dimen++;
6518 break;
6519
6520 default:
6521 /* We should know what sort of section it is by now. */
6e45f57b 6522 gcc_unreachable ();
6de9cd9a
DN
6523 }
6524 }
6525 /* We should have at least one non-elemental dimension. */
6e45f57b 6526 gcc_assert (newss->data.info.dimen > 0);
068e7338 6527 ss = newss;
6de9cd9a
DN
6528 break;
6529
6530 default:
6531 /* We should know what sort of section it is by now. */
6e45f57b 6532 gcc_unreachable ();
6de9cd9a
DN
6533 }
6534
6535 }
6536 return ss;
6537}
6538
6539
6540/* Walk an expression operator. If only one operand of a binary expression is
6541 scalar, we must also add the scalar term to the SS chain. */
6542
6543static gfc_ss *
6544gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
6545{
6546 gfc_ss *head;
6547 gfc_ss *head2;
6548 gfc_ss *newss;
6549
58b03ab2
TS
6550 head = gfc_walk_subexpr (ss, expr->value.op.op1);
6551 if (expr->value.op.op2 == NULL)
6de9cd9a
DN
6552 head2 = head;
6553 else
58b03ab2 6554 head2 = gfc_walk_subexpr (head, expr->value.op.op2);
6de9cd9a
DN
6555
6556 /* All operands are scalar. Pass back and let the caller deal with it. */
6557 if (head2 == ss)
6558 return head2;
6559
f7b529fa 6560 /* All operands require scalarization. */
58b03ab2 6561 if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
6de9cd9a
DN
6562 return head2;
6563
6564 /* One of the operands needs scalarization, the other is scalar.
6565 Create a gfc_ss for the scalar expression. */
6566 newss = gfc_get_ss ();
6567 newss->type = GFC_SS_SCALAR;
6568 if (head == ss)
6569 {
6570 /* First operand is scalar. We build the chain in reverse order, so
df2fba9e 6571 add the scalar SS after the second operand. */
6de9cd9a
DN
6572 head = head2;
6573 while (head && head->next != ss)
6574 head = head->next;
6575 /* Check we haven't somehow broken the chain. */
6e45f57b 6576 gcc_assert (head);
6de9cd9a
DN
6577 newss->next = ss;
6578 head->next = newss;
58b03ab2 6579 newss->expr = expr->value.op.op1;
6de9cd9a
DN
6580 }
6581 else /* head2 == head */
6582 {
6e45f57b 6583 gcc_assert (head2 == head);
6de9cd9a
DN
6584 /* Second operand is scalar. */
6585 newss->next = head2;
6586 head2 = newss;
58b03ab2 6587 newss->expr = expr->value.op.op2;
6de9cd9a
DN
6588 }
6589
6590 return head2;
6591}
6592
6593
6594/* Reverse a SS chain. */
6595
48474141 6596gfc_ss *
6de9cd9a
DN
6597gfc_reverse_ss (gfc_ss * ss)
6598{
6599 gfc_ss *next;
6600 gfc_ss *head;
6601
6e45f57b 6602 gcc_assert (ss != NULL);
6de9cd9a
DN
6603
6604 head = gfc_ss_terminator;
6605 while (ss != gfc_ss_terminator)
6606 {
6607 next = ss->next;
6e45f57b
PB
6608 /* Check we didn't somehow break the chain. */
6609 gcc_assert (next != NULL);
6de9cd9a
DN
6610 ss->next = head;
6611 head = ss;
6612 ss = next;
6613 }
6614
6615 return (head);
6616}
6617
6618
6619/* Walk the arguments of an elemental function. */
6620
6621gfc_ss *
48474141 6622gfc_walk_elemental_function_args (gfc_ss * ss, gfc_actual_arglist *arg,
6de9cd9a
DN
6623 gfc_ss_type type)
6624{
6de9cd9a
DN
6625 int scalar;
6626 gfc_ss *head;
6627 gfc_ss *tail;
6628 gfc_ss *newss;
6629
6630 head = gfc_ss_terminator;
6631 tail = NULL;
6632 scalar = 1;
48474141 6633 for (; arg; arg = arg->next)
6de9cd9a
DN
6634 {
6635 if (!arg->expr)
6636 continue;
6637
6638 newss = gfc_walk_subexpr (head, arg->expr);
6639 if (newss == head)
6640 {
1f2959f0 6641 /* Scalar argument. */
6de9cd9a
DN
6642 newss = gfc_get_ss ();
6643 newss->type = type;
6644 newss->expr = arg->expr;
6645 newss->next = head;
6646 }
6647 else
6648 scalar = 0;
6649
6650 head = newss;
6651 if (!tail)
6652 {
6653 tail = head;
6654 while (tail->next != gfc_ss_terminator)
6655 tail = tail->next;
6656 }
6657 }
6658
6659 if (scalar)
6660 {
6661 /* If all the arguments are scalar we don't need the argument SS. */
6662 gfc_free_ss_chain (head);
6663 /* Pass it back. */
6664 return ss;
6665 }
6666
6667 /* Add it onto the existing chain. */
6668 tail->next = ss;
6669 return head;
6670}
6671
6672
6673/* Walk a function call. Scalar functions are passed back, and taken out of
6674 scalarization loops. For elemental functions we walk their arguments.
6675 The result of functions returning arrays is stored in a temporary outside
6676 the loop, so that the function is only called once. Hence we do not need
6677 to walk their arguments. */
6678
6679static gfc_ss *
6680gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
6681{
6682 gfc_ss *newss;
6683 gfc_intrinsic_sym *isym;
6684 gfc_symbol *sym;
c74b74a8 6685 gfc_component *comp = NULL;
6de9cd9a
DN
6686
6687 isym = expr->value.function.isym;
6688
13413760 6689 /* Handle intrinsic functions separately. */
6de9cd9a
DN
6690 if (isym)
6691 return gfc_walk_intrinsic_function (ss, expr, isym);
6692
6693 sym = expr->value.function.esym;
6694 if (!sym)
6695 sym = expr->symtree->n.sym;
6696
6697 /* A function that returns arrays. */
f64edc8b 6698 gfc_is_proc_ptr_comp (expr, &comp);
c74b74a8
JW
6699 if ((!comp && gfc_return_by_reference (sym) && sym->result->attr.dimension)
6700 || (comp && comp->attr.dimension))
6de9cd9a
DN
6701 {
6702 newss = gfc_get_ss ();
6703 newss->type = GFC_SS_FUNCTION;
6704 newss->expr = expr;
6705 newss->next = ss;
6706 newss->data.info.dimen = expr->rank;
6707 return newss;
6708 }
6709
6710 /* Walk the parameters of an elemental function. For now we always pass
6711 by reference. */
6712 if (sym->attr.elemental)
48474141
PT
6713 return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
6714 GFC_SS_REFERENCE);
6de9cd9a 6715
e7dc5b4f 6716 /* Scalar functions are OK as these are evaluated outside the scalarization
6de9cd9a
DN
6717 loop. Pass back and let the caller deal with it. */
6718 return ss;
6719}
6720
6721
6722/* An array temporary is constructed for array constructors. */
6723
6724static gfc_ss *
6725gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
6726{
6727 gfc_ss *newss;
6728 int n;
6729
6730 newss = gfc_get_ss ();
6731 newss->type = GFC_SS_CONSTRUCTOR;
6732 newss->expr = expr;
6733 newss->next = ss;
6734 newss->data.info.dimen = expr->rank;
6735 for (n = 0; n < expr->rank; n++)
6736 newss->data.info.dim[n] = n;
6737
6738 return newss;
6739}
6740
6741
1f2959f0 6742/* Walk an expression. Add walked expressions to the head of the SS chain.
aa9c57ec 6743 A wholly scalar expression will not be added. */
6de9cd9a
DN
6744
6745static gfc_ss *
6746gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
6747{
6748 gfc_ss *head;
6749
6750 switch (expr->expr_type)
6751 {
6752 case EXPR_VARIABLE:
6753 head = gfc_walk_variable_expr (ss, expr);
6754 return head;
6755
6756 case EXPR_OP:
6757 head = gfc_walk_op_expr (ss, expr);
6758 return head;
6759
6760 case EXPR_FUNCTION:
6761 head = gfc_walk_function_expr (ss, expr);
6762 return head;
6763
6764 case EXPR_CONSTANT:
6765 case EXPR_NULL:
6766 case EXPR_STRUCTURE:
6767 /* Pass back and let the caller deal with it. */
6768 break;
6769
6770 case EXPR_ARRAY:
6771 head = gfc_walk_array_constructor (ss, expr);
6772 return head;
6773
6774 case EXPR_SUBSTRING:
6775 /* Pass back and let the caller deal with it. */
6776 break;
6777
6778 default:
6779 internal_error ("bad expression type during walk (%d)",
6780 expr->expr_type);
6781 }
6782 return ss;
6783}
6784
6785
6786/* Entry point for expression walking.
6787 A return value equal to the passed chain means this is
6788 a scalar expression. It is up to the caller to take whatever action is
1f2959f0 6789 necessary to translate these. */
6de9cd9a
DN
6790
6791gfc_ss *
6792gfc_walk_expr (gfc_expr * expr)
6793{
6794 gfc_ss *res;
6795
6796 res = gfc_walk_subexpr (gfc_ss_terminator, expr);
6797 return gfc_reverse_ss (res);
6798}