Debian Codegraph @

gcc/fortran/trans-array.cc

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