Branch data Line data Source code
1 : : /* Array translation routines
2 : : Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 : : Contributed by Paul Brook <paul@nowt.org>
4 : : and Steven Bosscher <s.bosscher@student.tudelft.nl>
5 : :
6 : : This file is part of GCC.
7 : :
8 : : GCC is free software; you can redistribute it and/or modify it under
9 : : the terms of the GNU General Public License as published by the Free
10 : : Software Foundation; either version 3, or (at your option) any later
11 : : version.
12 : :
13 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : : for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along with GCC; see the file COPYING3. If not see
20 : : <http://www.gnu.org/licenses/>. */
21 : :
22 : : /* trans-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 : 50617 : gfc_array_dataptr_type (tree desc)
105 : : {
106 : 50617 : 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 : 84935 : gfc_get_cfi_descriptor_field (tree desc, unsigned field_idx)
124 : : {
125 : 84935 : tree type = TREE_TYPE (desc);
126 : 84935 : 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 : 84935 : tree field = gfc_advance_chain (TYPE_FIELDS (type), field_idx);
132 : 84935 : gcc_assert (field != NULL_TREE);
133 : :
134 : 84935 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
135 : 84935 : desc, field, NULL_TREE);
136 : : }
137 : :
138 : : tree
139 : 14198 : gfc_get_cfi_desc_base_addr (tree desc)
140 : : {
141 : 14198 : return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_BASE_ADDR);
142 : : }
143 : :
144 : : tree
145 : 10680 : gfc_get_cfi_desc_elem_len (tree desc)
146 : : {
147 : 10680 : return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_ELEM_LEN);
148 : : }
149 : :
150 : : tree
151 : 7190 : gfc_get_cfi_desc_version (tree desc)
152 : : {
153 : 7190 : return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_VERSION);
154 : : }
155 : :
156 : : tree
157 : 7815 : gfc_get_cfi_desc_rank (tree desc)
158 : : {
159 : 7815 : return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_RANK);
160 : : }
161 : :
162 : : tree
163 : 7282 : gfc_get_cfi_desc_type (tree desc)
164 : : {
165 : 7282 : return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_TYPE);
166 : : }
167 : :
168 : : tree
169 : 7190 : gfc_get_cfi_desc_attribute (tree desc)
170 : : {
171 : 7190 : return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_ATTRIBUTE);
172 : : }
173 : :
174 : : static tree
175 : 30580 : gfc_get_cfi_dim_item (tree desc, tree idx, unsigned field_idx)
176 : : {
177 : 30580 : tree tmp = gfc_get_cfi_descriptor_field (desc, CFI_FIELD_DIM);
178 : 30580 : tmp = gfc_build_array_ref (tmp, idx, NULL_TREE, true);
179 : 30580 : tree field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (tmp)), field_idx);
180 : 30580 : gcc_assert (field != NULL_TREE);
181 : 30580 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
182 : 30580 : tmp, field, NULL_TREE);
183 : : }
184 : :
185 : : tree
186 : 6786 : gfc_get_cfi_dim_lbound (tree desc, tree idx)
187 : : {
188 : 6786 : return gfc_get_cfi_dim_item (desc, idx, CFI_DIM_FIELD_LOWER_BOUND);
189 : : }
190 : :
191 : : tree
192 : 11926 : gfc_get_cfi_dim_extent (tree desc, tree idx)
193 : : {
194 : 11926 : return gfc_get_cfi_dim_item (desc, idx, CFI_DIM_FIELD_EXTENT);
195 : : }
196 : :
197 : : tree
198 : 11868 : gfc_get_cfi_dim_sm (tree desc, tree idx)
199 : : {
200 : 11868 : 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 : 1581745 : gfc_get_descriptor_field (tree desc, unsigned field_idx)
246 : : {
247 : 1581745 : tree type = TREE_TYPE (desc);
248 : 1581745 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
249 : :
250 : 1581745 : tree field = gfc_advance_chain (TYPE_FIELDS (type), field_idx);
251 : 1581745 : gcc_assert (field != NULL_TREE);
252 : :
253 : 1581745 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
254 : 1581745 : 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 : 228510 : gfc_conv_descriptor_data_get (tree desc)
262 : : {
263 : 228510 : tree type = TREE_TYPE (desc);
264 : 228510 : if (TREE_CODE (type) == REFERENCE_TYPE)
265 : 0 : gcc_unreachable ();
266 : :
267 : 228510 : tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
268 : 228510 : 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 : 123510 : gfc_conv_descriptor_data_set (stmtblock_t *block, tree desc, tree value)
281 : : {
282 : 123510 : tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
283 : 123510 : gfc_add_modify (block, field, fold_convert (TREE_TYPE (field), value));
284 : 123510 : }
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 : 1003 : gfc_conv_descriptor_data_addr (tree desc)
292 : : {
293 : 1003 : tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
294 : 1003 : return gfc_build_addr_expr (NULL_TREE, field);
295 : : }
296 : :
297 : : static tree
298 : 167785 : gfc_conv_descriptor_offset (tree desc)
299 : : {
300 : 167785 : tree field = gfc_get_descriptor_field (desc, OFFSET_FIELD);
301 : 167785 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
302 : 167785 : return field;
303 : : }
304 : :
305 : : tree
306 : 60402 : gfc_conv_descriptor_offset_get (tree desc)
307 : : {
308 : 60402 : return gfc_conv_descriptor_offset (desc);
309 : : }
310 : :
311 : : void
312 : 103108 : gfc_conv_descriptor_offset_set (stmtblock_t *block, tree desc,
313 : : tree value)
314 : : {
315 : 103108 : tree t = gfc_conv_descriptor_offset (desc);
316 : 103108 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
317 : 103108 : }
318 : :
319 : :
320 : : tree
321 : 146574 : gfc_conv_descriptor_dtype (tree desc)
322 : : {
323 : 146574 : tree field = gfc_get_descriptor_field (desc, DTYPE_FIELD);
324 : 146574 : gcc_assert (TREE_TYPE (field) == get_dtype_type_node ());
325 : 146574 : return field;
326 : : }
327 : :
328 : : static tree
329 : 134632 : gfc_conv_descriptor_span (tree desc)
330 : : {
331 : 134632 : tree field = gfc_get_descriptor_field (desc, SPAN_FIELD);
332 : 134632 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
333 : 134632 : return field;
334 : : }
335 : :
336 : : tree
337 : 28351 : gfc_conv_descriptor_span_get (tree desc)
338 : : {
339 : 28351 : return gfc_conv_descriptor_span (desc);
340 : : }
341 : :
342 : : void
343 : 106281 : gfc_conv_descriptor_span_set (stmtblock_t *block, tree desc,
344 : : tree value)
345 : : {
346 : 106281 : tree t = gfc_conv_descriptor_span (desc);
347 : 106281 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
348 : 106281 : }
349 : :
350 : :
351 : : tree
352 : 17518 : gfc_conv_descriptor_rank (tree desc)
353 : : {
354 : 17518 : tree tmp;
355 : 17518 : tree dtype;
356 : :
357 : 17518 : dtype = gfc_conv_descriptor_dtype (desc);
358 : 17518 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_RANK);
359 : 17518 : gcc_assert (tmp != NULL_TREE
360 : : && TREE_TYPE (tmp) == signed_char_type_node);
361 : 17518 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
362 : 17518 : dtype, tmp, NULL_TREE);
363 : : }
364 : :
365 : :
366 : : tree
367 : 118 : gfc_conv_descriptor_version (tree desc)
368 : : {
369 : 118 : tree tmp;
370 : 118 : tree dtype;
371 : :
372 : 118 : dtype = gfc_conv_descriptor_dtype (desc);
373 : 118 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_VERSION);
374 : 118 : gcc_assert (tmp != NULL_TREE
375 : : && TREE_TYPE (tmp) == integer_type_node);
376 : 118 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
377 : 118 : dtype, tmp, NULL_TREE);
378 : : }
379 : :
380 : :
381 : : /* Return the element length from the descriptor dtype field. */
382 : :
383 : : tree
384 : 7506 : gfc_conv_descriptor_elem_len (tree desc)
385 : : {
386 : 7506 : tree tmp;
387 : 7506 : tree dtype;
388 : :
389 : 7506 : dtype = gfc_conv_descriptor_dtype (desc);
390 : 7506 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
391 : : GFC_DTYPE_ELEM_LEN);
392 : 7506 : gcc_assert (tmp != NULL_TREE
393 : : && TREE_TYPE (tmp) == size_type_node);
394 : 7506 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
395 : 7506 : dtype, tmp, NULL_TREE);
396 : : }
397 : :
398 : :
399 : : tree
400 : 0 : gfc_conv_descriptor_attribute (tree desc)
401 : : {
402 : 0 : tree tmp;
403 : 0 : tree dtype;
404 : :
405 : 0 : dtype = gfc_conv_descriptor_dtype (desc);
406 : 0 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
407 : : GFC_DTYPE_ATTRIBUTE);
408 : 0 : gcc_assert (tmp!= NULL_TREE
409 : : && TREE_TYPE (tmp) == short_integer_type_node);
410 : 0 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
411 : 0 : dtype, tmp, NULL_TREE);
412 : : }
413 : :
414 : : tree
415 : 73 : gfc_conv_descriptor_type (tree desc)
416 : : {
417 : 73 : tree tmp;
418 : 73 : tree dtype;
419 : :
420 : 73 : dtype = gfc_conv_descriptor_dtype (desc);
421 : 73 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_TYPE);
422 : 73 : gcc_assert (tmp!= NULL_TREE
423 : : && TREE_TYPE (tmp) == signed_char_type_node);
424 : 73 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
425 : 73 : dtype, tmp, NULL_TREE);
426 : : }
427 : :
428 : : tree
429 : 778059 : gfc_get_descriptor_dimension (tree desc)
430 : : {
431 : 778059 : tree field = gfc_get_descriptor_field (desc, DIMENSION_FIELD);
432 : 778059 : gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
433 : : && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == RECORD_TYPE);
434 : 778059 : return field;
435 : : }
436 : :
437 : :
438 : : static tree
439 : 774197 : gfc_conv_descriptor_dimension (tree desc, tree dim)
440 : : {
441 : 774197 : tree tmp;
442 : :
443 : 774197 : tmp = gfc_get_descriptor_dimension (desc);
444 : :
445 : 774197 : return gfc_build_array_ref (tmp, dim, NULL_TREE, true);
446 : : }
447 : :
448 : :
449 : : tree
450 : 1672 : gfc_conv_descriptor_token (tree desc)
451 : : {
452 : 1672 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
453 : 1672 : tree field = gfc_get_descriptor_field (desc, CAF_TOKEN_FIELD);
454 : : /* Should be a restricted pointer - except in the finalization wrapper. */
455 : 1672 : gcc_assert (TREE_TYPE (field) == prvoid_type_node
456 : : || TREE_TYPE (field) == pvoid_type_node);
457 : 1672 : return field;
458 : : }
459 : :
460 : : static tree
461 : 774197 : gfc_conv_descriptor_subfield (tree desc, tree dim, unsigned field_idx)
462 : : {
463 : 774197 : tree tmp = gfc_conv_descriptor_dimension (desc, dim);
464 : 774197 : tree field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (tmp)), field_idx);
465 : 774197 : gcc_assert (field != NULL_TREE);
466 : :
467 : 774197 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
468 : 774197 : tmp, field, NULL_TREE);
469 : : }
470 : :
471 : : static tree
472 : 210934 : gfc_conv_descriptor_stride (tree desc, tree dim)
473 : : {
474 : 210934 : tree field = gfc_conv_descriptor_subfield (desc, dim, STRIDE_SUBFIELD);
475 : 210934 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
476 : 210934 : return field;
477 : : }
478 : :
479 : : tree
480 : 123901 : gfc_conv_descriptor_stride_get (tree desc, tree dim)
481 : : {
482 : 123901 : tree type = TREE_TYPE (desc);
483 : 123901 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
484 : 123901 : if (integer_zerop (dim)
485 : 123901 : && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
486 : 38166 : ||GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT
487 : 37098 : ||GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT
488 : 36966 : ||GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT))
489 : 51272 : return gfc_index_one_node;
490 : :
491 : 72629 : return gfc_conv_descriptor_stride (desc, dim);
492 : : }
493 : :
494 : : void
495 : 138305 : gfc_conv_descriptor_stride_set (stmtblock_t *block, tree desc,
496 : : tree dim, tree value)
497 : : {
498 : 138305 : tree t = gfc_conv_descriptor_stride (desc, dim);
499 : 138305 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
500 : 138305 : }
501 : :
502 : : static tree
503 : 291790 : gfc_conv_descriptor_lbound (tree desc, tree dim)
504 : : {
505 : 291790 : tree field = gfc_conv_descriptor_subfield (desc, dim, LBOUND_SUBFIELD);
506 : 291790 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
507 : 291790 : return field;
508 : : }
509 : :
510 : : tree
511 : 149992 : gfc_conv_descriptor_lbound_get (tree desc, tree dim)
512 : : {
513 : 149992 : return gfc_conv_descriptor_lbound (desc, dim);
514 : : }
515 : :
516 : : void
517 : 141798 : gfc_conv_descriptor_lbound_set (stmtblock_t *block, tree desc,
518 : : tree dim, tree value)
519 : : {
520 : 141798 : tree t = gfc_conv_descriptor_lbound (desc, dim);
521 : 141798 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
522 : 141798 : }
523 : :
524 : : static tree
525 : 271473 : gfc_conv_descriptor_ubound (tree desc, tree dim)
526 : : {
527 : 271473 : tree field = gfc_conv_descriptor_subfield (desc, dim, UBOUND_SUBFIELD);
528 : 271473 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
529 : 271473 : return field;
530 : : }
531 : :
532 : : tree
533 : 128680 : gfc_conv_descriptor_ubound_get (tree desc, tree dim)
534 : : {
535 : 128680 : return gfc_conv_descriptor_ubound (desc, dim);
536 : : }
537 : :
538 : : void
539 : 142793 : gfc_conv_descriptor_ubound_set (stmtblock_t *block, tree desc,
540 : : tree dim, tree value)
541 : : {
542 : 142793 : tree t = gfc_conv_descriptor_ubound (desc, dim);
543 : 142793 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
544 : 142793 : }
545 : :
546 : : /* Build a null array descriptor constructor. */
547 : :
548 : : tree
549 : 798 : gfc_build_null_descriptor (tree type)
550 : : {
551 : 798 : tree field;
552 : 798 : tree tmp;
553 : :
554 : 798 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
555 : 798 : gcc_assert (DATA_FIELD == 0);
556 : 798 : field = TYPE_FIELDS (type);
557 : :
558 : : /* Set a NULL data pointer. */
559 : 798 : tmp = build_constructor_single (type, field, null_pointer_node);
560 : 798 : TREE_CONSTANT (tmp) = 1;
561 : : /* All other fields are ignored. */
562 : :
563 : 798 : return tmp;
564 : : }
565 : :
566 : :
567 : : /* Modify a descriptor such that the lbound of a given dimension is the value
568 : : specified. This also updates ubound and offset accordingly. */
569 : :
570 : : void
571 : 839 : gfc_conv_shift_descriptor_lbound (stmtblock_t* block, tree desc,
572 : : int dim, tree new_lbound)
573 : : {
574 : 839 : tree offs, ubound, lbound, stride;
575 : 839 : tree diff, offs_diff;
576 : :
577 : 839 : new_lbound = fold_convert (gfc_array_index_type, new_lbound);
578 : :
579 : 839 : offs = gfc_conv_descriptor_offset_get (desc);
580 : 839 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
581 : 839 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
582 : 839 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[dim]);
583 : :
584 : : /* Get difference (new - old) by which to shift stuff. */
585 : 839 : diff = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
586 : : new_lbound, lbound);
587 : :
588 : : /* Shift ubound and offset accordingly. This has to be done before
589 : : updating the lbound, as they depend on the lbound expression! */
590 : 839 : ubound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
591 : : ubound, diff);
592 : 839 : gfc_conv_descriptor_ubound_set (block, desc, gfc_rank_cst[dim], ubound);
593 : 839 : offs_diff = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
594 : : diff, stride);
595 : 839 : offs = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
596 : : offs, offs_diff);
597 : 839 : gfc_conv_descriptor_offset_set (block, desc, offs);
598 : :
599 : : /* Finally set lbound to value we want. */
600 : 839 : gfc_conv_descriptor_lbound_set (block, desc, gfc_rank_cst[dim], new_lbound);
601 : 839 : }
602 : :
603 : :
604 : : /* Obtain offsets for trans-types.cc(gfc_get_array_descr_info). */
605 : :
606 : : void
607 : 230723 : gfc_get_descriptor_offsets_for_info (const_tree desc_type, tree *data_off,
608 : : tree *dtype_off, tree *span_off,
609 : : tree *dim_off, tree *dim_size,
610 : : tree *stride_suboff, tree *lower_suboff,
611 : : tree *upper_suboff)
612 : : {
613 : 230723 : tree field;
614 : 230723 : tree type;
615 : :
616 : 230723 : type = TYPE_MAIN_VARIANT (desc_type);
617 : 230723 : field = gfc_advance_chain (TYPE_FIELDS (type), DATA_FIELD);
618 : 230723 : *data_off = byte_position (field);
619 : 230723 : field = gfc_advance_chain (TYPE_FIELDS (type), DTYPE_FIELD);
620 : 230723 : *dtype_off = byte_position (field);
621 : 230723 : field = gfc_advance_chain (TYPE_FIELDS (type), SPAN_FIELD);
622 : 230723 : *span_off = byte_position (field);
623 : 230723 : field = gfc_advance_chain (TYPE_FIELDS (type), DIMENSION_FIELD);
624 : 230723 : *dim_off = byte_position (field);
625 : 230723 : type = TREE_TYPE (TREE_TYPE (field));
626 : 230723 : *dim_size = TYPE_SIZE_UNIT (type);
627 : 230723 : field = gfc_advance_chain (TYPE_FIELDS (type), STRIDE_SUBFIELD);
628 : 230723 : *stride_suboff = byte_position (field);
629 : 230723 : field = gfc_advance_chain (TYPE_FIELDS (type), LBOUND_SUBFIELD);
630 : 230723 : *lower_suboff = byte_position (field);
631 : 230723 : field = gfc_advance_chain (TYPE_FIELDS (type), UBOUND_SUBFIELD);
632 : 230723 : *upper_suboff = byte_position (field);
633 : 230723 : }
634 : :
635 : :
636 : : /* Cleanup those #defines. */
637 : :
638 : : #undef DATA_FIELD
639 : : #undef OFFSET_FIELD
640 : : #undef DTYPE_FIELD
641 : : #undef SPAN_FIELD
642 : : #undef DIMENSION_FIELD
643 : : #undef CAF_TOKEN_FIELD
644 : : #undef STRIDE_SUBFIELD
645 : : #undef LBOUND_SUBFIELD
646 : : #undef UBOUND_SUBFIELD
647 : :
648 : :
649 : : /* Mark a SS chain as used. Flags specifies in which loops the SS is used.
650 : : flags & 1 = Main loop body.
651 : : flags & 2 = temp copy loop. */
652 : :
653 : : void
654 : 139088 : gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
655 : : {
656 : 323458 : for (; ss != gfc_ss_terminator; ss = ss->next)
657 : 184370 : ss->info->useflags = flags;
658 : 139088 : }
659 : :
660 : :
661 : : /* Free a gfc_ss chain. */
662 : :
663 : : void
664 : 147732 : gfc_free_ss_chain (gfc_ss * ss)
665 : : {
666 : 147732 : gfc_ss *next;
667 : :
668 : 303562 : while (ss != gfc_ss_terminator)
669 : : {
670 : 155830 : gcc_assert (ss != NULL);
671 : 155830 : next = ss->next;
672 : 155830 : gfc_free_ss (ss);
673 : 155830 : ss = next;
674 : : }
675 : 147732 : }
676 : :
677 : :
678 : : static void
679 : 402083 : free_ss_info (gfc_ss_info *ss_info)
680 : : {
681 : 402083 : int n;
682 : :
683 : 402083 : ss_info->refcount--;
684 : 402083 : if (ss_info->refcount > 0)
685 : : return;
686 : :
687 : 401339 : gcc_assert (ss_info->refcount == 0);
688 : :
689 : 401339 : switch (ss_info->type)
690 : : {
691 : : case GFC_SS_SECTION:
692 : 4496624 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
693 : 4215585 : if (ss_info->data.array.subscript[n])
694 : 4874 : gfc_free_ss_chain (ss_info->data.array.subscript[n]);
695 : : break;
696 : :
697 : : default:
698 : : break;
699 : : }
700 : :
701 : 401339 : free (ss_info);
702 : : }
703 : :
704 : :
705 : : /* Free a SS. */
706 : :
707 : : void
708 : 402083 : gfc_free_ss (gfc_ss * ss)
709 : : {
710 : 402083 : free_ss_info (ss->info);
711 : 402083 : free (ss);
712 : 402083 : }
713 : :
714 : :
715 : : /* Creates and initializes an array type gfc_ss struct. */
716 : :
717 : : gfc_ss *
718 : 338536 : gfc_get_array_ss (gfc_ss *next, gfc_expr *expr, int dimen, gfc_ss_type type)
719 : : {
720 : 338536 : gfc_ss *ss;
721 : 338536 : gfc_ss_info *ss_info;
722 : 338536 : int i;
723 : :
724 : 338536 : ss_info = gfc_get_ss_info ();
725 : 338536 : ss_info->refcount++;
726 : 338536 : ss_info->type = type;
727 : 338536 : ss_info->expr = expr;
728 : :
729 : 338536 : ss = gfc_get_ss ();
730 : 338536 : ss->info = ss_info;
731 : 338536 : ss->next = next;
732 : 338536 : ss->dimen = dimen;
733 : 696073 : for (i = 0; i < ss->dimen; i++)
734 : 357537 : ss->dim[i] = i;
735 : :
736 : 338536 : return ss;
737 : : }
738 : :
739 : :
740 : : /* Creates and initializes a temporary type gfc_ss struct. */
741 : :
742 : : gfc_ss *
743 : 8637 : gfc_get_temp_ss (tree type, tree string_length, int dimen)
744 : : {
745 : 8637 : gfc_ss *ss;
746 : 8637 : gfc_ss_info *ss_info;
747 : 8637 : int i;
748 : :
749 : 8637 : ss_info = gfc_get_ss_info ();
750 : 8637 : ss_info->refcount++;
751 : 8637 : ss_info->type = GFC_SS_TEMP;
752 : 8637 : ss_info->string_length = string_length;
753 : 8637 : ss_info->data.temp.type = type;
754 : :
755 : 8637 : ss = gfc_get_ss ();
756 : 8637 : ss->info = ss_info;
757 : 8637 : ss->next = gfc_ss_terminator;
758 : 8637 : ss->dimen = dimen;
759 : 19678 : for (i = 0; i < ss->dimen; i++)
760 : 11041 : ss->dim[i] = i;
761 : :
762 : 8637 : return ss;
763 : : }
764 : :
765 : :
766 : : /* Creates and initializes a scalar type gfc_ss struct. */
767 : :
768 : : gfc_ss *
769 : 58087 : gfc_get_scalar_ss (gfc_ss *next, gfc_expr *expr)
770 : : {
771 : 58087 : gfc_ss *ss;
772 : 58087 : gfc_ss_info *ss_info;
773 : :
774 : 58087 : ss_info = gfc_get_ss_info ();
775 : 58087 : ss_info->refcount++;
776 : 58087 : ss_info->type = GFC_SS_SCALAR;
777 : 58087 : ss_info->expr = expr;
778 : :
779 : 58087 : ss = gfc_get_ss ();
780 : 58087 : ss->info = ss_info;
781 : 58087 : ss->next = next;
782 : :
783 : 58087 : return ss;
784 : : }
785 : :
786 : :
787 : : /* Free all the SS associated with a loop. */
788 : :
789 : : void
790 : 148738 : gfc_cleanup_loop (gfc_loopinfo * loop)
791 : : {
792 : 148738 : gfc_loopinfo *loop_next, **ploop;
793 : 148738 : gfc_ss *ss;
794 : 148738 : gfc_ss *next;
795 : :
796 : 148738 : ss = loop->ss;
797 : 394684 : while (ss != gfc_ss_terminator)
798 : : {
799 : 245946 : gcc_assert (ss != NULL);
800 : 245946 : next = ss->loop_chain;
801 : 245946 : gfc_free_ss (ss);
802 : 245946 : ss = next;
803 : : }
804 : :
805 : : /* Remove reference to self in the parent loop. */
806 : 148738 : if (loop->parent)
807 : 553 : for (ploop = &loop->parent->nested; *ploop; ploop = &(*ploop)->next)
808 : 553 : if (*ploop == loop)
809 : : {
810 : 553 : *ploop = loop->next;
811 : 553 : break;
812 : : }
813 : :
814 : : /* Free non-freed nested loops. */
815 : 149291 : for (loop = loop->nested; loop; loop = loop_next)
816 : : {
817 : 553 : loop_next = loop->next;
818 : 553 : gfc_cleanup_loop (loop);
819 : 553 : free (loop);
820 : : }
821 : 148738 : }
822 : :
823 : :
824 : : static void
825 : 205744 : set_ss_loop (gfc_ss *ss, gfc_loopinfo *loop)
826 : : {
827 : 205744 : int n;
828 : :
829 : 458034 : for (; ss != gfc_ss_terminator; ss = ss->next)
830 : : {
831 : 252290 : ss->loop = loop;
832 : :
833 : 252290 : if (ss->info->type == GFC_SS_SCALAR
834 : : || ss->info->type == GFC_SS_REFERENCE
835 : 211354 : || ss->info->type == GFC_SS_TEMP)
836 : 49573 : continue;
837 : :
838 : 3243472 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
839 : 3040755 : if (ss->info->data.array.subscript[n] != NULL)
840 : 4724 : set_ss_loop (ss->info->data.array.subscript[n], loop);
841 : : }
842 : 205744 : }
843 : :
844 : :
845 : : /* Associate a SS chain with a loop. */
846 : :
847 : : void
848 : 201020 : gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
849 : : {
850 : 201020 : gfc_ss *ss;
851 : 201020 : gfc_loopinfo *nested_loop;
852 : :
853 : 201020 : if (head == gfc_ss_terminator)
854 : : return;
855 : :
856 : 201020 : set_ss_loop (head, loop);
857 : :
858 : 201020 : ss = head;
859 : 649606 : for (; ss && ss != gfc_ss_terminator; ss = ss->next)
860 : : {
861 : 247566 : if (ss->nested_ss)
862 : : {
863 : 737 : nested_loop = ss->nested_ss->loop;
864 : :
865 : : /* More than one ss can belong to the same loop. Hence, we add the
866 : : loop to the chain only if it is different from the previously
867 : : added one, to avoid duplicate nested loops. */
868 : 737 : if (nested_loop != loop->nested)
869 : : {
870 : 553 : gcc_assert (nested_loop->parent == NULL);
871 : 553 : nested_loop->parent = loop;
872 : :
873 : 553 : gcc_assert (nested_loop->next == NULL);
874 : 553 : nested_loop->next = loop->nested;
875 : 553 : loop->nested = nested_loop;
876 : : }
877 : : else
878 : 184 : gcc_assert (nested_loop->parent == loop);
879 : : }
880 : :
881 : 247566 : if (ss->next == gfc_ss_terminator)
882 : 201020 : ss->loop_chain = loop->ss;
883 : : else
884 : 46546 : ss->loop_chain = ss->next;
885 : : }
886 : 201020 : gcc_assert (ss == gfc_ss_terminator);
887 : 201020 : loop->ss = head;
888 : : }
889 : :
890 : :
891 : : /* Returns true if the expression is an array pointer. */
892 : :
893 : : static bool
894 : 308506 : is_pointer_array (tree expr)
895 : : {
896 : 308506 : if (expr == NULL_TREE
897 : 308506 : || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr))
898 : 384962 : || GFC_CLASS_TYPE_P (TREE_TYPE (expr)))
899 : : return false;
900 : :
901 : 76456 : if (VAR_P (expr)
902 : 76456 : && GFC_DECL_PTR_ARRAY_P (expr))
903 : : return true;
904 : :
905 : 70992 : if (TREE_CODE (expr) == PARM_DECL
906 : 70992 : && GFC_DECL_PTR_ARRAY_P (expr))
907 : : return true;
908 : :
909 : 70992 : if (INDIRECT_REF_P (expr)
910 : 70992 : && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 0)))
911 : : return true;
912 : :
913 : : /* The field declaration is marked as an pointer array. */
914 : 68918 : if (TREE_CODE (expr) == COMPONENT_REF
915 : 10290 : && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 1))
916 : 71152 : && !GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
917 : 2234 : return true;
918 : :
919 : : return false;
920 : : }
921 : :
922 : :
923 : : /* If the symbol or expression reference a CFI descriptor, return the
924 : : pointer to the converted gfc descriptor. If an array reference is
925 : : present as the last argument, check that it is the one applied to
926 : : the CFI descriptor in the expression. Note that the CFI object is
927 : : always the symbol in the expression! */
928 : :
929 : : static bool
930 : 310441 : get_CFI_desc (gfc_symbol *sym, gfc_expr *expr,
931 : : tree *desc, gfc_array_ref *ar)
932 : : {
933 : 310441 : tree tmp;
934 : :
935 : 310441 : if (!is_CFI_desc (sym, expr))
936 : : return false;
937 : :
938 : 4727 : if (expr && ar)
939 : : {
940 : 4061 : if (!(expr->ref && expr->ref->type == REF_ARRAY)
941 : 4043 : || (&expr->ref->u.ar != ar))
942 : : return false;
943 : : }
944 : :
945 : 4697 : if (sym == NULL)
946 : 1108 : tmp = expr->symtree->n.sym->backend_decl;
947 : : else
948 : 3589 : tmp = sym->backend_decl;
949 : :
950 : 4697 : if (tmp && DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
951 : 0 : tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
952 : :
953 : 4697 : *desc = tmp;
954 : 4697 : return true;
955 : : }
956 : :
957 : :
958 : : /* Return the span of an array. */
959 : :
960 : : tree
961 : 51728 : gfc_get_array_span (tree desc, gfc_expr *expr)
962 : : {
963 : 51728 : tree tmp;
964 : :
965 : 51728 : if (is_pointer_array (desc)
966 : 51728 : || (get_CFI_desc (NULL, expr, &desc, NULL)
967 : 1332 : && (POINTER_TYPE_P (TREE_TYPE (desc))
968 : 666 : ? GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc)))
969 : 0 : : GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))))
970 : : {
971 : 491 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
972 : 0 : desc = build_fold_indirect_ref_loc (input_location, desc);
973 : :
974 : : /* This will have the span field set. */
975 : 491 : tmp = gfc_conv_descriptor_span_get (desc);
976 : : }
977 : 51237 : else if (expr->ts.type == BT_ASSUMED)
978 : : {
979 : 127 : if (DECL_LANG_SPECIFIC (desc) && GFC_DECL_SAVED_DESCRIPTOR (desc))
980 : 127 : desc = GFC_DECL_SAVED_DESCRIPTOR (desc);
981 : 127 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
982 : 127 : desc = build_fold_indirect_ref_loc (input_location, desc);
983 : 127 : tmp = gfc_conv_descriptor_span_get (desc);
984 : : }
985 : 51110 : else if (TREE_CODE (desc) == COMPONENT_REF
986 : 555 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
987 : 51212 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (desc, 0))))
988 : : {
989 : : /* The descriptor is a class _data field and so use the vtable
990 : : size for the receiving span field. */
991 : 42 : tmp = gfc_get_vptr_from_expr (desc);
992 : 42 : tmp = gfc_vptr_size_get (tmp);
993 : : }
994 : 51068 : else if (expr && expr->expr_type == EXPR_VARIABLE
995 : 45740 : && expr->symtree->n.sym->ts.type == BT_CLASS
996 : 1001 : && expr->ref->type == REF_COMPONENT
997 : 1001 : && expr->ref->next->type == REF_ARRAY
998 : 1001 : && expr->ref->next->next == NULL
999 : 977 : && CLASS_DATA (expr->symtree->n.sym)->attr.dimension)
1000 : : {
1001 : : /* Dummys come in sometimes with the descriptor detached from
1002 : : the class field or declaration. */
1003 : 955 : tmp = gfc_class_vptr_get (expr->symtree->n.sym->backend_decl);
1004 : 955 : tmp = gfc_vptr_size_get (tmp);
1005 : : }
1006 : : else
1007 : : {
1008 : : /* If none of the fancy stuff works, the span is the element
1009 : : size of the array. Attempt to deal with unbounded character
1010 : : types if possible. Otherwise, return NULL_TREE. */
1011 : 50113 : tmp = gfc_get_element_type (TREE_TYPE (desc));
1012 : 50113 : if (tmp && TREE_CODE (tmp) == ARRAY_TYPE && TYPE_STRING_FLAG (tmp))
1013 : : {
1014 : 10388 : gcc_assert (expr->ts.type == BT_CHARACTER);
1015 : :
1016 : 10388 : tmp = gfc_get_character_len_in_bytes (tmp);
1017 : :
1018 : 10388 : if (tmp == NULL_TREE || integer_zerop (tmp))
1019 : : {
1020 : 80 : tree bs;
1021 : :
1022 : 80 : tmp = gfc_get_expr_charlen (expr);
1023 : 80 : tmp = fold_convert (gfc_array_index_type, tmp);
1024 : 80 : bs = build_int_cst (gfc_array_index_type, expr->ts.kind);
1025 : 80 : tmp = fold_build2_loc (input_location, MULT_EXPR,
1026 : : gfc_array_index_type, tmp, bs);
1027 : : }
1028 : :
1029 : 10388 : tmp = (tmp && !integer_zerop (tmp))
1030 : 20696 : ? (fold_convert (gfc_array_index_type, tmp)) : (NULL_TREE);
1031 : : }
1032 : : else
1033 : 39725 : tmp = fold_convert (gfc_array_index_type,
1034 : : size_in_bytes (tmp));
1035 : : }
1036 : 51728 : return tmp;
1037 : : }
1038 : :
1039 : :
1040 : : /* Generate an initializer for a static pointer or allocatable array. */
1041 : :
1042 : : void
1043 : 179 : gfc_trans_static_array_pointer (gfc_symbol * sym)
1044 : : {
1045 : 179 : tree type;
1046 : :
1047 : 179 : gcc_assert (TREE_STATIC (sym->backend_decl));
1048 : : /* Just zero the data member. */
1049 : 179 : type = TREE_TYPE (sym->backend_decl);
1050 : 179 : DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
1051 : 179 : }
1052 : :
1053 : :
1054 : : /* If the bounds of SE's loop have not yet been set, see if they can be
1055 : : determined from array spec AS, which is the array spec of a called
1056 : : function. MAPPING maps the callee's dummy arguments to the values
1057 : : that the caller is passing. Add any initialization and finalization
1058 : : code to SE. */
1059 : :
1060 : : void
1061 : 7641 : gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
1062 : : gfc_se * se, gfc_array_spec * as)
1063 : : {
1064 : 7641 : int n, dim, total_dim;
1065 : 7641 : gfc_se tmpse;
1066 : 7641 : gfc_ss *ss;
1067 : 7641 : tree lower;
1068 : 7641 : tree upper;
1069 : 7641 : tree tmp;
1070 : :
1071 : 7641 : total_dim = 0;
1072 : :
1073 : 7641 : if (!as || as->type != AS_EXPLICIT)
1074 : 6567 : return;
1075 : :
1076 : 2173 : for (ss = se->ss; ss; ss = ss->parent)
1077 : : {
1078 : 1099 : total_dim += ss->loop->dimen;
1079 : 2553 : for (n = 0; n < ss->loop->dimen; n++)
1080 : : {
1081 : : /* The bound is known, nothing to do. */
1082 : 1454 : if (ss->loop->to[n] != NULL_TREE)
1083 : 491 : continue;
1084 : :
1085 : 963 : dim = ss->dim[n];
1086 : 963 : gcc_assert (dim < as->rank);
1087 : 963 : gcc_assert (ss->loop->dimen <= as->rank);
1088 : :
1089 : : /* Evaluate the lower bound. */
1090 : 963 : gfc_init_se (&tmpse, NULL);
1091 : 963 : gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
1092 : 963 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
1093 : 963 : gfc_add_block_to_block (&se->post, &tmpse.post);
1094 : 963 : lower = fold_convert (gfc_array_index_type, tmpse.expr);
1095 : :
1096 : : /* ...and the upper bound. */
1097 : 963 : gfc_init_se (&tmpse, NULL);
1098 : 963 : gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
1099 : 963 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
1100 : 963 : gfc_add_block_to_block (&se->post, &tmpse.post);
1101 : 963 : upper = fold_convert (gfc_array_index_type, tmpse.expr);
1102 : :
1103 : : /* Set the upper bound of the loop to UPPER - LOWER. */
1104 : 963 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
1105 : : gfc_array_index_type, upper, lower);
1106 : 963 : tmp = gfc_evaluate_now (tmp, &se->pre);
1107 : 963 : ss->loop->to[n] = tmp;
1108 : : }
1109 : : }
1110 : :
1111 : 1074 : gcc_assert (total_dim == as->rank);
1112 : : }
1113 : :
1114 : :
1115 : : /* Generate code to allocate an array temporary, or create a variable to
1116 : : hold the data. If size is NULL, zero the descriptor so that the
1117 : : callee will allocate the array. If DEALLOC is true, also generate code to
1118 : : free the array afterwards.
1119 : :
1120 : : If INITIAL is not NULL, it is packed using internal_pack and the result used
1121 : : as data instead of allocating a fresh, unitialized area of memory.
1122 : :
1123 : : Initialization code is added to PRE and finalization code to POST.
1124 : : DYNAMIC is true if the caller may want to extend the array later
1125 : : using realloc. This prevents us from putting the array on the stack. */
1126 : :
1127 : : static void
1128 : 24613 : gfc_trans_allocate_array_storage (stmtblock_t * pre, stmtblock_t * post,
1129 : : gfc_array_info * info, tree size, tree nelem,
1130 : : tree initial, bool dynamic, bool dealloc)
1131 : : {
1132 : 24613 : tree tmp;
1133 : 24613 : tree desc;
1134 : 24613 : bool onstack;
1135 : :
1136 : 24613 : desc = info->descriptor;
1137 : 24613 : info->offset = gfc_index_zero_node;
1138 : 24613 : if (size == NULL_TREE || (dynamic && integer_zerop (size)))
1139 : : {
1140 : : /* A callee allocated array. */
1141 : 2444 : gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
1142 : 2444 : onstack = false;
1143 : : }
1144 : : else
1145 : : {
1146 : : /* Allocate the temporary. */
1147 : 44338 : onstack = !dynamic && initial == NULL_TREE
1148 : 22169 : && (flag_stack_arrays
1149 : 21842 : || gfc_can_put_var_on_stack (size));
1150 : :
1151 : 22169 : if (onstack)
1152 : : {
1153 : : /* Make a temporary variable to hold the data. */
1154 : 17837 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (nelem),
1155 : : nelem, gfc_index_one_node);
1156 : 17837 : tmp = gfc_evaluate_now (tmp, pre);
1157 : 17837 : tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1158 : : tmp);
1159 : 17837 : tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
1160 : : tmp);
1161 : 17837 : tmp = gfc_create_var (tmp, "A");
1162 : : /* If we're here only because of -fstack-arrays we have to
1163 : : emit a DECL_EXPR to make the gimplifier emit alloca calls. */
1164 : 17837 : if (!gfc_can_put_var_on_stack (size))
1165 : 17 : gfc_add_expr_to_block (pre,
1166 : : fold_build1_loc (input_location,
1167 : 17 : DECL_EXPR, TREE_TYPE (tmp),
1168 : : tmp));
1169 : 17837 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1170 : 17837 : gfc_conv_descriptor_data_set (pre, desc, tmp);
1171 : : }
1172 : : else
1173 : : {
1174 : : /* Allocate memory to hold the data or call internal_pack. */
1175 : 4332 : if (initial == NULL_TREE)
1176 : : {
1177 : 4231 : tmp = gfc_call_malloc (pre, NULL, size);
1178 : 4231 : tmp = gfc_evaluate_now (tmp, pre);
1179 : : }
1180 : : else
1181 : : {
1182 : 101 : tree packed;
1183 : 101 : tree source_data;
1184 : 101 : tree was_packed;
1185 : 101 : stmtblock_t do_copying;
1186 : :
1187 : 101 : tmp = TREE_TYPE (initial); /* Pointer to descriptor. */
1188 : 101 : gcc_assert (TREE_CODE (tmp) == POINTER_TYPE);
1189 : 101 : tmp = TREE_TYPE (tmp); /* The descriptor itself. */
1190 : 101 : tmp = gfc_get_element_type (tmp);
1191 : 101 : packed = gfc_create_var (build_pointer_type (tmp), "data");
1192 : :
1193 : 101 : tmp = build_call_expr_loc (input_location,
1194 : : gfor_fndecl_in_pack, 1, initial);
1195 : 101 : tmp = fold_convert (TREE_TYPE (packed), tmp);
1196 : 101 : gfc_add_modify (pre, packed, tmp);
1197 : :
1198 : 101 : tmp = build_fold_indirect_ref_loc (input_location,
1199 : : initial);
1200 : 101 : source_data = gfc_conv_descriptor_data_get (tmp);
1201 : :
1202 : : /* internal_pack may return source->data without any allocation
1203 : : or copying if it is already packed. If that's the case, we
1204 : : need to allocate and copy manually. */
1205 : :
1206 : 101 : gfc_start_block (&do_copying);
1207 : 101 : tmp = gfc_call_malloc (&do_copying, NULL, size);
1208 : 101 : tmp = fold_convert (TREE_TYPE (packed), tmp);
1209 : 101 : gfc_add_modify (&do_copying, packed, tmp);
1210 : 101 : tmp = gfc_build_memcpy_call (packed, source_data, size);
1211 : 101 : gfc_add_expr_to_block (&do_copying, tmp);
1212 : :
1213 : 101 : was_packed = fold_build2_loc (input_location, EQ_EXPR,
1214 : : logical_type_node, packed,
1215 : : source_data);
1216 : 101 : tmp = gfc_finish_block (&do_copying);
1217 : 101 : tmp = build3_v (COND_EXPR, was_packed, tmp,
1218 : : build_empty_stmt (input_location));
1219 : 101 : gfc_add_expr_to_block (pre, tmp);
1220 : :
1221 : 101 : tmp = fold_convert (pvoid_type_node, packed);
1222 : : }
1223 : :
1224 : 4332 : gfc_conv_descriptor_data_set (pre, desc, tmp);
1225 : : }
1226 : : }
1227 : 24613 : info->data = gfc_conv_descriptor_data_get (desc);
1228 : :
1229 : : /* The offset is zero because we create temporaries with a zero
1230 : : lower bound. */
1231 : 24613 : gfc_conv_descriptor_offset_set (pre, desc, gfc_index_zero_node);
1232 : :
1233 : 24613 : if (dealloc && !onstack)
1234 : : {
1235 : : /* Free the temporary. */
1236 : 6568 : tmp = gfc_conv_descriptor_data_get (desc);
1237 : 6568 : tmp = gfc_call_free (tmp);
1238 : 6568 : gfc_add_expr_to_block (post, tmp);
1239 : : }
1240 : 24613 : }
1241 : :
1242 : :
1243 : : /* Get the scalarizer array dimension corresponding to actual array dimension
1244 : : given by ARRAY_DIM.
1245 : :
1246 : : For example, if SS represents the array ref a(1,:,:,1), it is a
1247 : : bidimensional scalarizer array, and the result would be 0 for ARRAY_DIM=1,
1248 : : and 1 for ARRAY_DIM=2.
1249 : : If SS represents transpose(a(:,1,1,:)), it is again a bidimensional
1250 : : scalarizer array, and the result would be 1 for ARRAY_DIM=0 and 0 for
1251 : : ARRAY_DIM=3.
1252 : : If SS represents sum(a(:,:,:,1), dim=1), it is a 2+1-dimensional scalarizer
1253 : : array. If called on the inner ss, the result would be respectively 0,1,2 for
1254 : : ARRAY_DIM=0,1,2. If called on the outer ss, the result would be 0,1
1255 : : for ARRAY_DIM=1,2. */
1256 : :
1257 : : static int
1258 : 211747 : get_scalarizer_dim_for_array_dim (gfc_ss *ss, int array_dim)
1259 : : {
1260 : 211747 : int array_ref_dim;
1261 : 211747 : int n;
1262 : :
1263 : 211747 : array_ref_dim = 0;
1264 : :
1265 : 424490 : for (; ss; ss = ss->parent)
1266 : 538127 : for (n = 0; n < ss->dimen; n++)
1267 : 325384 : if (ss->dim[n] < array_dim)
1268 : 55176 : array_ref_dim++;
1269 : :
1270 : 211747 : return array_ref_dim;
1271 : : }
1272 : :
1273 : :
1274 : : static gfc_ss *
1275 : 177341 : innermost_ss (gfc_ss *ss)
1276 : : {
1277 : 323503 : while (ss->nested_ss != NULL)
1278 : : ss = ss->nested_ss;
1279 : :
1280 : 322496 : return ss;
1281 : : }
1282 : :
1283 : :
1284 : :
1285 : : /* Get the array reference dimension corresponding to the given loop dimension.
1286 : : It is different from the true array dimension given by the dim array in
1287 : : the case of a partial array reference (i.e. a(:,:,1,:) for example)
1288 : : It is different from the loop dimension in the case of a transposed array.
1289 : : */
1290 : :
1291 : : static int
1292 : 177341 : get_array_ref_dim_for_loop_dim (gfc_ss *ss, int loop_dim)
1293 : : {
1294 : 177341 : return get_scalarizer_dim_for_array_dim (innermost_ss (ss),
1295 : 177341 : ss->dim[loop_dim]);
1296 : : }
1297 : :
1298 : :
1299 : : /* Use the information in the ss to obtain the required information about
1300 : : the type and size of an array temporary, when the lhs in an assignment
1301 : : is a class expression. */
1302 : :
1303 : : static tree
1304 : 188 : get_class_info_from_ss (stmtblock_t * pre, gfc_ss *ss, tree *eltype)
1305 : : {
1306 : 188 : gfc_ss *lhs_ss;
1307 : 188 : gfc_ss *rhs_ss;
1308 : 188 : tree tmp;
1309 : 188 : tree tmp2;
1310 : 188 : tree vptr;
1311 : 188 : tree rhs_class_expr = NULL_TREE;
1312 : 188 : tree lhs_class_expr = NULL_TREE;
1313 : 188 : bool unlimited_rhs = false;
1314 : 188 : bool unlimited_lhs = false;
1315 : 188 : bool rhs_function = false;
1316 : 188 : gfc_symbol *vtab;
1317 : :
1318 : : /* The second element in the loop chain contains the source for the
1319 : : temporary; ie. the rhs of the assignment. */
1320 : 188 : rhs_ss = ss->loop->ss->loop_chain;
1321 : :
1322 : 188 : if (rhs_ss != gfc_ss_terminator
1323 : 188 : && rhs_ss->info
1324 : 188 : && rhs_ss->info->expr
1325 : 188 : && rhs_ss->info->expr->ts.type == BT_CLASS
1326 : 104 : && rhs_ss->info->data.array.descriptor)
1327 : : {
1328 : 98 : if (rhs_ss->info->expr->expr_type != EXPR_VARIABLE)
1329 : 50 : rhs_class_expr
1330 : 50 : = gfc_get_class_from_expr (rhs_ss->info->data.array.descriptor);
1331 : : else
1332 : 48 : rhs_class_expr = gfc_get_class_from_gfc_expr (rhs_ss->info->expr);
1333 : 98 : unlimited_rhs = UNLIMITED_POLY (rhs_ss->info->expr);
1334 : 98 : if (rhs_ss->info->expr->expr_type == EXPR_FUNCTION)
1335 : 31 : rhs_function = true;
1336 : : }
1337 : :
1338 : : /* For an assignment the lhs is the next element in the loop chain.
1339 : : If we have a class rhs, this had better be a class variable
1340 : : expression! */
1341 : 188 : lhs_ss = rhs_ss->loop_chain;
1342 : 188 : if (lhs_ss != gfc_ss_terminator
1343 : 176 : && lhs_ss->info
1344 : 176 : && lhs_ss->info->expr
1345 : 176 : && lhs_ss->info->expr->expr_type ==EXPR_VARIABLE
1346 : 176 : && lhs_ss->info->expr->ts.type == BT_CLASS)
1347 : : {
1348 : 176 : tmp = lhs_ss->info->data.array.descriptor;
1349 : 176 : unlimited_lhs = UNLIMITED_POLY (rhs_ss->info->expr);
1350 : : }
1351 : : else
1352 : : tmp = NULL_TREE;
1353 : :
1354 : : /* Get the lhs class expression. */
1355 : 176 : if (tmp != NULL_TREE && lhs_ss->loop_chain == gfc_ss_terminator)
1356 : 164 : lhs_class_expr = gfc_get_class_from_expr (tmp);
1357 : : else
1358 : : return rhs_class_expr;
1359 : :
1360 : 164 : gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (lhs_class_expr)));
1361 : :
1362 : : /* Set the lhs vptr and, if necessary, the _len field. */
1363 : 164 : if (rhs_class_expr)
1364 : : {
1365 : : /* Both lhs and rhs are class expressions. */
1366 : 73 : tmp = gfc_class_vptr_get (lhs_class_expr);
1367 : 146 : gfc_add_modify (pre, tmp,
1368 : 73 : fold_convert (TREE_TYPE (tmp),
1369 : : gfc_class_vptr_get (rhs_class_expr)));
1370 : 73 : if (unlimited_lhs)
1371 : : {
1372 : 31 : tmp = gfc_class_len_get (lhs_class_expr);
1373 : 31 : if (unlimited_rhs)
1374 : 31 : tmp2 = gfc_class_len_get (rhs_class_expr);
1375 : : else
1376 : 0 : tmp2 = build_int_cst (TREE_TYPE (tmp), 0);
1377 : 31 : gfc_add_modify (pre, tmp, tmp2);
1378 : : }
1379 : :
1380 : 73 : if (rhs_function)
1381 : : {
1382 : 31 : tmp = gfc_class_data_get (rhs_class_expr);
1383 : 31 : gfc_conv_descriptor_offset_set (pre, tmp, gfc_index_zero_node);
1384 : : }
1385 : : }
1386 : : else
1387 : : {
1388 : : /* lhs is class and rhs is intrinsic or derived type. */
1389 : 91 : *eltype = TREE_TYPE (rhs_ss->info->data.array.descriptor);
1390 : 91 : *eltype = gfc_get_element_type (*eltype);
1391 : 91 : vtab = gfc_find_vtab (&rhs_ss->info->expr->ts);
1392 : 91 : vptr = vtab->backend_decl;
1393 : 91 : if (vptr == NULL_TREE)
1394 : 12 : vptr = gfc_get_symbol_decl (vtab);
1395 : 91 : vptr = gfc_build_addr_expr (NULL_TREE, vptr);
1396 : 91 : tmp = gfc_class_vptr_get (lhs_class_expr);
1397 : 91 : gfc_add_modify (pre, tmp,
1398 : 91 : fold_convert (TREE_TYPE (tmp), vptr));
1399 : :
1400 : 91 : if (unlimited_lhs)
1401 : : {
1402 : 0 : tmp = gfc_class_len_get (lhs_class_expr);
1403 : 0 : if (rhs_ss->info
1404 : 0 : && rhs_ss->info->expr
1405 : 0 : && rhs_ss->info->expr->ts.type == BT_CHARACTER)
1406 : 0 : tmp2 = build_int_cst (TREE_TYPE (tmp),
1407 : 0 : rhs_ss->info->expr->ts.kind);
1408 : : else
1409 : 0 : tmp2 = build_int_cst (TREE_TYPE (tmp), 0);
1410 : 0 : gfc_add_modify (pre, tmp, tmp2);
1411 : : }
1412 : : }
1413 : :
1414 : : return rhs_class_expr;
1415 : : }
1416 : :
1417 : :
1418 : :
1419 : : /* Generate code to create and initialize the descriptor for a temporary
1420 : : array. This is used for both temporaries needed by the scalarizer, and
1421 : : functions returning arrays. Adjusts the loop variables to be
1422 : : zero-based, and calculates the loop bounds for callee allocated arrays.
1423 : : Allocate the array unless it's callee allocated (we have a callee
1424 : : allocated array if 'callee_alloc' is true, or if loop->to[n] is
1425 : : NULL_TREE for any n). Also fills in the descriptor, data and offset
1426 : : fields of info if known. Returns the size of the array, or NULL for a
1427 : : callee allocated array.
1428 : :
1429 : : 'eltype' == NULL signals that the temporary should be a class object.
1430 : : The 'initial' expression is used to obtain the size of the dynamic
1431 : : type; otherwise the allocation and initialization proceeds as for any
1432 : : other expression
1433 : :
1434 : : PRE, POST, INITIAL, DYNAMIC and DEALLOC are as for
1435 : : gfc_trans_allocate_array_storage. */
1436 : :
1437 : : tree
1438 : 24613 : gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post, gfc_ss * ss,
1439 : : tree eltype, tree initial, bool dynamic,
1440 : : bool dealloc, bool callee_alloc, locus * where)
1441 : : {
1442 : 24613 : gfc_loopinfo *loop;
1443 : 24613 : gfc_ss *s;
1444 : 24613 : gfc_array_info *info;
1445 : 24613 : tree from[GFC_MAX_DIMENSIONS], to[GFC_MAX_DIMENSIONS];
1446 : 24613 : tree type;
1447 : 24613 : tree desc;
1448 : 24613 : tree tmp;
1449 : 24613 : tree size;
1450 : 24613 : tree nelem;
1451 : 24613 : tree cond;
1452 : 24613 : tree or_expr;
1453 : 24613 : tree elemsize;
1454 : 24613 : tree class_expr = NULL_TREE;
1455 : 24613 : int n, dim, tmp_dim;
1456 : 24613 : int total_dim = 0;
1457 : :
1458 : : /* This signals a class array for which we need the size of the
1459 : : dynamic type. Generate an eltype and then the class expression. */
1460 : 24613 : if (eltype == NULL_TREE && initial)
1461 : : {
1462 : 6 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (initial)));
1463 : 6 : class_expr = build_fold_indirect_ref_loc (input_location, initial);
1464 : : /* Obtain the structure (class) expression. */
1465 : 6 : class_expr = gfc_get_class_from_expr (class_expr);
1466 : 6 : gcc_assert (class_expr);
1467 : : }
1468 : :
1469 : : /* Otherwise, some expressions, such as class functions, arising from
1470 : : dependency checking in assignments come here with class element type.
1471 : : The descriptor can be obtained from the ss->info and then converted
1472 : : to the class object. */
1473 : 24607 : if (class_expr == NULL_TREE && GFC_CLASS_TYPE_P (eltype))
1474 : 188 : class_expr = get_class_info_from_ss (pre, ss, &eltype);
1475 : :
1476 : : /* If the dynamic type is not available, use the declared type. */
1477 : 24613 : if (eltype && GFC_CLASS_TYPE_P (eltype))
1478 : 97 : eltype = gfc_get_element_type (TREE_TYPE (TYPE_FIELDS (eltype)));
1479 : :
1480 : 24613 : if (class_expr == NULL_TREE)
1481 : 24528 : elemsize = fold_convert (gfc_array_index_type,
1482 : : TYPE_SIZE_UNIT (eltype));
1483 : : else
1484 : : {
1485 : : /* Unlimited polymorphic entities are initialised with NULL vptr. They
1486 : : can be tested for by checking if the len field is present. If so
1487 : : test the vptr before using the vtable size. */
1488 : 85 : tmp = gfc_class_vptr_get (class_expr);
1489 : 85 : tmp = fold_build2_loc (input_location, NE_EXPR,
1490 : : logical_type_node,
1491 : 85 : tmp, build_int_cst (TREE_TYPE (tmp), 0));
1492 : 85 : elemsize = fold_build3_loc (input_location, COND_EXPR,
1493 : : gfc_array_index_type,
1494 : : tmp,
1495 : : gfc_class_vtab_size_get (class_expr),
1496 : : gfc_index_zero_node);
1497 : 85 : elemsize = gfc_evaluate_now (elemsize, pre);
1498 : 85 : elemsize = gfc_resize_class_size_with_len (pre, class_expr, elemsize);
1499 : : /* Casting the data as a character of the dynamic length ensures that
1500 : : assignment of elements works when needed. */
1501 : 85 : eltype = gfc_get_character_type_len (1, elemsize);
1502 : : }
1503 : :
1504 : 24613 : memset (from, 0, sizeof (from));
1505 : 24613 : memset (to, 0, sizeof (to));
1506 : :
1507 : 24613 : info = &ss->info->data.array;
1508 : :
1509 : 24613 : gcc_assert (ss->dimen > 0);
1510 : 24613 : gcc_assert (ss->loop->dimen == ss->dimen);
1511 : :
1512 : 24613 : if (warn_array_temporaries && where)
1513 : 204 : gfc_warning (OPT_Warray_temporaries,
1514 : : "Creating array temporary at %L", where);
1515 : :
1516 : : /* Set the lower bound to zero. */
1517 : 49261 : for (s = ss; s; s = s->parent)
1518 : : {
1519 : 24648 : loop = s->loop;
1520 : :
1521 : 24648 : total_dim += loop->dimen;
1522 : 56307 : for (n = 0; n < loop->dimen; n++)
1523 : : {
1524 : 31659 : dim = s->dim[n];
1525 : :
1526 : : /* Callee allocated arrays may not have a known bound yet. */
1527 : 31659 : if (loop->to[n])
1528 : 28917 : loop->to[n] = gfc_evaluate_now (
1529 : : fold_build2_loc (input_location, MINUS_EXPR,
1530 : : gfc_array_index_type,
1531 : : loop->to[n], loop->from[n]),
1532 : : pre);
1533 : 31659 : loop->from[n] = gfc_index_zero_node;
1534 : :
1535 : : /* We have just changed the loop bounds, we must clear the
1536 : : corresponding specloop, so that delta calculation is not skipped
1537 : : later in gfc_set_delta. */
1538 : 31659 : loop->specloop[n] = NULL;
1539 : :
1540 : : /* We are constructing the temporary's descriptor based on the loop
1541 : : dimensions. As the dimensions may be accessed in arbitrary order
1542 : : (think of transpose) the size taken from the n'th loop may not map
1543 : : to the n'th dimension of the array. We need to reconstruct loop
1544 : : infos in the right order before using it to set the descriptor
1545 : : bounds. */
1546 : 31659 : tmp_dim = get_scalarizer_dim_for_array_dim (ss, dim);
1547 : 31659 : from[tmp_dim] = loop->from[n];
1548 : 31659 : to[tmp_dim] = loop->to[n];
1549 : :
1550 : 31659 : info->delta[dim] = gfc_index_zero_node;
1551 : 31659 : info->start[dim] = gfc_index_zero_node;
1552 : 31659 : info->end[dim] = gfc_index_zero_node;
1553 : 31659 : info->stride[dim] = gfc_index_one_node;
1554 : : }
1555 : : }
1556 : :
1557 : : /* Initialize the descriptor. */
1558 : 24613 : type =
1559 : 24613 : gfc_get_array_type_bounds (eltype, total_dim, 0, from, to, 1,
1560 : : GFC_ARRAY_UNKNOWN, true);
1561 : 24613 : desc = gfc_create_var (type, "atmp");
1562 : 24613 : GFC_DECL_PACKED_ARRAY (desc) = 1;
1563 : :
1564 : : /* Emit a DECL_EXPR for the variable sized array type in
1565 : : GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
1566 : : sizes works correctly. */
1567 : 24613 : tree arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (type));
1568 : 24613 : if (! TYPE_NAME (arraytype))
1569 : 24613 : TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
1570 : : NULL_TREE, arraytype);
1571 : 24613 : gfc_add_expr_to_block (pre, build1 (DECL_EXPR,
1572 : 24613 : arraytype, TYPE_NAME (arraytype)));
1573 : :
1574 : 24613 : if (class_expr != NULL_TREE)
1575 : : {
1576 : 85 : tree class_data;
1577 : 85 : tree dtype;
1578 : :
1579 : : /* Create a class temporary. */
1580 : 85 : tmp = gfc_create_var (TREE_TYPE (class_expr), "ctmp");
1581 : 85 : gfc_add_modify (pre, tmp, class_expr);
1582 : :
1583 : : /* Assign the new descriptor to the _data field. This allows the
1584 : : vptr _copy to be used for scalarized assignment since the class
1585 : : temporary can be found from the descriptor. */
1586 : 85 : class_data = gfc_class_data_get (tmp);
1587 : 85 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1588 : 85 : TREE_TYPE (desc), desc);
1589 : 85 : gfc_add_modify (pre, class_data, tmp);
1590 : :
1591 : : /* Take the dtype from the class expression. */
1592 : 85 : dtype = gfc_conv_descriptor_dtype (gfc_class_data_get (class_expr));
1593 : 85 : tmp = gfc_conv_descriptor_dtype (class_data);
1594 : 85 : gfc_add_modify (pre, tmp, dtype);
1595 : :
1596 : : /* Point desc to the class _data field. */
1597 : 85 : desc = class_data;
1598 : : }
1599 : : else
1600 : : {
1601 : : /* Fill in the array dtype. */
1602 : 24528 : tmp = gfc_conv_descriptor_dtype (desc);
1603 : 24528 : gfc_add_modify (pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
1604 : : }
1605 : :
1606 : 24613 : info->descriptor = desc;
1607 : 24613 : size = gfc_index_one_node;
1608 : :
1609 : : /*
1610 : : Fill in the bounds and stride. This is a packed array, so:
1611 : :
1612 : : size = 1;
1613 : : for (n = 0; n < rank; n++)
1614 : : {
1615 : : stride[n] = size
1616 : : delta = ubound[n] + 1 - lbound[n];
1617 : : size = size * delta;
1618 : : }
1619 : : size = size * sizeof(element);
1620 : : */
1621 : :
1622 : 24613 : or_expr = NULL_TREE;
1623 : :
1624 : : /* If there is at least one null loop->to[n], it is a callee allocated
1625 : : array. */
1626 : 53530 : for (n = 0; n < total_dim; n++)
1627 : 30591 : if (to[n] == NULL_TREE)
1628 : : {
1629 : : size = NULL_TREE;
1630 : : break;
1631 : : }
1632 : :
1633 : 24613 : if (size == NULL_TREE)
1634 : 3358 : for (s = ss; s; s = s->parent)
1635 : 4431 : for (n = 0; n < s->loop->dimen; n++)
1636 : : {
1637 : 2747 : dim = get_scalarizer_dim_for_array_dim (ss, s->dim[n]);
1638 : :
1639 : : /* For a callee allocated array express the loop bounds in terms
1640 : : of the descriptor fields. */
1641 : 2747 : tmp = fold_build2_loc (input_location,
1642 : : MINUS_EXPR, gfc_array_index_type,
1643 : : gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]),
1644 : : gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]));
1645 : 2747 : s->loop->to[n] = tmp;
1646 : : }
1647 : : else
1648 : : {
1649 : 51851 : for (n = 0; n < total_dim; n++)
1650 : : {
1651 : : /* Store the stride and bound components in the descriptor. */
1652 : 28912 : gfc_conv_descriptor_stride_set (pre, desc, gfc_rank_cst[n], size);
1653 : :
1654 : 28912 : gfc_conv_descriptor_lbound_set (pre, desc, gfc_rank_cst[n],
1655 : : gfc_index_zero_node);
1656 : :
1657 : 28912 : gfc_conv_descriptor_ubound_set (pre, desc, gfc_rank_cst[n], to[n]);
1658 : :
1659 : 28912 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
1660 : : gfc_array_index_type,
1661 : : to[n], gfc_index_one_node);
1662 : :
1663 : : /* Check whether the size for this dimension is negative. */
1664 : 28912 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
1665 : : tmp, gfc_index_zero_node);
1666 : 28912 : cond = gfc_evaluate_now (cond, pre);
1667 : :
1668 : 28912 : if (n == 0)
1669 : : or_expr = cond;
1670 : : else
1671 : 5973 : or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
1672 : : logical_type_node, or_expr, cond);
1673 : :
1674 : 28912 : size = fold_build2_loc (input_location, MULT_EXPR,
1675 : : gfc_array_index_type, size, tmp);
1676 : 28912 : size = gfc_evaluate_now (size, pre);
1677 : : }
1678 : : }
1679 : :
1680 : : /* Get the size of the array. */
1681 : 24613 : if (size && !callee_alloc)
1682 : : {
1683 : : /* If or_expr is true, then the extent in at least one
1684 : : dimension is zero and the size is set to zero. */
1685 : 22746 : size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
1686 : : or_expr, gfc_index_zero_node, size);
1687 : :
1688 : 22746 : nelem = size;
1689 : 22746 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1690 : : size, elemsize);
1691 : : }
1692 : : else
1693 : : {
1694 : : nelem = size;
1695 : : size = NULL_TREE;
1696 : : }
1697 : :
1698 : : /* Set the span. */
1699 : 24613 : tmp = fold_convert (gfc_array_index_type, elemsize);
1700 : 24613 : gfc_conv_descriptor_span_set (pre, desc, tmp);
1701 : :
1702 : 24613 : gfc_trans_allocate_array_storage (pre, post, info, size, nelem, initial,
1703 : : dynamic, dealloc);
1704 : :
1705 : 49261 : while (ss->parent)
1706 : : ss = ss->parent;
1707 : :
1708 : 24613 : if (ss->dimen > ss->loop->temp_dim)
1709 : 21457 : ss->loop->temp_dim = ss->dimen;
1710 : :
1711 : 24613 : return size;
1712 : : }
1713 : :
1714 : :
1715 : : /* Return the number of iterations in a loop that starts at START,
1716 : : ends at END, and has step STEP. */
1717 : :
1718 : : static tree
1719 : 994 : gfc_get_iteration_count (tree start, tree end, tree step)
1720 : : {
1721 : 994 : tree tmp;
1722 : 994 : tree type;
1723 : :
1724 : 994 : type = TREE_TYPE (step);
1725 : 994 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type, end, start);
1726 : 994 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, tmp, step);
1727 : 994 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp,
1728 : 994 : build_int_cst (type, 1));
1729 : 994 : tmp = fold_build2_loc (input_location, MAX_EXPR, type, tmp,
1730 : 994 : build_int_cst (type, 0));
1731 : 994 : return fold_convert (gfc_array_index_type, tmp);
1732 : : }
1733 : :
1734 : :
1735 : : /* Extend the data in array DESC by EXTRA elements. */
1736 : :
1737 : : static void
1738 : 982 : gfc_grow_array (stmtblock_t * pblock, tree desc, tree extra)
1739 : : {
1740 : 982 : tree arg0, arg1;
1741 : 982 : tree tmp;
1742 : 982 : tree size;
1743 : 982 : tree ubound;
1744 : :
1745 : 982 : if (integer_zerop (extra))
1746 : : return;
1747 : :
1748 : 952 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[0]);
1749 : :
1750 : : /* Add EXTRA to the upper bound. */
1751 : 952 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1752 : : ubound, extra);
1753 : 952 : gfc_conv_descriptor_ubound_set (pblock, desc, gfc_rank_cst[0], tmp);
1754 : :
1755 : : /* Get the value of the current data pointer. */
1756 : 952 : arg0 = gfc_conv_descriptor_data_get (desc);
1757 : :
1758 : : /* Calculate the new array size. */
1759 : 952 : size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
1760 : 952 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1761 : : ubound, gfc_index_one_node);
1762 : 952 : arg1 = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
1763 : : fold_convert (size_type_node, tmp),
1764 : : fold_convert (size_type_node, size));
1765 : :
1766 : : /* Call the realloc() function. */
1767 : 952 : tmp = gfc_call_realloc (pblock, arg0, arg1);
1768 : 952 : gfc_conv_descriptor_data_set (pblock, desc, tmp);
1769 : : }
1770 : :
1771 : :
1772 : : /* Return true if the bounds of iterator I can only be determined
1773 : : at run time. */
1774 : :
1775 : : static inline bool
1776 : 2071 : gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
1777 : : {
1778 : 2071 : return (i->start->expr_type != EXPR_CONSTANT
1779 : 1638 : || i->end->expr_type != EXPR_CONSTANT
1780 : 2229 : || i->step->expr_type != EXPR_CONSTANT);
1781 : : }
1782 : :
1783 : :
1784 : : /* Split the size of constructor element EXPR into the sum of two terms,
1785 : : one of which can be determined at compile time and one of which must
1786 : : be calculated at run time. Set *SIZE to the former and return true
1787 : : if the latter might be nonzero. */
1788 : :
1789 : : static bool
1790 : 3066 : gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
1791 : : {
1792 : 3066 : if (expr->expr_type == EXPR_ARRAY)
1793 : 615 : return gfc_get_array_constructor_size (size, expr->value.constructor);
1794 : 2451 : else if (expr->rank > 0)
1795 : : {
1796 : : /* Calculate everything at run time. */
1797 : 977 : mpz_set_ui (*size, 0);
1798 : 977 : return true;
1799 : : }
1800 : : else
1801 : : {
1802 : : /* A single element. */
1803 : 1474 : mpz_set_ui (*size, 1);
1804 : 1474 : return false;
1805 : : }
1806 : : }
1807 : :
1808 : :
1809 : : /* Like gfc_get_array_constructor_element_size, but applied to the whole
1810 : : of array constructor C. */
1811 : :
1812 : : static bool
1813 : 2667 : gfc_get_array_constructor_size (mpz_t * size, gfc_constructor_base base)
1814 : : {
1815 : 2667 : gfc_constructor *c;
1816 : 2667 : gfc_iterator *i;
1817 : 2667 : mpz_t val;
1818 : 2667 : mpz_t len;
1819 : 2667 : bool dynamic;
1820 : :
1821 : 2667 : mpz_set_ui (*size, 0);
1822 : 2667 : mpz_init (len);
1823 : 2667 : mpz_init (val);
1824 : :
1825 : 2667 : dynamic = false;
1826 : 6660 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1827 : : {
1828 : 3993 : i = c->iterator;
1829 : 3993 : if (i && gfc_iterator_has_dynamic_bounds (i))
1830 : : dynamic = true;
1831 : : else
1832 : : {
1833 : 2573 : dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
1834 : 2573 : if (i)
1835 : : {
1836 : : /* Multiply the static part of the element size by the
1837 : : number of iterations. */
1838 : 116 : mpz_sub (val, i->end->value.integer, i->start->value.integer);
1839 : 116 : mpz_fdiv_q (val, val, i->step->value.integer);
1840 : 116 : mpz_add_ui (val, val, 1);
1841 : 116 : if (mpz_sgn (val) > 0)
1842 : 80 : mpz_mul (len, len, val);
1843 : : else
1844 : 36 : mpz_set_ui (len, 0);
1845 : : }
1846 : 2573 : mpz_add (*size, *size, len);
1847 : : }
1848 : : }
1849 : 2667 : mpz_clear (len);
1850 : 2667 : mpz_clear (val);
1851 : 2667 : return dynamic;
1852 : : }
1853 : :
1854 : :
1855 : : /* Make sure offset is a variable. */
1856 : :
1857 : : static void
1858 : 3218 : gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
1859 : : tree * offsetvar)
1860 : : {
1861 : : /* We should have already created the offset variable. We cannot
1862 : : create it here because we may be in an inner scope. */
1863 : 3218 : gcc_assert (*offsetvar != NULL_TREE);
1864 : 3218 : gfc_add_modify (pblock, *offsetvar, *poffset);
1865 : 3218 : *poffset = *offsetvar;
1866 : 3218 : TREE_USED (*offsetvar) = 1;
1867 : 3218 : }
1868 : :
1869 : :
1870 : : /* Variables needed for bounds-checking. */
1871 : : static bool first_len;
1872 : : static tree first_len_val;
1873 : : static bool typespec_chararray_ctor;
1874 : :
1875 : : static void
1876 : 12823 : gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
1877 : : tree offset, gfc_se * se, gfc_expr * expr)
1878 : : {
1879 : 12823 : tree tmp;
1880 : :
1881 : 12823 : gfc_conv_expr (se, expr);
1882 : :
1883 : : /* Store the value. */
1884 : 12823 : tmp = build_fold_indirect_ref_loc (input_location,
1885 : : gfc_conv_descriptor_data_get (desc));
1886 : 12823 : tmp = gfc_build_array_ref (tmp, offset, NULL);
1887 : :
1888 : 12823 : if (expr->expr_type == EXPR_FUNCTION && expr->ts.type == BT_DERIVED
1889 : 26 : && expr->ts.u.derived->attr.alloc_comp)
1890 : : {
1891 : 2 : if (!VAR_P (se->expr))
1892 : 2 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
1893 : 2 : gfc_add_expr_to_block (&se->finalblock,
1894 : : gfc_deallocate_alloc_comp_no_caf (
1895 : : expr->ts.u.derived, se->expr, expr->rank, true));
1896 : : }
1897 : :
1898 : 12823 : if (expr->ts.type == BT_CHARACTER)
1899 : : {
1900 : 1981 : int i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
1901 : 1981 : tree esize;
1902 : :
1903 : 1981 : esize = size_in_bytes (gfc_get_element_type (TREE_TYPE (desc)));
1904 : 1981 : esize = fold_convert (gfc_charlen_type_node, esize);
1905 : 3962 : esize = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
1906 : 1981 : TREE_TYPE (esize), esize,
1907 : 1981 : build_int_cst (TREE_TYPE (esize),
1908 : 1981 : gfc_character_kinds[i].bit_size / 8));
1909 : :
1910 : 1981 : gfc_conv_string_parameter (se);
1911 : 1981 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
1912 : : {
1913 : : /* The temporary is an array of pointers. */
1914 : 6 : se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
1915 : 6 : gfc_add_modify (&se->pre, tmp, se->expr);
1916 : : }
1917 : : else
1918 : : {
1919 : : /* The temporary is an array of string values. */
1920 : 1975 : tmp = gfc_build_addr_expr (gfc_get_pchar_type (expr->ts.kind), tmp);
1921 : : /* We know the temporary and the value will be the same length,
1922 : : so can use memcpy. */
1923 : 1975 : gfc_trans_string_copy (&se->pre, esize, tmp, expr->ts.kind,
1924 : : se->string_length, se->expr, expr->ts.kind);
1925 : : }
1926 : 1981 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !typespec_chararray_ctor)
1927 : : {
1928 : 295 : if (first_len)
1929 : : {
1930 : 115 : gfc_add_modify (&se->pre, first_len_val,
1931 : 115 : fold_convert (TREE_TYPE (first_len_val),
1932 : : se->string_length));
1933 : 115 : first_len = false;
1934 : : }
1935 : : else
1936 : : {
1937 : : /* Verify that all constructor elements are of the same
1938 : : length. */
1939 : 180 : tree rhs = fold_convert (TREE_TYPE (first_len_val),
1940 : : se->string_length);
1941 : 180 : tree cond = fold_build2_loc (input_location, NE_EXPR,
1942 : : logical_type_node, first_len_val,
1943 : : rhs);
1944 : 180 : gfc_trans_runtime_check
1945 : 180 : (true, false, cond, &se->pre, &expr->where,
1946 : : "Different CHARACTER lengths (%ld/%ld) in array constructor",
1947 : : fold_convert (long_integer_type_node, first_len_val),
1948 : : fold_convert (long_integer_type_node, se->string_length));
1949 : : }
1950 : : }
1951 : : }
1952 : 10842 : else if (GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
1953 : 10842 : && !GFC_CLASS_TYPE_P (gfc_get_element_type (TREE_TYPE (desc))))
1954 : : {
1955 : : /* Assignment of a CLASS array constructor to a derived type array. */
1956 : 24 : if (expr->expr_type == EXPR_FUNCTION)
1957 : 18 : se->expr = gfc_evaluate_now (se->expr, pblock);
1958 : 24 : se->expr = gfc_class_data_get (se->expr);
1959 : 24 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
1960 : 24 : se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
1961 : 24 : gfc_add_modify (&se->pre, tmp, se->expr);
1962 : : }
1963 : : else
1964 : : {
1965 : : /* TODO: Should the frontend already have done this conversion? */
1966 : 10818 : se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
1967 : 10818 : gfc_add_modify (&se->pre, tmp, se->expr);
1968 : : }
1969 : :
1970 : 12823 : gfc_add_block_to_block (pblock, &se->pre);
1971 : 12823 : gfc_add_block_to_block (pblock, &se->post);
1972 : 12823 : }
1973 : :
1974 : :
1975 : : /* Add the contents of an array to the constructor. DYNAMIC is as for
1976 : : gfc_trans_array_constructor_value. */
1977 : :
1978 : : static void
1979 : 1190 : gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
1980 : : tree type ATTRIBUTE_UNUSED,
1981 : : tree desc, gfc_expr * expr,
1982 : : tree * poffset, tree * offsetvar,
1983 : : bool dynamic)
1984 : : {
1985 : 1190 : gfc_se se;
1986 : 1190 : gfc_ss *ss;
1987 : 1190 : gfc_loopinfo loop;
1988 : 1190 : stmtblock_t body;
1989 : 1190 : tree tmp;
1990 : 1190 : tree size;
1991 : 1190 : int n;
1992 : :
1993 : : /* We need this to be a variable so we can increment it. */
1994 : 1190 : gfc_put_offset_into_var (pblock, poffset, offsetvar);
1995 : :
1996 : 1190 : gfc_init_se (&se, NULL);
1997 : :
1998 : : /* Walk the array expression. */
1999 : 1190 : ss = gfc_walk_expr (expr);
2000 : 1190 : gcc_assert (ss != gfc_ss_terminator);
2001 : :
2002 : : /* Initialize the scalarizer. */
2003 : 1190 : gfc_init_loopinfo (&loop);
2004 : 1190 : gfc_add_ss_to_loop (&loop, ss);
2005 : :
2006 : : /* Initialize the loop. */
2007 : 1190 : gfc_conv_ss_startstride (&loop);
2008 : 1190 : gfc_conv_loop_setup (&loop, &expr->where);
2009 : :
2010 : : /* Make sure the constructed array has room for the new data. */
2011 : 1190 : if (dynamic)
2012 : : {
2013 : : /* Set SIZE to the total number of elements in the subarray. */
2014 : 489 : size = gfc_index_one_node;
2015 : 990 : for (n = 0; n < loop.dimen; n++)
2016 : : {
2017 : 501 : tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
2018 : : gfc_index_one_node);
2019 : 501 : size = fold_build2_loc (input_location, MULT_EXPR,
2020 : : gfc_array_index_type, size, tmp);
2021 : : }
2022 : :
2023 : : /* Grow the constructed array by SIZE elements. */
2024 : 489 : gfc_grow_array (&loop.pre, desc, size);
2025 : : }
2026 : :
2027 : : /* Make the loop body. */
2028 : 1190 : gfc_mark_ss_chain_used (ss, 1);
2029 : 1190 : gfc_start_scalarized_body (&loop, &body);
2030 : 1190 : gfc_copy_loopinfo_to_se (&se, &loop);
2031 : 1190 : se.ss = ss;
2032 : :
2033 : 1190 : gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
2034 : 1190 : gcc_assert (se.ss == gfc_ss_terminator);
2035 : :
2036 : : /* Increment the offset. */
2037 : 1190 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2038 : : *poffset, gfc_index_one_node);
2039 : 1190 : gfc_add_modify (&body, *poffset, tmp);
2040 : :
2041 : : /* Finish the loop. */
2042 : 1190 : gfc_trans_scalarizing_loops (&loop, &body);
2043 : 1190 : gfc_add_block_to_block (&loop.pre, &loop.post);
2044 : 1190 : tmp = gfc_finish_block (&loop.pre);
2045 : 1190 : gfc_add_expr_to_block (pblock, tmp);
2046 : :
2047 : 1190 : gfc_cleanup_loop (&loop);
2048 : 1190 : }
2049 : :
2050 : :
2051 : : /* Assign the values to the elements of an array constructor. DYNAMIC
2052 : : is true if descriptor DESC only contains enough data for the static
2053 : : size calculated by gfc_get_array_constructor_size. When true, memory
2054 : : for the dynamic parts must be allocated using realloc. */
2055 : :
2056 : : static void
2057 : 8863 : gfc_trans_array_constructor_value (stmtblock_t * pblock,
2058 : : stmtblock_t * finalblock,
2059 : : tree type, tree desc,
2060 : : gfc_constructor_base base, tree * poffset,
2061 : : tree * offsetvar, bool dynamic)
2062 : : {
2063 : 8863 : tree tmp;
2064 : 8863 : tree start = NULL_TREE;
2065 : 8863 : tree end = NULL_TREE;
2066 : 8863 : tree step = NULL_TREE;
2067 : 8863 : stmtblock_t body;
2068 : 8863 : gfc_se se;
2069 : 8863 : mpz_t size;
2070 : 8863 : gfc_constructor *c;
2071 : 8863 : gfc_typespec ts;
2072 : 8863 : int ctr = 0;
2073 : :
2074 : 8863 : tree shadow_loopvar = NULL_TREE;
2075 : 8863 : gfc_saved_var saved_loopvar;
2076 : :
2077 : 8863 : ts.type = BT_UNKNOWN;
2078 : 8863 : mpz_init (size);
2079 : 23061 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
2080 : : {
2081 : 14198 : ctr++;
2082 : : /* If this is an iterator or an array, the offset must be a variable. */
2083 : 14198 : if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
2084 : 2028 : gfc_put_offset_into_var (pblock, poffset, offsetvar);
2085 : :
2086 : : /* Shadowing the iterator avoids changing its value and saves us from
2087 : : keeping track of it. Further, it makes sure that there's always a
2088 : : backend-decl for the symbol, even if there wasn't one before,
2089 : : e.g. in the case of an iterator that appears in a specification
2090 : : expression in an interface mapping. */
2091 : 14198 : if (c->iterator)
2092 : : {
2093 : 1260 : gfc_symbol *sym;
2094 : 1260 : tree type;
2095 : :
2096 : : /* Evaluate loop bounds before substituting the loop variable
2097 : : in case they depend on it. Such a case is invalid, but it is
2098 : : not more expensive to do the right thing here.
2099 : : See PR 44354. */
2100 : 1260 : gfc_init_se (&se, NULL);
2101 : 1260 : gfc_conv_expr_val (&se, c->iterator->start);
2102 : 1260 : gfc_add_block_to_block (pblock, &se.pre);
2103 : 1260 : start = gfc_evaluate_now (se.expr, pblock);
2104 : :
2105 : 1260 : gfc_init_se (&se, NULL);
2106 : 1260 : gfc_conv_expr_val (&se, c->iterator->end);
2107 : 1260 : gfc_add_block_to_block (pblock, &se.pre);
2108 : 1260 : end = gfc_evaluate_now (se.expr, pblock);
2109 : :
2110 : 1260 : gfc_init_se (&se, NULL);
2111 : 1260 : gfc_conv_expr_val (&se, c->iterator->step);
2112 : 1260 : gfc_add_block_to_block (pblock, &se.pre);
2113 : 1260 : step = gfc_evaluate_now (se.expr, pblock);
2114 : :
2115 : 1260 : sym = c->iterator->var->symtree->n.sym;
2116 : 1260 : type = gfc_typenode_for_spec (&sym->ts);
2117 : :
2118 : 1260 : shadow_loopvar = gfc_create_var (type, "shadow_loopvar");
2119 : 1260 : gfc_shadow_sym (sym, shadow_loopvar, &saved_loopvar);
2120 : : }
2121 : :
2122 : 14198 : gfc_start_block (&body);
2123 : :
2124 : 14198 : if (c->expr->expr_type == EXPR_ARRAY)
2125 : : {
2126 : : /* Array constructors can be nested. */
2127 : 1270 : gfc_trans_array_constructor_value (&body, finalblock, type,
2128 : : desc, c->expr->value.constructor,
2129 : : poffset, offsetvar, dynamic);
2130 : : }
2131 : 12928 : else if (c->expr->rank > 0)
2132 : : {
2133 : 1190 : gfc_trans_array_constructor_subarray (&body, type, desc, c->expr,
2134 : : poffset, offsetvar, dynamic);
2135 : : }
2136 : : else
2137 : : {
2138 : : /* This code really upsets the gimplifier so don't bother for now. */
2139 : : gfc_constructor *p;
2140 : : HOST_WIDE_INT n;
2141 : : HOST_WIDE_INT size;
2142 : :
2143 : : p = c;
2144 : : n = 0;
2145 : 13525 : while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
2146 : : {
2147 : 1787 : p = gfc_constructor_next (p);
2148 : 1787 : n++;
2149 : : }
2150 : : /* Constructor with few constant elements, or element size not
2151 : : known at compile time (e.g. deferred-length character). */
2152 : 11738 : if (n < 4 || !INTEGER_CST_P (TYPE_SIZE_UNIT (type)))
2153 : : {
2154 : : /* Scalar values. */
2155 : 11633 : gfc_init_se (&se, NULL);
2156 : 11633 : gfc_trans_array_ctor_element (&body, desc, *poffset,
2157 : : &se, c->expr);
2158 : :
2159 : 11633 : *poffset = fold_build2_loc (input_location, PLUS_EXPR,
2160 : : gfc_array_index_type,
2161 : : *poffset, gfc_index_one_node);
2162 : 11633 : if (finalblock)
2163 : 882 : gfc_add_block_to_block (finalblock, &se.finalblock);
2164 : : }
2165 : : else
2166 : : {
2167 : : /* Collect multiple scalar constants into a constructor. */
2168 : 105 : vec<constructor_elt, va_gc> *v = NULL;
2169 : 105 : tree init;
2170 : 105 : tree bound;
2171 : 105 : tree tmptype;
2172 : 105 : HOST_WIDE_INT idx = 0;
2173 : :
2174 : 105 : p = c;
2175 : : /* Count the number of consecutive scalar constants. */
2176 : 837 : while (p && !(p->iterator
2177 : 745 : || p->expr->expr_type != EXPR_CONSTANT))
2178 : : {
2179 : 732 : gfc_init_se (&se, NULL);
2180 : 732 : gfc_conv_constant (&se, p->expr);
2181 : :
2182 : 732 : if (c->expr->ts.type != BT_CHARACTER)
2183 : 660 : se.expr = fold_convert (type, se.expr);
2184 : : /* For constant character array constructors we build
2185 : : an array of pointers. */
2186 : 72 : else if (POINTER_TYPE_P (type))
2187 : 0 : se.expr = gfc_build_addr_expr
2188 : 0 : (gfc_get_pchar_type (p->expr->ts.kind),
2189 : : se.expr);
2190 : :
2191 : 732 : CONSTRUCTOR_APPEND_ELT (v,
2192 : : build_int_cst (gfc_array_index_type,
2193 : : idx++),
2194 : : se.expr);
2195 : 732 : c = p;
2196 : 732 : p = gfc_constructor_next (p);
2197 : : }
2198 : :
2199 : 105 : bound = size_int (n - 1);
2200 : : /* Create an array type to hold them. */
2201 : 105 : tmptype = build_range_type (gfc_array_index_type,
2202 : : gfc_index_zero_node, bound);
2203 : 105 : tmptype = build_array_type (type, tmptype);
2204 : :
2205 : 105 : init = build_constructor (tmptype, v);
2206 : 105 : TREE_CONSTANT (init) = 1;
2207 : 105 : TREE_STATIC (init) = 1;
2208 : : /* Create a static variable to hold the data. */
2209 : 105 : tmp = gfc_create_var (tmptype, "data");
2210 : 105 : TREE_STATIC (tmp) = 1;
2211 : 105 : TREE_CONSTANT (tmp) = 1;
2212 : 105 : TREE_READONLY (tmp) = 1;
2213 : 105 : DECL_INITIAL (tmp) = init;
2214 : 105 : init = tmp;
2215 : :
2216 : : /* Use BUILTIN_MEMCPY to assign the values. */
2217 : 105 : tmp = gfc_conv_descriptor_data_get (desc);
2218 : 105 : tmp = build_fold_indirect_ref_loc (input_location,
2219 : : tmp);
2220 : 105 : tmp = gfc_build_array_ref (tmp, *poffset, NULL);
2221 : 105 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
2222 : 105 : init = gfc_build_addr_expr (NULL_TREE, init);
2223 : :
2224 : 105 : size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2225 : 105 : bound = build_int_cst (size_type_node, n * size);
2226 : 105 : tmp = build_call_expr_loc (input_location,
2227 : : builtin_decl_explicit (BUILT_IN_MEMCPY),
2228 : : 3, tmp, init, bound);
2229 : 105 : gfc_add_expr_to_block (&body, tmp);
2230 : :
2231 : 105 : *poffset = fold_build2_loc (input_location, PLUS_EXPR,
2232 : : gfc_array_index_type, *poffset,
2233 : : build_int_cst (gfc_array_index_type, n));
2234 : : }
2235 : 11738 : if (!INTEGER_CST_P (*poffset))
2236 : : {
2237 : 1583 : gfc_add_modify (&body, *offsetvar, *poffset);
2238 : 1583 : *poffset = *offsetvar;
2239 : : }
2240 : :
2241 : 11738 : if (!c->iterator)
2242 : 11738 : ts = c->expr->ts;
2243 : : }
2244 : :
2245 : : /* The frontend should already have done any expansions
2246 : : at compile-time. */
2247 : 14198 : if (!c->iterator)
2248 : : {
2249 : : /* Pass the code as is. */
2250 : 12938 : tmp = gfc_finish_block (&body);
2251 : 12938 : gfc_add_expr_to_block (pblock, tmp);
2252 : : }
2253 : : else
2254 : : {
2255 : : /* Build the implied do-loop. */
2256 : 1260 : stmtblock_t implied_do_block;
2257 : 1260 : tree cond;
2258 : 1260 : tree exit_label;
2259 : 1260 : tree loopbody;
2260 : 1260 : tree tmp2;
2261 : :
2262 : 1260 : loopbody = gfc_finish_block (&body);
2263 : :
2264 : : /* Create a new block that holds the implied-do loop. A temporary
2265 : : loop-variable is used. */
2266 : 1260 : gfc_start_block(&implied_do_block);
2267 : :
2268 : : /* Initialize the loop. */
2269 : 1260 : gfc_add_modify (&implied_do_block, shadow_loopvar, start);
2270 : :
2271 : : /* If this array expands dynamically, and the number of iterations
2272 : : is not constant, we won't have allocated space for the static
2273 : : part of C->EXPR's size. Do that now. */
2274 : 1260 : if (dynamic && gfc_iterator_has_dynamic_bounds (c->iterator))
2275 : : {
2276 : : /* Get the number of iterations. */
2277 : 493 : tmp = gfc_get_iteration_count (shadow_loopvar, end, step);
2278 : :
2279 : : /* Get the static part of C->EXPR's size. */
2280 : 493 : gfc_get_array_constructor_element_size (&size, c->expr);
2281 : 493 : tmp2 = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
2282 : :
2283 : : /* Grow the array by TMP * TMP2 elements. */
2284 : 493 : tmp = fold_build2_loc (input_location, MULT_EXPR,
2285 : : gfc_array_index_type, tmp, tmp2);
2286 : 493 : gfc_grow_array (&implied_do_block, desc, tmp);
2287 : : }
2288 : :
2289 : : /* Generate the loop body. */
2290 : 1260 : exit_label = gfc_build_label_decl (NULL_TREE);
2291 : 1260 : gfc_start_block (&body);
2292 : :
2293 : : /* Generate the exit condition. Depending on the sign of
2294 : : the step variable we have to generate the correct
2295 : : comparison. */
2296 : 1260 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2297 : 1260 : step, build_int_cst (TREE_TYPE (step), 0));
2298 : 1260 : cond = fold_build3_loc (input_location, COND_EXPR,
2299 : : logical_type_node, tmp,
2300 : : fold_build2_loc (input_location, GT_EXPR,
2301 : : logical_type_node, shadow_loopvar, end),
2302 : : fold_build2_loc (input_location, LT_EXPR,
2303 : : logical_type_node, shadow_loopvar, end));
2304 : 1260 : tmp = build1_v (GOTO_EXPR, exit_label);
2305 : 1260 : TREE_USED (exit_label) = 1;
2306 : 1260 : tmp = build3_v (COND_EXPR, cond, tmp,
2307 : : build_empty_stmt (input_location));
2308 : 1260 : gfc_add_expr_to_block (&body, tmp);
2309 : :
2310 : : /* The main loop body. */
2311 : 1260 : gfc_add_expr_to_block (&body, loopbody);
2312 : :
2313 : : /* Increase loop variable by step. */
2314 : 1260 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
2315 : 1260 : TREE_TYPE (shadow_loopvar), shadow_loopvar,
2316 : : step);
2317 : 1260 : gfc_add_modify (&body, shadow_loopvar, tmp);
2318 : :
2319 : : /* Finish the loop. */
2320 : 1260 : tmp = gfc_finish_block (&body);
2321 : 1260 : tmp = build1_v (LOOP_EXPR, tmp);
2322 : 1260 : gfc_add_expr_to_block (&implied_do_block, tmp);
2323 : :
2324 : : /* Add the exit label. */
2325 : 1260 : tmp = build1_v (LABEL_EXPR, exit_label);
2326 : 1260 : gfc_add_expr_to_block (&implied_do_block, tmp);
2327 : :
2328 : : /* Finish the implied-do loop. */
2329 : 1260 : tmp = gfc_finish_block(&implied_do_block);
2330 : 1260 : gfc_add_expr_to_block(pblock, tmp);
2331 : :
2332 : 1260 : gfc_restore_sym (c->iterator->var->symtree->n.sym, &saved_loopvar);
2333 : : }
2334 : : }
2335 : :
2336 : : /* F2008 4.5.6.3 para 5: If an executable construct references a structure
2337 : : constructor or array constructor, the entity created by the constructor is
2338 : : finalized after execution of the innermost executable construct containing
2339 : : the reference. This, in fact, was later deleted by the Combined Techical
2340 : : Corrigenda 1 TO 4 for fortran 2008 (f08/0011).
2341 : :
2342 : : Transmit finalization of this constructor through 'finalblock'. */
2343 : 8863 : if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
2344 : 8863 : && !(gfc_option.allow_std & GFC_STD_GNU)
2345 : 52 : && finalblock != NULL
2346 : 24 : && gfc_may_be_finalized (ts)
2347 : 18 : && ctr > 0 && desc != NULL_TREE
2348 : 8881 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
2349 : : {
2350 : 18 : symbol_attribute attr;
2351 : 18 : gfc_se fse;
2352 : 18 : gfc_warning (0, "The structure constructor at %C has been"
2353 : : " finalized. This feature was removed by f08/0011."
2354 : : " Use -std=f2018 or -std=gnu to eliminate the"
2355 : : " finalization.");
2356 : 18 : attr.pointer = attr.allocatable = 0;
2357 : 18 : gfc_init_se (&fse, NULL);
2358 : 18 : fse.expr = desc;
2359 : 18 : gfc_finalize_tree_expr (&fse, ts.u.derived, attr, 1);
2360 : 18 : gfc_add_block_to_block (finalblock, &fse.pre);
2361 : 18 : gfc_add_block_to_block (finalblock, &fse.finalblock);
2362 : 18 : gfc_add_block_to_block (finalblock, &fse.post);
2363 : : }
2364 : :
2365 : 8863 : mpz_clear (size);
2366 : 8863 : }
2367 : :
2368 : :
2369 : : /* The array constructor code can create a string length with an operand
2370 : : in the form of a temporary variable. This variable will retain its
2371 : : context (current_function_decl). If we store this length tree in a
2372 : : gfc_charlen structure which is shared by a variable in another
2373 : : context, the resulting gfc_charlen structure with a variable in a
2374 : : different context, we could trip the assertion in expand_expr_real_1
2375 : : when it sees that a variable has been created in one context and
2376 : : referenced in another.
2377 : :
2378 : : If this might be the case, we create a new gfc_charlen structure and
2379 : : link it into the current namespace. */
2380 : :
2381 : : static void
2382 : 7957 : store_backend_decl (gfc_charlen **clp, tree len, bool force_new_cl)
2383 : : {
2384 : 7957 : if (force_new_cl)
2385 : : {
2386 : 7930 : gfc_charlen *new_cl = gfc_new_charlen (gfc_current_ns, *clp);
2387 : 7930 : *clp = new_cl;
2388 : : }
2389 : 7957 : (*clp)->backend_decl = len;
2390 : 7957 : }
2391 : :
2392 : : /* A catch-all to obtain the string length for anything that is not
2393 : : a substring of non-constant length, a constant, array or variable. */
2394 : :
2395 : : static void
2396 : 305 : get_array_ctor_all_strlen (stmtblock_t *block, gfc_expr *e, tree *len)
2397 : : {
2398 : 305 : gfc_se se;
2399 : :
2400 : : /* Don't bother if we already know the length is a constant. */
2401 : 305 : if (*len && INTEGER_CST_P (*len))
2402 : 40 : return;
2403 : :
2404 : 265 : if (!e->ref && e->ts.u.cl && e->ts.u.cl->length
2405 : 29 : && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2406 : : {
2407 : : /* This is easy. */
2408 : 1 : gfc_conv_const_charlen (e->ts.u.cl);
2409 : 1 : *len = e->ts.u.cl->backend_decl;
2410 : : }
2411 : : else
2412 : : {
2413 : : /* Otherwise, be brutal even if inefficient. */
2414 : 264 : gfc_init_se (&se, NULL);
2415 : :
2416 : : /* No function call, in case of side effects. */
2417 : 264 : se.no_function_call = 1;
2418 : 264 : if (e->rank == 0)
2419 : 122 : gfc_conv_expr (&se, e);
2420 : : else
2421 : 142 : gfc_conv_expr_descriptor (&se, e);
2422 : :
2423 : : /* Fix the value. */
2424 : 264 : *len = gfc_evaluate_now (se.string_length, &se.pre);
2425 : :
2426 : 264 : gfc_add_block_to_block (block, &se.pre);
2427 : 264 : gfc_add_block_to_block (block, &se.post);
2428 : :
2429 : 264 : store_backend_decl (&e->ts.u.cl, *len, true);
2430 : : }
2431 : : }
2432 : :
2433 : :
2434 : : /* Figure out the string length of a variable reference expression.
2435 : : Used by get_array_ctor_strlen. */
2436 : :
2437 : : static void
2438 : 900 : get_array_ctor_var_strlen (stmtblock_t *block, gfc_expr * expr, tree * len)
2439 : : {
2440 : 900 : gfc_ref *ref;
2441 : 900 : gfc_typespec *ts;
2442 : 900 : mpz_t char_len;
2443 : 900 : gfc_se se;
2444 : :
2445 : : /* Don't bother if we already know the length is a constant. */
2446 : 900 : if (*len && INTEGER_CST_P (*len))
2447 : 527 : return;
2448 : :
2449 : 453 : ts = &expr->symtree->n.sym->ts;
2450 : 727 : for (ref = expr->ref; ref; ref = ref->next)
2451 : : {
2452 : 354 : switch (ref->type)
2453 : : {
2454 : 231 : case REF_ARRAY:
2455 : : /* Array references don't change the string length. */
2456 : 231 : if (ts->deferred)
2457 : 135 : get_array_ctor_all_strlen (block, expr, len);
2458 : : break;
2459 : :
2460 : 43 : case REF_COMPONENT:
2461 : : /* Use the length of the component. */
2462 : 43 : ts = &ref->u.c.component->ts;
2463 : 43 : break;
2464 : :
2465 : 80 : case REF_SUBSTRING:
2466 : 80 : if (ref->u.ss.end == NULL
2467 : 68 : || ref->u.ss.start->expr_type != EXPR_CONSTANT
2468 : 61 : || ref->u.ss.end->expr_type != EXPR_CONSTANT)
2469 : : {
2470 : : /* Note that this might evaluate expr. */
2471 : 52 : get_array_ctor_all_strlen (block, expr, len);
2472 : 52 : return;
2473 : : }
2474 : 28 : mpz_init_set_ui (char_len, 1);
2475 : 28 : mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
2476 : 28 : mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
2477 : 28 : *len = gfc_conv_mpz_to_tree_type (char_len, gfc_charlen_type_node);
2478 : 28 : mpz_clear (char_len);
2479 : 28 : return;
2480 : :
2481 : : case REF_INQUIRY:
2482 : : break;
2483 : :
2484 : 0 : default:
2485 : 0 : gcc_unreachable ();
2486 : : }
2487 : : }
2488 : :
2489 : : /* A last ditch attempt that is sometimes needed for deferred characters. */
2490 : 373 : if (!ts->u.cl->backend_decl)
2491 : : {
2492 : 19 : gfc_init_se (&se, NULL);
2493 : 19 : if (expr->rank)
2494 : 12 : gfc_conv_expr_descriptor (&se, expr);
2495 : : else
2496 : 7 : gfc_conv_expr (&se, expr);
2497 : 19 : gcc_assert (se.string_length != NULL_TREE);
2498 : 19 : gfc_add_block_to_block (block, &se.pre);
2499 : 19 : ts->u.cl->backend_decl = se.string_length;
2500 : : }
2501 : :
2502 : 373 : *len = ts->u.cl->backend_decl;
2503 : : }
2504 : :
2505 : :
2506 : : /* Figure out the string length of a character array constructor.
2507 : : If len is NULL, don't calculate the length; this happens for recursive calls
2508 : : when a sub-array-constructor is an element but not at the first position,
2509 : : so when we're not interested in the length.
2510 : : Returns TRUE if all elements are character constants. */
2511 : :
2512 : : bool
2513 : 8366 : get_array_ctor_strlen (stmtblock_t *block, gfc_constructor_base base, tree * len)
2514 : : {
2515 : 8366 : gfc_constructor *c;
2516 : 8366 : bool is_const;
2517 : :
2518 : 8366 : is_const = true;
2519 : :
2520 : 8366 : if (gfc_constructor_first (base) == NULL)
2521 : : {
2522 : 303 : if (len)
2523 : 303 : *len = build_int_cstu (gfc_charlen_type_node, 0);
2524 : 303 : return is_const;
2525 : : }
2526 : :
2527 : : /* Loop over all constructor elements to find out is_const, but in len we
2528 : : want to store the length of the first, not the last, element. We can
2529 : : of course exit the loop as soon as is_const is found to be false. */
2530 : 8063 : for (c = gfc_constructor_first (base);
2531 : 44671 : c && is_const; c = gfc_constructor_next (c))
2532 : : {
2533 : 36608 : switch (c->expr->expr_type)
2534 : : {
2535 : 35499 : case EXPR_CONSTANT:
2536 : 35499 : if (len && !(*len && INTEGER_CST_P (*len)))
2537 : 394 : *len = build_int_cstu (gfc_charlen_type_node,
2538 : 394 : c->expr->value.character.length);
2539 : : break;
2540 : :
2541 : 31 : case EXPR_ARRAY:
2542 : 31 : if (!get_array_ctor_strlen (block, c->expr->value.constructor, len))
2543 : 1097 : is_const = false;
2544 : : break;
2545 : :
2546 : 960 : case EXPR_VARIABLE:
2547 : 960 : is_const = false;
2548 : 960 : if (len)
2549 : 900 : get_array_ctor_var_strlen (block, c->expr, len);
2550 : : break;
2551 : :
2552 : 118 : default:
2553 : 118 : is_const = false;
2554 : 118 : if (len)
2555 : 118 : get_array_ctor_all_strlen (block, c->expr, len);
2556 : : break;
2557 : : }
2558 : :
2559 : : /* After the first iteration, we don't want the length modified. */
2560 : 36608 : len = NULL;
2561 : : }
2562 : :
2563 : : return is_const;
2564 : : }
2565 : :
2566 : : /* Check whether the array constructor C consists entirely of constant
2567 : : elements, and if so returns the number of those elements, otherwise
2568 : : return zero. Note, an empty or NULL array constructor returns zero. */
2569 : :
2570 : : unsigned HOST_WIDE_INT
2571 : 49923 : gfc_constant_array_constructor_p (gfc_constructor_base base)
2572 : : {
2573 : 49923 : unsigned HOST_WIDE_INT nelem = 0;
2574 : :
2575 : 49923 : gfc_constructor *c = gfc_constructor_first (base);
2576 : 308048 : while (c)
2577 : : {
2578 : 218123 : if (c->iterator
2579 : 216800 : || c->expr->rank > 0
2580 : 215847 : || c->expr->expr_type != EXPR_CONSTANT)
2581 : : return 0;
2582 : 208202 : c = gfc_constructor_next (c);
2583 : 208202 : nelem++;
2584 : : }
2585 : : return nelem;
2586 : : }
2587 : :
2588 : :
2589 : : /* Given EXPR, the constant array constructor specified by an EXPR_ARRAY,
2590 : : and the tree type of it's elements, TYPE, return a static constant
2591 : : variable that is compile-time initialized. */
2592 : :
2593 : : tree
2594 : 32584 : gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
2595 : : {
2596 : 32584 : tree tmptype, init, tmp;
2597 : 32584 : HOST_WIDE_INT nelem;
2598 : 32584 : gfc_constructor *c;
2599 : 32584 : gfc_array_spec as;
2600 : 32584 : gfc_se se;
2601 : 32584 : int i;
2602 : 32584 : vec<constructor_elt, va_gc> *v = NULL;
2603 : :
2604 : : /* First traverse the constructor list, converting the constants
2605 : : to tree to build an initializer. */
2606 : 32584 : nelem = 0;
2607 : 32584 : c = gfc_constructor_first (expr->value.constructor);
2608 : 249472 : while (c)
2609 : : {
2610 : 184304 : gfc_init_se (&se, NULL);
2611 : 184304 : gfc_conv_constant (&se, c->expr);
2612 : 184304 : if (c->expr->ts.type != BT_CHARACTER)
2613 : 149675 : se.expr = fold_convert (type, se.expr);
2614 : 34629 : else if (POINTER_TYPE_P (type))
2615 : 34629 : se.expr = gfc_build_addr_expr (gfc_get_pchar_type (c->expr->ts.kind),
2616 : : se.expr);
2617 : 184304 : CONSTRUCTOR_APPEND_ELT (v, build_int_cst (gfc_array_index_type, nelem),
2618 : : se.expr);
2619 : 184304 : c = gfc_constructor_next (c);
2620 : 184304 : nelem++;
2621 : : }
2622 : :
2623 : : /* Next determine the tree type for the array. We use the gfortran
2624 : : front-end's gfc_get_nodesc_array_type in order to create a suitable
2625 : : GFC_ARRAY_TYPE_P that may be used by the scalarizer. */
2626 : :
2627 : 32584 : memset (&as, 0, sizeof (gfc_array_spec));
2628 : :
2629 : 32584 : as.rank = expr->rank;
2630 : 32584 : as.type = AS_EXPLICIT;
2631 : 32584 : if (!expr->shape)
2632 : : {
2633 : 3 : as.lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2634 : 3 : as.upper[0] = gfc_get_int_expr (gfc_default_integer_kind,
2635 : : NULL, nelem - 1);
2636 : : }
2637 : : else
2638 : 68702 : for (i = 0; i < expr->rank; i++)
2639 : : {
2640 : 36121 : int tmp = (int) mpz_get_si (expr->shape[i]);
2641 : 36121 : as.lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2642 : 36121 : as.upper[i] = gfc_get_int_expr (gfc_default_integer_kind,
2643 : 36121 : NULL, tmp - 1);
2644 : : }
2645 : :
2646 : 32584 : tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
2647 : :
2648 : : /* as is not needed anymore. */
2649 : 101292 : for (i = 0; i < as.rank + as.corank; i++)
2650 : : {
2651 : 36124 : gfc_free_expr (as.lower[i]);
2652 : 36124 : gfc_free_expr (as.upper[i]);
2653 : : }
2654 : :
2655 : 32584 : init = build_constructor (tmptype, v);
2656 : :
2657 : 32584 : TREE_CONSTANT (init) = 1;
2658 : 32584 : TREE_STATIC (init) = 1;
2659 : :
2660 : 32584 : tmp = build_decl (input_location, VAR_DECL, create_tmp_var_name ("A"),
2661 : : tmptype);
2662 : 32584 : DECL_ARTIFICIAL (tmp) = 1;
2663 : 32584 : DECL_IGNORED_P (tmp) = 1;
2664 : 32584 : TREE_STATIC (tmp) = 1;
2665 : 32584 : TREE_CONSTANT (tmp) = 1;
2666 : 32584 : TREE_READONLY (tmp) = 1;
2667 : 32584 : DECL_INITIAL (tmp) = init;
2668 : 32584 : pushdecl (tmp);
2669 : :
2670 : 32584 : return tmp;
2671 : : }
2672 : :
2673 : :
2674 : : /* Translate a constant EXPR_ARRAY array constructor for the scalarizer.
2675 : : This mostly initializes the scalarizer state info structure with the
2676 : : appropriate values to directly use the array created by the function
2677 : : gfc_build_constant_array_constructor. */
2678 : :
2679 : : static void
2680 : 28064 : trans_constant_array_constructor (gfc_ss * ss, tree type)
2681 : : {
2682 : 28064 : gfc_array_info *info;
2683 : 28064 : tree tmp;
2684 : 28064 : int i;
2685 : :
2686 : 28064 : tmp = gfc_build_constant_array_constructor (ss->info->expr, type);
2687 : :
2688 : 28064 : info = &ss->info->data.array;
2689 : :
2690 : 28064 : info->descriptor = tmp;
2691 : 28064 : info->data = gfc_build_addr_expr (NULL_TREE, tmp);
2692 : 28064 : info->offset = gfc_index_zero_node;
2693 : :
2694 : 58388 : for (i = 0; i < ss->dimen; i++)
2695 : : {
2696 : 30324 : info->delta[i] = gfc_index_zero_node;
2697 : 30324 : info->start[i] = gfc_index_zero_node;
2698 : 30324 : info->end[i] = gfc_index_zero_node;
2699 : 30324 : info->stride[i] = gfc_index_one_node;
2700 : : }
2701 : 28064 : }
2702 : :
2703 : :
2704 : : static int
2705 : 28070 : get_rank (gfc_loopinfo *loop)
2706 : : {
2707 : 28070 : int rank;
2708 : :
2709 : 28070 : rank = 0;
2710 : 124960 : for (; loop; loop = loop->parent)
2711 : 62486 : rank += loop->dimen;
2712 : :
2713 : 34404 : return rank;
2714 : : }
2715 : :
2716 : :
2717 : : /* Helper routine of gfc_trans_array_constructor to determine if the
2718 : : bounds of the loop specified by LOOP are constant and simple enough
2719 : : to use with trans_constant_array_constructor. Returns the
2720 : : iteration count of the loop if suitable, and NULL_TREE otherwise. */
2721 : :
2722 : : static tree
2723 : 28070 : constant_array_constructor_loop_size (gfc_loopinfo * l)
2724 : : {
2725 : 28070 : gfc_loopinfo *loop;
2726 : 28070 : tree size = gfc_index_one_node;
2727 : 28070 : tree tmp;
2728 : 28070 : int i, total_dim;
2729 : :
2730 : 28070 : total_dim = get_rank (l);
2731 : :
2732 : 56140 : for (loop = l; loop; loop = loop->parent)
2733 : : {
2734 : 58412 : for (i = 0; i < loop->dimen; i++)
2735 : : {
2736 : : /* If the bounds aren't constant, return NULL_TREE. */
2737 : 30342 : if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
2738 : : return NULL_TREE;
2739 : 30336 : if (!integer_zerop (loop->from[i]))
2740 : : {
2741 : : /* Only allow nonzero "from" in one-dimensional arrays. */
2742 : 0 : if (total_dim != 1)
2743 : : return NULL_TREE;
2744 : 0 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2745 : : gfc_array_index_type,
2746 : : loop->to[i], loop->from[i]);
2747 : : }
2748 : : else
2749 : 30336 : tmp = loop->to[i];
2750 : 30336 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
2751 : : gfc_array_index_type, tmp, gfc_index_one_node);
2752 : 30336 : size = fold_build2_loc (input_location, MULT_EXPR,
2753 : : gfc_array_index_type, size, tmp);
2754 : : }
2755 : : }
2756 : :
2757 : : return size;
2758 : : }
2759 : :
2760 : :
2761 : : static tree *
2762 : 35657 : get_loop_upper_bound_for_array (gfc_ss *array, int array_dim)
2763 : : {
2764 : 35657 : gfc_ss *ss;
2765 : 35657 : int n;
2766 : :
2767 : 35657 : gcc_assert (array->nested_ss == NULL);
2768 : :
2769 : 35657 : for (ss = array; ss; ss = ss->parent)
2770 : 35657 : for (n = 0; n < ss->loop->dimen; n++)
2771 : 35657 : if (array_dim == get_array_ref_dim_for_loop_dim (ss, n))
2772 : 35657 : return &(ss->loop->to[n]);
2773 : :
2774 : 0 : gcc_unreachable ();
2775 : : }
2776 : :
2777 : :
2778 : : static gfc_loopinfo *
2779 : 575148 : outermost_loop (gfc_loopinfo * loop)
2780 : : {
2781 : 731814 : while (loop->parent != NULL)
2782 : : loop = loop->parent;
2783 : :
2784 : 728573 : return loop;
2785 : : }
2786 : :
2787 : :
2788 : : /* Array constructors are handled by constructing a temporary, then using that
2789 : : within the scalarization loop. This is not optimal, but seems by far the
2790 : : simplest method. */
2791 : :
2792 : : static void
2793 : 35657 : trans_array_constructor (gfc_ss * ss, locus * where)
2794 : : {
2795 : 35657 : gfc_constructor_base c;
2796 : 35657 : tree offset;
2797 : 35657 : tree offsetvar;
2798 : 35657 : tree desc;
2799 : 35657 : tree type;
2800 : 35657 : tree tmp;
2801 : 35657 : tree *loop_ubound0;
2802 : 35657 : bool dynamic;
2803 : 35657 : bool old_first_len, old_typespec_chararray_ctor;
2804 : 35657 : tree old_first_len_val;
2805 : 35657 : gfc_loopinfo *loop, *outer_loop;
2806 : 35657 : gfc_ss_info *ss_info;
2807 : 35657 : gfc_expr *expr;
2808 : 35657 : gfc_ss *s;
2809 : 35657 : tree neg_len;
2810 : 35657 : char *msg;
2811 : 35657 : stmtblock_t finalblock;
2812 : 35657 : bool finalize_required;
2813 : :
2814 : : /* Save the old values for nested checking. */
2815 : 35657 : old_first_len = first_len;
2816 : 35657 : old_first_len_val = first_len_val;
2817 : 35657 : old_typespec_chararray_ctor = typespec_chararray_ctor;
2818 : :
2819 : 35657 : loop = ss->loop;
2820 : 35657 : outer_loop = outermost_loop (loop);
2821 : 35657 : ss_info = ss->info;
2822 : 35657 : expr = ss_info->expr;
2823 : :
2824 : : /* Do bounds-checking here and in gfc_trans_array_ctor_element only if no
2825 : : typespec was given for the array constructor. */
2826 : 71314 : typespec_chararray_ctor = (expr->ts.type == BT_CHARACTER
2827 : 7693 : && expr->ts.u.cl
2828 : 43350 : && expr->ts.u.cl->length_from_typespec);
2829 : :
2830 : 35657 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2831 : 2431 : && expr->ts.type == BT_CHARACTER && !typespec_chararray_ctor)
2832 : : {
2833 : 1450 : first_len_val = gfc_create_var (gfc_charlen_type_node, "len");
2834 : 1450 : first_len = true;
2835 : : }
2836 : :
2837 : 35657 : gcc_assert (ss->dimen == ss->loop->dimen);
2838 : :
2839 : 35657 : c = expr->value.constructor;
2840 : 35657 : if (expr->ts.type == BT_CHARACTER)
2841 : : {
2842 : 7693 : bool const_string;
2843 : 7693 : bool force_new_cl = false;
2844 : :
2845 : : /* get_array_ctor_strlen walks the elements of the constructor, if a
2846 : : typespec was given, we already know the string length and want the one
2847 : : specified there. */
2848 : 7693 : if (typespec_chararray_ctor && expr->ts.u.cl->length
2849 : 342 : && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2850 : : {
2851 : 27 : gfc_se length_se;
2852 : :
2853 : 27 : const_string = false;
2854 : 27 : gfc_init_se (&length_se, NULL);
2855 : 27 : gfc_conv_expr_type (&length_se, expr->ts.u.cl->length,
2856 : : gfc_charlen_type_node);
2857 : 27 : ss_info->string_length = length_se.expr;
2858 : :
2859 : : /* Check if the character length is negative. If it is, then
2860 : : set LEN = 0. */
2861 : 27 : neg_len = fold_build2_loc (input_location, LT_EXPR,
2862 : : logical_type_node, ss_info->string_length,
2863 : 27 : build_zero_cst (TREE_TYPE
2864 : : (ss_info->string_length)));
2865 : : /* Print a warning if bounds checking is enabled. */
2866 : 27 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2867 : : {
2868 : 18 : msg = xasprintf ("Negative character length treated as LEN = 0");
2869 : 18 : gfc_trans_runtime_check (false, true, neg_len, &length_se.pre,
2870 : : where, msg);
2871 : 18 : free (msg);
2872 : : }
2873 : :
2874 : 27 : ss_info->string_length
2875 : 27 : = fold_build3_loc (input_location, COND_EXPR,
2876 : : gfc_charlen_type_node, neg_len,
2877 : : build_zero_cst
2878 : 27 : (TREE_TYPE (ss_info->string_length)),
2879 : : ss_info->string_length);
2880 : 27 : ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
2881 : : &length_se.pre);
2882 : 27 : gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
2883 : 27 : gfc_add_block_to_block (&outer_loop->post, &length_se.post);
2884 : 27 : }
2885 : : else
2886 : : {
2887 : 7666 : const_string = get_array_ctor_strlen (&outer_loop->pre, c,
2888 : : &ss_info->string_length);
2889 : 7666 : force_new_cl = true;
2890 : :
2891 : : /* Initialize "len" with string length for bounds checking. */
2892 : 7666 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2893 : 1468 : && !typespec_chararray_ctor
2894 : 1450 : && ss_info->string_length)
2895 : : {
2896 : 1450 : gfc_se length_se;
2897 : :
2898 : 1450 : gfc_init_se (&length_se, NULL);
2899 : 1450 : gfc_add_modify (&length_se.pre, first_len_val,
2900 : 1450 : fold_convert (TREE_TYPE (first_len_val),
2901 : : ss_info->string_length));
2902 : 1450 : ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
2903 : : &length_se.pre);
2904 : 1450 : gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
2905 : 1450 : gfc_add_block_to_block (&outer_loop->post, &length_se.post);
2906 : : }
2907 : : }
2908 : :
2909 : : /* Complex character array constructors should have been taken care of
2910 : : and not end up here. */
2911 : 7693 : gcc_assert (ss_info->string_length);
2912 : :
2913 : 7693 : store_backend_decl (&expr->ts.u.cl, ss_info->string_length, force_new_cl);
2914 : :
2915 : 7693 : type = gfc_get_character_type_len (expr->ts.kind, ss_info->string_length);
2916 : 7693 : if (const_string)
2917 : 6781 : type = build_pointer_type (type);
2918 : : }
2919 : : else
2920 : 27989 : type = gfc_typenode_for_spec (expr->ts.type == BT_CLASS
2921 : 25 : ? &CLASS_DATA (expr)->ts : &expr->ts);
2922 : :
2923 : : /* See if the constructor determines the loop bounds. */
2924 : 35657 : dynamic = false;
2925 : :
2926 : 35657 : loop_ubound0 = get_loop_upper_bound_for_array (ss, 0);
2927 : :
2928 : 70061 : if (expr->shape && get_rank (loop) > 1 && *loop_ubound0 == NULL_TREE)
2929 : : {
2930 : : /* We have a multidimensional parameter. */
2931 : 0 : for (s = ss; s; s = s->parent)
2932 : : {
2933 : : int n;
2934 : 0 : for (n = 0; n < s->loop->dimen; n++)
2935 : : {
2936 : 0 : s->loop->from[n] = gfc_index_zero_node;
2937 : 0 : s->loop->to[n] = gfc_conv_mpz_to_tree (expr->shape[s->dim[n]],
2938 : : gfc_index_integer_kind);
2939 : 0 : s->loop->to[n] = fold_build2_loc (input_location, MINUS_EXPR,
2940 : : gfc_array_index_type,
2941 : 0 : s->loop->to[n],
2942 : : gfc_index_one_node);
2943 : : }
2944 : : }
2945 : : }
2946 : :
2947 : 35657 : if (*loop_ubound0 == NULL_TREE)
2948 : : {
2949 : 799 : mpz_t size;
2950 : :
2951 : : /* We should have a 1-dimensional, zero-based loop. */
2952 : 799 : gcc_assert (loop->parent == NULL && loop->nested == NULL);
2953 : 799 : gcc_assert (loop->dimen == 1);
2954 : 799 : gcc_assert (integer_zerop (loop->from[0]));
2955 : :
2956 : : /* Split the constructor size into a static part and a dynamic part.
2957 : : Allocate the static size up-front and record whether the dynamic
2958 : : size might be nonzero. */
2959 : 799 : mpz_init (size);
2960 : 799 : dynamic = gfc_get_array_constructor_size (&size, c);
2961 : 799 : mpz_sub_ui (size, size, 1);
2962 : 799 : loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
2963 : 799 : mpz_clear (size);
2964 : : }
2965 : :
2966 : : /* Special case constant array constructors. */
2967 : 799 : if (!dynamic)
2968 : : {
2969 : 34873 : unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
2970 : 34873 : if (nelem > 0)
2971 : : {
2972 : 28070 : tree size = constant_array_constructor_loop_size (loop);
2973 : 28070 : if (size && compare_tree_int (size, nelem) == 0)
2974 : : {
2975 : 28064 : trans_constant_array_constructor (ss, type);
2976 : 28064 : goto finish;
2977 : : }
2978 : : }
2979 : : }
2980 : :
2981 : 7593 : gfc_trans_create_temp_array (&outer_loop->pre, &outer_loop->post, ss, type,
2982 : : NULL_TREE, dynamic, true, false, where);
2983 : :
2984 : 7593 : desc = ss_info->data.array.descriptor;
2985 : 7593 : offset = gfc_index_zero_node;
2986 : 7593 : offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
2987 : 7593 : suppress_warning (offsetvar);
2988 : 7593 : TREE_USED (offsetvar) = 0;
2989 : :
2990 : 7593 : gfc_init_block (&finalblock);
2991 : 7593 : finalize_required = expr->must_finalize;
2992 : 7593 : if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->attr.alloc_comp)
2993 : : finalize_required = true;
2994 : 7617 : gfc_trans_array_constructor_value (&outer_loop->pre,
2995 : : finalize_required ? &finalblock : NULL,
2996 : : type, desc, c, &offset, &offsetvar,
2997 : : dynamic);
2998 : :
2999 : : /* If the array grows dynamically, the upper bound of the loop variable
3000 : : is determined by the array's final upper bound. */
3001 : 7593 : if (dynamic)
3002 : : {
3003 : 784 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
3004 : : gfc_array_index_type,
3005 : : offsetvar, gfc_index_one_node);
3006 : 784 : tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
3007 : 784 : gfc_conv_descriptor_ubound_set (&loop->pre, desc, gfc_rank_cst[0], tmp);
3008 : 784 : if (*loop_ubound0 && VAR_P (*loop_ubound0))
3009 : 0 : gfc_add_modify (&outer_loop->pre, *loop_ubound0, tmp);
3010 : : else
3011 : 784 : *loop_ubound0 = tmp;
3012 : : }
3013 : :
3014 : 7593 : if (TREE_USED (offsetvar))
3015 : 2028 : pushdecl (offsetvar);
3016 : : else
3017 : 5565 : gcc_assert (INTEGER_CST_P (offset));
3018 : :
3019 : : #if 0
3020 : : /* Disable bound checking for now because it's probably broken. */
3021 : : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3022 : : {
3023 : : gcc_unreachable ();
3024 : : }
3025 : : #endif
3026 : :
3027 : 5565 : finish:
3028 : : /* Restore old values of globals. */
3029 : 35657 : first_len = old_first_len;
3030 : 35657 : first_len_val = old_first_len_val;
3031 : 35657 : typespec_chararray_ctor = old_typespec_chararray_ctor;
3032 : :
3033 : : /* F2008 4.5.6.3 para 5: If an executable construct references a structure
3034 : : constructor or array constructor, the entity created by the constructor is
3035 : : finalized after execution of the innermost executable construct containing
3036 : : the reference. */
3037 : 35657 : if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
3038 : 1353 : && finalblock.head != NULL_TREE)
3039 : 20 : gfc_add_block_to_block (&loop->post, &finalblock);
3040 : :
3041 : 35657 : }
3042 : :
3043 : :
3044 : : /* INFO describes a GFC_SS_SECTION in loop LOOP, and this function is
3045 : : called after evaluating all of INFO's vector dimensions. Go through
3046 : : each such vector dimension and see if we can now fill in any missing
3047 : : loop bounds. */
3048 : :
3049 : : static void
3050 : 150893 : set_vector_loop_bounds (gfc_ss * ss)
3051 : : {
3052 : 150893 : gfc_loopinfo *loop, *outer_loop;
3053 : 150893 : gfc_array_info *info;
3054 : 150893 : gfc_se se;
3055 : 150893 : tree tmp;
3056 : 150893 : tree desc;
3057 : 150893 : tree zero;
3058 : 150893 : int n;
3059 : 150893 : int dim;
3060 : :
3061 : 150893 : outer_loop = outermost_loop (ss->loop);
3062 : :
3063 : 150893 : info = &ss->info->data.array;
3064 : :
3065 : 302423 : for (; ss; ss = ss->parent)
3066 : : {
3067 : 151530 : loop = ss->loop;
3068 : :
3069 : 352021 : for (n = 0; n < loop->dimen; n++)
3070 : : {
3071 : 200491 : dim = ss->dim[n];
3072 : 200491 : if (info->ref->u.ar.dimen_type[dim] != DIMEN_VECTOR
3073 : 744 : || loop->to[n] != NULL)
3074 : 200332 : continue;
3075 : :
3076 : : /* Loop variable N indexes vector dimension DIM, and we don't
3077 : : yet know the upper bound of loop variable N. Set it to the
3078 : : difference between the vector's upper and lower bounds. */
3079 : 159 : gcc_assert (loop->from[n] == gfc_index_zero_node);
3080 : 159 : gcc_assert (info->subscript[dim]
3081 : : && info->subscript[dim]->info->type == GFC_SS_VECTOR);
3082 : :
3083 : 159 : gfc_init_se (&se, NULL);
3084 : 159 : desc = info->subscript[dim]->info->data.array.descriptor;
3085 : 159 : zero = gfc_rank_cst[0];
3086 : 159 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
3087 : : gfc_array_index_type,
3088 : : gfc_conv_descriptor_ubound_get (desc, zero),
3089 : : gfc_conv_descriptor_lbound_get (desc, zero));
3090 : 159 : tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
3091 : 159 : loop->to[n] = tmp;
3092 : : }
3093 : : }
3094 : 150893 : }
3095 : :
3096 : :
3097 : : /* Tells whether a scalar argument to an elemental procedure is saved out
3098 : : of a scalarization loop as a value or as a reference. */
3099 : :
3100 : : bool
3101 : 36717 : gfc_scalar_elemental_arg_saved_as_reference (gfc_ss_info * ss_info)
3102 : : {
3103 : 36717 : if (ss_info->type != GFC_SS_REFERENCE)
3104 : : return false;
3105 : :
3106 : 3074 : if (ss_info->data.scalar.needs_temporary)
3107 : : return false;
3108 : :
3109 : : /* If the actual argument can be absent (in other words, it can
3110 : : be a NULL reference), don't try to evaluate it; pass instead
3111 : : the reference directly. */
3112 : 2710 : if (ss_info->can_be_null_ref)
3113 : : return true;
3114 : :
3115 : : /* If the expression is of polymorphic type, it's actual size is not known,
3116 : : so we avoid copying it anywhere. */
3117 : 2074 : if (ss_info->data.scalar.dummy_arg
3118 : 1232 : && gfc_dummy_arg_get_typespec (*ss_info->data.scalar.dummy_arg).type
3119 : : == BT_CLASS
3120 : 2124 : && ss_info->expr->ts.type == BT_CLASS)
3121 : : return true;
3122 : :
3123 : : /* If the expression is a data reference of aggregate type,
3124 : : and the data reference is not used on the left hand side,
3125 : : avoid a copy by saving a reference to the content. */
3126 : 2050 : if (!ss_info->data.scalar.needs_temporary
3127 : 2050 : && (ss_info->expr->ts.type == BT_DERIVED
3128 : 1126 : || ss_info->expr->ts.type == BT_CLASS)
3129 : 3022 : && gfc_expr_is_variable (ss_info->expr))
3130 : : return true;
3131 : :
3132 : : /* Otherwise the expression is evaluated to a temporary variable before the
3133 : : scalarization loop. */
3134 : : return false;
3135 : : }
3136 : :
3137 : :
3138 : : /* Add the pre and post chains for all the scalar expressions in a SS chain
3139 : : to loop. This is called after the loop parameters have been calculated,
3140 : : but before the actual scalarizing loops. */
3141 : :
3142 : : static void
3143 : 153577 : gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
3144 : : locus * where)
3145 : : {
3146 : 153577 : gfc_loopinfo *nested_loop, *outer_loop;
3147 : 153577 : gfc_se se;
3148 : 153577 : gfc_ss_info *ss_info;
3149 : 153577 : gfc_array_info *info;
3150 : 153577 : gfc_expr *expr;
3151 : 153577 : int n;
3152 : :
3153 : : /* Don't evaluate the arguments for realloc_lhs_loop_for_fcn_call; otherwise,
3154 : : arguments could get evaluated multiple times. */
3155 : 153577 : if (ss->is_alloc_lhs)
3156 : 152 : return;
3157 : :
3158 : 153425 : outer_loop = outermost_loop (loop);
3159 : :
3160 : : /* TODO: This can generate bad code if there are ordering dependencies,
3161 : : e.g., a callee allocated function and an unknown size constructor. */
3162 : 153425 : gcc_assert (ss != NULL);
3163 : :
3164 : 405353 : for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
3165 : : {
3166 : 251928 : gcc_assert (ss);
3167 : :
3168 : : /* Cross loop arrays are handled from within the most nested loop. */
3169 : 251928 : if (ss->nested_ss != NULL)
3170 : 737 : continue;
3171 : :
3172 : 251191 : ss_info = ss->info;
3173 : 251191 : expr = ss_info->expr;
3174 : 251191 : info = &ss_info->data.array;
3175 : :
3176 : 251191 : switch (ss_info->type)
3177 : : {
3178 : 39340 : case GFC_SS_SCALAR:
3179 : : /* Scalar expression. Evaluate this now. This includes elemental
3180 : : dimension indices, but not array section bounds. */
3181 : 39340 : gfc_init_se (&se, NULL);
3182 : 39340 : gfc_conv_expr (&se, expr);
3183 : 39340 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3184 : :
3185 : 39340 : if (expr->ts.type != BT_CHARACTER
3186 : 39340 : && !gfc_is_alloc_class_scalar_function (expr))
3187 : : {
3188 : : /* Move the evaluation of scalar expressions outside the
3189 : : scalarization loop, except for WHERE assignments. */
3190 : 35743 : if (subscript)
3191 : 3942 : se.expr = convert(gfc_array_index_type, se.expr);
3192 : 35743 : if (!ss_info->where)
3193 : 35329 : se.expr = gfc_evaluate_now (se.expr, &outer_loop->pre);
3194 : 35743 : gfc_add_block_to_block (&outer_loop->pre, &se.post);
3195 : : }
3196 : : else
3197 : 3597 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3198 : :
3199 : 39340 : ss_info->data.scalar.value = se.expr;
3200 : 39340 : ss_info->string_length = se.string_length;
3201 : 39340 : break;
3202 : :
3203 : 1537 : case GFC_SS_REFERENCE:
3204 : : /* Scalar argument to elemental procedure. */
3205 : 1537 : gfc_init_se (&se, NULL);
3206 : 1537 : if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
3207 : 792 : gfc_conv_expr_reference (&se, expr);
3208 : : else
3209 : : {
3210 : : /* Evaluate the argument outside the loop and pass
3211 : : a reference to the value. */
3212 : 745 : gfc_conv_expr (&se, expr);
3213 : : }
3214 : :
3215 : : /* Ensure that a pointer to the string is stored. */
3216 : 1537 : if (expr->ts.type == BT_CHARACTER)
3217 : 174 : gfc_conv_string_parameter (&se);
3218 : :
3219 : 1537 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3220 : 1537 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3221 : 1537 : if (gfc_is_class_scalar_expr (expr))
3222 : : /* This is necessary because the dynamic type will always be
3223 : : large than the declared type. In consequence, assigning
3224 : : the value to a temporary could segfault.
3225 : : OOP-TODO: see if this is generally correct or is the value
3226 : : has to be written to an allocated temporary, whose address
3227 : : is passed via ss_info. */
3228 : 48 : ss_info->data.scalar.value = se.expr;
3229 : : else
3230 : 1489 : ss_info->data.scalar.value = gfc_evaluate_now (se.expr,
3231 : : &outer_loop->pre);
3232 : :
3233 : 1537 : ss_info->string_length = se.string_length;
3234 : 1537 : break;
3235 : :
3236 : : case GFC_SS_SECTION:
3237 : : /* Add the expressions for scalar and vector subscripts. */
3238 : 2414288 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
3239 : 2263395 : if (info->subscript[n])
3240 : 4686 : gfc_add_loop_ss_code (loop, info->subscript[n], true, where);
3241 : :
3242 : 150893 : set_vector_loop_bounds (ss);
3243 : 150893 : break;
3244 : :
3245 : 744 : case GFC_SS_VECTOR:
3246 : : /* Get the vector's descriptor and store it in SS. */
3247 : 744 : gfc_init_se (&se, NULL);
3248 : 744 : gfc_conv_expr_descriptor (&se, expr);
3249 : 744 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3250 : 744 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3251 : 744 : info->descriptor = se.expr;
3252 : 744 : break;
3253 : :
3254 : 5060 : case GFC_SS_INTRINSIC:
3255 : 5060 : gfc_add_intrinsic_ss_code (loop, ss);
3256 : 5060 : break;
3257 : :
3258 : 8389 : case GFC_SS_FUNCTION:
3259 : : /* Array function return value. We call the function and save its
3260 : : result in a temporary for use inside the loop. */
3261 : 8389 : gfc_init_se (&se, NULL);
3262 : 8389 : se.loop = loop;
3263 : 8389 : se.ss = ss;
3264 : 8389 : if (gfc_is_class_array_function (expr))
3265 : 165 : expr->must_finalize = 1;
3266 : 8389 : gfc_conv_expr (&se, expr);
3267 : 8389 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3268 : 8389 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3269 : 8389 : gfc_add_block_to_block (&outer_loop->post, &se.finalblock);
3270 : 8389 : ss_info->string_length = se.string_length;
3271 : 8389 : break;
3272 : :
3273 : 35657 : case GFC_SS_CONSTRUCTOR:
3274 : 35657 : if (expr->ts.type == BT_CHARACTER
3275 : 7693 : && ss_info->string_length == NULL
3276 : 7693 : && expr->ts.u.cl
3277 : 7693 : && expr->ts.u.cl->length
3278 : 7351 : && expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
3279 : : {
3280 : 7300 : gfc_init_se (&se, NULL);
3281 : 7300 : gfc_conv_expr_type (&se, expr->ts.u.cl->length,
3282 : : gfc_charlen_type_node);
3283 : 7300 : ss_info->string_length = se.expr;
3284 : 7300 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3285 : 7300 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3286 : : }
3287 : 35657 : trans_array_constructor (ss, where);
3288 : 35657 : break;
3289 : :
3290 : : case GFC_SS_TEMP:
3291 : : case GFC_SS_COMPONENT:
3292 : : /* Do nothing. These are handled elsewhere. */
3293 : : break;
3294 : :
3295 : 0 : default:
3296 : 0 : gcc_unreachable ();
3297 : : }
3298 : : }
3299 : :
3300 : 153425 : if (!subscript)
3301 : 149292 : for (nested_loop = loop->nested; nested_loop;
3302 : 553 : nested_loop = nested_loop->next)
3303 : 553 : gfc_add_loop_ss_code (nested_loop, nested_loop->ss, subscript, where);
3304 : : }
3305 : :
3306 : :
3307 : : /* Translate expressions for the descriptor and data pointer of a SS. */
3308 : : /*GCC ARRAYS*/
3309 : :
3310 : : static void
3311 : 272189 : gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
3312 : : {
3313 : 272189 : gfc_se se;
3314 : 272189 : gfc_ss_info *ss_info;
3315 : 272189 : gfc_array_info *info;
3316 : 272189 : tree tmp;
3317 : :
3318 : 272189 : ss_info = ss->info;
3319 : 272189 : info = &ss_info->data.array;
3320 : :
3321 : : /* Get the descriptor for the array to be scalarized. */
3322 : 272189 : gcc_assert (ss_info->expr->expr_type == EXPR_VARIABLE);
3323 : 272189 : gfc_init_se (&se, NULL);
3324 : 272189 : se.descriptor_only = 1;
3325 : 272189 : gfc_conv_expr_lhs (&se, ss_info->expr);
3326 : 272189 : gfc_add_block_to_block (block, &se.pre);
3327 : 272189 : info->descriptor = se.expr;
3328 : 272189 : ss_info->string_length = se.string_length;
3329 : 272189 : ss_info->class_container = se.class_container;
3330 : :
3331 : 272189 : if (base)
3332 : : {
3333 : 99028 : if (ss_info->expr->ts.type == BT_CHARACTER && !ss_info->expr->ts.deferred
3334 : 21679 : && ss_info->expr->ts.u.cl->length == NULL)
3335 : : {
3336 : : /* Emit a DECL_EXPR for the variable sized array type in
3337 : : GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
3338 : : sizes works correctly. */
3339 : 1025 : tree arraytype = TREE_TYPE (
3340 : : GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (info->descriptor)));
3341 : 1025 : if (! TYPE_NAME (arraytype))
3342 : 873 : TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
3343 : : NULL_TREE, arraytype);
3344 : 1025 : gfc_add_expr_to_block (block, build1 (DECL_EXPR, arraytype,
3345 : 1025 : TYPE_NAME (arraytype)));
3346 : : }
3347 : : /* Also the data pointer. */
3348 : 99028 : tmp = gfc_conv_array_data (se.expr);
3349 : : /* If this is a variable or address or a class array, use it directly.
3350 : : Otherwise we must evaluate it now to avoid breaking dependency
3351 : : analysis by pulling the expressions for elemental array indices
3352 : : inside the loop. */
3353 : 99028 : if (!(DECL_P (tmp)
3354 : 89494 : || (TREE_CODE (tmp) == ADDR_EXPR
3355 : 60026 : && DECL_P (TREE_OPERAND (tmp, 0)))
3356 : 31978 : || (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
3357 : 29131 : && TREE_CODE (se.expr) == COMPONENT_REF
3358 : 6928 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (se.expr, 0))))))
3359 : 29378 : tmp = gfc_evaluate_now (tmp, block);
3360 : 99028 : info->data = tmp;
3361 : :
3362 : 99028 : tmp = gfc_conv_array_offset (se.expr);
3363 : 99028 : info->offset = gfc_evaluate_now (tmp, block);
3364 : :
3365 : : /* Make absolutely sure that the saved_offset is indeed saved
3366 : : so that the variable is still accessible after the loops
3367 : : are translated. */
3368 : 99028 : info->saved_offset = info->offset;
3369 : : }
3370 : 272189 : }
3371 : :
3372 : :
3373 : : /* Initialize a gfc_loopinfo structure. */
3374 : :
3375 : : void
3376 : 153345 : gfc_init_loopinfo (gfc_loopinfo * loop)
3377 : : {
3378 : 153345 : int n;
3379 : :
3380 : 153345 : memset (loop, 0, sizeof (gfc_loopinfo));
3381 : 153345 : gfc_init_block (&loop->pre);
3382 : 153345 : gfc_init_block (&loop->post);
3383 : :
3384 : : /* Initially scalarize in order and default to no loop reversal. */
3385 : 2606865 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
3386 : : {
3387 : 2300175 : loop->order[n] = n;
3388 : 2300175 : loop->reverse[n] = GFC_INHIBIT_REVERSE;
3389 : : }
3390 : :
3391 : 153345 : loop->ss = gfc_ss_terminator;
3392 : 153345 : }
3393 : :
3394 : :
3395 : : /* Copies the loop variable info to a gfc_se structure. Does not copy the SS
3396 : : chain. */
3397 : :
3398 : : void
3399 : 146457 : gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
3400 : : {
3401 : 146457 : se->loop = loop;
3402 : 146457 : }
3403 : :
3404 : :
3405 : : /* Return an expression for the data pointer of an array. */
3406 : :
3407 : : tree
3408 : 279252 : gfc_conv_array_data (tree descriptor)
3409 : : {
3410 : 279252 : tree type;
3411 : :
3412 : 279252 : type = TREE_TYPE (descriptor);
3413 : 279252 : if (GFC_ARRAY_TYPE_P (type))
3414 : : {
3415 : 203124 : if (TREE_CODE (type) == POINTER_TYPE)
3416 : : return descriptor;
3417 : : else
3418 : : {
3419 : : /* Descriptorless arrays. */
3420 : 153048 : return gfc_build_addr_expr (NULL_TREE, descriptor);
3421 : : }
3422 : : }
3423 : : else
3424 : 76128 : return gfc_conv_descriptor_data_get (descriptor);
3425 : : }
3426 : :
3427 : :
3428 : : /* Return an expression for the base offset of an array. */
3429 : :
3430 : : tree
3431 : 210507 : gfc_conv_array_offset (tree descriptor)
3432 : : {
3433 : 210507 : tree type;
3434 : :
3435 : 210507 : type = TREE_TYPE (descriptor);
3436 : 210507 : if (GFC_ARRAY_TYPE_P (type))
3437 : 156301 : return GFC_TYPE_ARRAY_OFFSET (type);
3438 : : else
3439 : 54206 : return gfc_conv_descriptor_offset_get (descriptor);
3440 : : }
3441 : :
3442 : :
3443 : : /* Get an expression for the array stride. */
3444 : :
3445 : : tree
3446 : 404778 : gfc_conv_array_stride (tree descriptor, int dim)
3447 : : {
3448 : 404778 : tree tmp;
3449 : 404778 : tree type;
3450 : :
3451 : 404778 : type = TREE_TYPE (descriptor);
3452 : :
3453 : : /* For descriptorless arrays use the array size. */
3454 : 404778 : tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
3455 : 404778 : if (tmp != NULL_TREE)
3456 : : return tmp;
3457 : :
3458 : 76690 : tmp = gfc_conv_descriptor_stride_get (descriptor, gfc_rank_cst[dim]);
3459 : 76690 : return tmp;
3460 : : }
3461 : :
3462 : :
3463 : : /* Like gfc_conv_array_stride, but for the lower bound. */
3464 : :
3465 : : tree
3466 : 271474 : gfc_conv_array_lbound (tree descriptor, int dim)
3467 : : {
3468 : 271474 : tree tmp;
3469 : 271474 : tree type;
3470 : :
3471 : 271474 : type = TREE_TYPE (descriptor);
3472 : :
3473 : 271474 : tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
3474 : 271474 : if (tmp != NULL_TREE)
3475 : : return tmp;
3476 : :
3477 : 11438 : tmp = gfc_conv_descriptor_lbound_get (descriptor, gfc_rank_cst[dim]);
3478 : 11438 : return tmp;
3479 : : }
3480 : :
3481 : :
3482 : : /* Like gfc_conv_array_stride, but for the upper bound. */
3483 : :
3484 : : tree
3485 : 176670 : gfc_conv_array_ubound (tree descriptor, int dim)
3486 : : {
3487 : 176670 : tree tmp;
3488 : 176670 : tree type;
3489 : :
3490 : 176670 : type = TREE_TYPE (descriptor);
3491 : :
3492 : 176670 : tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
3493 : 176670 : if (tmp != NULL_TREE)
3494 : : return tmp;
3495 : :
3496 : : /* This should only ever happen when passing an assumed shape array
3497 : : as an actual parameter. The value will never be used. */
3498 : 7212 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
3499 : 547 : return gfc_index_zero_node;
3500 : :
3501 : 6665 : tmp = gfc_conv_descriptor_ubound_get (descriptor, gfc_rank_cst[dim]);
3502 : 6665 : return tmp;
3503 : : }
3504 : :
3505 : :
3506 : : /* Generate abridged name of a part-ref for use in bounds-check message.
3507 : : Cases:
3508 : : (1) for an ordinary array variable x return "x"
3509 : : (2) for z a DT scalar and array component x (at level 1) return "z%%x"
3510 : : (3) for z a DT scalar and array component x (at level > 1) or
3511 : : for z a DT array and array x (at any number of levels): "z...%%x"
3512 : : */
3513 : :
3514 : : static char *
3515 : 35543 : abridged_ref_name (gfc_expr * expr, gfc_array_ref * ar)
3516 : : {
3517 : 35543 : gfc_ref *ref;
3518 : 35543 : gfc_symbol *sym;
3519 : 35543 : char *ref_name = NULL;
3520 : 35543 : const char *comp_name = NULL;
3521 : 35543 : int len_sym, last_len = 0, level = 0;
3522 : 35543 : bool sym_is_array;
3523 : :
3524 : 35543 : gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->ref != NULL);
3525 : :
3526 : 35543 : sym = expr->symtree->n.sym;
3527 : 71086 : sym_is_array = (sym->ts.type != BT_CLASS
3528 : 35543 : ? sym->as != NULL
3529 : 159 : : IS_CLASS_ARRAY (sym));
3530 : 35543 : len_sym = strlen (sym->name);
3531 : :
3532 : : /* Scan ref chain to get name of the array component (when ar != NULL) or
3533 : : array section, determine depth and remember its component name. */
3534 : 50065 : for (ref = expr->ref; ref; ref = ref->next)
3535 : : {
3536 : 36482 : if (ref->type == REF_COMPONENT
3537 : 630 : && strcmp (ref->u.c.component->name, "_data") != 0)
3538 : : {
3539 : 500 : level++;
3540 : 500 : comp_name = ref->u.c.component->name;
3541 : 500 : continue;
3542 : : }
3543 : :
3544 : 35982 : if (ref->type != REF_ARRAY)
3545 : 142 : continue;
3546 : :
3547 : 35840 : if (ar)
3548 : : {
3549 : 15507 : if (&ref->u.ar == ar)
3550 : : break;
3551 : : }
3552 : 20333 : else if (ref->u.ar.type == AR_SECTION)
3553 : : break;
3554 : : }
3555 : :
3556 : 35543 : if (level > 0)
3557 : 474 : last_len = strlen (comp_name);
3558 : :
3559 : : /* Provide a buffer sufficiently large to hold "x...%%z". */
3560 : 35543 : ref_name = XNEWVEC (char, len_sym + last_len + 6);
3561 : 35543 : strcpy (ref_name, sym->name);
3562 : :
3563 : 35543 : if (level == 1 && !sym_is_array)
3564 : : {
3565 : 190 : strcat (ref_name, "%%");
3566 : 190 : strcat (ref_name, comp_name);
3567 : : }
3568 : 35353 : else if (level > 0)
3569 : : {
3570 : 284 : strcat (ref_name, "...%%");
3571 : 284 : strcat (ref_name, comp_name);
3572 : : }
3573 : :
3574 : 35543 : return ref_name;
3575 : : }
3576 : :
3577 : :
3578 : : /* Generate code to perform an array index bound check. */
3579 : :
3580 : : static tree
3581 : 3622 : trans_array_bound_check (gfc_se * se, gfc_ss *ss, tree index, int n,
3582 : : locus * where, bool check_upper,
3583 : : const char *compname = NULL)
3584 : : {
3585 : 3622 : tree fault;
3586 : 3622 : tree tmp_lo, tmp_up;
3587 : 3622 : tree descriptor;
3588 : 3622 : char *msg;
3589 : 3622 : char *ref_name = NULL;
3590 : 3622 : const char * name = NULL;
3591 : 3622 : gfc_expr *expr;
3592 : :
3593 : 3622 : if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
3594 : : return index;
3595 : :
3596 : 216 : descriptor = ss->info->data.array.descriptor;
3597 : :
3598 : 216 : index = gfc_evaluate_now (index, &se->pre);
3599 : :
3600 : : /* We find a name for the error message. */
3601 : 216 : name = ss->info->expr->symtree->n.sym->name;
3602 : 216 : gcc_assert (name != NULL);
3603 : :
3604 : : /* When we have a component ref, get name of the array section.
3605 : : Note that there can only be one part ref. */
3606 : 216 : expr = ss->info->expr;
3607 : 216 : if (expr->ref && !compname)
3608 : 142 : name = ref_name = abridged_ref_name (expr, NULL);
3609 : :
3610 : 216 : if (VAR_P (descriptor))
3611 : 138 : name = IDENTIFIER_POINTER (DECL_NAME (descriptor));
3612 : :
3613 : : /* Use given (array component) name. */
3614 : 216 : if (compname)
3615 : 74 : name = compname;
3616 : :
3617 : : /* If upper bound is present, include both bounds in the error message. */
3618 : 216 : if (check_upper)
3619 : : {
3620 : 189 : tmp_lo = gfc_conv_array_lbound (descriptor, n);
3621 : 189 : tmp_up = gfc_conv_array_ubound (descriptor, n);
3622 : :
3623 : 189 : if (name)
3624 : 189 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3625 : : "outside of expected range (%%ld:%%ld)", n+1, name);
3626 : : else
3627 : 0 : msg = xasprintf ("Index '%%ld' of dimension %d "
3628 : : "outside of expected range (%%ld:%%ld)", n+1);
3629 : :
3630 : 189 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
3631 : : index, tmp_lo);
3632 : 189 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3633 : : fold_convert (long_integer_type_node, index),
3634 : : fold_convert (long_integer_type_node, tmp_lo),
3635 : : fold_convert (long_integer_type_node, tmp_up));
3636 : 189 : fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3637 : : index, tmp_up);
3638 : 189 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3639 : : fold_convert (long_integer_type_node, index),
3640 : : fold_convert (long_integer_type_node, tmp_lo),
3641 : : fold_convert (long_integer_type_node, tmp_up));
3642 : 189 : free (msg);
3643 : : }
3644 : : else
3645 : : {
3646 : 27 : tmp_lo = gfc_conv_array_lbound (descriptor, n);
3647 : :
3648 : 27 : if (name)
3649 : 27 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3650 : : "below lower bound of %%ld", n+1, name);
3651 : : else
3652 : 0 : msg = xasprintf ("Index '%%ld' of dimension %d "
3653 : : "below lower bound of %%ld", n+1);
3654 : :
3655 : 27 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
3656 : : index, tmp_lo);
3657 : 27 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3658 : : fold_convert (long_integer_type_node, index),
3659 : : fold_convert (long_integer_type_node, tmp_lo));
3660 : 27 : free (msg);
3661 : : }
3662 : :
3663 : 216 : free (ref_name);
3664 : 216 : return index;
3665 : : }
3666 : :
3667 : :
3668 : : /* Generate code for bounds checking for elemental dimensions. */
3669 : :
3670 : : static void
3671 : 6641 : array_bound_check_elemental (gfc_se * se, gfc_ss * ss, gfc_expr * expr)
3672 : : {
3673 : 6641 : gfc_array_ref *ar;
3674 : 6641 : gfc_ref *ref;
3675 : 6641 : char *var_name = NULL;
3676 : 6641 : int dim;
3677 : :
3678 : 6641 : if (expr->expr_type == EXPR_VARIABLE)
3679 : : {
3680 : 12515 : for (ref = expr->ref; ref; ref = ref->next)
3681 : : {
3682 : 6281 : if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3683 : : {
3684 : 3971 : ar = &ref->u.ar;
3685 : 3971 : var_name = abridged_ref_name (expr, ar);
3686 : 8231 : for (dim = 0; dim < ar->dimen; dim++)
3687 : : {
3688 : 4260 : if (ar->dimen_type[dim] == DIMEN_ELEMENT)
3689 : : {
3690 : 74 : gfc_se indexse;
3691 : 74 : gfc_init_se (&indexse, NULL);
3692 : 74 : gfc_conv_expr_type (&indexse, ar->start[dim],
3693 : : gfc_array_index_type);
3694 : 74 : trans_array_bound_check (se, ss, indexse.expr, dim,
3695 : : &ar->where,
3696 : 74 : ar->as->type != AS_ASSUMED_SIZE
3697 : 74 : || dim < ar->dimen - 1,
3698 : : var_name);
3699 : : }
3700 : : }
3701 : 3971 : free (var_name);
3702 : : }
3703 : : }
3704 : : }
3705 : 6641 : }
3706 : :
3707 : :
3708 : : /* Return the offset for an index. Performs bound checking for elemental
3709 : : dimensions. Single element references are processed separately.
3710 : : DIM is the array dimension, I is the loop dimension. */
3711 : :
3712 : : static tree
3713 : 185484 : conv_array_index_offset (gfc_se * se, gfc_ss * ss, int dim, int i,
3714 : : gfc_array_ref * ar, tree stride)
3715 : : {
3716 : 185484 : gfc_array_info *info;
3717 : 185484 : tree index;
3718 : 185484 : tree desc;
3719 : 185484 : tree data;
3720 : :
3721 : 185484 : info = &ss->info->data.array;
3722 : :
3723 : : /* Get the index into the array for this dimension. */
3724 : 185484 : if (ar)
3725 : : {
3726 : 128922 : gcc_assert (ar->type != AR_ELEMENT);
3727 : 128922 : switch (ar->dimen_type[dim])
3728 : : {
3729 : 0 : case DIMEN_THIS_IMAGE:
3730 : 0 : gcc_unreachable ();
3731 : 2811 : break;
3732 : 2811 : case DIMEN_ELEMENT:
3733 : : /* Elemental dimension. */
3734 : 2811 : gcc_assert (info->subscript[dim]
3735 : : && info->subscript[dim]->info->type == GFC_SS_SCALAR);
3736 : : /* We've already translated this value outside the loop. */
3737 : 2811 : index = info->subscript[dim]->info->data.scalar.value;
3738 : :
3739 : 5622 : index = trans_array_bound_check (se, ss, index, dim, &ar->where,
3740 : 2811 : ar->as->type != AS_ASSUMED_SIZE
3741 : 2811 : || dim < ar->dimen - 1);
3742 : 2811 : break;
3743 : :
3744 : 737 : case DIMEN_VECTOR:
3745 : 737 : gcc_assert (info && se->loop);
3746 : 737 : gcc_assert (info->subscript[dim]
3747 : : && info->subscript[dim]->info->type == GFC_SS_VECTOR);
3748 : 737 : desc = info->subscript[dim]->info->data.array.descriptor;
3749 : :
3750 : : /* Get a zero-based index into the vector. */
3751 : 737 : index = fold_build2_loc (input_location, MINUS_EXPR,
3752 : : gfc_array_index_type,
3753 : : se->loop->loopvar[i], se->loop->from[i]);
3754 : :
3755 : : /* Multiply the index by the stride. */
3756 : 737 : index = fold_build2_loc (input_location, MULT_EXPR,
3757 : : gfc_array_index_type,
3758 : : index, gfc_conv_array_stride (desc, 0));
3759 : :
3760 : : /* Read the vector to get an index into info->descriptor. */
3761 : 737 : data = build_fold_indirect_ref_loc (input_location,
3762 : : gfc_conv_array_data (desc));
3763 : 737 : index = gfc_build_array_ref (data, index, NULL);
3764 : 737 : index = gfc_evaluate_now (index, &se->pre);
3765 : 737 : index = fold_convert (gfc_array_index_type, index);
3766 : :
3767 : : /* Do any bounds checking on the final info->descriptor index. */
3768 : 1474 : index = trans_array_bound_check (se, ss, index, dim, &ar->where,
3769 : 737 : ar->as->type != AS_ASSUMED_SIZE
3770 : 737 : || dim < ar->dimen - 1);
3771 : 737 : break;
3772 : :
3773 : 125374 : case DIMEN_RANGE:
3774 : : /* Scalarized dimension. */
3775 : 125374 : gcc_assert (info && se->loop);
3776 : :
3777 : : /* Multiply the loop variable by the stride and delta. */
3778 : 125374 : index = se->loop->loopvar[i];
3779 : 125374 : if (!integer_onep (info->stride[dim]))
3780 : 5336 : index = fold_build2_loc (input_location, MULT_EXPR,
3781 : : gfc_array_index_type, index,
3782 : : info->stride[dim]);
3783 : 125374 : if (!integer_zerop (info->delta[dim]))
3784 : 48238 : index = fold_build2_loc (input_location, PLUS_EXPR,
3785 : : gfc_array_index_type, index,
3786 : : info->delta[dim]);
3787 : : break;
3788 : :
3789 : 0 : default:
3790 : 0 : gcc_unreachable ();
3791 : : }
3792 : : }
3793 : : else
3794 : : {
3795 : : /* Temporary array or derived type component. */
3796 : 56562 : gcc_assert (se->loop);
3797 : 56562 : index = se->loop->loopvar[se->loop->order[i]];
3798 : :
3799 : : /* Pointer functions can have stride[0] different from unity.
3800 : : Use the stride returned by the function call and stored in
3801 : : the descriptor for the temporary. */
3802 : 56562 : if (se->ss && se->ss->info->type == GFC_SS_FUNCTION
3803 : 7000 : && se->ss->info->expr
3804 : 7000 : && se->ss->info->expr->symtree
3805 : 7000 : && se->ss->info->expr->symtree->n.sym->result
3806 : 6667 : && se->ss->info->expr->symtree->n.sym->result->attr.pointer)
3807 : 138 : stride = gfc_conv_descriptor_stride_get (info->descriptor,
3808 : : gfc_rank_cst[dim]);
3809 : :
3810 : 56562 : if (info->delta[dim] && !integer_zerop (info->delta[dim]))
3811 : 708 : index = fold_build2_loc (input_location, PLUS_EXPR,
3812 : : gfc_array_index_type, index, info->delta[dim]);
3813 : : }
3814 : :
3815 : : /* Multiply by the stride. */
3816 : 185484 : if (stride != NULL && !integer_onep (stride))
3817 : 47401 : index = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
3818 : : index, stride);
3819 : :
3820 : 185484 : return index;
3821 : : }
3822 : :
3823 : :
3824 : : /* Build a scalarized array reference using the vptr 'size'. */
3825 : :
3826 : : static bool
3827 : 151904 : build_class_array_ref (gfc_se *se, tree base, tree index)
3828 : : {
3829 : 151904 : tree size;
3830 : 151904 : tree decl = NULL_TREE;
3831 : 151904 : tree tmp;
3832 : 151904 : gfc_expr *expr = se->ss->info->expr;
3833 : 151904 : gfc_expr *class_expr;
3834 : 151904 : gfc_typespec *ts;
3835 : 151904 : gfc_symbol *sym;
3836 : :
3837 : 151904 : tmp = !VAR_P (base) ? gfc_get_class_from_expr (base) : NULL_TREE;
3838 : :
3839 : 67988 : if (tmp != NULL_TREE)
3840 : : decl = tmp;
3841 : : else
3842 : : {
3843 : : /* The base expression does not contain a class component, either
3844 : : because it is a temporary array or array descriptor. Class
3845 : : array functions are correctly resolved above. */
3846 : 149032 : if (!expr
3847 : 149032 : || (expr->ts.type != BT_CLASS
3848 : 138432 : && !gfc_is_class_array_ref (expr, NULL)))
3849 : 148658 : return false;
3850 : :
3851 : : /* Obtain the expression for the class entity or component that is
3852 : : followed by an array reference, which is not an element, so that
3853 : : the span of the array can be obtained. */
3854 : 374 : class_expr = gfc_find_and_cut_at_last_class_ref (expr, false, &ts);
3855 : :
3856 : 374 : if (!ts)
3857 : : return false;
3858 : :
3859 : 349 : sym = (!class_expr && expr) ? expr->symtree->n.sym : NULL;
3860 : 0 : if (sym && sym->attr.function
3861 : 0 : && sym == sym->result
3862 : 0 : && sym->backend_decl == current_function_decl)
3863 : : /* The temporary is the data field of the class data component
3864 : : of the current function. */
3865 : 0 : decl = gfc_get_fake_result_decl (sym, 0);
3866 : 349 : else if (sym)
3867 : : {
3868 : 0 : if (decl == NULL_TREE)
3869 : 0 : decl = expr->symtree->n.sym->backend_decl;
3870 : : /* For class arrays the tree containing the class is stored in
3871 : : GFC_DECL_SAVED_DESCRIPTOR of the sym's backend_decl.
3872 : : For all others it's sym's backend_decl directly. */
3873 : 0 : if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
3874 : 0 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
3875 : : }
3876 : : else
3877 : 349 : decl = gfc_get_class_from_gfc_expr (class_expr);
3878 : :
3879 : 349 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
3880 : 0 : decl = build_fold_indirect_ref_loc (input_location, decl);
3881 : :
3882 : 349 : if (!GFC_CLASS_TYPE_P (TREE_TYPE (decl)))
3883 : : return false;
3884 : : }
3885 : :
3886 : 3221 : se->class_vptr = gfc_evaluate_now (gfc_class_vptr_get (decl), &se->pre);
3887 : :
3888 : 3221 : size = gfc_class_vtab_size_get (decl);
3889 : : /* For unlimited polymorphic entities then _len component needs to be
3890 : : multiplied with the size. */
3891 : 3221 : size = gfc_resize_class_size_with_len (&se->pre, decl, size);
3892 : 3221 : size = fold_convert (TREE_TYPE (index), size);
3893 : :
3894 : : /* Return the element in the se expression. */
3895 : 3221 : se->expr = gfc_build_spanned_array_ref (base, index, size);
3896 : 3221 : return true;
3897 : : }
3898 : :
3899 : :
3900 : : /* Indicates that the tree EXPR is a reference to an array that can’t
3901 : : have any negative stride. */
3902 : :
3903 : : static bool
3904 : 262031 : non_negative_strides_array_p (tree expr)
3905 : : {
3906 : 273946 : if (expr == NULL_TREE)
3907 : : return false;
3908 : :
3909 : 273946 : tree type = TREE_TYPE (expr);
3910 : 273946 : if (POINTER_TYPE_P (type))
3911 : 62024 : type = TREE_TYPE (type);
3912 : :
3913 : 273946 : if (TYPE_LANG_SPECIFIC (type))
3914 : : {
3915 : 273946 : gfc_array_kind array_kind = GFC_TYPE_ARRAY_AKIND (type);
3916 : :
3917 : 273946 : if (array_kind == GFC_ARRAY_ALLOCATABLE
3918 : 273946 : || array_kind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3919 : : return true;
3920 : : }
3921 : :
3922 : : /* An array with descriptor can have negative strides.
3923 : : We try to be conservative and return false by default here
3924 : : if we don’t recognize a contiguous array instead of
3925 : : returning false if we can identify a non-contiguous one. */
3926 : 233716 : if (!GFC_ARRAY_TYPE_P (type))
3927 : : return false;
3928 : :
3929 : : /* If the array was originally a dummy with a descriptor, strides can be
3930 : : negative. */
3931 : 204386 : if (DECL_P (expr)
3932 : 196412 : && DECL_LANG_SPECIFIC (expr)
3933 : 44402 : && GFC_DECL_SAVED_DESCRIPTOR (expr)
3934 : 216320 : && GFC_DECL_SAVED_DESCRIPTOR (expr) != expr)
3935 : 11915 : return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (expr));
3936 : :
3937 : : return true;
3938 : : }
3939 : :
3940 : :
3941 : : /* Build a scalarized reference to an array. */
3942 : :
3943 : : static void
3944 : 151904 : gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar,
3945 : : bool tmp_array = false)
3946 : : {
3947 : 151904 : gfc_array_info *info;
3948 : 151904 : tree decl = NULL_TREE;
3949 : 151904 : tree index;
3950 : 151904 : tree base;
3951 : 151904 : gfc_ss *ss;
3952 : 151904 : gfc_expr *expr;
3953 : 151904 : int n;
3954 : :
3955 : 151904 : ss = se->ss;
3956 : 151904 : expr = ss->info->expr;
3957 : 151904 : info = &ss->info->data.array;
3958 : 151904 : if (ar)
3959 : 103816 : n = se->loop->order[0];
3960 : : else
3961 : : n = 0;
3962 : :
3963 : 151904 : index = conv_array_index_offset (se, ss, ss->dim[n], n, ar, info->stride0);
3964 : : /* Add the offset for this dimension to the stored offset for all other
3965 : : dimensions. */
3966 : 151904 : if (info->offset && !integer_zerop (info->offset))
3967 : 110504 : index = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
3968 : : index, info->offset);
3969 : :
3970 : 151904 : base = build_fold_indirect_ref_loc (input_location, info->data);
3971 : :
3972 : : /* Use the vptr 'size' field to access the element of a class array. */
3973 : 151904 : if (build_class_array_ref (se, base, index))
3974 : 3221 : return;
3975 : :
3976 : 148683 : if (get_CFI_desc (NULL, expr, &decl, ar))
3977 : 442 : decl = build_fold_indirect_ref_loc (input_location, decl);
3978 : :
3979 : : /* A pointer array component can be detected from its field decl. Fix
3980 : : the descriptor, mark the resulting variable decl and pass it to
3981 : : gfc_build_array_ref. */
3982 : 148683 : if (is_pointer_array (info->descriptor)
3983 : 148683 : || (expr && expr->ts.deferred && info->descriptor
3984 : 2374 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (info->descriptor))))
3985 : : {
3986 : 7905 : if (TREE_CODE (info->descriptor) == COMPONENT_REF)
3987 : 1238 : decl = info->descriptor;
3988 : 6667 : else if (INDIRECT_REF_P (info->descriptor))
3989 : 1369 : decl = TREE_OPERAND (info->descriptor, 0);
3990 : :
3991 : 7905 : if (decl == NULL_TREE)
3992 : 5298 : decl = info->descriptor;
3993 : : }
3994 : :
3995 : 148683 : bool non_negative_stride = tmp_array
3996 : 148683 : || non_negative_strides_array_p (info->descriptor);
3997 : 148683 : se->expr = gfc_build_array_ref (base, index, decl,
3998 : : non_negative_stride);
3999 : : }
4000 : :
4001 : :
4002 : : /* Translate access of temporary array. */
4003 : :
4004 : : void
4005 : 48088 : gfc_conv_tmp_array_ref (gfc_se * se)
4006 : : {
4007 : 48088 : se->string_length = se->ss->info->string_length;
4008 : 48088 : gfc_conv_scalarized_array_ref (se, NULL, true);
4009 : 48088 : gfc_advance_se_ss_chain (se);
4010 : 48088 : }
4011 : :
4012 : : /* Add T to the offset pair *OFFSET, *CST_OFFSET. */
4013 : :
4014 : : static void
4015 : 247544 : add_to_offset (tree *cst_offset, tree *offset, tree t)
4016 : : {
4017 : 247544 : if (TREE_CODE (t) == INTEGER_CST)
4018 : 124141 : *cst_offset = int_const_binop (PLUS_EXPR, *cst_offset, t);
4019 : : else
4020 : : {
4021 : 123403 : if (!integer_zerop (*offset))
4022 : 45469 : *offset = fold_build2_loc (input_location, PLUS_EXPR,
4023 : : gfc_array_index_type, *offset, t);
4024 : : else
4025 : 77934 : *offset = t;
4026 : : }
4027 : 247544 : }
4028 : :
4029 : :
4030 : : static tree
4031 : 161138 : build_array_ref (tree desc, tree offset, tree decl, tree vptr)
4032 : : {
4033 : 161138 : tree tmp;
4034 : 161138 : tree type;
4035 : 161138 : tree cdesc;
4036 : :
4037 : : /* For class arrays the class declaration is stored in the saved
4038 : : descriptor. */
4039 : 161138 : if (INDIRECT_REF_P (desc)
4040 : 6860 : && DECL_LANG_SPECIFIC (TREE_OPERAND (desc, 0))
4041 : 163332 : && GFC_DECL_SAVED_DESCRIPTOR (TREE_OPERAND (desc, 0)))
4042 : 780 : cdesc = gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (
4043 : : TREE_OPERAND (desc, 0)));
4044 : : else
4045 : : cdesc = desc;
4046 : :
4047 : : /* Class container types do not always have the GFC_CLASS_TYPE_P
4048 : : but the canonical type does. */
4049 : 161138 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (cdesc))
4050 : 161138 : && TREE_CODE (cdesc) == COMPONENT_REF)
4051 : : {
4052 : 8073 : type = TREE_TYPE (TREE_OPERAND (cdesc, 0));
4053 : 8073 : if (TYPE_CANONICAL (type)
4054 : 8073 : && GFC_CLASS_TYPE_P (TYPE_CANONICAL (type)))
4055 : 3049 : vptr = gfc_class_vptr_get (TREE_OPERAND (cdesc, 0));
4056 : : }
4057 : :
4058 : 161138 : tmp = gfc_conv_array_data (desc);
4059 : 161138 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
4060 : 161138 : tmp = gfc_build_array_ref (tmp, offset, decl,
4061 : 161138 : non_negative_strides_array_p (desc),
4062 : : vptr);
4063 : 161138 : return tmp;
4064 : : }
4065 : :
4066 : :
4067 : : /* Build an array reference. se->expr already holds the array descriptor.
4068 : : This should be either a variable, indirect variable reference or component
4069 : : reference. For arrays which do not have a descriptor, se->expr will be
4070 : : the data pointer.
4071 : : a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
4072 : :
4073 : : void
4074 : 218345 : gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_expr *expr,
4075 : : locus * where)
4076 : : {
4077 : 218345 : int n;
4078 : 218345 : tree offset, cst_offset;
4079 : 218345 : tree tmp;
4080 : 218345 : tree stride;
4081 : 218345 : tree decl = NULL_TREE;
4082 : 218345 : gfc_se indexse;
4083 : 218345 : gfc_se tmpse;
4084 : 218345 : gfc_symbol * sym = expr->symtree->n.sym;
4085 : 218345 : char *var_name = NULL;
4086 : :
4087 : 218345 : if (ar->dimen == 0)
4088 : : {
4089 : 4008 : gcc_assert (ar->codimen || sym->attr.select_rank_temporary
4090 : : || (ar->as && ar->as->corank));
4091 : :
4092 : 4008 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
4093 : 804 : se->expr = build_fold_indirect_ref (gfc_conv_array_data (se->expr));
4094 : : else
4095 : : {
4096 : 3204 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr))
4097 : 3204 : && TREE_CODE (TREE_TYPE (se->expr)) == POINTER_TYPE)
4098 : 2394 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
4099 : :
4100 : : /* Use the actual tree type and not the wrapped coarray. */
4101 : 3204 : if (!se->want_pointer)
4102 : 2416 : se->expr = fold_convert (TYPE_MAIN_VARIANT (TREE_TYPE (se->expr)),
4103 : : se->expr);
4104 : : }
4105 : :
4106 : 107824 : return;
4107 : : }
4108 : :
4109 : : /* Handle scalarized references separately. */
4110 : 214337 : if (ar->type != AR_ELEMENT)
4111 : : {
4112 : 103816 : gfc_conv_scalarized_array_ref (se, ar);
4113 : 103816 : gfc_advance_se_ss_chain (se);
4114 : 103816 : return;
4115 : : }
4116 : :
4117 : 110521 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
4118 : 11409 : var_name = abridged_ref_name (expr, ar);
4119 : :
4120 : 110521 : decl = se->expr;
4121 : 110521 : if (UNLIMITED_POLY(sym)
4122 : 98 : && IS_CLASS_ARRAY (sym)
4123 : 97 : && sym->attr.dummy
4124 : 54 : && ar->as->type != AS_DEFERRED)
4125 : 42 : decl = sym->backend_decl;
4126 : :
4127 : 110521 : cst_offset = offset = gfc_index_zero_node;
4128 : 110521 : add_to_offset (&cst_offset, &offset, gfc_conv_array_offset (decl));
4129 : :
4130 : : /* Calculate the offsets from all the dimensions. Make sure to associate
4131 : : the final offset so that we form a chain of loop invariant summands. */
4132 : 247544 : for (n = ar->dimen - 1; n >= 0; n--)
4133 : : {
4134 : : /* Calculate the index for this dimension. */
4135 : 137023 : gfc_init_se (&indexse, se);
4136 : 137023 : gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
4137 : 137023 : gfc_add_block_to_block (&se->pre, &indexse.pre);
4138 : :
4139 : 137023 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && ! expr->no_bounds_check)
4140 : : {
4141 : : /* Check array bounds. */
4142 : 14911 : tree cond;
4143 : 14911 : char *msg;
4144 : :
4145 : : /* Evaluate the indexse.expr only once. */
4146 : 14911 : indexse.expr = save_expr (indexse.expr);
4147 : :
4148 : : /* Lower bound. */
4149 : 14911 : tmp = gfc_conv_array_lbound (decl, n);
4150 : 14911 : if (sym->attr.temporary)
4151 : : {
4152 : 18 : gfc_init_se (&tmpse, se);
4153 : 18 : gfc_conv_expr_type (&tmpse, ar->as->lower[n],
4154 : : gfc_array_index_type);
4155 : 18 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
4156 : 18 : tmp = tmpse.expr;
4157 : : }
4158 : :
4159 : 14911 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
4160 : : indexse.expr, tmp);
4161 : 14911 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4162 : : "below lower bound of %%ld", n+1, var_name);
4163 : 14911 : gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
4164 : : fold_convert (long_integer_type_node,
4165 : : indexse.expr),
4166 : : fold_convert (long_integer_type_node, tmp));
4167 : 14911 : free (msg);
4168 : :
4169 : : /* Upper bound, but not for the last dimension of assumed-size
4170 : : arrays. */
4171 : 14911 : if (n < ar->dimen - 1 || ar->as->type != AS_ASSUMED_SIZE)
4172 : : {
4173 : 13178 : tmp = gfc_conv_array_ubound (decl, n);
4174 : 13178 : if (sym->attr.temporary)
4175 : : {
4176 : 18 : gfc_init_se (&tmpse, se);
4177 : 18 : gfc_conv_expr_type (&tmpse, ar->as->upper[n],
4178 : : gfc_array_index_type);
4179 : 18 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
4180 : 18 : tmp = tmpse.expr;
4181 : : }
4182 : :
4183 : 13178 : cond = fold_build2_loc (input_location, GT_EXPR,
4184 : : logical_type_node, indexse.expr, tmp);
4185 : 13178 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4186 : : "above upper bound of %%ld", n+1, var_name);
4187 : 13178 : gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
4188 : : fold_convert (long_integer_type_node,
4189 : : indexse.expr),
4190 : : fold_convert (long_integer_type_node, tmp));
4191 : 13178 : free (msg);
4192 : : }
4193 : : }
4194 : :
4195 : : /* Multiply the index by the stride. */
4196 : 137023 : stride = gfc_conv_array_stride (decl, n);
4197 : 137023 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4198 : : indexse.expr, stride);
4199 : :
4200 : : /* And add it to the total. */
4201 : 137023 : add_to_offset (&cst_offset, &offset, tmp);
4202 : : }
4203 : :
4204 : 110521 : if (!integer_zerop (cst_offset))
4205 : 59316 : offset = fold_build2_loc (input_location, PLUS_EXPR,
4206 : : gfc_array_index_type, offset, cst_offset);
4207 : :
4208 : : /* A pointer array component can be detected from its field decl. Fix
4209 : : the descriptor, mark the resulting variable decl and pass it to
4210 : : build_array_ref. */
4211 : 110521 : decl = NULL_TREE;
4212 : 110521 : if (get_CFI_desc (sym, expr, &decl, ar))
4213 : 3589 : decl = build_fold_indirect_ref_loc (input_location, decl);
4214 : 109807 : if (!expr->ts.deferred && !sym->attr.codimension
4215 : 218491 : && is_pointer_array (se->expr))
4216 : : {
4217 : 3744 : if (TREE_CODE (se->expr) == COMPONENT_REF)
4218 : 1102 : decl = se->expr;
4219 : 2642 : else if (INDIRECT_REF_P (se->expr))
4220 : 701 : decl = TREE_OPERAND (se->expr, 0);
4221 : : else
4222 : 1941 : decl = se->expr;
4223 : : }
4224 : 106777 : else if (expr->ts.deferred
4225 : 106063 : || (sym->ts.type == BT_CHARACTER
4226 : 14235 : && sym->attr.select_type_temporary))
4227 : : {
4228 : 2382 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
4229 : : {
4230 : 2346 : decl = se->expr;
4231 : 2346 : if (INDIRECT_REF_P (decl))
4232 : 20 : decl = TREE_OPERAND (decl, 0);
4233 : : }
4234 : : else
4235 : 36 : decl = sym->backend_decl;
4236 : : }
4237 : 104395 : else if (sym->ts.type == BT_CLASS)
4238 : : {
4239 : 2057 : if (UNLIMITED_POLY (sym))
4240 : : {
4241 : 98 : gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
4242 : 98 : gfc_init_se (&tmpse, NULL);
4243 : 98 : gfc_conv_expr (&tmpse, class_expr);
4244 : 98 : if (!se->class_vptr)
4245 : 98 : se->class_vptr = gfc_class_vptr_get (tmpse.expr);
4246 : 98 : gfc_free_expr (class_expr);
4247 : 98 : decl = tmpse.expr;
4248 : 98 : }
4249 : : else
4250 : 1959 : decl = NULL_TREE;
4251 : : }
4252 : :
4253 : 110521 : free (var_name);
4254 : 110521 : se->expr = build_array_ref (se->expr, offset, decl, se->class_vptr);
4255 : : }
4256 : :
4257 : :
4258 : : /* Add the offset corresponding to array's ARRAY_DIM dimension and loop's
4259 : : LOOP_DIM dimension (if any) to array's offset. */
4260 : :
4261 : : static void
4262 : 33580 : add_array_offset (stmtblock_t *pblock, gfc_loopinfo *loop, gfc_ss *ss,
4263 : : gfc_array_ref *ar, int array_dim, int loop_dim)
4264 : : {
4265 : 33580 : gfc_se se;
4266 : 33580 : gfc_array_info *info;
4267 : 33580 : tree stride, index;
4268 : :
4269 : 33580 : info = &ss->info->data.array;
4270 : :
4271 : 33580 : gfc_init_se (&se, NULL);
4272 : 33580 : se.loop = loop;
4273 : 33580 : se.expr = info->descriptor;
4274 : 33580 : stride = gfc_conv_array_stride (info->descriptor, array_dim);
4275 : 33580 : index = conv_array_index_offset (&se, ss, array_dim, loop_dim, ar, stride);
4276 : 33580 : gfc_add_block_to_block (pblock, &se.pre);
4277 : :
4278 : 33580 : info->offset = fold_build2_loc (input_location, PLUS_EXPR,
4279 : : gfc_array_index_type,
4280 : : info->offset, index);
4281 : 33580 : info->offset = gfc_evaluate_now (info->offset, pblock);
4282 : 33580 : }
4283 : :
4284 : :
4285 : : /* Generate the code to be executed immediately before entering a
4286 : : scalarization loop. */
4287 : :
4288 : : static void
4289 : 109113 : gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
4290 : : stmtblock_t * pblock)
4291 : : {
4292 : 109113 : tree stride;
4293 : 109113 : gfc_ss_info *ss_info;
4294 : 109113 : gfc_array_info *info;
4295 : 109113 : gfc_ss_type ss_type;
4296 : 109113 : gfc_ss *ss, *pss;
4297 : 109113 : gfc_loopinfo *ploop;
4298 : 109113 : gfc_array_ref *ar;
4299 : :
4300 : : /* This code will be executed before entering the scalarization loop
4301 : : for this dimension. */
4302 : 333731 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4303 : : {
4304 : 224618 : ss_info = ss->info;
4305 : :
4306 : 224618 : if ((ss_info->useflags & flag) == 0)
4307 : 5646 : continue;
4308 : :
4309 : 218972 : ss_type = ss_info->type;
4310 : 262020 : if (ss_type != GFC_SS_SECTION
4311 : : && ss_type != GFC_SS_FUNCTION
4312 : 218972 : && ss_type != GFC_SS_CONSTRUCTOR
4313 : 218972 : && ss_type != GFC_SS_COMPONENT)
4314 : 43048 : continue;
4315 : :
4316 : 175924 : info = &ss_info->data.array;
4317 : :
4318 : 175924 : gcc_assert (dim < ss->dimen);
4319 : 175924 : gcc_assert (ss->dimen == loop->dimen);
4320 : :
4321 : 175924 : if (info->ref)
4322 : 120410 : ar = &info->ref->u.ar;
4323 : : else
4324 : : ar = NULL;
4325 : :
4326 : 175924 : if (dim == loop->dimen - 1 && loop->parent != NULL)
4327 : : {
4328 : : /* If we are in the outermost dimension of this loop, the previous
4329 : : dimension shall be in the parent loop. */
4330 : 688 : gcc_assert (ss->parent != NULL);
4331 : :
4332 : 688 : pss = ss->parent;
4333 : 688 : ploop = loop->parent;
4334 : :
4335 : : /* ss and ss->parent are about the same array. */
4336 : 688 : gcc_assert (ss_info == pss->info);
4337 : : }
4338 : : else
4339 : : {
4340 : : ploop = loop;
4341 : : pss = ss;
4342 : : }
4343 : :
4344 : 175924 : if (dim == loop->dimen - 1 && loop->parent == NULL)
4345 : : {
4346 : 145155 : gcc_assert (0 == ploop->order[0]);
4347 : :
4348 : 290310 : stride = gfc_conv_array_stride (info->descriptor,
4349 : 145155 : innermost_ss (ss)->dim[0]);
4350 : :
4351 : : /* Calculate the stride of the innermost loop. Hopefully this will
4352 : : allow the backend optimizers to do their stuff more effectively.
4353 : : */
4354 : 145155 : info->stride0 = gfc_evaluate_now (stride, pblock);
4355 : :
4356 : : /* For the outermost loop calculate the offset due to any
4357 : : elemental dimensions. It will have been initialized with the
4358 : : base offset of the array. */
4359 : 145155 : if (info->ref)
4360 : : {
4361 : 221208 : for (int i = 0; i < ar->dimen; i++)
4362 : : {
4363 : 123093 : if (ar->dimen_type[i] != DIMEN_ELEMENT)
4364 : 120282 : continue;
4365 : :
4366 : 2811 : add_array_offset (pblock, loop, ss, ar, i, /* unused */ -1);
4367 : : }
4368 : : }
4369 : : }
4370 : : else
4371 : : {
4372 : 30769 : int i;
4373 : :
4374 : 30769 : if (dim == loop->dimen - 1)
4375 : : i = 0;
4376 : : else
4377 : 30081 : i = dim + 1;
4378 : :
4379 : : /* For the time being, there is no loop reordering. */
4380 : 30769 : gcc_assert (i == ploop->order[i]);
4381 : 30769 : i = ploop->order[i];
4382 : :
4383 : : /* Add the offset for the previous loop dimension. */
4384 : 30769 : add_array_offset (pblock, ploop, ss, ar, pss->dim[i], i);
4385 : : }
4386 : :
4387 : : /* Remember this offset for the second loop. */
4388 : 175924 : if (dim == loop->temp_dim - 1 && loop->parent == NULL)
4389 : 47715 : info->saved_offset = info->offset;
4390 : : }
4391 : 109113 : }
4392 : :
4393 : :
4394 : : /* Start a scalarized expression. Creates a scope and declares loop
4395 : : variables. */
4396 : :
4397 : : void
4398 : 90486 : gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
4399 : : {
4400 : 90486 : int dim;
4401 : 90486 : int n;
4402 : 90486 : int flags;
4403 : :
4404 : 90486 : gcc_assert (!loop->array_parameter);
4405 : :
4406 : 199481 : for (dim = loop->dimen - 1; dim >= 0; dim--)
4407 : : {
4408 : 108995 : n = loop->order[dim];
4409 : :
4410 : 108995 : gfc_start_block (&loop->code[n]);
4411 : :
4412 : : /* Create the loop variable. */
4413 : 108995 : loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
4414 : :
4415 : 108995 : if (dim < loop->temp_dim)
4416 : : flags = 3;
4417 : : else
4418 : 75823 : flags = 1;
4419 : : /* Calculate values that will be constant within this loop. */
4420 : 108995 : gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
4421 : : }
4422 : 90486 : gfc_start_block (pbody);
4423 : 90486 : }
4424 : :
4425 : :
4426 : : /* Generates the actual loop code for a scalarization loop. */
4427 : :
4428 : : static void
4429 : 117462 : gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
4430 : : stmtblock_t * pbody)
4431 : : {
4432 : 117462 : stmtblock_t block;
4433 : 117462 : tree cond;
4434 : 117462 : tree tmp;
4435 : 117462 : tree loopbody;
4436 : 117462 : tree exit_label;
4437 : 117462 : tree stmt;
4438 : 117462 : tree init;
4439 : 117462 : tree incr;
4440 : :
4441 : 117462 : if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS
4442 : : | OMPWS_SCALARIZER_BODY))
4443 : : == (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS)
4444 : 108 : && n == loop->dimen - 1)
4445 : : {
4446 : : /* We create an OMP_FOR construct for the outermost scalarized loop. */
4447 : 80 : init = make_tree_vec (1);
4448 : 80 : cond = make_tree_vec (1);
4449 : 80 : incr = make_tree_vec (1);
4450 : :
4451 : : /* Cycle statement is implemented with a goto. Exit statement must not
4452 : : be present for this loop. */
4453 : 80 : exit_label = gfc_build_label_decl (NULL_TREE);
4454 : 80 : TREE_USED (exit_label) = 1;
4455 : :
4456 : : /* Label for cycle statements (if needed). */
4457 : 80 : tmp = build1_v (LABEL_EXPR, exit_label);
4458 : 80 : gfc_add_expr_to_block (pbody, tmp);
4459 : :
4460 : 80 : stmt = make_node (OMP_FOR);
4461 : :
4462 : 80 : TREE_TYPE (stmt) = void_type_node;
4463 : 80 : OMP_FOR_BODY (stmt) = loopbody = gfc_finish_block (pbody);
4464 : :
4465 : 80 : OMP_FOR_CLAUSES (stmt) = build_omp_clause (input_location,
4466 : : OMP_CLAUSE_SCHEDULE);
4467 : 80 : OMP_CLAUSE_SCHEDULE_KIND (OMP_FOR_CLAUSES (stmt))
4468 : 80 : = OMP_CLAUSE_SCHEDULE_STATIC;
4469 : 80 : if (ompws_flags & OMPWS_NOWAIT)
4470 : 33 : OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (stmt))
4471 : 66 : = build_omp_clause (input_location, OMP_CLAUSE_NOWAIT);
4472 : :
4473 : : /* Initialize the loopvar. */
4474 : 80 : TREE_VEC_ELT (init, 0) = build2_v (MODIFY_EXPR, loop->loopvar[n],
4475 : : loop->from[n]);
4476 : 80 : OMP_FOR_INIT (stmt) = init;
4477 : : /* The exit condition. */
4478 : 80 : TREE_VEC_ELT (cond, 0) = build2_loc (input_location, LE_EXPR,
4479 : : logical_type_node,
4480 : : loop->loopvar[n], loop->to[n]);
4481 : 80 : SET_EXPR_LOCATION (TREE_VEC_ELT (cond, 0), input_location);
4482 : 80 : OMP_FOR_COND (stmt) = cond;
4483 : : /* Increment the loopvar. */
4484 : 80 : tmp = build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4485 : : loop->loopvar[n], gfc_index_one_node);
4486 : 80 : TREE_VEC_ELT (incr, 0) = fold_build2_loc (input_location, MODIFY_EXPR,
4487 : : void_type_node, loop->loopvar[n], tmp);
4488 : 80 : OMP_FOR_INCR (stmt) = incr;
4489 : :
4490 : 80 : ompws_flags &= ~OMPWS_CURR_SINGLEUNIT;
4491 : 80 : gfc_add_expr_to_block (&loop->code[n], stmt);
4492 : : }
4493 : : else
4494 : : {
4495 : 234764 : bool reverse_loop = (loop->reverse[n] == GFC_REVERSE_SET)
4496 : 117382 : && (loop->temp_ss == NULL);
4497 : :
4498 : 117382 : loopbody = gfc_finish_block (pbody);
4499 : :
4500 : 117382 : if (reverse_loop)
4501 : 178 : std::swap (loop->from[n], loop->to[n]);
4502 : :
4503 : : /* Initialize the loopvar. */
4504 : 117382 : if (loop->loopvar[n] != loop->from[n])
4505 : 113775 : gfc_add_modify (&loop->code[n], loop->loopvar[n], loop->from[n]);
4506 : :
4507 : 117382 : exit_label = gfc_build_label_decl (NULL_TREE);
4508 : :
4509 : : /* Generate the loop body. */
4510 : 117382 : gfc_init_block (&block);
4511 : :
4512 : : /* The exit condition. */
4513 : 234586 : cond = fold_build2_loc (input_location, reverse_loop ? LT_EXPR : GT_EXPR,
4514 : : logical_type_node, loop->loopvar[n], loop->to[n]);
4515 : 117382 : tmp = build1_v (GOTO_EXPR, exit_label);
4516 : 117382 : TREE_USED (exit_label) = 1;
4517 : 117382 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
4518 : 117382 : gfc_add_expr_to_block (&block, tmp);
4519 : :
4520 : : /* The main body. */
4521 : 117382 : gfc_add_expr_to_block (&block, loopbody);
4522 : :
4523 : : /* Increment the loopvar. */
4524 : 234586 : tmp = fold_build2_loc (input_location,
4525 : : reverse_loop ? MINUS_EXPR : PLUS_EXPR,
4526 : : gfc_array_index_type, loop->loopvar[n],
4527 : : gfc_index_one_node);
4528 : :
4529 : 117382 : gfc_add_modify (&block, loop->loopvar[n], tmp);
4530 : :
4531 : : /* Build the loop. */
4532 : 117382 : tmp = gfc_finish_block (&block);
4533 : 117382 : tmp = build1_v (LOOP_EXPR, tmp);
4534 : 117382 : gfc_add_expr_to_block (&loop->code[n], tmp);
4535 : :
4536 : : /* Add the exit label. */
4537 : 117382 : tmp = build1_v (LABEL_EXPR, exit_label);
4538 : 117382 : gfc_add_expr_to_block (&loop->code[n], tmp);
4539 : : }
4540 : :
4541 : 117462 : }
4542 : :
4543 : :
4544 : : /* Finishes and generates the loops for a scalarized expression. */
4545 : :
4546 : : void
4547 : 94350 : gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
4548 : : {
4549 : 94350 : int dim;
4550 : 94350 : int n;
4551 : 94350 : gfc_ss *ss;
4552 : 94350 : stmtblock_t *pblock;
4553 : 94350 : tree tmp;
4554 : :
4555 : 94350 : pblock = body;
4556 : : /* Generate the loops. */
4557 : 207195 : for (dim = 0; dim < loop->dimen; dim++)
4558 : : {
4559 : 112845 : n = loop->order[dim];
4560 : 112845 : gfc_trans_scalarized_loop_end (loop, n, pblock);
4561 : 112845 : loop->loopvar[n] = NULL_TREE;
4562 : 112845 : pblock = &loop->code[n];
4563 : : }
4564 : :
4565 : 94350 : tmp = gfc_finish_block (pblock);
4566 : 94350 : gfc_add_expr_to_block (&loop->pre, tmp);
4567 : :
4568 : : /* Clear all the used flags. */
4569 : 281478 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4570 : 187128 : if (ss->parent == NULL)
4571 : 186381 : ss->info->useflags = 0;
4572 : 94350 : }
4573 : :
4574 : :
4575 : : /* Finish the main body of a scalarized expression, and start the secondary
4576 : : copying body. */
4577 : :
4578 : : void
4579 : 4499 : gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
4580 : : {
4581 : 4499 : int dim;
4582 : 4499 : int n;
4583 : 4499 : stmtblock_t *pblock;
4584 : 4499 : gfc_ss *ss;
4585 : :
4586 : 4499 : pblock = body;
4587 : : /* We finish as many loops as are used by the temporary. */
4588 : 4617 : for (dim = 0; dim < loop->temp_dim - 1; dim++)
4589 : : {
4590 : 118 : n = loop->order[dim];
4591 : 118 : gfc_trans_scalarized_loop_end (loop, n, pblock);
4592 : 118 : loop->loopvar[n] = NULL_TREE;
4593 : 118 : pblock = &loop->code[n];
4594 : : }
4595 : :
4596 : : /* We don't want to finish the outermost loop entirely. */
4597 : 4499 : n = loop->order[loop->temp_dim - 1];
4598 : 4499 : gfc_trans_scalarized_loop_end (loop, n, pblock);
4599 : :
4600 : : /* Restore the initial offsets. */
4601 : 16082 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4602 : : {
4603 : 11583 : gfc_ss_type ss_type;
4604 : 11583 : gfc_ss_info *ss_info;
4605 : :
4606 : 11583 : ss_info = ss->info;
4607 : :
4608 : 11583 : if ((ss_info->useflags & 2) == 0)
4609 : 3906 : continue;
4610 : :
4611 : 7677 : ss_type = ss_info->type;
4612 : 7717 : if (ss_type != GFC_SS_SECTION
4613 : : && ss_type != GFC_SS_FUNCTION
4614 : 7677 : && ss_type != GFC_SS_CONSTRUCTOR
4615 : 7677 : && ss_type != GFC_SS_COMPONENT)
4616 : 40 : continue;
4617 : :
4618 : 7637 : ss_info->data.array.offset = ss_info->data.array.saved_offset;
4619 : : }
4620 : :
4621 : : /* Restart all the inner loops we just finished. */
4622 : 4617 : for (dim = loop->temp_dim - 2; dim >= 0; dim--)
4623 : : {
4624 : 118 : n = loop->order[dim];
4625 : :
4626 : 118 : gfc_start_block (&loop->code[n]);
4627 : :
4628 : 118 : loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
4629 : :
4630 : 118 : gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
4631 : : }
4632 : :
4633 : : /* Start a block for the secondary copying code. */
4634 : 4499 : gfc_start_block (body);
4635 : 4499 : }
4636 : :
4637 : :
4638 : : /* Precalculate (either lower or upper) bound of an array section.
4639 : : BLOCK: Block in which the (pre)calculation code will go.
4640 : : BOUNDS[DIM]: Where the bound value will be stored once evaluated.
4641 : : VALUES[DIM]: Specified bound (NULL <=> unspecified).
4642 : : DESC: Array descriptor from which the bound will be picked if unspecified
4643 : : (either lower or upper bound according to LBOUND). */
4644 : :
4645 : : static void
4646 : 401467 : evaluate_bound (stmtblock_t *block, tree *bounds, gfc_expr ** values,
4647 : : tree desc, int dim, bool lbound, bool deferred)
4648 : : {
4649 : 401467 : gfc_se se;
4650 : 401467 : gfc_expr * input_val = values[dim];
4651 : 401467 : tree *output = &bounds[dim];
4652 : :
4653 : :
4654 : 401467 : if (input_val)
4655 : : {
4656 : : /* Specified section bound. */
4657 : 45060 : gfc_init_se (&se, NULL);
4658 : 45060 : gfc_conv_expr_type (&se, input_val, gfc_array_index_type);
4659 : 45060 : gfc_add_block_to_block (block, &se.pre);
4660 : 45060 : *output = se.expr;
4661 : : }
4662 : 356407 : else if (deferred && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
4663 : : {
4664 : : /* The gfc_conv_array_lbound () routine returns a constant zero for
4665 : : deferred length arrays, which in the scalarizer wreaks havoc, when
4666 : : copying to a (newly allocated) one-based array.
4667 : : Keep returning the actual result in sync for both bounds. */
4668 : 110167 : *output = lbound ? gfc_conv_descriptor_lbound_get (desc,
4669 : : gfc_rank_cst[dim]):
4670 : 36789 : gfc_conv_descriptor_ubound_get (desc,
4671 : : gfc_rank_cst[dim]);
4672 : : }
4673 : : else
4674 : : {
4675 : : /* No specific bound specified so use the bound of the array. */
4676 : 421068 : *output = lbound ? gfc_conv_array_lbound (desc, dim) :
4677 : 138039 : gfc_conv_array_ubound (desc, dim);
4678 : : }
4679 : 401467 : *output = gfc_evaluate_now (*output, block);
4680 : 401467 : }
4681 : :
4682 : :
4683 : : /* Calculate the lower bound of an array section. */
4684 : :
4685 : : static void
4686 : 201270 : gfc_conv_section_startstride (stmtblock_t * block, gfc_ss * ss, int dim)
4687 : : {
4688 : 201270 : gfc_expr *stride = NULL;
4689 : 201270 : tree desc;
4690 : 201270 : gfc_se se;
4691 : 201270 : gfc_array_info *info;
4692 : 201270 : gfc_array_ref *ar;
4693 : :
4694 : 201270 : gcc_assert (ss->info->type == GFC_SS_SECTION);
4695 : :
4696 : 201270 : info = &ss->info->data.array;
4697 : 201270 : ar = &info->ref->u.ar;
4698 : :
4699 : 201270 : if (ar->dimen_type[dim] == DIMEN_VECTOR)
4700 : : {
4701 : : /* We use a zero-based index to access the vector. */
4702 : 744 : info->start[dim] = gfc_index_zero_node;
4703 : 744 : info->end[dim] = NULL;
4704 : 744 : info->stride[dim] = gfc_index_one_node;
4705 : 744 : return;
4706 : : }
4707 : :
4708 : 200526 : gcc_assert (ar->dimen_type[dim] == DIMEN_RANGE
4709 : : || ar->dimen_type[dim] == DIMEN_THIS_IMAGE);
4710 : 200526 : desc = info->descriptor;
4711 : 200526 : stride = ar->stride[dim];
4712 : :
4713 : :
4714 : : /* Calculate the start of the range. For vector subscripts this will
4715 : : be the range of the vector. */
4716 : 200526 : evaluate_bound (block, info->start, ar->start, desc, dim, true,
4717 : 200526 : ar->as->type == AS_DEFERRED);
4718 : :
4719 : : /* Similarly calculate the end. Although this is not used in the
4720 : : scalarizer, it is needed when checking bounds and where the end
4721 : : is an expression with side-effects. */
4722 : 200526 : evaluate_bound (block, info->end, ar->end, desc, dim, false,
4723 : 200526 : ar->as->type == AS_DEFERRED);
4724 : :
4725 : :
4726 : : /* Calculate the stride. */
4727 : 200526 : if (stride == NULL)
4728 : 188949 : info->stride[dim] = gfc_index_one_node;
4729 : : else
4730 : : {
4731 : 11577 : gfc_init_se (&se, NULL);
4732 : 11577 : gfc_conv_expr_type (&se, stride, gfc_array_index_type);
4733 : 11577 : gfc_add_block_to_block (block, &se.pre);
4734 : 11577 : info->stride[dim] = gfc_evaluate_now (se.expr, block);
4735 : : }
4736 : : }
4737 : :
4738 : :
4739 : : /* Calculates the range start and stride for a SS chain. Also gets the
4740 : : descriptor and data pointer. The range of vector subscripts is the size
4741 : : of the vector. Array bounds are also checked. */
4742 : :
4743 : : void
4744 : 148891 : gfc_conv_ss_startstride (gfc_loopinfo * loop)
4745 : : {
4746 : 148891 : int n;
4747 : 148891 : tree tmp;
4748 : 148891 : gfc_ss *ss;
4749 : 148891 : tree desc;
4750 : :
4751 : 148891 : gfc_loopinfo * const outer_loop = outermost_loop (loop);
4752 : :
4753 : 148891 : loop->dimen = 0;
4754 : : /* Determine the rank of the loop. */
4755 : 164769 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4756 : : {
4757 : 164769 : switch (ss->info->type)
4758 : : {
4759 : 143965 : case GFC_SS_SECTION:
4760 : 143965 : case GFC_SS_CONSTRUCTOR:
4761 : 143965 : case GFC_SS_FUNCTION:
4762 : 143965 : case GFC_SS_COMPONENT:
4763 : 143965 : loop->dimen = ss->dimen;
4764 : 143965 : goto done;
4765 : :
4766 : : /* As usual, lbound and ubound are exceptions!. */
4767 : 4926 : case GFC_SS_INTRINSIC:
4768 : 4926 : switch (ss->info->expr->value.function.isym->id)
4769 : : {
4770 : 4926 : case GFC_ISYM_LBOUND:
4771 : 4926 : case GFC_ISYM_UBOUND:
4772 : 4926 : case GFC_ISYM_LCOBOUND:
4773 : 4926 : case GFC_ISYM_UCOBOUND:
4774 : 4926 : case GFC_ISYM_SHAPE:
4775 : 4926 : case GFC_ISYM_THIS_IMAGE:
4776 : 4926 : loop->dimen = ss->dimen;
4777 : 4926 : goto done;
4778 : :
4779 : : default:
4780 : : break;
4781 : : }
4782 : :
4783 : 15878 : default:
4784 : 15878 : break;
4785 : : }
4786 : : }
4787 : :
4788 : : /* We should have determined the rank of the expression by now. If
4789 : : not, that's bad news. */
4790 : 0 : gcc_unreachable ();
4791 : :
4792 : : done:
4793 : : /* Loop over all the SS in the chain. */
4794 : 387800 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4795 : : {
4796 : 238909 : gfc_ss_info *ss_info;
4797 : 238909 : gfc_array_info *info;
4798 : 238909 : gfc_expr *expr;
4799 : :
4800 : 238909 : ss_info = ss->info;
4801 : 238909 : expr = ss_info->expr;
4802 : 238909 : info = &ss_info->data.array;
4803 : :
4804 : 238909 : if (expr && expr->shape && !info->shape)
4805 : 141676 : info->shape = expr->shape;
4806 : :
4807 : 238909 : switch (ss_info->type)
4808 : : {
4809 : 151682 : case GFC_SS_SECTION:
4810 : : /* Get the descriptor for the array. If it is a cross loops array,
4811 : : we got the descriptor already in the outermost loop. */
4812 : 151682 : if (ss->parent == NULL)
4813 : 151045 : gfc_conv_ss_descriptor (&outer_loop->pre, ss,
4814 : 151045 : !loop->array_parameter);
4815 : :
4816 : 352424 : for (n = 0; n < ss->dimen; n++)
4817 : 200742 : gfc_conv_section_startstride (&outer_loop->pre, ss, ss->dim[n]);
4818 : : break;
4819 : :
4820 : 5060 : case GFC_SS_INTRINSIC:
4821 : 5060 : switch (expr->value.function.isym->id)
4822 : : {
4823 : : /* Fall through to supply start and stride. */
4824 : 2822 : case GFC_ISYM_LBOUND:
4825 : 2822 : case GFC_ISYM_UBOUND:
4826 : : /* This is the variant without DIM=... */
4827 : 2822 : gcc_assert (expr->value.function.actual->next->expr == NULL);
4828 : : /* Fall through. */
4829 : :
4830 : 4847 : case GFC_ISYM_SHAPE:
4831 : 4847 : {
4832 : 4847 : gfc_expr *arg;
4833 : :
4834 : 4847 : arg = expr->value.function.actual->expr;
4835 : 4847 : if (arg->rank == -1)
4836 : : {
4837 : 1151 : gfc_se se;
4838 : 1151 : tree rank, tmp;
4839 : :
4840 : : /* The rank (hence the return value's shape) is unknown,
4841 : : we have to retrieve it. */
4842 : 1151 : gfc_init_se (&se, NULL);
4843 : 1151 : se.descriptor_only = 1;
4844 : 1151 : gfc_conv_expr (&se, arg);
4845 : : /* This is a bare variable, so there is no preliminary
4846 : : or cleanup code. */
4847 : 1151 : gcc_assert (se.pre.head == NULL_TREE
4848 : : && se.post.head == NULL_TREE);
4849 : 1151 : rank = gfc_conv_descriptor_rank (se.expr);
4850 : 1151 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
4851 : : gfc_array_index_type,
4852 : : fold_convert (gfc_array_index_type,
4853 : : rank),
4854 : : gfc_index_one_node);
4855 : 1151 : info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
4856 : 1151 : info->start[0] = gfc_index_zero_node;
4857 : 1151 : info->stride[0] = gfc_index_one_node;
4858 : 1151 : continue;
4859 : 1151 : }
4860 : : /* Otherwise fall through GFC_SS_FUNCTION. */
4861 : : gcc_fallthrough ();
4862 : : }
4863 : : case GFC_ISYM_LCOBOUND:
4864 : : case GFC_ISYM_UCOBOUND:
4865 : : case GFC_ISYM_THIS_IMAGE:
4866 : : break;
4867 : :
4868 : 0 : default:
4869 : 0 : continue;
4870 : 0 : }
4871 : :
4872 : : /* FALLTHRU */
4873 : : case GFC_SS_CONSTRUCTOR:
4874 : : case GFC_SS_FUNCTION:
4875 : 103255 : for (n = 0; n < ss->dimen; n++)
4876 : : {
4877 : 55107 : int dim = ss->dim[n];
4878 : :
4879 : 55107 : info->start[dim] = gfc_index_zero_node;
4880 : 55107 : info->end[dim] = gfc_index_zero_node;
4881 : 55107 : info->stride[dim] = gfc_index_one_node;
4882 : : }
4883 : : break;
4884 : :
4885 : : default:
4886 : : break;
4887 : : }
4888 : : }
4889 : :
4890 : : /* The rest is just runtime bounds checking. */
4891 : 148891 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
4892 : : {
4893 : 16477 : stmtblock_t block;
4894 : 16477 : tree lbound, ubound;
4895 : 16477 : tree end;
4896 : 16477 : tree size[GFC_MAX_DIMENSIONS];
4897 : 16477 : tree stride_pos, stride_neg, non_zerosized, tmp2, tmp3;
4898 : 16477 : gfc_array_info *info;
4899 : 16477 : char *msg;
4900 : 16477 : int dim;
4901 : :
4902 : 16477 : gfc_start_block (&block);
4903 : :
4904 : 52747 : for (n = 0; n < loop->dimen; n++)
4905 : 19793 : size[n] = NULL_TREE;
4906 : :
4907 : : /* If there is a constructor involved, derive size[] from its shape. */
4908 : 38009 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4909 : : {
4910 : 23918 : gfc_ss_info *ss_info;
4911 : :
4912 : 23918 : ss_info = ss->info;
4913 : 23918 : info = &ss_info->data.array;
4914 : :
4915 : 23918 : if (ss_info->type == GFC_SS_CONSTRUCTOR && info->shape)
4916 : : {
4917 : 5036 : for (n = 0; n < loop->dimen; n++)
4918 : : {
4919 : 2650 : if (size[n] == NULL)
4920 : : {
4921 : 2650 : gcc_assert (info->shape[n]);
4922 : 2650 : size[n] = gfc_conv_mpz_to_tree (info->shape[n],
4923 : : gfc_index_integer_kind);
4924 : : }
4925 : : }
4926 : : break;
4927 : : }
4928 : : }
4929 : :
4930 : 40719 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4931 : : {
4932 : 24242 : stmtblock_t inner;
4933 : 24242 : gfc_ss_info *ss_info;
4934 : 24242 : gfc_expr *expr;
4935 : 24242 : locus *expr_loc;
4936 : 24242 : const char *expr_name;
4937 : 24242 : char *ref_name = NULL;
4938 : :
4939 : 24242 : ss_info = ss->info;
4940 : 24242 : if (ss_info->type != GFC_SS_SECTION)
4941 : 4221 : continue;
4942 : :
4943 : : /* Catch allocatable lhs in f2003. */
4944 : 20353 : if (flag_realloc_lhs && ss->no_bounds_check)
4945 : 332 : continue;
4946 : :
4947 : 20021 : expr = ss_info->expr;
4948 : 20021 : expr_loc = &expr->where;
4949 : 20021 : if (expr->ref)
4950 : 20021 : expr_name = ref_name = abridged_ref_name (expr, NULL);
4951 : : else
4952 : 0 : expr_name = expr->symtree->name;
4953 : :
4954 : 20021 : gfc_start_block (&inner);
4955 : :
4956 : : /* TODO: range checking for mapped dimensions. */
4957 : 20021 : info = &ss_info->data.array;
4958 : :
4959 : : /* This code only checks ranges. Elemental and vector
4960 : : dimensions are checked later. */
4961 : 43268 : for (n = 0; n < loop->dimen; n++)
4962 : : {
4963 : 23247 : bool check_upper;
4964 : :
4965 : 23247 : dim = ss->dim[n];
4966 : 23247 : if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
4967 : 14 : continue;
4968 : :
4969 : 23233 : if (dim == info->ref->u.ar.dimen - 1
4970 : 19908 : && info->ref->u.ar.as->type == AS_ASSUMED_SIZE)
4971 : : check_upper = false;
4972 : : else
4973 : 22943 : check_upper = true;
4974 : :
4975 : : /* Zero stride is not allowed. */
4976 : 23233 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
4977 : : info->stride[dim], gfc_index_zero_node);
4978 : 23233 : msg = xasprintf ("Zero stride is not allowed, for dimension %d "
4979 : : "of array '%s'", dim + 1, expr_name);
4980 : 23233 : gfc_trans_runtime_check (true, false, tmp, &inner,
4981 : : expr_loc, msg);
4982 : 23233 : free (msg);
4983 : :
4984 : 23233 : desc = info->descriptor;
4985 : :
4986 : : /* This is the run-time equivalent of resolve.cc's
4987 : : check_dimension(). The logical is more readable there
4988 : : than it is here, with all the trees. */
4989 : 23233 : lbound = gfc_conv_array_lbound (desc, dim);
4990 : 23233 : end = info->end[dim];
4991 : 23233 : if (check_upper)
4992 : 22943 : ubound = gfc_conv_array_ubound (desc, dim);
4993 : : else
4994 : : ubound = NULL;
4995 : :
4996 : : /* non_zerosized is true when the selected range is not
4997 : : empty. */
4998 : 23233 : stride_pos = fold_build2_loc (input_location, GT_EXPR,
4999 : : logical_type_node, info->stride[dim],
5000 : : gfc_index_zero_node);
5001 : 23233 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
5002 : : info->start[dim], end);
5003 : 23233 : stride_pos = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5004 : : logical_type_node, stride_pos, tmp);
5005 : :
5006 : 23233 : stride_neg = fold_build2_loc (input_location, LT_EXPR,
5007 : : logical_type_node,
5008 : : info->stride[dim], gfc_index_zero_node);
5009 : 23233 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
5010 : : info->start[dim], end);
5011 : 23233 : stride_neg = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5012 : : logical_type_node,
5013 : : stride_neg, tmp);
5014 : 23233 : non_zerosized = fold_build2_loc (input_location, TRUTH_OR_EXPR,
5015 : : logical_type_node,
5016 : : stride_pos, stride_neg);
5017 : :
5018 : : /* Check the start of the range against the lower and upper
5019 : : bounds of the array, if the range is not empty.
5020 : : If upper bound is present, include both bounds in the
5021 : : error message. */
5022 : 23233 : if (check_upper)
5023 : : {
5024 : 22943 : tmp = fold_build2_loc (input_location, LT_EXPR,
5025 : : logical_type_node,
5026 : : info->start[dim], lbound);
5027 : 22943 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5028 : : logical_type_node,
5029 : : non_zerosized, tmp);
5030 : 22943 : tmp2 = fold_build2_loc (input_location, GT_EXPR,
5031 : : logical_type_node,
5032 : : info->start[dim], ubound);
5033 : 22943 : tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5034 : : logical_type_node,
5035 : : non_zerosized, tmp2);
5036 : 22943 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
5037 : : "outside of expected range (%%ld:%%ld)",
5038 : : dim + 1, expr_name);
5039 : 22943 : gfc_trans_runtime_check (true, false, tmp, &inner,
5040 : : expr_loc, msg,
5041 : : fold_convert (long_integer_type_node, info->start[dim]),
5042 : : fold_convert (long_integer_type_node, lbound),
5043 : : fold_convert (long_integer_type_node, ubound));
5044 : 22943 : gfc_trans_runtime_check (true, false, tmp2, &inner,
5045 : : expr_loc, msg,
5046 : : fold_convert (long_integer_type_node, info->start[dim]),
5047 : : fold_convert (long_integer_type_node, lbound),
5048 : : fold_convert (long_integer_type_node, ubound));
5049 : 22943 : free (msg);
5050 : : }
5051 : : else
5052 : : {
5053 : 290 : tmp = fold_build2_loc (input_location, LT_EXPR,
5054 : : logical_type_node,
5055 : : info->start[dim], lbound);
5056 : 290 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5057 : : logical_type_node, non_zerosized, tmp);
5058 : 290 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
5059 : : "below lower bound of %%ld",
5060 : : dim + 1, expr_name);
5061 : 290 : gfc_trans_runtime_check (true, false, tmp, &inner,
5062 : : expr_loc, msg,
5063 : : fold_convert (long_integer_type_node, info->start[dim]),
5064 : : fold_convert (long_integer_type_node, lbound));
5065 : 290 : free (msg);
5066 : : }
5067 : :
5068 : : /* Compute the last element of the range, which is not
5069 : : necessarily "end" (think 0:5:3, which doesn't contain 5)
5070 : : and check it against both lower and upper bounds. */
5071 : :
5072 : 23233 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5073 : : gfc_array_index_type, end,
5074 : : info->start[dim]);
5075 : 23233 : tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
5076 : : gfc_array_index_type, tmp,
5077 : : info->stride[dim]);
5078 : 23233 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5079 : : gfc_array_index_type, end, tmp);
5080 : 23233 : tmp2 = fold_build2_loc (input_location, LT_EXPR,
5081 : : logical_type_node, tmp, lbound);
5082 : 23233 : tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5083 : : logical_type_node, non_zerosized, tmp2);
5084 : 23233 : if (check_upper)
5085 : : {
5086 : 22943 : tmp3 = fold_build2_loc (input_location, GT_EXPR,
5087 : : logical_type_node, tmp, ubound);
5088 : 22943 : tmp3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5089 : : logical_type_node, non_zerosized, tmp3);
5090 : 22943 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
5091 : : "outside of expected range (%%ld:%%ld)",
5092 : : dim + 1, expr_name);
5093 : 22943 : gfc_trans_runtime_check (true, false, tmp2, &inner,
5094 : : expr_loc, msg,
5095 : : fold_convert (long_integer_type_node, tmp),
5096 : : fold_convert (long_integer_type_node, ubound),
5097 : : fold_convert (long_integer_type_node, lbound));
5098 : 22943 : gfc_trans_runtime_check (true, false, tmp3, &inner,
5099 : : expr_loc, msg,
5100 : : fold_convert (long_integer_type_node, tmp),
5101 : : fold_convert (long_integer_type_node, ubound),
5102 : : fold_convert (long_integer_type_node, lbound));
5103 : 22943 : free (msg);
5104 : : }
5105 : : else
5106 : : {
5107 : 290 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
5108 : : "below lower bound of %%ld",
5109 : : dim + 1, expr_name);
5110 : 290 : gfc_trans_runtime_check (true, false, tmp2, &inner,
5111 : : expr_loc, msg,
5112 : : fold_convert (long_integer_type_node, tmp),
5113 : : fold_convert (long_integer_type_node, lbound));
5114 : 290 : free (msg);
5115 : : }
5116 : :
5117 : : /* Check the section sizes match. */
5118 : 23233 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5119 : : gfc_array_index_type, end,
5120 : : info->start[dim]);
5121 : 23233 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
5122 : : gfc_array_index_type, tmp,
5123 : : info->stride[dim]);
5124 : 23233 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5125 : : gfc_array_index_type,
5126 : : gfc_index_one_node, tmp);
5127 : 23233 : tmp = fold_build2_loc (input_location, MAX_EXPR,
5128 : : gfc_array_index_type, tmp,
5129 : 23233 : build_int_cst (gfc_array_index_type, 0));
5130 : : /* We remember the size of the first section, and check all the
5131 : : others against this. */
5132 : 23233 : if (size[n])
5133 : : {
5134 : 6969 : tmp3 = fold_build2_loc (input_location, NE_EXPR,
5135 : : logical_type_node, tmp, size[n]);
5136 : 6969 : msg = xasprintf ("Array bound mismatch for dimension %d "
5137 : : "of array '%s' (%%ld/%%ld)",
5138 : : dim + 1, expr_name);
5139 : :
5140 : 6969 : gfc_trans_runtime_check (true, false, tmp3, &inner,
5141 : : expr_loc, msg,
5142 : : fold_convert (long_integer_type_node, tmp),
5143 : : fold_convert (long_integer_type_node, size[n]));
5144 : :
5145 : 6969 : free (msg);
5146 : : }
5147 : : else
5148 : 16264 : size[n] = gfc_evaluate_now (tmp, &inner);
5149 : : }
5150 : :
5151 : 20021 : tmp = gfc_finish_block (&inner);
5152 : :
5153 : : /* For optional arguments, only check bounds if the argument is
5154 : : present. */
5155 : 20021 : if ((expr->symtree->n.sym->attr.optional
5156 : 20021 : || expr->symtree->n.sym->attr.not_always_present)
5157 : 296 : && expr->symtree->n.sym->attr.dummy)
5158 : 295 : tmp = build3_v (COND_EXPR,
5159 : : gfc_conv_expr_present (expr->symtree->n.sym),
5160 : : tmp, build_empty_stmt (input_location));
5161 : :
5162 : 20021 : gfc_add_expr_to_block (&block, tmp);
5163 : :
5164 : 20021 : free (ref_name);
5165 : : }
5166 : :
5167 : 16477 : tmp = gfc_finish_block (&block);
5168 : 16477 : gfc_add_expr_to_block (&outer_loop->pre, tmp);
5169 : : }
5170 : :
5171 : 149444 : for (loop = loop->nested; loop; loop = loop->next)
5172 : 553 : gfc_conv_ss_startstride (loop);
5173 : 148891 : }
5174 : :
5175 : : /* Return true if both symbols could refer to the same data object. Does
5176 : : not take account of aliasing due to equivalence statements. */
5177 : :
5178 : : static bool
5179 : 9360 : symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym, bool lsym_pointer,
5180 : : bool lsym_target, bool rsym_pointer, bool rsym_target)
5181 : : {
5182 : : /* Aliasing isn't possible if the symbols have different base types. */
5183 : 9360 : if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
5184 : : return 0;
5185 : :
5186 : : /* Pointers can point to other pointers and target objects. */
5187 : :
5188 : 7419 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5189 : 7235 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5190 : : return 1;
5191 : :
5192 : : /* Special case: Argument association, cf. F90 12.4.1.6, F2003 12.4.1.7
5193 : : and F2008 12.5.2.13 items 3b and 4b. The pointer case (a) is already
5194 : : checked above. */
5195 : 7327 : if (lsym_target && rsym_target
5196 : 14 : && ((lsym->attr.dummy && !lsym->attr.contiguous
5197 : 0 : && (!lsym->attr.dimension || lsym->as->type == AS_ASSUMED_SHAPE))
5198 : 14 : || (rsym->attr.dummy && !rsym->attr.contiguous
5199 : 6 : && (!rsym->attr.dimension
5200 : 6 : || rsym->as->type == AS_ASSUMED_SHAPE))))
5201 : 6 : return 1;
5202 : :
5203 : : return 0;
5204 : : }
5205 : :
5206 : :
5207 : : /* Return true if the two SS could be aliased, i.e. both point to the same data
5208 : : object. */
5209 : : /* TODO: resolve aliases based on frontend expressions. */
5210 : :
5211 : : static int
5212 : 8102 : gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
5213 : : {
5214 : 8102 : gfc_ref *lref;
5215 : 8102 : gfc_ref *rref;
5216 : 8102 : gfc_expr *lexpr, *rexpr;
5217 : 8102 : gfc_symbol *lsym;
5218 : 8102 : gfc_symbol *rsym;
5219 : 8102 : bool lsym_pointer, lsym_target, rsym_pointer, rsym_target;
5220 : :
5221 : 8102 : lexpr = lss->info->expr;
5222 : 8102 : rexpr = rss->info->expr;
5223 : :
5224 : 8102 : lsym = lexpr->symtree->n.sym;
5225 : 8102 : rsym = rexpr->symtree->n.sym;
5226 : :
5227 : 8102 : lsym_pointer = lsym->attr.pointer;
5228 : 8102 : lsym_target = lsym->attr.target;
5229 : 8102 : rsym_pointer = rsym->attr.pointer;
5230 : 8102 : rsym_target = rsym->attr.target;
5231 : :
5232 : 8102 : if (symbols_could_alias (lsym, rsym, lsym_pointer, lsym_target,
5233 : : rsym_pointer, rsym_target))
5234 : : return 1;
5235 : :
5236 : 8036 : if (rsym->ts.type != BT_DERIVED && rsym->ts.type != BT_CLASS
5237 : 7315 : && lsym->ts.type != BT_DERIVED && lsym->ts.type != BT_CLASS)
5238 : : return 0;
5239 : :
5240 : : /* For derived types we must check all the component types. We can ignore
5241 : : array references as these will have the same base type as the previous
5242 : : component ref. */
5243 : 1535 : for (lref = lexpr->ref; lref != lss->info->data.array.ref; lref = lref->next)
5244 : : {
5245 : 584 : if (lref->type != REF_COMPONENT)
5246 : 65 : continue;
5247 : :
5248 : 519 : lsym_pointer = lsym_pointer || lref->u.c.sym->attr.pointer;
5249 : 519 : lsym_target = lsym_target || lref->u.c.sym->attr.target;
5250 : :
5251 : 519 : if (symbols_could_alias (lref->u.c.sym, rsym, lsym_pointer, lsym_target,
5252 : : rsym_pointer, rsym_target))
5253 : : return 1;
5254 : :
5255 : 519 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5256 : 504 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5257 : : {
5258 : 6 : if (gfc_compare_types (&lref->u.c.component->ts,
5259 : : &rsym->ts))
5260 : : return 1;
5261 : : }
5262 : :
5263 : 888 : for (rref = rexpr->ref; rref != rss->info->data.array.ref;
5264 : 375 : rref = rref->next)
5265 : : {
5266 : 376 : if (rref->type != REF_COMPONENT)
5267 : 36 : continue;
5268 : :
5269 : 340 : rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
5270 : 340 : rsym_target = lsym_target || rref->u.c.sym->attr.target;
5271 : :
5272 : 340 : if (symbols_could_alias (lref->u.c.sym, rref->u.c.sym,
5273 : : lsym_pointer, lsym_target,
5274 : : rsym_pointer, rsym_target))
5275 : : return 1;
5276 : :
5277 : 339 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5278 : 335 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5279 : : {
5280 : 0 : if (gfc_compare_types (&lref->u.c.component->ts,
5281 : 0 : &rref->u.c.sym->ts))
5282 : : return 1;
5283 : 0 : if (gfc_compare_types (&lref->u.c.sym->ts,
5284 : 0 : &rref->u.c.component->ts))
5285 : : return 1;
5286 : 0 : if (gfc_compare_types (&lref->u.c.component->ts,
5287 : 0 : &rref->u.c.component->ts))
5288 : : return 1;
5289 : : }
5290 : : }
5291 : : }
5292 : :
5293 : 951 : lsym_pointer = lsym->attr.pointer;
5294 : 951 : lsym_target = lsym->attr.target;
5295 : :
5296 : 1344 : for (rref = rexpr->ref; rref != rss->info->data.array.ref; rref = rref->next)
5297 : : {
5298 : 474 : if (rref->type != REF_COMPONENT)
5299 : : break;
5300 : :
5301 : 399 : rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
5302 : 399 : rsym_target = lsym_target || rref->u.c.sym->attr.target;
5303 : :
5304 : 399 : if (symbols_could_alias (rref->u.c.sym, lsym,
5305 : : lsym_pointer, lsym_target,
5306 : : rsym_pointer, rsym_target))
5307 : : return 1;
5308 : :
5309 : 399 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5310 : 381 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5311 : : {
5312 : 6 : if (gfc_compare_types (&lsym->ts, &rref->u.c.component->ts))
5313 : : return 1;
5314 : : }
5315 : : }
5316 : :
5317 : : return 0;
5318 : : }
5319 : :
5320 : :
5321 : : /* Resolve array data dependencies. Creates a temporary if required. */
5322 : : /* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
5323 : : dependency.cc. */
5324 : :
5325 : : void
5326 : 31424 : gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
5327 : : gfc_ss * rss)
5328 : : {
5329 : 31424 : gfc_ss *ss;
5330 : 31424 : gfc_ref *lref;
5331 : 31424 : gfc_ref *rref;
5332 : 31424 : gfc_ss_info *ss_info;
5333 : 31424 : gfc_expr *dest_expr;
5334 : 31424 : gfc_expr *ss_expr;
5335 : 31424 : int nDepend = 0;
5336 : 31424 : int i, j;
5337 : :
5338 : 31424 : loop->temp_ss = NULL;
5339 : 31424 : dest_expr = dest->info->expr;
5340 : :
5341 : 65904 : for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
5342 : : {
5343 : 35372 : ss_info = ss->info;
5344 : 35372 : ss_expr = ss_info->expr;
5345 : :
5346 : 35372 : if (ss_info->array_outer_dependency)
5347 : : {
5348 : : nDepend = 1;
5349 : : break;
5350 : : }
5351 : :
5352 : 35262 : if (ss_info->type != GFC_SS_SECTION)
5353 : : {
5354 : 24877 : if (flag_realloc_lhs
5355 : 24069 : && dest_expr != ss_expr
5356 : 24069 : && gfc_is_reallocatable_lhs (dest_expr)
5357 : 28751 : && ss_expr->rank)
5358 : 2050 : nDepend = gfc_check_dependency (dest_expr, ss_expr, true);
5359 : :
5360 : : /* Check for cases like c(:)(1:2) = c(2)(2:3) */
5361 : 24877 : if (!nDepend && dest_expr->rank > 0
5362 : 24541 : && dest_expr->ts.type == BT_CHARACTER
5363 : 4094 : && ss_expr->expr_type == EXPR_VARIABLE)
5364 : :
5365 : 169 : nDepend = gfc_check_dependency (dest_expr, ss_expr, false);
5366 : :
5367 : 24877 : if (ss_info->type == GFC_SS_REFERENCE
5368 : 24877 : && gfc_check_dependency (dest_expr, ss_expr, false))
5369 : 182 : ss_info->data.scalar.needs_temporary = 1;
5370 : :
5371 : 24877 : if (nDepend)
5372 : : break;
5373 : : else
5374 : 24529 : continue;
5375 : : }
5376 : :
5377 : 10385 : if (dest_expr->symtree->n.sym != ss_expr->symtree->n.sym)
5378 : : {
5379 : 8102 : if (gfc_could_be_alias (dest, ss)
5380 : 8102 : || gfc_are_equivalenced_arrays (dest_expr, ss_expr))
5381 : : {
5382 : : nDepend = 1;
5383 : : break;
5384 : : }
5385 : : }
5386 : : else
5387 : : {
5388 : 2283 : lref = dest_expr->ref;
5389 : 2283 : rref = ss_expr->ref;
5390 : :
5391 : 2283 : nDepend = gfc_dep_resolver (lref, rref, &loop->reverse[0]);
5392 : :
5393 : 2283 : if (nDepend == 1)
5394 : : break;
5395 : :
5396 : 4589 : for (i = 0; i < dest->dimen; i++)
5397 : 6236 : for (j = 0; j < ss->dimen; j++)
5398 : 3716 : if (i != j
5399 : 1133 : && dest->dim[i] == ss->dim[j])
5400 : : {
5401 : : /* If we don't access array elements in the same order,
5402 : : there is a dependency. */
5403 : 63 : nDepend = 1;
5404 : 63 : goto temporary;
5405 : : }
5406 : : #if 0
5407 : : /* TODO : loop shifting. */
5408 : : if (nDepend == 1)
5409 : : {
5410 : : /* Mark the dimensions for LOOP SHIFTING */
5411 : : for (n = 0; n < loop->dimen; n++)
5412 : : {
5413 : : int dim = dest->data.info.dim[n];
5414 : :
5415 : : if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
5416 : : depends[n] = 2;
5417 : : else if (! gfc_is_same_range (&lref->u.ar,
5418 : : &rref->u.ar, dim, 0))
5419 : : depends[n] = 1;
5420 : : }
5421 : :
5422 : : /* Put all the dimensions with dependencies in the
5423 : : innermost loops. */
5424 : : dim = 0;
5425 : : for (n = 0; n < loop->dimen; n++)
5426 : : {
5427 : : gcc_assert (loop->order[n] == n);
5428 : : if (depends[n])
5429 : : loop->order[dim++] = n;
5430 : : }
5431 : : for (n = 0; n < loop->dimen; n++)
5432 : : {
5433 : : if (! depends[n])
5434 : : loop->order[dim++] = n;
5435 : : }
5436 : :
5437 : : gcc_assert (dim == loop->dimen);
5438 : : break;
5439 : : }
5440 : : #endif
5441 : : }
5442 : : }
5443 : :
5444 : 562 : temporary:
5445 : :
5446 : 31424 : if (nDepend == 1)
5447 : : {
5448 : 892 : tree base_type = gfc_typenode_for_spec (&dest_expr->ts);
5449 : 892 : if (GFC_ARRAY_TYPE_P (base_type)
5450 : 892 : || GFC_DESCRIPTOR_TYPE_P (base_type))
5451 : 0 : base_type = gfc_get_element_type (base_type);
5452 : 892 : loop->temp_ss = gfc_get_temp_ss (base_type, dest->info->string_length,
5453 : : loop->dimen);
5454 : 892 : gfc_add_ss_to_loop (loop, loop->temp_ss);
5455 : : }
5456 : : else
5457 : 30532 : loop->temp_ss = NULL;
5458 : 31424 : }
5459 : :
5460 : :
5461 : : /* Browse through each array's information from the scalarizer and set the loop
5462 : : bounds according to the "best" one (per dimension), i.e. the one which
5463 : : provides the most information (constant bounds, shape, etc.). */
5464 : :
5465 : : static void
5466 : 148891 : set_loop_bounds (gfc_loopinfo *loop)
5467 : : {
5468 : 148891 : int n, dim, spec_dim;
5469 : 148891 : gfc_array_info *info;
5470 : 148891 : gfc_array_info *specinfo;
5471 : 148891 : gfc_ss *ss;
5472 : 148891 : tree tmp;
5473 : 148891 : gfc_ss **loopspec;
5474 : 148891 : bool dynamic[GFC_MAX_DIMENSIONS];
5475 : 148891 : mpz_t *cshape;
5476 : 148891 : mpz_t i;
5477 : 148891 : bool nonoptional_arr;
5478 : :
5479 : 148891 : gfc_loopinfo * const outer_loop = outermost_loop (loop);
5480 : :
5481 : 148891 : loopspec = loop->specloop;
5482 : :
5483 : 148891 : mpz_init (i);
5484 : 344784 : for (n = 0; n < loop->dimen; n++)
5485 : : {
5486 : 195893 : loopspec[n] = NULL;
5487 : 195893 : dynamic[n] = false;
5488 : :
5489 : : /* If there are both optional and nonoptional array arguments, scalarize
5490 : : over the nonoptional; otherwise, it does not matter as then all
5491 : : (optional) arrays have to be present per F2008, 125.2.12p3(6). */
5492 : :
5493 : 195893 : nonoptional_arr = false;
5494 : :
5495 : 226617 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5496 : 226597 : if (ss->info->type != GFC_SS_SCALAR && ss->info->type != GFC_SS_TEMP
5497 : 196900 : && ss->info->type != GFC_SS_REFERENCE && !ss->info->can_be_null_ref)
5498 : : {
5499 : : nonoptional_arr = true;
5500 : : break;
5501 : : }
5502 : :
5503 : : /* We use one SS term, and use that to determine the bounds of the
5504 : : loop for this dimension. We try to pick the simplest term. */
5505 : 509029 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5506 : : {
5507 : 313136 : gfc_ss_type ss_type;
5508 : :
5509 : 313136 : ss_type = ss->info->type;
5510 : 368495 : if (ss_type == GFC_SS_SCALAR
5511 : 313136 : || ss_type == GFC_SS_TEMP
5512 : 259959 : || ss_type == GFC_SS_REFERENCE
5513 : 257996 : || (ss->info->can_be_null_ref && nonoptional_arr))
5514 : 55359 : continue;
5515 : :
5516 : 257777 : info = &ss->info->data.array;
5517 : 257777 : dim = ss->dim[n];
5518 : :
5519 : 257777 : if (loopspec[n] != NULL)
5520 : : {
5521 : 61884 : specinfo = &loopspec[n]->info->data.array;
5522 : 61884 : spec_dim = loopspec[n]->dim[n];
5523 : : }
5524 : : else
5525 : : {
5526 : : /* Silence uninitialized warnings. */
5527 : : specinfo = NULL;
5528 : : spec_dim = 0;
5529 : : }
5530 : :
5531 : 257777 : if (info->shape)
5532 : : {
5533 : : /* The frontend has worked out the size for us. */
5534 : 180983 : if (!loopspec[n]
5535 : 47277 : || !specinfo->shape
5536 : 219987 : || !integer_zerop (specinfo->start[spec_dim]))
5537 : : /* Prefer zero-based descriptors if possible. */
5538 : 166835 : loopspec[n] = ss;
5539 : 180983 : continue;
5540 : : }
5541 : :
5542 : 76794 : if (ss_type == GFC_SS_CONSTRUCTOR)
5543 : : {
5544 : 1253 : gfc_constructor_base base;
5545 : : /* An unknown size constructor will always be rank one.
5546 : : Higher rank constructors will either have known shape,
5547 : : or still be wrapped in a call to reshape. */
5548 : 1253 : gcc_assert (loop->dimen == 1);
5549 : :
5550 : : /* Always prefer to use the constructor bounds if the size
5551 : : can be determined at compile time. Prefer not to otherwise,
5552 : : since the general case involves realloc, and it's better to
5553 : : avoid that overhead if possible. */
5554 : 1253 : base = ss->info->expr->value.constructor;
5555 : 1253 : dynamic[n] = gfc_get_array_constructor_size (&i, base);
5556 : 1253 : if (!dynamic[n] || !loopspec[n])
5557 : 1068 : loopspec[n] = ss;
5558 : 1253 : continue;
5559 : 1253 : }
5560 : :
5561 : : /* Avoid using an allocatable lhs in an assignment, since
5562 : : there might be a reallocation coming. */
5563 : 75541 : if (loopspec[n] && ss->is_alloc_lhs)
5564 : 5278 : continue;
5565 : :
5566 : 70263 : if (!loopspec[n])
5567 : 61119 : loopspec[n] = ss;
5568 : : /* Criteria for choosing a loop specifier (most important first):
5569 : : doesn't need realloc
5570 : : stride of one
5571 : : known stride
5572 : : known lower bound
5573 : : known upper bound
5574 : : */
5575 : 9144 : else if (loopspec[n]->info->type == GFC_SS_CONSTRUCTOR && dynamic[n])
5576 : 170 : loopspec[n] = ss;
5577 : 8974 : else if (integer_onep (info->stride[dim])
5578 : 8974 : && !integer_onep (specinfo->stride[spec_dim]))
5579 : 78 : loopspec[n] = ss;
5580 : 8896 : else if (INTEGER_CST_P (info->stride[dim])
5581 : 8726 : && !INTEGER_CST_P (specinfo->stride[spec_dim]))
5582 : 0 : loopspec[n] = ss;
5583 : 8896 : else if (INTEGER_CST_P (info->start[dim])
5584 : 3948 : && !INTEGER_CST_P (specinfo->start[spec_dim])
5585 : 538 : && integer_onep (info->stride[dim])
5586 : 269 : == integer_onep (specinfo->stride[spec_dim])
5587 : 8896 : && INTEGER_CST_P (info->stride[dim])
5588 : 242 : == INTEGER_CST_P (specinfo->stride[spec_dim]))
5589 : 242 : loopspec[n] = ss;
5590 : : /* We don't work out the upper bound.
5591 : : else if (INTEGER_CST_P (info->finish[n])
5592 : : && ! INTEGER_CST_P (specinfo->finish[n]))
5593 : : loopspec[n] = ss; */
5594 : : }
5595 : :
5596 : : /* We should have found the scalarization loop specifier. If not,
5597 : : that's bad news. */
5598 : 195893 : gcc_assert (loopspec[n]);
5599 : :
5600 : 195893 : info = &loopspec[n]->info->data.array;
5601 : 195893 : dim = loopspec[n]->dim[n];
5602 : :
5603 : : /* Set the extents of this range. */
5604 : 195893 : cshape = info->shape;
5605 : 195893 : if (cshape && INTEGER_CST_P (info->start[dim])
5606 : 141684 : && INTEGER_CST_P (info->stride[dim]))
5607 : : {
5608 : 141684 : loop->from[n] = info->start[dim];
5609 : 141684 : mpz_set (i, cshape[get_array_ref_dim_for_loop_dim (loopspec[n], n)]);
5610 : 141684 : mpz_sub_ui (i, i, 1);
5611 : : /* To = from + (size - 1) * stride. */
5612 : 141684 : tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
5613 : 141684 : if (!integer_onep (info->stride[dim]))
5614 : 8355 : tmp = fold_build2_loc (input_location, MULT_EXPR,
5615 : : gfc_array_index_type, tmp,
5616 : : info->stride[dim]);
5617 : 141684 : loop->to[n] = fold_build2_loc (input_location, PLUS_EXPR,
5618 : : gfc_array_index_type,
5619 : : loop->from[n], tmp);
5620 : : }
5621 : : else
5622 : : {
5623 : 54209 : loop->from[n] = info->start[dim];
5624 : 54209 : switch (loopspec[n]->info->type)
5625 : : {
5626 : 799 : case GFC_SS_CONSTRUCTOR:
5627 : : /* The upper bound is calculated when we expand the
5628 : : constructor. */
5629 : 799 : gcc_assert (loop->to[n] == NULL_TREE);
5630 : : break;
5631 : :
5632 : 48584 : case GFC_SS_SECTION:
5633 : : /* Use the end expression if it exists and is not constant,
5634 : : so that it is only evaluated once. */
5635 : 48584 : loop->to[n] = info->end[dim];
5636 : 48584 : break;
5637 : :
5638 : 4071 : case GFC_SS_FUNCTION:
5639 : : /* The loop bound will be set when we generate the call. */
5640 : 4071 : gcc_assert (loop->to[n] == NULL_TREE);
5641 : : break;
5642 : :
5643 : 749 : case GFC_SS_INTRINSIC:
5644 : 749 : {
5645 : 749 : gfc_expr *expr = loopspec[n]->info->expr;
5646 : :
5647 : : /* The {l,u}bound of an assumed rank. */
5648 : 749 : if (expr->value.function.isym->id == GFC_ISYM_SHAPE)
5649 : 237 : gcc_assert (expr->value.function.actual->expr->rank == -1);
5650 : : else
5651 : 512 : gcc_assert ((expr->value.function.isym->id == GFC_ISYM_LBOUND
5652 : : || expr->value.function.isym->id == GFC_ISYM_UBOUND)
5653 : : && expr->value.function.actual->next->expr == NULL
5654 : : && expr->value.function.actual->expr->rank == -1);
5655 : :
5656 : 749 : loop->to[n] = info->end[dim];
5657 : 749 : break;
5658 : : }
5659 : :
5660 : 6 : case GFC_SS_COMPONENT:
5661 : 6 : {
5662 : 6 : if (info->end[dim] != NULL_TREE)
5663 : : {
5664 : 6 : loop->to[n] = info->end[dim];
5665 : 6 : break;
5666 : : }
5667 : : else
5668 : 0 : gcc_unreachable ();
5669 : : }
5670 : :
5671 : 0 : default:
5672 : 0 : gcc_unreachable ();
5673 : : }
5674 : : }
5675 : :
5676 : : /* Transform everything so we have a simple incrementing variable. */
5677 : 195893 : if (integer_onep (info->stride[dim]))
5678 : 185824 : info->delta[dim] = gfc_index_zero_node;
5679 : : else
5680 : : {
5681 : : /* Set the delta for this section. */
5682 : 10069 : info->delta[dim] = gfc_evaluate_now (loop->from[n], &outer_loop->pre);
5683 : : /* Number of iterations is (end - start + step) / step.
5684 : : with start = 0, this simplifies to
5685 : : last = end / step;
5686 : : for (i = 0; i<=last; i++){...}; */
5687 : 10069 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5688 : : gfc_array_index_type, loop->to[n],
5689 : : loop->from[n]);
5690 : 10069 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
5691 : : gfc_array_index_type, tmp, info->stride[dim]);
5692 : 10069 : tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
5693 : 10069 : tmp, build_int_cst (gfc_array_index_type, -1));
5694 : 10069 : loop->to[n] = gfc_evaluate_now (tmp, &outer_loop->pre);
5695 : : /* Make the loop variable start at 0. */
5696 : 10069 : loop->from[n] = gfc_index_zero_node;
5697 : : }
5698 : : }
5699 : 148891 : mpz_clear (i);
5700 : :
5701 : 149444 : for (loop = loop->nested; loop; loop = loop->next)
5702 : 553 : set_loop_bounds (loop);
5703 : 148891 : }
5704 : :
5705 : :
5706 : : /* Initialize the scalarization loop. Creates the loop variables. Determines
5707 : : the range of the loop variables. Creates a temporary if required.
5708 : : Also generates code for scalar expressions which have been
5709 : : moved outside the loop. */
5710 : :
5711 : : void
5712 : 148338 : gfc_conv_loop_setup (gfc_loopinfo * loop, locus * where)
5713 : : {
5714 : 148338 : gfc_ss *tmp_ss;
5715 : 148338 : tree tmp;
5716 : :
5717 : 148338 : set_loop_bounds (loop);
5718 : :
5719 : : /* Add all the scalar code that can be taken out of the loops.
5720 : : This may include calculating the loop bounds, so do it before
5721 : : allocating the temporary. */
5722 : 148338 : gfc_add_loop_ss_code (loop, loop->ss, false, where);
5723 : :
5724 : 148338 : tmp_ss = loop->temp_ss;
5725 : : /* If we want a temporary then create it. */
5726 : 148338 : if (tmp_ss != NULL)
5727 : : {
5728 : 8637 : gfc_ss_info *tmp_ss_info;
5729 : :
5730 : 8637 : tmp_ss_info = tmp_ss->info;
5731 : 8637 : gcc_assert (tmp_ss_info->type == GFC_SS_TEMP);
5732 : 8637 : gcc_assert (loop->parent == NULL);
5733 : :
5734 : : /* Make absolutely sure that this is a complete type. */
5735 : 8637 : if (tmp_ss_info->string_length)
5736 : 2571 : tmp_ss_info->data.temp.type
5737 : 2571 : = gfc_get_character_type_len_for_eltype
5738 : 2571 : (TREE_TYPE (tmp_ss_info->data.temp.type),
5739 : : tmp_ss_info->string_length);
5740 : :
5741 : 8637 : tmp = tmp_ss_info->data.temp.type;
5742 : 8637 : memset (&tmp_ss_info->data.array, 0, sizeof (gfc_array_info));
5743 : 8637 : tmp_ss_info->type = GFC_SS_SECTION;
5744 : :
5745 : 8637 : gcc_assert (tmp_ss->dimen != 0);
5746 : :
5747 : 8637 : gfc_trans_create_temp_array (&loop->pre, &loop->post, tmp_ss, tmp,
5748 : : NULL_TREE, false, true, false, where);
5749 : : }
5750 : :
5751 : : /* For array parameters we don't have loop variables, so don't calculate the
5752 : : translations. */
5753 : 148338 : if (!loop->array_parameter)
5754 : 90084 : gfc_set_delta (loop);
5755 : 148338 : }
5756 : :
5757 : :
5758 : : /* Calculates how to transform from loop variables to array indices for each
5759 : : array: once loop bounds are chosen, sets the difference (DELTA field) between
5760 : : loop bounds and array reference bounds, for each array info. */
5761 : :
5762 : : void
5763 : 90816 : gfc_set_delta (gfc_loopinfo *loop)
5764 : : {
5765 : 90816 : gfc_ss *ss, **loopspec;
5766 : 90816 : gfc_array_info *info;
5767 : 90816 : tree tmp;
5768 : 90816 : int n, dim;
5769 : :
5770 : 90816 : gfc_loopinfo * const outer_loop = outermost_loop (loop);
5771 : :
5772 : 90816 : loopspec = loop->specloop;
5773 : :
5774 : : /* Calculate the translation from loop variables to array indices. */
5775 : 278820 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5776 : : {
5777 : 188004 : gfc_ss_type ss_type;
5778 : :
5779 : 188004 : ss_type = ss->info->type;
5780 : 188004 : if (ss_type != GFC_SS_SECTION
5781 : 188004 : && ss_type != GFC_SS_COMPONENT
5782 : 78434 : && ss_type != GFC_SS_CONSTRUCTOR)
5783 : 48099 : continue;
5784 : :
5785 : 307087 : info = &ss->info->data.array;
5786 : :
5787 : 307087 : for (n = 0; n < ss->dimen; n++)
5788 : : {
5789 : : /* If we are specifying the range the delta is already set. */
5790 : 167182 : if (loopspec[n] != ss)
5791 : : {
5792 : 87984 : dim = ss->dim[n];
5793 : :
5794 : : /* Calculate the offset relative to the loop variable.
5795 : : First multiply by the stride. */
5796 : 87984 : tmp = loop->from[n];
5797 : 87984 : if (!integer_onep (info->stride[dim]))
5798 : 2439 : tmp = fold_build2_loc (input_location, MULT_EXPR,
5799 : : gfc_array_index_type,
5800 : : tmp, info->stride[dim]);
5801 : :
5802 : : /* Then subtract this from our starting value. */
5803 : 87984 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5804 : : gfc_array_index_type,
5805 : : info->start[dim], tmp);
5806 : :
5807 : 87984 : info->delta[dim] = gfc_evaluate_now (tmp, &outer_loop->pre);
5808 : : }
5809 : : }
5810 : : }
5811 : :
5812 : 91393 : for (loop = loop->nested; loop; loop = loop->next)
5813 : 577 : gfc_set_delta (loop);
5814 : 90816 : }
5815 : :
5816 : :
5817 : : /* Calculate the size of a given array dimension from the bounds. This
5818 : : is simply (ubound - lbound + 1) if this expression is positive
5819 : : or 0 if it is negative (pick either one if it is zero). Optionally
5820 : : (if or_expr is present) OR the (expression != 0) condition to it. */
5821 : :
5822 : : tree
5823 : 18454 : gfc_conv_array_extent_dim (tree lbound, tree ubound, tree* or_expr)
5824 : : {
5825 : 18454 : tree res;
5826 : 18454 : tree cond;
5827 : :
5828 : : /* Calculate (ubound - lbound + 1). */
5829 : 18454 : res = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5830 : : ubound, lbound);
5831 : 18454 : res = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, res,
5832 : : gfc_index_one_node);
5833 : :
5834 : : /* Check whether the size for this dimension is negative. */
5835 : 18454 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, res,
5836 : : gfc_index_zero_node);
5837 : 18454 : res = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
5838 : : gfc_index_zero_node, res);
5839 : :
5840 : : /* Build OR expression. */
5841 : 18454 : if (or_expr)
5842 : 13996 : *or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
5843 : : logical_type_node, *or_expr, cond);
5844 : :
5845 : 18454 : return res;
5846 : : }
5847 : :
5848 : :
5849 : : /* For an array descriptor, get the total number of elements. This is just
5850 : : the product of the extents along from_dim to to_dim. */
5851 : :
5852 : : static tree
5853 : 1620 : gfc_conv_descriptor_size_1 (tree desc, int from_dim, int to_dim)
5854 : : {
5855 : 1620 : tree res;
5856 : 1620 : int dim;
5857 : :
5858 : 1620 : res = gfc_index_one_node;
5859 : :
5860 : 3928 : for (dim = from_dim; dim < to_dim; ++dim)
5861 : : {
5862 : 2308 : tree lbound;
5863 : 2308 : tree ubound;
5864 : 2308 : tree extent;
5865 : :
5866 : 2308 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
5867 : 2308 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
5868 : :
5869 : 2308 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
5870 : 2308 : res = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5871 : : res, extent);
5872 : : }
5873 : :
5874 : 1620 : return res;
5875 : : }
5876 : :
5877 : :
5878 : : /* Full size of an array. */
5879 : :
5880 : : tree
5881 : 1590 : gfc_conv_descriptor_size (tree desc, int rank)
5882 : : {
5883 : 1590 : return gfc_conv_descriptor_size_1 (desc, 0, rank);
5884 : : }
5885 : :
5886 : :
5887 : : /* Size of a coarray for all dimensions but the last. */
5888 : :
5889 : : tree
5890 : 30 : gfc_conv_descriptor_cosize (tree desc, int rank, int corank)
5891 : : {
5892 : 30 : return gfc_conv_descriptor_size_1 (desc, rank, rank + corank - 1);
5893 : : }
5894 : :
5895 : :
5896 : : /* Fills in an array descriptor, and returns the size of the array.
5897 : : The size will be a simple_val, ie a variable or a constant. Also
5898 : : calculates the offset of the base. The pointer argument overflow,
5899 : : which should be of integer type, will increase in value if overflow
5900 : : occurs during the size calculation. Returns the size of the array.
5901 : : {
5902 : : stride = 1;
5903 : : offset = 0;
5904 : : for (n = 0; n < rank; n++)
5905 : : {
5906 : : a.lbound[n] = specified_lower_bound;
5907 : : offset = offset + a.lbond[n] * stride;
5908 : : size = 1 - lbound;
5909 : : a.ubound[n] = specified_upper_bound;
5910 : : a.stride[n] = stride;
5911 : : size = size >= 0 ? ubound + size : 0; //size = ubound + 1 - lbound
5912 : : overflow += size == 0 ? 0: (MAX/size < stride ? 1: 0);
5913 : : stride = stride * size;
5914 : : }
5915 : : for (n = rank; n < rank+corank; n++)
5916 : : (Set lcobound/ucobound as above.)
5917 : : element_size = sizeof (array element);
5918 : : if (!rank)
5919 : : return element_size
5920 : : stride = (size_t) stride;
5921 : : overflow += element_size == 0 ? 0: (MAX/element_size < stride ? 1: 0);
5922 : : stride = stride * element_size;
5923 : : return (stride);
5924 : : } */
5925 : : /*GCC ARRAYS*/
5926 : :
5927 : : static tree
5928 : 10368 : gfc_array_init_size (tree descriptor, int rank, int corank, tree * poffset,
5929 : : gfc_expr ** lower, gfc_expr ** upper, stmtblock_t * pblock,
5930 : : stmtblock_t * descriptor_block, tree * overflow,
5931 : : tree expr3_elem_size, tree *nelems, gfc_expr *expr3,
5932 : : tree expr3_desc, bool e3_has_nodescriptor, gfc_expr *expr,
5933 : : tree *element_size)
5934 : : {
5935 : 10368 : tree type;
5936 : 10368 : tree tmp;
5937 : 10368 : tree size;
5938 : 10368 : tree offset;
5939 : 10368 : tree stride;
5940 : 10368 : tree or_expr;
5941 : 10368 : tree thencase;
5942 : 10368 : tree elsecase;
5943 : 10368 : tree cond;
5944 : 10368 : tree var;
5945 : 10368 : stmtblock_t thenblock;
5946 : 10368 : stmtblock_t elseblock;
5947 : 10368 : gfc_expr *ubound;
5948 : 10368 : gfc_se se;
5949 : 10368 : int n;
5950 : :
5951 : 10368 : type = TREE_TYPE (descriptor);
5952 : :
5953 : 10368 : stride = gfc_index_one_node;
5954 : 10368 : offset = gfc_index_zero_node;
5955 : :
5956 : : /* Set the dtype before the alloc, because registration of coarrays needs
5957 : : it initialized. */
5958 : 10368 : if (expr->ts.type == BT_CHARACTER
5959 : 975 : && expr->ts.deferred
5960 : 470 : && VAR_P (expr->ts.u.cl->backend_decl))
5961 : : {
5962 : 293 : type = gfc_typenode_for_spec (&expr->ts);
5963 : 293 : tmp = gfc_conv_descriptor_dtype (descriptor);
5964 : 293 : gfc_add_modify (pblock, tmp, gfc_get_dtype_rank_type (rank, type));
5965 : : }
5966 : 10075 : else if (expr->ts.type == BT_CHARACTER
5967 : 682 : && expr->ts.deferred
5968 : 177 : && TREE_CODE (descriptor) == COMPONENT_REF)
5969 : : {
5970 : : /* Deferred character components have their string length tucked away
5971 : : in a hidden field of the derived type. Obtain that and use it to
5972 : : set the dtype. The charlen backend decl is zero because the field
5973 : : type is zero length. */
5974 : 159 : gfc_ref *ref;
5975 : 159 : tmp = NULL_TREE;
5976 : 159 : for (ref = expr->ref; ref; ref = ref->next)
5977 : 159 : if (ref->type == REF_COMPONENT
5978 : 159 : && gfc_deferred_strlen (ref->u.c.component, &tmp))
5979 : : break;
5980 : 159 : gcc_assert (tmp != NULL_TREE);
5981 : 159 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
5982 : 159 : TREE_OPERAND (descriptor, 0), tmp, NULL_TREE);
5983 : 159 : tmp = fold_convert (gfc_charlen_type_node, tmp);
5984 : 159 : type = gfc_get_character_type_len (expr->ts.kind, tmp);
5985 : 159 : tmp = gfc_conv_descriptor_dtype (descriptor);
5986 : 159 : gfc_add_modify (pblock, tmp, gfc_get_dtype_rank_type (rank, type));
5987 : 159 : }
5988 : 9916 : else if (expr3_desc && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr3_desc)))
5989 : : {
5990 : 871 : tmp = gfc_conv_descriptor_dtype (descriptor);
5991 : 871 : gfc_add_modify (pblock, tmp, gfc_conv_descriptor_dtype (expr3_desc));
5992 : : }
5993 : : else
5994 : : {
5995 : 9045 : tmp = gfc_conv_descriptor_dtype (descriptor);
5996 : 9045 : gfc_add_modify (pblock, tmp, gfc_get_dtype (type));
5997 : : }
5998 : :
5999 : 10368 : or_expr = logical_false_node;
6000 : :
6001 : 24364 : for (n = 0; n < rank; n++)
6002 : : {
6003 : 13996 : tree conv_lbound;
6004 : 13996 : tree conv_ubound;
6005 : :
6006 : : /* We have 3 possibilities for determining the size of the array:
6007 : : lower == NULL => lbound = 1, ubound = upper[n]
6008 : : upper[n] = NULL => lbound = 1, ubound = lower[n]
6009 : : upper[n] != NULL => lbound = lower[n], ubound = upper[n] */
6010 : 13996 : ubound = upper[n];
6011 : :
6012 : : /* Set lower bound. */
6013 : 13996 : gfc_init_se (&se, NULL);
6014 : 13996 : if (expr3_desc != NULL_TREE)
6015 : : {
6016 : 1310 : if (e3_has_nodescriptor)
6017 : : /* The lbound of nondescriptor arrays like array constructors,
6018 : : nonallocatable/nonpointer function results/variables,
6019 : : start at zero, but when allocating it, the standard expects
6020 : : the array to start at one. */
6021 : 857 : se.expr = gfc_index_one_node;
6022 : : else
6023 : 453 : se.expr = gfc_conv_descriptor_lbound_get (expr3_desc,
6024 : : gfc_rank_cst[n]);
6025 : : }
6026 : 12686 : else if (lower == NULL)
6027 : 9961 : se.expr = gfc_index_one_node;
6028 : : else
6029 : : {
6030 : 2725 : gcc_assert (lower[n]);
6031 : 2725 : if (ubound)
6032 : : {
6033 : 2176 : gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
6034 : 2176 : gfc_add_block_to_block (pblock, &se.pre);
6035 : : }
6036 : : else
6037 : : {
6038 : 549 : se.expr = gfc_index_one_node;
6039 : 549 : ubound = lower[n];
6040 : : }
6041 : : }
6042 : 13996 : gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
6043 : : gfc_rank_cst[n], se.expr);
6044 : 13996 : conv_lbound = se.expr;
6045 : :
6046 : : /* Work out the offset for this component. */
6047 : 13996 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6048 : : se.expr, stride);
6049 : 13996 : offset = fold_build2_loc (input_location, MINUS_EXPR,
6050 : : gfc_array_index_type, offset, tmp);
6051 : :
6052 : : /* Set upper bound. */
6053 : 13996 : gfc_init_se (&se, NULL);
6054 : 13996 : if (expr3_desc != NULL_TREE)
6055 : : {
6056 : 1310 : if (e3_has_nodescriptor)
6057 : : {
6058 : : /* The lbound of nondescriptor arrays like array constructors,
6059 : : nonallocatable/nonpointer function results/variables,
6060 : : start at zero, but when allocating it, the standard expects
6061 : : the array to start at one. Therefore fix the upper bound to be
6062 : : (desc.ubound - desc.lbound) + 1. */
6063 : 857 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
6064 : : gfc_array_index_type,
6065 : : gfc_conv_descriptor_ubound_get (
6066 : : expr3_desc, gfc_rank_cst[n]),
6067 : : gfc_conv_descriptor_lbound_get (
6068 : : expr3_desc, gfc_rank_cst[n]));
6069 : 857 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
6070 : : gfc_array_index_type, tmp,
6071 : : gfc_index_one_node);
6072 : 857 : se.expr = gfc_evaluate_now (tmp, pblock);
6073 : : }
6074 : : else
6075 : 453 : se.expr = gfc_conv_descriptor_ubound_get (expr3_desc,
6076 : : gfc_rank_cst[n]);
6077 : : }
6078 : : else
6079 : : {
6080 : 12686 : gcc_assert (ubound);
6081 : 12686 : gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
6082 : 12686 : gfc_add_block_to_block (pblock, &se.pre);
6083 : 12686 : if (ubound->expr_type == EXPR_FUNCTION)
6084 : 735 : se.expr = gfc_evaluate_now (se.expr, pblock);
6085 : : }
6086 : 13996 : gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
6087 : : gfc_rank_cst[n], se.expr);
6088 : 13996 : conv_ubound = se.expr;
6089 : :
6090 : : /* Store the stride. */
6091 : 13996 : gfc_conv_descriptor_stride_set (descriptor_block, descriptor,
6092 : : gfc_rank_cst[n], stride);
6093 : :
6094 : : /* Calculate size and check whether extent is negative. */
6095 : 13996 : size = gfc_conv_array_extent_dim (conv_lbound, conv_ubound, &or_expr);
6096 : 13996 : size = gfc_evaluate_now (size, pblock);
6097 : :
6098 : : /* Check whether multiplying the stride by the number of
6099 : : elements in this dimension would overflow. We must also check
6100 : : whether the current dimension has zero size in order to avoid
6101 : : division by zero.
6102 : : */
6103 : 13996 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6104 : : gfc_array_index_type,
6105 : 13996 : fold_convert (gfc_array_index_type,
6106 : : TYPE_MAX_VALUE (gfc_array_index_type)),
6107 : : size);
6108 : 13996 : cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
6109 : : logical_type_node, tmp, stride),
6110 : : PRED_FORTRAN_OVERFLOW);
6111 : 13996 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6112 : : integer_one_node, integer_zero_node);
6113 : 13996 : cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
6114 : : logical_type_node, size,
6115 : : gfc_index_zero_node),
6116 : : PRED_FORTRAN_SIZE_ZERO);
6117 : 13996 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6118 : : integer_zero_node, tmp);
6119 : 13996 : tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
6120 : : *overflow, tmp);
6121 : 13996 : *overflow = gfc_evaluate_now (tmp, pblock);
6122 : :
6123 : : /* Multiply the stride by the number of elements in this dimension. */
6124 : 13996 : stride = fold_build2_loc (input_location, MULT_EXPR,
6125 : : gfc_array_index_type, stride, size);
6126 : 13996 : stride = gfc_evaluate_now (stride, pblock);
6127 : : }
6128 : :
6129 : 10849 : for (n = rank; n < rank + corank; n++)
6130 : : {
6131 : 481 : ubound = upper[n];
6132 : :
6133 : : /* Set lower bound. */
6134 : 481 : gfc_init_se (&se, NULL);
6135 : 481 : if (lower == NULL || lower[n] == NULL)
6136 : : {
6137 : 265 : gcc_assert (n == rank + corank - 1);
6138 : 265 : se.expr = gfc_index_one_node;
6139 : : }
6140 : : else
6141 : : {
6142 : 216 : if (ubound || n == rank + corank - 1)
6143 : : {
6144 : 142 : gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
6145 : 142 : gfc_add_block_to_block (pblock, &se.pre);
6146 : : }
6147 : : else
6148 : : {
6149 : 74 : se.expr = gfc_index_one_node;
6150 : 74 : ubound = lower[n];
6151 : : }
6152 : : }
6153 : 481 : gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
6154 : : gfc_rank_cst[n], se.expr);
6155 : :
6156 : 481 : if (n < rank + corank - 1)
6157 : : {
6158 : 143 : gfc_init_se (&se, NULL);
6159 : 143 : gcc_assert (ubound);
6160 : 143 : gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
6161 : 143 : gfc_add_block_to_block (pblock, &se.pre);
6162 : 143 : gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
6163 : : gfc_rank_cst[n], se.expr);
6164 : : }
6165 : : }
6166 : :
6167 : : /* The stride is the number of elements in the array, so multiply by the
6168 : : size of an element to get the total size. Obviously, if there is a
6169 : : SOURCE expression (expr3) we must use its element size. */
6170 : 10368 : if (expr3_elem_size != NULL_TREE)
6171 : 2747 : tmp = expr3_elem_size;
6172 : 7621 : else if (expr3 != NULL)
6173 : : {
6174 : 0 : if (expr3->ts.type == BT_CLASS)
6175 : : {
6176 : 0 : gfc_se se_sz;
6177 : 0 : gfc_expr *sz = gfc_copy_expr (expr3);
6178 : 0 : gfc_add_vptr_component (sz);
6179 : 0 : gfc_add_size_component (sz);
6180 : 0 : gfc_init_se (&se_sz, NULL);
6181 : 0 : gfc_conv_expr (&se_sz, sz);
6182 : 0 : gfc_free_expr (sz);
6183 : 0 : tmp = se_sz.expr;
6184 : : }
6185 : : else
6186 : : {
6187 : 0 : tmp = gfc_typenode_for_spec (&expr3->ts);
6188 : 0 : tmp = TYPE_SIZE_UNIT (tmp);
6189 : : }
6190 : : }
6191 : : else
6192 : 7621 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
6193 : :
6194 : : /* Convert to size_t. */
6195 : 10368 : *element_size = fold_convert (size_type_node, tmp);
6196 : :
6197 : 10368 : if (rank == 0)
6198 : : return *element_size;
6199 : :
6200 : 10223 : *nelems = gfc_evaluate_now (stride, pblock);
6201 : 10223 : stride = fold_convert (size_type_node, stride);
6202 : :
6203 : : /* First check for overflow. Since an array of type character can
6204 : : have zero element_size, we must check for that before
6205 : : dividing. */
6206 : 10223 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6207 : : size_type_node,
6208 : 10223 : TYPE_MAX_VALUE (size_type_node), *element_size);
6209 : 10223 : cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
6210 : : logical_type_node, tmp, stride),
6211 : : PRED_FORTRAN_OVERFLOW);
6212 : 10223 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6213 : : integer_one_node, integer_zero_node);
6214 : 10223 : cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
6215 : : logical_type_node, *element_size,
6216 : 10223 : build_int_cst (size_type_node, 0)),
6217 : : PRED_FORTRAN_SIZE_ZERO);
6218 : 10223 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6219 : : integer_zero_node, tmp);
6220 : 10223 : tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
6221 : : *overflow, tmp);
6222 : 10223 : *overflow = gfc_evaluate_now (tmp, pblock);
6223 : :
6224 : 10223 : size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6225 : : stride, *element_size);
6226 : :
6227 : 10223 : if (poffset != NULL)
6228 : : {
6229 : 10223 : offset = gfc_evaluate_now (offset, pblock);
6230 : 10223 : *poffset = offset;
6231 : : }
6232 : :
6233 : 10223 : if (integer_zerop (or_expr))
6234 : : return size;
6235 : 3257 : if (integer_onep (or_expr))
6236 : 388 : return build_int_cst (size_type_node, 0);
6237 : :
6238 : 2869 : var = gfc_create_var (TREE_TYPE (size), "size");
6239 : 2869 : gfc_start_block (&thenblock);
6240 : 2869 : gfc_add_modify (&thenblock, var, build_int_cst (size_type_node, 0));
6241 : 2869 : thencase = gfc_finish_block (&thenblock);
6242 : :
6243 : 2869 : gfc_start_block (&elseblock);
6244 : 2869 : gfc_add_modify (&elseblock, var, size);
6245 : 2869 : elsecase = gfc_finish_block (&elseblock);
6246 : :
6247 : 2869 : tmp = gfc_evaluate_now (or_expr, pblock);
6248 : 2869 : tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
6249 : 2869 : gfc_add_expr_to_block (pblock, tmp);
6250 : :
6251 : 2869 : return var;
6252 : : }
6253 : :
6254 : :
6255 : : /* Retrieve the last ref from the chain. This routine is specific to
6256 : : gfc_array_allocate ()'s needs. */
6257 : :
6258 : : bool
6259 : 16335 : retrieve_last_ref (gfc_ref **ref_in, gfc_ref **prev_ref_in)
6260 : : {
6261 : 16335 : gfc_ref *ref, *prev_ref;
6262 : :
6263 : 16335 : ref = *ref_in;
6264 : : /* Prevent warnings for uninitialized variables. */
6265 : 16335 : prev_ref = *prev_ref_in;
6266 : 22411 : while (ref && ref->next != NULL)
6267 : : {
6268 : 6076 : gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT
6269 : : || (ref->u.ar.dimen == 0 && ref->u.ar.codimen > 0));
6270 : : prev_ref = ref;
6271 : : ref = ref->next;
6272 : : }
6273 : :
6274 : 16335 : if (ref == NULL || ref->type != REF_ARRAY)
6275 : : return false;
6276 : :
6277 : 11492 : *ref_in = ref;
6278 : 11492 : *prev_ref_in = prev_ref;
6279 : 11492 : return true;
6280 : : }
6281 : :
6282 : : /* Initializes the descriptor and generates a call to _gfor_allocate. Does
6283 : : the work for an ALLOCATE statement. */
6284 : : /*GCC ARRAYS*/
6285 : :
6286 : : bool
6287 : 15211 : gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree status, tree errmsg,
6288 : : tree errlen, tree label_finish, tree expr3_elem_size,
6289 : : tree *nelems, gfc_expr *expr3, tree e3_arr_desc,
6290 : : bool e3_has_nodescriptor, gfc_omp_namelist *omp_alloc)
6291 : : {
6292 : 15211 : tree tmp;
6293 : 15211 : tree pointer;
6294 : 15211 : tree offset = NULL_TREE;
6295 : 15211 : tree token = NULL_TREE;
6296 : 15211 : tree size;
6297 : 15211 : tree msg;
6298 : 15211 : tree error = NULL_TREE;
6299 : 15211 : tree overflow; /* Boolean storing whether size calculation overflows. */
6300 : 15211 : tree var_overflow = NULL_TREE;
6301 : 15211 : tree cond;
6302 : 15211 : tree set_descriptor;
6303 : 15211 : tree not_prev_allocated = NULL_TREE;
6304 : 15211 : tree element_size = NULL_TREE;
6305 : 15211 : stmtblock_t set_descriptor_block;
6306 : 15211 : stmtblock_t elseblock;
6307 : 15211 : gfc_expr **lower;
6308 : 15211 : gfc_expr **upper;
6309 : 15211 : gfc_ref *ref, *prev_ref = NULL, *coref;
6310 : 15211 : bool allocatable, coarray, dimension, alloc_w_e3_arr_spec = false,
6311 : : non_ulimate_coarray_ptr_comp;
6312 : 15211 : tree omp_cond = NULL_TREE, omp_alt_alloc = NULL_TREE;
6313 : :
6314 : 15211 : ref = expr->ref;
6315 : :
6316 : : /* Find the last reference in the chain. */
6317 : 15211 : if (!retrieve_last_ref (&ref, &prev_ref))
6318 : : return false;
6319 : :
6320 : : /* Take the allocatable and coarray properties solely from the expr-ref's
6321 : : attributes and not from source=-expression. */
6322 : 10368 : if (!prev_ref)
6323 : : {
6324 : 7079 : allocatable = expr->symtree->n.sym->attr.allocatable;
6325 : 7079 : dimension = expr->symtree->n.sym->attr.dimension;
6326 : 7079 : non_ulimate_coarray_ptr_comp = false;
6327 : : }
6328 : : else
6329 : : {
6330 : 3289 : allocatable = prev_ref->u.c.component->attr.allocatable;
6331 : : /* Pointer components in coarrayed derived types must be treated
6332 : : specially in that they are registered without a check if the are
6333 : : already associated. This does not hold for ultimate coarray
6334 : : pointers. */
6335 : 3289 : non_ulimate_coarray_ptr_comp = (prev_ref->u.c.component->attr.pointer
6336 : 3289 : && !prev_ref->u.c.component->attr.codimension);
6337 : 3289 : dimension = prev_ref->u.c.component->attr.dimension;
6338 : : }
6339 : :
6340 : : /* For allocatable/pointer arrays in derived types, one of the refs has to be
6341 : : a coarray. In this case it does not matter whether we are on this_image
6342 : : or not. */
6343 : 10368 : coarray = false;
6344 : 24768 : for (coref = expr->ref; coref; coref = coref->next)
6345 : 14900 : if (coref->type == REF_ARRAY && coref->u.ar.codimen > 0)
6346 : : {
6347 : : coarray = true;
6348 : : break;
6349 : : }
6350 : :
6351 : 10368 : if (!dimension)
6352 : 145 : gcc_assert (coarray);
6353 : :
6354 : 10368 : if (ref->u.ar.type == AR_FULL && expr3 != NULL)
6355 : : {
6356 : 1124 : gfc_ref *old_ref = ref;
6357 : : /* F08:C633: Array shape from expr3. */
6358 : 1124 : ref = expr3->ref;
6359 : :
6360 : : /* Find the last reference in the chain. */
6361 : 1124 : if (!retrieve_last_ref (&ref, &prev_ref))
6362 : : {
6363 : 0 : if (expr3->expr_type == EXPR_FUNCTION
6364 : 0 : && gfc_expr_attr (expr3).dimension)
6365 : 0 : ref = old_ref;
6366 : : else
6367 : 0 : return false;
6368 : : }
6369 : : alloc_w_e3_arr_spec = true;
6370 : : }
6371 : :
6372 : : /* Figure out the size of the array. */
6373 : 10368 : switch (ref->u.ar.type)
6374 : : {
6375 : 7746 : case AR_ELEMENT:
6376 : 7746 : if (!coarray)
6377 : : {
6378 : 7292 : lower = NULL;
6379 : 7292 : upper = ref->u.ar.start;
6380 : 7292 : break;
6381 : : }
6382 : : /* Fall through. */
6383 : :
6384 : 1982 : case AR_SECTION:
6385 : 1982 : lower = ref->u.ar.start;
6386 : 1982 : upper = ref->u.ar.end;
6387 : 1982 : break;
6388 : :
6389 : 1094 : case AR_FULL:
6390 : 1094 : gcc_assert (ref->u.ar.as->type == AS_EXPLICIT
6391 : : || alloc_w_e3_arr_spec);
6392 : :
6393 : 1094 : lower = ref->u.ar.as->lower;
6394 : 1094 : upper = ref->u.ar.as->upper;
6395 : 1094 : break;
6396 : :
6397 : 0 : default:
6398 : 0 : gcc_unreachable ();
6399 : 10368 : break;
6400 : : }
6401 : :
6402 : 10368 : overflow = integer_zero_node;
6403 : :
6404 : 10368 : if (expr->ts.type == BT_CHARACTER
6405 : 975 : && TREE_CODE (se->string_length) == COMPONENT_REF
6406 : 159 : && expr->ts.u.cl->backend_decl != se->string_length
6407 : 159 : && VAR_P (expr->ts.u.cl->backend_decl))
6408 : 0 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
6409 : 0 : fold_convert (TREE_TYPE (expr->ts.u.cl->backend_decl),
6410 : : se->string_length));
6411 : :
6412 : 10368 : gfc_init_block (&set_descriptor_block);
6413 : : /* Take the corank only from the actual ref and not from the coref. The
6414 : : later will mislead the generation of the array dimensions for allocatable/
6415 : : pointer components in derived types. */
6416 : 20112 : size = gfc_array_init_size (se->expr, alloc_w_e3_arr_spec ? expr->rank
6417 : 9244 : : ref->u.ar.as->rank,
6418 : 500 : coarray ? ref->u.ar.as->corank : 0,
6419 : : &offset, lower, upper,
6420 : : &se->pre, &set_descriptor_block, &overflow,
6421 : : expr3_elem_size, nelems, expr3, e3_arr_desc,
6422 : : e3_has_nodescriptor, expr, &element_size);
6423 : :
6424 : 10368 : if (dimension)
6425 : : {
6426 : 10223 : var_overflow = gfc_create_var (integer_type_node, "overflow");
6427 : 10223 : gfc_add_modify (&se->pre, var_overflow, overflow);
6428 : :
6429 : 10223 : if (status == NULL_TREE)
6430 : : {
6431 : : /* Generate the block of code handling overflow. */
6432 : 10009 : msg = gfc_build_addr_expr (pchar_type_node,
6433 : : gfc_build_localized_cstring_const
6434 : : ("Integer overflow when calculating the amount of "
6435 : : "memory to allocate"));
6436 : 10009 : error = build_call_expr_loc (input_location,
6437 : : gfor_fndecl_runtime_error, 1, msg);
6438 : : }
6439 : : else
6440 : : {
6441 : 214 : tree status_type = TREE_TYPE (status);
6442 : 214 : stmtblock_t set_status_block;
6443 : :
6444 : 214 : gfc_start_block (&set_status_block);
6445 : 214 : gfc_add_modify (&set_status_block, status,
6446 : 214 : build_int_cst (status_type, LIBERROR_ALLOCATION));
6447 : 214 : error = gfc_finish_block (&set_status_block);
6448 : : }
6449 : : }
6450 : :
6451 : : /* Allocate memory to store the data. */
6452 : 10368 : if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
6453 : 0 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
6454 : :
6455 : 10368 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
6456 : : {
6457 : 279 : pointer = non_ulimate_coarray_ptr_comp ? se->expr
6458 : 210 : : gfc_conv_descriptor_data_get (se->expr);
6459 : 279 : token = gfc_conv_descriptor_token (se->expr);
6460 : 279 : token = gfc_build_addr_expr (NULL_TREE, token);
6461 : : }
6462 : : else
6463 : : {
6464 : 10089 : pointer = gfc_conv_descriptor_data_get (se->expr);
6465 : 10089 : if (omp_alloc)
6466 : 30 : omp_cond = boolean_true_node;
6467 : : }
6468 : 10368 : STRIP_NOPS (pointer);
6469 : :
6470 : 10368 : if (allocatable)
6471 : : {
6472 : 8356 : not_prev_allocated = gfc_create_var (logical_type_node,
6473 : : "not_prev_allocated");
6474 : 8356 : tmp = fold_build2_loc (input_location, EQ_EXPR,
6475 : : logical_type_node, pointer,
6476 : 8356 : build_int_cst (TREE_TYPE (pointer), 0));
6477 : :
6478 : 8356 : gfc_add_modify (&se->pre, not_prev_allocated, tmp);
6479 : : }
6480 : :
6481 : 10368 : gfc_start_block (&elseblock);
6482 : :
6483 : 10368 : tree succ_add_expr = NULL_TREE;
6484 : 10368 : if (omp_cond)
6485 : : {
6486 : 30 : tree align, alloc, sz;
6487 : 30 : gfc_se se2;
6488 : 30 : if (omp_alloc->u2.allocator)
6489 : : {
6490 : 7 : gfc_init_se (&se2, NULL);
6491 : 7 : gfc_conv_expr (&se2, omp_alloc->u2.allocator);
6492 : 7 : gfc_add_block_to_block (&elseblock, &se2.pre);
6493 : 7 : alloc = gfc_evaluate_now (se2.expr, &elseblock);
6494 : 7 : gfc_add_block_to_block (&elseblock, &se2.post);
6495 : : }
6496 : : else
6497 : 23 : alloc = build_zero_cst (ptr_type_node);
6498 : 30 : tmp = TREE_TYPE (TREE_TYPE (pointer));
6499 : 30 : if (tmp == void_type_node)
6500 : 30 : tmp = gfc_typenode_for_spec (&expr->ts, 0);
6501 : 30 : if (omp_alloc->u.align)
6502 : : {
6503 : 15 : gfc_init_se (&se2, NULL);
6504 : 15 : gfc_conv_expr (&se2, omp_alloc->u.align);
6505 : 15 : gcc_assert (CONSTANT_CLASS_P (se2.expr)
6506 : : && se2.pre.head == NULL
6507 : : && se2.post.head == NULL);
6508 : 15 : align = build_int_cst (size_type_node,
6509 : 15 : MAX (tree_to_uhwi (se2.expr),
6510 : : TYPE_ALIGN_UNIT (tmp)));
6511 : : }
6512 : : else
6513 : 15 : align = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (tmp));
6514 : 30 : sz = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
6515 : : fold_convert (size_type_node, size),
6516 : 30 : build_int_cst (size_type_node, 1));
6517 : 30 : omp_alt_alloc = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
6518 : 30 : DECL_ATTRIBUTES (omp_alt_alloc)
6519 : 30 : = tree_cons (get_identifier ("omp allocator"),
6520 : : build_tree_list (NULL_TREE, alloc),
6521 : 30 : DECL_ATTRIBUTES (omp_alt_alloc));
6522 : 30 : omp_alt_alloc = build_call_expr (omp_alt_alloc, 3, align, sz, alloc);
6523 : 30 : succ_add_expr = fold_build2_loc (input_location, MODIFY_EXPR,
6524 : : void_type_node,
6525 : : gfc_conv_descriptor_version (se->expr),
6526 : 30 : build_int_cst (integer_type_node, 1));
6527 : : }
6528 : :
6529 : : /* The allocatable variant takes the old pointer as first argument. */
6530 : 10368 : if (allocatable)
6531 : 8784 : gfc_allocate_allocatable (&elseblock, pointer, size, token,
6532 : : status, errmsg, errlen, label_finish, expr,
6533 : 428 : coref != NULL ? coref->u.ar.as->corank : 0,
6534 : : omp_cond, omp_alt_alloc, succ_add_expr);
6535 : 2012 : else if (non_ulimate_coarray_ptr_comp && token)
6536 : : /* The token is set only for GFC_FCOARRAY_LIB mode. */
6537 : 69 : gfc_allocate_using_caf_lib (&elseblock, pointer, size, token, status,
6538 : : errmsg, errlen,
6539 : : GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY);
6540 : : else
6541 : 1943 : gfc_allocate_using_malloc (&elseblock, pointer, size, status,
6542 : : omp_cond, omp_alt_alloc, succ_add_expr);
6543 : :
6544 : 10368 : if (dimension)
6545 : : {
6546 : 10223 : cond = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
6547 : : logical_type_node, var_overflow, integer_zero_node),
6548 : : PRED_FORTRAN_OVERFLOW);
6549 : 10223 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6550 : : error, gfc_finish_block (&elseblock));
6551 : : }
6552 : : else
6553 : 145 : tmp = gfc_finish_block (&elseblock);
6554 : :
6555 : 10368 : gfc_add_expr_to_block (&se->pre, tmp);
6556 : :
6557 : : /* Update the array descriptor with the offset and the span. */
6558 : 10368 : if (dimension)
6559 : : {
6560 : 10223 : gfc_conv_descriptor_offset_set (&set_descriptor_block, se->expr, offset);
6561 : 10223 : tmp = fold_convert (gfc_array_index_type, element_size);
6562 : 10223 : gfc_conv_descriptor_span_set (&set_descriptor_block, se->expr, tmp);
6563 : : }
6564 : :
6565 : 10368 : set_descriptor = gfc_finish_block (&set_descriptor_block);
6566 : 10368 : if (status != NULL_TREE)
6567 : : {
6568 : 228 : cond = fold_build2_loc (input_location, EQ_EXPR,
6569 : : logical_type_node, status,
6570 : 228 : build_int_cst (TREE_TYPE (status), 0));
6571 : :
6572 : 228 : if (not_prev_allocated != NULL_TREE)
6573 : 207 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
6574 : : logical_type_node, cond, not_prev_allocated);
6575 : :
6576 : 228 : gfc_add_expr_to_block (&se->pre,
6577 : : fold_build3_loc (input_location, COND_EXPR, void_type_node,
6578 : : cond,
6579 : : set_descriptor,
6580 : : build_empty_stmt (input_location)));
6581 : : }
6582 : : else
6583 : 10140 : gfc_add_expr_to_block (&se->pre, set_descriptor);
6584 : :
6585 : 10368 : expr->symtree->n.sym->allocated_in_scope = 1;
6586 : :
6587 : 10368 : return true;
6588 : : }
6589 : :
6590 : :
6591 : : /* Create an array constructor from an initialization expression.
6592 : : We assume the frontend already did any expansions and conversions. */
6593 : :
6594 : : tree
6595 : 6719 : gfc_conv_array_initializer (tree type, gfc_expr * expr)
6596 : : {
6597 : 6719 : gfc_constructor *c;
6598 : 6719 : tree tmp;
6599 : 6719 : gfc_se se;
6600 : 6719 : tree index, range;
6601 : 6719 : vec<constructor_elt, va_gc> *v = NULL;
6602 : :
6603 : 6719 : if (expr->expr_type == EXPR_VARIABLE
6604 : 0 : && expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6605 : 0 : && expr->symtree->n.sym->value)
6606 : 6719 : expr = expr->symtree->n.sym->value;
6607 : :
6608 : 6719 : switch (expr->expr_type)
6609 : : {
6610 : 1047 : case EXPR_CONSTANT:
6611 : 1047 : case EXPR_STRUCTURE:
6612 : : /* A single scalar or derived type value. Create an array with all
6613 : : elements equal to that value. */
6614 : 1047 : gfc_init_se (&se, NULL);
6615 : :
6616 : 1047 : if (expr->expr_type == EXPR_CONSTANT)
6617 : 365 : gfc_conv_constant (&se, expr);
6618 : : else
6619 : 682 : gfc_conv_structure (&se, expr, 1);
6620 : :
6621 : 2094 : if (tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
6622 : 1047 : TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
6623 : : break;
6624 : 2070 : else if (tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
6625 : 1035 : TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
6626 : 144 : range = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
6627 : : else
6628 : 1782 : range = build2 (RANGE_EXPR, gfc_array_index_type,
6629 : 891 : TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
6630 : 891 : TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
6631 : 1035 : CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
6632 : 1035 : break;
6633 : :
6634 : 5672 : case EXPR_ARRAY:
6635 : : /* Create a vector of all the elements. */
6636 : 5672 : for (c = gfc_constructor_first (expr->value.constructor);
6637 : 149809 : c && c->expr; c = gfc_constructor_next (c))
6638 : : {
6639 : 144137 : if (c->iterator)
6640 : : {
6641 : : /* Problems occur when we get something like
6642 : : integer :: a(lots) = (/(i, i=1, lots)/) */
6643 : 0 : gfc_fatal_error ("The number of elements in the array "
6644 : : "constructor at %L requires an increase of "
6645 : : "the allowed %d upper limit. See "
6646 : : "%<-fmax-array-constructor%> option",
6647 : : &expr->where, flag_max_array_constructor);
6648 : : return NULL_TREE;
6649 : : }
6650 : 144137 : if (mpz_cmp_si (c->offset, 0) != 0)
6651 : 138698 : index = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
6652 : : else
6653 : : index = NULL_TREE;
6654 : :
6655 : 144137 : if (mpz_cmp_si (c->repeat, 1) > 0)
6656 : : {
6657 : 152 : tree tmp1, tmp2;
6658 : 152 : mpz_t maxval;
6659 : :
6660 : 152 : mpz_init (maxval);
6661 : 152 : mpz_add (maxval, c->offset, c->repeat);
6662 : 152 : mpz_sub_ui (maxval, maxval, 1);
6663 : 152 : tmp2 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
6664 : 152 : if (mpz_cmp_si (c->offset, 0) != 0)
6665 : : {
6666 : 27 : mpz_add_ui (maxval, c->offset, 1);
6667 : 27 : tmp1 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
6668 : : }
6669 : : else
6670 : 125 : tmp1 = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
6671 : :
6672 : 152 : range = fold_build2 (RANGE_EXPR, gfc_array_index_type, tmp1, tmp2);
6673 : 152 : mpz_clear (maxval);
6674 : : }
6675 : : else
6676 : : range = NULL;
6677 : :
6678 : 144137 : gfc_init_se (&se, NULL);
6679 : 144137 : switch (c->expr->expr_type)
6680 : : {
6681 : 142937 : case EXPR_CONSTANT:
6682 : 142937 : gfc_conv_constant (&se, c->expr);
6683 : :
6684 : : /* See gfortran.dg/charlen_15.f90 for instance. */
6685 : 142937 : if (TREE_CODE (se.expr) == STRING_CST
6686 : 5067 : && TREE_CODE (type) == ARRAY_TYPE)
6687 : : {
6688 : : tree atype = type;
6689 : 10134 : while (TREE_CODE (TREE_TYPE (atype)) == ARRAY_TYPE)
6690 : 5067 : atype = TREE_TYPE (atype);
6691 : 5067 : gcc_checking_assert (TREE_CODE (TREE_TYPE (atype))
6692 : : == INTEGER_TYPE);
6693 : 5067 : gcc_checking_assert (TREE_TYPE (TREE_TYPE (se.expr))
6694 : : == TREE_TYPE (atype));
6695 : 5067 : if (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (se.expr)))
6696 : 5067 : > tree_to_uhwi (TYPE_SIZE_UNIT (atype)))
6697 : : {
6698 : 0 : unsigned HOST_WIDE_INT size
6699 : 0 : = tree_to_uhwi (TYPE_SIZE_UNIT (atype));
6700 : 0 : const char *p = TREE_STRING_POINTER (se.expr);
6701 : :
6702 : 0 : se.expr = build_string (size, p);
6703 : : }
6704 : 5067 : TREE_TYPE (se.expr) = atype;
6705 : : }
6706 : : break;
6707 : :
6708 : 1200 : case EXPR_STRUCTURE:
6709 : 1200 : gfc_conv_structure (&se, c->expr, 1);
6710 : 1200 : break;
6711 : :
6712 : 0 : default:
6713 : : /* Catch those occasional beasts that do not simplify
6714 : : for one reason or another, assuming that if they are
6715 : : standard defying the frontend will catch them. */
6716 : 0 : gfc_conv_expr (&se, c->expr);
6717 : 0 : break;
6718 : : }
6719 : :
6720 : 144137 : if (range == NULL_TREE)
6721 : 143985 : CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
6722 : : else
6723 : : {
6724 : 152 : if (index != NULL_TREE)
6725 : 27 : CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
6726 : 144289 : CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
6727 : : }
6728 : : }
6729 : : break;
6730 : :
6731 : 0 : case EXPR_NULL:
6732 : 0 : return gfc_build_null_descriptor (type);
6733 : :
6734 : 0 : default:
6735 : 0 : gcc_unreachable ();
6736 : : }
6737 : :
6738 : : /* Create a constructor from the list of elements. */
6739 : 6719 : tmp = build_constructor (type, v);
6740 : 6719 : TREE_CONSTANT (tmp) = 1;
6741 : 6719 : return tmp;
6742 : : }
6743 : :
6744 : :
6745 : : /* Generate code to evaluate non-constant coarray cobounds. */
6746 : :
6747 : : void
6748 : 18342 : gfc_trans_array_cobounds (tree type, stmtblock_t * pblock,
6749 : : const gfc_symbol *sym)
6750 : : {
6751 : 18342 : int dim;
6752 : 18342 : tree ubound;
6753 : 18342 : tree lbound;
6754 : 18342 : gfc_se se;
6755 : 18342 : gfc_array_spec *as;
6756 : :
6757 : 18342 : as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
6758 : :
6759 : 18982 : for (dim = as->rank; dim < as->rank + as->corank; dim++)
6760 : : {
6761 : : /* Evaluate non-constant array bound expressions.
6762 : : F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
6763 : : references a function, the result is finalized before execution of the
6764 : : executable constructs in the scoping unit.
6765 : : Adding the finalblocks enables this. */
6766 : 640 : lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
6767 : 640 : if (as->lower[dim] && !INTEGER_CST_P (lbound))
6768 : : {
6769 : 96 : gfc_init_se (&se, NULL);
6770 : 96 : gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
6771 : 96 : gfc_add_block_to_block (pblock, &se.pre);
6772 : 96 : gfc_add_block_to_block (pblock, &se.finalblock);
6773 : 96 : gfc_add_modify (pblock, lbound, se.expr);
6774 : : }
6775 : 640 : ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
6776 : 640 : if (as->upper[dim] && !INTEGER_CST_P (ubound))
6777 : : {
6778 : 50 : gfc_init_se (&se, NULL);
6779 : 50 : gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
6780 : 50 : gfc_add_block_to_block (pblock, &se.pre);
6781 : 50 : gfc_add_block_to_block (pblock, &se.finalblock);
6782 : 50 : gfc_add_modify (pblock, ubound, se.expr);
6783 : : }
6784 : : }
6785 : 18342 : }
6786 : :
6787 : :
6788 : : /* Generate code to evaluate non-constant array bounds. Sets *poffset and
6789 : : returns the size (in elements) of the array. */
6790 : :
6791 : : tree
6792 : 11923 : gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
6793 : : stmtblock_t * pblock)
6794 : : {
6795 : 11923 : gfc_array_spec *as;
6796 : 11923 : tree size;
6797 : 11923 : tree stride;
6798 : 11923 : tree offset;
6799 : 11923 : tree ubound;
6800 : 11923 : tree lbound;
6801 : 11923 : tree tmp;
6802 : 11923 : gfc_se se;
6803 : :
6804 : 11923 : int dim;
6805 : :
6806 : 11923 : as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
6807 : :
6808 : 11923 : size = gfc_index_one_node;
6809 : 11923 : offset = gfc_index_zero_node;
6810 : 11923 : stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
6811 : 11923 : if (stride && VAR_P (stride))
6812 : 111 : gfc_add_modify (pblock, stride, gfc_index_one_node);
6813 : 27129 : for (dim = 0; dim < as->rank; dim++)
6814 : : {
6815 : : /* Evaluate non-constant array bound expressions.
6816 : : F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
6817 : : references a function, the result is finalized before execution of the
6818 : : executable constructs in the scoping unit.
6819 : : Adding the finalblocks enables this. */
6820 : 15206 : lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
6821 : 15206 : if (as->lower[dim] && !INTEGER_CST_P (lbound))
6822 : : {
6823 : 484 : gfc_init_se (&se, NULL);
6824 : 484 : gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
6825 : 484 : gfc_add_block_to_block (pblock, &se.pre);
6826 : 484 : gfc_add_block_to_block (pblock, &se.finalblock);
6827 : 484 : gfc_add_modify (pblock, lbound, se.expr);
6828 : : }
6829 : 15206 : ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
6830 : 15206 : if (as->upper[dim] && !INTEGER_CST_P (ubound))
6831 : : {
6832 : 9122 : gfc_init_se (&se, NULL);
6833 : 9122 : gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
6834 : 9122 : gfc_add_block_to_block (pblock, &se.pre);
6835 : 9122 : gfc_add_block_to_block (pblock, &se.finalblock);
6836 : 9122 : gfc_add_modify (pblock, ubound, se.expr);
6837 : : }
6838 : : /* The offset of this dimension. offset = offset - lbound * stride. */
6839 : 15206 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6840 : : lbound, size);
6841 : 15206 : offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6842 : : offset, tmp);
6843 : :
6844 : : /* The size of this dimension, and the stride of the next. */
6845 : 15206 : if (dim + 1 < as->rank)
6846 : 3355 : stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
6847 : : else
6848 : 11851 : stride = GFC_TYPE_ARRAY_SIZE (type);
6849 : :
6850 : 15206 : if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
6851 : : {
6852 : : /* Calculate stride = size * (ubound + 1 - lbound). */
6853 : 9307 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
6854 : : gfc_array_index_type,
6855 : : gfc_index_one_node, lbound);
6856 : 9307 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
6857 : : gfc_array_index_type, ubound, tmp);
6858 : 9307 : tmp = fold_build2_loc (input_location, MULT_EXPR,
6859 : : gfc_array_index_type, size, tmp);
6860 : 9307 : if (stride)
6861 : 9307 : gfc_add_modify (pblock, stride, tmp);
6862 : : else
6863 : 0 : stride = gfc_evaluate_now (tmp, pblock);
6864 : :
6865 : : /* Make sure that negative size arrays are translated
6866 : : to being zero size. */
6867 : 9307 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
6868 : : stride, gfc_index_zero_node);
6869 : 9307 : tmp = fold_build3_loc (input_location, COND_EXPR,
6870 : : gfc_array_index_type, tmp,
6871 : : stride, gfc_index_zero_node);
6872 : 9307 : gfc_add_modify (pblock, stride, tmp);
6873 : : }
6874 : :
6875 : : size = stride;
6876 : : }
6877 : :
6878 : 11923 : gfc_trans_array_cobounds (type, pblock, sym);
6879 : 11923 : gfc_trans_vla_type_sizes (sym, pblock);
6880 : :
6881 : 11923 : *poffset = offset;
6882 : 11923 : return size;
6883 : : }
6884 : :
6885 : :
6886 : : /* Generate code to initialize/allocate an array variable. */
6887 : :
6888 : : void
6889 : 27239 : gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym,
6890 : : gfc_wrapped_block * block)
6891 : : {
6892 : 27239 : stmtblock_t init;
6893 : 27239 : tree type;
6894 : 27239 : tree tmp = NULL_TREE;
6895 : 27239 : tree size;
6896 : 27239 : tree offset;
6897 : 27239 : tree space;
6898 : 27239 : tree inittree;
6899 : 27239 : bool onstack;
6900 : 27239 : bool back;
6901 : :
6902 : 27239 : gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
6903 : :
6904 : : /* Do nothing for USEd variables. */
6905 : 27239 : if (sym->attr.use_assoc)
6906 : 22540 : return;
6907 : :
6908 : 27199 : type = TREE_TYPE (decl);
6909 : 27199 : gcc_assert (GFC_ARRAY_TYPE_P (type));
6910 : 27199 : onstack = TREE_CODE (type) != POINTER_TYPE;
6911 : :
6912 : : /* In the case of non-dummy symbols with dependencies on an old-fashioned
6913 : : function result (ie. proc_name = proc_name->result), gfc_add_init_cleanup
6914 : : must be called with the last, optional argument false so that the alloc-
6915 : : ation occurs after the processing of the result. */
6916 : 27199 : back = sym->fn_result_dep;
6917 : :
6918 : 27199 : gfc_init_block (&init);
6919 : :
6920 : : /* Evaluate character string length. */
6921 : 27199 : if (sym->ts.type == BT_CHARACTER
6922 : 2905 : && onstack && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
6923 : : {
6924 : 43 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
6925 : :
6926 : 43 : gfc_trans_vla_type_sizes (sym, &init);
6927 : :
6928 : : /* Emit a DECL_EXPR for this variable, which will cause the
6929 : : gimplifier to allocate storage, and all that good stuff. */
6930 : 43 : tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
6931 : 43 : gfc_add_expr_to_block (&init, tmp);
6932 : 43 : if (sym->attr.omp_allocate)
6933 : : {
6934 : : /* Save location of size calculation to ensure GOMP_alloc is placed
6935 : : after it. */
6936 : 0 : tree omp_alloc = lookup_attribute ("omp allocate",
6937 : 0 : DECL_ATTRIBUTES (decl));
6938 : 0 : TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
6939 : 0 : = build_tree_list (NULL_TREE, tsi_stmt (tsi_last (init.head)));
6940 : : }
6941 : : }
6942 : :
6943 : 26997 : if (onstack)
6944 : : {
6945 : 22360 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
6946 : : back);
6947 : 22360 : return;
6948 : : }
6949 : :
6950 : 4839 : type = TREE_TYPE (type);
6951 : :
6952 : 4839 : gcc_assert (!sym->attr.use_assoc);
6953 : 4839 : gcc_assert (!sym->module);
6954 : :
6955 : 4839 : if (sym->ts.type == BT_CHARACTER
6956 : 202 : && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
6957 : 94 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
6958 : :
6959 : 4839 : size = gfc_trans_array_bounds (type, sym, &offset, &init);
6960 : :
6961 : : /* Don't actually allocate space for Cray Pointees. */
6962 : 4839 : if (sym->attr.cray_pointee)
6963 : : {
6964 : 140 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
6965 : 49 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
6966 : :
6967 : 140 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
6968 : 140 : return;
6969 : : }
6970 : 4699 : if (sym->attr.omp_allocate)
6971 : : {
6972 : : /* The size is the number of elements in the array, so multiply by the
6973 : : size of an element to get the total size. */
6974 : 7 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
6975 : 7 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6976 : : size, fold_convert (gfc_array_index_type, tmp));
6977 : 7 : size = gfc_evaluate_now (size, &init);
6978 : :
6979 : 7 : tree omp_alloc = lookup_attribute ("omp allocate",
6980 : 7 : DECL_ATTRIBUTES (decl));
6981 : 7 : TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
6982 : 7 : = build_tree_list (size, NULL_TREE);
6983 : 7 : space = NULL_TREE;
6984 : : }
6985 : 4692 : else if (flag_stack_arrays)
6986 : : {
6987 : 11 : gcc_assert (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE);
6988 : 11 : space = build_decl (gfc_get_location (&sym->declared_at),
6989 : : VAR_DECL, create_tmp_var_name ("A"),
6990 : 11 : TREE_TYPE (TREE_TYPE (decl)));
6991 : 11 : gfc_trans_vla_type_sizes (sym, &init);
6992 : : }
6993 : : else
6994 : : {
6995 : : /* The size is the number of elements in the array, so multiply by the
6996 : : size of an element to get the total size. */
6997 : 4681 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
6998 : 4681 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6999 : : size, fold_convert (gfc_array_index_type, tmp));
7000 : :
7001 : : /* Allocate memory to hold the data. */
7002 : 4681 : tmp = gfc_call_malloc (&init, TREE_TYPE (decl), size);
7003 : 4681 : gfc_add_modify (&init, decl, tmp);
7004 : :
7005 : : /* Free the temporary. */
7006 : 4681 : tmp = gfc_call_free (decl);
7007 : 4681 : space = NULL_TREE;
7008 : : }
7009 : :
7010 : : /* Set offset of the array. */
7011 : 4699 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7012 : 398 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7013 : :
7014 : : /* Automatic arrays should not have initializers. */
7015 : 4699 : gcc_assert (!sym->value);
7016 : :
7017 : 4699 : inittree = gfc_finish_block (&init);
7018 : :
7019 : 4699 : if (space)
7020 : : {
7021 : 11 : tree addr;
7022 : 11 : pushdecl (space);
7023 : :
7024 : : /* Don't create new scope, emit the DECL_EXPR in exactly the scope
7025 : : where also space is located. */
7026 : 11 : gfc_init_block (&init);
7027 : 11 : tmp = fold_build1_loc (input_location, DECL_EXPR,
7028 : 11 : TREE_TYPE (space), space);
7029 : 11 : gfc_add_expr_to_block (&init, tmp);
7030 : 11 : addr = fold_build1_loc (gfc_get_location (&sym->declared_at),
7031 : 11 : ADDR_EXPR, TREE_TYPE (decl), space);
7032 : 11 : gfc_add_modify (&init, decl, addr);
7033 : 11 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
7034 : : back);
7035 : 11 : tmp = NULL_TREE;
7036 : : }
7037 : 4699 : gfc_add_init_cleanup (block, inittree, tmp, back);
7038 : : }
7039 : :
7040 : :
7041 : : /* Generate entry and exit code for g77 calling convention arrays. */
7042 : :
7043 : : void
7044 : 6830 : gfc_trans_g77_array (gfc_symbol * sym, gfc_wrapped_block * block)
7045 : : {
7046 : 6830 : tree parm;
7047 : 6830 : tree type;
7048 : 6830 : locus loc;
7049 : 6830 : tree offset;
7050 : 6830 : tree tmp;
7051 : 6830 : tree stmt;
7052 : 6830 : stmtblock_t init;
7053 : :
7054 : 6830 : gfc_save_backend_locus (&loc);
7055 : 6830 : gfc_set_backend_locus (&sym->declared_at);
7056 : :
7057 : : /* Descriptor type. */
7058 : 6830 : parm = sym->backend_decl;
7059 : 6830 : type = TREE_TYPE (parm);
7060 : 6830 : gcc_assert (GFC_ARRAY_TYPE_P (type));
7061 : :
7062 : 6830 : gfc_start_block (&init);
7063 : :
7064 : 6830 : if (sym->ts.type == BT_CHARACTER
7065 : 618 : && VAR_P (sym->ts.u.cl->backend_decl))
7066 : 79 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7067 : :
7068 : : /* Evaluate the bounds of the array. */
7069 : 6830 : gfc_trans_array_bounds (type, sym, &offset, &init);
7070 : :
7071 : : /* Set the offset. */
7072 : 6830 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7073 : 1178 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7074 : :
7075 : : /* Set the pointer itself if we aren't using the parameter directly. */
7076 : 6830 : if (TREE_CODE (parm) != PARM_DECL)
7077 : : {
7078 : 492 : tmp = GFC_DECL_SAVED_DESCRIPTOR (parm);
7079 : 492 : if (sym->ts.type == BT_CLASS)
7080 : : {
7081 : 220 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
7082 : 220 : tmp = gfc_class_data_get (tmp);
7083 : 220 : tmp = gfc_conv_descriptor_data_get (tmp);
7084 : : }
7085 : 492 : tmp = convert (TREE_TYPE (parm), tmp);
7086 : 492 : gfc_add_modify (&init, parm, tmp);
7087 : : }
7088 : 6830 : stmt = gfc_finish_block (&init);
7089 : :
7090 : 6830 : gfc_restore_backend_locus (&loc);
7091 : :
7092 : : /* Add the initialization code to the start of the function. */
7093 : :
7094 : 6830 : if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
7095 : : || sym->attr.optional
7096 : 6830 : || sym->attr.not_always_present)
7097 : : {
7098 : 550 : tree nullify;
7099 : 550 : if (TREE_CODE (parm) != PARM_DECL)
7100 : 103 : nullify = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7101 : : parm, null_pointer_node);
7102 : : else
7103 : 447 : nullify = build_empty_stmt (input_location);
7104 : 550 : tmp = gfc_conv_expr_present (sym, true);
7105 : 550 : stmt = build3_v (COND_EXPR, tmp, stmt, nullify);
7106 : : }
7107 : :
7108 : 6830 : gfc_add_init_cleanup (block, stmt, NULL_TREE);
7109 : 6830 : }
7110 : :
7111 : :
7112 : : /* Modify the descriptor of an array parameter so that it has the
7113 : : correct lower bound. Also move the upper bound accordingly.
7114 : : If the array is not packed, it will be copied into a temporary.
7115 : : For each dimension we set the new lower and upper bounds. Then we copy the
7116 : : stride and calculate the offset for this dimension. We also work out
7117 : : what the stride of a packed array would be, and see it the two match.
7118 : : If the array need repacking, we set the stride to the values we just
7119 : : calculated, recalculate the offset and copy the array data.
7120 : : Code is also added to copy the data back at the end of the function.
7121 : : */
7122 : :
7123 : : void
7124 : 11682 : gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc,
7125 : : gfc_wrapped_block * block)
7126 : : {
7127 : 11682 : tree size;
7128 : 11682 : tree type;
7129 : 11682 : tree offset;
7130 : 11682 : locus loc;
7131 : 11682 : stmtblock_t init;
7132 : 11682 : tree stmtInit, stmtCleanup;
7133 : 11682 : tree lbound;
7134 : 11682 : tree ubound;
7135 : 11682 : tree dubound;
7136 : 11682 : tree dlbound;
7137 : 11682 : tree dumdesc;
7138 : 11682 : tree tmp;
7139 : 11682 : tree stride, stride2;
7140 : 11682 : tree stmt_packed;
7141 : 11682 : tree stmt_unpacked;
7142 : 11682 : tree partial;
7143 : 11682 : gfc_se se;
7144 : 11682 : int n;
7145 : 11682 : int checkparm;
7146 : 11682 : int no_repack;
7147 : 11682 : bool optional_arg;
7148 : 11682 : gfc_array_spec *as;
7149 : 11682 : bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
7150 : :
7151 : : /* Do nothing for pointer and allocatable arrays. */
7152 : 11682 : if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
7153 : 11585 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
7154 : 11585 : || sym->attr.allocatable
7155 : 11464 : || (is_classarray && CLASS_DATA (sym)->attr.allocatable))
7156 : 5549 : return;
7157 : :
7158 : 661 : if ((!is_classarray
7159 : 661 : || (is_classarray && CLASS_DATA (sym)->as->type == AS_EXPLICIT))
7160 : 10964 : && sym->attr.dummy && !sym->attr.elemental && gfc_is_nodesc_array (sym))
7161 : : {
7162 : 5331 : gfc_trans_g77_array (sym, block);
7163 : 5331 : return;
7164 : : }
7165 : :
7166 : 6133 : loc.nextc = NULL;
7167 : 6133 : gfc_save_backend_locus (&loc);
7168 : : /* loc.nextc is not set by save_backend_locus but the location routines
7169 : : depend on it. */
7170 : 6133 : if (loc.nextc == NULL)
7171 : 6133 : loc.nextc = loc.lb->line;
7172 : 6133 : gfc_set_backend_locus (&sym->declared_at);
7173 : :
7174 : : /* Descriptor type. */
7175 : 6133 : type = TREE_TYPE (tmpdesc);
7176 : 6133 : gcc_assert (GFC_ARRAY_TYPE_P (type));
7177 : 6133 : dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
7178 : 6133 : if (is_classarray)
7179 : : /* For a class array the dummy array descriptor is in the _class
7180 : : component. */
7181 : 519 : dumdesc = gfc_class_data_get (dumdesc);
7182 : : else
7183 : 5614 : dumdesc = build_fold_indirect_ref_loc (input_location, dumdesc);
7184 : 6133 : as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
7185 : 6133 : gfc_start_block (&init);
7186 : :
7187 : 6133 : if (sym->ts.type == BT_CHARACTER
7188 : 757 : && VAR_P (sym->ts.u.cl->backend_decl))
7189 : 87 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7190 : :
7191 : : /* TODO: Fix the exclusion of class arrays from extent checking. */
7192 : 980 : checkparm = (as->type == AS_EXPLICIT && !is_classarray
7193 : 7094 : && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS));
7194 : :
7195 : 6133 : no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
7196 : 6132 : || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
7197 : :
7198 : 6133 : if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
7199 : : {
7200 : : /* For non-constant shape arrays we only check if the first dimension
7201 : : is contiguous. Repacking higher dimensions wouldn't gain us
7202 : : anything as we still don't know the array stride. */
7203 : 1 : partial = gfc_create_var (logical_type_node, "partial");
7204 : 1 : TREE_USED (partial) = 1;
7205 : 1 : tmp = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
7206 : 1 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
7207 : : gfc_index_one_node);
7208 : 1 : gfc_add_modify (&init, partial, tmp);
7209 : : }
7210 : : else
7211 : : partial = NULL_TREE;
7212 : :
7213 : : /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
7214 : : here, however I think it does the right thing. */
7215 : 6133 : if (no_repack)
7216 : : {
7217 : : /* Set the first stride. */
7218 : 6131 : stride = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
7219 : 6131 : stride = gfc_evaluate_now (stride, &init);
7220 : :
7221 : 6131 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7222 : : stride, gfc_index_zero_node);
7223 : 6131 : tmp = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
7224 : : tmp, gfc_index_one_node, stride);
7225 : 6131 : stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
7226 : 6131 : gfc_add_modify (&init, stride, tmp);
7227 : :
7228 : : /* Allow the user to disable array repacking. */
7229 : 6131 : stmt_unpacked = NULL_TREE;
7230 : : }
7231 : : else
7232 : : {
7233 : 2 : gcc_assert (integer_onep (GFC_TYPE_ARRAY_STRIDE (type, 0)));
7234 : : /* A library call to repack the array if necessary. */
7235 : 2 : tmp = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
7236 : 2 : stmt_unpacked = build_call_expr_loc (input_location,
7237 : : gfor_fndecl_in_pack, 1, tmp);
7238 : :
7239 : 2 : stride = gfc_index_one_node;
7240 : :
7241 : 2 : if (warn_array_temporaries)
7242 : 1 : gfc_warning (OPT_Warray_temporaries,
7243 : : "Creating array temporary at %L", &loc);
7244 : : }
7245 : :
7246 : : /* This is for the case where the array data is used directly without
7247 : : calling the repack function. */
7248 : 6133 : if (no_repack || partial != NULL_TREE)
7249 : 6132 : stmt_packed = gfc_conv_descriptor_data_get (dumdesc);
7250 : : else
7251 : : stmt_packed = NULL_TREE;
7252 : :
7253 : : /* Assign the data pointer. */
7254 : 6133 : if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
7255 : : {
7256 : : /* Don't repack unknown shape arrays when the first stride is 1. */
7257 : 1 : tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (stmt_packed),
7258 : : partial, stmt_packed, stmt_unpacked);
7259 : : }
7260 : : else
7261 : 6132 : tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
7262 : 6133 : gfc_add_modify (&init, tmpdesc, fold_convert (type, tmp));
7263 : :
7264 : 6133 : offset = gfc_index_zero_node;
7265 : 6133 : size = gfc_index_one_node;
7266 : :
7267 : : /* Evaluate the bounds of the array. */
7268 : 14132 : for (n = 0; n < as->rank; n++)
7269 : : {
7270 : 7999 : if (checkparm || !as->upper[n])
7271 : : {
7272 : : /* Get the bounds of the actual parameter. */
7273 : 6784 : dubound = gfc_conv_descriptor_ubound_get (dumdesc, gfc_rank_cst[n]);
7274 : 6784 : dlbound = gfc_conv_descriptor_lbound_get (dumdesc, gfc_rank_cst[n]);
7275 : : }
7276 : : else
7277 : : {
7278 : : dubound = NULL_TREE;
7279 : : dlbound = NULL_TREE;
7280 : : }
7281 : :
7282 : 7999 : lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
7283 : 7999 : if (!INTEGER_CST_P (lbound))
7284 : : {
7285 : 45 : gfc_init_se (&se, NULL);
7286 : 45 : gfc_conv_expr_type (&se, as->lower[n],
7287 : : gfc_array_index_type);
7288 : 45 : gfc_add_block_to_block (&init, &se.pre);
7289 : 45 : gfc_add_modify (&init, lbound, se.expr);
7290 : : }
7291 : :
7292 : 7999 : ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
7293 : : /* Set the desired upper bound. */
7294 : 7999 : if (as->upper[n])
7295 : : {
7296 : : /* We know what we want the upper bound to be. */
7297 : 1273 : if (!INTEGER_CST_P (ubound))
7298 : : {
7299 : 617 : gfc_init_se (&se, NULL);
7300 : 617 : gfc_conv_expr_type (&se, as->upper[n],
7301 : : gfc_array_index_type);
7302 : 617 : gfc_add_block_to_block (&init, &se.pre);
7303 : 617 : gfc_add_modify (&init, ubound, se.expr);
7304 : : }
7305 : :
7306 : : /* Check the sizes match. */
7307 : 1273 : if (checkparm)
7308 : : {
7309 : : /* Check (ubound(a) - lbound(a) == ubound(b) - lbound(b)). */
7310 : 58 : char * msg;
7311 : 58 : tree temp;
7312 : :
7313 : 58 : temp = fold_build2_loc (input_location, MINUS_EXPR,
7314 : : gfc_array_index_type, ubound, lbound);
7315 : 58 : temp = fold_build2_loc (input_location, PLUS_EXPR,
7316 : : gfc_array_index_type,
7317 : : gfc_index_one_node, temp);
7318 : 58 : stride2 = fold_build2_loc (input_location, MINUS_EXPR,
7319 : : gfc_array_index_type, dubound,
7320 : : dlbound);
7321 : 58 : stride2 = fold_build2_loc (input_location, PLUS_EXPR,
7322 : : gfc_array_index_type,
7323 : : gfc_index_one_node, stride2);
7324 : 58 : tmp = fold_build2_loc (input_location, NE_EXPR,
7325 : : gfc_array_index_type, temp, stride2);
7326 : 58 : msg = xasprintf ("Dimension %d of array '%s' has extent "
7327 : : "%%ld instead of %%ld", n+1, sym->name);
7328 : :
7329 : 58 : gfc_trans_runtime_check (true, false, tmp, &init, &loc, msg,
7330 : : fold_convert (long_integer_type_node, temp),
7331 : : fold_convert (long_integer_type_node, stride2));
7332 : :
7333 : 58 : free (msg);
7334 : : }
7335 : : }
7336 : : else
7337 : : {
7338 : : /* For assumed shape arrays move the upper bound by the same amount
7339 : : as the lower bound. */
7340 : 6726 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7341 : : gfc_array_index_type, dubound, dlbound);
7342 : 6726 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7343 : : gfc_array_index_type, tmp, lbound);
7344 : 6726 : gfc_add_modify (&init, ubound, tmp);
7345 : : }
7346 : : /* The offset of this dimension. offset = offset - lbound * stride. */
7347 : 7999 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7348 : : lbound, stride);
7349 : 7999 : offset = fold_build2_loc (input_location, MINUS_EXPR,
7350 : : gfc_array_index_type, offset, tmp);
7351 : :
7352 : : /* The size of this dimension, and the stride of the next. */
7353 : 7999 : if (n + 1 < as->rank)
7354 : : {
7355 : 1866 : stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
7356 : :
7357 : 1866 : if (no_repack || partial != NULL_TREE)
7358 : 1865 : stmt_unpacked =
7359 : 1865 : gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[n+1]);
7360 : :
7361 : : /* Figure out the stride if not a known constant. */
7362 : 1866 : if (!INTEGER_CST_P (stride))
7363 : : {
7364 : 1865 : if (no_repack)
7365 : : stmt_packed = NULL_TREE;
7366 : : else
7367 : : {
7368 : : /* Calculate stride = size * (ubound + 1 - lbound). */
7369 : 0 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7370 : : gfc_array_index_type,
7371 : : gfc_index_one_node, lbound);
7372 : 0 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7373 : : gfc_array_index_type, ubound, tmp);
7374 : 0 : size = fold_build2_loc (input_location, MULT_EXPR,
7375 : : gfc_array_index_type, size, tmp);
7376 : 0 : stmt_packed = size;
7377 : : }
7378 : :
7379 : : /* Assign the stride. */
7380 : 1865 : if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
7381 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR,
7382 : : gfc_array_index_type, partial,
7383 : : stmt_unpacked, stmt_packed);
7384 : : else
7385 : 1865 : tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
7386 : 1865 : gfc_add_modify (&init, stride, tmp);
7387 : : }
7388 : : }
7389 : : else
7390 : : {
7391 : 6133 : stride = GFC_TYPE_ARRAY_SIZE (type);
7392 : :
7393 : 6133 : if (stride && !INTEGER_CST_P (stride))
7394 : : {
7395 : : /* Calculate size = stride * (ubound + 1 - lbound). */
7396 : 6132 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7397 : : gfc_array_index_type,
7398 : : gfc_index_one_node, lbound);
7399 : 6132 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7400 : : gfc_array_index_type,
7401 : : ubound, tmp);
7402 : 18396 : tmp = fold_build2_loc (input_location, MULT_EXPR,
7403 : : gfc_array_index_type,
7404 : 6132 : GFC_TYPE_ARRAY_STRIDE (type, n), tmp);
7405 : 6132 : gfc_add_modify (&init, stride, tmp);
7406 : : }
7407 : : }
7408 : : }
7409 : :
7410 : 6133 : gfc_trans_array_cobounds (type, &init, sym);
7411 : :
7412 : : /* Set the offset. */
7413 : 6133 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7414 : 6131 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7415 : :
7416 : 6133 : gfc_trans_vla_type_sizes (sym, &init);
7417 : :
7418 : 6133 : stmtInit = gfc_finish_block (&init);
7419 : :
7420 : : /* Only do the entry/initialization code if the arg is present. */
7421 : 6133 : dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
7422 : 6133 : optional_arg = (sym->attr.optional
7423 : 6133 : || (sym->ns->proc_name->attr.entry_master
7424 : 79 : && sym->attr.dummy));
7425 : : if (optional_arg)
7426 : : {
7427 : 621 : tree zero_init = fold_convert (TREE_TYPE (tmpdesc), null_pointer_node);
7428 : 621 : zero_init = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7429 : : tmpdesc, zero_init);
7430 : 621 : tmp = gfc_conv_expr_present (sym, true);
7431 : 621 : stmtInit = build3_v (COND_EXPR, tmp, stmtInit, zero_init);
7432 : : }
7433 : :
7434 : : /* Cleanup code. */
7435 : 6133 : if (no_repack)
7436 : : stmtCleanup = NULL_TREE;
7437 : : else
7438 : : {
7439 : 2 : stmtblock_t cleanup;
7440 : 2 : gfc_start_block (&cleanup);
7441 : :
7442 : 2 : if (sym->attr.intent != INTENT_IN)
7443 : : {
7444 : : /* Copy the data back. */
7445 : 2 : tmp = build_call_expr_loc (input_location,
7446 : : gfor_fndecl_in_unpack, 2, dumdesc, tmpdesc);
7447 : 2 : gfc_add_expr_to_block (&cleanup, tmp);
7448 : : }
7449 : :
7450 : : /* Free the temporary. */
7451 : 2 : tmp = gfc_call_free (tmpdesc);
7452 : 2 : gfc_add_expr_to_block (&cleanup, tmp);
7453 : :
7454 : 2 : stmtCleanup = gfc_finish_block (&cleanup);
7455 : :
7456 : : /* Only do the cleanup if the array was repacked. */
7457 : 2 : if (is_classarray)
7458 : : /* For a class array the dummy array descriptor is in the _class
7459 : : component. */
7460 : 1 : tmp = gfc_class_data_get (dumdesc);
7461 : : else
7462 : 1 : tmp = build_fold_indirect_ref_loc (input_location, dumdesc);
7463 : 2 : tmp = gfc_conv_descriptor_data_get (tmp);
7464 : 2 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
7465 : : tmp, tmpdesc);
7466 : 2 : stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
7467 : : build_empty_stmt (input_location));
7468 : :
7469 : 2 : if (optional_arg)
7470 : : {
7471 : 0 : tmp = gfc_conv_expr_present (sym);
7472 : 0 : stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
7473 : : build_empty_stmt (input_location));
7474 : : }
7475 : : }
7476 : :
7477 : : /* We don't need to free any memory allocated by internal_pack as it will
7478 : : be freed at the end of the function by pop_context. */
7479 : 6133 : gfc_add_init_cleanup (block, stmtInit, stmtCleanup);
7480 : :
7481 : 6133 : gfc_restore_backend_locus (&loc);
7482 : : }
7483 : :
7484 : :
7485 : : /* Calculate the overall offset, including subreferences. */
7486 : : void
7487 : 51442 : gfc_get_dataptr_offset (stmtblock_t *block, tree parm, tree desc, tree offset,
7488 : : bool subref, gfc_expr *expr)
7489 : : {
7490 : 51442 : tree tmp;
7491 : 51442 : tree field;
7492 : 51442 : tree stride;
7493 : 51442 : tree index;
7494 : 51442 : gfc_ref *ref;
7495 : 51442 : gfc_se start;
7496 : 51442 : int n;
7497 : :
7498 : : /* If offset is NULL and this is not a subreferenced array, there is
7499 : : nothing to do. */
7500 : 51442 : if (offset == NULL_TREE)
7501 : : {
7502 : 940 : if (subref)
7503 : 115 : offset = gfc_index_zero_node;
7504 : : else
7505 : 825 : return;
7506 : : }
7507 : :
7508 : 50617 : tmp = build_array_ref (desc, offset, NULL, NULL);
7509 : :
7510 : : /* Offset the data pointer for pointer assignments from arrays with
7511 : : subreferences; e.g. my_integer => my_type(:)%integer_component. */
7512 : 50617 : if (subref)
7513 : : {
7514 : : /* Go past the array reference. */
7515 : 737 : for (ref = expr->ref; ref; ref = ref->next)
7516 : 737 : if (ref->type == REF_ARRAY &&
7517 : 651 : ref->u.ar.type != AR_ELEMENT)
7518 : : {
7519 : 627 : ref = ref->next;
7520 : 627 : break;
7521 : : }
7522 : :
7523 : : /* Calculate the offset for each subsequent subreference. */
7524 : 1226 : for (; ref; ref = ref->next)
7525 : : {
7526 : 599 : switch (ref->type)
7527 : : {
7528 : 264 : case REF_COMPONENT:
7529 : 264 : field = ref->u.c.component->backend_decl;
7530 : 264 : gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
7531 : 528 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
7532 : 264 : TREE_TYPE (field),
7533 : : tmp, field, NULL_TREE);
7534 : 264 : break;
7535 : :
7536 : 299 : case REF_SUBSTRING:
7537 : 299 : gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
7538 : 299 : gfc_init_se (&start, NULL);
7539 : 299 : gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
7540 : 299 : gfc_add_block_to_block (block, &start.pre);
7541 : 299 : tmp = gfc_build_array_ref (tmp, start.expr, NULL);
7542 : 299 : break;
7543 : :
7544 : 24 : case REF_ARRAY:
7545 : 24 : gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE
7546 : : && ref->u.ar.type == AR_ELEMENT);
7547 : :
7548 : : /* TODO - Add bounds checking. */
7549 : 24 : stride = gfc_index_one_node;
7550 : 24 : index = gfc_index_zero_node;
7551 : 55 : for (n = 0; n < ref->u.ar.dimen; n++)
7552 : : {
7553 : 31 : tree itmp;
7554 : 31 : tree jtmp;
7555 : :
7556 : : /* Update the index. */
7557 : 31 : gfc_init_se (&start, NULL);
7558 : 31 : gfc_conv_expr_type (&start, ref->u.ar.start[n], gfc_array_index_type);
7559 : 31 : itmp = gfc_evaluate_now (start.expr, block);
7560 : 31 : gfc_init_se (&start, NULL);
7561 : 31 : gfc_conv_expr_type (&start, ref->u.ar.as->lower[n], gfc_array_index_type);
7562 : 31 : jtmp = gfc_evaluate_now (start.expr, block);
7563 : 31 : itmp = fold_build2_loc (input_location, MINUS_EXPR,
7564 : : gfc_array_index_type, itmp, jtmp);
7565 : 31 : itmp = fold_build2_loc (input_location, MULT_EXPR,
7566 : : gfc_array_index_type, itmp, stride);
7567 : 31 : index = fold_build2_loc (input_location, PLUS_EXPR,
7568 : : gfc_array_index_type, itmp, index);
7569 : 31 : index = gfc_evaluate_now (index, block);
7570 : :
7571 : : /* Update the stride. */
7572 : 31 : gfc_init_se (&start, NULL);
7573 : 31 : gfc_conv_expr_type (&start, ref->u.ar.as->upper[n], gfc_array_index_type);
7574 : 31 : itmp = fold_build2_loc (input_location, MINUS_EXPR,
7575 : : gfc_array_index_type, start.expr,
7576 : : jtmp);
7577 : 31 : itmp = fold_build2_loc (input_location, PLUS_EXPR,
7578 : : gfc_array_index_type,
7579 : : gfc_index_one_node, itmp);
7580 : 31 : stride = fold_build2_loc (input_location, MULT_EXPR,
7581 : : gfc_array_index_type, stride, itmp);
7582 : 31 : stride = gfc_evaluate_now (stride, block);
7583 : : }
7584 : :
7585 : : /* Apply the index to obtain the array element. */
7586 : 24 : tmp = gfc_build_array_ref (tmp, index, NULL);
7587 : 24 : break;
7588 : :
7589 : 12 : case REF_INQUIRY:
7590 : 12 : switch (ref->u.i)
7591 : : {
7592 : 6 : case INQUIRY_RE:
7593 : 12 : tmp = fold_build1_loc (input_location, REALPART_EXPR,
7594 : 6 : TREE_TYPE (TREE_TYPE (tmp)), tmp);
7595 : 6 : break;
7596 : :
7597 : 6 : case INQUIRY_IM:
7598 : 12 : tmp = fold_build1_loc (input_location, IMAGPART_EXPR,
7599 : 6 : TREE_TYPE (TREE_TYPE (tmp)), tmp);
7600 : 6 : break;
7601 : :
7602 : : default:
7603 : : break;
7604 : : }
7605 : : break;
7606 : :
7607 : 0 : default:
7608 : 0 : gcc_unreachable ();
7609 : 599 : break;
7610 : : }
7611 : : }
7612 : : }
7613 : :
7614 : : /* Set the target data pointer. */
7615 : 50617 : offset = gfc_build_addr_expr (gfc_array_dataptr_type (desc), tmp);
7616 : :
7617 : : /* Check for optional dummy argument being present. Arguments of BIND(C)
7618 : : procedures are excepted here since they are handled differently. */
7619 : 50617 : if (expr->expr_type == EXPR_VARIABLE
7620 : 45276 : && expr->symtree->n.sym->attr.dummy
7621 : 45276 : && expr->symtree->n.sym->attr.optional
7622 : 51584 : && !is_CFI_desc (NULL, expr))
7623 : 1574 : offset = build3_loc (input_location, COND_EXPR, TREE_TYPE (offset),
7624 : 787 : gfc_conv_expr_present (expr->symtree->n.sym), offset,
7625 : 787 : fold_convert (TREE_TYPE (offset), gfc_index_zero_node));
7626 : :
7627 : 50617 : gfc_conv_descriptor_data_set (block, parm, offset);
7628 : : }
7629 : :
7630 : :
7631 : : /* gfc_conv_expr_descriptor needs the string length an expression
7632 : : so that the size of the temporary can be obtained. This is done
7633 : : by adding up the string lengths of all the elements in the
7634 : : expression. Function with non-constant expressions have their
7635 : : string lengths mapped onto the actual arguments using the
7636 : : interface mapping machinery in trans-expr.cc. */
7637 : : static void
7638 : 1488 : get_array_charlen (gfc_expr *expr, gfc_se *se)
7639 : : {
7640 : 1488 : gfc_interface_mapping mapping;
7641 : 1488 : gfc_formal_arglist *formal;
7642 : 1488 : gfc_actual_arglist *arg;
7643 : 1488 : gfc_se tse;
7644 : 1488 : gfc_expr *e;
7645 : :
7646 : 1488 : if (expr->ts.u.cl->length
7647 : 1488 : && gfc_is_constant_expr (expr->ts.u.cl->length))
7648 : : {
7649 : 1144 : if (!expr->ts.u.cl->backend_decl)
7650 : 437 : gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
7651 : 1276 : return;
7652 : : }
7653 : :
7654 : 344 : switch (expr->expr_type)
7655 : : {
7656 : 130 : case EXPR_ARRAY:
7657 : :
7658 : : /* This is somewhat brutal. The expression for the first
7659 : : element of the array is evaluated and assigned to a
7660 : : new string length for the original expression. */
7661 : 130 : e = gfc_constructor_first (expr->value.constructor)->expr;
7662 : :
7663 : 130 : gfc_init_se (&tse, NULL);
7664 : :
7665 : : /* Avoid evaluating trailing array references since all we need is
7666 : : the string length. */
7667 : 130 : if (e->rank)
7668 : 38 : tse.descriptor_only = 1;
7669 : 130 : if (e->rank && e->expr_type != EXPR_VARIABLE)
7670 : 1 : gfc_conv_expr_descriptor (&tse, e);
7671 : : else
7672 : 129 : gfc_conv_expr (&tse, e);
7673 : :
7674 : 130 : gfc_add_block_to_block (&se->pre, &tse.pre);
7675 : 130 : gfc_add_block_to_block (&se->post, &tse.post);
7676 : :
7677 : 130 : if (!expr->ts.u.cl->backend_decl || !VAR_P (expr->ts.u.cl->backend_decl))
7678 : : {
7679 : 87 : expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7680 : 87 : expr->ts.u.cl->backend_decl =
7681 : 87 : gfc_create_var (gfc_charlen_type_node, "sln");
7682 : : }
7683 : :
7684 : 130 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
7685 : : tse.string_length);
7686 : :
7687 : : /* Make sure that deferred length components point to the hidden
7688 : : string_length component. */
7689 : 130 : if (TREE_CODE (tse.expr) == COMPONENT_REF
7690 : 25 : && TREE_CODE (tse.string_length) == COMPONENT_REF
7691 : 149 : && TREE_OPERAND (tse.expr, 0) == TREE_OPERAND (tse.string_length, 0))
7692 : 19 : e->ts.u.cl->backend_decl = expr->ts.u.cl->backend_decl;
7693 : :
7694 : : return;
7695 : :
7696 : 90 : case EXPR_OP:
7697 : 90 : get_array_charlen (expr->value.op.op1, se);
7698 : :
7699 : : /* For parentheses the expression ts.u.cl should be identical. */
7700 : 90 : if (expr->value.op.op == INTRINSIC_PARENTHESES)
7701 : : {
7702 : 2 : if (expr->value.op.op1->ts.u.cl != expr->ts.u.cl)
7703 : 2 : expr->ts.u.cl->backend_decl
7704 : 2 : = expr->value.op.op1->ts.u.cl->backend_decl;
7705 : 2 : return;
7706 : : }
7707 : :
7708 : 176 : expr->ts.u.cl->backend_decl =
7709 : 88 : gfc_create_var (gfc_charlen_type_node, "sln");
7710 : :
7711 : 88 : if (expr->value.op.op2)
7712 : : {
7713 : 88 : get_array_charlen (expr->value.op.op2, se);
7714 : :
7715 : 88 : gcc_assert (expr->value.op.op == INTRINSIC_CONCAT);
7716 : :
7717 : : /* Add the string lengths and assign them to the expression
7718 : : string length backend declaration. */
7719 : 88 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
7720 : : fold_build2_loc (input_location, PLUS_EXPR,
7721 : : gfc_charlen_type_node,
7722 : 88 : expr->value.op.op1->ts.u.cl->backend_decl,
7723 : 88 : expr->value.op.op2->ts.u.cl->backend_decl));
7724 : : }
7725 : : else
7726 : 0 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
7727 : 0 : expr->value.op.op1->ts.u.cl->backend_decl);
7728 : : break;
7729 : :
7730 : 43 : case EXPR_FUNCTION:
7731 : 43 : if (expr->value.function.esym == NULL
7732 : 37 : || expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7733 : : {
7734 : 6 : gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
7735 : 6 : break;
7736 : : }
7737 : :
7738 : : /* Map expressions involving the dummy arguments onto the actual
7739 : : argument expressions. */
7740 : 37 : gfc_init_interface_mapping (&mapping);
7741 : 37 : formal = gfc_sym_get_dummy_args (expr->symtree->n.sym);
7742 : 37 : arg = expr->value.function.actual;
7743 : :
7744 : : /* Set se = NULL in the calls to the interface mapping, to suppress any
7745 : : backend stuff. */
7746 : 113 : for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
7747 : : {
7748 : 38 : if (!arg->expr)
7749 : 0 : continue;
7750 : 38 : if (formal->sym)
7751 : 38 : gfc_add_interface_mapping (&mapping, formal->sym, NULL, arg->expr);
7752 : : }
7753 : :
7754 : 37 : gfc_init_se (&tse, NULL);
7755 : :
7756 : : /* Build the expression for the character length and convert it. */
7757 : 37 : gfc_apply_interface_mapping (&mapping, &tse, expr->ts.u.cl->length);
7758 : :
7759 : 37 : gfc_add_block_to_block (&se->pre, &tse.pre);
7760 : 37 : gfc_add_block_to_block (&se->post, &tse.post);
7761 : 37 : tse.expr = fold_convert (gfc_charlen_type_node, tse.expr);
7762 : 74 : tse.expr = fold_build2_loc (input_location, MAX_EXPR,
7763 : 37 : TREE_TYPE (tse.expr), tse.expr,
7764 : 37 : build_zero_cst (TREE_TYPE (tse.expr)));
7765 : 37 : expr->ts.u.cl->backend_decl = tse.expr;
7766 : 37 : gfc_free_interface_mapping (&mapping);
7767 : 37 : break;
7768 : :
7769 : 81 : default:
7770 : 81 : gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
7771 : 81 : break;
7772 : : }
7773 : : }
7774 : :
7775 : :
7776 : : /* Helper function to check dimensions. */
7777 : : static bool
7778 : 144 : transposed_dims (gfc_ss *ss)
7779 : : {
7780 : 144 : int n;
7781 : :
7782 : 142634 : for (n = 0; n < ss->dimen; n++)
7783 : 71216 : if (ss->dim[n] != n)
7784 : : return true;
7785 : : return false;
7786 : : }
7787 : :
7788 : :
7789 : : /* Convert the last ref of a scalar coarray from an AR_ELEMENT to an
7790 : : AR_FULL, suitable for the scalarizer. */
7791 : :
7792 : : static gfc_ss *
7793 : 931 : walk_coarray (gfc_expr *e)
7794 : : {
7795 : 931 : gfc_ss *ss;
7796 : :
7797 : 931 : gcc_assert (gfc_get_corank (e) > 0);
7798 : :
7799 : 931 : ss = gfc_walk_expr (e);
7800 : :
7801 : : /* Fix scalar coarray. */
7802 : 931 : if (ss == gfc_ss_terminator)
7803 : : {
7804 : 202 : gfc_ref *ref;
7805 : :
7806 : 202 : ref = e->ref;
7807 : 303 : while (ref)
7808 : : {
7809 : 303 : if (ref->type == REF_ARRAY
7810 : 202 : && ref->u.ar.codimen > 0)
7811 : : break;
7812 : :
7813 : 101 : ref = ref->next;
7814 : : }
7815 : :
7816 : 202 : gcc_assert (ref != NULL);
7817 : 202 : if (ref->u.ar.type == AR_ELEMENT)
7818 : 202 : ref->u.ar.type = AR_SECTION;
7819 : 202 : ss = gfc_reverse_ss (gfc_walk_array_ref (ss, e, ref));
7820 : : }
7821 : :
7822 : 931 : return ss;
7823 : : }
7824 : :
7825 : : gfc_array_spec *
7826 : 1721 : get_coarray_as (const gfc_expr *e)
7827 : : {
7828 : 1721 : gfc_array_spec *as;
7829 : 1721 : gfc_symbol *sym = e->symtree->n.sym;
7830 : 1721 : gfc_component *comp;
7831 : :
7832 : 1721 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.codimension)
7833 : 529 : as = CLASS_DATA (sym)->as;
7834 : 1192 : else if (sym->attr.codimension)
7835 : 1146 : as = sym->as;
7836 : : else
7837 : : as = nullptr;
7838 : :
7839 : 4047 : for (gfc_ref *ref = e->ref; ref; ref = ref->next)
7840 : : {
7841 : 2326 : switch (ref->type)
7842 : : {
7843 : 605 : case REF_COMPONENT:
7844 : 605 : comp = ref->u.c.component;
7845 : 605 : if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.codimension)
7846 : 18 : as = CLASS_DATA (comp)->as;
7847 : 587 : else if (comp->ts.type != BT_CLASS && comp->attr.codimension)
7848 : 575 : as = comp->as;
7849 : : break;
7850 : :
7851 : : case REF_ARRAY:
7852 : : case REF_SUBSTRING:
7853 : : case REF_INQUIRY:
7854 : : break;
7855 : : }
7856 : : }
7857 : :
7858 : 1721 : return as;
7859 : : }
7860 : :
7861 : : bool
7862 : 121004 : is_explicit_coarray (gfc_expr *expr)
7863 : : {
7864 : 121004 : if (!gfc_is_coarray (expr))
7865 : : return false;
7866 : :
7867 : 1721 : gfc_array_spec *cas = get_coarray_as (expr);
7868 : 1721 : return cas && cas->cotype == AS_EXPLICIT;
7869 : : }
7870 : :
7871 : : /* Convert an array for passing as an actual argument. Expressions and
7872 : : vector subscripts are evaluated and stored in a temporary, which is then
7873 : : passed. For whole arrays the descriptor is passed. For array sections
7874 : : a modified copy of the descriptor is passed, but using the original data.
7875 : :
7876 : : This function is also used for array pointer assignments, and there
7877 : : are three cases:
7878 : :
7879 : : - se->want_pointer && !se->direct_byref
7880 : : EXPR is an actual argument. On exit, se->expr contains a
7881 : : pointer to the array descriptor.
7882 : :
7883 : : - !se->want_pointer && !se->direct_byref
7884 : : EXPR is an actual argument to an intrinsic function or the
7885 : : left-hand side of a pointer assignment. On exit, se->expr
7886 : : contains the descriptor for EXPR.
7887 : :
7888 : : - !se->want_pointer && se->direct_byref
7889 : : EXPR is the right-hand side of a pointer assignment and
7890 : : se->expr is the descriptor for the previously-evaluated
7891 : : left-hand side. The function creates an assignment from
7892 : : EXPR to se->expr.
7893 : :
7894 : :
7895 : : The se->force_tmp flag disables the non-copying descriptor optimization
7896 : : that is used for transpose. It may be used in cases where there is an
7897 : : alias between the transpose argument and another argument in the same
7898 : : function call. */
7899 : :
7900 : : void
7901 : 133385 : gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr)
7902 : : {
7903 : 133385 : gfc_ss *ss;
7904 : 133385 : gfc_ss_type ss_type;
7905 : 133385 : gfc_ss_info *ss_info;
7906 : 133385 : gfc_loopinfo loop;
7907 : 133385 : gfc_array_info *info;
7908 : 133385 : int need_tmp;
7909 : 133385 : int n;
7910 : 133385 : tree tmp;
7911 : 133385 : tree desc;
7912 : 133385 : stmtblock_t block;
7913 : 133385 : tree start;
7914 : 133385 : int full;
7915 : 133385 : bool subref_array_target = false;
7916 : 133385 : bool deferred_array_component = false;
7917 : 133385 : bool substr = false;
7918 : 133385 : gfc_expr *arg, *ss_expr;
7919 : :
7920 : 133385 : if (se->want_coarray)
7921 : 931 : ss = walk_coarray (expr);
7922 : : else
7923 : 132454 : ss = gfc_walk_expr (expr);
7924 : :
7925 : 133385 : gcc_assert (ss != NULL);
7926 : 133385 : gcc_assert (ss != gfc_ss_terminator);
7927 : :
7928 : 133385 : ss_info = ss->info;
7929 : 133385 : ss_type = ss_info->type;
7930 : 133385 : ss_expr = ss_info->expr;
7931 : :
7932 : : /* Special case: TRANSPOSE which needs no temporary. */
7933 : 137239 : while (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym
7934 : 136975 : && (arg = gfc_get_noncopying_intrinsic_argument (expr)) != NULL)
7935 : : {
7936 : : /* This is a call to transpose which has already been handled by the
7937 : : scalarizer, so that we just need to get its argument's descriptor. */
7938 : 427 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
7939 : 427 : expr = expr->value.function.actual->expr;
7940 : : }
7941 : :
7942 : 133385 : if (!se->direct_byref)
7943 : 256375 : se->unlimited_polymorphic = UNLIMITED_POLY (expr);
7944 : :
7945 : : /* Special case things we know we can pass easily. */
7946 : 133385 : switch (expr->expr_type)
7947 : : {
7948 : 121144 : case EXPR_VARIABLE:
7949 : : /* If we have a linear array section, we can pass it directly.
7950 : : Otherwise we need to copy it into a temporary. */
7951 : :
7952 : 121144 : gcc_assert (ss_type == GFC_SS_SECTION);
7953 : 121144 : gcc_assert (ss_expr == expr);
7954 : 121144 : info = &ss_info->data.array;
7955 : :
7956 : : /* Get the descriptor for the array. */
7957 : 121144 : gfc_conv_ss_descriptor (&se->pre, ss, 0);
7958 : 121144 : desc = info->descriptor;
7959 : :
7960 : : /* The charlen backend decl for deferred character components cannot
7961 : : be used because it is fixed at zero. Instead, the hidden string
7962 : : length component is used. */
7963 : 121144 : if (expr->ts.type == BT_CHARACTER
7964 : 18949 : && expr->ts.deferred
7965 : 2599 : && TREE_CODE (desc) == COMPONENT_REF)
7966 : 121144 : deferred_array_component = true;
7967 : :
7968 : 121144 : substr = info->ref && info->ref->next
7969 : 121765 : && info->ref->next->type == REF_SUBSTRING;
7970 : :
7971 : 121144 : subref_array_target = (is_subref_array (expr)
7972 : 121144 : && (se->direct_byref
7973 : 2223 : || expr->ts.type == BT_CHARACTER));
7974 : 121144 : need_tmp = (gfc_ref_needs_temporary_p (expr->ref)
7975 : 121144 : && !subref_array_target);
7976 : :
7977 : 121144 : if (se->force_tmp)
7978 : : need_tmp = 1;
7979 : 121094 : else if (se->force_no_tmp)
7980 : : need_tmp = 0;
7981 : :
7982 : 115458 : if (need_tmp)
7983 : : full = 0;
7984 : 121004 : else if (is_explicit_coarray (expr))
7985 : : full = 0;
7986 : 120374 : else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
7987 : : {
7988 : : /* Create a new descriptor if the array doesn't have one. */
7989 : : full = 0;
7990 : : }
7991 : 75383 : else if (info->ref->u.ar.type == AR_FULL || se->descriptor_only)
7992 : : full = 1;
7993 : 5752 : else if (se->direct_byref)
7994 : : full = 0;
7995 : 5426 : else if (info->ref->u.ar.dimen == 0 && !info->ref->next)
7996 : : full = 1;
7997 : 5320 : else if (info->ref->u.ar.type == AR_SECTION && se->want_pointer)
7998 : : full = 0;
7999 : : else
8000 : 3553 : full = gfc_full_array_ref_p (info->ref, NULL);
8001 : :
8002 : 143273 : if (full && !transposed_dims (ss))
8003 : : {
8004 : 69926 : if (se->direct_byref && !se->byref_noassign)
8005 : : {
8006 : : /* Copy the descriptor for pointer assignments. */
8007 : 928 : gfc_add_modify (&se->pre, se->expr, desc);
8008 : :
8009 : : /* Add any offsets from subreferences. */
8010 : 928 : gfc_get_dataptr_offset (&se->pre, se->expr, desc, NULL_TREE,
8011 : : subref_array_target, expr);
8012 : :
8013 : : /* ....and set the span field. */
8014 : 928 : if (ss_info->expr->ts.type == BT_CHARACTER)
8015 : 141 : tmp = gfc_conv_descriptor_span_get (desc);
8016 : : else
8017 : 787 : tmp = gfc_get_array_span (desc, expr);
8018 : 928 : gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
8019 : : }
8020 : 68998 : else if (se->want_pointer)
8021 : : {
8022 : : /* We pass full arrays directly. This means that pointers and
8023 : : allocatable arrays should also work. */
8024 : 10882 : se->expr = gfc_build_addr_expr (NULL_TREE, desc);
8025 : : }
8026 : : else
8027 : : {
8028 : 58116 : se->expr = desc;
8029 : : }
8030 : :
8031 : 69926 : if (expr->ts.type == BT_CHARACTER && !deferred_array_component)
8032 : 7933 : se->string_length = gfc_get_expr_charlen (expr);
8033 : : /* The ss_info string length is returned set to the value of the
8034 : : hidden string length component. */
8035 : 61751 : else if (deferred_array_component)
8036 : 242 : se->string_length = ss_info->string_length;
8037 : :
8038 : 69926 : se->class_container = ss_info->class_container;
8039 : :
8040 : 69926 : gfc_free_ss_chain (ss);
8041 : 139977 : return;
8042 : : }
8043 : : break;
8044 : :
8045 : 3427 : case EXPR_FUNCTION:
8046 : : /* A transformational function return value will be a temporary
8047 : : array descriptor. We still need to go through the scalarizer
8048 : : to create the descriptor. Elemental functions are handled as
8049 : : arbitrary expressions, i.e. copy to a temporary. */
8050 : :
8051 : 3427 : if (se->direct_byref)
8052 : : {
8053 : 125 : gcc_assert (ss_type == GFC_SS_FUNCTION && ss_expr == expr);
8054 : :
8055 : : /* For pointer assignments pass the descriptor directly. */
8056 : 125 : if (se->ss == NULL)
8057 : 125 : se->ss = ss;
8058 : : else
8059 : 0 : gcc_assert (se->ss == ss);
8060 : :
8061 : 125 : if (!is_pointer_array (se->expr))
8062 : : {
8063 : 119 : tmp = gfc_get_element_type (TREE_TYPE (se->expr));
8064 : 119 : tmp = fold_convert (gfc_array_index_type,
8065 : : size_in_bytes (tmp));
8066 : 119 : gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
8067 : : }
8068 : :
8069 : 125 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
8070 : 125 : gfc_conv_expr (se, expr);
8071 : :
8072 : 125 : gfc_free_ss_chain (ss);
8073 : 125 : return;
8074 : : }
8075 : :
8076 : 3302 : if (ss_expr != expr || ss_type != GFC_SS_FUNCTION)
8077 : : {
8078 : 1935 : if (ss_expr != expr)
8079 : : /* Elemental function. */
8080 : 1218 : gcc_assert ((expr->value.function.esym != NULL
8081 : : && expr->value.function.esym->attr.elemental)
8082 : : || (expr->value.function.isym != NULL
8083 : : && expr->value.function.isym->elemental)
8084 : : || (gfc_expr_attr (expr).proc_pointer
8085 : : && gfc_expr_attr (expr).elemental)
8086 : : || gfc_inline_intrinsic_function_p (expr));
8087 : :
8088 : 1935 : need_tmp = 1;
8089 : 1935 : if (expr->ts.type == BT_CHARACTER
8090 : 35 : && expr->ts.u.cl->length
8091 : 29 : && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8092 : 13 : get_array_charlen (expr, se);
8093 : :
8094 : : info = NULL;
8095 : : }
8096 : : else
8097 : : {
8098 : : /* Transformational function. */
8099 : 1367 : info = &ss_info->data.array;
8100 : 1367 : need_tmp = 0;
8101 : : }
8102 : : break;
8103 : :
8104 : 8126 : case EXPR_ARRAY:
8105 : : /* Constant array constructors don't need a temporary. */
8106 : 8126 : if (ss_type == GFC_SS_CONSTRUCTOR
8107 : 8126 : && expr->ts.type != BT_CHARACTER
8108 : 15083 : && gfc_constant_array_constructor_p (expr->value.constructor))
8109 : : {
8110 : 5328 : need_tmp = 0;
8111 : 5328 : info = &ss_info->data.array;
8112 : : }
8113 : : else
8114 : : {
8115 : : need_tmp = 1;
8116 : : info = NULL;
8117 : : }
8118 : : break;
8119 : :
8120 : : default:
8121 : : /* Something complicated. Copy it into a temporary. */
8122 : : need_tmp = 1;
8123 : : info = NULL;
8124 : : break;
8125 : : }
8126 : :
8127 : : /* If we are creating a temporary, we don't need to bother about aliases
8128 : : anymore. */
8129 : 63334 : if (need_tmp)
8130 : 5561 : se->force_tmp = 0;
8131 : :
8132 : 63334 : gfc_init_loopinfo (&loop);
8133 : :
8134 : : /* Associate the SS with the loop. */
8135 : 63334 : gfc_add_ss_to_loop (&loop, ss);
8136 : :
8137 : : /* Tell the scalarizer not to bother creating loop variables, etc. */
8138 : 63334 : if (!need_tmp)
8139 : 57773 : loop.array_parameter = 1;
8140 : : else
8141 : : /* The right-hand side of a pointer assignment mustn't use a temporary. */
8142 : 5561 : gcc_assert (!se->direct_byref);
8143 : :
8144 : : /* Do we need bounds checking or not? */
8145 : 63334 : ss->no_bounds_check = expr->no_bounds_check;
8146 : :
8147 : : /* Setup the scalarizing loops and bounds. */
8148 : 63334 : gfc_conv_ss_startstride (&loop);
8149 : :
8150 : : /* Add bounds-checking for elemental dimensions. */
8151 : 63334 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !expr->no_bounds_check)
8152 : 6641 : array_bound_check_elemental (se, ss, expr);
8153 : :
8154 : 63334 : if (need_tmp)
8155 : : {
8156 : 5561 : if (expr->ts.type == BT_CHARACTER
8157 : 1367 : && (!expr->ts.u.cl->backend_decl || expr->expr_type == EXPR_ARRAY))
8158 : 1297 : get_array_charlen (expr, se);
8159 : :
8160 : : /* Tell the scalarizer to make a temporary. */
8161 : 5561 : loop.temp_ss = gfc_get_temp_ss (gfc_typenode_for_spec (&expr->ts),
8162 : 5561 : ((expr->ts.type == BT_CHARACTER)
8163 : 1367 : ? expr->ts.u.cl->backend_decl
8164 : : : NULL),
8165 : : loop.dimen);
8166 : :
8167 : 5561 : se->string_length = loop.temp_ss->info->string_length;
8168 : 5561 : gcc_assert (loop.temp_ss->dimen == loop.dimen);
8169 : 5561 : gfc_add_ss_to_loop (&loop, loop.temp_ss);
8170 : : }
8171 : :
8172 : 63334 : gfc_conv_loop_setup (&loop, & expr->where);
8173 : :
8174 : 63334 : if (need_tmp)
8175 : : {
8176 : : /* Copy into a temporary and pass that. We don't need to copy the data
8177 : : back because expressions and vector subscripts must be INTENT_IN. */
8178 : : /* TODO: Optimize passing function return values. */
8179 : 5561 : gfc_se lse;
8180 : 5561 : gfc_se rse;
8181 : 5561 : bool deep_copy;
8182 : :
8183 : : /* Start the copying loops. */
8184 : 5561 : gfc_mark_ss_chain_used (loop.temp_ss, 1);
8185 : 5561 : gfc_mark_ss_chain_used (ss, 1);
8186 : 5561 : gfc_start_scalarized_body (&loop, &block);
8187 : :
8188 : : /* Copy each data element. */
8189 : 5561 : gfc_init_se (&lse, NULL);
8190 : 5561 : gfc_copy_loopinfo_to_se (&lse, &loop);
8191 : 5561 : gfc_init_se (&rse, NULL);
8192 : 5561 : gfc_copy_loopinfo_to_se (&rse, &loop);
8193 : :
8194 : 5561 : lse.ss = loop.temp_ss;
8195 : 5561 : rse.ss = ss;
8196 : :
8197 : 5561 : gfc_conv_tmp_array_ref (&lse);
8198 : 5561 : if (expr->ts.type == BT_CHARACTER)
8199 : : {
8200 : 1367 : gfc_conv_expr (&rse, expr);
8201 : 1367 : if (POINTER_TYPE_P (TREE_TYPE (rse.expr)))
8202 : 1076 : rse.expr = build_fold_indirect_ref_loc (input_location,
8203 : : rse.expr);
8204 : : }
8205 : : else
8206 : 4194 : gfc_conv_expr_val (&rse, expr);
8207 : :
8208 : 5561 : gfc_add_block_to_block (&block, &rse.pre);
8209 : 5561 : gfc_add_block_to_block (&block, &lse.pre);
8210 : :
8211 : 5561 : lse.string_length = rse.string_length;
8212 : :
8213 : 11122 : deep_copy = !se->data_not_needed
8214 : 5561 : && (expr->expr_type == EXPR_VARIABLE
8215 : 5168 : || expr->expr_type == EXPR_ARRAY);
8216 : 5561 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
8217 : : deep_copy, false);
8218 : 5561 : gfc_add_expr_to_block (&block, tmp);
8219 : :
8220 : : /* Finish the copying loops. */
8221 : 5561 : gfc_trans_scalarizing_loops (&loop, &block);
8222 : :
8223 : 5561 : desc = loop.temp_ss->info->data.array.descriptor;
8224 : : }
8225 : 59140 : else if (expr->expr_type == EXPR_FUNCTION && !transposed_dims (ss))
8226 : : {
8227 : 1354 : desc = info->descriptor;
8228 : 1354 : se->string_length = ss_info->string_length;
8229 : : }
8230 : : else
8231 : : {
8232 : : /* We pass sections without copying to a temporary. Make a new
8233 : : descriptor and point it at the section we want. The loop variable
8234 : : limits will be the limits of the section.
8235 : : A function may decide to repack the array to speed up access, but
8236 : : we're not bothered about that here. */
8237 : 56419 : int dim, ndim, codim;
8238 : 56419 : tree parm;
8239 : 56419 : tree parmtype;
8240 : 56419 : tree dtype;
8241 : 56419 : tree stride;
8242 : 56419 : tree from;
8243 : 56419 : tree to;
8244 : 56419 : tree base;
8245 : 56419 : tree offset;
8246 : :
8247 : 56419 : ndim = info->ref ? info->ref->u.ar.dimen : ss->dimen;
8248 : :
8249 : 56419 : if (se->want_coarray)
8250 : : {
8251 : 415 : gfc_array_ref *ar = &info->ref->u.ar;
8252 : :
8253 : 415 : codim = gfc_get_corank (expr);
8254 : 1358 : for (n = 0; n < codim - 1; n++)
8255 : : {
8256 : : /* Make sure we are not lost somehow. */
8257 : 528 : gcc_assert (ar->dimen_type[n + ndim] == DIMEN_THIS_IMAGE);
8258 : :
8259 : : /* Make sure the call to gfc_conv_section_startstride won't
8260 : : generate unnecessary code to calculate stride. */
8261 : 528 : gcc_assert (ar->stride[n + ndim] == NULL);
8262 : :
8263 : 528 : gfc_conv_section_startstride (&loop.pre, ss, n + ndim);
8264 : 528 : loop.from[n + loop.dimen] = info->start[n + ndim];
8265 : 528 : loop.to[n + loop.dimen] = info->end[n + ndim];
8266 : : }
8267 : :
8268 : 415 : gcc_assert (n == codim - 1);
8269 : 415 : evaluate_bound (&loop.pre, info->start, ar->start,
8270 : : info->descriptor, n + ndim, true,
8271 : 415 : ar->as->type == AS_DEFERRED);
8272 : 415 : loop.from[n + loop.dimen] = info->start[n + ndim];
8273 : : }
8274 : : else
8275 : : codim = 0;
8276 : :
8277 : : /* Set the string_length for a character array. */
8278 : 56419 : if (expr->ts.type == BT_CHARACTER)
8279 : : {
8280 : 10767 : if (deferred_array_component && !substr)
8281 : 37 : se->string_length = ss_info->string_length;
8282 : : else
8283 : 10730 : se->string_length = gfc_get_expr_charlen (expr);
8284 : :
8285 : 10767 : if (VAR_P (se->string_length)
8286 : 946 : && expr->ts.u.cl->backend_decl == se->string_length)
8287 : 940 : tmp = ss_info->string_length;
8288 : : else
8289 : : tmp = se->string_length;
8290 : :
8291 : 10767 : if (expr->ts.deferred && expr->ts.u.cl->backend_decl
8292 : 172 : && VAR_P (expr->ts.u.cl->backend_decl))
8293 : 112 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl, tmp);
8294 : : else
8295 : 10655 : expr->ts.u.cl->backend_decl = tmp;
8296 : : }
8297 : :
8298 : : /* If we have an array section, are assigning or passing an array
8299 : : section argument make sure that the lower bound is 1. References
8300 : : to the full array should otherwise keep the original bounds. */
8301 : 56419 : if (!info->ref || info->ref->u.ar.type != AR_FULL)
8302 : 65026 : for (dim = 0; dim < loop.dimen; dim++)
8303 : 38118 : if (!integer_onep (loop.from[dim]))
8304 : : {
8305 : 19117 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
8306 : : gfc_array_index_type, gfc_index_one_node,
8307 : : loop.from[dim]);
8308 : 19117 : loop.to[dim] = fold_build2_loc (input_location, PLUS_EXPR,
8309 : : gfc_array_index_type,
8310 : : loop.to[dim], tmp);
8311 : 19117 : loop.from[dim] = gfc_index_one_node;
8312 : : }
8313 : :
8314 : 56419 : desc = info->descriptor;
8315 : 56419 : if (se->direct_byref && !se->byref_noassign)
8316 : : {
8317 : : /* For pointer assignments we fill in the destination. */
8318 : 2291 : parm = se->expr;
8319 : 2291 : parmtype = TREE_TYPE (parm);
8320 : : }
8321 : : else
8322 : : {
8323 : : /* Otherwise make a new one. */
8324 : 54128 : if (expr->ts.type == BT_CHARACTER)
8325 : 10183 : parmtype = gfc_typenode_for_spec (&expr->ts);
8326 : : else
8327 : 43945 : parmtype = gfc_get_element_type (TREE_TYPE (desc));
8328 : :
8329 : 54128 : parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, codim,
8330 : : loop.from, loop.to, 0,
8331 : : GFC_ARRAY_UNKNOWN, false);
8332 : 54128 : parm = gfc_create_var (parmtype, "parm");
8333 : :
8334 : : /* When expression is a class object, then add the class' handle to
8335 : : the parm_decl. */
8336 : 54128 : if (expr->ts.type == BT_CLASS && expr->expr_type == EXPR_VARIABLE)
8337 : : {
8338 : 1065 : gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
8339 : 1065 : gfc_se classse;
8340 : :
8341 : : /* class_expr can be NULL, when no _class ref is in expr.
8342 : : We must not fix this here with a gfc_fix_class_ref (). */
8343 : 1065 : if (class_expr)
8344 : : {
8345 : 1055 : gfc_init_se (&classse, NULL);
8346 : 1055 : gfc_conv_expr (&classse, class_expr);
8347 : 1055 : gfc_free_expr (class_expr);
8348 : :
8349 : 1055 : gcc_assert (classse.pre.head == NULL_TREE
8350 : : && classse.post.head == NULL_TREE);
8351 : 1055 : gfc_allocate_lang_decl (parm);
8352 : 1055 : GFC_DECL_SAVED_DESCRIPTOR (parm) = classse.expr;
8353 : : }
8354 : : }
8355 : : }
8356 : :
8357 : 56419 : if (expr->ts.type == BT_CHARACTER
8358 : 56419 : && VAR_P (TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)))))
8359 : : {
8360 : 0 : tree elem_len = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)));
8361 : 0 : gfc_add_modify (&loop.pre, elem_len,
8362 : 0 : fold_convert (TREE_TYPE (elem_len),
8363 : : gfc_get_array_span (desc, expr)));
8364 : : }
8365 : :
8366 : : /* Set the span field. */
8367 : 56419 : tmp = NULL_TREE;
8368 : 56419 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
8369 : 5484 : tmp = gfc_conv_descriptor_span_get (desc);
8370 : : else
8371 : 50935 : tmp = gfc_get_array_span (desc, expr);
8372 : 56419 : if (tmp)
8373 : 56339 : gfc_conv_descriptor_span_set (&loop.pre, parm, tmp);
8374 : :
8375 : : /* The following can be somewhat confusing. We have two
8376 : : descriptors, a new one and the original array.
8377 : : {parm, parmtype, dim} refer to the new one.
8378 : : {desc, type, n, loop} refer to the original, which maybe
8379 : : a descriptorless array.
8380 : : The bounds of the scalarization are the bounds of the section.
8381 : : We don't have to worry about numeric overflows when calculating
8382 : : the offsets because all elements are within the array data. */
8383 : :
8384 : : /* Set the dtype. */
8385 : 56419 : tmp = gfc_conv_descriptor_dtype (parm);
8386 : 56419 : if (se->unlimited_polymorphic)
8387 : 523 : dtype = gfc_get_dtype (TREE_TYPE (desc), &loop.dimen);
8388 : 55896 : else if (expr->ts.type == BT_ASSUMED)
8389 : : {
8390 : 127 : tree tmp2 = desc;
8391 : 127 : if (DECL_LANG_SPECIFIC (tmp2) && GFC_DECL_SAVED_DESCRIPTOR (tmp2))
8392 : 127 : tmp2 = GFC_DECL_SAVED_DESCRIPTOR (tmp2);
8393 : 127 : if (POINTER_TYPE_P (TREE_TYPE (tmp2)))
8394 : 127 : tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
8395 : 127 : dtype = gfc_conv_descriptor_dtype (tmp2);
8396 : : }
8397 : : else
8398 : 55769 : dtype = gfc_get_dtype (parmtype);
8399 : 56419 : gfc_add_modify (&loop.pre, tmp, dtype);
8400 : :
8401 : : /* The 1st element in the section. */
8402 : 56419 : base = gfc_index_zero_node;
8403 : :
8404 : : /* The offset from the 1st element in the section. */
8405 : 56419 : offset = gfc_index_zero_node;
8406 : :
8407 : 141232 : for (n = 0; n < ndim; n++)
8408 : : {
8409 : 84813 : stride = gfc_conv_array_stride (desc, n);
8410 : :
8411 : : /* Work out the 1st element in the section. */
8412 : 84813 : if (info->ref
8413 : 79303 : && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
8414 : : {
8415 : 854 : gcc_assert (info->subscript[n]
8416 : : && info->subscript[n]->info->type == GFC_SS_SCALAR);
8417 : 854 : start = info->subscript[n]->info->data.scalar.value;
8418 : : }
8419 : : else
8420 : : {
8421 : : /* Evaluate and remember the start of the section. */
8422 : 83959 : start = info->start[n];
8423 : 83959 : stride = gfc_evaluate_now (stride, &loop.pre);
8424 : : }
8425 : :
8426 : 84813 : tmp = gfc_conv_array_lbound (desc, n);
8427 : 84813 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
8428 : : start, tmp);
8429 : 84813 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
8430 : : tmp, stride);
8431 : 84813 : base = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
8432 : : base, tmp);
8433 : :
8434 : 84813 : if (info->ref
8435 : 79303 : && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
8436 : : {
8437 : : /* For elemental dimensions, we only need the 1st
8438 : : element in the section. */
8439 : 854 : continue;
8440 : : }
8441 : :
8442 : : /* Vector subscripts need copying and are handled elsewhere. */
8443 : 83959 : if (info->ref)
8444 : 78449 : gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
8445 : :
8446 : : /* look for the corresponding scalarizer dimension: dim. */
8447 : 120873 : for (dim = 0; dim < ndim; dim++)
8448 : 120873 : if (ss->dim[dim] == n)
8449 : : break;
8450 : :
8451 : : /* loop exited early: the DIM being looked for has been found. */
8452 : 83959 : gcc_assert (dim < ndim);
8453 : :
8454 : : /* Set the new lower bound. */
8455 : 83959 : from = loop.from[dim];
8456 : 83959 : to = loop.to[dim];
8457 : :
8458 : 83959 : gfc_conv_descriptor_lbound_set (&loop.pre, parm,
8459 : : gfc_rank_cst[dim], from);
8460 : :
8461 : : /* Set the new upper bound. */
8462 : 83959 : gfc_conv_descriptor_ubound_set (&loop.pre, parm,
8463 : : gfc_rank_cst[dim], to);
8464 : :
8465 : : /* Multiply the stride by the section stride to get the
8466 : : total stride. */
8467 : 83959 : stride = fold_build2_loc (input_location, MULT_EXPR,
8468 : : gfc_array_index_type,
8469 : : stride, info->stride[n]);
8470 : :
8471 : 83959 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8472 : 83959 : TREE_TYPE (offset), stride, from);
8473 : 83959 : offset = fold_build2_loc (input_location, MINUS_EXPR,
8474 : 83959 : TREE_TYPE (offset), offset, tmp);
8475 : :
8476 : : /* Store the new stride. */
8477 : 83959 : gfc_conv_descriptor_stride_set (&loop.pre, parm,
8478 : : gfc_rank_cst[dim], stride);
8479 : : }
8480 : :
8481 : 57362 : for (n = loop.dimen; n < loop.dimen + codim; n++)
8482 : : {
8483 : 943 : from = loop.from[n];
8484 : 943 : to = loop.to[n];
8485 : 943 : gfc_conv_descriptor_lbound_set (&loop.pre, parm,
8486 : : gfc_rank_cst[n], from);
8487 : 943 : if (n < loop.dimen + codim - 1)
8488 : 528 : gfc_conv_descriptor_ubound_set (&loop.pre, parm,
8489 : : gfc_rank_cst[n], to);
8490 : : }
8491 : :
8492 : 56419 : if (se->data_not_needed)
8493 : 5917 : gfc_conv_descriptor_data_set (&loop.pre, parm,
8494 : : gfc_index_zero_node);
8495 : : else
8496 : : /* Point the data pointer at the 1st element in the section. */
8497 : 50502 : gfc_get_dataptr_offset (&loop.pre, parm, desc, base,
8498 : : subref_array_target, expr);
8499 : :
8500 : 56419 : gfc_conv_descriptor_offset_set (&loop.pre, parm, offset);
8501 : :
8502 : 56419 : desc = parm;
8503 : : }
8504 : :
8505 : : /* For class arrays add the class tree into the saved descriptor to
8506 : : enable getting of _vptr and the like. */
8507 : 63334 : if (expr->expr_type == EXPR_VARIABLE && VAR_P (desc)
8508 : 50576 : && IS_CLASS_ARRAY (expr->symtree->n.sym))
8509 : : {
8510 : 1042 : gfc_allocate_lang_decl (desc);
8511 : 1042 : GFC_DECL_SAVED_DESCRIPTOR (desc) =
8512 : 1042 : DECL_LANG_SPECIFIC (expr->symtree->n.sym->backend_decl) ?
8513 : 956 : GFC_DECL_SAVED_DESCRIPTOR (expr->symtree->n.sym->backend_decl)
8514 : : : expr->symtree->n.sym->backend_decl;
8515 : : }
8516 : 62292 : else if (expr->expr_type == EXPR_ARRAY && VAR_P (desc)
8517 : 8126 : && IS_CLASS_ARRAY (expr))
8518 : : {
8519 : 12 : tree vtype;
8520 : 12 : gfc_allocate_lang_decl (desc);
8521 : 12 : tmp = gfc_create_var (expr->ts.u.derived->backend_decl, "class");
8522 : 12 : GFC_DECL_SAVED_DESCRIPTOR (desc) = tmp;
8523 : 12 : vtype = gfc_class_vptr_get (tmp);
8524 : 12 : gfc_add_modify (&se->pre, vtype,
8525 : 12 : gfc_build_addr_expr (TREE_TYPE (vtype),
8526 : 12 : gfc_find_vtab (&expr->ts)->backend_decl));
8527 : : }
8528 : 63334 : if (!se->direct_byref || se->byref_noassign)
8529 : : {
8530 : : /* Get a pointer to the new descriptor. */
8531 : 61043 : if (se->want_pointer)
8532 : 31518 : se->expr = gfc_build_addr_expr (NULL_TREE, desc);
8533 : : else
8534 : 29525 : se->expr = desc;
8535 : : }
8536 : :
8537 : 63334 : gfc_add_block_to_block (&se->pre, &loop.pre);
8538 : 63334 : gfc_add_block_to_block (&se->post, &loop.post);
8539 : :
8540 : : /* Cleanup the scalarizer. */
8541 : 63334 : gfc_cleanup_loop (&loop);
8542 : : }
8543 : :
8544 : :
8545 : : /* Calculate the array size (number of elements); if dim != NULL_TREE,
8546 : : return size for that dim (dim=0..rank-1; only for GFC_DESCRIPTOR_TYPE_P). */
8547 : : tree
8548 : 13780 : gfc_tree_array_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree dim)
8549 : : {
8550 : 13780 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
8551 : : {
8552 : 34 : gcc_assert (dim == NULL_TREE);
8553 : 34 : return GFC_TYPE_ARRAY_SIZE (TREE_TYPE (desc));
8554 : : }
8555 : 13746 : tree size, tmp, rank = NULL_TREE, cond = NULL_TREE;
8556 : 13746 : symbol_attribute attr = gfc_expr_attr (expr);
8557 : 13746 : gfc_array_spec *as = gfc_get_full_arrayspec_from_expr (expr);
8558 : 13746 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
8559 : 13746 : if ((!attr.pointer && !attr.allocatable && as && as->type == AS_ASSUMED_RANK)
8560 : 11118 : || !dim)
8561 : : {
8562 : 8094 : if (expr->rank < 0)
8563 : 2762 : rank = fold_convert (signed_char_type_node,
8564 : : gfc_conv_descriptor_rank (desc));
8565 : : else
8566 : 5332 : rank = build_int_cst (signed_char_type_node, expr->rank);
8567 : : }
8568 : :
8569 : 8094 : if (dim || expr->rank == 1)
8570 : : {
8571 : 9926 : if (!dim)
8572 : 4274 : dim = gfc_index_zero_node;
8573 : 12143 : tree ubound = gfc_conv_descriptor_ubound_get (desc, dim);
8574 : 12143 : tree lbound = gfc_conv_descriptor_lbound_get (desc, dim);
8575 : :
8576 : 12143 : size = fold_build2_loc (input_location, MINUS_EXPR,
8577 : : gfc_array_index_type, ubound, lbound);
8578 : 12143 : size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8579 : : size, gfc_index_one_node);
8580 : : /* if (!allocatable && !pointer && assumed rank)
8581 : : size = (idx == rank && ubound[rank-1] == -1 ? -1 : size;
8582 : : else
8583 : : size = max (0, size); */
8584 : 12143 : size = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
8585 : : size, gfc_index_zero_node);
8586 : 12143 : if (!attr.pointer && !attr.allocatable
8587 : 7482 : && as && as->type == AS_ASSUMED_RANK)
8588 : : {
8589 : 2217 : tmp = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
8590 : 2217 : rank, build_int_cst (signed_char_type_node, 1));
8591 : 2217 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8592 : : fold_convert (signed_char_type_node, dim),
8593 : : tmp);
8594 : 2217 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8595 : : gfc_conv_descriptor_ubound_get (desc, dim),
8596 : 2217 : build_int_cst (gfc_array_index_type, -1));
8597 : 2217 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
8598 : : cond, tmp);
8599 : 2217 : tmp = build_int_cst (gfc_array_index_type, -1);
8600 : 2217 : size = build3_loc (input_location, COND_EXPR, gfc_array_index_type,
8601 : : cond, tmp, size);
8602 : : }
8603 : 12143 : return size;
8604 : : }
8605 : :
8606 : : /* size = 1. */
8607 : 1603 : size = gfc_create_var (gfc_array_index_type, "size");
8608 : 1603 : gfc_add_modify (block, size, build_int_cst (TREE_TYPE (size), 1));
8609 : 1603 : tree extent = gfc_create_var (gfc_array_index_type, "extent");
8610 : :
8611 : 1603 : stmtblock_t cond_block, loop_body;
8612 : 1603 : gfc_init_block (&cond_block);
8613 : 1603 : gfc_init_block (&loop_body);
8614 : :
8615 : : /* Loop: for (i = 0; i < rank; ++i). */
8616 : 1603 : tree idx = gfc_create_var (signed_char_type_node, "idx");
8617 : : /* Loop body. */
8618 : : /* #if (assumed-rank + !allocatable && !pointer)
8619 : : if (idx == rank - 1 && dim[idx].ubound == -1)
8620 : : extent = -1;
8621 : : else
8622 : : #endif
8623 : : extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1
8624 : : if (extent < 0)
8625 : : extent = 0
8626 : : size *= extent. */
8627 : 1603 : cond = NULL_TREE;
8628 : 1603 : if (!attr.pointer && !attr.allocatable && as && as->type == AS_ASSUMED_RANK)
8629 : : {
8630 : 411 : tmp = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
8631 : 411 : rank, build_int_cst (signed_char_type_node, 1));
8632 : 411 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8633 : : idx, tmp);
8634 : 411 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8635 : : gfc_conv_descriptor_ubound_get (desc, idx),
8636 : 411 : build_int_cst (gfc_array_index_type, -1));
8637 : 411 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
8638 : : cond, tmp);
8639 : : }
8640 : 1603 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8641 : : gfc_conv_descriptor_ubound_get (desc, idx),
8642 : : gfc_conv_descriptor_lbound_get (desc, idx));
8643 : 1603 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8644 : : tmp, gfc_index_one_node);
8645 : 1603 : gfc_add_modify (&cond_block, extent, tmp);
8646 : 1603 : tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
8647 : : extent, gfc_index_zero_node);
8648 : 1603 : tmp = build3_v (COND_EXPR, tmp,
8649 : : fold_build2_loc (input_location, MODIFY_EXPR,
8650 : : gfc_array_index_type,
8651 : : extent, gfc_index_zero_node),
8652 : : build_empty_stmt (input_location));
8653 : 1603 : gfc_add_expr_to_block (&cond_block, tmp);
8654 : 1603 : tmp = gfc_finish_block (&cond_block);
8655 : 1603 : if (cond)
8656 : 411 : tmp = build3_v (COND_EXPR, cond,
8657 : : fold_build2_loc (input_location, MODIFY_EXPR,
8658 : : gfc_array_index_type, extent,
8659 : : build_int_cst (gfc_array_index_type, -1)),
8660 : : tmp);
8661 : 1603 : gfc_add_expr_to_block (&loop_body, tmp);
8662 : : /* size *= extent. */
8663 : 1603 : gfc_add_modify (&loop_body, size,
8664 : : fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8665 : : size, extent));
8666 : : /* Generate loop. */
8667 : 1603 : gfc_simple_for_loop (block, idx, build_int_cst (TREE_TYPE (idx), 0), rank, LT_EXPR,
8668 : 1603 : build_int_cst (TREE_TYPE (idx), 1),
8669 : : gfc_finish_block (&loop_body));
8670 : 1603 : return size;
8671 : : }
8672 : :
8673 : : /* Helper function for gfc_conv_array_parameter if array size needs to be
8674 : : computed. */
8675 : :
8676 : : static void
8677 : 102 : array_parameter_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree *size)
8678 : : {
8679 : 102 : tree elem;
8680 : 102 : *size = gfc_tree_array_size (block, desc, expr, NULL);
8681 : 102 : elem = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
8682 : 102 : *size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8683 : : *size, fold_convert (gfc_array_index_type, elem));
8684 : 102 : }
8685 : :
8686 : : /* Helper function - return true if the argument is a pointer. */
8687 : :
8688 : : static bool
8689 : 737 : is_pointer (gfc_expr *e)
8690 : : {
8691 : 737 : gfc_symbol *sym;
8692 : :
8693 : 737 : if (e->expr_type != EXPR_VARIABLE || e->symtree == NULL)
8694 : : return false;
8695 : :
8696 : 737 : sym = e->symtree->n.sym;
8697 : 737 : if (sym == NULL)
8698 : : return false;
8699 : :
8700 : 737 : return sym->attr.pointer || sym->attr.proc_pointer;
8701 : : }
8702 : :
8703 : : /* Convert an array for passing as an actual parameter. */
8704 : :
8705 : : void
8706 : 53405 : gfc_conv_array_parameter (gfc_se *se, gfc_expr *expr, bool g77,
8707 : : const gfc_symbol *fsym, const char *proc_name,
8708 : : tree *size, tree *lbshift, tree *packed)
8709 : : {
8710 : 53405 : tree ptr;
8711 : 53405 : tree desc;
8712 : 53405 : tree tmp = NULL_TREE;
8713 : 53405 : tree stmt;
8714 : 53405 : tree parent = DECL_CONTEXT (current_function_decl);
8715 : 53405 : tree ctree;
8716 : 53405 : tree pack_attr = NULL_TREE; /* Set when packing class arrays. */
8717 : 53405 : bool full_array_var;
8718 : 53405 : bool this_array_result;
8719 : 53405 : bool contiguous;
8720 : 53405 : bool no_pack;
8721 : 53405 : bool array_constructor;
8722 : 53405 : bool good_allocatable;
8723 : 53405 : bool ultimate_ptr_comp;
8724 : 53405 : bool ultimate_alloc_comp;
8725 : 53405 : gfc_symbol *sym;
8726 : 53405 : stmtblock_t block;
8727 : 53405 : gfc_ref *ref;
8728 : :
8729 : 53405 : ultimate_ptr_comp = false;
8730 : 53405 : ultimate_alloc_comp = false;
8731 : :
8732 : 53979 : for (ref = expr->ref; ref; ref = ref->next)
8733 : : {
8734 : 45585 : if (ref->next == NULL)
8735 : : break;
8736 : :
8737 : 574 : if (ref->type == REF_COMPONENT)
8738 : : {
8739 : 502 : ultimate_ptr_comp = ref->u.c.component->attr.pointer;
8740 : 502 : ultimate_alloc_comp = ref->u.c.component->attr.allocatable;
8741 : : }
8742 : : }
8743 : :
8744 : 53405 : full_array_var = false;
8745 : 53405 : contiguous = false;
8746 : :
8747 : 53405 : if (expr->expr_type == EXPR_VARIABLE && ref && !ultimate_ptr_comp)
8748 : 44934 : full_array_var = gfc_full_array_ref_p (ref, &contiguous);
8749 : :
8750 : 44934 : sym = full_array_var ? expr->symtree->n.sym : NULL;
8751 : :
8752 : : /* The symbol should have an array specification. */
8753 : 50511 : gcc_assert (!sym || sym->as || ref->u.ar.as);
8754 : :
8755 : 53405 : if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_CHARACTER)
8756 : : {
8757 : 667 : get_array_ctor_strlen (&se->pre, expr->value.constructor, &tmp);
8758 : 667 : expr->ts.u.cl->backend_decl = tmp;
8759 : 667 : se->string_length = tmp;
8760 : : }
8761 : :
8762 : : /* Is this the result of the enclosing procedure? */
8763 : 53405 : this_array_result = (full_array_var && sym->attr.flavor == FL_PROCEDURE);
8764 : 58 : if (this_array_result
8765 : 58 : && (sym->backend_decl != current_function_decl)
8766 : 0 : && (sym->backend_decl != parent))
8767 : 53405 : this_array_result = false;
8768 : :
8769 : : /* Passing an optional dummy argument as actual to an optional dummy? */
8770 : 53405 : bool pass_optional;
8771 : 53405 : pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
8772 : :
8773 : : /* Passing address of the array if it is not pointer or assumed-shape. */
8774 : 53405 : if (full_array_var && g77 && !this_array_result
8775 : 13881 : && sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
8776 : : {
8777 : 11036 : tmp = gfc_get_symbol_decl (sym);
8778 : :
8779 : 11036 : if (sym->ts.type == BT_CHARACTER)
8780 : 2709 : se->string_length = sym->ts.u.cl->backend_decl;
8781 : :
8782 : 11036 : if (!sym->attr.pointer
8783 : 10531 : && sym->as
8784 : 10531 : && sym->as->type != AS_ASSUMED_SHAPE
8785 : 10274 : && sym->as->type != AS_DEFERRED
8786 : 9233 : && sym->as->type != AS_ASSUMED_RANK
8787 : 9165 : && !sym->attr.allocatable)
8788 : : {
8789 : : /* Some variables are declared directly, others are declared as
8790 : : pointers and allocated on the heap. */
8791 : 9165 : if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
8792 : 2464 : se->expr = tmp;
8793 : : else
8794 : 6701 : se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
8795 : 9165 : if (size)
8796 : 34 : array_parameter_size (&se->pre, tmp, expr, size);
8797 : 14724 : return;
8798 : : }
8799 : :
8800 : 1871 : if (sym->attr.allocatable)
8801 : : {
8802 : 921 : if (sym->attr.dummy || sym->attr.result)
8803 : : {
8804 : 298 : gfc_conv_expr_descriptor (se, expr);
8805 : 298 : tmp = se->expr;
8806 : : }
8807 : 921 : if (size)
8808 : 6 : array_parameter_size (&se->pre, tmp, expr, size);
8809 : 921 : se->expr = gfc_conv_array_data (tmp);
8810 : 921 : if (pass_optional)
8811 : : {
8812 : 18 : tree cond = gfc_conv_expr_present (sym);
8813 : 36 : se->expr = build3_loc (input_location, COND_EXPR,
8814 : 18 : TREE_TYPE (se->expr), cond, se->expr,
8815 : 18 : fold_convert (TREE_TYPE (se->expr),
8816 : : null_pointer_node));
8817 : : }
8818 : 921 : return;
8819 : : }
8820 : : }
8821 : :
8822 : : /* A convenient reduction in scope. */
8823 : 43319 : contiguous = g77 && !this_array_result && contiguous;
8824 : :
8825 : : /* There is no need to pack and unpack the array, if it is contiguous
8826 : : and not a deferred- or assumed-shape array, or if it is simply
8827 : : contiguous. */
8828 : 43319 : no_pack = false;
8829 : : // clang-format off
8830 : 43319 : if (sym)
8831 : : {
8832 : 32029 : symbol_attribute *attr = &(IS_CLASS_ARRAY (sym)
8833 : : ? CLASS_DATA (sym)->attr : sym->attr);
8834 : 133 : gfc_array_spec *as = IS_CLASS_ARRAY (sym)
8835 : 32162 : ? CLASS_DATA (sym)->as : sym->as;
8836 : 32029 : no_pack = (as
8837 : 31778 : && !attr->pointer
8838 : 28740 : && as->type != AS_DEFERRED
8839 : 22810 : && as->type != AS_ASSUMED_RANK
8840 : 52427 : && as->type != AS_ASSUMED_SHAPE);
8841 : : }
8842 : 43319 : if (ref && ref->u.ar.as)
8843 : 34923 : no_pack = no_pack
8844 : 34923 : || (ref->u.ar.as->type != AS_DEFERRED
8845 : : && ref->u.ar.as->type != AS_ASSUMED_RANK
8846 : : && ref->u.ar.as->type != AS_ASSUMED_SHAPE);
8847 : 86638 : no_pack = contiguous
8848 : 43319 : && (no_pack || gfc_is_simply_contiguous (expr, false, true));
8849 : : // clang-format on
8850 : :
8851 : : /* If we have an EXPR_OP or a function returning an explicit-shaped
8852 : : or allocatable array, an array temporary will be generated which
8853 : : does not need to be packed / unpacked if passed to an
8854 : : explicit-shape dummy array. */
8855 : :
8856 : 43319 : if (g77)
8857 : : {
8858 : 5699 : if (expr->expr_type == EXPR_OP)
8859 : : no_pack = 1;
8860 : 5622 : else if (expr->expr_type == EXPR_FUNCTION && expr->value.function.esym)
8861 : : {
8862 : 41 : gfc_symbol *result = expr->value.function.esym->result;
8863 : 41 : if (result->attr.dimension
8864 : 41 : && (result->as->type == AS_EXPLICIT
8865 : : || result->attr.allocatable
8866 : 14 : || result->attr.contiguous))
8867 : 43319 : no_pack = 1;
8868 : : }
8869 : : }
8870 : :
8871 : : /* Array constructors are always contiguous and do not need packing. */
8872 : 43319 : array_constructor = g77 && !this_array_result && expr->expr_type == EXPR_ARRAY;
8873 : :
8874 : : /* Same is true of contiguous sections from allocatable variables. */
8875 : 86638 : good_allocatable = contiguous
8876 : 3924 : && expr->symtree
8877 : 47243 : && expr->symtree->n.sym->attr.allocatable;
8878 : :
8879 : : /* Or ultimate allocatable components. */
8880 : 43319 : ultimate_alloc_comp = contiguous && ultimate_alloc_comp;
8881 : :
8882 : 43319 : if (no_pack || array_constructor || good_allocatable || ultimate_alloc_comp)
8883 : : {
8884 : 4248 : gfc_conv_expr_descriptor (se, expr);
8885 : : /* Deallocate the allocatable components of structures that are
8886 : : not variable. */
8887 : 4248 : if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
8888 : 2766 : && expr->ts.u.derived->attr.alloc_comp
8889 : 1512 : && expr->expr_type != EXPR_VARIABLE)
8890 : : {
8891 : 2 : tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, se->expr, expr->rank);
8892 : :
8893 : : /* The components shall be deallocated before their containing entity. */
8894 : 2 : gfc_prepend_expr_to_block (&se->post, tmp);
8895 : : }
8896 : 4248 : if (expr->ts.type == BT_CHARACTER && expr->expr_type != EXPR_FUNCTION)
8897 : 279 : se->string_length = expr->ts.u.cl->backend_decl;
8898 : 4248 : if (size)
8899 : 32 : array_parameter_size (&se->pre, se->expr, expr, size);
8900 : 4248 : se->expr = gfc_conv_array_data (se->expr);
8901 : 4248 : return;
8902 : : }
8903 : :
8904 : 39071 : if (fsym && fsym->ts.type == BT_CLASS)
8905 : : {
8906 : 1203 : gcc_assert (se->expr);
8907 : : ctree = se->expr;
8908 : : }
8909 : : else
8910 : : ctree = NULL_TREE;
8911 : :
8912 : 39071 : if (this_array_result)
8913 : : {
8914 : : /* Result of the enclosing function. */
8915 : 58 : gfc_conv_expr_descriptor (se, expr);
8916 : 58 : if (size)
8917 : 0 : array_parameter_size (&se->pre, se->expr, expr, size);
8918 : 58 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
8919 : :
8920 : 18 : if (g77 && TREE_TYPE (TREE_TYPE (se->expr)) != NULL_TREE
8921 : 76 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
8922 : 18 : se->expr = gfc_conv_array_data (build_fold_indirect_ref_loc (input_location,
8923 : : se->expr));
8924 : :
8925 : 58 : return;
8926 : : }
8927 : : else
8928 : : {
8929 : : /* Every other type of array. */
8930 : 39013 : se->want_pointer = (ctree) ? 0 : 1;
8931 : 39013 : gfc_conv_expr_descriptor (se, expr);
8932 : :
8933 : 39013 : if (size)
8934 : 30 : array_parameter_size (&se->pre,
8935 : : build_fold_indirect_ref_loc (input_location,
8936 : : se->expr),
8937 : : expr, size);
8938 : 39013 : if (ctree)
8939 : : {
8940 : 1203 : stmtblock_t block;
8941 : :
8942 : 1203 : gfc_init_block (&block);
8943 : 1203 : if (lbshift && *lbshift)
8944 : : {
8945 : : /* Apply a shift of the lbound when supplied. */
8946 : 86 : for (int dim = 0; dim < expr->rank; ++dim)
8947 : 43 : gfc_conv_shift_descriptor_lbound (&block, se->expr, dim,
8948 : : *lbshift);
8949 : : }
8950 : 1203 : tmp = gfc_class_data_get (ctree);
8951 : 1203 : if (expr->rank > 1 && CLASS_DATA (fsym)->as->rank != expr->rank
8952 : 84 : && CLASS_DATA (fsym)->as->type == AS_EXPLICIT && !no_pack)
8953 : : {
8954 : 36 : tree arr = gfc_create_var (TREE_TYPE (tmp), "parm");
8955 : 36 : gfc_conv_descriptor_data_set (&block, arr,
8956 : : gfc_conv_descriptor_data_get (
8957 : : se->expr));
8958 : 36 : gfc_conv_descriptor_lbound_set (&block, arr, gfc_index_zero_node,
8959 : : gfc_index_zero_node);
8960 : 36 : gfc_conv_descriptor_ubound_set (
8961 : : &block, arr, gfc_index_zero_node,
8962 : : gfc_conv_descriptor_size (se->expr, expr->rank));
8963 : 36 : gfc_conv_descriptor_stride_set (
8964 : : &block, arr, gfc_index_zero_node,
8965 : : gfc_conv_descriptor_stride_get (se->expr, gfc_index_zero_node));
8966 : 36 : gfc_add_modify (&block, gfc_conv_descriptor_dtype (arr),
8967 : : gfc_conv_descriptor_dtype (se->expr));
8968 : 36 : gfc_add_modify (&block, gfc_conv_descriptor_rank (arr),
8969 : 36 : build_int_cst (signed_char_type_node, 1));
8970 : 36 : gfc_conv_descriptor_span_set (&block, arr,
8971 : : gfc_conv_descriptor_span_get (arr));
8972 : 36 : gfc_conv_descriptor_offset_set (&block, arr, gfc_index_zero_node);
8973 : 36 : se->expr = arr;
8974 : : }
8975 : 1203 : gfc_class_array_data_assign (&block, tmp, se->expr, true);
8976 : :
8977 : : /* Handle optional. */
8978 : 1203 : if (fsym && fsym->attr.optional && sym && sym->attr.optional)
8979 : 348 : tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
8980 : : gfc_finish_block (&block),
8981 : : build_empty_stmt (input_location));
8982 : : else
8983 : 855 : tmp = gfc_finish_block (&block);
8984 : :
8985 : 1203 : gfc_add_expr_to_block (&se->pre, tmp);
8986 : : }
8987 : : }
8988 : :
8989 : : /* Deallocate the allocatable components of structures that are
8990 : : not variable, for descriptorless arguments.
8991 : : Arguments with a descriptor are handled in gfc_conv_procedure_call. */
8992 : 39013 : if (g77 && (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
8993 : 66 : && expr->ts.u.derived->attr.alloc_comp
8994 : 12 : && expr->expr_type != EXPR_VARIABLE)
8995 : : {
8996 : 0 : tmp = build_fold_indirect_ref_loc (input_location, se->expr);
8997 : 0 : tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
8998 : :
8999 : : /* The components shall be deallocated before their containing entity. */
9000 : 0 : gfc_prepend_expr_to_block (&se->post, tmp);
9001 : : }
9002 : :
9003 : 37580 : if (g77 || (fsym && fsym->attr.contiguous
9004 : 1515 : && !gfc_is_simply_contiguous (expr, false, true)))
9005 : : {
9006 : 1577 : tree origptr = NULL_TREE, packedptr = NULL_TREE;
9007 : :
9008 : 1577 : desc = se->expr;
9009 : :
9010 : : /* For contiguous arrays, save the original value of the descriptor. */
9011 : 1577 : if (!g77 && !ctree)
9012 : : {
9013 : 48 : origptr = gfc_create_var (pvoid_type_node, "origptr");
9014 : 48 : tmp = build_fold_indirect_ref_loc (input_location, desc);
9015 : 48 : tmp = gfc_conv_array_data (tmp);
9016 : 96 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
9017 : 48 : TREE_TYPE (origptr), origptr,
9018 : 48 : fold_convert (TREE_TYPE (origptr), tmp));
9019 : 48 : gfc_add_expr_to_block (&se->pre, tmp);
9020 : : }
9021 : :
9022 : : /* Repack the array. */
9023 : 1577 : if (warn_array_temporaries)
9024 : : {
9025 : 28 : if (fsym)
9026 : 18 : gfc_warning (OPT_Warray_temporaries,
9027 : : "Creating array temporary at %L for argument %qs",
9028 : 18 : &expr->where, fsym->name);
9029 : : else
9030 : 10 : gfc_warning (OPT_Warray_temporaries,
9031 : : "Creating array temporary at %L", &expr->where);
9032 : : }
9033 : :
9034 : : /* When optimizing, we can use gfc_conv_subref_array_arg for
9035 : : making the packing and unpacking operation visible to the
9036 : : optimizers. */
9037 : :
9038 : 1433 : if (g77 && flag_inline_arg_packing && expr->expr_type == EXPR_VARIABLE
9039 : 737 : && !is_pointer (expr) && ! gfc_has_dimen_vector_ref (expr)
9040 : 353 : && !(expr->symtree->n.sym->as
9041 : 339 : && expr->symtree->n.sym->as->type == AS_ASSUMED_RANK)
9042 : 1930 : && (fsym == NULL || fsym->ts.type != BT_ASSUMED))
9043 : : {
9044 : 332 : gfc_conv_subref_array_arg (se, expr, g77,
9045 : 170 : fsym ? fsym->attr.intent : INTENT_INOUT,
9046 : : false, fsym, proc_name, sym, true);
9047 : 332 : return;
9048 : : }
9049 : :
9050 : 1245 : if (ctree)
9051 : : {
9052 : 96 : packedptr
9053 : 96 : = gfc_build_addr_expr (NULL_TREE, gfc_create_var (TREE_TYPE (ctree),
9054 : : "packed"));
9055 : 96 : if (fsym)
9056 : : {
9057 : 96 : int pack_mask = 0;
9058 : :
9059 : : /* Set bit 0 to the mask, when this is an unlimited_poly
9060 : : class. */
9061 : 96 : if (CLASS_DATA (fsym)->ts.u.derived->attr.unlimited_polymorphic)
9062 : 36 : pack_mask = 1 << 0;
9063 : 96 : pack_attr = build_int_cst (integer_type_node, pack_mask);
9064 : : }
9065 : : else
9066 : 0 : pack_attr = integer_zero_node;
9067 : :
9068 : 96 : gfc_add_expr_to_block (
9069 : : &se->pre,
9070 : : build_call_expr_loc (input_location, gfor_fndecl_in_pack_class, 4,
9071 : : packedptr,
9072 : : gfc_build_addr_expr (NULL_TREE, ctree),
9073 : 96 : size_in_bytes (TREE_TYPE (ctree)), pack_attr));
9074 : 96 : ptr = gfc_conv_array_data (gfc_class_data_get (packedptr));
9075 : 96 : se->expr = packedptr;
9076 : 96 : if (packed)
9077 : 96 : *packed = packedptr;
9078 : : }
9079 : : else
9080 : : {
9081 : 1149 : ptr = build_call_expr_loc (input_location, gfor_fndecl_in_pack, 1,
9082 : : desc);
9083 : :
9084 : 1149 : if (fsym && fsym->attr.optional && sym && sym->attr.optional)
9085 : : {
9086 : 11 : tmp = gfc_conv_expr_present (sym);
9087 : 22 : ptr = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
9088 : 11 : tmp, fold_convert (TREE_TYPE (se->expr), ptr),
9089 : 11 : fold_convert (TREE_TYPE (se->expr),
9090 : : null_pointer_node));
9091 : : }
9092 : :
9093 : 1149 : ptr = gfc_evaluate_now (ptr, &se->pre);
9094 : : }
9095 : :
9096 : : /* Use the packed data for the actual argument, except for contiguous arrays,
9097 : : where the descriptor's data component is set. */
9098 : 1245 : if (g77)
9099 : 1101 : se->expr = ptr;
9100 : : else
9101 : : {
9102 : 144 : tmp = build_fold_indirect_ref_loc (input_location, desc);
9103 : :
9104 : 144 : gfc_ss * ss = gfc_walk_expr (expr);
9105 : 288 : if (!transposed_dims (ss))
9106 : : {
9107 : 138 : if (!ctree)
9108 : 48 : gfc_conv_descriptor_data_set (&se->pre, tmp, ptr);
9109 : : }
9110 : 6 : else if (!ctree)
9111 : : {
9112 : 0 : tree old_field, new_field;
9113 : :
9114 : : /* The original descriptor has transposed dims so we can't reuse
9115 : : it directly; we have to create a new one. */
9116 : 0 : tree old_desc = tmp;
9117 : 0 : tree new_desc = gfc_create_var (TREE_TYPE (old_desc), "arg_desc");
9118 : :
9119 : 0 : old_field = gfc_conv_descriptor_dtype (old_desc);
9120 : 0 : new_field = gfc_conv_descriptor_dtype (new_desc);
9121 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9122 : :
9123 : 0 : old_field = gfc_conv_descriptor_offset (old_desc);
9124 : 0 : new_field = gfc_conv_descriptor_offset (new_desc);
9125 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9126 : :
9127 : 0 : for (int i = 0; i < expr->rank; i++)
9128 : : {
9129 : 0 : old_field = gfc_conv_descriptor_dimension (old_desc,
9130 : 0 : gfc_rank_cst[get_array_ref_dim_for_loop_dim (ss, i)]);
9131 : 0 : new_field = gfc_conv_descriptor_dimension (new_desc,
9132 : : gfc_rank_cst[i]);
9133 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9134 : : }
9135 : :
9136 : 0 : if (flag_coarray == GFC_FCOARRAY_LIB
9137 : 0 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (old_desc))
9138 : 0 : && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (old_desc))
9139 : : == GFC_ARRAY_ALLOCATABLE)
9140 : : {
9141 : 0 : old_field = gfc_conv_descriptor_token (old_desc);
9142 : 0 : new_field = gfc_conv_descriptor_token (new_desc);
9143 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9144 : : }
9145 : :
9146 : 0 : gfc_conv_descriptor_data_set (&se->pre, new_desc, ptr);
9147 : 0 : se->expr = gfc_build_addr_expr (NULL_TREE, new_desc);
9148 : : }
9149 : 144 : gfc_free_ss (ss);
9150 : : }
9151 : :
9152 : 1245 : if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
9153 : : {
9154 : 8 : char * msg;
9155 : :
9156 : 8 : if (fsym && proc_name)
9157 : 8 : msg = xasprintf ("An array temporary was created for argument "
9158 : 8 : "'%s' of procedure '%s'", fsym->name, proc_name);
9159 : : else
9160 : 0 : msg = xasprintf ("An array temporary was created");
9161 : :
9162 : 8 : tmp = build_fold_indirect_ref_loc (input_location,
9163 : : desc);
9164 : 8 : tmp = gfc_conv_array_data (tmp);
9165 : 8 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9166 : 8 : fold_convert (TREE_TYPE (tmp), ptr), tmp);
9167 : :
9168 : 8 : if (pass_optional)
9169 : 6 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9170 : : logical_type_node,
9171 : : gfc_conv_expr_present (sym), tmp);
9172 : :
9173 : 8 : gfc_trans_runtime_check (false, true, tmp, &se->pre,
9174 : : &expr->where, msg);
9175 : 8 : free (msg);
9176 : : }
9177 : :
9178 : 1245 : gfc_start_block (&block);
9179 : :
9180 : : /* Copy the data back. */
9181 : 1245 : if (fsym == NULL || fsym->attr.intent != INTENT_IN)
9182 : : {
9183 : 1124 : if (ctree)
9184 : : {
9185 : 66 : tmp = gfc_build_addr_expr (NULL_TREE, ctree);
9186 : 66 : tmp = build_call_expr_loc (input_location,
9187 : : gfor_fndecl_in_unpack_class, 4, tmp,
9188 : : packedptr,
9189 : 66 : size_in_bytes (TREE_TYPE (ctree)),
9190 : : pack_attr);
9191 : : }
9192 : : else
9193 : 1058 : tmp = build_call_expr_loc (input_location, gfor_fndecl_in_unpack, 2,
9194 : : desc, ptr);
9195 : 1124 : gfc_add_expr_to_block (&block, tmp);
9196 : : }
9197 : 121 : else if (ctree && fsym->attr.intent == INTENT_IN)
9198 : : {
9199 : : /* Need to free the memory for class arrays, that got packed. */
9200 : 30 : gfc_add_expr_to_block (&block, gfc_call_free (ptr));
9201 : : }
9202 : :
9203 : : /* Free the temporary. */
9204 : 1154 : if (!ctree)
9205 : 1149 : gfc_add_expr_to_block (&block, gfc_call_free (ptr));
9206 : :
9207 : 1245 : stmt = gfc_finish_block (&block);
9208 : :
9209 : 1245 : gfc_init_block (&block);
9210 : : /* Only if it was repacked. This code needs to be executed before the
9211 : : loop cleanup code. */
9212 : 1245 : tmp = (ctree) ? desc : build_fold_indirect_ref_loc (input_location, desc);
9213 : 1245 : tmp = gfc_conv_array_data (tmp);
9214 : 1245 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9215 : 1245 : fold_convert (TREE_TYPE (tmp), ptr), tmp);
9216 : :
9217 : 1245 : if (pass_optional)
9218 : 11 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9219 : : logical_type_node,
9220 : : gfc_conv_expr_present (sym), tmp);
9221 : :
9222 : 1245 : tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
9223 : :
9224 : 1245 : gfc_add_expr_to_block (&block, tmp);
9225 : 1245 : gfc_add_block_to_block (&block, &se->post);
9226 : :
9227 : 1245 : gfc_init_block (&se->post);
9228 : :
9229 : : /* Reset the descriptor pointer. */
9230 : 1245 : if (!g77 && !ctree)
9231 : : {
9232 : 48 : tmp = build_fold_indirect_ref_loc (input_location, desc);
9233 : 48 : gfc_conv_descriptor_data_set (&se->post, tmp, origptr);
9234 : : }
9235 : :
9236 : 1245 : gfc_add_block_to_block (&se->post, &block);
9237 : : }
9238 : : }
9239 : :
9240 : :
9241 : : /* This helper function calculates the size in words of a full array. */
9242 : :
9243 : : tree
9244 : 14885 : gfc_full_array_size (stmtblock_t *block, tree decl, int rank)
9245 : : {
9246 : 14885 : tree idx;
9247 : 14885 : tree nelems;
9248 : 14885 : tree tmp;
9249 : 14885 : idx = gfc_rank_cst[rank - 1];
9250 : 14885 : nelems = gfc_conv_descriptor_ubound_get (decl, idx);
9251 : 14885 : tmp = gfc_conv_descriptor_lbound_get (decl, idx);
9252 : 14885 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9253 : : nelems, tmp);
9254 : 14885 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
9255 : : tmp, gfc_index_one_node);
9256 : 14885 : tmp = gfc_evaluate_now (tmp, block);
9257 : :
9258 : 14885 : nelems = gfc_conv_descriptor_stride_get (decl, idx);
9259 : 14885 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9260 : : nelems, tmp);
9261 : 14885 : return gfc_evaluate_now (tmp, block);
9262 : : }
9263 : :
9264 : :
9265 : : /* Allocate dest to the same size as src, and copy src -> dest.
9266 : : If no_malloc is set, only the copy is done. */
9267 : :
9268 : : static tree
9269 : 6237 : duplicate_allocatable (tree dest, tree src, tree type, int rank,
9270 : : bool no_malloc, bool no_memcpy, tree str_sz,
9271 : : tree add_when_allocated)
9272 : : {
9273 : 6237 : tree tmp;
9274 : 6237 : tree eltype;
9275 : 6237 : tree size;
9276 : 6237 : tree nelems;
9277 : 6237 : tree null_cond;
9278 : 6237 : tree null_data;
9279 : 6237 : stmtblock_t block;
9280 : :
9281 : : /* If the source is null, set the destination to null. Then,
9282 : : allocate memory to the destination. */
9283 : 6237 : gfc_init_block (&block);
9284 : :
9285 : 6237 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
9286 : : {
9287 : 1757 : gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
9288 : 1757 : null_data = gfc_finish_block (&block);
9289 : :
9290 : 1757 : gfc_init_block (&block);
9291 : 1757 : eltype = TREE_TYPE (type);
9292 : 1757 : if (str_sz != NULL_TREE)
9293 : : size = str_sz;
9294 : : else
9295 : 1484 : size = TYPE_SIZE_UNIT (eltype);
9296 : :
9297 : 1757 : if (!no_malloc)
9298 : : {
9299 : 1757 : tmp = gfc_call_malloc (&block, type, size);
9300 : 1757 : gfc_add_modify (&block, dest, fold_convert (type, tmp));
9301 : : }
9302 : :
9303 : 1757 : if (!no_memcpy)
9304 : : {
9305 : 1332 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9306 : 1332 : tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
9307 : : fold_convert (size_type_node, size));
9308 : 1332 : gfc_add_expr_to_block (&block, tmp);
9309 : : }
9310 : : }
9311 : : else
9312 : : {
9313 : 4480 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9314 : 4480 : null_data = gfc_finish_block (&block);
9315 : :
9316 : 4480 : gfc_init_block (&block);
9317 : 4480 : if (rank)
9318 : 4469 : nelems = gfc_full_array_size (&block, src, rank);
9319 : : else
9320 : 11 : nelems = gfc_index_one_node;
9321 : :
9322 : : /* If type is not the array type, then it is the element type. */
9323 : 4480 : if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
9324 : 4450 : eltype = gfc_get_element_type (type);
9325 : : else
9326 : : eltype = type;
9327 : :
9328 : 4480 : if (str_sz != NULL_TREE)
9329 : 43 : tmp = fold_convert (gfc_array_index_type, str_sz);
9330 : : else
9331 : 4437 : tmp = fold_convert (gfc_array_index_type,
9332 : : TYPE_SIZE_UNIT (eltype));
9333 : :
9334 : 4480 : tmp = gfc_evaluate_now (tmp, &block);
9335 : 4480 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9336 : : nelems, tmp);
9337 : 4480 : if (!no_malloc)
9338 : : {
9339 : 4449 : tmp = TREE_TYPE (gfc_conv_descriptor_data_get (src));
9340 : 4449 : tmp = gfc_call_malloc (&block, tmp, size);
9341 : 4449 : gfc_conv_descriptor_data_set (&block, dest, tmp);
9342 : : }
9343 : :
9344 : : /* We know the temporary and the value will be the same length,
9345 : : so can use memcpy. */
9346 : 4480 : if (!no_memcpy)
9347 : : {
9348 : 4050 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9349 : 4050 : tmp = build_call_expr_loc (input_location, tmp, 3,
9350 : : gfc_conv_descriptor_data_get (dest),
9351 : : gfc_conv_descriptor_data_get (src),
9352 : : fold_convert (size_type_node, size));
9353 : 4050 : gfc_add_expr_to_block (&block, tmp);
9354 : : }
9355 : : }
9356 : :
9357 : 6237 : gfc_add_expr_to_block (&block, add_when_allocated);
9358 : 6237 : tmp = gfc_finish_block (&block);
9359 : :
9360 : : /* Null the destination if the source is null; otherwise do
9361 : : the allocate and copy. */
9362 : 6237 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
9363 : : null_cond = src;
9364 : : else
9365 : 4480 : null_cond = gfc_conv_descriptor_data_get (src);
9366 : :
9367 : 6237 : null_cond = convert (pvoid_type_node, null_cond);
9368 : 6237 : null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9369 : : null_cond, null_pointer_node);
9370 : 6237 : return build3_v (COND_EXPR, null_cond, tmp, null_data);
9371 : : }
9372 : :
9373 : :
9374 : : /* Allocate dest to the same size as src, and copy data src -> dest. */
9375 : :
9376 : : tree
9377 : 5004 : gfc_duplicate_allocatable (tree dest, tree src, tree type, int rank,
9378 : : tree add_when_allocated)
9379 : : {
9380 : 5004 : return duplicate_allocatable (dest, src, type, rank, false, false,
9381 : 5004 : NULL_TREE, add_when_allocated);
9382 : : }
9383 : :
9384 : :
9385 : : /* Copy data src -> dest. */
9386 : :
9387 : : tree
9388 : 31 : gfc_copy_allocatable_data (tree dest, tree src, tree type, int rank)
9389 : : {
9390 : 31 : return duplicate_allocatable (dest, src, type, rank, true, false,
9391 : 31 : NULL_TREE, NULL_TREE);
9392 : : }
9393 : :
9394 : : /* Allocate dest to the same size as src, but don't copy anything. */
9395 : :
9396 : : tree
9397 : 855 : gfc_duplicate_allocatable_nocopy (tree dest, tree src, tree type, int rank)
9398 : : {
9399 : 855 : return duplicate_allocatable (dest, src, type, rank, false, true,
9400 : 855 : NULL_TREE, NULL_TREE);
9401 : : }
9402 : :
9403 : :
9404 : : static tree
9405 : 49 : duplicate_allocatable_coarray (tree dest, tree dest_tok, tree src,
9406 : : tree type, int rank)
9407 : : {
9408 : 49 : tree tmp;
9409 : 49 : tree size;
9410 : 49 : tree nelems;
9411 : 49 : tree null_cond;
9412 : 49 : tree null_data;
9413 : 49 : stmtblock_t block, globalblock;
9414 : :
9415 : : /* If the source is null, set the destination to null. Then,
9416 : : allocate memory to the destination. */
9417 : 49 : gfc_init_block (&block);
9418 : 49 : gfc_init_block (&globalblock);
9419 : :
9420 : 49 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
9421 : : {
9422 : 13 : gfc_se se;
9423 : 13 : symbol_attribute attr;
9424 : 13 : tree dummy_desc;
9425 : :
9426 : 13 : gfc_init_se (&se, NULL);
9427 : 13 : gfc_clear_attr (&attr);
9428 : 13 : attr.allocatable = 1;
9429 : 13 : dummy_desc = gfc_conv_scalar_to_descriptor (&se, dest, attr);
9430 : 13 : gfc_add_block_to_block (&globalblock, &se.pre);
9431 : 13 : size = TYPE_SIZE_UNIT (TREE_TYPE (type));
9432 : :
9433 : 13 : gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
9434 : 13 : gfc_allocate_using_caf_lib (&block, dummy_desc, size,
9435 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9436 : : NULL_TREE, NULL_TREE, NULL_TREE,
9437 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
9438 : 13 : null_data = gfc_finish_block (&block);
9439 : :
9440 : 13 : gfc_init_block (&block);
9441 : :
9442 : 13 : gfc_allocate_using_caf_lib (&block, dummy_desc,
9443 : : fold_convert (size_type_node, size),
9444 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9445 : : NULL_TREE, NULL_TREE, NULL_TREE,
9446 : : GFC_CAF_COARRAY_ALLOC);
9447 : :
9448 : 13 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9449 : 13 : tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
9450 : : fold_convert (size_type_node, size));
9451 : 13 : gfc_add_expr_to_block (&block, tmp);
9452 : : }
9453 : : else
9454 : : {
9455 : : /* Set the rank or unitialized memory access may be reported. */
9456 : 36 : tmp = gfc_conv_descriptor_rank (dest);
9457 : 36 : gfc_add_modify (&globalblock, tmp, build_int_cst (TREE_TYPE (tmp), rank));
9458 : :
9459 : 36 : if (rank)
9460 : 36 : nelems = gfc_full_array_size (&block, src, rank);
9461 : : else
9462 : 0 : nelems = integer_one_node;
9463 : :
9464 : 36 : tmp = fold_convert (size_type_node,
9465 : : TYPE_SIZE_UNIT (gfc_get_element_type (type)));
9466 : 36 : size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
9467 : : fold_convert (size_type_node, nelems), tmp);
9468 : :
9469 : 36 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9470 : 36 : gfc_allocate_using_caf_lib (&block, dest, fold_convert (size_type_node,
9471 : : size),
9472 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9473 : : NULL_TREE, NULL_TREE, NULL_TREE,
9474 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
9475 : 36 : null_data = gfc_finish_block (&block);
9476 : :
9477 : 36 : gfc_init_block (&block);
9478 : 36 : gfc_allocate_using_caf_lib (&block, dest,
9479 : : fold_convert (size_type_node, size),
9480 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9481 : : NULL_TREE, NULL_TREE, NULL_TREE,
9482 : : GFC_CAF_COARRAY_ALLOC);
9483 : :
9484 : 36 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9485 : 36 : tmp = build_call_expr_loc (input_location, tmp, 3,
9486 : : gfc_conv_descriptor_data_get (dest),
9487 : : gfc_conv_descriptor_data_get (src),
9488 : : fold_convert (size_type_node, size));
9489 : 36 : gfc_add_expr_to_block (&block, tmp);
9490 : : }
9491 : :
9492 : 49 : tmp = gfc_finish_block (&block);
9493 : :
9494 : : /* Null the destination if the source is null; otherwise do
9495 : : the register and copy. */
9496 : 49 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
9497 : : null_cond = src;
9498 : : else
9499 : 36 : null_cond = gfc_conv_descriptor_data_get (src);
9500 : :
9501 : 49 : null_cond = convert (pvoid_type_node, null_cond);
9502 : 49 : null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9503 : : null_cond, null_pointer_node);
9504 : 49 : gfc_add_expr_to_block (&globalblock, build3_v (COND_EXPR, null_cond, tmp,
9505 : : null_data));
9506 : 49 : return gfc_finish_block (&globalblock);
9507 : : }
9508 : :
9509 : :
9510 : : /* Helper function to abstract whether coarray processing is enabled. */
9511 : :
9512 : : static bool
9513 : 73 : caf_enabled (int caf_mode)
9514 : : {
9515 : 73 : return (caf_mode & GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY)
9516 : 73 : == GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY;
9517 : : }
9518 : :
9519 : :
9520 : : /* Helper function to abstract whether coarray processing is enabled
9521 : : and we are in a derived type coarray. */
9522 : :
9523 : : static bool
9524 : 7310 : caf_in_coarray (int caf_mode)
9525 : : {
9526 : 7310 : static const int pat = GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
9527 : : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY;
9528 : 7310 : return (caf_mode & pat) == pat;
9529 : : }
9530 : :
9531 : :
9532 : : /* Helper function to abstract whether coarray is to deallocate only. */
9533 : :
9534 : : bool
9535 : 299 : gfc_caf_is_dealloc_only (int caf_mode)
9536 : : {
9537 : 299 : return (caf_mode & GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY)
9538 : 299 : == GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY;
9539 : : }
9540 : :
9541 : :
9542 : : /* Recursively traverse an object of derived type, generating code to
9543 : : deallocate, nullify or copy allocatable components. This is the work horse
9544 : : function for the functions named in this enum. */
9545 : :
9546 : : enum {DEALLOCATE_ALLOC_COMP = 1, NULLIFY_ALLOC_COMP,
9547 : : COPY_ALLOC_COMP, COPY_ONLY_ALLOC_COMP, REASSIGN_CAF_COMP,
9548 : : ALLOCATE_PDT_COMP, DEALLOCATE_PDT_COMP, CHECK_PDT_DUMMY,
9549 : : BCAST_ALLOC_COMP};
9550 : :
9551 : : static gfc_actual_arglist *pdt_param_list;
9552 : :
9553 : : static tree
9554 : 14359 : structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest,
9555 : : int rank, int purpose, int caf_mode,
9556 : : gfc_co_subroutines_args *args,
9557 : : bool no_finalization = false)
9558 : : {
9559 : 14359 : gfc_component *c;
9560 : 14359 : gfc_loopinfo loop;
9561 : 14359 : stmtblock_t fnblock;
9562 : 14359 : stmtblock_t loopbody;
9563 : 14359 : stmtblock_t tmpblock;
9564 : 14359 : tree decl_type;
9565 : 14359 : tree tmp;
9566 : 14359 : tree comp;
9567 : 14359 : tree dcmp;
9568 : 14359 : tree nelems;
9569 : 14359 : tree index;
9570 : 14359 : tree var;
9571 : 14359 : tree cdecl;
9572 : 14359 : tree ctype;
9573 : 14359 : tree vref, dref;
9574 : 14359 : tree null_cond = NULL_TREE;
9575 : 14359 : tree add_when_allocated;
9576 : 14359 : tree dealloc_fndecl;
9577 : 14359 : tree caf_token;
9578 : 14359 : gfc_symbol *vtab;
9579 : 14359 : int caf_dereg_mode;
9580 : 14359 : symbol_attribute *attr;
9581 : 14359 : bool deallocate_called;
9582 : :
9583 : 14359 : gfc_init_block (&fnblock);
9584 : :
9585 : 14359 : decl_type = TREE_TYPE (decl);
9586 : :
9587 : 14359 : if ((POINTER_TYPE_P (decl_type))
9588 : : || (TREE_CODE (decl_type) == REFERENCE_TYPE && rank == 0))
9589 : : {
9590 : 1094 : decl = build_fold_indirect_ref_loc (input_location, decl);
9591 : : /* Deref dest in sync with decl, but only when it is not NULL. */
9592 : 1094 : if (dest)
9593 : 68 : dest = build_fold_indirect_ref_loc (input_location, dest);
9594 : :
9595 : : /* Update the decl_type because it got dereferenced. */
9596 : 1094 : decl_type = TREE_TYPE (decl);
9597 : : }
9598 : :
9599 : : /* If this is an array of derived types with allocatable components
9600 : : build a loop and recursively call this function. */
9601 : 14359 : if (TREE_CODE (decl_type) == ARRAY_TYPE
9602 : 14359 : || (GFC_DESCRIPTOR_TYPE_P (decl_type) && rank != 0))
9603 : : {
9604 : 2327 : tmp = gfc_conv_array_data (decl);
9605 : 2327 : var = build_fold_indirect_ref_loc (input_location, tmp);
9606 : :
9607 : : /* Get the number of elements - 1 and set the counter. */
9608 : 2327 : if (GFC_DESCRIPTOR_TYPE_P (decl_type))
9609 : : {
9610 : : /* Use the descriptor for an allocatable array. Since this
9611 : : is a full array reference, we only need the descriptor
9612 : : information from dimension = rank. */
9613 : 1574 : tmp = gfc_full_array_size (&fnblock, decl, rank);
9614 : 1574 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9615 : : gfc_array_index_type, tmp,
9616 : : gfc_index_one_node);
9617 : :
9618 : 1574 : null_cond = gfc_conv_descriptor_data_get (decl);
9619 : 1574 : null_cond = fold_build2_loc (input_location, NE_EXPR,
9620 : : logical_type_node, null_cond,
9621 : 1574 : build_int_cst (TREE_TYPE (null_cond), 0));
9622 : : }
9623 : : else
9624 : : {
9625 : : /* Otherwise use the TYPE_DOMAIN information. */
9626 : 753 : tmp = array_type_nelts (decl_type);
9627 : 753 : tmp = fold_convert (gfc_array_index_type, tmp);
9628 : : }
9629 : :
9630 : : /* Remember that this is, in fact, the no. of elements - 1. */
9631 : 2327 : nelems = gfc_evaluate_now (tmp, &fnblock);
9632 : 2327 : index = gfc_create_var (gfc_array_index_type, "S");
9633 : :
9634 : : /* Build the body of the loop. */
9635 : 2327 : gfc_init_block (&loopbody);
9636 : :
9637 : 2327 : vref = gfc_build_array_ref (var, index, NULL);
9638 : :
9639 : 2327 : if (purpose == COPY_ALLOC_COMP || purpose == COPY_ONLY_ALLOC_COMP)
9640 : : {
9641 : 451 : tmp = build_fold_indirect_ref_loc (input_location,
9642 : : gfc_conv_array_data (dest));
9643 : 451 : dref = gfc_build_array_ref (tmp, index, NULL);
9644 : 451 : tmp = structure_alloc_comps (der_type, vref, dref, rank,
9645 : : COPY_ALLOC_COMP, caf_mode, args,
9646 : : no_finalization);
9647 : : }
9648 : : else
9649 : 1876 : tmp = structure_alloc_comps (der_type, vref, NULL_TREE, rank, purpose,
9650 : : caf_mode, args, no_finalization);
9651 : :
9652 : 2327 : gfc_add_expr_to_block (&loopbody, tmp);
9653 : :
9654 : : /* Build the loop and return. */
9655 : 2327 : gfc_init_loopinfo (&loop);
9656 : 2327 : loop.dimen = 1;
9657 : 2327 : loop.from[0] = gfc_index_zero_node;
9658 : 2327 : loop.loopvar[0] = index;
9659 : 2327 : loop.to[0] = nelems;
9660 : 2327 : gfc_trans_scalarizing_loops (&loop, &loopbody);
9661 : 2327 : gfc_add_block_to_block (&fnblock, &loop.pre);
9662 : :
9663 : 2327 : tmp = gfc_finish_block (&fnblock);
9664 : : /* When copying allocateable components, the above implements the
9665 : : deep copy. Nevertheless is a deep copy only allowed, when the current
9666 : : component is allocated, for which code will be generated in
9667 : : gfc_duplicate_allocatable (), where the deep copy code is just added
9668 : : into the if's body, by adding tmp (the deep copy code) as last
9669 : : argument to gfc_duplicate_allocatable (). */
9670 : 2327 : if (purpose == COPY_ALLOC_COMP
9671 : 2327 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
9672 : 380 : tmp = gfc_duplicate_allocatable (dest, decl, decl_type, rank,
9673 : : tmp);
9674 : 1947 : else if (null_cond != NULL_TREE)
9675 : 1194 : tmp = build3_v (COND_EXPR, null_cond, tmp,
9676 : : build_empty_stmt (input_location));
9677 : :
9678 : 2327 : return tmp;
9679 : : }
9680 : :
9681 : 12032 : if (purpose == DEALLOCATE_ALLOC_COMP && der_type->attr.pdt_type)
9682 : : {
9683 : 35 : tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
9684 : : DEALLOCATE_PDT_COMP, 0, args,
9685 : : no_finalization);
9686 : 35 : gfc_add_expr_to_block (&fnblock, tmp);
9687 : : }
9688 : 11997 : else if (purpose == ALLOCATE_PDT_COMP && der_type->attr.alloc_comp)
9689 : : {
9690 : 14 : tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
9691 : : NULLIFY_ALLOC_COMP, 0, args,
9692 : : no_finalization);
9693 : 14 : gfc_add_expr_to_block (&fnblock, tmp);
9694 : : }
9695 : :
9696 : : /* Still having a descriptor array of rank == 0 here, indicates an
9697 : : allocatable coarrays. Dereference it correctly. */
9698 : 12032 : if (GFC_DESCRIPTOR_TYPE_P (decl_type))
9699 : : {
9700 : 4 : decl = build_fold_indirect_ref (gfc_conv_array_data (decl));
9701 : : }
9702 : : /* Otherwise, act on the components or recursively call self to
9703 : : act on a chain of components. */
9704 : 33613 : for (c = der_type->components; c; c = c->next)
9705 : : {
9706 : 21581 : bool cmp_has_alloc_comps = (c->ts.type == BT_DERIVED
9707 : 21581 : || c->ts.type == BT_CLASS)
9708 : 21581 : && c->ts.u.derived->attr.alloc_comp;
9709 : 4219 : bool same_type = (c->ts.type == BT_DERIVED && der_type == c->ts.u.derived)
9710 : 25184 : || (c->ts.type == BT_CLASS && der_type == CLASS_DATA (c)->ts.u.derived);
9711 : :
9712 : 43162 : bool is_pdt_type = c->ts.type == BT_DERIVED
9713 : 21581 : && c->ts.u.derived->attr.pdt_type;
9714 : :
9715 : 21581 : cdecl = c->backend_decl;
9716 : 21581 : ctype = TREE_TYPE (cdecl);
9717 : :
9718 : 21581 : switch (purpose)
9719 : : {
9720 : :
9721 : 3 : case BCAST_ALLOC_COMP:
9722 : :
9723 : 3 : tree ubound;
9724 : 3 : tree cdesc;
9725 : 3 : stmtblock_t derived_type_block;
9726 : :
9727 : 3 : gfc_init_block (&tmpblock);
9728 : :
9729 : 3 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9730 : : decl, cdecl, NULL_TREE);
9731 : :
9732 : : /* Shortcut to get the attributes of the component. */
9733 : 3 : if (c->ts.type == BT_CLASS)
9734 : : {
9735 : 0 : attr = &CLASS_DATA (c)->attr;
9736 : 0 : if (attr->class_pointer)
9737 : 0 : continue;
9738 : : }
9739 : : else
9740 : : {
9741 : 3 : attr = &c->attr;
9742 : 3 : if (attr->pointer)
9743 : 0 : continue;
9744 : : }
9745 : :
9746 : : /* Do not broadcast a caf_token. These are local to the image. */
9747 : 3 : if (attr->caf_token)
9748 : 1 : continue;
9749 : :
9750 : 2 : add_when_allocated = NULL_TREE;
9751 : 2 : if (cmp_has_alloc_comps
9752 : 0 : && !c->attr.pointer && !c->attr.proc_pointer)
9753 : : {
9754 : 0 : if (c->ts.type == BT_CLASS)
9755 : : {
9756 : 0 : rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
9757 : 0 : add_when_allocated
9758 : 0 : = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
9759 : : comp, NULL_TREE, rank, purpose,
9760 : : caf_mode, args, no_finalization);
9761 : : }
9762 : : else
9763 : : {
9764 : 0 : rank = c->as ? c->as->rank : 0;
9765 : 0 : add_when_allocated = structure_alloc_comps (c->ts.u.derived,
9766 : : comp, NULL_TREE,
9767 : : rank, purpose,
9768 : : caf_mode, args,
9769 : : no_finalization);
9770 : : }
9771 : : }
9772 : :
9773 : 2 : gfc_init_block (&derived_type_block);
9774 : 2 : if (add_when_allocated)
9775 : 0 : gfc_add_expr_to_block (&derived_type_block, add_when_allocated);
9776 : 2 : tmp = gfc_finish_block (&derived_type_block);
9777 : 2 : gfc_add_expr_to_block (&tmpblock, tmp);
9778 : :
9779 : : /* Convert the component into a rank 1 descriptor type. */
9780 : 2 : if (attr->dimension)
9781 : : {
9782 : 0 : tmp = gfc_get_element_type (TREE_TYPE (comp));
9783 : 0 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
9784 : 0 : ubound = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (comp));
9785 : : else
9786 : 0 : ubound = gfc_full_array_size (&tmpblock, comp,
9787 : 0 : c->ts.type == BT_CLASS
9788 : 0 : ? CLASS_DATA (c)->as->rank
9789 : 0 : : c->as->rank);
9790 : : }
9791 : : else
9792 : : {
9793 : 2 : tmp = TREE_TYPE (comp);
9794 : 2 : ubound = build_int_cst (gfc_array_index_type, 1);
9795 : : }
9796 : :
9797 : : /* Treat strings like arrays. Or the other way around, do not
9798 : : * generate an additional array layer for scalar components. */
9799 : 2 : if (attr->dimension || c->ts.type == BT_CHARACTER)
9800 : : {
9801 : 0 : cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
9802 : : &ubound, 1,
9803 : : GFC_ARRAY_ALLOCATABLE, false);
9804 : :
9805 : 0 : cdesc = gfc_create_var (cdesc, "cdesc");
9806 : 0 : DECL_ARTIFICIAL (cdesc) = 1;
9807 : :
9808 : 0 : gfc_add_modify (&tmpblock, gfc_conv_descriptor_dtype (cdesc),
9809 : : gfc_get_dtype_rank_type (1, tmp));
9810 : 0 : gfc_conv_descriptor_lbound_set (&tmpblock, cdesc,
9811 : : gfc_index_zero_node,
9812 : : gfc_index_one_node);
9813 : 0 : gfc_conv_descriptor_stride_set (&tmpblock, cdesc,
9814 : : gfc_index_zero_node,
9815 : : gfc_index_one_node);
9816 : 0 : gfc_conv_descriptor_ubound_set (&tmpblock, cdesc,
9817 : : gfc_index_zero_node, ubound);
9818 : : }
9819 : : else
9820 : : /* Prevent warning. */
9821 : : cdesc = NULL_TREE;
9822 : :
9823 : 2 : if (attr->dimension)
9824 : : {
9825 : 0 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
9826 : 0 : comp = gfc_conv_descriptor_data_get (comp);
9827 : : else
9828 : 0 : comp = gfc_build_addr_expr (NULL_TREE, comp);
9829 : : }
9830 : : else
9831 : : {
9832 : 2 : gfc_se se;
9833 : :
9834 : 2 : gfc_init_se (&se, NULL);
9835 : :
9836 : 2 : comp = gfc_conv_scalar_to_descriptor (&se, comp,
9837 : 2 : c->ts.type == BT_CLASS
9838 : 2 : ? CLASS_DATA (c)->attr
9839 : : : c->attr);
9840 : 2 : if (c->ts.type == BT_CHARACTER)
9841 : 0 : comp = gfc_build_addr_expr (NULL_TREE, comp);
9842 : 2 : gfc_add_block_to_block (&tmpblock, &se.pre);
9843 : : }
9844 : :
9845 : 2 : if (attr->dimension || c->ts.type == BT_CHARACTER)
9846 : 0 : gfc_conv_descriptor_data_set (&tmpblock, cdesc, comp);
9847 : : else
9848 : 2 : cdesc = comp;
9849 : :
9850 : 2 : tree fndecl;
9851 : :
9852 : 2 : fndecl = build_call_expr_loc (input_location,
9853 : : gfor_fndecl_co_broadcast, 5,
9854 : : gfc_build_addr_expr (pvoid_type_node,cdesc),
9855 : : args->image_index,
9856 : : null_pointer_node, null_pointer_node,
9857 : : null_pointer_node);
9858 : :
9859 : 2 : gfc_add_expr_to_block (&tmpblock, fndecl);
9860 : 2 : gfc_add_block_to_block (&fnblock, &tmpblock);
9861 : :
9862 : 18038 : break;
9863 : :
9864 : 8865 : case DEALLOCATE_ALLOC_COMP:
9865 : :
9866 : 8865 : gfc_init_block (&tmpblock);
9867 : :
9868 : 8865 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9869 : : decl, cdecl, NULL_TREE);
9870 : :
9871 : : /* Shortcut to get the attributes of the component. */
9872 : 8865 : if (c->ts.type == BT_CLASS)
9873 : : {
9874 : 881 : attr = &CLASS_DATA (c)->attr;
9875 : 881 : if (attr->class_pointer)
9876 : 6 : continue;
9877 : : }
9878 : : else
9879 : : {
9880 : 7984 : attr = &c->attr;
9881 : 7984 : if (attr->pointer)
9882 : 72 : continue;
9883 : : }
9884 : :
9885 : 8787 : if (!no_finalization && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
9886 : 6850 : || (c->ts.type == BT_CLASS && !CLASS_DATA (c)->attr.class_pointer)))
9887 : : /* Call the finalizer, which will free the memory and nullify the
9888 : : pointer of an array. */
9889 : 2517 : deallocate_called = gfc_add_comp_finalizer_call (&tmpblock, comp, c,
9890 : 2517 : caf_enabled (caf_mode))
9891 : 2517 : && attr->dimension;
9892 : : else
9893 : : deallocate_called = false;
9894 : :
9895 : : /* Add the _class ref for classes. */
9896 : 8787 : if (c->ts.type == BT_CLASS && attr->allocatable)
9897 : 875 : comp = gfc_class_data_get (comp);
9898 : :
9899 : 8787 : add_when_allocated = NULL_TREE;
9900 : 8787 : if (cmp_has_alloc_comps
9901 : 1775 : && !c->attr.pointer && !c->attr.proc_pointer
9902 : : && !same_type
9903 : 1751 : && !deallocate_called)
9904 : : {
9905 : : /* Add checked deallocation of the components. This code is
9906 : : obviously added because the finalizer is not trusted to free
9907 : : all memory. */
9908 : 1169 : if (c->ts.type == BT_CLASS)
9909 : : {
9910 : 241 : rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
9911 : 241 : add_when_allocated
9912 : 241 : = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
9913 : : comp, NULL_TREE, rank, purpose,
9914 : : caf_mode, args, no_finalization);
9915 : : }
9916 : : else
9917 : : {
9918 : 928 : rank = c->as ? c->as->rank : 0;
9919 : 928 : add_when_allocated = structure_alloc_comps (c->ts.u.derived,
9920 : : comp, NULL_TREE,
9921 : : rank, purpose,
9922 : : caf_mode, args,
9923 : : no_finalization);
9924 : : }
9925 : : }
9926 : :
9927 : 5995 : if (attr->allocatable && !same_type
9928 : 14392 : && (!attr->codimension || caf_enabled (caf_mode)))
9929 : : {
9930 : : /* Handle all types of components besides components of the
9931 : : same_type as the current one, because those would create an
9932 : : endless loop. */
9933 : 5599 : caf_dereg_mode
9934 : 5549 : = (caf_in_coarray (caf_mode) || attr->codimension)
9935 : 5620 : ? (gfc_caf_is_dealloc_only (caf_mode)
9936 : : ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
9937 : : : GFC_CAF_COARRAY_DEREGISTER)
9938 : : : GFC_CAF_COARRAY_NOCOARRAY;
9939 : :
9940 : 5599 : caf_token = NULL_TREE;
9941 : : /* Coarray components are handled directly by
9942 : : deallocate_with_status. */
9943 : 5599 : if (!attr->codimension
9944 : 5578 : && caf_dereg_mode != GFC_CAF_COARRAY_NOCOARRAY)
9945 : : {
9946 : 50 : if (c->caf_token)
9947 : 17 : caf_token = fold_build3_loc (input_location, COMPONENT_REF,
9948 : 17 : TREE_TYPE (c->caf_token),
9949 : : decl, c->caf_token, NULL_TREE);
9950 : 33 : else if (attr->dimension && !attr->proc_pointer)
9951 : 33 : caf_token = gfc_conv_descriptor_token (comp);
9952 : : }
9953 : :
9954 : 5599 : tmp = gfc_deallocate_with_status (comp, NULL_TREE, NULL_TREE,
9955 : : NULL_TREE, NULL_TREE, true,
9956 : : NULL, caf_dereg_mode, NULL_TREE,
9957 : : add_when_allocated, caf_token);
9958 : :
9959 : 5599 : gfc_add_expr_to_block (&tmpblock, tmp);
9960 : : }
9961 : 3188 : else if (attr->allocatable && !attr->codimension
9962 : 390 : && !deallocate_called)
9963 : : {
9964 : : /* Case of recursive allocatable derived types. */
9965 : 390 : tree is_allocated;
9966 : 390 : tree ubound;
9967 : 390 : tree cdesc;
9968 : 390 : stmtblock_t dealloc_block;
9969 : :
9970 : 390 : gfc_init_block (&dealloc_block);
9971 : 390 : if (add_when_allocated)
9972 : 0 : gfc_add_expr_to_block (&dealloc_block, add_when_allocated);
9973 : :
9974 : : /* Convert the component into a rank 1 descriptor type. */
9975 : 390 : if (attr->dimension)
9976 : : {
9977 : 34 : tmp = gfc_get_element_type (TREE_TYPE (comp));
9978 : 34 : ubound = gfc_full_array_size (&dealloc_block, comp,
9979 : 34 : c->ts.type == BT_CLASS
9980 : 0 : ? CLASS_DATA (c)->as->rank
9981 : 34 : : c->as->rank);
9982 : : }
9983 : : else
9984 : : {
9985 : 356 : tmp = TREE_TYPE (comp);
9986 : 356 : ubound = build_int_cst (gfc_array_index_type, 1);
9987 : : }
9988 : :
9989 : 390 : cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
9990 : : &ubound, 1,
9991 : : GFC_ARRAY_ALLOCATABLE, false);
9992 : :
9993 : 390 : cdesc = gfc_create_var (cdesc, "cdesc");
9994 : 390 : DECL_ARTIFICIAL (cdesc) = 1;
9995 : :
9996 : 390 : gfc_add_modify (&dealloc_block, gfc_conv_descriptor_dtype (cdesc),
9997 : : gfc_get_dtype_rank_type (1, tmp));
9998 : 390 : gfc_conv_descriptor_lbound_set (&dealloc_block, cdesc,
9999 : : gfc_index_zero_node,
10000 : : gfc_index_one_node);
10001 : 390 : gfc_conv_descriptor_stride_set (&dealloc_block, cdesc,
10002 : : gfc_index_zero_node,
10003 : : gfc_index_one_node);
10004 : 390 : gfc_conv_descriptor_ubound_set (&dealloc_block, cdesc,
10005 : : gfc_index_zero_node, ubound);
10006 : :
10007 : 390 : if (attr->dimension)
10008 : 34 : comp = gfc_conv_descriptor_data_get (comp);
10009 : :
10010 : 390 : gfc_conv_descriptor_data_set (&dealloc_block, cdesc, comp);
10011 : :
10012 : : /* Now call the deallocator. */
10013 : 390 : vtab = gfc_find_vtab (&c->ts);
10014 : 390 : if (vtab->backend_decl == NULL)
10015 : 8 : gfc_get_symbol_decl (vtab);
10016 : 390 : tmp = gfc_build_addr_expr (NULL_TREE, vtab->backend_decl);
10017 : 390 : dealloc_fndecl = gfc_vptr_deallocate_get (tmp);
10018 : 390 : dealloc_fndecl = build_fold_indirect_ref_loc (input_location,
10019 : : dealloc_fndecl);
10020 : 390 : tmp = build_int_cst (TREE_TYPE (comp), 0);
10021 : 390 : is_allocated = fold_build2_loc (input_location, NE_EXPR,
10022 : : logical_type_node, tmp,
10023 : : comp);
10024 : 390 : cdesc = gfc_build_addr_expr (NULL_TREE, cdesc);
10025 : :
10026 : 390 : tmp = build_call_expr_loc (input_location,
10027 : : dealloc_fndecl, 1,
10028 : : cdesc);
10029 : 390 : gfc_add_expr_to_block (&dealloc_block, tmp);
10030 : :
10031 : 390 : tmp = gfc_finish_block (&dealloc_block);
10032 : :
10033 : 390 : tmp = fold_build3_loc (input_location, COND_EXPR,
10034 : : void_type_node, is_allocated, tmp,
10035 : : build_empty_stmt (input_location));
10036 : :
10037 : 390 : gfc_add_expr_to_block (&tmpblock, tmp);
10038 : 390 : }
10039 : 2798 : else if (add_when_allocated)
10040 : 491 : gfc_add_expr_to_block (&tmpblock, add_when_allocated);
10041 : :
10042 : 875 : if (c->ts.type == BT_CLASS && attr->allocatable
10043 : 9662 : && (!attr->codimension || !caf_enabled (caf_mode)))
10044 : : {
10045 : : /* Finally, reset the vptr to the declared type vtable and, if
10046 : : necessary reset the _len field.
10047 : :
10048 : : First recover the reference to the component and obtain
10049 : : the vptr. */
10050 : 860 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10051 : : decl, cdecl, NULL_TREE);
10052 : 860 : tmp = gfc_class_vptr_get (comp);
10053 : :
10054 : 860 : if (UNLIMITED_POLY (c))
10055 : : {
10056 : : /* Both vptr and _len field should be nulled. */
10057 : 162 : gfc_add_modify (&tmpblock, tmp,
10058 : 162 : build_int_cst (TREE_TYPE (tmp), 0));
10059 : 162 : tmp = gfc_class_len_get (comp);
10060 : 162 : gfc_add_modify (&tmpblock, tmp,
10061 : 162 : build_int_cst (TREE_TYPE (tmp), 0));
10062 : : }
10063 : : else
10064 : : {
10065 : : /* Build the vtable address and set the vptr with it. */
10066 : 698 : gfc_reset_vptr (&tmpblock, nullptr, tmp, c->ts.u.derived);
10067 : : }
10068 : : }
10069 : :
10070 : : /* Now add the deallocation of this component. */
10071 : 8787 : gfc_add_block_to_block (&fnblock, &tmpblock);
10072 : 8787 : break;
10073 : :
10074 : 3581 : case NULLIFY_ALLOC_COMP:
10075 : : /* Nullify
10076 : : - allocatable components (regular or in class)
10077 : : - components that have allocatable components
10078 : : - pointer components when in a coarray.
10079 : : Skip everything else especially proc_pointers, which may come
10080 : : coupled with the regular pointer attribute. */
10081 : 4724 : if (c->attr.proc_pointer
10082 : 3581 : || !(c->attr.allocatable || (c->ts.type == BT_CLASS
10083 : 411 : && CLASS_DATA (c)->attr.allocatable)
10084 : 1457 : || (cmp_has_alloc_comps
10085 : 220 : && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
10086 : 18 : || (c->ts.type == BT_CLASS
10087 : 12 : && !CLASS_DATA (c)->attr.class_pointer)))
10088 : 1255 : || (caf_in_coarray (caf_mode) && c->attr.pointer)))
10089 : 1143 : continue;
10090 : :
10091 : : /* Process class components first, because they always have the
10092 : : pointer-attribute set which would be caught wrong else. */
10093 : 2438 : if (c->ts.type == BT_CLASS
10094 : 398 : && (CLASS_DATA (c)->attr.allocatable
10095 : 398 : || CLASS_DATA (c)->attr.class_pointer))
10096 : : {
10097 : 398 : tree class_ref;
10098 : :
10099 : : /* Allocatable CLASS components. */
10100 : 398 : class_ref = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10101 : : decl, cdecl, NULL_TREE);
10102 : :
10103 : 398 : comp = gfc_class_data_get (class_ref);
10104 : 398 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
10105 : 215 : gfc_conv_descriptor_data_set (&fnblock, comp,
10106 : : null_pointer_node);
10107 : : else
10108 : : {
10109 : 183 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
10110 : : void_type_node, comp,
10111 : 183 : build_int_cst (TREE_TYPE (comp), 0));
10112 : 183 : gfc_add_expr_to_block (&fnblock, tmp);
10113 : : }
10114 : :
10115 : : /* The dynamic type of a disassociated pointer or unallocated
10116 : : allocatable variable is its declared type. An unlimited
10117 : : polymorphic entity has no declared type. */
10118 : 398 : gfc_reset_vptr (&fnblock, nullptr, class_ref, c->ts.u.derived);
10119 : :
10120 : 398 : cmp_has_alloc_comps = false;
10121 : 398 : }
10122 : : /* Coarrays need the component to be nulled before the api-call
10123 : : is made. */
10124 : 2040 : else if (c->attr.pointer || c->attr.allocatable)
10125 : : {
10126 : 1838 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10127 : : decl, cdecl, NULL_TREE);
10128 : 1838 : if (c->attr.dimension || c->attr.codimension)
10129 : 1333 : gfc_conv_descriptor_data_set (&fnblock, comp,
10130 : : null_pointer_node);
10131 : : else
10132 : 505 : gfc_add_modify (&fnblock, comp,
10133 : 505 : build_int_cst (TREE_TYPE (comp), 0));
10134 : 1838 : if (gfc_deferred_strlen (c, &comp))
10135 : : {
10136 : 244 : comp = fold_build3_loc (input_location, COMPONENT_REF,
10137 : 244 : TREE_TYPE (comp),
10138 : : decl, comp, NULL_TREE);
10139 : 488 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
10140 : 244 : TREE_TYPE (comp), comp,
10141 : 244 : build_int_cst (TREE_TYPE (comp), 0));
10142 : 244 : gfc_add_expr_to_block (&fnblock, tmp);
10143 : : }
10144 : : cmp_has_alloc_comps = false;
10145 : : }
10146 : :
10147 : 2438 : if (flag_coarray == GFC_FCOARRAY_LIB && caf_in_coarray (caf_mode))
10148 : : {
10149 : : /* Register a component of a derived type coarray with the
10150 : : coarray library. Do not register ultimate component
10151 : : coarrays here. They are treated like regular coarrays and
10152 : : are either allocated on all images or on none. */
10153 : 124 : tree token;
10154 : :
10155 : 124 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10156 : : decl, cdecl, NULL_TREE);
10157 : 124 : if (c->attr.dimension)
10158 : : {
10159 : : /* Set the dtype, because caf_register needs it. */
10160 : 99 : gfc_add_modify (&fnblock, gfc_conv_descriptor_dtype (comp),
10161 : 99 : gfc_get_dtype (TREE_TYPE (comp)));
10162 : 99 : tmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10163 : : decl, cdecl, NULL_TREE);
10164 : 99 : token = gfc_conv_descriptor_token (tmp);
10165 : : }
10166 : : else
10167 : : {
10168 : 25 : gfc_se se;
10169 : :
10170 : 25 : gfc_init_se (&se, NULL);
10171 : 25 : token = fold_build3_loc (input_location, COMPONENT_REF,
10172 : : pvoid_type_node, decl, c->caf_token,
10173 : : NULL_TREE);
10174 : 25 : comp = gfc_conv_scalar_to_descriptor (&se, comp,
10175 : 25 : c->ts.type == BT_CLASS
10176 : 25 : ? CLASS_DATA (c)->attr
10177 : : : c->attr);
10178 : 25 : gfc_add_block_to_block (&fnblock, &se.pre);
10179 : : }
10180 : :
10181 : 124 : gfc_allocate_using_caf_lib (&fnblock, comp, size_zero_node,
10182 : : gfc_build_addr_expr (NULL_TREE,
10183 : : token),
10184 : : NULL_TREE, NULL_TREE, NULL_TREE,
10185 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
10186 : : }
10187 : :
10188 : 2438 : if (cmp_has_alloc_comps)
10189 : : {
10190 : 202 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10191 : : decl, cdecl, NULL_TREE);
10192 : 202 : rank = c->as ? c->as->rank : 0;
10193 : 202 : tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE,
10194 : : rank, purpose, caf_mode, args,
10195 : : no_finalization);
10196 : 202 : gfc_add_expr_to_block (&fnblock, tmp);
10197 : : }
10198 : : break;
10199 : :
10200 : 29 : case REASSIGN_CAF_COMP:
10201 : 29 : if (caf_enabled (caf_mode)
10202 : 29 : && (c->attr.codimension
10203 : 23 : || (c->ts.type == BT_CLASS
10204 : 2 : && (CLASS_DATA (c)->attr.coarray_comp
10205 : 2 : || caf_in_coarray (caf_mode)))
10206 : 21 : || (c->ts.type == BT_DERIVED
10207 : 7 : && (c->ts.u.derived->attr.coarray_comp
10208 : 6 : || caf_in_coarray (caf_mode))))
10209 : 44 : && !same_type)
10210 : : {
10211 : 13 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10212 : : decl, cdecl, NULL_TREE);
10213 : 13 : dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10214 : : dest, cdecl, NULL_TREE);
10215 : :
10216 : 13 : if (c->attr.codimension)
10217 : : {
10218 : 6 : if (c->ts.type == BT_CLASS)
10219 : : {
10220 : 0 : comp = gfc_class_data_get (comp);
10221 : 0 : dcmp = gfc_class_data_get (dcmp);
10222 : : }
10223 : 6 : gfc_conv_descriptor_data_set (&fnblock, dcmp,
10224 : : gfc_conv_descriptor_data_get (comp));
10225 : : }
10226 : : else
10227 : : {
10228 : 7 : tmp = structure_alloc_comps (c->ts.u.derived, comp, dcmp,
10229 : : rank, purpose, caf_mode
10230 : : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY,
10231 : : args, no_finalization);
10232 : 7 : gfc_add_expr_to_block (&fnblock, tmp);
10233 : : }
10234 : : }
10235 : : break;
10236 : :
10237 : 6960 : case COPY_ALLOC_COMP:
10238 : 6960 : if (c->attr.pointer || c->attr.proc_pointer)
10239 : 144 : continue;
10240 : :
10241 : : /* We need source and destination components. */
10242 : 6816 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype, decl,
10243 : : cdecl, NULL_TREE);
10244 : 6816 : dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype, dest,
10245 : : cdecl, NULL_TREE);
10246 : 6816 : dcmp = fold_convert (TREE_TYPE (comp), dcmp);
10247 : :
10248 : 6816 : if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
10249 : : {
10250 : 650 : tree ftn_tree;
10251 : 650 : tree size;
10252 : 650 : tree dst_data;
10253 : 650 : tree src_data;
10254 : 650 : tree null_data;
10255 : :
10256 : 650 : dst_data = gfc_class_data_get (dcmp);
10257 : 650 : src_data = gfc_class_data_get (comp);
10258 : 650 : size = fold_convert (size_type_node,
10259 : : gfc_class_vtab_size_get (comp));
10260 : :
10261 : 650 : if (CLASS_DATA (c)->attr.dimension)
10262 : : {
10263 : 640 : nelems = gfc_conv_descriptor_size (src_data,
10264 : 320 : CLASS_DATA (c)->as->rank);
10265 : 320 : size = fold_build2_loc (input_location, MULT_EXPR,
10266 : : size_type_node, size,
10267 : : fold_convert (size_type_node,
10268 : : nelems));
10269 : : }
10270 : : else
10271 : 330 : nelems = build_int_cst (size_type_node, 1);
10272 : :
10273 : 650 : if (CLASS_DATA (c)->attr.dimension
10274 : 650 : || CLASS_DATA (c)->attr.codimension)
10275 : : {
10276 : 328 : src_data = gfc_conv_descriptor_data_get (src_data);
10277 : 328 : dst_data = gfc_conv_descriptor_data_get (dst_data);
10278 : : }
10279 : :
10280 : 650 : gfc_init_block (&tmpblock);
10281 : :
10282 : 650 : gfc_add_modify (&tmpblock, gfc_class_vptr_get (dcmp),
10283 : : gfc_class_vptr_get (comp));
10284 : :
10285 : : /* Copy the unlimited '_len' field. If it is greater than zero
10286 : : (ie. a character(_len)), multiply it by size and use this
10287 : : for the malloc call. */
10288 : 650 : if (UNLIMITED_POLY (c))
10289 : : {
10290 : 111 : gfc_add_modify (&tmpblock, gfc_class_len_get (dcmp),
10291 : : gfc_class_len_get (comp));
10292 : 111 : size = gfc_resize_class_size_with_len (&tmpblock, comp, size);
10293 : : }
10294 : :
10295 : : /* Coarray component have to have the same allocation status and
10296 : : shape/type-parameter/effective-type on the LHS and RHS of an
10297 : : intrinsic assignment. Hence, we did not deallocated them - and
10298 : : do not allocate them here. */
10299 : 650 : if (!CLASS_DATA (c)->attr.codimension)
10300 : : {
10301 : 635 : ftn_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
10302 : 635 : tmp = build_call_expr_loc (input_location, ftn_tree, 1, size);
10303 : 635 : gfc_add_modify (&tmpblock, dst_data,
10304 : 635 : fold_convert (TREE_TYPE (dst_data), tmp));
10305 : : }
10306 : :
10307 : 1285 : tmp = gfc_copy_class_to_class (comp, dcmp, nelems,
10308 : 650 : UNLIMITED_POLY (c));
10309 : 650 : gfc_add_expr_to_block (&tmpblock, tmp);
10310 : 650 : tmp = gfc_finish_block (&tmpblock);
10311 : :
10312 : 650 : gfc_init_block (&tmpblock);
10313 : 650 : gfc_add_modify (&tmpblock, dst_data,
10314 : 650 : fold_convert (TREE_TYPE (dst_data),
10315 : : null_pointer_node));
10316 : 650 : null_data = gfc_finish_block (&tmpblock);
10317 : :
10318 : 650 : null_cond = fold_build2_loc (input_location, NE_EXPR,
10319 : : logical_type_node, src_data,
10320 : : null_pointer_node);
10321 : :
10322 : 650 : gfc_add_expr_to_block (&fnblock, build3_v (COND_EXPR, null_cond,
10323 : : tmp, null_data));
10324 : 650 : continue;
10325 : 650 : }
10326 : :
10327 : : /* To implement guarded deep copy, i.e., deep copy only allocatable
10328 : : components that are really allocated, the deep copy code has to
10329 : : be generated first and then added to the if-block in
10330 : : gfc_duplicate_allocatable (). */
10331 : 6166 : if (cmp_has_alloc_comps && !c->attr.proc_pointer && !same_type)
10332 : : {
10333 : 976 : rank = c->as ? c->as->rank : 0;
10334 : 976 : tmp = fold_convert (TREE_TYPE (dcmp), comp);
10335 : 976 : gfc_add_modify (&fnblock, dcmp, tmp);
10336 : 976 : add_when_allocated = structure_alloc_comps (c->ts.u.derived,
10337 : : comp, dcmp,
10338 : : rank, purpose,
10339 : : caf_mode, args,
10340 : : no_finalization);
10341 : : }
10342 : : else
10343 : : add_when_allocated = NULL_TREE;
10344 : :
10345 : 6166 : if (gfc_deferred_strlen (c, &tmp))
10346 : : {
10347 : 316 : tree len, size;
10348 : 316 : len = tmp;
10349 : 316 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
10350 : 316 : TREE_TYPE (len),
10351 : : decl, len, NULL_TREE);
10352 : 316 : len = fold_build3_loc (input_location, COMPONENT_REF,
10353 : 316 : TREE_TYPE (len),
10354 : : dest, len, NULL_TREE);
10355 : 316 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
10356 : 316 : TREE_TYPE (len), len, tmp);
10357 : 316 : gfc_add_expr_to_block (&fnblock, tmp);
10358 : 316 : size = size_of_string_in_bytes (c->ts.kind, len);
10359 : : /* This component cannot have allocatable components,
10360 : : therefore add_when_allocated of duplicate_allocatable ()
10361 : : is always NULL. */
10362 : 316 : rank = c->as ? c->as->rank : 0;
10363 : 316 : tmp = duplicate_allocatable (dcmp, comp, ctype, rank,
10364 : : false, false, size, NULL_TREE);
10365 : 316 : gfc_add_expr_to_block (&fnblock, tmp);
10366 : : }
10367 : 5850 : else if (c->attr.pdt_array)
10368 : : {
10369 : 31 : tmp = duplicate_allocatable (dcmp, comp, ctype,
10370 : 31 : c->as ? c->as->rank : 0,
10371 : : false, false, NULL_TREE, NULL_TREE);
10372 : 31 : gfc_add_expr_to_block (&fnblock, tmp);
10373 : : }
10374 : 5819 : else if ((c->attr.allocatable)
10375 : 3262 : && !c->attr.proc_pointer && !same_type
10376 : 9016 : && (!(cmp_has_alloc_comps && c->as) || c->attr.codimension
10377 : 248 : || caf_in_coarray (caf_mode)))
10378 : : {
10379 : 2950 : rank = c->as ? c->as->rank : 0;
10380 : 2950 : if (c->attr.codimension)
10381 : 13 : tmp = gfc_copy_allocatable_data (dcmp, comp, ctype, rank);
10382 : 2937 : else if (flag_coarray == GFC_FCOARRAY_LIB
10383 : 2937 : && caf_in_coarray (caf_mode))
10384 : : {
10385 : 49 : tree dst_tok;
10386 : 49 : if (c->as)
10387 : 36 : dst_tok = gfc_conv_descriptor_token (dcmp);
10388 : : else
10389 : : {
10390 : : /* For a scalar allocatable component the caf_token is
10391 : : the next component. */
10392 : 13 : if (!c->caf_token)
10393 : 1 : c->caf_token = c->next->backend_decl;
10394 : 13 : dst_tok = fold_build3_loc (input_location,
10395 : : COMPONENT_REF,
10396 : : pvoid_type_node, dest,
10397 : : c->caf_token,
10398 : : NULL_TREE);
10399 : : }
10400 : 49 : tmp = duplicate_allocatable_coarray (dcmp, dst_tok, comp,
10401 : : ctype, rank);
10402 : : }
10403 : : else
10404 : 2888 : tmp = gfc_duplicate_allocatable (dcmp, comp, ctype, rank,
10405 : : add_when_allocated);
10406 : 2950 : gfc_add_expr_to_block (&fnblock, tmp);
10407 : : }
10408 : : else
10409 : 2869 : if (cmp_has_alloc_comps || is_pdt_type)
10410 : 997 : gfc_add_expr_to_block (&fnblock, add_when_allocated);
10411 : :
10412 : : break;
10413 : :
10414 : 900 : case ALLOCATE_PDT_COMP:
10415 : :
10416 : 900 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10417 : : decl, cdecl, NULL_TREE);
10418 : :
10419 : : /* Set the PDT KIND and LEN fields. */
10420 : 900 : if (c->attr.pdt_kind || c->attr.pdt_len)
10421 : : {
10422 : 439 : gfc_se tse;
10423 : 439 : gfc_expr *c_expr = NULL;
10424 : 439 : gfc_actual_arglist *param = pdt_param_list;
10425 : 439 : gfc_init_se (&tse, NULL);
10426 : 1767 : for (; param; param = param->next)
10427 : 889 : if (param->name && !strcmp (c->name, param->name))
10428 : 415 : c_expr = param->expr;
10429 : :
10430 : 439 : if (!c_expr)
10431 : 24 : c_expr = c->initializer;
10432 : :
10433 : 24 : if (c_expr)
10434 : : {
10435 : 439 : gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
10436 : 439 : gfc_add_modify (&fnblock, comp, tse.expr);
10437 : : }
10438 : : }
10439 : :
10440 : 900 : if (c->attr.pdt_string)
10441 : : {
10442 : 72 : gfc_se tse;
10443 : 72 : gfc_init_se (&tse, NULL);
10444 : 72 : tree strlen = NULL_TREE;
10445 : 72 : gfc_expr *e = gfc_copy_expr (c->ts.u.cl->length);
10446 : : /* Convert the parameterized string length to its value. The
10447 : : string length is stored in a hidden field in the same way as
10448 : : deferred string lengths. */
10449 : 72 : gfc_insert_parameter_exprs (e, pdt_param_list);
10450 : 72 : if (gfc_deferred_strlen (c, &strlen) && strlen != NULL_TREE)
10451 : : {
10452 : 72 : gfc_conv_expr_type (&tse, e,
10453 : 72 : TREE_TYPE (strlen));
10454 : 72 : strlen = fold_build3_loc (input_location, COMPONENT_REF,
10455 : 72 : TREE_TYPE (strlen),
10456 : : decl, strlen, NULL_TREE);
10457 : 72 : gfc_add_modify (&fnblock, strlen, tse.expr);
10458 : 72 : c->ts.u.cl->backend_decl = strlen;
10459 : : }
10460 : 72 : gfc_free_expr (e);
10461 : :
10462 : : /* Scalar parameterized strings can be allocated now. */
10463 : 72 : if (!c->as)
10464 : : {
10465 : 72 : tmp = fold_convert (gfc_array_index_type, strlen);
10466 : 72 : tmp = size_of_string_in_bytes (c->ts.kind, tmp);
10467 : 72 : tmp = gfc_evaluate_now (tmp, &fnblock);
10468 : 72 : tmp = gfc_call_malloc (&fnblock, TREE_TYPE (comp), tmp);
10469 : 72 : gfc_add_modify (&fnblock, comp, tmp);
10470 : : }
10471 : : }
10472 : :
10473 : : /* Allocate parameterized arrays of parameterized derived types. */
10474 : 900 : if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
10475 : 735 : && !((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10476 : 62 : && (c->ts.u.derived && c->ts.u.derived->attr.pdt_type)))
10477 : 673 : continue;
10478 : :
10479 : 227 : if (c->ts.type == BT_CLASS)
10480 : 0 : comp = gfc_class_data_get (comp);
10481 : :
10482 : 227 : if (c->attr.pdt_array)
10483 : : {
10484 : 165 : gfc_se tse;
10485 : 165 : int i;
10486 : 165 : tree size = gfc_index_one_node;
10487 : 165 : tree offset = gfc_index_zero_node;
10488 : 165 : tree lower, upper;
10489 : 165 : gfc_expr *e;
10490 : :
10491 : : /* This chunk takes the expressions for 'lower' and 'upper'
10492 : : in the arrayspec and substitutes in the expressions for
10493 : : the parameters from 'pdt_param_list'. The descriptor
10494 : : fields can then be filled from the values so obtained. */
10495 : 165 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)));
10496 : 408 : for (i = 0; i < c->as->rank; i++)
10497 : : {
10498 : 243 : gfc_init_se (&tse, NULL);
10499 : 243 : e = gfc_copy_expr (c->as->lower[i]);
10500 : 243 : gfc_insert_parameter_exprs (e, pdt_param_list);
10501 : 243 : gfc_conv_expr_type (&tse, e, gfc_array_index_type);
10502 : 243 : gfc_free_expr (e);
10503 : 243 : lower = tse.expr;
10504 : 243 : gfc_conv_descriptor_lbound_set (&fnblock, comp,
10505 : : gfc_rank_cst[i],
10506 : : lower);
10507 : 243 : e = gfc_copy_expr (c->as->upper[i]);
10508 : 243 : gfc_insert_parameter_exprs (e, pdt_param_list);
10509 : 243 : gfc_conv_expr_type (&tse, e, gfc_array_index_type);
10510 : 243 : gfc_free_expr (e);
10511 : 243 : upper = tse.expr;
10512 : 243 : gfc_conv_descriptor_ubound_set (&fnblock, comp,
10513 : : gfc_rank_cst[i],
10514 : : upper);
10515 : 243 : gfc_conv_descriptor_stride_set (&fnblock, comp,
10516 : : gfc_rank_cst[i],
10517 : : size);
10518 : 243 : size = gfc_evaluate_now (size, &fnblock);
10519 : 243 : offset = fold_build2_loc (input_location,
10520 : : MINUS_EXPR,
10521 : : gfc_array_index_type,
10522 : : offset, size);
10523 : 243 : offset = gfc_evaluate_now (offset, &fnblock);
10524 : 243 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
10525 : : gfc_array_index_type,
10526 : : upper, lower);
10527 : 243 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
10528 : : gfc_array_index_type,
10529 : : tmp, gfc_index_one_node);
10530 : 243 : size = fold_build2_loc (input_location, MULT_EXPR,
10531 : : gfc_array_index_type, size, tmp);
10532 : : }
10533 : 165 : gfc_conv_descriptor_offset_set (&fnblock, comp, offset);
10534 : 165 : if (c->ts.type == BT_CLASS)
10535 : : {
10536 : 0 : tmp = gfc_get_vptr_from_expr (comp);
10537 : 0 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
10538 : 0 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
10539 : 0 : tmp = gfc_vptr_size_get (tmp);
10540 : : }
10541 : : else
10542 : 165 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (ctype));
10543 : 165 : tmp = fold_convert (gfc_array_index_type, tmp);
10544 : 165 : size = fold_build2_loc (input_location, MULT_EXPR,
10545 : : gfc_array_index_type, size, tmp);
10546 : 165 : size = gfc_evaluate_now (size, &fnblock);
10547 : 165 : tmp = gfc_call_malloc (&fnblock, NULL, size);
10548 : 165 : gfc_conv_descriptor_data_set (&fnblock, comp, tmp);
10549 : 165 : tmp = gfc_conv_descriptor_dtype (comp);
10550 : 165 : gfc_add_modify (&fnblock, tmp, gfc_get_dtype (ctype));
10551 : :
10552 : 165 : if (c->initializer && c->initializer->rank)
10553 : : {
10554 : 48 : gfc_init_se (&tse, NULL);
10555 : 48 : e = gfc_copy_expr (c->initializer);
10556 : 48 : gfc_insert_parameter_exprs (e, pdt_param_list);
10557 : 48 : gfc_conv_expr_descriptor (&tse, e);
10558 : 48 : gfc_add_block_to_block (&fnblock, &tse.pre);
10559 : 48 : gfc_free_expr (e);
10560 : 48 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
10561 : 48 : tmp = build_call_expr_loc (input_location, tmp, 3,
10562 : : gfc_conv_descriptor_data_get (comp),
10563 : : gfc_conv_descriptor_data_get (tse.expr),
10564 : : fold_convert (size_type_node, size));
10565 : 48 : gfc_add_expr_to_block (&fnblock, tmp);
10566 : 48 : gfc_add_block_to_block (&fnblock, &tse.post);
10567 : : }
10568 : : }
10569 : :
10570 : : /* Recurse in to PDT components. */
10571 : 227 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10572 : 74 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
10573 : 74 : && !(c->attr.pointer || c->attr.allocatable))
10574 : : {
10575 : 48 : bool is_deferred = false;
10576 : 48 : gfc_actual_arglist *tail = c->param_list;
10577 : :
10578 : 126 : for (; tail; tail = tail->next)
10579 : 78 : if (!tail->expr)
10580 : 24 : is_deferred = true;
10581 : :
10582 : 48 : tail = is_deferred ? pdt_param_list : c->param_list;
10583 : 48 : tmp = gfc_allocate_pdt_comp (c->ts.u.derived, comp,
10584 : 48 : c->as ? c->as->rank : 0,
10585 : : tail);
10586 : 48 : gfc_add_expr_to_block (&fnblock, tmp);
10587 : : }
10588 : :
10589 : : break;
10590 : :
10591 : 979 : case DEALLOCATE_PDT_COMP:
10592 : : /* Deallocate array or parameterized string length components
10593 : : of parameterized derived types. */
10594 : 979 : if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
10595 : 797 : && !c->attr.pdt_string
10596 : 731 : && !((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10597 : 91 : && (c->ts.u.derived && c->ts.u.derived->attr.pdt_type)))
10598 : 640 : continue;
10599 : :
10600 : 339 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10601 : : decl, cdecl, NULL_TREE);
10602 : 339 : if (c->ts.type == BT_CLASS)
10603 : 0 : comp = gfc_class_data_get (comp);
10604 : :
10605 : : /* Recurse in to PDT components. */
10606 : 339 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10607 : 97 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
10608 : 97 : && (!c->attr.pointer && !c->attr.allocatable))
10609 : : {
10610 : 42 : tmp = gfc_deallocate_pdt_comp (c->ts.u.derived, comp,
10611 : 42 : c->as ? c->as->rank : 0);
10612 : 42 : gfc_add_expr_to_block (&fnblock, tmp);
10613 : : }
10614 : :
10615 : 339 : if (c->attr.pdt_array || c->attr.pdt_string)
10616 : : {
10617 : 248 : tmp = comp;
10618 : 248 : if (c->attr.pdt_array)
10619 : 182 : tmp = gfc_conv_descriptor_data_get (comp);
10620 : 248 : null_cond = fold_build2_loc (input_location, NE_EXPR,
10621 : : logical_type_node, tmp,
10622 : 248 : build_int_cst (TREE_TYPE (tmp), 0));
10623 : 248 : if (flag_openmp_allocators)
10624 : : {
10625 : 0 : tree cd, t;
10626 : 0 : if (c->attr.pdt_array)
10627 : 0 : cd = fold_build2_loc (input_location, EQ_EXPR,
10628 : : boolean_type_node,
10629 : : gfc_conv_descriptor_version (comp),
10630 : 0 : build_int_cst (integer_type_node, 1));
10631 : : else
10632 : 0 : cd = gfc_omp_call_is_alloc (tmp);
10633 : 0 : t = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
10634 : 0 : t = build_call_expr_loc (input_location, t, 1, tmp);
10635 : :
10636 : 0 : stmtblock_t tblock;
10637 : 0 : gfc_init_block (&tblock);
10638 : 0 : gfc_add_expr_to_block (&tblock, t);
10639 : 0 : if (c->attr.pdt_array)
10640 : 0 : gfc_add_modify (&tblock, gfc_conv_descriptor_version (comp),
10641 : : integer_zero_node);
10642 : 0 : tmp = build3_loc (input_location, COND_EXPR, void_type_node,
10643 : : cd, gfc_finish_block (&tblock),
10644 : : gfc_call_free (tmp));
10645 : : }
10646 : : else
10647 : 248 : tmp = gfc_call_free (tmp);
10648 : 248 : tmp = build3_v (COND_EXPR, null_cond, tmp,
10649 : : build_empty_stmt (input_location));
10650 : 248 : gfc_add_expr_to_block (&fnblock, tmp);
10651 : :
10652 : 248 : if (c->attr.pdt_array)
10653 : 182 : gfc_conv_descriptor_data_set (&fnblock, comp, null_pointer_node);
10654 : : else
10655 : : {
10656 : 66 : tmp = fold_convert (TREE_TYPE (comp), null_pointer_node);
10657 : 66 : gfc_add_modify (&fnblock, comp, tmp);
10658 : : }
10659 : : }
10660 : :
10661 : : break;
10662 : :
10663 : 264 : case CHECK_PDT_DUMMY:
10664 : :
10665 : 264 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10666 : : decl, cdecl, NULL_TREE);
10667 : 264 : if (c->ts.type == BT_CLASS)
10668 : 0 : comp = gfc_class_data_get (comp);
10669 : :
10670 : : /* Recurse in to PDT components. */
10671 : 264 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10672 : 0 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_type)
10673 : : {
10674 : 0 : tmp = gfc_check_pdt_dummy (c->ts.u.derived, comp,
10675 : 0 : c->as ? c->as->rank : 0,
10676 : : pdt_param_list);
10677 : 0 : gfc_add_expr_to_block (&fnblock, tmp);
10678 : : }
10679 : :
10680 : 264 : if (!c->attr.pdt_len)
10681 : 216 : continue;
10682 : : else
10683 : : {
10684 : 48 : gfc_se tse;
10685 : 48 : gfc_expr *c_expr = NULL;
10686 : 48 : gfc_actual_arglist *param = pdt_param_list;
10687 : :
10688 : 48 : gfc_init_se (&tse, NULL);
10689 : 186 : for (; param; param = param->next)
10690 : 90 : if (!strcmp (c->name, param->name)
10691 : 48 : && param->spec_type == SPEC_EXPLICIT)
10692 : 30 : c_expr = param->expr;
10693 : :
10694 : 48 : if (c_expr)
10695 : : {
10696 : 30 : tree error, cond, cname;
10697 : 30 : gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
10698 : 30 : cond = fold_build2_loc (input_location, NE_EXPR,
10699 : : logical_type_node,
10700 : : comp, tse.expr);
10701 : 30 : cname = gfc_build_cstring_const (c->name);
10702 : 30 : cname = gfc_build_addr_expr (pchar_type_node, cname);
10703 : 30 : error = gfc_trans_runtime_error (true, NULL,
10704 : : "The value of the PDT LEN "
10705 : : "parameter '%s' does not "
10706 : : "agree with that in the "
10707 : : "dummy declaration",
10708 : : cname);
10709 : 30 : tmp = fold_build3_loc (input_location, COND_EXPR,
10710 : : void_type_node, cond, error,
10711 : : build_empty_stmt (input_location));
10712 : 30 : gfc_add_expr_to_block (&fnblock, tmp);
10713 : : }
10714 : : }
10715 : 48 : break;
10716 : :
10717 : 0 : default:
10718 : 0 : gcc_unreachable ();
10719 : 3545 : break;
10720 : : }
10721 : : }
10722 : :
10723 : 12032 : return gfc_finish_block (&fnblock);
10724 : : }
10725 : :
10726 : : /* Recursively traverse an object of derived type, generating code to
10727 : : nullify allocatable components. */
10728 : :
10729 : : tree
10730 : 2079 : gfc_nullify_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
10731 : : int caf_mode)
10732 : : {
10733 : 2079 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10734 : : NULLIFY_ALLOC_COMP,
10735 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
10736 : 2079 : NULL);
10737 : : }
10738 : :
10739 : :
10740 : : /* Recursively traverse an object of derived type, generating code to
10741 : : deallocate allocatable components. */
10742 : :
10743 : : tree
10744 : 2241 : gfc_deallocate_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
10745 : : int caf_mode)
10746 : : {
10747 : 2241 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10748 : : DEALLOCATE_ALLOC_COMP,
10749 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
10750 : 2241 : NULL);
10751 : : }
10752 : :
10753 : : tree
10754 : 1 : gfc_bcast_alloc_comp (gfc_symbol *derived, gfc_expr *expr, int rank,
10755 : : tree image_index, tree stat, tree errmsg,
10756 : : tree errmsg_len)
10757 : : {
10758 : 1 : tree tmp, array;
10759 : 1 : gfc_se argse;
10760 : 1 : stmtblock_t block, post_block;
10761 : 1 : gfc_co_subroutines_args args;
10762 : :
10763 : 1 : args.image_index = image_index;
10764 : 1 : args.stat = stat;
10765 : 1 : args.errmsg = errmsg;
10766 : 1 : args.errmsg_len = errmsg_len;
10767 : :
10768 : 1 : if (rank == 0)
10769 : : {
10770 : 1 : gfc_start_block (&block);
10771 : 1 : gfc_init_block (&post_block);
10772 : 1 : gfc_init_se (&argse, NULL);
10773 : 1 : gfc_conv_expr (&argse, expr);
10774 : 1 : gfc_add_block_to_block (&block, &argse.pre);
10775 : 1 : gfc_add_block_to_block (&post_block, &argse.post);
10776 : 1 : array = argse.expr;
10777 : : }
10778 : : else
10779 : : {
10780 : 0 : gfc_init_se (&argse, NULL);
10781 : 0 : argse.want_pointer = 1;
10782 : 0 : gfc_conv_expr_descriptor (&argse, expr);
10783 : 0 : array = argse.expr;
10784 : : }
10785 : :
10786 : 1 : tmp = structure_alloc_comps (derived, array, NULL_TREE, rank,
10787 : : BCAST_ALLOC_COMP,
10788 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
10789 : : &args);
10790 : 1 : return tmp;
10791 : : }
10792 : :
10793 : : /* Recursively traverse an object of derived type, generating code to
10794 : : deallocate allocatable components. But do not deallocate coarrays.
10795 : : To be used for intrinsic assignment, which may not change the allocation
10796 : : status of coarrays. */
10797 : :
10798 : : tree
10799 : 1545 : gfc_deallocate_alloc_comp_no_caf (gfc_symbol * der_type, tree decl, int rank,
10800 : : bool no_finalization)
10801 : : {
10802 : 1545 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10803 : : DEALLOCATE_ALLOC_COMP, 0, NULL,
10804 : 1545 : no_finalization);
10805 : : }
10806 : :
10807 : :
10808 : : tree
10809 : 4 : gfc_reassign_alloc_comp_caf (gfc_symbol *der_type, tree decl, tree dest)
10810 : : {
10811 : 4 : return structure_alloc_comps (der_type, decl, dest, 0, REASSIGN_CAF_COMP,
10812 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
10813 : 4 : NULL);
10814 : : }
10815 : :
10816 : :
10817 : : /* Recursively traverse an object of derived type, generating code to
10818 : : copy it and its allocatable components. */
10819 : :
10820 : : tree
10821 : 3127 : gfc_copy_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank,
10822 : : int caf_mode)
10823 : : {
10824 : 3127 : return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
10825 : 3127 : caf_mode, NULL);
10826 : : }
10827 : :
10828 : :
10829 : : /* Recursively traverse an object of derived type, generating code to
10830 : : copy it and its allocatable components, while suppressing any
10831 : : finalization that might occur. This is used in the finalization of
10832 : : function results. */
10833 : :
10834 : : tree
10835 : 37 : gfc_copy_alloc_comp_no_fini (gfc_symbol * der_type, tree decl, tree dest,
10836 : : int rank, int caf_mode)
10837 : : {
10838 : 37 : return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
10839 : 37 : caf_mode, NULL, true);
10840 : : }
10841 : :
10842 : :
10843 : : /* Recursively traverse an object of derived type, generating code to
10844 : : copy only its allocatable components. */
10845 : :
10846 : : tree
10847 : 0 : gfc_copy_only_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
10848 : : {
10849 : 0 : return structure_alloc_comps (der_type, decl, dest, rank,
10850 : 0 : COPY_ONLY_ALLOC_COMP, 0, NULL);
10851 : : }
10852 : :
10853 : :
10854 : : /* Recursively traverse an object of parameterized derived type, generating
10855 : : code to allocate parameterized components. */
10856 : :
10857 : : tree
10858 : 280 : gfc_allocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank,
10859 : : gfc_actual_arglist *param_list)
10860 : : {
10861 : 280 : tree res;
10862 : 280 : gfc_actual_arglist *old_param_list = pdt_param_list;
10863 : 280 : pdt_param_list = param_list;
10864 : 280 : res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10865 : : ALLOCATE_PDT_COMP, 0, NULL);
10866 : 280 : pdt_param_list = old_param_list;
10867 : 280 : return res;
10868 : : }
10869 : :
10870 : : /* Recursively traverse an object of parameterized derived type, generating
10871 : : code to deallocate parameterized components. */
10872 : :
10873 : : tree
10874 : 267 : gfc_deallocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank)
10875 : : {
10876 : 267 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10877 : 267 : DEALLOCATE_PDT_COMP, 0, NULL);
10878 : : }
10879 : :
10880 : :
10881 : : /* Recursively traverse a dummy of parameterized derived type to check the
10882 : : values of LEN parameters. */
10883 : :
10884 : : tree
10885 : 48 : gfc_check_pdt_dummy (gfc_symbol * der_type, tree decl, int rank,
10886 : : gfc_actual_arglist *param_list)
10887 : : {
10888 : 48 : tree res;
10889 : 48 : gfc_actual_arglist *old_param_list = pdt_param_list;
10890 : 48 : pdt_param_list = param_list;
10891 : 48 : res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10892 : : CHECK_PDT_DUMMY, 0, NULL);
10893 : 48 : pdt_param_list = old_param_list;
10894 : 48 : return res;
10895 : : }
10896 : :
10897 : :
10898 : : /* Returns the value of LBOUND for an expression. This could be broken out
10899 : : from gfc_conv_intrinsic_bound but this seemed to be simpler. This is
10900 : : called by gfc_alloc_allocatable_for_assignment. */
10901 : : static tree
10902 : 604 : get_std_lbound (gfc_expr *expr, tree desc, int dim, bool assumed_size)
10903 : : {
10904 : 604 : tree lbound;
10905 : 604 : tree ubound;
10906 : 604 : tree stride;
10907 : 604 : tree cond, cond1, cond3, cond4;
10908 : 604 : tree tmp;
10909 : 604 : gfc_ref *ref;
10910 : :
10911 : 604 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
10912 : : {
10913 : 283 : tmp = gfc_rank_cst[dim];
10914 : 283 : lbound = gfc_conv_descriptor_lbound_get (desc, tmp);
10915 : 283 : ubound = gfc_conv_descriptor_ubound_get (desc, tmp);
10916 : 283 : stride = gfc_conv_descriptor_stride_get (desc, tmp);
10917 : 283 : cond1 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10918 : : ubound, lbound);
10919 : 283 : cond3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10920 : : stride, gfc_index_zero_node);
10921 : 283 : cond3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10922 : : logical_type_node, cond3, cond1);
10923 : 283 : cond4 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
10924 : : stride, gfc_index_zero_node);
10925 : 283 : if (assumed_size)
10926 : 0 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10927 : : tmp, build_int_cst (gfc_array_index_type,
10928 : 0 : expr->rank - 1));
10929 : : else
10930 : 283 : cond = logical_false_node;
10931 : :
10932 : 283 : cond1 = fold_build2_loc (input_location, TRUTH_OR_EXPR,
10933 : : logical_type_node, cond3, cond4);
10934 : 283 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
10935 : : logical_type_node, cond, cond1);
10936 : :
10937 : 283 : return fold_build3_loc (input_location, COND_EXPR,
10938 : : gfc_array_index_type, cond,
10939 : 283 : lbound, gfc_index_one_node);
10940 : : }
10941 : :
10942 : 321 : if (expr->expr_type == EXPR_FUNCTION)
10943 : : {
10944 : : /* A conversion function, so use the argument. */
10945 : 7 : gcc_assert (expr->value.function.isym
10946 : : && expr->value.function.isym->conversion);
10947 : 7 : expr = expr->value.function.actual->expr;
10948 : : }
10949 : :
10950 : 321 : if (expr->expr_type == EXPR_VARIABLE)
10951 : : {
10952 : 321 : tmp = TREE_TYPE (expr->symtree->n.sym->backend_decl);
10953 : 672 : for (ref = expr->ref; ref; ref = ref->next)
10954 : : {
10955 : 351 : if (ref->type == REF_COMPONENT
10956 : 29 : && ref->u.c.component->as
10957 : 28 : && ref->next
10958 : 28 : && ref->next->u.ar.type == AR_FULL)
10959 : 28 : tmp = TREE_TYPE (ref->u.c.component->backend_decl);
10960 : : }
10961 : 321 : return GFC_TYPE_ARRAY_LBOUND(tmp, dim);
10962 : : }
10963 : :
10964 : 0 : return gfc_index_one_node;
10965 : : }
10966 : :
10967 : :
10968 : : /* Returns true if an expression represents an lhs that can be reallocated
10969 : : on assignment. */
10970 : :
10971 : : bool
10972 : 412543 : gfc_is_reallocatable_lhs (gfc_expr *expr)
10973 : : {
10974 : 412543 : gfc_ref * ref;
10975 : 412543 : gfc_symbol *sym;
10976 : :
10977 : 412543 : if (!expr->ref)
10978 : : return false;
10979 : :
10980 : 174044 : sym = expr->symtree->n.sym;
10981 : :
10982 : 174044 : if (sym->attr.associate_var && !expr->ref)
10983 : : return false;
10984 : :
10985 : : /* An allocatable class variable with no reference. */
10986 : 174044 : if (sym->ts.type == BT_CLASS
10987 : 6780 : && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
10988 : 6618 : && CLASS_DATA (sym)->attr.allocatable
10989 : : && expr->ref
10990 : 4142 : && ((expr->ref->type == REF_ARRAY && expr->ref->u.ar.type == AR_FULL
10991 : 0 : && expr->ref->next == NULL)
10992 : 4142 : || (expr->ref->type == REF_COMPONENT
10993 : 4142 : && strcmp (expr->ref->u.c.component->name, "_data") == 0
10994 : 3424 : && (expr->ref->next == NULL
10995 : 2663 : || (expr->ref->next->type == REF_ARRAY
10996 : 2663 : && expr->ref->next->u.ar.type == AR_FULL
10997 : 2095 : && expr->ref->next->next == NULL)))))
10998 : : return true;
10999 : :
11000 : : /* An allocatable variable. */
11001 : 171365 : if (sym->attr.allocatable
11002 : 29059 : && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
11003 : : && expr->ref
11004 : 29059 : && expr->ref->type == REF_ARRAY
11005 : 27882 : && expr->ref->u.ar.type == AR_FULL)
11006 : : return true;
11007 : :
11008 : : /* All that can be left are allocatable components. */
11009 : 156420 : if ((sym->ts.type != BT_DERIVED
11010 : 127394 : && sym->ts.type != BT_CLASS)
11011 : 33127 : || !sym->ts.u.derived->attr.alloc_comp)
11012 : : return false;
11013 : :
11014 : : /* Find a component ref followed by an array reference. */
11015 : 18821 : for (ref = expr->ref; ref; ref = ref->next)
11016 : 14655 : if (ref->next
11017 : 10489 : && ref->type == REF_COMPONENT
11018 : 7624 : && ref->next->type == REF_ARRAY
11019 : 5790 : && !ref->next->next)
11020 : : break;
11021 : :
11022 : 8786 : if (!ref)
11023 : : return false;
11024 : :
11025 : : /* Return true if valid reallocatable lhs. */
11026 : 4620 : if (ref->u.c.component->attr.allocatable
11027 : 4507 : && ref->next->u.ar.type == AR_FULL)
11028 : 3199 : return true;
11029 : :
11030 : : return false;
11031 : : }
11032 : :
11033 : :
11034 : : static tree
11035 : 56 : concat_str_length (gfc_expr* expr)
11036 : : {
11037 : 56 : tree type;
11038 : 56 : tree len1;
11039 : 56 : tree len2;
11040 : 56 : gfc_se se;
11041 : :
11042 : 56 : type = gfc_typenode_for_spec (&expr->value.op.op1->ts);
11043 : 56 : len1 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
11044 : 56 : if (len1 == NULL_TREE)
11045 : : {
11046 : 56 : if (expr->value.op.op1->expr_type == EXPR_OP)
11047 : 31 : len1 = concat_str_length (expr->value.op.op1);
11048 : 25 : else if (expr->value.op.op1->expr_type == EXPR_CONSTANT)
11049 : 25 : len1 = build_int_cst (gfc_charlen_type_node,
11050 : 25 : expr->value.op.op1->value.character.length);
11051 : 0 : else if (expr->value.op.op1->ts.u.cl->length)
11052 : : {
11053 : 0 : gfc_init_se (&se, NULL);
11054 : 0 : gfc_conv_expr (&se, expr->value.op.op1->ts.u.cl->length);
11055 : 0 : len1 = se.expr;
11056 : : }
11057 : : else
11058 : : {
11059 : : /* Last resort! */
11060 : 0 : gfc_init_se (&se, NULL);
11061 : 0 : se.want_pointer = 1;
11062 : 0 : se.descriptor_only = 1;
11063 : 0 : gfc_conv_expr (&se, expr->value.op.op1);
11064 : 0 : len1 = se.string_length;
11065 : : }
11066 : : }
11067 : :
11068 : 56 : type = gfc_typenode_for_spec (&expr->value.op.op2->ts);
11069 : 56 : len2 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
11070 : 56 : if (len2 == NULL_TREE)
11071 : : {
11072 : 31 : if (expr->value.op.op2->expr_type == EXPR_OP)
11073 : 0 : len2 = concat_str_length (expr->value.op.op2);
11074 : 31 : else if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
11075 : 25 : len2 = build_int_cst (gfc_charlen_type_node,
11076 : 25 : expr->value.op.op2->value.character.length);
11077 : 6 : else if (expr->value.op.op2->ts.u.cl->length)
11078 : : {
11079 : 6 : gfc_init_se (&se, NULL);
11080 : 6 : gfc_conv_expr (&se, expr->value.op.op2->ts.u.cl->length);
11081 : 6 : len2 = se.expr;
11082 : : }
11083 : : else
11084 : : {
11085 : : /* Last resort! */
11086 : 0 : gfc_init_se (&se, NULL);
11087 : 0 : se.want_pointer = 1;
11088 : 0 : se.descriptor_only = 1;
11089 : 0 : gfc_conv_expr (&se, expr->value.op.op2);
11090 : 0 : len2 = se.string_length;
11091 : : }
11092 : : }
11093 : :
11094 : 56 : gcc_assert(len1 && len2);
11095 : 56 : len1 = fold_convert (gfc_charlen_type_node, len1);
11096 : 56 : len2 = fold_convert (gfc_charlen_type_node, len2);
11097 : :
11098 : 56 : return fold_build2_loc (input_location, PLUS_EXPR,
11099 : 56 : gfc_charlen_type_node, len1, len2);
11100 : : }
11101 : :
11102 : :
11103 : : /* Allocate the lhs of an assignment to an allocatable array, otherwise
11104 : : reallocate it. */
11105 : :
11106 : : tree
11107 : 3216 : gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop,
11108 : : gfc_expr *expr1,
11109 : : gfc_expr *expr2)
11110 : : {
11111 : 3216 : stmtblock_t realloc_block;
11112 : 3216 : stmtblock_t alloc_block;
11113 : 3216 : stmtblock_t fblock;
11114 : 3216 : stmtblock_t loop_pre_block;
11115 : 3216 : gfc_ref *ref;
11116 : 3216 : gfc_ss *rss;
11117 : 3216 : gfc_ss *lss;
11118 : 3216 : gfc_array_info *linfo;
11119 : 3216 : tree realloc_expr;
11120 : 3216 : tree alloc_expr;
11121 : 3216 : tree size1;
11122 : 3216 : tree size2;
11123 : 3216 : tree elemsize1;
11124 : 3216 : tree elemsize2;
11125 : 3216 : tree array1;
11126 : 3216 : tree cond_null;
11127 : 3216 : tree cond;
11128 : 3216 : tree tmp;
11129 : 3216 : tree tmp2;
11130 : 3216 : tree lbound;
11131 : 3216 : tree ubound;
11132 : 3216 : tree desc;
11133 : 3216 : tree old_desc;
11134 : 3216 : tree desc2;
11135 : 3216 : tree offset;
11136 : 3216 : tree jump_label1;
11137 : 3216 : tree jump_label2;
11138 : 3216 : tree lbd;
11139 : 3216 : tree class_expr2 = NULL_TREE;
11140 : 3216 : int n;
11141 : 3216 : int dim;
11142 : 3216 : gfc_array_spec * as;
11143 : 3216 : bool coarray = (flag_coarray == GFC_FCOARRAY_LIB
11144 : 3216 : && gfc_caf_attr (expr1, true).codimension);
11145 : 3216 : tree token;
11146 : 3216 : gfc_se caf_se;
11147 : :
11148 : : /* x = f(...) with x allocatable. In this case, expr1 is the rhs.
11149 : : Find the lhs expression in the loop chain and set expr1 and
11150 : : expr2 accordingly. */
11151 : 3216 : if (expr1->expr_type == EXPR_FUNCTION && expr2 == NULL)
11152 : : {
11153 : 152 : expr2 = expr1;
11154 : : /* Find the ss for the lhs. */
11155 : 152 : lss = loop->ss;
11156 : 304 : for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
11157 : 304 : if (lss->info->expr && lss->info->expr->expr_type == EXPR_VARIABLE)
11158 : : break;
11159 : 152 : if (lss == gfc_ss_terminator)
11160 : : return NULL_TREE;
11161 : 152 : expr1 = lss->info->expr;
11162 : : }
11163 : :
11164 : : /* Bail out if this is not a valid allocate on assignment. */
11165 : 3216 : if (!gfc_is_reallocatable_lhs (expr1)
11166 : 3216 : || (expr2 && !expr2->rank))
11167 : : return NULL_TREE;
11168 : :
11169 : : /* Find the ss for the lhs. */
11170 : 3216 : lss = loop->ss;
11171 : 7290 : for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
11172 : 7290 : if (lss->info->expr == expr1)
11173 : : break;
11174 : :
11175 : 3216 : if (lss == gfc_ss_terminator)
11176 : : return NULL_TREE;
11177 : :
11178 : 3216 : linfo = &lss->info->data.array;
11179 : :
11180 : : /* Find an ss for the rhs. For operator expressions, we see the
11181 : : ss's for the operands. Any one of these will do. */
11182 : 3216 : rss = loop->ss;
11183 : 3619 : for (; rss && rss != gfc_ss_terminator; rss = rss->loop_chain)
11184 : 3619 : if (rss->info->expr != expr1 && rss != loop->temp_ss)
11185 : : break;
11186 : :
11187 : 3216 : if (expr2 && rss == gfc_ss_terminator)
11188 : : return NULL_TREE;
11189 : :
11190 : : /* Ensure that the string length from the current scope is used. */
11191 : 3216 : if (expr2->ts.type == BT_CHARACTER
11192 : 813 : && expr2->expr_type == EXPR_FUNCTION
11193 : 112 : && !expr2->value.function.isym)
11194 : 21 : expr2->ts.u.cl->backend_decl = rss->info->string_length;
11195 : :
11196 : 3216 : gfc_start_block (&fblock);
11197 : :
11198 : : /* Since the lhs is allocatable, this must be a descriptor type.
11199 : : Get the data and array size. */
11200 : 3216 : desc = linfo->descriptor;
11201 : 3216 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
11202 : 3216 : array1 = gfc_conv_descriptor_data_get (desc);
11203 : :
11204 : 3216 : if (expr2)
11205 : 3216 : desc2 = rss->info->data.array.descriptor;
11206 : : else
11207 : : desc2 = NULL_TREE;
11208 : :
11209 : : /* Get the old lhs element size for deferred character and class expr1. */
11210 : 3216 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11211 : : {
11212 : 571 : if (expr1->ts.u.cl->backend_decl
11213 : 571 : && VAR_P (expr1->ts.u.cl->backend_decl))
11214 : : elemsize1 = expr1->ts.u.cl->backend_decl;
11215 : : else
11216 : 63 : elemsize1 = lss->info->string_length;
11217 : 571 : tree unit_size = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
11218 : 1142 : elemsize1 = fold_build2_loc (input_location, MULT_EXPR,
11219 : 571 : TREE_TYPE (elemsize1), elemsize1,
11220 : 571 : fold_convert (TREE_TYPE (elemsize1), unit_size));
11221 : :
11222 : 571 : }
11223 : 2645 : else if (expr1->ts.type == BT_CLASS)
11224 : : {
11225 : : /* Unfortunately, the lhs vptr is set too early in many cases.
11226 : : Play it safe by using the descriptor element length. */
11227 : 386 : tmp = gfc_conv_descriptor_elem_len (desc);
11228 : 386 : elemsize1 = fold_convert (gfc_array_index_type, tmp);
11229 : : }
11230 : : else
11231 : : elemsize1 = NULL_TREE;
11232 : 957 : if (elemsize1 != NULL_TREE)
11233 : 957 : elemsize1 = gfc_evaluate_now (elemsize1, &fblock);
11234 : :
11235 : : /* Get the new lhs size in bytes. */
11236 : 3216 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11237 : : {
11238 : 571 : if (expr2->ts.deferred)
11239 : : {
11240 : 183 : if (expr2->ts.u.cl->backend_decl
11241 : 183 : && VAR_P (expr2->ts.u.cl->backend_decl))
11242 : : tmp = expr2->ts.u.cl->backend_decl;
11243 : : else
11244 : 0 : tmp = rss->info->string_length;
11245 : : }
11246 : : else
11247 : : {
11248 : 388 : tmp = expr2->ts.u.cl->backend_decl;
11249 : 388 : if (!tmp && expr2->expr_type == EXPR_OP
11250 : 25 : && expr2->value.op.op == INTRINSIC_CONCAT)
11251 : : {
11252 : 25 : tmp = concat_str_length (expr2);
11253 : 25 : expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
11254 : : }
11255 : 12 : else if (!tmp && expr2->ts.u.cl->length)
11256 : : {
11257 : 12 : gfc_se tmpse;
11258 : 12 : gfc_init_se (&tmpse, NULL);
11259 : 12 : gfc_conv_expr_type (&tmpse, expr2->ts.u.cl->length,
11260 : : gfc_charlen_type_node);
11261 : 12 : tmp = tmpse.expr;
11262 : 12 : expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
11263 : : }
11264 : 388 : tmp = fold_convert (TREE_TYPE (expr1->ts.u.cl->backend_decl), tmp);
11265 : : }
11266 : :
11267 : 571 : if (expr1->ts.u.cl->backend_decl
11268 : 571 : && VAR_P (expr1->ts.u.cl->backend_decl))
11269 : 508 : gfc_add_modify (&fblock, expr1->ts.u.cl->backend_decl, tmp);
11270 : : else
11271 : 63 : gfc_add_modify (&fblock, lss->info->string_length, tmp);
11272 : :
11273 : 571 : if (expr1->ts.kind > 1)
11274 : 12 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11275 : 6 : TREE_TYPE (tmp),
11276 : 6 : tmp, build_int_cst (TREE_TYPE (tmp),
11277 : 6 : expr1->ts.kind));
11278 : : }
11279 : 2645 : else if (expr1->ts.type == BT_CHARACTER && expr1->ts.u.cl->backend_decl)
11280 : : {
11281 : 211 : tmp = TYPE_SIZE_UNIT (TREE_TYPE (gfc_typenode_for_spec (&expr1->ts)));
11282 : 211 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11283 : : gfc_array_index_type, tmp,
11284 : 211 : expr1->ts.u.cl->backend_decl);
11285 : : }
11286 : 2434 : else if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
11287 : 68 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
11288 : 2366 : else if (expr1->ts.type == BT_CLASS && expr2->ts.type == BT_CLASS)
11289 : : {
11290 : 178 : tmp = expr2->rank ? gfc_get_class_from_expr (desc2) : NULL_TREE;
11291 : 178 : if (tmp == NULL_TREE && expr2->expr_type == EXPR_VARIABLE)
11292 : 18 : tmp = class_expr2 = gfc_get_class_from_gfc_expr (expr2);
11293 : :
11294 : 25 : if (tmp != NULL_TREE)
11295 : 171 : tmp = gfc_class_vtab_size_get (tmp);
11296 : : else
11297 : 7 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
11298 : : }
11299 : : else
11300 : 2188 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
11301 : 3216 : elemsize2 = fold_convert (gfc_array_index_type, tmp);
11302 : 3216 : elemsize2 = gfc_evaluate_now (elemsize2, &fblock);
11303 : :
11304 : : /* 7.4.1.3 "If variable is an allocated allocatable variable, it is
11305 : : deallocated if expr is an array of different shape or any of the
11306 : : corresponding length type parameter values of variable and expr
11307 : : differ." This assures F95 compatibility. */
11308 : 3216 : jump_label1 = gfc_build_label_decl (NULL_TREE);
11309 : 3216 : jump_label2 = gfc_build_label_decl (NULL_TREE);
11310 : :
11311 : : /* Allocate if data is NULL. */
11312 : 3216 : cond_null = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11313 : 3216 : array1, build_int_cst (TREE_TYPE (array1), 0));
11314 : 3216 : cond_null= gfc_evaluate_now (cond_null, &fblock);
11315 : :
11316 : : /* If the data is null, set the descriptor bounds and offset. This suppresses
11317 : : the maybe used uninitialized warning and forces the use of malloc because
11318 : : the size is zero in all dimensions. Note that this block is only executed
11319 : : if the lhs is unallocated and is only applied once in any namespace.
11320 : : Component references are not subject to the warnings. */
11321 : 5661 : for (ref = expr1->ref; ref; ref = ref->next)
11322 : 3411 : if (ref->type == REF_COMPONENT)
11323 : : break;
11324 : :
11325 : 3216 : if (!expr1->symtree->n.sym->allocated_in_scope && !ref)
11326 : : {
11327 : 1059 : gfc_start_block (&loop_pre_block);
11328 : 3445 : for (n = 0; n < expr1->rank; n++)
11329 : : {
11330 : 1327 : gfc_conv_descriptor_lbound_set (&loop_pre_block, desc,
11331 : : gfc_rank_cst[n],
11332 : : gfc_index_one_node);
11333 : 1327 : gfc_conv_descriptor_ubound_set (&loop_pre_block, desc,
11334 : : gfc_rank_cst[n],
11335 : : gfc_index_zero_node);
11336 : 1327 : gfc_conv_descriptor_stride_set (&loop_pre_block, desc,
11337 : : gfc_rank_cst[n],
11338 : : gfc_index_zero_node);
11339 : : }
11340 : :
11341 : 1059 : tmp = gfc_conv_descriptor_offset (desc);
11342 : 1059 : gfc_add_modify (&loop_pre_block, tmp, gfc_index_zero_node);
11343 : :
11344 : 1059 : tmp = fold_build2_loc (input_location, EQ_EXPR,
11345 : : logical_type_node, array1,
11346 : 1059 : build_int_cst (TREE_TYPE (array1), 0));
11347 : 1059 : tmp = build3_v (COND_EXPR, tmp,
11348 : : gfc_finish_block (&loop_pre_block),
11349 : : build_empty_stmt (input_location));
11350 : 1059 : gfc_prepend_expr_to_block (&loop->pre, tmp);
11351 : :
11352 : 1059 : expr1->symtree->n.sym->allocated_in_scope = 1;
11353 : : }
11354 : :
11355 : 3216 : tmp = build3_v (COND_EXPR, cond_null,
11356 : : build1_v (GOTO_EXPR, jump_label1),
11357 : : build_empty_stmt (input_location));
11358 : 3216 : gfc_add_expr_to_block (&fblock, tmp);
11359 : :
11360 : : /* Get arrayspec if expr is a full array. */
11361 : 3216 : if (expr2 && expr2->expr_type == EXPR_FUNCTION
11362 : 822 : && expr2->value.function.isym
11363 : 403 : && expr2->value.function.isym->conversion)
11364 : : {
11365 : : /* For conversion functions, take the arg. */
11366 : 243 : gfc_expr *arg = expr2->value.function.actual->expr;
11367 : 243 : as = gfc_get_full_arrayspec_from_expr (arg);
11368 : 243 : }
11369 : : else if (expr2)
11370 : 2973 : as = gfc_get_full_arrayspec_from_expr (expr2);
11371 : : else
11372 : : as = NULL;
11373 : :
11374 : : /* If the lhs shape is not the same as the rhs jump to setting the
11375 : : bounds and doing the reallocation....... */
11376 : 7335 : for (n = 0; n < expr1->rank; n++)
11377 : : {
11378 : : /* Check the shape. */
11379 : 4119 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11380 : 4119 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
11381 : 4119 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11382 : : gfc_array_index_type,
11383 : : loop->to[n], loop->from[n]);
11384 : 4119 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11385 : : gfc_array_index_type,
11386 : : tmp, lbound);
11387 : 4119 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11388 : : gfc_array_index_type,
11389 : : tmp, ubound);
11390 : 4119 : cond = fold_build2_loc (input_location, NE_EXPR,
11391 : : logical_type_node,
11392 : : tmp, gfc_index_zero_node);
11393 : 4119 : tmp = build3_v (COND_EXPR, cond,
11394 : : build1_v (GOTO_EXPR, jump_label1),
11395 : : build_empty_stmt (input_location));
11396 : 4119 : gfc_add_expr_to_block (&fblock, tmp);
11397 : : }
11398 : :
11399 : : /* ...else if the element lengths are not the same also go to
11400 : : setting the bounds and doing the reallocation.... */
11401 : 3216 : if (elemsize1 != NULL_TREE)
11402 : : {
11403 : 957 : cond = fold_build2_loc (input_location, NE_EXPR,
11404 : : logical_type_node,
11405 : : elemsize1, elemsize2);
11406 : 957 : tmp = build3_v (COND_EXPR, cond,
11407 : : build1_v (GOTO_EXPR, jump_label1),
11408 : : build_empty_stmt (input_location));
11409 : 957 : gfc_add_expr_to_block (&fblock, tmp);
11410 : : }
11411 : :
11412 : : /* ....else jump past the (re)alloc code. */
11413 : 3216 : tmp = build1_v (GOTO_EXPR, jump_label2);
11414 : 3216 : gfc_add_expr_to_block (&fblock, tmp);
11415 : :
11416 : : /* Add the label to start automatic (re)allocation. */
11417 : 3216 : tmp = build1_v (LABEL_EXPR, jump_label1);
11418 : 3216 : gfc_add_expr_to_block (&fblock, tmp);
11419 : :
11420 : : /* Get the rhs size and fix it. */
11421 : 3216 : size2 = gfc_index_one_node;
11422 : 7335 : for (n = 0; n < expr2->rank; n++)
11423 : : {
11424 : 4119 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11425 : : gfc_array_index_type,
11426 : : loop->to[n], loop->from[n]);
11427 : 4119 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11428 : : gfc_array_index_type,
11429 : : tmp, gfc_index_one_node);
11430 : 4119 : size2 = fold_build2_loc (input_location, MULT_EXPR,
11431 : : gfc_array_index_type,
11432 : : tmp, size2);
11433 : : }
11434 : 3216 : size2 = gfc_evaluate_now (size2, &fblock);
11435 : :
11436 : : /* Deallocation of allocatable components will have to occur on
11437 : : reallocation. Fix the old descriptor now. */
11438 : 3216 : if ((expr1->ts.type == BT_DERIVED)
11439 : 222 : && expr1->ts.u.derived->attr.alloc_comp)
11440 : 85 : old_desc = gfc_evaluate_now (desc, &fblock);
11441 : : else
11442 : : old_desc = NULL_TREE;
11443 : :
11444 : : /* Now modify the lhs descriptor and the associated scalarizer
11445 : : variables. F2003 7.4.1.3: "If variable is or becomes an
11446 : : unallocated allocatable variable, then it is allocated with each
11447 : : deferred type parameter equal to the corresponding type parameters
11448 : : of expr , with the shape of expr , and with each lower bound equal
11449 : : to the corresponding element of LBOUND(expr)."
11450 : : Reuse size1 to keep a dimension-by-dimension track of the
11451 : : stride of the new array. */
11452 : 3216 : size1 = gfc_index_one_node;
11453 : 3216 : offset = gfc_index_zero_node;
11454 : :
11455 : 7335 : for (n = 0; n < expr2->rank; n++)
11456 : : {
11457 : 4119 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11458 : : gfc_array_index_type,
11459 : : loop->to[n], loop->from[n]);
11460 : 4119 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11461 : : gfc_array_index_type,
11462 : : tmp, gfc_index_one_node);
11463 : :
11464 : 4119 : lbound = gfc_index_one_node;
11465 : 4119 : ubound = tmp;
11466 : :
11467 : 4119 : if (as)
11468 : : {
11469 : 1208 : lbd = get_std_lbound (expr2, desc2, n,
11470 : 604 : as->type == AS_ASSUMED_SIZE);
11471 : 604 : ubound = fold_build2_loc (input_location,
11472 : : MINUS_EXPR,
11473 : : gfc_array_index_type,
11474 : : ubound, lbound);
11475 : 604 : ubound = fold_build2_loc (input_location,
11476 : : PLUS_EXPR,
11477 : : gfc_array_index_type,
11478 : : ubound, lbd);
11479 : 604 : lbound = lbd;
11480 : : }
11481 : :
11482 : 4119 : gfc_conv_descriptor_lbound_set (&fblock, desc,
11483 : : gfc_rank_cst[n],
11484 : : lbound);
11485 : 4119 : gfc_conv_descriptor_ubound_set (&fblock, desc,
11486 : : gfc_rank_cst[n],
11487 : : ubound);
11488 : 4119 : gfc_conv_descriptor_stride_set (&fblock, desc,
11489 : : gfc_rank_cst[n],
11490 : : size1);
11491 : 4119 : lbound = gfc_conv_descriptor_lbound_get (desc,
11492 : : gfc_rank_cst[n]);
11493 : 4119 : tmp2 = fold_build2_loc (input_location, MULT_EXPR,
11494 : : gfc_array_index_type,
11495 : : lbound, size1);
11496 : 4119 : offset = fold_build2_loc (input_location, MINUS_EXPR,
11497 : : gfc_array_index_type,
11498 : : offset, tmp2);
11499 : 4119 : size1 = fold_build2_loc (input_location, MULT_EXPR,
11500 : : gfc_array_index_type,
11501 : : tmp, size1);
11502 : : }
11503 : :
11504 : : /* Set the lhs descriptor and scalarizer offsets. For rank > 1,
11505 : : the array offset is saved and the info.offset is used for a
11506 : : running offset. Use the saved_offset instead. */
11507 : 3216 : tmp = gfc_conv_descriptor_offset (desc);
11508 : 3216 : gfc_add_modify (&fblock, tmp, offset);
11509 : 3216 : if (linfo->saved_offset
11510 : 3216 : && VAR_P (linfo->saved_offset))
11511 : 3216 : gfc_add_modify (&fblock, linfo->saved_offset, tmp);
11512 : :
11513 : : /* Now set the deltas for the lhs. */
11514 : 7335 : for (n = 0; n < expr1->rank; n++)
11515 : : {
11516 : 4119 : tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11517 : 4119 : dim = lss->dim[n];
11518 : 4119 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11519 : : gfc_array_index_type, tmp,
11520 : : loop->from[dim]);
11521 : 4119 : if (linfo->delta[dim] && VAR_P (linfo->delta[dim]))
11522 : 4119 : gfc_add_modify (&fblock, linfo->delta[dim], tmp);
11523 : : }
11524 : :
11525 : : /* Take into account _len of unlimited polymorphic entities, so that span
11526 : : for array descriptors and allocation sizes are computed correctly. */
11527 : 3216 : if (UNLIMITED_POLY (expr2))
11528 : : {
11529 : 68 : tree len = gfc_class_len_get (TREE_OPERAND (desc2, 0));
11530 : 68 : len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
11531 : : fold_convert (size_type_node, len),
11532 : : size_one_node);
11533 : 68 : elemsize2 = fold_build2_loc (input_location, MULT_EXPR,
11534 : : gfc_array_index_type, elemsize2,
11535 : : fold_convert (gfc_array_index_type, len));
11536 : : }
11537 : :
11538 : 3216 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
11539 : 3216 : gfc_conv_descriptor_span_set (&fblock, desc, elemsize2);
11540 : :
11541 : 3216 : size2 = fold_build2_loc (input_location, MULT_EXPR,
11542 : : gfc_array_index_type,
11543 : : elemsize2, size2);
11544 : 3216 : size2 = fold_convert (size_type_node, size2);
11545 : 3216 : size2 = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
11546 : : size2, size_one_node);
11547 : 3216 : size2 = gfc_evaluate_now (size2, &fblock);
11548 : :
11549 : : /* For deferred character length, the 'size' field of the dtype might
11550 : : have changed so set the dtype. */
11551 : 3216 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
11552 : 3216 : && expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11553 : : {
11554 : 571 : tree type;
11555 : 571 : tmp = gfc_conv_descriptor_dtype (desc);
11556 : 571 : if (expr2->ts.u.cl->backend_decl)
11557 : 571 : type = gfc_typenode_for_spec (&expr2->ts);
11558 : : else
11559 : 0 : type = gfc_typenode_for_spec (&expr1->ts);
11560 : :
11561 : 571 : gfc_add_modify (&fblock, tmp,
11562 : : gfc_get_dtype_rank_type (expr1->rank,type));
11563 : : }
11564 : 2645 : else if (expr1->ts.type == BT_CLASS)
11565 : : {
11566 : 386 : tree type;
11567 : 386 : tmp = gfc_conv_descriptor_dtype (desc);
11568 : :
11569 : 386 : if (expr2->ts.type != BT_CLASS)
11570 : 208 : type = gfc_typenode_for_spec (&expr2->ts);
11571 : : else
11572 : 178 : type = gfc_get_character_type_len (1, elemsize2);
11573 : :
11574 : 386 : gfc_add_modify (&fblock, tmp,
11575 : : gfc_get_dtype_rank_type (expr2->rank,type));
11576 : : /* Set the _len field as well... */
11577 : 386 : if (UNLIMITED_POLY (expr1))
11578 : : {
11579 : 136 : tmp = gfc_class_len_get (TREE_OPERAND (desc, 0));
11580 : 136 : if (expr2->ts.type == BT_CHARACTER)
11581 : 31 : gfc_add_modify (&fblock, tmp,
11582 : 31 : fold_convert (TREE_TYPE (tmp),
11583 : : TYPE_SIZE_UNIT (type)));
11584 : 105 : else if (UNLIMITED_POLY (expr2))
11585 : 68 : gfc_add_modify (&fblock, tmp,
11586 : 68 : gfc_class_len_get (TREE_OPERAND (desc2, 0)));
11587 : : else
11588 : 37 : gfc_add_modify (&fblock, tmp,
11589 : 37 : build_int_cst (TREE_TYPE (tmp), 0));
11590 : : }
11591 : : /* ...and the vptr. */
11592 : 386 : tmp = gfc_class_vptr_get (TREE_OPERAND (desc, 0));
11593 : 386 : if (expr2->ts.type == BT_CLASS && !VAR_P (desc2)
11594 : 171 : && TREE_CODE (desc2) == COMPONENT_REF)
11595 : : {
11596 : 153 : tmp2 = gfc_get_class_from_expr (desc2);
11597 : 153 : tmp2 = gfc_class_vptr_get (tmp2);
11598 : : }
11599 : 233 : else if (expr2->ts.type == BT_CLASS && class_expr2 != NULL_TREE)
11600 : 18 : tmp2 = gfc_class_vptr_get (class_expr2);
11601 : : else
11602 : : {
11603 : 215 : tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
11604 : 215 : tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
11605 : : }
11606 : :
11607 : 386 : gfc_add_modify (&fblock, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
11608 : : }
11609 : 2259 : else if (coarray && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
11610 : : {
11611 : 38 : gfc_add_modify (&fblock, gfc_conv_descriptor_dtype (desc),
11612 : 38 : gfc_get_dtype (TREE_TYPE (desc)));
11613 : : }
11614 : :
11615 : : /* Realloc expression. Note that the scalarizer uses desc.data
11616 : : in the array reference - (*desc.data)[<element>]. */
11617 : 3216 : gfc_init_block (&realloc_block);
11618 : 3216 : gfc_init_se (&caf_se, NULL);
11619 : :
11620 : 3216 : if (coarray)
11621 : : {
11622 : 38 : token = gfc_get_ultimate_alloc_ptr_comps_caf_token (&caf_se, expr1);
11623 : 38 : if (token == NULL_TREE)
11624 : : {
11625 : 8 : tmp = gfc_get_tree_for_caf_expr (expr1);
11626 : 8 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
11627 : 6 : tmp = build_fold_indirect_ref (tmp);
11628 : 8 : gfc_get_caf_token_offset (&caf_se, &token, NULL, tmp, NULL_TREE,
11629 : : expr1);
11630 : 8 : token = gfc_build_addr_expr (NULL_TREE, token);
11631 : : }
11632 : :
11633 : 38 : gfc_add_block_to_block (&realloc_block, &caf_se.pre);
11634 : : }
11635 : 3216 : if ((expr1->ts.type == BT_DERIVED)
11636 : 222 : && expr1->ts.u.derived->attr.alloc_comp)
11637 : : {
11638 : 85 : tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, old_desc,
11639 : : expr1->rank, true);
11640 : 85 : gfc_add_expr_to_block (&realloc_block, tmp);
11641 : : }
11642 : :
11643 : 3216 : if (!coarray)
11644 : : {
11645 : 3178 : tmp = build_call_expr_loc (input_location,
11646 : : builtin_decl_explicit (BUILT_IN_REALLOC), 2,
11647 : : fold_convert (pvoid_type_node, array1),
11648 : : size2);
11649 : 3178 : if (flag_openmp_allocators)
11650 : : {
11651 : 2 : tree cond, omp_tmp;
11652 : 2 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
11653 : : gfc_conv_descriptor_version (desc),
11654 : 2 : build_int_cst (integer_type_node, 1));
11655 : 2 : omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
11656 : 2 : omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
11657 : : fold_convert (pvoid_type_node, array1), size2,
11658 : : build_zero_cst (ptr_type_node),
11659 : : build_zero_cst (ptr_type_node));
11660 : 2 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
11661 : : omp_tmp, tmp);
11662 : : }
11663 : :
11664 : 3178 : gfc_conv_descriptor_data_set (&realloc_block, desc, tmp);
11665 : : }
11666 : : else
11667 : : {
11668 : 38 : tmp = build_call_expr_loc (input_location,
11669 : : gfor_fndecl_caf_deregister, 5, token,
11670 : : build_int_cst (integer_type_node,
11671 : 38 : GFC_CAF_COARRAY_DEALLOCATE_ONLY),
11672 : : null_pointer_node, null_pointer_node,
11673 : : integer_zero_node);
11674 : 38 : gfc_add_expr_to_block (&realloc_block, tmp);
11675 : 76 : tmp = build_call_expr_loc (input_location,
11676 : : gfor_fndecl_caf_register,
11677 : : 7, size2,
11678 : : build_int_cst (integer_type_node,
11679 : 38 : GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY),
11680 : : token, gfc_build_addr_expr (NULL_TREE, desc),
11681 : : null_pointer_node, null_pointer_node,
11682 : : integer_zero_node);
11683 : 38 : gfc_add_expr_to_block (&realloc_block, tmp);
11684 : : }
11685 : :
11686 : 3216 : if ((expr1->ts.type == BT_DERIVED)
11687 : 222 : && expr1->ts.u.derived->attr.alloc_comp)
11688 : : {
11689 : 85 : tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
11690 : : expr1->rank);
11691 : 85 : gfc_add_expr_to_block (&realloc_block, tmp);
11692 : : }
11693 : :
11694 : 3216 : gfc_add_block_to_block (&realloc_block, &caf_se.post);
11695 : 3216 : realloc_expr = gfc_finish_block (&realloc_block);
11696 : :
11697 : : /* Malloc expression. */
11698 : 3216 : gfc_init_block (&alloc_block);
11699 : 3216 : if (!coarray)
11700 : : {
11701 : 3178 : tmp = build_call_expr_loc (input_location,
11702 : : builtin_decl_explicit (BUILT_IN_MALLOC),
11703 : : 1, size2);
11704 : 3178 : gfc_conv_descriptor_data_set (&alloc_block,
11705 : : desc, tmp);
11706 : : }
11707 : : else
11708 : : {
11709 : 76 : tmp = build_call_expr_loc (input_location,
11710 : : gfor_fndecl_caf_register,
11711 : : 7, size2,
11712 : : build_int_cst (integer_type_node,
11713 : 38 : GFC_CAF_COARRAY_ALLOC),
11714 : : token, gfc_build_addr_expr (NULL_TREE, desc),
11715 : : null_pointer_node, null_pointer_node,
11716 : : integer_zero_node);
11717 : 38 : gfc_add_expr_to_block (&alloc_block, tmp);
11718 : : }
11719 : :
11720 : :
11721 : : /* We already set the dtype in the case of deferred character
11722 : : length arrays and class lvalues. */
11723 : 3216 : if (!(GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
11724 : 3216 : && ((expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11725 : 2645 : || coarray))
11726 : 5823 : && expr1->ts.type != BT_CLASS)
11727 : : {
11728 : 2221 : tmp = gfc_conv_descriptor_dtype (desc);
11729 : 2221 : gfc_add_modify (&alloc_block, tmp, gfc_get_dtype (TREE_TYPE (desc)));
11730 : : }
11731 : :
11732 : 3216 : if ((expr1->ts.type == BT_DERIVED)
11733 : 222 : && expr1->ts.u.derived->attr.alloc_comp)
11734 : : {
11735 : 85 : tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
11736 : : expr1->rank);
11737 : 85 : gfc_add_expr_to_block (&alloc_block, tmp);
11738 : : }
11739 : 3216 : alloc_expr = gfc_finish_block (&alloc_block);
11740 : :
11741 : : /* Malloc if not allocated; realloc otherwise. */
11742 : 3216 : tmp = build3_v (COND_EXPR, cond_null, alloc_expr, realloc_expr);
11743 : 3216 : gfc_add_expr_to_block (&fblock, tmp);
11744 : :
11745 : : /* Make sure that the scalarizer data pointer is updated. */
11746 : 3216 : if (linfo->data && VAR_P (linfo->data))
11747 : : {
11748 : 2830 : tmp = gfc_conv_descriptor_data_get (desc);
11749 : 2830 : gfc_add_modify (&fblock, linfo->data, tmp);
11750 : : }
11751 : :
11752 : : /* Add the label for same shape lhs and rhs. */
11753 : 3216 : tmp = build1_v (LABEL_EXPR, jump_label2);
11754 : 3216 : gfc_add_expr_to_block (&fblock, tmp);
11755 : :
11756 : 3216 : return gfc_finish_block (&fblock);
11757 : : }
11758 : :
11759 : :
11760 : : /* Initialize class descriptor's TKR information. */
11761 : :
11762 : : void
11763 : 2643 : gfc_trans_class_array (gfc_symbol * sym, gfc_wrapped_block * block)
11764 : : {
11765 : 2643 : tree type, etype;
11766 : 2643 : tree tmp;
11767 : 2643 : tree descriptor;
11768 : 2643 : stmtblock_t init;
11769 : 2643 : locus loc;
11770 : 2643 : int rank;
11771 : :
11772 : : /* Make sure the frontend gets these right. */
11773 : 2643 : gcc_assert (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
11774 : : && (CLASS_DATA (sym)->attr.class_pointer
11775 : : || CLASS_DATA (sym)->attr.allocatable));
11776 : :
11777 : 2643 : gcc_assert (VAR_P (sym->backend_decl)
11778 : : || TREE_CODE (sym->backend_decl) == PARM_DECL);
11779 : :
11780 : 2643 : if (sym->attr.dummy)
11781 : 1337 : return;
11782 : :
11783 : 2643 : descriptor = gfc_class_data_get (sym->backend_decl);
11784 : 2643 : type = TREE_TYPE (descriptor);
11785 : :
11786 : 2643 : if (type == NULL || !GFC_DESCRIPTOR_TYPE_P (type))
11787 : : return;
11788 : :
11789 : 1306 : gfc_save_backend_locus (&loc);
11790 : 1306 : gfc_set_backend_locus (&sym->declared_at);
11791 : 1306 : gfc_init_block (&init);
11792 : :
11793 : 1306 : rank = CLASS_DATA (sym)->as ? (CLASS_DATA (sym)->as->rank) : (0);
11794 : 1306 : gcc_assert (rank>=0);
11795 : 1306 : tmp = gfc_conv_descriptor_dtype (descriptor);
11796 : 1306 : etype = gfc_get_element_type (type);
11797 : 1306 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp), tmp,
11798 : : gfc_get_dtype_rank_type (rank, etype));
11799 : 1306 : gfc_add_expr_to_block (&init, tmp);
11800 : :
11801 : 1306 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
11802 : 1306 : gfc_restore_backend_locus (&loc);
11803 : : }
11804 : :
11805 : :
11806 : : /* NULLIFY an allocatable/pointer array on function entry, free it on exit.
11807 : : Do likewise, recursively if necessary, with the allocatable components of
11808 : : derived types. This function is also called for assumed-rank arrays, which
11809 : : are always dummy arguments. */
11810 : :
11811 : : void
11812 : 13473 : gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
11813 : : {
11814 : 13473 : tree type;
11815 : 13473 : tree tmp;
11816 : 13473 : tree descriptor;
11817 : 13473 : stmtblock_t init;
11818 : 13473 : stmtblock_t cleanup;
11819 : 13473 : locus loc;
11820 : 13473 : int rank;
11821 : 13473 : bool sym_has_alloc_comp, has_finalizer;
11822 : :
11823 : 26946 : sym_has_alloc_comp = (sym->ts.type == BT_DERIVED
11824 : 7877 : || sym->ts.type == BT_CLASS)
11825 : 13473 : && sym->ts.u.derived->attr.alloc_comp;
11826 : 13473 : has_finalizer = gfc_may_be_finalized (sym->ts);
11827 : :
11828 : : /* Make sure the frontend gets these right. */
11829 : 13473 : gcc_assert (sym->attr.pointer || sym->attr.allocatable || sym_has_alloc_comp
11830 : : || has_finalizer
11831 : : || (sym->as->type == AS_ASSUMED_RANK && sym->attr.dummy));
11832 : :
11833 : 13473 : gfc_save_backend_locus (&loc);
11834 : 13473 : gfc_set_backend_locus (&sym->declared_at);
11835 : 13473 : gfc_init_block (&init);
11836 : :
11837 : 13473 : gcc_assert (VAR_P (sym->backend_decl)
11838 : : || TREE_CODE (sym->backend_decl) == PARM_DECL);
11839 : :
11840 : 13473 : if (sym->ts.type == BT_CHARACTER
11841 : 1189 : && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
11842 : : {
11843 : 698 : if (sym->ts.deferred && !sym->ts.u.cl->length && !sym->attr.dummy)
11844 : 516 : gfc_add_modify (&init, sym->ts.u.cl->backend_decl,
11845 : 516 : build_zero_cst (TREE_TYPE (sym->ts.u.cl->backend_decl)));
11846 : 698 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
11847 : 698 : gfc_trans_vla_type_sizes (sym, &init);
11848 : :
11849 : : /* Presence check of optional deferred-length character dummy. */
11850 : 698 : if (sym->ts.deferred && sym->attr.dummy && sym->attr.optional)
11851 : : {
11852 : 43 : tmp = gfc_finish_block (&init);
11853 : 43 : tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
11854 : : tmp, build_empty_stmt (input_location));
11855 : 43 : gfc_add_expr_to_block (&init, tmp);
11856 : : }
11857 : : }
11858 : :
11859 : : /* Dummy, use associated and result variables don't need anything special. */
11860 : 13473 : if (sym->attr.dummy || sym->attr.use_assoc || sym->attr.result)
11861 : : {
11862 : 588 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
11863 : 588 : gfc_restore_backend_locus (&loc);
11864 : 1355 : return;
11865 : : }
11866 : :
11867 : 12885 : descriptor = sym->backend_decl;
11868 : :
11869 : : /* Although static, derived types with default initializers and
11870 : : allocatable components must not be nulled wholesale; instead they
11871 : : are treated component by component. */
11872 : 12885 : if (TREE_STATIC (descriptor) && !sym_has_alloc_comp && !has_finalizer)
11873 : : {
11874 : : /* SAVEd variables are not freed on exit. */
11875 : 179 : gfc_trans_static_array_pointer (sym);
11876 : :
11877 : 179 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
11878 : 179 : gfc_restore_backend_locus (&loc);
11879 : 179 : return;
11880 : : }
11881 : :
11882 : : /* Get the descriptor type. */
11883 : 12706 : type = TREE_TYPE (sym->backend_decl);
11884 : :
11885 : 12706 : if ((sym_has_alloc_comp || (has_finalizer && sym->ts.type != BT_CLASS))
11886 : 4368 : && !(sym->attr.pointer || sym->attr.allocatable))
11887 : : {
11888 : 2389 : if (!sym->attr.save
11889 : 2062 : && !(TREE_STATIC (sym->backend_decl) && sym->attr.is_main_program))
11890 : : {
11891 : 2062 : if (sym->value == NULL
11892 : 2062 : || !gfc_has_default_initializer (sym->ts.u.derived))
11893 : : {
11894 : 1778 : rank = sym->as ? sym->as->rank : 0;
11895 : 1778 : tmp = gfc_nullify_alloc_comp (sym->ts.u.derived,
11896 : : descriptor, rank);
11897 : 1778 : gfc_add_expr_to_block (&init, tmp);
11898 : : }
11899 : : else
11900 : 284 : gfc_init_default_dt (sym, &init, false);
11901 : : }
11902 : : }
11903 : 10317 : else if (!GFC_DESCRIPTOR_TYPE_P (type))
11904 : : {
11905 : : /* If the backend_decl is not a descriptor, we must have a pointer
11906 : : to one. */
11907 : 1537 : descriptor = build_fold_indirect_ref_loc (input_location,
11908 : : sym->backend_decl);
11909 : 1537 : type = TREE_TYPE (descriptor);
11910 : : }
11911 : :
11912 : : /* NULLIFY the data pointer, for non-saved allocatables. */
11913 : 12706 : if (GFC_DESCRIPTOR_TYPE_P (type) && !sym->attr.save && sym->attr.allocatable)
11914 : : {
11915 : 5616 : gfc_conv_descriptor_data_set (&init, descriptor, null_pointer_node);
11916 : 5616 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
11917 : : {
11918 : : /* Declare the variable static so its array descriptor stays present
11919 : : after leaving the scope. It may still be accessed through another
11920 : : image. This may happen, for example, with the caf_mpi
11921 : : implementation. */
11922 : 83 : TREE_STATIC (descriptor) = 1;
11923 : 83 : tmp = gfc_conv_descriptor_token (descriptor);
11924 : 83 : gfc_add_modify (&init, tmp, fold_convert (TREE_TYPE (tmp),
11925 : : null_pointer_node));
11926 : : }
11927 : : }
11928 : :
11929 : : /* Set initial TKR for pointers and allocatables */
11930 : 12706 : if (GFC_DESCRIPTOR_TYPE_P (type)
11931 : 12706 : && (sym->attr.pointer || sym->attr.allocatable))
11932 : : {
11933 : 8780 : tree etype;
11934 : :
11935 : 8780 : gcc_assert (sym->as && sym->as->rank>=0);
11936 : 8780 : tmp = gfc_conv_descriptor_dtype (descriptor);
11937 : 8780 : etype = gfc_get_element_type (type);
11938 : 8780 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
11939 : 8780 : TREE_TYPE (tmp), tmp,
11940 : 8780 : gfc_get_dtype_rank_type (sym->as->rank, etype));
11941 : 8780 : gfc_add_expr_to_block (&init, tmp);
11942 : : }
11943 : 12706 : gfc_restore_backend_locus (&loc);
11944 : 12706 : gfc_init_block (&cleanup);
11945 : :
11946 : : /* Allocatable arrays need to be freed when they go out of scope.
11947 : : The allocatable components of pointers must not be touched. */
11948 : 12706 : if (!sym->attr.allocatable && has_finalizer && sym->ts.type != BT_CLASS
11949 : 430 : && !sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
11950 : 210 : && !sym->ns->proc_name->attr.is_main_program)
11951 : : {
11952 : 186 : gfc_expr *e;
11953 : 186 : sym->attr.referenced = 1;
11954 : 186 : e = gfc_lval_expr_from_sym (sym);
11955 : 186 : gfc_add_finalizer_call (&cleanup, e);
11956 : 186 : gfc_free_expr (e);
11957 : 186 : }
11958 : 12520 : else if ((!sym->attr.allocatable || !has_finalizer)
11959 : 12409 : && sym_has_alloc_comp && !(sym->attr.function || sym->attr.result)
11960 : 3926 : && !sym->attr.pointer && !sym->attr.save
11961 : 2038 : && !(sym->attr.artificial && sym->name[0] == '_')
11962 : 2008 : && !sym->ns->proc_name->attr.is_main_program)
11963 : : {
11964 : 568 : int rank;
11965 : 568 : rank = sym->as ? sym->as->rank : 0;
11966 : 568 : tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank,
11967 : 568 : (sym->attr.codimension
11968 : 2 : && flag_coarray == GFC_FCOARRAY_LIB)
11969 : : ? GFC_STRUCTURE_CAF_MODE_IN_COARRAY
11970 : : : 0);
11971 : 568 : gfc_add_expr_to_block (&cleanup, tmp);
11972 : : }
11973 : :
11974 : 12706 : if (sym->attr.allocatable && (sym->attr.dimension || sym->attr.codimension)
11975 : 5622 : && !sym->attr.save && !sym->attr.result
11976 : 5616 : && !sym->ns->proc_name->attr.is_main_program)
11977 : : {
11978 : 1937 : gfc_expr *e;
11979 : 1937 : e = has_finalizer ? gfc_lval_expr_from_sym (sym) : NULL;
11980 : 3874 : tmp = gfc_deallocate_with_status (sym->backend_decl, NULL_TREE, NULL_TREE,
11981 : : NULL_TREE, NULL_TREE, true, e,
11982 : 1937 : sym->attr.codimension
11983 : : ? GFC_CAF_COARRAY_DEREGISTER
11984 : : : GFC_CAF_COARRAY_NOCOARRAY,
11985 : : NULL_TREE, gfc_finish_block (&cleanup));
11986 : 1937 : if (e)
11987 : 32 : gfc_free_expr (e);
11988 : 1937 : gfc_init_block (&cleanup);
11989 : 1937 : gfc_add_expr_to_block (&cleanup, tmp);
11990 : : }
11991 : :
11992 : 12706 : gfc_add_init_cleanup (block, gfc_finish_block (&init),
11993 : : gfc_finish_block (&cleanup));
11994 : : }
11995 : :
11996 : : /************ Expression Walking Functions ******************/
11997 : :
11998 : : /* Walk a variable reference.
11999 : :
12000 : : Possible extension - multiple component subscripts.
12001 : : x(:,:) = foo%a(:)%b(:)
12002 : : Transforms to
12003 : : forall (i=..., j=...)
12004 : : x(i,j) = foo%a(j)%b(i)
12005 : : end forall
12006 : : This adds a fair amount of complexity because you need to deal with more
12007 : : than one ref. Maybe handle in a similar manner to vector subscripts.
12008 : : Maybe not worth the effort. */
12009 : :
12010 : :
12011 : : static gfc_ss *
12012 : 515090 : gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
12013 : : {
12014 : 515090 : gfc_ref *ref;
12015 : :
12016 : 515090 : gfc_fix_class_refs (expr);
12017 : :
12018 : 610791 : for (ref = expr->ref; ref; ref = ref->next)
12019 : 371238 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
12020 : : break;
12021 : :
12022 : 515090 : return gfc_walk_array_ref (ss, expr, ref);
12023 : : }
12024 : :
12025 : :
12026 : : gfc_ss *
12027 : 515292 : gfc_walk_array_ref (gfc_ss * ss, gfc_expr * expr, gfc_ref * ref)
12028 : : {
12029 : 515292 : gfc_array_ref *ar;
12030 : 515292 : gfc_ss *newss;
12031 : 515292 : int n;
12032 : :
12033 : 797589 : for (; ref; ref = ref->next)
12034 : : {
12035 : 282297 : if (ref->type == REF_SUBSTRING)
12036 : : {
12037 : 1206 : ss = gfc_get_scalar_ss (ss, ref->u.ss.start);
12038 : 1206 : if (ref->u.ss.end)
12039 : 1194 : ss = gfc_get_scalar_ss (ss, ref->u.ss.end);
12040 : : }
12041 : :
12042 : : /* We're only interested in array sections from now on. */
12043 : 282297 : if (ref->type != REF_ARRAY)
12044 : 6330 : continue;
12045 : :
12046 : 275967 : ar = &ref->u.ar;
12047 : :
12048 : 275967 : switch (ar->type)
12049 : : {
12050 : 158 : case AR_ELEMENT:
12051 : 363 : for (n = ar->dimen - 1; n >= 0; n--)
12052 : 205 : ss = gfc_get_scalar_ss (ss, ar->start[n]);
12053 : : break;
12054 : :
12055 : 227592 : case AR_FULL:
12056 : : /* Assumed shape arrays from interface mapping need this fix. */
12057 : 227592 : if (!ar->as && expr->symtree->n.sym->as)
12058 : : {
12059 : 6 : ar->as = gfc_get_array_spec();
12060 : 6 : *ar->as = *expr->symtree->n.sym->as;
12061 : : }
12062 : 227592 : newss = gfc_get_array_ss (ss, expr, ar->as->rank, GFC_SS_SECTION);
12063 : 227592 : newss->info->data.array.ref = ref;
12064 : :
12065 : : /* Make sure array is the same as array(:,:), this way
12066 : : we don't need to special case all the time. */
12067 : 227592 : ar->dimen = ar->as->rank;
12068 : 511656 : for (n = 0; n < ar->dimen; n++)
12069 : : {
12070 : 284064 : ar->dimen_type[n] = DIMEN_RANGE;
12071 : :
12072 : 284064 : gcc_assert (ar->start[n] == NULL);
12073 : 284064 : gcc_assert (ar->end[n] == NULL);
12074 : 284064 : gcc_assert (ar->stride[n] == NULL);
12075 : : }
12076 : : ss = newss;
12077 : : break;
12078 : :
12079 : 48217 : case AR_SECTION:
12080 : 48217 : newss = gfc_get_array_ss (ss, expr, 0, GFC_SS_SECTION);
12081 : 48217 : newss->info->data.array.ref = ref;
12082 : :
12083 : : /* We add SS chains for all the subscripts in the section. */
12084 : 118499 : for (n = 0; n < ar->dimen; n++)
12085 : : {
12086 : 70282 : gfc_ss *indexss;
12087 : :
12088 : 70282 : switch (ar->dimen_type[n])
12089 : : {
12090 : 4176 : case DIMEN_ELEMENT:
12091 : : /* Add SS for elemental (scalar) subscripts. */
12092 : 4176 : gcc_assert (ar->start[n]);
12093 : 4176 : indexss = gfc_get_scalar_ss (gfc_ss_terminator, ar->start[n]);
12094 : 4176 : indexss->loop_chain = gfc_ss_terminator;
12095 : 4176 : newss->info->data.array.subscript[n] = indexss;
12096 : 4176 : break;
12097 : :
12098 : 65308 : case DIMEN_RANGE:
12099 : : /* We don't add anything for sections, just remember this
12100 : : dimension for later. */
12101 : 65308 : newss->dim[newss->dimen] = n;
12102 : 65308 : newss->dimen++;
12103 : 65308 : break;
12104 : :
12105 : 798 : case DIMEN_VECTOR:
12106 : : /* Create a GFC_SS_VECTOR index in which we can store
12107 : : the vector's descriptor. */
12108 : 798 : indexss = gfc_get_array_ss (gfc_ss_terminator, ar->start[n],
12109 : : 1, GFC_SS_VECTOR);
12110 : 798 : indexss->loop_chain = gfc_ss_terminator;
12111 : 798 : newss->info->data.array.subscript[n] = indexss;
12112 : 798 : newss->dim[newss->dimen] = n;
12113 : 798 : newss->dimen++;
12114 : 798 : break;
12115 : :
12116 : 0 : default:
12117 : : /* We should know what sort of section it is by now. */
12118 : 0 : gcc_unreachable ();
12119 : : }
12120 : : }
12121 : : /* We should have at least one non-elemental dimension,
12122 : : unless we are creating a descriptor for a (scalar) coarray. */
12123 : 48217 : gcc_assert (newss->dimen > 0
12124 : : || newss->info->data.array.ref->u.ar.as->corank > 0);
12125 : : ss = newss;
12126 : : break;
12127 : :
12128 : 0 : default:
12129 : : /* We should know what sort of section it is by now. */
12130 : 0 : gcc_unreachable ();
12131 : : }
12132 : :
12133 : : }
12134 : 515292 : return ss;
12135 : : }
12136 : :
12137 : :
12138 : : /* Walk an expression operator. If only one operand of a binary expression is
12139 : : scalar, we must also add the scalar term to the SS chain. */
12140 : :
12141 : : static gfc_ss *
12142 : 46648 : gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
12143 : : {
12144 : 46648 : gfc_ss *head;
12145 : 46648 : gfc_ss *head2;
12146 : :
12147 : 46648 : head = gfc_walk_subexpr (ss, expr->value.op.op1);
12148 : 46648 : if (expr->value.op.op2 == NULL)
12149 : : head2 = head;
12150 : : else
12151 : 44680 : head2 = gfc_walk_subexpr (head, expr->value.op.op2);
12152 : :
12153 : : /* All operands are scalar. Pass back and let the caller deal with it. */
12154 : 46648 : if (head2 == ss)
12155 : : return head2;
12156 : :
12157 : : /* All operands require scalarization. */
12158 : 42209 : if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
12159 : : return head2;
12160 : :
12161 : : /* One of the operands needs scalarization, the other is scalar.
12162 : : Create a gfc_ss for the scalar expression. */
12163 : 15400 : if (head == ss)
12164 : : {
12165 : : /* First operand is scalar. We build the chain in reverse order, so
12166 : : add the scalar SS after the second operand. */
12167 : : head = head2;
12168 : 2076 : while (head && head->next != ss)
12169 : : head = head->next;
12170 : : /* Check we haven't somehow broken the chain. */
12171 : 1833 : gcc_assert (head);
12172 : 1833 : head->next = gfc_get_scalar_ss (ss, expr->value.op.op1);
12173 : : }
12174 : : else /* head2 == head */
12175 : : {
12176 : 13567 : gcc_assert (head2 == head);
12177 : : /* Second operand is scalar. */
12178 : 13567 : head2 = gfc_get_scalar_ss (head2, expr->value.op.op2);
12179 : : }
12180 : :
12181 : : return head2;
12182 : : }
12183 : :
12184 : :
12185 : : /* Reverse a SS chain. */
12186 : :
12187 : : gfc_ss *
12188 : 670653 : gfc_reverse_ss (gfc_ss * ss)
12189 : : {
12190 : 670653 : gfc_ss *next;
12191 : 670653 : gfc_ss *head;
12192 : :
12193 : 670653 : gcc_assert (ss != NULL);
12194 : :
12195 : : head = gfc_ss_terminator;
12196 : 1026765 : while (ss != gfc_ss_terminator)
12197 : : {
12198 : 356112 : next = ss->next;
12199 : : /* Check we didn't somehow break the chain. */
12200 : 356112 : gcc_assert (next != NULL);
12201 : 356112 : ss->next = head;
12202 : 356112 : head = ss;
12203 : 356112 : ss = next;
12204 : : }
12205 : :
12206 : 670653 : return (head);
12207 : : }
12208 : :
12209 : :
12210 : : /* Given an expression referring to a procedure, return the symbol of its
12211 : : interface. We can't get the procedure symbol directly as we have to handle
12212 : : the case of (deferred) type-bound procedures. */
12213 : :
12214 : : gfc_symbol *
12215 : 138 : gfc_get_proc_ifc_for_expr (gfc_expr *procedure_ref)
12216 : : {
12217 : 138 : gfc_symbol *sym;
12218 : 138 : gfc_ref *ref;
12219 : :
12220 : 138 : if (procedure_ref == NULL)
12221 : : return NULL;
12222 : :
12223 : : /* Normal procedure case. */
12224 : 138 : if (procedure_ref->expr_type == EXPR_FUNCTION
12225 : 138 : && procedure_ref->value.function.esym)
12226 : : sym = procedure_ref->value.function.esym;
12227 : : else
12228 : 24 : sym = procedure_ref->symtree->n.sym;
12229 : :
12230 : : /* Typebound procedure case. */
12231 : 186 : for (ref = procedure_ref->ref; ref; ref = ref->next)
12232 : : {
12233 : 48 : if (ref->type == REF_COMPONENT
12234 : 48 : && ref->u.c.component->attr.proc_pointer)
12235 : 24 : sym = ref->u.c.component->ts.interface;
12236 : : else
12237 : : sym = NULL;
12238 : : }
12239 : :
12240 : : return sym;
12241 : : }
12242 : :
12243 : :
12244 : : /* Given an expression referring to an intrinsic function call,
12245 : : return the intrinsic symbol. */
12246 : :
12247 : : gfc_intrinsic_sym *
12248 : 6752 : gfc_get_intrinsic_for_expr (gfc_expr *call)
12249 : : {
12250 : 6752 : if (call == NULL)
12251 : : return NULL;
12252 : :
12253 : : /* Normal procedure case. */
12254 : 2186 : if (call->expr_type == EXPR_FUNCTION)
12255 : 2080 : return call->value.function.isym;
12256 : : else
12257 : : return NULL;
12258 : : }
12259 : :
12260 : :
12261 : : /* Indicates whether an argument to an intrinsic function should be used in
12262 : : scalarization. It is usually the case, except for some intrinsics
12263 : : requiring the value to be constant, and using the value at compile time only.
12264 : : As the value is not used at runtime in those cases, we don’t produce code
12265 : : for it, and it should not be visible to the scalarizer.
12266 : : FUNCTION is the intrinsic function being called, ACTUAL_ARG is the actual
12267 : : argument being examined in that call, and ARG_NUM the index number
12268 : : of ACTUAL_ARG in the list of arguments.
12269 : : The intrinsic procedure’s dummy argument associated with ACTUAL_ARG is
12270 : : identified using the name in ACTUAL_ARG if it is present (that is: if it’s
12271 : : a keyword argument), otherwise using ARG_NUM. */
12272 : :
12273 : : static bool
12274 : 30576 : arg_evaluated_for_scalarization (gfc_intrinsic_sym *function,
12275 : : gfc_dummy_arg *dummy_arg)
12276 : : {
12277 : 30576 : if (function != NULL && dummy_arg != NULL)
12278 : : {
12279 : 11549 : switch (function->id)
12280 : : {
12281 : 241 : case GFC_ISYM_INDEX:
12282 : 241 : case GFC_ISYM_LEN_TRIM:
12283 : 241 : case GFC_ISYM_MASKL:
12284 : 241 : case GFC_ISYM_MASKR:
12285 : 241 : case GFC_ISYM_SCAN:
12286 : 241 : case GFC_ISYM_VERIFY:
12287 : 241 : if (strcmp ("kind", gfc_dummy_arg_get_name (*dummy_arg)) == 0)
12288 : : return false;
12289 : : /* Fallthrough. */
12290 : :
12291 : : default:
12292 : : break;
12293 : : }
12294 : : }
12295 : :
12296 : : return true;
12297 : : }
12298 : :
12299 : :
12300 : : /* Walk the arguments of an elemental function.
12301 : : PROC_EXPR is used to check whether an argument is permitted to be absent. If
12302 : : it is NULL, we don't do the check and the argument is assumed to be present.
12303 : : */
12304 : :
12305 : : gfc_ss *
12306 : 20568 : gfc_walk_elemental_function_args (gfc_ss * ss, gfc_actual_arglist *arg,
12307 : : gfc_intrinsic_sym *intrinsic_sym,
12308 : : gfc_ss_type type)
12309 : : {
12310 : 20568 : int scalar;
12311 : 20568 : gfc_ss *head;
12312 : 20568 : gfc_ss *tail;
12313 : 20568 : gfc_ss *newss;
12314 : :
12315 : 20568 : head = gfc_ss_terminator;
12316 : 20568 : tail = NULL;
12317 : :
12318 : 20568 : scalar = 1;
12319 : 52321 : for (; arg; arg = arg->next)
12320 : : {
12321 : 31753 : gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
12322 : 32963 : if (!arg->expr
12323 : 30726 : || arg->expr->expr_type == EXPR_NULL
12324 : 62329 : || !arg_evaluated_for_scalarization (intrinsic_sym, dummy_arg))
12325 : 1210 : continue;
12326 : :
12327 : 30543 : newss = gfc_walk_subexpr (head, arg->expr);
12328 : 30543 : if (newss == head)
12329 : : {
12330 : : /* Scalar argument. */
12331 : 17393 : gcc_assert (type == GFC_SS_SCALAR || type == GFC_SS_REFERENCE);
12332 : 17393 : newss = gfc_get_scalar_ss (head, arg->expr);
12333 : 17393 : newss->info->type = type;
12334 : 17393 : if (dummy_arg)
12335 : 14514 : newss->info->data.scalar.dummy_arg = dummy_arg;
12336 : : }
12337 : : else
12338 : : scalar = 0;
12339 : :
12340 : 30543 : if (dummy_arg != NULL
12341 : 23661 : && gfc_dummy_arg_is_optional (*dummy_arg)
12342 : 1968 : && arg->expr->expr_type == EXPR_VARIABLE
12343 : 31897 : && (gfc_expr_attr (arg->expr).optional
12344 : 971 : || gfc_expr_attr (arg->expr).allocatable
12345 : 29889 : || gfc_expr_attr (arg->expr).pointer))
12346 : 907 : newss->info->can_be_null_ref = true;
12347 : :
12348 : 30543 : head = newss;
12349 : 30543 : if (!tail)
12350 : : {
12351 : : tail = head;
12352 : 26052 : while (tail->next != gfc_ss_terminator)
12353 : : tail = tail->next;
12354 : : }
12355 : : }
12356 : :
12357 : 20568 : if (scalar)
12358 : : {
12359 : : /* If all the arguments are scalar we don't need the argument SS. */
12360 : 9634 : gfc_free_ss_chain (head);
12361 : : /* Pass it back. */
12362 : 9634 : return ss;
12363 : : }
12364 : :
12365 : : /* Add it onto the existing chain. */
12366 : 10934 : tail->next = ss;
12367 : 10934 : return head;
12368 : : }
12369 : :
12370 : :
12371 : : /* Walk a function call. Scalar functions are passed back, and taken out of
12372 : : scalarization loops. For elemental functions we walk their arguments.
12373 : : The result of functions returning arrays is stored in a temporary outside
12374 : : the loop, so that the function is only called once. Hence we do not need
12375 : : to walk their arguments. */
12376 : :
12377 : : static gfc_ss *
12378 : 43449 : gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
12379 : : {
12380 : 43449 : gfc_intrinsic_sym *isym;
12381 : 43449 : gfc_symbol *sym;
12382 : 43449 : gfc_component *comp = NULL;
12383 : :
12384 : 43449 : isym = expr->value.function.isym;
12385 : :
12386 : : /* Handle intrinsic functions separately. */
12387 : 43449 : if (isym)
12388 : 36359 : return gfc_walk_intrinsic_function (ss, expr, isym);
12389 : :
12390 : 7090 : sym = expr->value.function.esym;
12391 : 7090 : if (!sym)
12392 : 539 : sym = expr->symtree->n.sym;
12393 : :
12394 : 7090 : if (gfc_is_class_array_function (expr))
12395 : 216 : return gfc_get_array_ss (ss, expr,
12396 : 216 : CLASS_DATA (expr->value.function.esym->result)->as->rank,
12397 : 216 : GFC_SS_FUNCTION);
12398 : :
12399 : : /* A function that returns arrays. */
12400 : 6874 : comp = gfc_get_proc_ptr_comp (expr);
12401 : 6483 : if ((!comp && gfc_return_by_reference (sym) && sym->result->attr.dimension)
12402 : 6874 : || (comp && comp->attr.dimension))
12403 : 2559 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
12404 : :
12405 : : /* Walk the parameters of an elemental function. For now we always pass
12406 : : by reference. */
12407 : 4315 : if (sym->attr.elemental || (comp && comp->attr.elemental))
12408 : : {
12409 : 2044 : gfc_ss *old_ss = ss;
12410 : :
12411 : 2044 : ss = gfc_walk_elemental_function_args (old_ss,
12412 : : expr->value.function.actual,
12413 : : gfc_get_intrinsic_for_expr (expr),
12414 : : GFC_SS_REFERENCE);
12415 : 2044 : if (ss != old_ss
12416 : 1010 : && (comp
12417 : 949 : || sym->attr.proc_pointer
12418 : : || sym->attr.if_source != IFSRC_DECL
12419 : 949 : || sym->attr.array_outer_dependency))
12420 : 224 : ss->info->array_outer_dependency = 1;
12421 : : }
12422 : :
12423 : : /* Scalar functions are OK as these are evaluated outside the scalarization
12424 : : loop. Pass back and let the caller deal with it. */
12425 : : return ss;
12426 : : }
12427 : :
12428 : :
12429 : : /* An array temporary is constructed for array constructors. */
12430 : :
12431 : : static gfc_ss *
12432 : 41516 : gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
12433 : : {
12434 : 0 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_CONSTRUCTOR);
12435 : : }
12436 : :
12437 : :
12438 : : /* Walk an expression. Add walked expressions to the head of the SS chain.
12439 : : A wholly scalar expression will not be added. */
12440 : :
12441 : : gfc_ss *
12442 : 792826 : gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
12443 : : {
12444 : 792826 : gfc_ss *head;
12445 : :
12446 : 792826 : switch (expr->expr_type)
12447 : : {
12448 : 515090 : case EXPR_VARIABLE:
12449 : 515090 : head = gfc_walk_variable_expr (ss, expr);
12450 : 515090 : return head;
12451 : :
12452 : 46648 : case EXPR_OP:
12453 : 46648 : head = gfc_walk_op_expr (ss, expr);
12454 : 46648 : return head;
12455 : :
12456 : 43449 : case EXPR_FUNCTION:
12457 : 43449 : head = gfc_walk_function_expr (ss, expr);
12458 : 43449 : return head;
12459 : :
12460 : : case EXPR_CONSTANT:
12461 : : case EXPR_NULL:
12462 : : case EXPR_STRUCTURE:
12463 : : /* Pass back and let the caller deal with it. */
12464 : : break;
12465 : :
12466 : 41516 : case EXPR_ARRAY:
12467 : 41516 : head = gfc_walk_array_constructor (ss, expr);
12468 : 41516 : return head;
12469 : :
12470 : : case EXPR_SUBSTRING:
12471 : : /* Pass back and let the caller deal with it. */
12472 : : break;
12473 : :
12474 : 0 : default:
12475 : 0 : gfc_internal_error ("bad expression type during walk (%d)",
12476 : : expr->expr_type);
12477 : : }
12478 : : return ss;
12479 : : }
12480 : :
12481 : :
12482 : : /* Entry point for expression walking.
12483 : : A return value equal to the passed chain means this is
12484 : : a scalar expression. It is up to the caller to take whatever action is
12485 : : necessary to translate these. */
12486 : :
12487 : : gfc_ss *
12488 : 668679 : gfc_walk_expr (gfc_expr * expr)
12489 : : {
12490 : 668679 : gfc_ss *res;
12491 : :
12492 : 668679 : res = gfc_walk_subexpr (gfc_ss_terminator, expr);
12493 : 668679 : return gfc_reverse_ss (res);
12494 : : }
|