Branch data Line data Source code
1 : : /* Array translation routines
2 : : Copyright (C) 2002-2025 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 : 57812 : gfc_array_dataptr_type (tree desc)
105 : : {
106 : 57812 : 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 : 1927791 : gfc_get_descriptor_field (tree desc, unsigned field_idx)
246 : : {
247 : 1927791 : tree type = TREE_TYPE (desc);
248 : 1927791 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
249 : :
250 : 1927791 : tree field = gfc_advance_chain (TYPE_FIELDS (type), field_idx);
251 : 1927791 : gcc_assert (field != NULL_TREE);
252 : :
253 : 1927791 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
254 : 1927791 : 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 : 261330 : gfc_conv_descriptor_data_get (tree desc)
262 : : {
263 : 261330 : tree type = TREE_TYPE (desc);
264 : 261330 : if (TREE_CODE (type) == REFERENCE_TYPE)
265 : 0 : gcc_unreachable ();
266 : :
267 : 261330 : tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
268 : 261330 : 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 : 147608 : gfc_conv_descriptor_data_set (stmtblock_t *block, tree desc, tree value)
281 : : {
282 : 147608 : tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
283 : 147608 : gfc_add_modify (block, field, fold_convert (TREE_TYPE (field), value));
284 : 147608 : }
285 : :
286 : :
287 : : static tree
288 : 199396 : gfc_conv_descriptor_offset (tree desc)
289 : : {
290 : 199396 : tree field = gfc_get_descriptor_field (desc, OFFSET_FIELD);
291 : 199396 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
292 : 199396 : return field;
293 : : }
294 : :
295 : : tree
296 : 72057 : gfc_conv_descriptor_offset_get (tree desc)
297 : : {
298 : 72057 : return gfc_conv_descriptor_offset (desc);
299 : : }
300 : :
301 : : void
302 : 121482 : gfc_conv_descriptor_offset_set (stmtblock_t *block, tree desc,
303 : : tree value)
304 : : {
305 : 121482 : tree t = gfc_conv_descriptor_offset (desc);
306 : 121482 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
307 : 121482 : }
308 : :
309 : :
310 : : tree
311 : 167074 : gfc_conv_descriptor_dtype (tree desc)
312 : : {
313 : 167074 : tree field = gfc_get_descriptor_field (desc, DTYPE_FIELD);
314 : 167074 : gcc_assert (TREE_TYPE (field) == get_dtype_type_node ());
315 : 167074 : return field;
316 : : }
317 : :
318 : : static tree
319 : 150497 : gfc_conv_descriptor_span (tree desc)
320 : : {
321 : 150497 : tree field = gfc_get_descriptor_field (desc, SPAN_FIELD);
322 : 150497 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
323 : 150497 : return field;
324 : : }
325 : :
326 : : tree
327 : 32691 : gfc_conv_descriptor_span_get (tree desc)
328 : : {
329 : 32691 : return gfc_conv_descriptor_span (desc);
330 : : }
331 : :
332 : : void
333 : 117806 : gfc_conv_descriptor_span_set (stmtblock_t *block, tree desc,
334 : : tree value)
335 : : {
336 : 117806 : tree t = gfc_conv_descriptor_span (desc);
337 : 117806 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
338 : 117806 : }
339 : :
340 : :
341 : : tree
342 : 19121 : gfc_conv_descriptor_rank (tree desc)
343 : : {
344 : 19121 : tree tmp;
345 : 19121 : tree dtype;
346 : :
347 : 19121 : dtype = gfc_conv_descriptor_dtype (desc);
348 : 19121 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_RANK);
349 : 19121 : gcc_assert (tmp != NULL_TREE
350 : : && TREE_TYPE (tmp) == signed_char_type_node);
351 : 19121 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
352 : 19121 : dtype, tmp, NULL_TREE);
353 : : }
354 : :
355 : :
356 : : tree
357 : 127 : gfc_conv_descriptor_version (tree desc)
358 : : {
359 : 127 : tree tmp;
360 : 127 : tree dtype;
361 : :
362 : 127 : dtype = gfc_conv_descriptor_dtype (desc);
363 : 127 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_VERSION);
364 : 127 : gcc_assert (tmp != NULL_TREE
365 : : && TREE_TYPE (tmp) == integer_type_node);
366 : 127 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
367 : 127 : dtype, tmp, NULL_TREE);
368 : : }
369 : :
370 : :
371 : : /* Return the element length from the descriptor dtype field. */
372 : :
373 : : tree
374 : 8368 : gfc_conv_descriptor_elem_len (tree desc)
375 : : {
376 : 8368 : tree tmp;
377 : 8368 : tree dtype;
378 : :
379 : 8368 : dtype = gfc_conv_descriptor_dtype (desc);
380 : 8368 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
381 : : GFC_DTYPE_ELEM_LEN);
382 : 8368 : gcc_assert (tmp != NULL_TREE
383 : : && TREE_TYPE (tmp) == size_type_node);
384 : 8368 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
385 : 8368 : dtype, tmp, NULL_TREE);
386 : : }
387 : :
388 : :
389 : : tree
390 : 0 : gfc_conv_descriptor_attribute (tree desc)
391 : : {
392 : 0 : tree tmp;
393 : 0 : tree dtype;
394 : :
395 : 0 : dtype = gfc_conv_descriptor_dtype (desc);
396 : 0 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
397 : : GFC_DTYPE_ATTRIBUTE);
398 : 0 : gcc_assert (tmp!= NULL_TREE
399 : : && TREE_TYPE (tmp) == short_integer_type_node);
400 : 0 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
401 : 0 : dtype, tmp, NULL_TREE);
402 : : }
403 : :
404 : : tree
405 : 73 : gfc_conv_descriptor_type (tree desc)
406 : : {
407 : 73 : tree tmp;
408 : 73 : tree dtype;
409 : :
410 : 73 : dtype = gfc_conv_descriptor_dtype (desc);
411 : 73 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_TYPE);
412 : 73 : gcc_assert (tmp!= NULL_TREE
413 : : && TREE_TYPE (tmp) == signed_char_type_node);
414 : 73 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
415 : 73 : dtype, tmp, NULL_TREE);
416 : : }
417 : :
418 : : tree
419 : 1000269 : gfc_get_descriptor_dimension (tree desc)
420 : : {
421 : 1000269 : tree field = gfc_get_descriptor_field (desc, DIMENSION_FIELD);
422 : 1000269 : gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
423 : : && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == RECORD_TYPE);
424 : 1000269 : return field;
425 : : }
426 : :
427 : :
428 : : static tree
429 : 996293 : gfc_conv_descriptor_dimension (tree desc, tree dim)
430 : : {
431 : 996293 : tree tmp;
432 : :
433 : 996293 : tmp = gfc_get_descriptor_dimension (desc);
434 : :
435 : 996293 : return gfc_build_array_ref (tmp, dim, NULL_TREE, true);
436 : : }
437 : :
438 : :
439 : : tree
440 : 1617 : gfc_conv_descriptor_token (tree desc)
441 : : {
442 : 1617 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
443 : 1617 : tree field = gfc_get_descriptor_field (desc, CAF_TOKEN_FIELD);
444 : : /* Should be a restricted pointer - except in the finalization wrapper. */
445 : 1617 : gcc_assert (TREE_TYPE (field) == prvoid_type_node
446 : : || TREE_TYPE (field) == pvoid_type_node);
447 : 1617 : return field;
448 : : }
449 : :
450 : : static tree
451 : 996293 : gfc_conv_descriptor_subfield (tree desc, tree dim, unsigned field_idx)
452 : : {
453 : 996293 : tree tmp = gfc_conv_descriptor_dimension (desc, dim);
454 : 996293 : tree field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (tmp)), field_idx);
455 : 996293 : gcc_assert (field != NULL_TREE);
456 : :
457 : 996293 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
458 : 996293 : tmp, field, NULL_TREE);
459 : : }
460 : :
461 : : static tree
462 : 269094 : gfc_conv_descriptor_stride (tree desc, tree dim)
463 : : {
464 : 269094 : tree field = gfc_conv_descriptor_subfield (desc, dim, STRIDE_SUBFIELD);
465 : 269094 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
466 : 269094 : return field;
467 : : }
468 : :
469 : : tree
470 : 158006 : gfc_conv_descriptor_stride_get (tree desc, tree dim)
471 : : {
472 : 158006 : tree type = TREE_TYPE (desc);
473 : 158006 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
474 : 158006 : if (integer_zerop (dim)
475 : 158006 : && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
476 : 41406 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT
477 : 40338 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT
478 : 40188 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
479 : 40038 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT
480 : 40038 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT))
481 : 64272 : return gfc_index_one_node;
482 : :
483 : 93734 : return gfc_conv_descriptor_stride (desc, dim);
484 : : }
485 : :
486 : : void
487 : 175360 : gfc_conv_descriptor_stride_set (stmtblock_t *block, tree desc,
488 : : tree dim, tree value)
489 : : {
490 : 175360 : tree t = gfc_conv_descriptor_stride (desc, dim);
491 : 175360 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
492 : 175360 : }
493 : :
494 : : static tree
495 : 377414 : gfc_conv_descriptor_lbound (tree desc, tree dim)
496 : : {
497 : 377414 : tree field = gfc_conv_descriptor_subfield (desc, dim, LBOUND_SUBFIELD);
498 : 377414 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
499 : 377414 : return field;
500 : : }
501 : :
502 : : tree
503 : 198386 : gfc_conv_descriptor_lbound_get (tree desc, tree dim)
504 : : {
505 : 198386 : return gfc_conv_descriptor_lbound (desc, dim);
506 : : }
507 : :
508 : : void
509 : 179028 : gfc_conv_descriptor_lbound_set (stmtblock_t *block, tree desc,
510 : : tree dim, tree value)
511 : : {
512 : 179028 : tree t = gfc_conv_descriptor_lbound (desc, dim);
513 : 179028 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
514 : 179028 : }
515 : :
516 : : static tree
517 : 349785 : gfc_conv_descriptor_ubound (tree desc, tree dim)
518 : : {
519 : 349785 : tree field = gfc_conv_descriptor_subfield (desc, dim, UBOUND_SUBFIELD);
520 : 349785 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
521 : 349785 : return field;
522 : : }
523 : :
524 : : tree
525 : 170658 : gfc_conv_descriptor_ubound_get (tree desc, tree dim)
526 : : {
527 : 170658 : return gfc_conv_descriptor_ubound (desc, dim);
528 : : }
529 : :
530 : : void
531 : 179127 : gfc_conv_descriptor_ubound_set (stmtblock_t *block, tree desc,
532 : : tree dim, tree value)
533 : : {
534 : 179127 : tree t = gfc_conv_descriptor_ubound (desc, dim);
535 : 179127 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
536 : 179127 : }
537 : :
538 : : /* Build a null array descriptor constructor. */
539 : :
540 : : tree
541 : 964 : gfc_build_null_descriptor (tree type)
542 : : {
543 : 964 : tree field;
544 : 964 : tree tmp;
545 : :
546 : 964 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
547 : 964 : gcc_assert (DATA_FIELD == 0);
548 : 964 : field = TYPE_FIELDS (type);
549 : :
550 : : /* Set a NULL data pointer. */
551 : 964 : tmp = build_constructor_single (type, field, null_pointer_node);
552 : 964 : TREE_CONSTANT (tmp) = 1;
553 : : /* All other fields are ignored. */
554 : :
555 : 964 : return tmp;
556 : : }
557 : :
558 : :
559 : : /* Modify a descriptor such that the lbound of a given dimension is the value
560 : : specified. This also updates ubound and offset accordingly. */
561 : :
562 : : void
563 : 870 : gfc_conv_shift_descriptor_lbound (stmtblock_t* block, tree desc,
564 : : int dim, tree new_lbound)
565 : : {
566 : 870 : tree offs, ubound, lbound, stride;
567 : 870 : tree diff, offs_diff;
568 : :
569 : 870 : new_lbound = fold_convert (gfc_array_index_type, new_lbound);
570 : :
571 : 870 : offs = gfc_conv_descriptor_offset_get (desc);
572 : 870 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
573 : 870 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
574 : 870 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[dim]);
575 : :
576 : : /* Get difference (new - old) by which to shift stuff. */
577 : 870 : diff = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
578 : : new_lbound, lbound);
579 : :
580 : : /* Shift ubound and offset accordingly. This has to be done before
581 : : updating the lbound, as they depend on the lbound expression! */
582 : 870 : ubound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
583 : : ubound, diff);
584 : 870 : gfc_conv_descriptor_ubound_set (block, desc, gfc_rank_cst[dim], ubound);
585 : 870 : offs_diff = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
586 : : diff, stride);
587 : 870 : offs = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
588 : : offs, offs_diff);
589 : 870 : gfc_conv_descriptor_offset_set (block, desc, offs);
590 : :
591 : : /* Finally set lbound to value we want. */
592 : 870 : gfc_conv_descriptor_lbound_set (block, desc, gfc_rank_cst[dim], new_lbound);
593 : 870 : }
594 : :
595 : :
596 : : /* Obtain offsets for trans-types.cc(gfc_get_array_descr_info). */
597 : :
598 : : void
599 : 267879 : gfc_get_descriptor_offsets_for_info (const_tree desc_type, tree *data_off,
600 : : tree *dtype_off, tree *span_off,
601 : : tree *dim_off, tree *dim_size,
602 : : tree *stride_suboff, tree *lower_suboff,
603 : : tree *upper_suboff)
604 : : {
605 : 267879 : tree field;
606 : 267879 : tree type;
607 : :
608 : 267879 : type = TYPE_MAIN_VARIANT (desc_type);
609 : 267879 : field = gfc_advance_chain (TYPE_FIELDS (type), DATA_FIELD);
610 : 267879 : *data_off = byte_position (field);
611 : 267879 : field = gfc_advance_chain (TYPE_FIELDS (type), DTYPE_FIELD);
612 : 267879 : *dtype_off = byte_position (field);
613 : 267879 : field = gfc_advance_chain (TYPE_FIELDS (type), SPAN_FIELD);
614 : 267879 : *span_off = byte_position (field);
615 : 267879 : field = gfc_advance_chain (TYPE_FIELDS (type), DIMENSION_FIELD);
616 : 267879 : *dim_off = byte_position (field);
617 : 267879 : type = TREE_TYPE (TREE_TYPE (field));
618 : 267879 : *dim_size = TYPE_SIZE_UNIT (type);
619 : 267879 : field = gfc_advance_chain (TYPE_FIELDS (type), STRIDE_SUBFIELD);
620 : 267879 : *stride_suboff = byte_position (field);
621 : 267879 : field = gfc_advance_chain (TYPE_FIELDS (type), LBOUND_SUBFIELD);
622 : 267879 : *lower_suboff = byte_position (field);
623 : 267879 : field = gfc_advance_chain (TYPE_FIELDS (type), UBOUND_SUBFIELD);
624 : 267879 : *upper_suboff = byte_position (field);
625 : 267879 : }
626 : :
627 : :
628 : : /* Cleanup those #defines. */
629 : :
630 : : #undef DATA_FIELD
631 : : #undef OFFSET_FIELD
632 : : #undef DTYPE_FIELD
633 : : #undef SPAN_FIELD
634 : : #undef DIMENSION_FIELD
635 : : #undef CAF_TOKEN_FIELD
636 : : #undef STRIDE_SUBFIELD
637 : : #undef LBOUND_SUBFIELD
638 : : #undef UBOUND_SUBFIELD
639 : :
640 : :
641 : : /* Mark a SS chain as used. Flags specifies in which loops the SS is used.
642 : : flags & 1 = Main loop body.
643 : : flags & 2 = temp copy loop. */
644 : :
645 : : void
646 : 166932 : gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
647 : : {
648 : 392534 : for (; ss != gfc_ss_terminator; ss = ss->next)
649 : 225602 : ss->info->useflags = flags;
650 : 166932 : }
651 : :
652 : :
653 : : /* Free a gfc_ss chain. */
654 : :
655 : : void
656 : 172623 : gfc_free_ss_chain (gfc_ss * ss)
657 : : {
658 : 172623 : gfc_ss *next;
659 : :
660 : 353452 : while (ss != gfc_ss_terminator)
661 : : {
662 : 180829 : gcc_assert (ss != NULL);
663 : 180829 : next = ss->next;
664 : 180829 : gfc_free_ss (ss);
665 : 180829 : ss = next;
666 : : }
667 : 172623 : }
668 : :
669 : :
670 : : static void
671 : 475113 : free_ss_info (gfc_ss_info *ss_info)
672 : : {
673 : 475113 : int n;
674 : :
675 : 475113 : ss_info->refcount--;
676 : 475113 : if (ss_info->refcount > 0)
677 : : return;
678 : :
679 : 470366 : gcc_assert (ss_info->refcount == 0);
680 : :
681 : 470366 : switch (ss_info->type)
682 : : {
683 : : case GFC_SS_SECTION:
684 : 5198960 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
685 : 4874025 : if (ss_info->data.array.subscript[n])
686 : 6822 : gfc_free_ss_chain (ss_info->data.array.subscript[n]);
687 : : break;
688 : :
689 : : default:
690 : : break;
691 : : }
692 : :
693 : 470366 : free (ss_info);
694 : : }
695 : :
696 : :
697 : : /* Free a SS. */
698 : :
699 : : void
700 : 475113 : gfc_free_ss (gfc_ss * ss)
701 : : {
702 : 475113 : free_ss_info (ss->info);
703 : 475113 : free (ss);
704 : 475113 : }
705 : :
706 : :
707 : : /* Creates and initializes an array type gfc_ss struct. */
708 : :
709 : : gfc_ss *
710 : 398861 : gfc_get_array_ss (gfc_ss *next, gfc_expr *expr, int dimen, gfc_ss_type type)
711 : : {
712 : 398861 : gfc_ss *ss;
713 : 398861 : gfc_ss_info *ss_info;
714 : 398861 : int i;
715 : :
716 : 398861 : ss_info = gfc_get_ss_info ();
717 : 398861 : ss_info->refcount++;
718 : 398861 : ss_info->type = type;
719 : 398861 : ss_info->expr = expr;
720 : :
721 : 398861 : ss = gfc_get_ss ();
722 : 398861 : ss->info = ss_info;
723 : 398861 : ss->next = next;
724 : 398861 : ss->dimen = dimen;
725 : 847080 : for (i = 0; i < ss->dimen; i++)
726 : 448219 : ss->dim[i] = i;
727 : :
728 : 398861 : return ss;
729 : : }
730 : :
731 : :
732 : : /* Creates and initializes a temporary type gfc_ss struct. */
733 : :
734 : : gfc_ss *
735 : 11187 : gfc_get_temp_ss (tree type, tree string_length, int dimen)
736 : : {
737 : 11187 : gfc_ss *ss;
738 : 11187 : gfc_ss_info *ss_info;
739 : 11187 : int i;
740 : :
741 : 11187 : ss_info = gfc_get_ss_info ();
742 : 11187 : ss_info->refcount++;
743 : 11187 : ss_info->type = GFC_SS_TEMP;
744 : 11187 : ss_info->string_length = string_length;
745 : 11187 : ss_info->data.temp.type = type;
746 : :
747 : 11187 : ss = gfc_get_ss ();
748 : 11187 : ss->info = ss_info;
749 : 11187 : ss->next = gfc_ss_terminator;
750 : 11187 : ss->dimen = dimen;
751 : 25009 : for (i = 0; i < ss->dimen; i++)
752 : 13822 : ss->dim[i] = i;
753 : :
754 : 11187 : return ss;
755 : : }
756 : :
757 : :
758 : : /* Creates and initializes a scalar type gfc_ss struct. */
759 : :
760 : : gfc_ss *
761 : 64333 : gfc_get_scalar_ss (gfc_ss *next, gfc_expr *expr)
762 : : {
763 : 64333 : gfc_ss *ss;
764 : 64333 : gfc_ss_info *ss_info;
765 : :
766 : 64333 : ss_info = gfc_get_ss_info ();
767 : 64333 : ss_info->refcount++;
768 : 64333 : ss_info->type = GFC_SS_SCALAR;
769 : 64333 : ss_info->expr = expr;
770 : :
771 : 64333 : ss = gfc_get_ss ();
772 : 64333 : ss->info = ss_info;
773 : 64333 : ss->next = next;
774 : :
775 : 64333 : return ss;
776 : : }
777 : :
778 : :
779 : : /* Free all the SS associated with a loop. */
780 : :
781 : : void
782 : 178050 : gfc_cleanup_loop (gfc_loopinfo * loop)
783 : : {
784 : 178050 : gfc_loopinfo *loop_next, **ploop;
785 : 178050 : gfc_ss *ss;
786 : 178050 : gfc_ss *next;
787 : :
788 : 178050 : ss = loop->ss;
789 : 471847 : while (ss != gfc_ss_terminator)
790 : : {
791 : 293797 : gcc_assert (ss != NULL);
792 : 293797 : next = ss->loop_chain;
793 : 293797 : gfc_free_ss (ss);
794 : 293797 : ss = next;
795 : : }
796 : :
797 : : /* Remove reference to self in the parent loop. */
798 : 178050 : if (loop->parent)
799 : 3364 : for (ploop = &loop->parent->nested; *ploop; ploop = &(*ploop)->next)
800 : 3364 : if (*ploop == loop)
801 : : {
802 : 3364 : *ploop = loop->next;
803 : 3364 : break;
804 : : }
805 : :
806 : : /* Free non-freed nested loops. */
807 : 181414 : for (loop = loop->nested; loop; loop = loop_next)
808 : : {
809 : 3364 : loop_next = loop->next;
810 : 3364 : gfc_cleanup_loop (loop);
811 : 3364 : free (loop);
812 : : }
813 : 178050 : }
814 : :
815 : :
816 : : static void
817 : 240927 : set_ss_loop (gfc_ss *ss, gfc_loopinfo *loop)
818 : : {
819 : 240927 : int n;
820 : :
821 : 543008 : for (; ss != gfc_ss_terminator; ss = ss->next)
822 : : {
823 : 302081 : ss->loop = loop;
824 : :
825 : 302081 : if (ss->info->type == GFC_SS_SCALAR
826 : : || ss->info->type == GFC_SS_REFERENCE
827 : 255622 : || ss->info->type == GFC_SS_TEMP)
828 : 57646 : continue;
829 : :
830 : 3910960 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
831 : 3666525 : if (ss->info->data.array.subscript[n] != NULL)
832 : 6672 : set_ss_loop (ss->info->data.array.subscript[n], loop);
833 : : }
834 : 240927 : }
835 : :
836 : :
837 : : /* Associate a SS chain with a loop. */
838 : :
839 : : void
840 : 234255 : gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
841 : : {
842 : 234255 : gfc_ss *ss;
843 : 234255 : gfc_loopinfo *nested_loop;
844 : :
845 : 234255 : if (head == gfc_ss_terminator)
846 : : return;
847 : :
848 : 234255 : set_ss_loop (head, loop);
849 : :
850 : 234255 : ss = head;
851 : 763919 : for (; ss && ss != gfc_ss_terminator; ss = ss->next)
852 : : {
853 : 295409 : if (ss->nested_ss)
854 : : {
855 : 4740 : nested_loop = ss->nested_ss->loop;
856 : :
857 : : /* More than one ss can belong to the same loop. Hence, we add the
858 : : loop to the chain only if it is different from the previously
859 : : added one, to avoid duplicate nested loops. */
860 : 4740 : if (nested_loop != loop->nested)
861 : : {
862 : 3364 : gcc_assert (nested_loop->parent == NULL);
863 : 3364 : nested_loop->parent = loop;
864 : :
865 : 3364 : gcc_assert (nested_loop->next == NULL);
866 : 3364 : nested_loop->next = loop->nested;
867 : 3364 : loop->nested = nested_loop;
868 : : }
869 : : else
870 : 1376 : gcc_assert (nested_loop->parent == loop);
871 : : }
872 : :
873 : 295409 : if (ss->next == gfc_ss_terminator)
874 : 234255 : ss->loop_chain = loop->ss;
875 : : else
876 : 61154 : ss->loop_chain = ss->next;
877 : : }
878 : 234255 : gcc_assert (ss == gfc_ss_terminator);
879 : 234255 : loop->ss = head;
880 : : }
881 : :
882 : :
883 : : /* Returns true if the expression is an array pointer. */
884 : :
885 : : static bool
886 : 354519 : is_pointer_array (tree expr)
887 : : {
888 : 354519 : if (expr == NULL_TREE
889 : 354519 : || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr))
890 : 446891 : || GFC_CLASS_TYPE_P (TREE_TYPE (expr)))
891 : : return false;
892 : :
893 : 92372 : if (VAR_P (expr)
894 : 92372 : && GFC_DECL_PTR_ARRAY_P (expr))
895 : : return true;
896 : :
897 : 86258 : if (TREE_CODE (expr) == PARM_DECL
898 : 86258 : && GFC_DECL_PTR_ARRAY_P (expr))
899 : : return true;
900 : :
901 : 86258 : if (INDIRECT_REF_P (expr)
902 : 86258 : && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 0)))
903 : : return true;
904 : :
905 : : /* The field declaration is marked as an pointer array. */
906 : 83860 : if (TREE_CODE (expr) == COMPONENT_REF
907 : 12322 : && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 1))
908 : 86695 : && !GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
909 : 2835 : return true;
910 : :
911 : : return false;
912 : : }
913 : :
914 : :
915 : : /* If the symbol or expression reference a CFI descriptor, return the
916 : : pointer to the converted gfc descriptor. If an array reference is
917 : : present as the last argument, check that it is the one applied to
918 : : the CFI descriptor in the expression. Note that the CFI object is
919 : : always the symbol in the expression! */
920 : :
921 : : static bool
922 : 356424 : get_CFI_desc (gfc_symbol *sym, gfc_expr *expr,
923 : : tree *desc, gfc_array_ref *ar)
924 : : {
925 : 356424 : tree tmp;
926 : :
927 : 356424 : if (!is_CFI_desc (sym, expr))
928 : : return false;
929 : :
930 : 4727 : if (expr && ar)
931 : : {
932 : 4061 : if (!(expr->ref && expr->ref->type == REF_ARRAY)
933 : 4043 : || (&expr->ref->u.ar != ar))
934 : : return false;
935 : : }
936 : :
937 : 4697 : if (sym == NULL)
938 : 1108 : tmp = expr->symtree->n.sym->backend_decl;
939 : : else
940 : 3589 : tmp = sym->backend_decl;
941 : :
942 : 4697 : if (tmp && DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
943 : 0 : tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
944 : :
945 : 4697 : *desc = tmp;
946 : 4697 : return true;
947 : : }
948 : :
949 : :
950 : : /* A helper function for gfc_get_array_span that returns the array element size
951 : : of a class entity. */
952 : : static tree
953 : 1078 : class_array_element_size (tree decl, bool unlimited)
954 : : {
955 : : /* Class dummys usually require extraction from the saved descriptor,
956 : : which gfc_class_vptr_get does for us if necessary. This, of course,
957 : : will be a component of the class object. */
958 : 1078 : tree vptr = gfc_class_vptr_get (decl);
959 : : /* If this is an unlimited polymorphic entity with a character payload,
960 : : the element size will be corrected for the string length. */
961 : 1078 : if (unlimited)
962 : 974 : return gfc_resize_class_size_with_len (NULL,
963 : 487 : TREE_OPERAND (vptr, 0),
964 : 487 : gfc_vptr_size_get (vptr));
965 : : else
966 : 591 : return gfc_vptr_size_get (vptr);
967 : : }
968 : :
969 : :
970 : : /* Return the span of an array. */
971 : :
972 : : tree
973 : 56973 : gfc_get_array_span (tree desc, gfc_expr *expr)
974 : : {
975 : 56973 : tree tmp;
976 : 56973 : gfc_symbol *sym = (expr && expr->expr_type == EXPR_VARIABLE) ?
977 : 50387 : expr->symtree->n.sym : NULL;
978 : :
979 : 56973 : if (is_pointer_array (desc)
980 : 56973 : || (get_CFI_desc (NULL, expr, &desc, NULL)
981 : 1332 : && (POINTER_TYPE_P (TREE_TYPE (desc))
982 : 666 : ? GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc)))
983 : 0 : : GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))))
984 : : {
985 : 557 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
986 : 0 : desc = build_fold_indirect_ref_loc (input_location, desc);
987 : :
988 : : /* This will have the span field set. */
989 : 557 : tmp = gfc_conv_descriptor_span_get (desc);
990 : : }
991 : 56416 : else if (expr->ts.type == BT_ASSUMED)
992 : : {
993 : 127 : if (DECL_LANG_SPECIFIC (desc) && GFC_DECL_SAVED_DESCRIPTOR (desc))
994 : 127 : desc = GFC_DECL_SAVED_DESCRIPTOR (desc);
995 : 127 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
996 : 127 : desc = build_fold_indirect_ref_loc (input_location, desc);
997 : 127 : tmp = gfc_conv_descriptor_span_get (desc);
998 : : }
999 : 56289 : else if (TREE_CODE (desc) == COMPONENT_REF
1000 : 481 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
1001 : 56414 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (desc, 0))))
1002 : : /* The descriptor is the _data field of a class object. */
1003 : 56 : tmp = class_array_element_size (TREE_OPERAND (desc, 0),
1004 : 56 : UNLIMITED_POLY (expr));
1005 : 56233 : else if (sym && sym->ts.type == BT_CLASS
1006 : 1063 : && expr->ref->type == REF_COMPONENT
1007 : 1063 : && expr->ref->next->type == REF_ARRAY
1008 : 1063 : && expr->ref->next->next == NULL
1009 : 1045 : && CLASS_DATA (sym)->attr.dimension)
1010 : : /* Having escaped the above, this can only be a class array dummy. */
1011 : 1022 : tmp = class_array_element_size (sym->backend_decl,
1012 : 1022 : UNLIMITED_POLY (sym));
1013 : : else
1014 : : {
1015 : : /* If none of the fancy stuff works, the span is the element
1016 : : size of the array. Attempt to deal with unbounded character
1017 : : types if possible. Otherwise, return NULL_TREE. */
1018 : 55211 : tmp = gfc_get_element_type (TREE_TYPE (desc));
1019 : 55211 : if (tmp && TREE_CODE (tmp) == ARRAY_TYPE && TYPE_STRING_FLAG (tmp))
1020 : : {
1021 : 10921 : gcc_assert (expr->ts.type == BT_CHARACTER);
1022 : :
1023 : 10921 : tmp = gfc_get_character_len_in_bytes (tmp);
1024 : :
1025 : 10921 : if (tmp == NULL_TREE || integer_zerop (tmp))
1026 : : {
1027 : 80 : tree bs;
1028 : :
1029 : 80 : tmp = gfc_get_expr_charlen (expr);
1030 : 80 : tmp = fold_convert (gfc_array_index_type, tmp);
1031 : 80 : bs = build_int_cst (gfc_array_index_type, expr->ts.kind);
1032 : 80 : tmp = fold_build2_loc (input_location, MULT_EXPR,
1033 : : gfc_array_index_type, tmp, bs);
1034 : : }
1035 : :
1036 : 21762 : tmp = (tmp && !integer_zerop (tmp))
1037 : 21762 : ? (fold_convert (gfc_array_index_type, tmp)) : (NULL_TREE);
1038 : : }
1039 : : else
1040 : 44290 : tmp = fold_convert (gfc_array_index_type,
1041 : : size_in_bytes (tmp));
1042 : : }
1043 : 56973 : return tmp;
1044 : : }
1045 : :
1046 : :
1047 : : /* Generate an initializer for a static pointer or allocatable array. */
1048 : :
1049 : : void
1050 : 220 : gfc_trans_static_array_pointer (gfc_symbol * sym)
1051 : : {
1052 : 220 : tree type;
1053 : :
1054 : 220 : gcc_assert (TREE_STATIC (sym->backend_decl));
1055 : : /* Just zero the data member. */
1056 : 220 : type = TREE_TYPE (sym->backend_decl);
1057 : 220 : DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
1058 : 220 : }
1059 : :
1060 : :
1061 : : /* If the bounds of SE's loop have not yet been set, see if they can be
1062 : : determined from array spec AS, which is the array spec of a called
1063 : : function. MAPPING maps the callee's dummy arguments to the values
1064 : : that the caller is passing. Add any initialization and finalization
1065 : : code to SE. */
1066 : :
1067 : : void
1068 : 8532 : gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
1069 : : gfc_se * se, gfc_array_spec * as)
1070 : : {
1071 : 8532 : int n, dim, total_dim;
1072 : 8532 : gfc_se tmpse;
1073 : 8532 : gfc_ss *ss;
1074 : 8532 : tree lower;
1075 : 8532 : tree upper;
1076 : 8532 : tree tmp;
1077 : :
1078 : 8532 : total_dim = 0;
1079 : :
1080 : 8532 : if (!as || as->type != AS_EXPLICIT)
1081 : 7414 : return;
1082 : :
1083 : 2261 : for (ss = se->ss; ss; ss = ss->parent)
1084 : : {
1085 : 1143 : total_dim += ss->loop->dimen;
1086 : 2641 : for (n = 0; n < ss->loop->dimen; n++)
1087 : : {
1088 : : /* The bound is known, nothing to do. */
1089 : 1498 : if (ss->loop->to[n] != NULL_TREE)
1090 : 485 : continue;
1091 : :
1092 : 1013 : dim = ss->dim[n];
1093 : 1013 : gcc_assert (dim < as->rank);
1094 : 1013 : gcc_assert (ss->loop->dimen <= as->rank);
1095 : :
1096 : : /* Evaluate the lower bound. */
1097 : 1013 : gfc_init_se (&tmpse, NULL);
1098 : 1013 : gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
1099 : 1013 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
1100 : 1013 : gfc_add_block_to_block (&se->post, &tmpse.post);
1101 : 1013 : lower = fold_convert (gfc_array_index_type, tmpse.expr);
1102 : :
1103 : : /* ...and the upper bound. */
1104 : 1013 : gfc_init_se (&tmpse, NULL);
1105 : 1013 : gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
1106 : 1013 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
1107 : 1013 : gfc_add_block_to_block (&se->post, &tmpse.post);
1108 : 1013 : upper = fold_convert (gfc_array_index_type, tmpse.expr);
1109 : :
1110 : : /* Set the upper bound of the loop to UPPER - LOWER. */
1111 : 1013 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
1112 : : gfc_array_index_type, upper, lower);
1113 : 1013 : tmp = gfc_evaluate_now (tmp, &se->pre);
1114 : 1013 : ss->loop->to[n] = tmp;
1115 : : }
1116 : : }
1117 : :
1118 : 1118 : gcc_assert (total_dim == as->rank);
1119 : : }
1120 : :
1121 : :
1122 : : /* Generate code to allocate an array temporary, or create a variable to
1123 : : hold the data. If size is NULL, zero the descriptor so that the
1124 : : callee will allocate the array. If DEALLOC is true, also generate code to
1125 : : free the array afterwards.
1126 : :
1127 : : If INITIAL is not NULL, it is packed using internal_pack and the result used
1128 : : as data instead of allocating a fresh, unitialized area of memory.
1129 : :
1130 : : Initialization code is added to PRE and finalization code to POST.
1131 : : DYNAMIC is true if the caller may want to extend the array later
1132 : : using realloc. This prevents us from putting the array on the stack. */
1133 : :
1134 : : static void
1135 : 27947 : gfc_trans_allocate_array_storage (stmtblock_t * pre, stmtblock_t * post,
1136 : : gfc_array_info * info, tree size, tree nelem,
1137 : : tree initial, bool dynamic, bool dealloc)
1138 : : {
1139 : 27947 : tree tmp;
1140 : 27947 : tree desc;
1141 : 27947 : bool onstack;
1142 : :
1143 : 27947 : desc = info->descriptor;
1144 : 27947 : info->offset = gfc_index_zero_node;
1145 : 27947 : if (size == NULL_TREE || (dynamic && integer_zerop (size)))
1146 : : {
1147 : : /* A callee allocated array. */
1148 : 2694 : gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
1149 : 2694 : onstack = false;
1150 : : }
1151 : : else
1152 : : {
1153 : : /* Allocate the temporary. */
1154 : 50506 : onstack = !dynamic && initial == NULL_TREE
1155 : 25253 : && (flag_stack_arrays
1156 : 24920 : || gfc_can_put_var_on_stack (size));
1157 : :
1158 : 25253 : if (onstack)
1159 : : {
1160 : : /* Make a temporary variable to hold the data. */
1161 : 20483 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (nelem),
1162 : : nelem, gfc_index_one_node);
1163 : 20483 : tmp = gfc_evaluate_now (tmp, pre);
1164 : 20483 : tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1165 : : tmp);
1166 : 20483 : tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
1167 : : tmp);
1168 : 20483 : tmp = gfc_create_var (tmp, "A");
1169 : : /* If we're here only because of -fstack-arrays we have to
1170 : : emit a DECL_EXPR to make the gimplifier emit alloca calls. */
1171 : 20483 : if (!gfc_can_put_var_on_stack (size))
1172 : 17 : gfc_add_expr_to_block (pre,
1173 : : fold_build1_loc (input_location,
1174 : 17 : DECL_EXPR, TREE_TYPE (tmp),
1175 : : tmp));
1176 : 20483 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1177 : 20483 : gfc_conv_descriptor_data_set (pre, desc, tmp);
1178 : : }
1179 : : else
1180 : : {
1181 : : /* Allocate memory to hold the data or call internal_pack. */
1182 : 4770 : if (initial == NULL_TREE)
1183 : : {
1184 : 4669 : tmp = gfc_call_malloc (pre, NULL, size);
1185 : 4669 : tmp = gfc_evaluate_now (tmp, pre);
1186 : : }
1187 : : else
1188 : : {
1189 : 101 : tree packed;
1190 : 101 : tree source_data;
1191 : 101 : tree was_packed;
1192 : 101 : stmtblock_t do_copying;
1193 : :
1194 : 101 : tmp = TREE_TYPE (initial); /* Pointer to descriptor. */
1195 : 101 : gcc_assert (TREE_CODE (tmp) == POINTER_TYPE);
1196 : 101 : tmp = TREE_TYPE (tmp); /* The descriptor itself. */
1197 : 101 : tmp = gfc_get_element_type (tmp);
1198 : 101 : packed = gfc_create_var (build_pointer_type (tmp), "data");
1199 : :
1200 : 101 : tmp = build_call_expr_loc (input_location,
1201 : : gfor_fndecl_in_pack, 1, initial);
1202 : 101 : tmp = fold_convert (TREE_TYPE (packed), tmp);
1203 : 101 : gfc_add_modify (pre, packed, tmp);
1204 : :
1205 : 101 : tmp = build_fold_indirect_ref_loc (input_location,
1206 : : initial);
1207 : 101 : source_data = gfc_conv_descriptor_data_get (tmp);
1208 : :
1209 : : /* internal_pack may return source->data without any allocation
1210 : : or copying if it is already packed. If that's the case, we
1211 : : need to allocate and copy manually. */
1212 : :
1213 : 101 : gfc_start_block (&do_copying);
1214 : 101 : tmp = gfc_call_malloc (&do_copying, NULL, size);
1215 : 101 : tmp = fold_convert (TREE_TYPE (packed), tmp);
1216 : 101 : gfc_add_modify (&do_copying, packed, tmp);
1217 : 101 : tmp = gfc_build_memcpy_call (packed, source_data, size);
1218 : 101 : gfc_add_expr_to_block (&do_copying, tmp);
1219 : :
1220 : 101 : was_packed = fold_build2_loc (input_location, EQ_EXPR,
1221 : : logical_type_node, packed,
1222 : : source_data);
1223 : 101 : tmp = gfc_finish_block (&do_copying);
1224 : 101 : tmp = build3_v (COND_EXPR, was_packed, tmp,
1225 : : build_empty_stmt (input_location));
1226 : 101 : gfc_add_expr_to_block (pre, tmp);
1227 : :
1228 : 101 : tmp = fold_convert (pvoid_type_node, packed);
1229 : : }
1230 : :
1231 : 4770 : gfc_conv_descriptor_data_set (pre, desc, tmp);
1232 : : }
1233 : : }
1234 : 27947 : info->data = gfc_conv_descriptor_data_get (desc);
1235 : :
1236 : : /* The offset is zero because we create temporaries with a zero
1237 : : lower bound. */
1238 : 27947 : gfc_conv_descriptor_offset_set (pre, desc, gfc_index_zero_node);
1239 : :
1240 : 27947 : if (dealloc && !onstack)
1241 : : {
1242 : : /* Free the temporary. */
1243 : 7224 : tmp = gfc_conv_descriptor_data_get (desc);
1244 : 7224 : tmp = gfc_call_free (tmp);
1245 : 7224 : gfc_add_expr_to_block (post, tmp);
1246 : : }
1247 : 27947 : }
1248 : :
1249 : :
1250 : : /* Get the scalarizer array dimension corresponding to actual array dimension
1251 : : given by ARRAY_DIM.
1252 : :
1253 : : For example, if SS represents the array ref a(1,:,:,1), it is a
1254 : : bidimensional scalarizer array, and the result would be 0 for ARRAY_DIM=1,
1255 : : and 1 for ARRAY_DIM=2.
1256 : : If SS represents transpose(a(:,1,1,:)), it is again a bidimensional
1257 : : scalarizer array, and the result would be 1 for ARRAY_DIM=0 and 0 for
1258 : : ARRAY_DIM=3.
1259 : : If SS represents sum(a(:,:,:,1), dim=1), it is a 2+1-dimensional scalarizer
1260 : : array. If called on the inner ss, the result would be respectively 0,1,2 for
1261 : : ARRAY_DIM=0,1,2. If called on the outer ss, the result would be 0,1
1262 : : for ARRAY_DIM=1,2. */
1263 : :
1264 : : static int
1265 : 256440 : get_scalarizer_dim_for_array_dim (gfc_ss *ss, int array_dim)
1266 : : {
1267 : 256440 : int array_ref_dim;
1268 : 256440 : int n;
1269 : :
1270 : 256440 : array_ref_dim = 0;
1271 : :
1272 : 519013 : for (; ss; ss = ss->parent)
1273 : 678210 : for (n = 0; n < ss->dimen; n++)
1274 : 415637 : if (ss->dim[n] < array_dim)
1275 : 76949 : array_ref_dim++;
1276 : :
1277 : 256440 : return array_ref_dim;
1278 : : }
1279 : :
1280 : :
1281 : : static gfc_ss *
1282 : 216102 : innermost_ss (gfc_ss *ss)
1283 : : {
1284 : 396127 : while (ss->nested_ss != NULL)
1285 : : ss = ss->nested_ss;
1286 : :
1287 : 387919 : return ss;
1288 : : }
1289 : :
1290 : :
1291 : :
1292 : : /* Get the array reference dimension corresponding to the given loop dimension.
1293 : : It is different from the true array dimension given by the dim array in
1294 : : the case of a partial array reference (i.e. a(:,:,1,:) for example)
1295 : : It is different from the loop dimension in the case of a transposed array.
1296 : : */
1297 : :
1298 : : static int
1299 : 216102 : get_array_ref_dim_for_loop_dim (gfc_ss *ss, int loop_dim)
1300 : : {
1301 : 216102 : return get_scalarizer_dim_for_array_dim (innermost_ss (ss),
1302 : 216102 : ss->dim[loop_dim]);
1303 : : }
1304 : :
1305 : :
1306 : : /* Use the information in the ss to obtain the required information about
1307 : : the type and size of an array temporary, when the lhs in an assignment
1308 : : is a class expression. */
1309 : :
1310 : : static tree
1311 : 309 : get_class_info_from_ss (stmtblock_t * pre, gfc_ss *ss, tree *eltype,
1312 : : gfc_ss **fcnss)
1313 : : {
1314 : 309 : gfc_ss *loop_ss = ss->loop->ss;
1315 : 309 : gfc_ss *lhs_ss;
1316 : 309 : gfc_ss *rhs_ss;
1317 : 309 : gfc_ss *fcn_ss = NULL;
1318 : 309 : tree tmp;
1319 : 309 : tree tmp2;
1320 : 309 : tree vptr;
1321 : 309 : tree class_expr = NULL_TREE;
1322 : 309 : tree lhs_class_expr = NULL_TREE;
1323 : 309 : bool unlimited_rhs = false;
1324 : 309 : bool unlimited_lhs = false;
1325 : 309 : bool rhs_function = false;
1326 : 309 : bool unlimited_arg1 = false;
1327 : 309 : gfc_symbol *vtab;
1328 : 309 : tree cntnr = NULL_TREE;
1329 : :
1330 : : /* The second element in the loop chain contains the source for the
1331 : : class temporary created in gfc_trans_create_temp_array. */
1332 : 309 : rhs_ss = loop_ss->loop_chain;
1333 : :
1334 : 309 : if (rhs_ss != gfc_ss_terminator
1335 : 285 : && rhs_ss->info
1336 : 285 : && rhs_ss->info->expr
1337 : 285 : && rhs_ss->info->expr->ts.type == BT_CLASS
1338 : 170 : && rhs_ss->info->data.array.descriptor)
1339 : : {
1340 : 158 : if (rhs_ss->info->expr->expr_type != EXPR_VARIABLE)
1341 : 56 : class_expr
1342 : 56 : = gfc_get_class_from_expr (rhs_ss->info->data.array.descriptor);
1343 : : else
1344 : 102 : class_expr = gfc_get_class_from_gfc_expr (rhs_ss->info->expr);
1345 : 158 : unlimited_rhs = UNLIMITED_POLY (rhs_ss->info->expr);
1346 : 158 : if (rhs_ss->info->expr->expr_type == EXPR_FUNCTION)
1347 : : rhs_function = true;
1348 : : }
1349 : :
1350 : : /* Usually, ss points to the function. When the function call is an actual
1351 : : argument, it is instead rhs_ss because the ss chain is shifted by one. */
1352 : 309 : *fcnss = fcn_ss = rhs_function ? rhs_ss : ss;
1353 : :
1354 : : /* If this is a transformational function with a class result, the info
1355 : : class_container field points to the class container of arg1. */
1356 : 309 : if (class_expr != NULL_TREE
1357 : 139 : && fcn_ss->info && fcn_ss->info->expr
1358 : 91 : && fcn_ss->info->expr->expr_type == EXPR_FUNCTION
1359 : 91 : && fcn_ss->info->expr->value.function.isym
1360 : 60 : && fcn_ss->info->expr->value.function.isym->transformational)
1361 : : {
1362 : 60 : cntnr = ss->info->class_container;
1363 : 60 : unlimited_arg1
1364 : 60 : = UNLIMITED_POLY (fcn_ss->info->expr->value.function.actual->expr);
1365 : : }
1366 : :
1367 : : /* For an assignment the lhs is the next element in the loop chain.
1368 : : If we have a class rhs, this had better be a class variable
1369 : : expression! Otherwise, the class container from arg1 can be used
1370 : : to set the vptr and len fields of the result class container. */
1371 : 309 : lhs_ss = rhs_ss->loop_chain;
1372 : 309 : if (lhs_ss && lhs_ss != gfc_ss_terminator
1373 : 219 : && lhs_ss->info && lhs_ss->info->expr
1374 : 219 : && lhs_ss->info->expr->expr_type ==EXPR_VARIABLE
1375 : 219 : && lhs_ss->info->expr->ts.type == BT_CLASS)
1376 : : {
1377 : 219 : tmp = lhs_ss->info->data.array.descriptor;
1378 : 219 : unlimited_lhs = UNLIMITED_POLY (rhs_ss->info->expr);
1379 : : }
1380 : 90 : else if (cntnr != NULL_TREE)
1381 : : {
1382 : 54 : tmp = gfc_class_vptr_get (class_expr);
1383 : 54 : gfc_add_modify (pre, tmp, fold_convert (TREE_TYPE (tmp),
1384 : : gfc_class_vptr_get (cntnr)));
1385 : 54 : if (unlimited_rhs)
1386 : : {
1387 : 6 : tmp = gfc_class_len_get (class_expr);
1388 : 6 : if (unlimited_arg1)
1389 : 6 : gfc_add_modify (pre, tmp, gfc_class_len_get (cntnr));
1390 : : }
1391 : : tmp = NULL_TREE;
1392 : : }
1393 : : else
1394 : : tmp = NULL_TREE;
1395 : :
1396 : : /* Get the lhs class expression. */
1397 : 219 : if (tmp != NULL_TREE && lhs_ss->loop_chain == gfc_ss_terminator)
1398 : 207 : lhs_class_expr = gfc_get_class_from_expr (tmp);
1399 : : else
1400 : 102 : return class_expr;
1401 : :
1402 : 207 : gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (lhs_class_expr)));
1403 : :
1404 : : /* Set the lhs vptr and, if necessary, the _len field. */
1405 : 207 : if (class_expr)
1406 : : {
1407 : : /* Both lhs and rhs are class expressions. */
1408 : 79 : tmp = gfc_class_vptr_get (lhs_class_expr);
1409 : 158 : gfc_add_modify (pre, tmp,
1410 : 79 : fold_convert (TREE_TYPE (tmp),
1411 : : gfc_class_vptr_get (class_expr)));
1412 : 79 : if (unlimited_lhs)
1413 : : {
1414 : 31 : gcc_assert (unlimited_rhs);
1415 : 31 : tmp = gfc_class_len_get (lhs_class_expr);
1416 : 31 : tmp2 = gfc_class_len_get (class_expr);
1417 : 31 : gfc_add_modify (pre, tmp, tmp2);
1418 : : }
1419 : : }
1420 : 128 : else if (rhs_ss->info->data.array.descriptor)
1421 : : {
1422 : : /* lhs is class and rhs is intrinsic or derived type. */
1423 : 122 : *eltype = TREE_TYPE (rhs_ss->info->data.array.descriptor);
1424 : 122 : *eltype = gfc_get_element_type (*eltype);
1425 : 122 : vtab = gfc_find_vtab (&rhs_ss->info->expr->ts);
1426 : 122 : vptr = vtab->backend_decl;
1427 : 122 : if (vptr == NULL_TREE)
1428 : 24 : vptr = gfc_get_symbol_decl (vtab);
1429 : 122 : vptr = gfc_build_addr_expr (NULL_TREE, vptr);
1430 : 122 : tmp = gfc_class_vptr_get (lhs_class_expr);
1431 : 122 : gfc_add_modify (pre, tmp,
1432 : 122 : fold_convert (TREE_TYPE (tmp), vptr));
1433 : :
1434 : 122 : if (unlimited_lhs)
1435 : : {
1436 : 0 : tmp = gfc_class_len_get (lhs_class_expr);
1437 : 0 : if (rhs_ss->info
1438 : 0 : && rhs_ss->info->expr
1439 : 0 : && rhs_ss->info->expr->ts.type == BT_CHARACTER)
1440 : 0 : tmp2 = build_int_cst (TREE_TYPE (tmp),
1441 : 0 : rhs_ss->info->expr->ts.kind);
1442 : : else
1443 : 0 : tmp2 = build_int_cst (TREE_TYPE (tmp), 0);
1444 : 0 : gfc_add_modify (pre, tmp, tmp2);
1445 : : }
1446 : : }
1447 : :
1448 : : return class_expr;
1449 : : }
1450 : :
1451 : :
1452 : :
1453 : : /* Generate code to create and initialize the descriptor for a temporary
1454 : : array. This is used for both temporaries needed by the scalarizer, and
1455 : : functions returning arrays. Adjusts the loop variables to be
1456 : : zero-based, and calculates the loop bounds for callee allocated arrays.
1457 : : Allocate the array unless it's callee allocated (we have a callee
1458 : : allocated array if 'callee_alloc' is true, or if loop->to[n] is
1459 : : NULL_TREE for any n). Also fills in the descriptor, data and offset
1460 : : fields of info if known. Returns the size of the array, or NULL for a
1461 : : callee allocated array.
1462 : :
1463 : : 'eltype' == NULL signals that the temporary should be a class object.
1464 : : The 'initial' expression is used to obtain the size of the dynamic
1465 : : type; otherwise the allocation and initialization proceeds as for any
1466 : : other expression
1467 : :
1468 : : PRE, POST, INITIAL, DYNAMIC and DEALLOC are as for
1469 : : gfc_trans_allocate_array_storage. */
1470 : :
1471 : : tree
1472 : 27947 : gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post, gfc_ss * ss,
1473 : : tree eltype, tree initial, bool dynamic,
1474 : : bool dealloc, bool callee_alloc, locus * where)
1475 : : {
1476 : 27947 : gfc_loopinfo *loop;
1477 : 27947 : gfc_ss *s;
1478 : 27947 : gfc_array_info *info;
1479 : 27947 : tree from[GFC_MAX_DIMENSIONS], to[GFC_MAX_DIMENSIONS];
1480 : 27947 : tree type;
1481 : 27947 : tree desc;
1482 : 27947 : tree tmp;
1483 : 27947 : tree size;
1484 : 27947 : tree nelem;
1485 : 27947 : tree cond;
1486 : 27947 : tree or_expr;
1487 : 27947 : tree elemsize;
1488 : 27947 : tree class_expr = NULL_TREE;
1489 : 27947 : gfc_ss *fcn_ss = NULL;
1490 : 27947 : int n, dim, tmp_dim;
1491 : 27947 : int total_dim = 0;
1492 : :
1493 : : /* This signals a class array for which we need the size of the
1494 : : dynamic type. Generate an eltype and then the class expression. */
1495 : 27947 : if (eltype == NULL_TREE && initial)
1496 : : {
1497 : 6 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (initial)));
1498 : 6 : class_expr = build_fold_indirect_ref_loc (input_location, initial);
1499 : : /* Obtain the structure (class) expression. */
1500 : 6 : class_expr = gfc_get_class_from_expr (class_expr);
1501 : 6 : gcc_assert (class_expr);
1502 : : }
1503 : :
1504 : : /* Otherwise, some expressions, such as class functions, arising from
1505 : : dependency checking in assignments come here with class element type.
1506 : : The descriptor can be obtained from the ss->info and then converted
1507 : : to the class object. */
1508 : 27941 : if (class_expr == NULL_TREE && GFC_CLASS_TYPE_P (eltype))
1509 : 309 : class_expr = get_class_info_from_ss (pre, ss, &eltype, &fcn_ss);
1510 : :
1511 : : /* If the dynamic type is not available, use the declared type. */
1512 : 27947 : if (eltype && GFC_CLASS_TYPE_P (eltype))
1513 : 187 : eltype = gfc_get_element_type (TREE_TYPE (TYPE_FIELDS (eltype)));
1514 : :
1515 : 27947 : if (class_expr == NULL_TREE)
1516 : 27802 : elemsize = fold_convert (gfc_array_index_type,
1517 : : TYPE_SIZE_UNIT (eltype));
1518 : : else
1519 : : {
1520 : : /* Unlimited polymorphic entities are initialised with NULL vptr. They
1521 : : can be tested for by checking if the len field is present. If so
1522 : : test the vptr before using the vtable size. */
1523 : 145 : tmp = gfc_class_vptr_get (class_expr);
1524 : 145 : tmp = fold_build2_loc (input_location, NE_EXPR,
1525 : : logical_type_node,
1526 : 145 : tmp, build_int_cst (TREE_TYPE (tmp), 0));
1527 : 145 : elemsize = fold_build3_loc (input_location, COND_EXPR,
1528 : : gfc_array_index_type,
1529 : : tmp,
1530 : : gfc_class_vtab_size_get (class_expr),
1531 : : gfc_index_zero_node);
1532 : 145 : elemsize = gfc_evaluate_now (elemsize, pre);
1533 : 145 : elemsize = gfc_resize_class_size_with_len (pre, class_expr, elemsize);
1534 : : /* Casting the data as a character of the dynamic length ensures that
1535 : : assignment of elements works when needed. */
1536 : 145 : eltype = gfc_get_character_type_len (1, elemsize);
1537 : : }
1538 : :
1539 : 27947 : memset (from, 0, sizeof (from));
1540 : 27947 : memset (to, 0, sizeof (to));
1541 : :
1542 : 27947 : info = &ss->info->data.array;
1543 : :
1544 : 27947 : gcc_assert (ss->dimen > 0);
1545 : 27947 : gcc_assert (ss->loop->dimen == ss->dimen);
1546 : :
1547 : 27947 : if (warn_array_temporaries && where)
1548 : 205 : gfc_warning (OPT_Warray_temporaries,
1549 : : "Creating array temporary at %L", where);
1550 : :
1551 : : /* Set the lower bound to zero. */
1552 : 55929 : for (s = ss; s; s = s->parent)
1553 : : {
1554 : 27982 : loop = s->loop;
1555 : :
1556 : 27982 : total_dim += loop->dimen;
1557 : 65074 : for (n = 0; n < loop->dimen; n++)
1558 : : {
1559 : 37092 : dim = s->dim[n];
1560 : :
1561 : : /* Callee allocated arrays may not have a known bound yet. */
1562 : 37092 : if (loop->to[n])
1563 : 33851 : loop->to[n] = gfc_evaluate_now (
1564 : : fold_build2_loc (input_location, MINUS_EXPR,
1565 : : gfc_array_index_type,
1566 : : loop->to[n], loop->from[n]),
1567 : : pre);
1568 : 37092 : loop->from[n] = gfc_index_zero_node;
1569 : :
1570 : : /* We have just changed the loop bounds, we must clear the
1571 : : corresponding specloop, so that delta calculation is not skipped
1572 : : later in gfc_set_delta. */
1573 : 37092 : loop->specloop[n] = NULL;
1574 : :
1575 : : /* We are constructing the temporary's descriptor based on the loop
1576 : : dimensions. As the dimensions may be accessed in arbitrary order
1577 : : (think of transpose) the size taken from the n'th loop may not map
1578 : : to the n'th dimension of the array. We need to reconstruct loop
1579 : : infos in the right order before using it to set the descriptor
1580 : : bounds. */
1581 : 37092 : tmp_dim = get_scalarizer_dim_for_array_dim (ss, dim);
1582 : 37092 : from[tmp_dim] = loop->from[n];
1583 : 37092 : to[tmp_dim] = loop->to[n];
1584 : :
1585 : 37092 : info->delta[dim] = gfc_index_zero_node;
1586 : 37092 : info->start[dim] = gfc_index_zero_node;
1587 : 37092 : info->end[dim] = gfc_index_zero_node;
1588 : 37092 : info->stride[dim] = gfc_index_one_node;
1589 : : }
1590 : : }
1591 : :
1592 : : /* Initialize the descriptor. */
1593 : 27947 : type =
1594 : 27947 : gfc_get_array_type_bounds (eltype, total_dim, 0, from, to, 1,
1595 : : GFC_ARRAY_UNKNOWN, true);
1596 : 27947 : desc = gfc_create_var (type, "atmp");
1597 : 27947 : GFC_DECL_PACKED_ARRAY (desc) = 1;
1598 : :
1599 : : /* Emit a DECL_EXPR for the variable sized array type in
1600 : : GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
1601 : : sizes works correctly. */
1602 : 27947 : tree arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (type));
1603 : 27947 : if (! TYPE_NAME (arraytype))
1604 : 27947 : TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
1605 : : NULL_TREE, arraytype);
1606 : 27947 : gfc_add_expr_to_block (pre, build1 (DECL_EXPR,
1607 : 27947 : arraytype, TYPE_NAME (arraytype)));
1608 : :
1609 : 27947 : if (fcn_ss && fcn_ss->info && fcn_ss->info->class_container)
1610 : : {
1611 : 90 : suppress_warning (desc);
1612 : 90 : TREE_USED (desc) = 0;
1613 : : }
1614 : :
1615 : 27947 : if (class_expr != NULL_TREE
1616 : 27802 : || (fcn_ss && fcn_ss->info && fcn_ss->info->class_container))
1617 : : {
1618 : 175 : tree class_data;
1619 : 175 : tree dtype;
1620 : 175 : gfc_expr *expr1 = fcn_ss ? fcn_ss->info->expr : NULL;
1621 : 169 : bool rank_changer;
1622 : :
1623 : : /* Pick out these transformational functions because they change the rank
1624 : : or shape of the first argument. This requires that the class type be
1625 : : changed, the dtype updated and the correct rank used. */
1626 : 121 : rank_changer = expr1 && expr1->expr_type == EXPR_FUNCTION
1627 : 121 : && expr1->value.function.isym
1628 : 259 : && (expr1->value.function.isym->id == GFC_ISYM_RESHAPE
1629 : : || expr1->value.function.isym->id == GFC_ISYM_SPREAD
1630 : : || expr1->value.function.isym->id == GFC_ISYM_PACK
1631 : : || expr1->value.function.isym->id == GFC_ISYM_UNPACK);
1632 : :
1633 : : /* Create a class temporary for the result using the lhs class object. */
1634 : 175 : if (class_expr != NULL_TREE && !rank_changer)
1635 : : {
1636 : 97 : tmp = gfc_create_var (TREE_TYPE (class_expr), "ctmp");
1637 : 97 : gfc_add_modify (pre, tmp, class_expr);
1638 : : }
1639 : : else
1640 : : {
1641 : 78 : tree vptr;
1642 : 78 : class_expr = fcn_ss->info->class_container;
1643 : 78 : gcc_assert (expr1);
1644 : :
1645 : : /* Build a new class container using the arg1 class object. The class
1646 : : typespec must be rebuilt because the rank might have changed. */
1647 : 78 : gfc_typespec ts = CLASS_DATA (expr1)->ts;
1648 : 78 : symbol_attribute attr = CLASS_DATA (expr1)->attr;
1649 : 78 : gfc_change_class (&ts, &attr, NULL, expr1->rank, 0);
1650 : 78 : tmp = gfc_create_var (gfc_typenode_for_spec (&ts), "ctmp");
1651 : 78 : fcn_ss->info->class_container = tmp;
1652 : :
1653 : : /* Set the vptr and obtain the element size. */
1654 : 78 : vptr = gfc_class_vptr_get (tmp);
1655 : 156 : gfc_add_modify (pre, vptr,
1656 : 78 : fold_convert (TREE_TYPE (vptr),
1657 : : gfc_class_vptr_get (class_expr)));
1658 : 78 : elemsize = gfc_class_vtab_size_get (class_expr);
1659 : :
1660 : : /* Set the _len field, if necessary. */
1661 : 78 : if (UNLIMITED_POLY (expr1))
1662 : : {
1663 : 18 : gfc_add_modify (pre, gfc_class_len_get (tmp),
1664 : : gfc_class_len_get (class_expr));
1665 : 18 : elemsize = gfc_resize_class_size_with_len (pre, class_expr,
1666 : : elemsize);
1667 : : }
1668 : :
1669 : 78 : elemsize = gfc_evaluate_now (elemsize, pre);
1670 : : }
1671 : :
1672 : 175 : class_data = gfc_class_data_get (tmp);
1673 : :
1674 : 175 : if (rank_changer)
1675 : : {
1676 : : /* Take the dtype from the class expression. */
1677 : 72 : dtype = gfc_conv_descriptor_dtype (gfc_class_data_get (class_expr));
1678 : 72 : tmp = gfc_conv_descriptor_dtype (desc);
1679 : 72 : gfc_add_modify (pre, tmp, dtype);
1680 : :
1681 : : /* These transformational functions change the rank. */
1682 : 72 : tmp = gfc_conv_descriptor_rank (desc);
1683 : 72 : gfc_add_modify (pre, tmp,
1684 : 72 : build_int_cst (TREE_TYPE (tmp), ss->loop->dimen));
1685 : 72 : fcn_ss->info->class_container = NULL_TREE;
1686 : : }
1687 : :
1688 : : /* Assign the new descriptor to the _data field. This allows the
1689 : : vptr _copy to be used for scalarized assignment since the class
1690 : : temporary can be found from the descriptor. */
1691 : 175 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1692 : 175 : TREE_TYPE (desc), desc);
1693 : 175 : gfc_add_modify (pre, class_data, tmp);
1694 : :
1695 : : /* Point desc to the class _data field. */
1696 : 175 : desc = class_data;
1697 : 175 : }
1698 : : else
1699 : : {
1700 : : /* Fill in the array dtype. */
1701 : 27772 : tmp = gfc_conv_descriptor_dtype (desc);
1702 : 27772 : gfc_add_modify (pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
1703 : : }
1704 : :
1705 : 27947 : info->descriptor = desc;
1706 : 27947 : size = gfc_index_one_node;
1707 : :
1708 : : /*
1709 : : Fill in the bounds and stride. This is a packed array, so:
1710 : :
1711 : : size = 1;
1712 : : for (n = 0; n < rank; n++)
1713 : : {
1714 : : stride[n] = size
1715 : : delta = ubound[n] + 1 - lbound[n];
1716 : : size = size * delta;
1717 : : }
1718 : : size = size * sizeof(element);
1719 : : */
1720 : :
1721 : 27947 : or_expr = NULL_TREE;
1722 : :
1723 : : /* If there is at least one null loop->to[n], it is a callee allocated
1724 : : array. */
1725 : 61798 : for (n = 0; n < total_dim; n++)
1726 : 35748 : if (to[n] == NULL_TREE)
1727 : : {
1728 : : size = NULL_TREE;
1729 : : break;
1730 : : }
1731 : :
1732 : 27947 : if (size == NULL_TREE)
1733 : 3804 : for (s = ss; s; s = s->parent)
1734 : 5153 : for (n = 0; n < s->loop->dimen; n++)
1735 : : {
1736 : 3246 : dim = get_scalarizer_dim_for_array_dim (ss, s->dim[n]);
1737 : :
1738 : : /* For a callee allocated array express the loop bounds in terms
1739 : : of the descriptor fields. */
1740 : 3246 : tmp = fold_build2_loc (input_location,
1741 : : MINUS_EXPR, gfc_array_index_type,
1742 : : gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]),
1743 : : gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]));
1744 : 3246 : s->loop->to[n] = tmp;
1745 : : }
1746 : : else
1747 : : {
1748 : 59896 : for (n = 0; n < total_dim; n++)
1749 : : {
1750 : : /* Store the stride and bound components in the descriptor. */
1751 : 33846 : gfc_conv_descriptor_stride_set (pre, desc, gfc_rank_cst[n], size);
1752 : :
1753 : 33846 : gfc_conv_descriptor_lbound_set (pre, desc, gfc_rank_cst[n],
1754 : : gfc_index_zero_node);
1755 : :
1756 : 33846 : gfc_conv_descriptor_ubound_set (pre, desc, gfc_rank_cst[n], to[n]);
1757 : :
1758 : 33846 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
1759 : : gfc_array_index_type,
1760 : : to[n], gfc_index_one_node);
1761 : :
1762 : : /* Check whether the size for this dimension is negative. */
1763 : 33846 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
1764 : : tmp, gfc_index_zero_node);
1765 : 33846 : cond = gfc_evaluate_now (cond, pre);
1766 : :
1767 : 33846 : if (n == 0)
1768 : : or_expr = cond;
1769 : : else
1770 : 7796 : or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
1771 : : logical_type_node, or_expr, cond);
1772 : :
1773 : 33846 : size = fold_build2_loc (input_location, MULT_EXPR,
1774 : : gfc_array_index_type, size, tmp);
1775 : 33846 : size = gfc_evaluate_now (size, pre);
1776 : : }
1777 : : }
1778 : :
1779 : : /* Get the size of the array. */
1780 : 27947 : if (size && !callee_alloc)
1781 : : {
1782 : : /* If or_expr is true, then the extent in at least one
1783 : : dimension is zero and the size is set to zero. */
1784 : 25860 : size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
1785 : : or_expr, gfc_index_zero_node, size);
1786 : :
1787 : 25860 : nelem = size;
1788 : 25860 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1789 : : size, elemsize);
1790 : : }
1791 : : else
1792 : : {
1793 : : nelem = size;
1794 : : size = NULL_TREE;
1795 : : }
1796 : :
1797 : : /* Set the span. */
1798 : 27947 : tmp = fold_convert (gfc_array_index_type, elemsize);
1799 : 27947 : gfc_conv_descriptor_span_set (pre, desc, tmp);
1800 : :
1801 : 27947 : gfc_trans_allocate_array_storage (pre, post, info, size, nelem, initial,
1802 : : dynamic, dealloc);
1803 : :
1804 : 55929 : while (ss->parent)
1805 : : ss = ss->parent;
1806 : :
1807 : 27947 : if (ss->dimen > ss->loop->temp_dim)
1808 : 23858 : ss->loop->temp_dim = ss->dimen;
1809 : :
1810 : 27947 : return size;
1811 : : }
1812 : :
1813 : :
1814 : : /* Return the number of iterations in a loop that starts at START,
1815 : : ends at END, and has step STEP. */
1816 : :
1817 : : static tree
1818 : 1030 : gfc_get_iteration_count (tree start, tree end, tree step)
1819 : : {
1820 : 1030 : tree tmp;
1821 : 1030 : tree type;
1822 : :
1823 : 1030 : type = TREE_TYPE (step);
1824 : 1030 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type, end, start);
1825 : 1030 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, tmp, step);
1826 : 1030 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp,
1827 : : build_int_cst (type, 1));
1828 : 1030 : tmp = fold_build2_loc (input_location, MAX_EXPR, type, tmp,
1829 : : build_int_cst (type, 0));
1830 : 1030 : return fold_convert (gfc_array_index_type, tmp);
1831 : : }
1832 : :
1833 : :
1834 : : /* Extend the data in array DESC by EXTRA elements. */
1835 : :
1836 : : static void
1837 : 1018 : gfc_grow_array (stmtblock_t * pblock, tree desc, tree extra)
1838 : : {
1839 : 1018 : tree arg0, arg1;
1840 : 1018 : tree tmp;
1841 : 1018 : tree size;
1842 : 1018 : tree ubound;
1843 : :
1844 : 1018 : if (integer_zerop (extra))
1845 : : return;
1846 : :
1847 : 988 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[0]);
1848 : :
1849 : : /* Add EXTRA to the upper bound. */
1850 : 988 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1851 : : ubound, extra);
1852 : 988 : gfc_conv_descriptor_ubound_set (pblock, desc, gfc_rank_cst[0], tmp);
1853 : :
1854 : : /* Get the value of the current data pointer. */
1855 : 988 : arg0 = gfc_conv_descriptor_data_get (desc);
1856 : :
1857 : : /* Calculate the new array size. */
1858 : 988 : size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
1859 : 988 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1860 : : ubound, gfc_index_one_node);
1861 : 988 : arg1 = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
1862 : : fold_convert (size_type_node, tmp),
1863 : : fold_convert (size_type_node, size));
1864 : :
1865 : : /* Call the realloc() function. */
1866 : 988 : tmp = gfc_call_realloc (pblock, arg0, arg1);
1867 : 988 : gfc_conv_descriptor_data_set (pblock, desc, tmp);
1868 : : }
1869 : :
1870 : :
1871 : : /* Return true if the bounds of iterator I can only be determined
1872 : : at run time. */
1873 : :
1874 : : static inline bool
1875 : 2144 : gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
1876 : : {
1877 : 2144 : return (i->start->expr_type != EXPR_CONSTANT
1878 : 1726 : || i->end->expr_type != EXPR_CONSTANT
1879 : 2312 : || i->step->expr_type != EXPR_CONSTANT);
1880 : : }
1881 : :
1882 : :
1883 : : /* Split the size of constructor element EXPR into the sum of two terms,
1884 : : one of which can be determined at compile time and one of which must
1885 : : be calculated at run time. Set *SIZE to the former and return true
1886 : : if the latter might be nonzero. */
1887 : :
1888 : : static bool
1889 : 3170 : gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
1890 : : {
1891 : 3170 : if (expr->expr_type == EXPR_ARRAY)
1892 : 642 : return gfc_get_array_constructor_size (size, expr->value.constructor);
1893 : 2528 : else if (expr->rank > 0)
1894 : : {
1895 : : /* Calculate everything at run time. */
1896 : 1013 : mpz_set_ui (*size, 0);
1897 : 1013 : return true;
1898 : : }
1899 : : else
1900 : : {
1901 : : /* A single element. */
1902 : 1515 : mpz_set_ui (*size, 1);
1903 : 1515 : return false;
1904 : : }
1905 : : }
1906 : :
1907 : :
1908 : : /* Like gfc_get_array_constructor_element_size, but applied to the whole
1909 : : of array constructor C. */
1910 : :
1911 : : static bool
1912 : 2780 : gfc_get_array_constructor_size (mpz_t * size, gfc_constructor_base base)
1913 : : {
1914 : 2780 : gfc_constructor *c;
1915 : 2780 : gfc_iterator *i;
1916 : 2780 : mpz_t val;
1917 : 2780 : mpz_t len;
1918 : 2780 : bool dynamic;
1919 : :
1920 : 2780 : mpz_set_ui (*size, 0);
1921 : 2780 : mpz_init (len);
1922 : 2780 : mpz_init (val);
1923 : :
1924 : 2780 : dynamic = false;
1925 : 6902 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1926 : : {
1927 : 4122 : i = c->iterator;
1928 : 4122 : if (i && gfc_iterator_has_dynamic_bounds (i))
1929 : : dynamic = true;
1930 : : else
1931 : : {
1932 : 2658 : dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
1933 : 2658 : if (i)
1934 : : {
1935 : : /* Multiply the static part of the element size by the
1936 : : number of iterations. */
1937 : 124 : mpz_sub (val, i->end->value.integer, i->start->value.integer);
1938 : 124 : mpz_fdiv_q (val, val, i->step->value.integer);
1939 : 124 : mpz_add_ui (val, val, 1);
1940 : 124 : if (mpz_sgn (val) > 0)
1941 : 88 : mpz_mul (len, len, val);
1942 : : else
1943 : 36 : mpz_set_ui (len, 0);
1944 : : }
1945 : 2658 : mpz_add (*size, *size, len);
1946 : : }
1947 : : }
1948 : 2780 : mpz_clear (len);
1949 : 2780 : mpz_clear (val);
1950 : 2780 : return dynamic;
1951 : : }
1952 : :
1953 : :
1954 : : /* Make sure offset is a variable. */
1955 : :
1956 : : static void
1957 : 4746 : gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
1958 : : tree * offsetvar)
1959 : : {
1960 : : /* We should have already created the offset variable. We cannot
1961 : : create it here because we may be in an inner scope. */
1962 : 4746 : gcc_assert (*offsetvar != NULL_TREE);
1963 : 4746 : gfc_add_modify (pblock, *offsetvar, *poffset);
1964 : 4746 : *poffset = *offsetvar;
1965 : 4746 : TREE_USED (*offsetvar) = 1;
1966 : 4746 : }
1967 : :
1968 : :
1969 : : /* Variables needed for bounds-checking. */
1970 : : static bool first_len;
1971 : : static tree first_len_val;
1972 : : static bool typespec_chararray_ctor;
1973 : :
1974 : : static void
1975 : 12695 : gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
1976 : : tree offset, gfc_se * se, gfc_expr * expr)
1977 : : {
1978 : 12695 : tree tmp, offset_eval;
1979 : :
1980 : 12695 : gfc_conv_expr (se, expr);
1981 : :
1982 : : /* Store the value. */
1983 : 12695 : tmp = build_fold_indirect_ref_loc (input_location,
1984 : : gfc_conv_descriptor_data_get (desc));
1985 : : /* The offset may change, so get its value now and use that to free memory.
1986 : : */
1987 : 12695 : offset_eval = gfc_evaluate_now (offset, &se->pre);
1988 : 12695 : tmp = gfc_build_array_ref (tmp, offset_eval, NULL);
1989 : :
1990 : 12695 : if (expr->expr_type == EXPR_FUNCTION && expr->ts.type == BT_DERIVED
1991 : 53 : && expr->ts.u.derived->attr.alloc_comp)
1992 : 21 : gfc_add_expr_to_block (&se->finalblock,
1993 : : gfc_deallocate_alloc_comp_no_caf (expr->ts.u.derived,
1994 : : tmp, expr->rank,
1995 : : true));
1996 : :
1997 : 12695 : if (expr->ts.type == BT_CHARACTER)
1998 : : {
1999 : 2116 : int i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
2000 : 2116 : tree esize;
2001 : :
2002 : 2116 : esize = size_in_bytes (gfc_get_element_type (TREE_TYPE (desc)));
2003 : 2116 : esize = fold_convert (gfc_charlen_type_node, esize);
2004 : 4232 : esize = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
2005 : 2116 : TREE_TYPE (esize), esize,
2006 : 2116 : build_int_cst (TREE_TYPE (esize),
2007 : 2116 : gfc_character_kinds[i].bit_size / 8));
2008 : :
2009 : 2116 : gfc_conv_string_parameter (se);
2010 : 2116 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
2011 : : {
2012 : : /* The temporary is an array of pointers. */
2013 : 6 : se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
2014 : 6 : gfc_add_modify (&se->pre, tmp, se->expr);
2015 : : }
2016 : : else
2017 : : {
2018 : : /* The temporary is an array of string values. */
2019 : 2110 : tmp = gfc_build_addr_expr (gfc_get_pchar_type (expr->ts.kind), tmp);
2020 : : /* We know the temporary and the value will be the same length,
2021 : : so can use memcpy. */
2022 : 2110 : gfc_trans_string_copy (&se->pre, esize, tmp, expr->ts.kind,
2023 : : se->string_length, se->expr, expr->ts.kind);
2024 : : }
2025 : 2116 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !typespec_chararray_ctor)
2026 : : {
2027 : 310 : if (first_len)
2028 : : {
2029 : 130 : gfc_add_modify (&se->pre, first_len_val,
2030 : 130 : fold_convert (TREE_TYPE (first_len_val),
2031 : : se->string_length));
2032 : 130 : first_len = false;
2033 : : }
2034 : : else
2035 : : {
2036 : : /* Verify that all constructor elements are of the same
2037 : : length. */
2038 : 180 : tree rhs = fold_convert (TREE_TYPE (first_len_val),
2039 : : se->string_length);
2040 : 180 : tree cond = fold_build2_loc (input_location, NE_EXPR,
2041 : : logical_type_node, first_len_val,
2042 : : rhs);
2043 : 180 : gfc_trans_runtime_check
2044 : 180 : (true, false, cond, &se->pre, &expr->where,
2045 : : "Different CHARACTER lengths (%ld/%ld) in array constructor",
2046 : : fold_convert (long_integer_type_node, first_len_val),
2047 : : fold_convert (long_integer_type_node, se->string_length));
2048 : : }
2049 : : }
2050 : : }
2051 : 10579 : else if (GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
2052 : 10579 : && !GFC_CLASS_TYPE_P (gfc_get_element_type (TREE_TYPE (desc))))
2053 : : {
2054 : : /* Assignment of a CLASS array constructor to a derived type array. */
2055 : 24 : if (expr->expr_type == EXPR_FUNCTION)
2056 : 18 : se->expr = gfc_evaluate_now (se->expr, pblock);
2057 : 24 : se->expr = gfc_class_data_get (se->expr);
2058 : 24 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
2059 : 24 : se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
2060 : 24 : gfc_add_modify (&se->pre, tmp, se->expr);
2061 : : }
2062 : : else
2063 : : {
2064 : : /* TODO: Should the frontend already have done this conversion? */
2065 : 10555 : se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
2066 : 10555 : gfc_add_modify (&se->pre, tmp, se->expr);
2067 : : }
2068 : :
2069 : 12695 : gfc_add_block_to_block (pblock, &se->pre);
2070 : 12695 : gfc_add_block_to_block (pblock, &se->post);
2071 : 12695 : }
2072 : :
2073 : :
2074 : : /* Add the contents of an array to the constructor. DYNAMIC is as for
2075 : : gfc_trans_array_constructor_value. */
2076 : :
2077 : : static void
2078 : 1925 : gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
2079 : : tree type ATTRIBUTE_UNUSED,
2080 : : tree desc, gfc_expr * expr,
2081 : : tree * poffset, tree * offsetvar,
2082 : : bool dynamic)
2083 : : {
2084 : 1925 : gfc_se se;
2085 : 1925 : gfc_ss *ss;
2086 : 1925 : gfc_loopinfo loop;
2087 : 1925 : stmtblock_t body;
2088 : 1925 : tree tmp;
2089 : 1925 : tree size;
2090 : 1925 : int n;
2091 : :
2092 : : /* We need this to be a variable so we can increment it. */
2093 : 1925 : gfc_put_offset_into_var (pblock, poffset, offsetvar);
2094 : :
2095 : 1925 : gfc_init_se (&se, NULL);
2096 : :
2097 : : /* Walk the array expression. */
2098 : 1925 : ss = gfc_walk_expr (expr);
2099 : 1925 : gcc_assert (ss != gfc_ss_terminator);
2100 : :
2101 : : /* Initialize the scalarizer. */
2102 : 1925 : gfc_init_loopinfo (&loop);
2103 : 1925 : gfc_add_ss_to_loop (&loop, ss);
2104 : :
2105 : : /* Initialize the loop. */
2106 : 1925 : gfc_conv_ss_startstride (&loop);
2107 : 1925 : gfc_conv_loop_setup (&loop, &expr->where);
2108 : :
2109 : : /* Make sure the constructed array has room for the new data. */
2110 : 1925 : if (dynamic)
2111 : : {
2112 : : /* Set SIZE to the total number of elements in the subarray. */
2113 : 506 : size = gfc_index_one_node;
2114 : 1024 : for (n = 0; n < loop.dimen; n++)
2115 : : {
2116 : 518 : tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
2117 : : gfc_index_one_node);
2118 : 518 : size = fold_build2_loc (input_location, MULT_EXPR,
2119 : : gfc_array_index_type, size, tmp);
2120 : : }
2121 : :
2122 : : /* Grow the constructed array by SIZE elements. */
2123 : 506 : gfc_grow_array (&loop.pre, desc, size);
2124 : : }
2125 : :
2126 : : /* Make the loop body. */
2127 : 1925 : gfc_mark_ss_chain_used (ss, 1);
2128 : 1925 : gfc_start_scalarized_body (&loop, &body);
2129 : 1925 : gfc_copy_loopinfo_to_se (&se, &loop);
2130 : 1925 : se.ss = ss;
2131 : :
2132 : 1925 : gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
2133 : 1925 : gcc_assert (se.ss == gfc_ss_terminator);
2134 : :
2135 : : /* Increment the offset. */
2136 : 1925 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2137 : : *poffset, gfc_index_one_node);
2138 : 1925 : gfc_add_modify (&body, *poffset, tmp);
2139 : :
2140 : : /* Finish the loop. */
2141 : 1925 : gfc_trans_scalarizing_loops (&loop, &body);
2142 : 1925 : gfc_add_block_to_block (&loop.pre, &loop.post);
2143 : 1925 : tmp = gfc_finish_block (&loop.pre);
2144 : 1925 : gfc_add_expr_to_block (pblock, tmp);
2145 : :
2146 : 1925 : gfc_cleanup_loop (&loop);
2147 : 1925 : }
2148 : :
2149 : :
2150 : : /* Assign the values to the elements of an array constructor. DYNAMIC
2151 : : is true if descriptor DESC only contains enough data for the static
2152 : : size calculated by gfc_get_array_constructor_size. When true, memory
2153 : : for the dynamic parts must be allocated using realloc. */
2154 : :
2155 : : static void
2156 : 8559 : gfc_trans_array_constructor_value (stmtblock_t * pblock,
2157 : : stmtblock_t * finalblock,
2158 : : tree type, tree desc,
2159 : : gfc_constructor_base base, tree * poffset,
2160 : : tree * offsetvar, bool dynamic)
2161 : : {
2162 : 8559 : tree tmp;
2163 : 8559 : tree start = NULL_TREE;
2164 : 8559 : tree end = NULL_TREE;
2165 : 8559 : tree step = NULL_TREE;
2166 : 8559 : stmtblock_t body;
2167 : 8559 : gfc_se se;
2168 : 8559 : mpz_t size;
2169 : 8559 : gfc_constructor *c;
2170 : 8559 : gfc_typespec ts;
2171 : 8559 : int ctr = 0;
2172 : :
2173 : 8559 : tree shadow_loopvar = NULL_TREE;
2174 : 8559 : gfc_saved_var saved_loopvar;
2175 : :
2176 : 8559 : ts.type = BT_UNKNOWN;
2177 : 8559 : mpz_init (size);
2178 : 22676 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
2179 : : {
2180 : 14117 : ctr++;
2181 : : /* If this is an iterator or an array, the offset must be a variable. */
2182 : 14117 : if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
2183 : 2821 : gfc_put_offset_into_var (pblock, poffset, offsetvar);
2184 : :
2185 : : /* Shadowing the iterator avoids changing its value and saves us from
2186 : : keeping track of it. Further, it makes sure that there's always a
2187 : : backend-decl for the symbol, even if there wasn't one before,
2188 : : e.g. in the case of an iterator that appears in a specification
2189 : : expression in an interface mapping. */
2190 : 14117 : if (c->iterator)
2191 : : {
2192 : 1307 : gfc_symbol *sym;
2193 : 1307 : tree type;
2194 : :
2195 : : /* Evaluate loop bounds before substituting the loop variable
2196 : : in case they depend on it. Such a case is invalid, but it is
2197 : : not more expensive to do the right thing here.
2198 : : See PR 44354. */
2199 : 1307 : gfc_init_se (&se, NULL);
2200 : 1307 : gfc_conv_expr_val (&se, c->iterator->start);
2201 : 1307 : gfc_add_block_to_block (pblock, &se.pre);
2202 : 1307 : start = gfc_evaluate_now (se.expr, pblock);
2203 : :
2204 : 1307 : gfc_init_se (&se, NULL);
2205 : 1307 : gfc_conv_expr_val (&se, c->iterator->end);
2206 : 1307 : gfc_add_block_to_block (pblock, &se.pre);
2207 : 1307 : end = gfc_evaluate_now (se.expr, pblock);
2208 : :
2209 : 1307 : gfc_init_se (&se, NULL);
2210 : 1307 : gfc_conv_expr_val (&se, c->iterator->step);
2211 : 1307 : gfc_add_block_to_block (pblock, &se.pre);
2212 : 1307 : step = gfc_evaluate_now (se.expr, pblock);
2213 : :
2214 : 1307 : sym = c->iterator->var->symtree->n.sym;
2215 : 1307 : type = gfc_typenode_for_spec (&sym->ts);
2216 : :
2217 : 1307 : shadow_loopvar = gfc_create_var (type, "shadow_loopvar");
2218 : 1307 : gfc_shadow_sym (sym, shadow_loopvar, &saved_loopvar);
2219 : : }
2220 : :
2221 : 14117 : gfc_start_block (&body);
2222 : :
2223 : 14117 : if (c->expr->expr_type == EXPR_ARRAY)
2224 : : {
2225 : : /* Array constructors can be nested. */
2226 : 1317 : gfc_trans_array_constructor_value (&body, finalblock, type,
2227 : : desc, c->expr->value.constructor,
2228 : : poffset, offsetvar, dynamic);
2229 : : }
2230 : 12800 : else if (c->expr->rank > 0)
2231 : : {
2232 : 1925 : gfc_trans_array_constructor_subarray (&body, type, desc, c->expr,
2233 : : poffset, offsetvar, dynamic);
2234 : : }
2235 : : else
2236 : : {
2237 : : /* This code really upsets the gimplifier so don't bother for now. */
2238 : : gfc_constructor *p;
2239 : : HOST_WIDE_INT n;
2240 : : HOST_WIDE_INT size;
2241 : :
2242 : : p = c;
2243 : : n = 0;
2244 : 12662 : while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
2245 : : {
2246 : 1787 : p = gfc_constructor_next (p);
2247 : 1787 : n++;
2248 : : }
2249 : : /* Constructor with few constant elements, or element size not
2250 : : known at compile time (e.g. deferred-length character). */
2251 : 10875 : if (n < 4 || !INTEGER_CST_P (TYPE_SIZE_UNIT (type)))
2252 : : {
2253 : : /* Scalar values. */
2254 : 10770 : gfc_init_se (&se, NULL);
2255 : 10770 : gfc_trans_array_ctor_element (&body, desc, *poffset,
2256 : : &se, c->expr);
2257 : :
2258 : 10770 : *poffset = fold_build2_loc (input_location, PLUS_EXPR,
2259 : : gfc_array_index_type,
2260 : : *poffset, gfc_index_one_node);
2261 : 10770 : if (finalblock)
2262 : 953 : gfc_add_block_to_block (finalblock, &se.finalblock);
2263 : : }
2264 : : else
2265 : : {
2266 : : /* Collect multiple scalar constants into a constructor. */
2267 : 105 : vec<constructor_elt, va_gc> *v = NULL;
2268 : 105 : tree init;
2269 : 105 : tree bound;
2270 : 105 : tree tmptype;
2271 : 105 : HOST_WIDE_INT idx = 0;
2272 : :
2273 : 105 : p = c;
2274 : : /* Count the number of consecutive scalar constants. */
2275 : 837 : while (p && !(p->iterator
2276 : 745 : || p->expr->expr_type != EXPR_CONSTANT))
2277 : : {
2278 : 732 : gfc_init_se (&se, NULL);
2279 : 732 : gfc_conv_constant (&se, p->expr);
2280 : :
2281 : 732 : if (c->expr->ts.type != BT_CHARACTER)
2282 : 660 : se.expr = fold_convert (type, se.expr);
2283 : : /* For constant character array constructors we build
2284 : : an array of pointers. */
2285 : 72 : else if (POINTER_TYPE_P (type))
2286 : 0 : se.expr = gfc_build_addr_expr
2287 : 0 : (gfc_get_pchar_type (p->expr->ts.kind),
2288 : : se.expr);
2289 : :
2290 : 732 : CONSTRUCTOR_APPEND_ELT (v,
2291 : : build_int_cst (gfc_array_index_type,
2292 : : idx++),
2293 : : se.expr);
2294 : 732 : c = p;
2295 : 732 : p = gfc_constructor_next (p);
2296 : : }
2297 : :
2298 : 105 : bound = size_int (n - 1);
2299 : : /* Create an array type to hold them. */
2300 : 105 : tmptype = build_range_type (gfc_array_index_type,
2301 : : gfc_index_zero_node, bound);
2302 : 105 : tmptype = build_array_type (type, tmptype);
2303 : :
2304 : 105 : init = build_constructor (tmptype, v);
2305 : 105 : TREE_CONSTANT (init) = 1;
2306 : 105 : TREE_STATIC (init) = 1;
2307 : : /* Create a static variable to hold the data. */
2308 : 105 : tmp = gfc_create_var (tmptype, "data");
2309 : 105 : TREE_STATIC (tmp) = 1;
2310 : 105 : TREE_CONSTANT (tmp) = 1;
2311 : 105 : TREE_READONLY (tmp) = 1;
2312 : 105 : DECL_INITIAL (tmp) = init;
2313 : 105 : init = tmp;
2314 : :
2315 : : /* Use BUILTIN_MEMCPY to assign the values. */
2316 : 105 : tmp = gfc_conv_descriptor_data_get (desc);
2317 : 105 : tmp = build_fold_indirect_ref_loc (input_location,
2318 : : tmp);
2319 : 105 : tmp = gfc_build_array_ref (tmp, *poffset, NULL);
2320 : 105 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
2321 : 105 : init = gfc_build_addr_expr (NULL_TREE, init);
2322 : :
2323 : 105 : size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2324 : 105 : bound = build_int_cst (size_type_node, n * size);
2325 : 105 : tmp = build_call_expr_loc (input_location,
2326 : : builtin_decl_explicit (BUILT_IN_MEMCPY),
2327 : : 3, tmp, init, bound);
2328 : 105 : gfc_add_expr_to_block (&body, tmp);
2329 : :
2330 : 105 : *poffset = fold_build2_loc (input_location, PLUS_EXPR,
2331 : : gfc_array_index_type, *poffset,
2332 : 105 : build_int_cst (gfc_array_index_type, n));
2333 : : }
2334 : 10875 : if (!INTEGER_CST_P (*poffset))
2335 : : {
2336 : 1622 : gfc_add_modify (&body, *offsetvar, *poffset);
2337 : 1622 : *poffset = *offsetvar;
2338 : : }
2339 : :
2340 : 10875 : if (!c->iterator)
2341 : 10875 : ts = c->expr->ts;
2342 : : }
2343 : :
2344 : : /* The frontend should already have done any expansions
2345 : : at compile-time. */
2346 : 14117 : if (!c->iterator)
2347 : : {
2348 : : /* Pass the code as is. */
2349 : 12810 : tmp = gfc_finish_block (&body);
2350 : 12810 : gfc_add_expr_to_block (pblock, tmp);
2351 : : }
2352 : : else
2353 : : {
2354 : : /* Build the implied do-loop. */
2355 : 1307 : stmtblock_t implied_do_block;
2356 : 1307 : tree cond;
2357 : 1307 : tree exit_label;
2358 : 1307 : tree loopbody;
2359 : 1307 : tree tmp2;
2360 : :
2361 : 1307 : loopbody = gfc_finish_block (&body);
2362 : :
2363 : : /* Create a new block that holds the implied-do loop. A temporary
2364 : : loop-variable is used. */
2365 : 1307 : gfc_start_block(&implied_do_block);
2366 : :
2367 : : /* Initialize the loop. */
2368 : 1307 : gfc_add_modify (&implied_do_block, shadow_loopvar, start);
2369 : :
2370 : : /* If this array expands dynamically, and the number of iterations
2371 : : is not constant, we won't have allocated space for the static
2372 : : part of C->EXPR's size. Do that now. */
2373 : 1307 : if (dynamic && gfc_iterator_has_dynamic_bounds (c->iterator))
2374 : : {
2375 : : /* Get the number of iterations. */
2376 : 512 : tmp = gfc_get_iteration_count (shadow_loopvar, end, step);
2377 : :
2378 : : /* Get the static part of C->EXPR's size. */
2379 : 512 : gfc_get_array_constructor_element_size (&size, c->expr);
2380 : 512 : tmp2 = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
2381 : :
2382 : : /* Grow the array by TMP * TMP2 elements. */
2383 : 512 : tmp = fold_build2_loc (input_location, MULT_EXPR,
2384 : : gfc_array_index_type, tmp, tmp2);
2385 : 512 : gfc_grow_array (&implied_do_block, desc, tmp);
2386 : : }
2387 : :
2388 : : /* Generate the loop body. */
2389 : 1307 : exit_label = gfc_build_label_decl (NULL_TREE);
2390 : 1307 : gfc_start_block (&body);
2391 : :
2392 : : /* Generate the exit condition. Depending on the sign of
2393 : : the step variable we have to generate the correct
2394 : : comparison. */
2395 : 1307 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2396 : 1307 : step, build_int_cst (TREE_TYPE (step), 0));
2397 : 1307 : cond = fold_build3_loc (input_location, COND_EXPR,
2398 : : logical_type_node, tmp,
2399 : : fold_build2_loc (input_location, GT_EXPR,
2400 : : logical_type_node, shadow_loopvar, end),
2401 : : fold_build2_loc (input_location, LT_EXPR,
2402 : : logical_type_node, shadow_loopvar, end));
2403 : 1307 : tmp = build1_v (GOTO_EXPR, exit_label);
2404 : 1307 : TREE_USED (exit_label) = 1;
2405 : 1307 : tmp = build3_v (COND_EXPR, cond, tmp,
2406 : : build_empty_stmt (input_location));
2407 : 1307 : gfc_add_expr_to_block (&body, tmp);
2408 : :
2409 : : /* The main loop body. */
2410 : 1307 : gfc_add_expr_to_block (&body, loopbody);
2411 : :
2412 : : /* Increase loop variable by step. */
2413 : 1307 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
2414 : 1307 : TREE_TYPE (shadow_loopvar), shadow_loopvar,
2415 : : step);
2416 : 1307 : gfc_add_modify (&body, shadow_loopvar, tmp);
2417 : :
2418 : : /* Finish the loop. */
2419 : 1307 : tmp = gfc_finish_block (&body);
2420 : 1307 : tmp = build1_v (LOOP_EXPR, tmp);
2421 : 1307 : gfc_add_expr_to_block (&implied_do_block, tmp);
2422 : :
2423 : : /* Add the exit label. */
2424 : 1307 : tmp = build1_v (LABEL_EXPR, exit_label);
2425 : 1307 : gfc_add_expr_to_block (&implied_do_block, tmp);
2426 : :
2427 : : /* Finish the implied-do loop. */
2428 : 1307 : tmp = gfc_finish_block(&implied_do_block);
2429 : 1307 : gfc_add_expr_to_block(pblock, tmp);
2430 : :
2431 : 1307 : gfc_restore_sym (c->iterator->var->symtree->n.sym, &saved_loopvar);
2432 : : }
2433 : : }
2434 : :
2435 : : /* F2008 4.5.6.3 para 5: If an executable construct references a structure
2436 : : constructor or array constructor, the entity created by the constructor is
2437 : : finalized after execution of the innermost executable construct containing
2438 : : the reference. This, in fact, was later deleted by the Combined Techical
2439 : : Corrigenda 1 TO 4 for fortran 2008 (f08/0011).
2440 : :
2441 : : Transmit finalization of this constructor through 'finalblock'. */
2442 : 8559 : if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
2443 : 8559 : && !(gfc_option.allow_std & GFC_STD_GNU)
2444 : 70 : && finalblock != NULL
2445 : 24 : && gfc_may_be_finalized (ts)
2446 : 18 : && ctr > 0 && desc != NULL_TREE
2447 : 8577 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
2448 : : {
2449 : 18 : symbol_attribute attr;
2450 : 18 : gfc_se fse;
2451 : 18 : locus loc;
2452 : 18 : gfc_locus_from_location (&loc, input_location);
2453 : 18 : gfc_warning (0, "The structure constructor at %L has been"
2454 : : " finalized. This feature was removed by f08/0011."
2455 : : " Use -std=f2018 or -std=gnu to eliminate the"
2456 : : " finalization.", &loc);
2457 : 18 : attr.pointer = attr.allocatable = 0;
2458 : 18 : gfc_init_se (&fse, NULL);
2459 : 18 : fse.expr = desc;
2460 : 18 : gfc_finalize_tree_expr (&fse, ts.u.derived, attr, 1);
2461 : 18 : gfc_add_block_to_block (finalblock, &fse.pre);
2462 : 18 : gfc_add_block_to_block (finalblock, &fse.finalblock);
2463 : 18 : gfc_add_block_to_block (finalblock, &fse.post);
2464 : : }
2465 : :
2466 : 8559 : mpz_clear (size);
2467 : 8559 : }
2468 : :
2469 : :
2470 : : /* The array constructor code can create a string length with an operand
2471 : : in the form of a temporary variable. This variable will retain its
2472 : : context (current_function_decl). If we store this length tree in a
2473 : : gfc_charlen structure which is shared by a variable in another
2474 : : context, the resulting gfc_charlen structure with a variable in a
2475 : : different context, we could trip the assertion in expand_expr_real_1
2476 : : when it sees that a variable has been created in one context and
2477 : : referenced in another.
2478 : :
2479 : : If this might be the case, we create a new gfc_charlen structure and
2480 : : link it into the current namespace. */
2481 : :
2482 : : static void
2483 : 8097 : store_backend_decl (gfc_charlen **clp, tree len, bool force_new_cl)
2484 : : {
2485 : 8097 : if (force_new_cl)
2486 : : {
2487 : 8070 : gfc_charlen *new_cl = gfc_new_charlen (gfc_current_ns, *clp);
2488 : 8070 : *clp = new_cl;
2489 : : }
2490 : 8097 : (*clp)->backend_decl = len;
2491 : 8097 : }
2492 : :
2493 : : /* A catch-all to obtain the string length for anything that is not
2494 : : a substring of non-constant length, a constant, array or variable. */
2495 : :
2496 : : static void
2497 : 318 : get_array_ctor_all_strlen (stmtblock_t *block, gfc_expr *e, tree *len)
2498 : : {
2499 : 318 : gfc_se se;
2500 : :
2501 : : /* Don't bother if we already know the length is a constant. */
2502 : 318 : if (*len && INTEGER_CST_P (*len))
2503 : 40 : return;
2504 : :
2505 : 278 : if (!e->ref && e->ts.u.cl && e->ts.u.cl->length
2506 : 29 : && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2507 : : {
2508 : : /* This is easy. */
2509 : 1 : gfc_conv_const_charlen (e->ts.u.cl);
2510 : 1 : *len = e->ts.u.cl->backend_decl;
2511 : : }
2512 : : else
2513 : : {
2514 : : /* Otherwise, be brutal even if inefficient. */
2515 : 277 : gfc_init_se (&se, NULL);
2516 : :
2517 : : /* No function call, in case of side effects. */
2518 : 277 : se.no_function_call = 1;
2519 : 277 : if (e->rank == 0)
2520 : 134 : gfc_conv_expr (&se, e);
2521 : : else
2522 : 143 : gfc_conv_expr_descriptor (&se, e);
2523 : :
2524 : : /* Fix the value. */
2525 : 277 : *len = gfc_evaluate_now (se.string_length, &se.pre);
2526 : :
2527 : 277 : gfc_add_block_to_block (block, &se.pre);
2528 : 277 : gfc_add_block_to_block (block, &se.post);
2529 : :
2530 : 277 : store_backend_decl (&e->ts.u.cl, *len, true);
2531 : : }
2532 : : }
2533 : :
2534 : :
2535 : : /* Figure out the string length of a variable reference expression.
2536 : : Used by get_array_ctor_strlen. */
2537 : :
2538 : : static void
2539 : 930 : get_array_ctor_var_strlen (stmtblock_t *block, gfc_expr * expr, tree * len)
2540 : : {
2541 : 930 : gfc_ref *ref;
2542 : 930 : gfc_typespec *ts;
2543 : 930 : mpz_t char_len;
2544 : 930 : gfc_se se;
2545 : :
2546 : : /* Don't bother if we already know the length is a constant. */
2547 : 930 : if (*len && INTEGER_CST_P (*len))
2548 : 557 : return;
2549 : :
2550 : 468 : ts = &expr->symtree->n.sym->ts;
2551 : 747 : for (ref = expr->ref; ref; ref = ref->next)
2552 : : {
2553 : 374 : switch (ref->type)
2554 : : {
2555 : 234 : case REF_ARRAY:
2556 : : /* Array references don't change the string length. */
2557 : 234 : if (ts->deferred)
2558 : 136 : get_array_ctor_all_strlen (block, expr, len);
2559 : : break;
2560 : :
2561 : 45 : case REF_COMPONENT:
2562 : : /* Use the length of the component. */
2563 : 45 : ts = &ref->u.c.component->ts;
2564 : 45 : break;
2565 : :
2566 : 95 : case REF_SUBSTRING:
2567 : 95 : if (ref->u.ss.end == NULL
2568 : 83 : || ref->u.ss.start->expr_type != EXPR_CONSTANT
2569 : 64 : || ref->u.ss.end->expr_type != EXPR_CONSTANT)
2570 : : {
2571 : : /* Note that this might evaluate expr. */
2572 : 64 : get_array_ctor_all_strlen (block, expr, len);
2573 : 64 : return;
2574 : : }
2575 : 31 : mpz_init_set_ui (char_len, 1);
2576 : 31 : mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
2577 : 31 : mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
2578 : 31 : *len = gfc_conv_mpz_to_tree_type (char_len, gfc_charlen_type_node);
2579 : 31 : mpz_clear (char_len);
2580 : 31 : return;
2581 : :
2582 : : case REF_INQUIRY:
2583 : : break;
2584 : :
2585 : 0 : default:
2586 : 0 : gcc_unreachable ();
2587 : : }
2588 : : }
2589 : :
2590 : : /* A last ditch attempt that is sometimes needed for deferred characters. */
2591 : 373 : if (!ts->u.cl->backend_decl)
2592 : : {
2593 : 19 : gfc_init_se (&se, NULL);
2594 : 19 : if (expr->rank)
2595 : 12 : gfc_conv_expr_descriptor (&se, expr);
2596 : : else
2597 : 7 : gfc_conv_expr (&se, expr);
2598 : 19 : gcc_assert (se.string_length != NULL_TREE);
2599 : 19 : gfc_add_block_to_block (block, &se.pre);
2600 : 19 : ts->u.cl->backend_decl = se.string_length;
2601 : : }
2602 : :
2603 : 373 : *len = ts->u.cl->backend_decl;
2604 : : }
2605 : :
2606 : :
2607 : : /* Figure out the string length of a character array constructor.
2608 : : If len is NULL, don't calculate the length; this happens for recursive calls
2609 : : when a sub-array-constructor is an element but not at the first position,
2610 : : so when we're not interested in the length.
2611 : : Returns TRUE if all elements are character constants. */
2612 : :
2613 : : bool
2614 : 8516 : get_array_ctor_strlen (stmtblock_t *block, gfc_constructor_base base, tree * len)
2615 : : {
2616 : 8516 : gfc_constructor *c;
2617 : 8516 : bool is_const;
2618 : :
2619 : 8516 : is_const = true;
2620 : :
2621 : 8516 : if (gfc_constructor_first (base) == NULL)
2622 : : {
2623 : 303 : if (len)
2624 : 303 : *len = build_int_cstu (gfc_charlen_type_node, 0);
2625 : 303 : return is_const;
2626 : : }
2627 : :
2628 : : /* Loop over all constructor elements to find out is_const, but in len we
2629 : : want to store the length of the first, not the last, element. We can
2630 : : of course exit the loop as soon as is_const is found to be false. */
2631 : 8213 : for (c = gfc_constructor_first (base);
2632 : 45224 : c && is_const; c = gfc_constructor_next (c))
2633 : : {
2634 : 37011 : switch (c->expr->expr_type)
2635 : : {
2636 : 35860 : case EXPR_CONSTANT:
2637 : 35860 : if (len && !(*len && INTEGER_CST_P (*len)))
2638 : 392 : *len = build_int_cstu (gfc_charlen_type_node,
2639 : 392 : c->expr->value.character.length);
2640 : : break;
2641 : :
2642 : 43 : case EXPR_ARRAY:
2643 : 43 : if (!get_array_ctor_strlen (block, c->expr->value.constructor, len))
2644 : 1139 : is_const = false;
2645 : : break;
2646 : :
2647 : 990 : case EXPR_VARIABLE:
2648 : 990 : is_const = false;
2649 : 990 : if (len)
2650 : 930 : get_array_ctor_var_strlen (block, c->expr, len);
2651 : : break;
2652 : :
2653 : 118 : default:
2654 : 118 : is_const = false;
2655 : 118 : if (len)
2656 : 118 : get_array_ctor_all_strlen (block, c->expr, len);
2657 : : break;
2658 : : }
2659 : :
2660 : : /* After the first iteration, we don't want the length modified. */
2661 : 37011 : len = NULL;
2662 : : }
2663 : :
2664 : : return is_const;
2665 : : }
2666 : :
2667 : : /* Check whether the array constructor C consists entirely of constant
2668 : : elements, and if so returns the number of those elements, otherwise
2669 : : return zero. Note, an empty or NULL array constructor returns zero. */
2670 : :
2671 : : unsigned HOST_WIDE_INT
2672 : 56879 : gfc_constant_array_constructor_p (gfc_constructor_base base)
2673 : : {
2674 : 56879 : unsigned HOST_WIDE_INT nelem = 0;
2675 : :
2676 : 56879 : gfc_constructor *c = gfc_constructor_first (base);
2677 : 457398 : while (c)
2678 : : {
2679 : 351836 : if (c->iterator
2680 : 350458 : || c->expr->rank > 0
2681 : 348034 : || c->expr->expr_type != EXPR_CONSTANT)
2682 : : return 0;
2683 : 343640 : c = gfc_constructor_next (c);
2684 : 343640 : nelem++;
2685 : : }
2686 : : return nelem;
2687 : : }
2688 : :
2689 : :
2690 : : /* Given EXPR, the constant array constructor specified by an EXPR_ARRAY,
2691 : : and the tree type of it's elements, TYPE, return a static constant
2692 : : variable that is compile-time initialized. */
2693 : :
2694 : : tree
2695 : 38991 : gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
2696 : : {
2697 : 38991 : tree tmptype, init, tmp;
2698 : 38991 : HOST_WIDE_INT nelem;
2699 : 38991 : gfc_constructor *c;
2700 : 38991 : gfc_array_spec as;
2701 : 38991 : gfc_se se;
2702 : 38991 : int i;
2703 : 38991 : vec<constructor_elt, va_gc> *v = NULL;
2704 : :
2705 : : /* First traverse the constructor list, converting the constants
2706 : : to tree to build an initializer. */
2707 : 38991 : nelem = 0;
2708 : 38991 : c = gfc_constructor_first (expr->value.constructor);
2709 : 356660 : while (c)
2710 : : {
2711 : 278678 : gfc_init_se (&se, NULL);
2712 : 278678 : gfc_conv_constant (&se, c->expr);
2713 : 278678 : if (c->expr->ts.type != BT_CHARACTER)
2714 : 243720 : se.expr = fold_convert (type, se.expr);
2715 : 34958 : else if (POINTER_TYPE_P (type))
2716 : 34958 : se.expr = gfc_build_addr_expr (gfc_get_pchar_type (c->expr->ts.kind),
2717 : : se.expr);
2718 : 278678 : CONSTRUCTOR_APPEND_ELT (v, build_int_cst (gfc_array_index_type, nelem),
2719 : : se.expr);
2720 : 278678 : c = gfc_constructor_next (c);
2721 : 278678 : nelem++;
2722 : : }
2723 : :
2724 : : /* Next determine the tree type for the array. We use the gfortran
2725 : : front-end's gfc_get_nodesc_array_type in order to create a suitable
2726 : : GFC_ARRAY_TYPE_P that may be used by the scalarizer. */
2727 : :
2728 : 38991 : memset (&as, 0, sizeof (gfc_array_spec));
2729 : :
2730 : 38991 : as.rank = expr->rank;
2731 : 38991 : as.type = AS_EXPLICIT;
2732 : 38991 : if (!expr->shape)
2733 : : {
2734 : 3 : as.lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2735 : 3 : as.upper[0] = gfc_get_int_expr (gfc_default_integer_kind,
2736 : : NULL, nelem - 1);
2737 : : }
2738 : : else
2739 : 83589 : for (i = 0; i < expr->rank; i++)
2740 : : {
2741 : 44601 : int tmp = (int) mpz_get_si (expr->shape[i]);
2742 : 44601 : as.lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2743 : 44601 : as.upper[i] = gfc_get_int_expr (gfc_default_integer_kind,
2744 : 44601 : NULL, tmp - 1);
2745 : : }
2746 : :
2747 : 38991 : tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
2748 : :
2749 : : /* as is not needed anymore. */
2750 : 122586 : for (i = 0; i < as.rank + as.corank; i++)
2751 : : {
2752 : 44604 : gfc_free_expr (as.lower[i]);
2753 : 44604 : gfc_free_expr (as.upper[i]);
2754 : : }
2755 : :
2756 : 38991 : init = build_constructor (tmptype, v);
2757 : :
2758 : 38991 : TREE_CONSTANT (init) = 1;
2759 : 38991 : TREE_STATIC (init) = 1;
2760 : :
2761 : 38991 : tmp = build_decl (input_location, VAR_DECL, create_tmp_var_name ("A"),
2762 : : tmptype);
2763 : 38991 : DECL_ARTIFICIAL (tmp) = 1;
2764 : 38991 : DECL_IGNORED_P (tmp) = 1;
2765 : 38991 : TREE_STATIC (tmp) = 1;
2766 : 38991 : TREE_CONSTANT (tmp) = 1;
2767 : 38991 : TREE_READONLY (tmp) = 1;
2768 : 38991 : DECL_INITIAL (tmp) = init;
2769 : 38991 : pushdecl (tmp);
2770 : :
2771 : 38991 : return tmp;
2772 : : }
2773 : :
2774 : :
2775 : : /* Translate a constant EXPR_ARRAY array constructor for the scalarizer.
2776 : : This mostly initializes the scalarizer state info structure with the
2777 : : appropriate values to directly use the array created by the function
2778 : : gfc_build_constant_array_constructor. */
2779 : :
2780 : : static void
2781 : 33887 : trans_constant_array_constructor (gfc_ss * ss, tree type)
2782 : : {
2783 : 33887 : gfc_array_info *info;
2784 : 33887 : tree tmp;
2785 : 33887 : int i;
2786 : :
2787 : 33887 : tmp = gfc_build_constant_array_constructor (ss->info->expr, type);
2788 : :
2789 : 33887 : info = &ss->info->data.array;
2790 : :
2791 : 33887 : info->descriptor = tmp;
2792 : 33887 : info->data = gfc_build_addr_expr (NULL_TREE, tmp);
2793 : 33887 : info->offset = gfc_index_zero_node;
2794 : :
2795 : 71132 : for (i = 0; i < ss->dimen; i++)
2796 : : {
2797 : 37245 : info->delta[i] = gfc_index_zero_node;
2798 : 37245 : info->start[i] = gfc_index_zero_node;
2799 : 37245 : info->end[i] = gfc_index_zero_node;
2800 : 37245 : info->stride[i] = gfc_index_one_node;
2801 : : }
2802 : 33887 : }
2803 : :
2804 : :
2805 : : static int
2806 : 33893 : get_rank (gfc_loopinfo *loop)
2807 : : {
2808 : 33893 : int rank;
2809 : :
2810 : 33893 : rank = 0;
2811 : 147456 : for (; loop; loop = loop->parent)
2812 : 73734 : rank += loop->dimen;
2813 : :
2814 : 39829 : return rank;
2815 : : }
2816 : :
2817 : :
2818 : : /* Helper routine of gfc_trans_array_constructor to determine if the
2819 : : bounds of the loop specified by LOOP are constant and simple enough
2820 : : to use with trans_constant_array_constructor. Returns the
2821 : : iteration count of the loop if suitable, and NULL_TREE otherwise. */
2822 : :
2823 : : static tree
2824 : 33893 : constant_array_constructor_loop_size (gfc_loopinfo * l)
2825 : : {
2826 : 33893 : gfc_loopinfo *loop;
2827 : 33893 : tree size = gfc_index_one_node;
2828 : 33893 : tree tmp;
2829 : 33893 : int i, total_dim;
2830 : :
2831 : 33893 : total_dim = get_rank (l);
2832 : :
2833 : 67786 : for (loop = l; loop; loop = loop->parent)
2834 : : {
2835 : 71156 : for (i = 0; i < loop->dimen; i++)
2836 : : {
2837 : : /* If the bounds aren't constant, return NULL_TREE. */
2838 : 37263 : if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
2839 : : return NULL_TREE;
2840 : 37257 : if (!integer_zerop (loop->from[i]))
2841 : : {
2842 : : /* Only allow nonzero "from" in one-dimensional arrays. */
2843 : 0 : if (total_dim != 1)
2844 : : return NULL_TREE;
2845 : 0 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2846 : : gfc_array_index_type,
2847 : : loop->to[i], loop->from[i]);
2848 : : }
2849 : : else
2850 : 37257 : tmp = loop->to[i];
2851 : 37257 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
2852 : : gfc_array_index_type, tmp, gfc_index_one_node);
2853 : 37257 : size = fold_build2_loc (input_location, MULT_EXPR,
2854 : : gfc_array_index_type, size, tmp);
2855 : : }
2856 : : }
2857 : :
2858 : : return size;
2859 : : }
2860 : :
2861 : :
2862 : : static tree *
2863 : 41129 : get_loop_upper_bound_for_array (gfc_ss *array, int array_dim)
2864 : : {
2865 : 41129 : gfc_ss *ss;
2866 : 41129 : int n;
2867 : :
2868 : 41129 : gcc_assert (array->nested_ss == NULL);
2869 : :
2870 : 41129 : for (ss = array; ss; ss = ss->parent)
2871 : 41129 : for (n = 0; n < ss->loop->dimen; n++)
2872 : 41129 : if (array_dim == get_array_ref_dim_for_loop_dim (ss, n))
2873 : 41129 : return &(ss->loop->to[n]);
2874 : :
2875 : 0 : gcc_unreachable ();
2876 : : }
2877 : :
2878 : :
2879 : : static gfc_loopinfo *
2880 : 686286 : outermost_loop (gfc_loopinfo * loop)
2881 : : {
2882 : 889469 : while (loop->parent != NULL)
2883 : : loop = loop->parent;
2884 : :
2885 : 686286 : return loop;
2886 : : }
2887 : :
2888 : :
2889 : : /* Array constructors are handled by constructing a temporary, then using that
2890 : : within the scalarization loop. This is not optimal, but seems by far the
2891 : : simplest method. */
2892 : :
2893 : : static void
2894 : 41129 : trans_array_constructor (gfc_ss * ss, locus * where)
2895 : : {
2896 : 41129 : gfc_constructor_base c;
2897 : 41129 : tree offset;
2898 : 41129 : tree offsetvar;
2899 : 41129 : tree desc;
2900 : 41129 : tree type;
2901 : 41129 : tree tmp;
2902 : 41129 : tree *loop_ubound0;
2903 : 41129 : bool dynamic;
2904 : 41129 : bool old_first_len, old_typespec_chararray_ctor;
2905 : 41129 : tree old_first_len_val;
2906 : 41129 : gfc_loopinfo *loop, *outer_loop;
2907 : 41129 : gfc_ss_info *ss_info;
2908 : 41129 : gfc_expr *expr;
2909 : 41129 : gfc_ss *s;
2910 : 41129 : tree neg_len;
2911 : 41129 : char *msg;
2912 : 41129 : stmtblock_t finalblock;
2913 : 41129 : bool finalize_required;
2914 : :
2915 : : /* Save the old values for nested checking. */
2916 : 41129 : old_first_len = first_len;
2917 : 41129 : old_first_len_val = first_len_val;
2918 : 41129 : old_typespec_chararray_ctor = typespec_chararray_ctor;
2919 : :
2920 : 41129 : loop = ss->loop;
2921 : 41129 : outer_loop = outermost_loop (loop);
2922 : 41129 : ss_info = ss->info;
2923 : 41129 : expr = ss_info->expr;
2924 : :
2925 : : /* Do bounds-checking here and in gfc_trans_array_ctor_element only if no
2926 : : typespec was given for the array constructor. */
2927 : 82258 : typespec_chararray_ctor = (expr->ts.type == BT_CHARACTER
2928 : 7820 : && expr->ts.u.cl
2929 : 48949 : && expr->ts.u.cl->length_from_typespec);
2930 : :
2931 : 41129 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2932 : 2538 : && expr->ts.type == BT_CHARACTER && !typespec_chararray_ctor)
2933 : : {
2934 : 1468 : first_len_val = gfc_create_var (gfc_charlen_type_node, "len");
2935 : 1468 : first_len = true;
2936 : : }
2937 : :
2938 : 41129 : gcc_assert (ss->dimen == ss->loop->dimen);
2939 : :
2940 : 41129 : c = expr->value.constructor;
2941 : 41129 : if (expr->ts.type == BT_CHARACTER)
2942 : : {
2943 : 7820 : bool const_string;
2944 : 7820 : bool force_new_cl = false;
2945 : :
2946 : : /* get_array_ctor_strlen walks the elements of the constructor, if a
2947 : : typespec was given, we already know the string length and want the one
2948 : : specified there. */
2949 : 7820 : if (typespec_chararray_ctor && expr->ts.u.cl->length
2950 : 344 : && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2951 : : {
2952 : 27 : gfc_se length_se;
2953 : :
2954 : 27 : const_string = false;
2955 : 27 : gfc_init_se (&length_se, NULL);
2956 : 27 : gfc_conv_expr_type (&length_se, expr->ts.u.cl->length,
2957 : : gfc_charlen_type_node);
2958 : 27 : ss_info->string_length = length_se.expr;
2959 : :
2960 : : /* Check if the character length is negative. If it is, then
2961 : : set LEN = 0. */
2962 : 27 : neg_len = fold_build2_loc (input_location, LT_EXPR,
2963 : : logical_type_node, ss_info->string_length,
2964 : 27 : build_zero_cst (TREE_TYPE
2965 : : (ss_info->string_length)));
2966 : : /* Print a warning if bounds checking is enabled. */
2967 : 27 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2968 : : {
2969 : 18 : msg = xasprintf ("Negative character length treated as LEN = 0");
2970 : 18 : gfc_trans_runtime_check (false, true, neg_len, &length_se.pre,
2971 : : where, msg);
2972 : 18 : free (msg);
2973 : : }
2974 : :
2975 : 27 : ss_info->string_length
2976 : 27 : = fold_build3_loc (input_location, COND_EXPR,
2977 : : gfc_charlen_type_node, neg_len,
2978 : : build_zero_cst
2979 : 27 : (TREE_TYPE (ss_info->string_length)),
2980 : : ss_info->string_length);
2981 : 27 : ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
2982 : : &length_se.pre);
2983 : 27 : gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
2984 : 27 : gfc_add_block_to_block (&outer_loop->post, &length_se.post);
2985 : 27 : }
2986 : : else
2987 : : {
2988 : 7793 : const_string = get_array_ctor_strlen (&outer_loop->pre, c,
2989 : : &ss_info->string_length);
2990 : 7793 : force_new_cl = true;
2991 : :
2992 : : /* Initialize "len" with string length for bounds checking. */
2993 : 7793 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2994 : 1486 : && !typespec_chararray_ctor
2995 : 1468 : && ss_info->string_length)
2996 : : {
2997 : 1468 : gfc_se length_se;
2998 : :
2999 : 1468 : gfc_init_se (&length_se, NULL);
3000 : 1468 : gfc_add_modify (&length_se.pre, first_len_val,
3001 : 1468 : fold_convert (TREE_TYPE (first_len_val),
3002 : : ss_info->string_length));
3003 : 1468 : ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
3004 : : &length_se.pre);
3005 : 1468 : gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
3006 : 1468 : gfc_add_block_to_block (&outer_loop->post, &length_se.post);
3007 : : }
3008 : : }
3009 : :
3010 : : /* Complex character array constructors should have been taken care of
3011 : : and not end up here. */
3012 : 7820 : gcc_assert (ss_info->string_length);
3013 : :
3014 : 7820 : store_backend_decl (&expr->ts.u.cl, ss_info->string_length, force_new_cl);
3015 : :
3016 : 7820 : type = gfc_get_character_type_len (expr->ts.kind, ss_info->string_length);
3017 : 7820 : if (const_string)
3018 : 6881 : type = build_pointer_type (type);
3019 : : }
3020 : : else
3021 : 33334 : type = gfc_typenode_for_spec (expr->ts.type == BT_CLASS
3022 : 25 : ? &CLASS_DATA (expr)->ts : &expr->ts);
3023 : :
3024 : : /* See if the constructor determines the loop bounds. */
3025 : 41129 : dynamic = false;
3026 : :
3027 : 41129 : loop_ubound0 = get_loop_upper_bound_for_array (ss, 0);
3028 : :
3029 : 80958 : if (expr->shape && get_rank (loop) > 1 && *loop_ubound0 == NULL_TREE)
3030 : : {
3031 : : /* We have a multidimensional parameter. */
3032 : 0 : for (s = ss; s; s = s->parent)
3033 : : {
3034 : : int n;
3035 : 0 : for (n = 0; n < s->loop->dimen; n++)
3036 : : {
3037 : 0 : s->loop->from[n] = gfc_index_zero_node;
3038 : 0 : s->loop->to[n] = gfc_conv_mpz_to_tree (expr->shape[s->dim[n]],
3039 : : gfc_index_integer_kind);
3040 : 0 : s->loop->to[n] = fold_build2_loc (input_location, MINUS_EXPR,
3041 : : gfc_array_index_type,
3042 : 0 : s->loop->to[n],
3043 : : gfc_index_one_node);
3044 : : }
3045 : : }
3046 : : }
3047 : :
3048 : 41129 : if (*loop_ubound0 == NULL_TREE)
3049 : : {
3050 : 838 : mpz_t size;
3051 : :
3052 : : /* We should have a 1-dimensional, zero-based loop. */
3053 : 838 : gcc_assert (loop->parent == NULL && loop->nested == NULL);
3054 : 838 : gcc_assert (loop->dimen == 1);
3055 : 838 : gcc_assert (integer_zerop (loop->from[0]));
3056 : :
3057 : : /* Split the constructor size into a static part and a dynamic part.
3058 : : Allocate the static size up-front and record whether the dynamic
3059 : : size might be nonzero. */
3060 : 838 : mpz_init (size);
3061 : 838 : dynamic = gfc_get_array_constructor_size (&size, c);
3062 : 838 : mpz_sub_ui (size, size, 1);
3063 : 838 : loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
3064 : 838 : mpz_clear (size);
3065 : : }
3066 : :
3067 : : /* Special case constant array constructors. */
3068 : 838 : if (!dynamic)
3069 : : {
3070 : 40309 : unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
3071 : 40309 : if (nelem > 0)
3072 : : {
3073 : 33893 : tree size = constant_array_constructor_loop_size (loop);
3074 : 33893 : if (size && compare_tree_int (size, nelem) == 0)
3075 : : {
3076 : 33887 : trans_constant_array_constructor (ss, type);
3077 : 33887 : goto finish;
3078 : : }
3079 : : }
3080 : : }
3081 : :
3082 : 7242 : gfc_trans_create_temp_array (&outer_loop->pre, &outer_loop->post, ss, type,
3083 : : NULL_TREE, dynamic, true, false, where);
3084 : :
3085 : 7242 : desc = ss_info->data.array.descriptor;
3086 : 7242 : offset = gfc_index_zero_node;
3087 : 7242 : offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
3088 : 7242 : suppress_warning (offsetvar);
3089 : 7242 : TREE_USED (offsetvar) = 0;
3090 : :
3091 : 7242 : gfc_init_block (&finalblock);
3092 : 7242 : finalize_required = expr->must_finalize;
3093 : 7242 : if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->attr.alloc_comp)
3094 : : finalize_required = true;
3095 : 7272 : gfc_trans_array_constructor_value (&outer_loop->pre,
3096 : : finalize_required ? &finalblock : NULL,
3097 : : type, desc, c, &offset, &offsetvar,
3098 : : dynamic);
3099 : :
3100 : : /* If the array grows dynamically, the upper bound of the loop variable
3101 : : is determined by the array's final upper bound. */
3102 : 7242 : if (dynamic)
3103 : : {
3104 : 820 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
3105 : : gfc_array_index_type,
3106 : : offsetvar, gfc_index_one_node);
3107 : 820 : tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
3108 : 820 : if (*loop_ubound0 && VAR_P (*loop_ubound0))
3109 : 0 : gfc_add_modify (&outer_loop->pre, *loop_ubound0, tmp);
3110 : : else
3111 : 820 : *loop_ubound0 = tmp;
3112 : : }
3113 : :
3114 : 7242 : if (TREE_USED (offsetvar))
3115 : 2821 : pushdecl (offsetvar);
3116 : : else
3117 : 4421 : gcc_assert (INTEGER_CST_P (offset));
3118 : :
3119 : : #if 0
3120 : : /* Disable bound checking for now because it's probably broken. */
3121 : : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3122 : : {
3123 : : gcc_unreachable ();
3124 : : }
3125 : : #endif
3126 : :
3127 : 4421 : finish:
3128 : : /* Restore old values of globals. */
3129 : 41129 : first_len = old_first_len;
3130 : 41129 : first_len_val = old_first_len_val;
3131 : 41129 : typespec_chararray_ctor = old_typespec_chararray_ctor;
3132 : :
3133 : : /* F2008 4.5.6.3 para 5: If an executable construct references a structure
3134 : : constructor or array constructor, the entity created by the constructor is
3135 : : finalized after execution of the innermost executable construct containing
3136 : : the reference. */
3137 : 41129 : if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
3138 : 1559 : && finalblock.head != NULL_TREE)
3139 : 36 : gfc_prepend_expr_to_block (&loop->post, finalblock.head);
3140 : 41129 : }
3141 : :
3142 : :
3143 : : /* INFO describes a GFC_SS_SECTION in loop LOOP, and this function is
3144 : : called after evaluating all of INFO's vector dimensions. Go through
3145 : : each such vector dimension and see if we can now fill in any missing
3146 : : loop bounds. */
3147 : :
3148 : : static void
3149 : 175785 : set_vector_loop_bounds (gfc_ss * ss)
3150 : : {
3151 : 175785 : gfc_loopinfo *loop, *outer_loop;
3152 : 175785 : gfc_array_info *info;
3153 : 175785 : gfc_se se;
3154 : 175785 : tree tmp;
3155 : 175785 : tree desc;
3156 : 175785 : tree zero;
3157 : 175785 : int n;
3158 : 175785 : int dim;
3159 : :
3160 : 175785 : outer_loop = outermost_loop (ss->loop);
3161 : :
3162 : 175785 : info = &ss->info->data.array;
3163 : :
3164 : 356206 : for (; ss; ss = ss->parent)
3165 : : {
3166 : 180421 : loop = ss->loop;
3167 : :
3168 : 431338 : for (n = 0; n < loop->dimen; n++)
3169 : : {
3170 : 250917 : dim = ss->dim[n];
3171 : 250917 : if (info->ref->u.ar.dimen_type[dim] != DIMEN_VECTOR
3172 : 752 : || loop->to[n] != NULL)
3173 : 250750 : continue;
3174 : :
3175 : : /* Loop variable N indexes vector dimension DIM, and we don't
3176 : : yet know the upper bound of loop variable N. Set it to the
3177 : : difference between the vector's upper and lower bounds. */
3178 : 167 : gcc_assert (loop->from[n] == gfc_index_zero_node);
3179 : 167 : gcc_assert (info->subscript[dim]
3180 : : && info->subscript[dim]->info->type == GFC_SS_VECTOR);
3181 : :
3182 : 167 : gfc_init_se (&se, NULL);
3183 : 167 : desc = info->subscript[dim]->info->data.array.descriptor;
3184 : 167 : zero = gfc_rank_cst[0];
3185 : 167 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
3186 : : gfc_array_index_type,
3187 : : gfc_conv_descriptor_ubound_get (desc, zero),
3188 : : gfc_conv_descriptor_lbound_get (desc, zero));
3189 : 167 : tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
3190 : 167 : loop->to[n] = tmp;
3191 : : }
3192 : : }
3193 : 175785 : }
3194 : :
3195 : :
3196 : : /* Tells whether a scalar argument to an elemental procedure is saved out
3197 : : of a scalarization loop as a value or as a reference. */
3198 : :
3199 : : bool
3200 : 43970 : gfc_scalar_elemental_arg_saved_as_reference (gfc_ss_info * ss_info)
3201 : : {
3202 : 43970 : if (ss_info->type != GFC_SS_REFERENCE)
3203 : : return false;
3204 : :
3205 : 10242 : if (ss_info->data.scalar.needs_temporary)
3206 : : return false;
3207 : :
3208 : : /* If the actual argument can be absent (in other words, it can
3209 : : be a NULL reference), don't try to evaluate it; pass instead
3210 : : the reference directly. */
3211 : 9878 : if (ss_info->can_be_null_ref)
3212 : : return true;
3213 : :
3214 : : /* If the expression is of polymorphic type, it's actual size is not known,
3215 : : so we avoid copying it anywhere. */
3216 : 9202 : if (ss_info->data.scalar.dummy_arg
3217 : 1400 : && gfc_dummy_arg_get_typespec (*ss_info->data.scalar.dummy_arg).type
3218 : : == BT_CLASS
3219 : 9324 : && ss_info->expr->ts.type == BT_CLASS)
3220 : : return true;
3221 : :
3222 : : /* If the expression is a data reference of aggregate type,
3223 : : and the data reference is not used on the left hand side,
3224 : : avoid a copy by saving a reference to the content. */
3225 : 9178 : if (!ss_info->data.scalar.needs_temporary
3226 : 9178 : && (ss_info->expr->ts.type == BT_DERIVED
3227 : 8230 : || ss_info->expr->ts.type == BT_CLASS)
3228 : 10174 : && gfc_expr_is_variable (ss_info->expr))
3229 : : return true;
3230 : :
3231 : : /* Otherwise the expression is evaluated to a temporary variable before the
3232 : : scalarization loop. */
3233 : : return false;
3234 : : }
3235 : :
3236 : :
3237 : : /* Add the pre and post chains for all the scalar expressions in a SS chain
3238 : : to loop. This is called after the loop parameters have been calculated,
3239 : : but before the actual scalarizing loops. */
3240 : :
3241 : : static void
3242 : 184801 : gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
3243 : : locus * where)
3244 : : {
3245 : 184801 : gfc_loopinfo *nested_loop, *outer_loop;
3246 : 184801 : gfc_se se;
3247 : 184801 : gfc_ss_info *ss_info;
3248 : 184801 : gfc_array_info *info;
3249 : 184801 : gfc_expr *expr;
3250 : 184801 : int n;
3251 : :
3252 : : /* Don't evaluate the arguments for realloc_lhs_loop_for_fcn_call; otherwise,
3253 : : arguments could get evaluated multiple times. */
3254 : 184801 : if (ss->is_alloc_lhs)
3255 : 166 : return;
3256 : :
3257 : 486326 : outer_loop = outermost_loop (loop);
3258 : :
3259 : : /* TODO: This can generate bad code if there are ordering dependencies,
3260 : : e.g., a callee allocated function and an unknown size constructor. */
3261 : : gcc_assert (ss != NULL);
3262 : :
3263 : 486326 : for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
3264 : : {
3265 : 301691 : gcc_assert (ss);
3266 : :
3267 : : /* Cross loop arrays are handled from within the most nested loop. */
3268 : 301691 : if (ss->nested_ss != NULL)
3269 : 4740 : continue;
3270 : :
3271 : 296951 : ss_info = ss->info;
3272 : 296951 : expr = ss_info->expr;
3273 : 296951 : info = &ss_info->data.array;
3274 : :
3275 : 296951 : switch (ss_info->type)
3276 : : {
3277 : 41275 : case GFC_SS_SCALAR:
3278 : : /* Scalar expression. Evaluate this now. This includes elemental
3279 : : dimension indices, but not array section bounds. */
3280 : 41275 : gfc_init_se (&se, NULL);
3281 : 41275 : gfc_conv_expr (&se, expr);
3282 : 41275 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3283 : :
3284 : 41275 : if (expr->ts.type != BT_CHARACTER
3285 : 41275 : && !gfc_is_alloc_class_scalar_function (expr))
3286 : : {
3287 : : /* Move the evaluation of scalar expressions outside the
3288 : : scalarization loop, except for WHERE assignments. */
3289 : 37615 : if (subscript)
3290 : 5882 : se.expr = convert(gfc_array_index_type, se.expr);
3291 : 37615 : if (!ss_info->where)
3292 : 37201 : se.expr = gfc_evaluate_now (se.expr, &outer_loop->pre);
3293 : 37615 : gfc_add_block_to_block (&outer_loop->pre, &se.post);
3294 : : }
3295 : : else
3296 : 3660 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3297 : :
3298 : 41275 : ss_info->data.scalar.value = se.expr;
3299 : 41275 : ss_info->string_length = se.string_length;
3300 : 41275 : break;
3301 : :
3302 : 5121 : case GFC_SS_REFERENCE:
3303 : : /* Scalar argument to elemental procedure. */
3304 : 5121 : gfc_init_se (&se, NULL);
3305 : 5121 : if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
3306 : 824 : gfc_conv_expr_reference (&se, expr);
3307 : : else
3308 : : {
3309 : : /* Evaluate the argument outside the loop and pass
3310 : : a reference to the value. */
3311 : 4297 : gfc_conv_expr (&se, expr);
3312 : : }
3313 : :
3314 : : /* Ensure that a pointer to the string is stored. */
3315 : 5121 : if (expr->ts.type == BT_CHARACTER)
3316 : 174 : gfc_conv_string_parameter (&se);
3317 : :
3318 : 5121 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3319 : 5121 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3320 : 5121 : if (gfc_is_class_scalar_expr (expr))
3321 : : /* This is necessary because the dynamic type will always be
3322 : : large than the declared type. In consequence, assigning
3323 : : the value to a temporary could segfault.
3324 : : OOP-TODO: see if this is generally correct or is the value
3325 : : has to be written to an allocated temporary, whose address
3326 : : is passed via ss_info. */
3327 : 48 : ss_info->data.scalar.value = se.expr;
3328 : : else
3329 : 5073 : ss_info->data.scalar.value = gfc_evaluate_now (se.expr,
3330 : : &outer_loop->pre);
3331 : :
3332 : 5121 : ss_info->string_length = se.string_length;
3333 : 5121 : break;
3334 : :
3335 : : case GFC_SS_SECTION:
3336 : : /* Add the expressions for scalar and vector subscripts. */
3337 : 2812560 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
3338 : 2636775 : if (info->subscript[n])
3339 : 6634 : gfc_add_loop_ss_code (loop, info->subscript[n], true, where);
3340 : :
3341 : 175785 : set_vector_loop_bounds (ss);
3342 : 175785 : break;
3343 : :
3344 : 752 : case GFC_SS_VECTOR:
3345 : : /* Get the vector's descriptor and store it in SS. */
3346 : 752 : gfc_init_se (&se, NULL);
3347 : 752 : gfc_conv_expr_descriptor (&se, expr);
3348 : 752 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3349 : 752 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3350 : 752 : info->descriptor = se.expr;
3351 : 752 : break;
3352 : :
3353 : 11333 : case GFC_SS_INTRINSIC:
3354 : 11333 : gfc_add_intrinsic_ss_code (loop, ss);
3355 : 11333 : break;
3356 : :
3357 : 9358 : case GFC_SS_FUNCTION:
3358 : 9358 : {
3359 : : /* Array function return value. We call the function and save its
3360 : : result in a temporary for use inside the loop. */
3361 : 9358 : gfc_init_se (&se, NULL);
3362 : 9358 : se.loop = loop;
3363 : 9358 : se.ss = ss;
3364 : 9358 : bool class_func = gfc_is_class_array_function (expr);
3365 : 9358 : if (class_func)
3366 : 183 : expr->must_finalize = 1;
3367 : 9358 : gfc_conv_expr (&se, expr);
3368 : 9358 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3369 : 9358 : if (class_func
3370 : 183 : && se.expr
3371 : 9541 : && GFC_CLASS_TYPE_P (TREE_TYPE (se.expr)))
3372 : : {
3373 : 183 : tree tmp = gfc_class_data_get (se.expr);
3374 : 183 : info->descriptor = tmp;
3375 : 183 : info->data = gfc_conv_descriptor_data_get (tmp);
3376 : 183 : info->offset = gfc_conv_descriptor_offset_get (tmp);
3377 : 366 : for (gfc_ss *s = ss; s; s = s->parent)
3378 : 378 : for (int n = 0; n < s->dimen; n++)
3379 : : {
3380 : 195 : int dim = s->dim[n];
3381 : 195 : tree tree_dim = gfc_rank_cst[dim];
3382 : :
3383 : 195 : tree start;
3384 : 195 : start = gfc_conv_descriptor_lbound_get (tmp, tree_dim);
3385 : 195 : start = gfc_evaluate_now (start, &outer_loop->pre);
3386 : 195 : info->start[dim] = start;
3387 : :
3388 : 195 : tree end;
3389 : 195 : end = gfc_conv_descriptor_ubound_get (tmp, tree_dim);
3390 : 195 : end = gfc_evaluate_now (end, &outer_loop->pre);
3391 : 195 : info->end[dim] = end;
3392 : :
3393 : 195 : tree stride;
3394 : 195 : stride = gfc_conv_descriptor_stride_get (tmp, tree_dim);
3395 : 195 : stride = gfc_evaluate_now (stride, &outer_loop->pre);
3396 : 195 : info->stride[dim] = stride;
3397 : : }
3398 : : }
3399 : 9358 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3400 : 9358 : gfc_add_block_to_block (&outer_loop->post, &se.finalblock);
3401 : 9358 : ss_info->string_length = se.string_length;
3402 : : }
3403 : 9358 : break;
3404 : :
3405 : 41129 : case GFC_SS_CONSTRUCTOR:
3406 : 41129 : if (expr->ts.type == BT_CHARACTER
3407 : 7820 : && ss_info->string_length == NULL
3408 : 7820 : && expr->ts.u.cl
3409 : 7820 : && expr->ts.u.cl->length
3410 : 7476 : && expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
3411 : : {
3412 : 7425 : gfc_init_se (&se, NULL);
3413 : 7425 : gfc_conv_expr_type (&se, expr->ts.u.cl->length,
3414 : : gfc_charlen_type_node);
3415 : 7425 : ss_info->string_length = se.expr;
3416 : 7425 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3417 : 7425 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3418 : : }
3419 : 41129 : trans_array_constructor (ss, where);
3420 : 41129 : break;
3421 : :
3422 : : case GFC_SS_TEMP:
3423 : : case GFC_SS_COMPONENT:
3424 : : /* Do nothing. These are handled elsewhere. */
3425 : : break;
3426 : :
3427 : 0 : default:
3428 : 0 : gcc_unreachable ();
3429 : : }
3430 : : }
3431 : :
3432 : 184635 : if (!subscript)
3433 : 181365 : for (nested_loop = loop->nested; nested_loop;
3434 : 3364 : nested_loop = nested_loop->next)
3435 : 3364 : gfc_add_loop_ss_code (nested_loop, nested_loop->ss, subscript, where);
3436 : : }
3437 : :
3438 : :
3439 : : /* Given an array descriptor expression DESCR and its data pointer DATA, decide
3440 : : whether to either save the data pointer to a variable and use the variable or
3441 : : use the data pointer expression directly without any intermediary variable.
3442 : : */
3443 : :
3444 : : static bool
3445 : 123770 : save_descriptor_data (tree descr, tree data)
3446 : : {
3447 : 123770 : return !(DECL_P (data)
3448 : 113686 : || (TREE_CODE (data) == ADDR_EXPR
3449 : 69132 : && DECL_P (TREE_OPERAND (data, 0)))
3450 : 47869 : || (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (descr))
3451 : 44210 : && TREE_CODE (descr) == COMPONENT_REF
3452 : 9098 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (descr, 0)))));
3453 : : }
3454 : :
3455 : :
3456 : : /* Type of the DATA argument passed to walk_tree by substitute_subexpr_in_expr
3457 : : and used by maybe_substitute_expr. */
3458 : :
3459 : : typedef struct
3460 : : {
3461 : : tree target, repl;
3462 : : }
3463 : : substitute_t;
3464 : :
3465 : :
3466 : : /* Check if the expression in *TP is equal to the substitution target provided
3467 : : in DATA->TARGET and replace it with DATA->REPL in that case. This is a
3468 : : callback function for use with walk_tree. */
3469 : :
3470 : : static tree
3471 : 20476 : maybe_substitute_expr (tree *tp, int *walk_subtree, void *data)
3472 : : {
3473 : 20476 : substitute_t *subst = (substitute_t *) data;
3474 : 20476 : if (*tp == subst->target)
3475 : : {
3476 : 4006 : *tp = subst->repl;
3477 : 4006 : *walk_subtree = 0;
3478 : : }
3479 : :
3480 : 20476 : return NULL_TREE;
3481 : : }
3482 : :
3483 : :
3484 : : /* Substitute in EXPR any occurence of TARGET with REPLACEMENT. */
3485 : :
3486 : : static void
3487 : 3831 : substitute_subexpr_in_expr (tree target, tree replacement, tree expr)
3488 : : {
3489 : 3831 : substitute_t subst;
3490 : 3831 : subst.target = target;
3491 : 3831 : subst.repl = replacement;
3492 : :
3493 : 3831 : walk_tree (&expr, maybe_substitute_expr, &subst, nullptr);
3494 : 3831 : }
3495 : :
3496 : :
3497 : : /* Save REF to a fresh variable in all of REPLACEMENT_ROOTS, appending extra
3498 : : code to CODE. Before returning, add REF to REPLACEMENT_ROOTS and clear
3499 : : REF. */
3500 : :
3501 : : static void
3502 : 3635 : save_ref (tree &code, tree &ref, vec<tree> &replacement_roots)
3503 : : {
3504 : 3635 : stmtblock_t tmp_block;
3505 : 3635 : gfc_init_block (&tmp_block);
3506 : 3635 : tree var = gfc_evaluate_now (ref, &tmp_block);
3507 : 3635 : gfc_add_expr_to_block (&tmp_block, code);
3508 : 3635 : code = gfc_finish_block (&tmp_block);
3509 : :
3510 : 3635 : unsigned i;
3511 : 3635 : tree repl_root;
3512 : 7466 : FOR_EACH_VEC_ELT (replacement_roots, i, repl_root)
3513 : 3831 : substitute_subexpr_in_expr (ref, var, repl_root);
3514 : :
3515 : 3635 : replacement_roots.safe_push (ref);
3516 : 3635 : ref = NULL_TREE;
3517 : 3635 : }
3518 : :
3519 : :
3520 : : /* Save the descriptor reference VALUE to storage pointed by DESC_PTR. Before
3521 : : that, try to factor subexpressions of VALUE to variables, adding extra code
3522 : : to BLOCK.
3523 : :
3524 : : The candidate references to factoring are dereferenced pointers because they
3525 : : are cheap to copy and array descriptors because they are often the base of
3526 : : multiple subreferences. */
3527 : :
3528 : : static void
3529 : 313305 : set_factored_descriptor_value (tree *desc_ptr, tree value, stmtblock_t *block)
3530 : : {
3531 : : /* As the reference is processed from outer to inner, variable definitions
3532 : : will be generated in reversed order, so can't be put directly in BLOCK.
3533 : : We use TMP_BLOCK instead. */
3534 : 313305 : tree accumulated_code = NULL_TREE;
3535 : :
3536 : : /* The current candidate to factoring. */
3537 : 313305 : tree saveable_ref = NULL_TREE;
3538 : :
3539 : : /* The root expressions in which we look for subexpressions to replace with
3540 : : variables. */
3541 : 313305 : auto_vec<tree> replacement_roots;
3542 : 313305 : replacement_roots.safe_push (value);
3543 : :
3544 : 313305 : tree data_ref = value;
3545 : 313305 : tree next_ref = NULL_TREE;
3546 : :
3547 : : /* If the candidate reference is not followed by a subreference, it can't be
3548 : : saved to a variable as it may be reallocatable, and we have to keep the
3549 : : parent reference to be able to store the new pointer value in case of
3550 : : reallocation. */
3551 : 313305 : bool maybe_reallocatable = true;
3552 : :
3553 : 408549 : while (true)
3554 : : {
3555 : 408549 : if (!maybe_reallocatable
3556 : 408549 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (data_ref)))
3557 : 1332 : saveable_ref = data_ref;
3558 : :
3559 : 408549 : if (TREE_CODE (data_ref) == INDIRECT_REF)
3560 : : {
3561 : 52702 : next_ref = TREE_OPERAND (data_ref, 0);
3562 : :
3563 : 52702 : if (!maybe_reallocatable)
3564 : : {
3565 : 13352 : if (saveable_ref != NULL_TREE && saveable_ref != data_ref)
3566 : : {
3567 : : /* A reference worth saving has been seen, and now the pointer
3568 : : to the current reference is also worth saving. If the
3569 : : previous reference to save wasn't the current one, do save
3570 : : it now. Otherwise drop it as we prefer saving the
3571 : : pointer. */
3572 : 1783 : save_ref (accumulated_code, saveable_ref, replacement_roots);
3573 : : }
3574 : :
3575 : : /* Don't evaluate the pointer to a variable yet; do it only if the
3576 : : variable would be significantly more simple than the reference
3577 : : it replaces. That is if the reference contains anything
3578 : : different from NOPs, COMPONENTs and DECLs. */
3579 : 13352 : saveable_ref = next_ref;
3580 : : }
3581 : : }
3582 : 355847 : else if (TREE_CODE (data_ref) == COMPONENT_REF)
3583 : : {
3584 : 35742 : maybe_reallocatable = false;
3585 : 35742 : next_ref = TREE_OPERAND (data_ref, 0);
3586 : : }
3587 : 320105 : else if (TREE_CODE (data_ref) == NOP_EXPR)
3588 : 2562 : next_ref = TREE_OPERAND (data_ref, 0);
3589 : : else
3590 : : {
3591 : 317543 : if (DECL_P (data_ref))
3592 : : break;
3593 : :
3594 : 5924 : if (TREE_CODE (data_ref) == ARRAY_REF)
3595 : : {
3596 : 4238 : maybe_reallocatable = false;
3597 : 4238 : next_ref = TREE_OPERAND (data_ref, 0);
3598 : : }
3599 : :
3600 : 5924 : if (saveable_ref != NULL_TREE)
3601 : : /* We have seen a reference worth saving. Do it now. */
3602 : 1852 : save_ref (accumulated_code, saveable_ref, replacement_roots);
3603 : :
3604 : 5924 : if (TREE_CODE (data_ref) != ARRAY_REF)
3605 : : break;
3606 : : }
3607 : :
3608 : : data_ref = next_ref;
3609 : : }
3610 : :
3611 : 313305 : *desc_ptr = value;
3612 : 313305 : gfc_add_expr_to_block (block, accumulated_code);
3613 : 313305 : }
3614 : :
3615 : :
3616 : : /* Translate expressions for the descriptor and data pointer of a SS. */
3617 : : /*GCC ARRAYS*/
3618 : :
3619 : : static void
3620 : 313305 : gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
3621 : : {
3622 : 313305 : gfc_se se;
3623 : 313305 : gfc_ss_info *ss_info;
3624 : 313305 : gfc_array_info *info;
3625 : 313305 : tree tmp;
3626 : :
3627 : 313305 : ss_info = ss->info;
3628 : 313305 : info = &ss_info->data.array;
3629 : :
3630 : : /* Get the descriptor for the array to be scalarized. */
3631 : 313305 : gcc_assert (ss_info->expr->expr_type == EXPR_VARIABLE);
3632 : 313305 : gfc_init_se (&se, NULL);
3633 : 313305 : se.descriptor_only = 1;
3634 : 313305 : gfc_conv_expr_lhs (&se, ss_info->expr);
3635 : 313305 : gfc_add_block_to_block (block, &se.pre);
3636 : 313305 : set_factored_descriptor_value (&info->descriptor, se.expr, block);
3637 : 313305 : ss_info->string_length = se.string_length;
3638 : 313305 : ss_info->class_container = se.class_container;
3639 : :
3640 : 313305 : if (base)
3641 : : {
3642 : 117913 : if (ss_info->expr->ts.type == BT_CHARACTER && !ss_info->expr->ts.deferred
3643 : 21971 : && ss_info->expr->ts.u.cl->length == NULL)
3644 : : {
3645 : : /* Emit a DECL_EXPR for the variable sized array type in
3646 : : GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
3647 : : sizes works correctly. */
3648 : 1085 : tree arraytype = TREE_TYPE (
3649 : : GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (info->descriptor)));
3650 : 1085 : if (! TYPE_NAME (arraytype))
3651 : 893 : TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
3652 : : NULL_TREE, arraytype);
3653 : 1085 : gfc_add_expr_to_block (block, build1 (DECL_EXPR, arraytype,
3654 : 1085 : TYPE_NAME (arraytype)));
3655 : : }
3656 : : /* Also the data pointer. */
3657 : 117913 : tmp = gfc_conv_array_data (se.expr);
3658 : : /* If this is a variable or address or a class array, use it directly.
3659 : : Otherwise we must evaluate it now to avoid breaking dependency
3660 : : analysis by pulling the expressions for elemental array indices
3661 : : inside the loop. */
3662 : 117913 : if (save_descriptor_data (se.expr, tmp) && !ss->is_alloc_lhs)
3663 : 34166 : tmp = gfc_evaluate_now (tmp, block);
3664 : 117913 : info->data = tmp;
3665 : :
3666 : 117913 : tmp = gfc_conv_array_offset (se.expr);
3667 : 117913 : if (!ss->is_alloc_lhs)
3668 : 112222 : tmp = gfc_evaluate_now (tmp, block);
3669 : 117913 : info->offset = tmp;
3670 : :
3671 : : /* Make absolutely sure that the saved_offset is indeed saved
3672 : : so that the variable is still accessible after the loops
3673 : : are translated. */
3674 : 117913 : info->saved_offset = info->offset;
3675 : : }
3676 : 313305 : }
3677 : :
3678 : :
3679 : : /* Initialize a gfc_loopinfo structure. */
3680 : :
3681 : : void
3682 : 183025 : gfc_init_loopinfo (gfc_loopinfo * loop)
3683 : : {
3684 : 183025 : int n;
3685 : :
3686 : 183025 : memset (loop, 0, sizeof (gfc_loopinfo));
3687 : 183025 : gfc_init_block (&loop->pre);
3688 : 183025 : gfc_init_block (&loop->post);
3689 : :
3690 : : /* Initially scalarize in order and default to no loop reversal. */
3691 : 3111425 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
3692 : : {
3693 : 2745375 : loop->order[n] = n;
3694 : 2745375 : loop->reverse[n] = GFC_INHIBIT_REVERSE;
3695 : : }
3696 : :
3697 : 183025 : loop->ss = gfc_ss_terminator;
3698 : 183025 : }
3699 : :
3700 : :
3701 : : /* Copies the loop variable info to a gfc_se structure. Does not copy the SS
3702 : : chain. */
3703 : :
3704 : : void
3705 : 183724 : gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
3706 : : {
3707 : 183724 : se->loop = loop;
3708 : 183724 : }
3709 : :
3710 : :
3711 : : /* Return an expression for the data pointer of an array. */
3712 : :
3713 : : tree
3714 : 315839 : gfc_conv_array_data (tree descriptor)
3715 : : {
3716 : 315839 : tree type;
3717 : :
3718 : 315839 : type = TREE_TYPE (descriptor);
3719 : 315839 : if (GFC_ARRAY_TYPE_P (type))
3720 : : {
3721 : 224625 : if (TREE_CODE (type) == POINTER_TYPE)
3722 : : return descriptor;
3723 : : else
3724 : : {
3725 : : /* Descriptorless arrays. */
3726 : 172115 : return gfc_build_addr_expr (NULL_TREE, descriptor);
3727 : : }
3728 : : }
3729 : : else
3730 : 91214 : return gfc_conv_descriptor_data_get (descriptor);
3731 : : }
3732 : :
3733 : :
3734 : : /* Return an expression for the base offset of an array. */
3735 : :
3736 : : tree
3737 : 235035 : gfc_conv_array_offset (tree descriptor)
3738 : : {
3739 : 235035 : tree type;
3740 : :
3741 : 235035 : type = TREE_TYPE (descriptor);
3742 : 235035 : if (GFC_ARRAY_TYPE_P (type))
3743 : 169378 : return GFC_TYPE_ARRAY_OFFSET (type);
3744 : : else
3745 : 65657 : return gfc_conv_descriptor_offset_get (descriptor);
3746 : : }
3747 : :
3748 : :
3749 : : /* Get an expression for the array stride. */
3750 : :
3751 : : tree
3752 : 476611 : gfc_conv_array_stride (tree descriptor, int dim)
3753 : : {
3754 : 476611 : tree tmp;
3755 : 476611 : tree type;
3756 : :
3757 : 476611 : type = TREE_TYPE (descriptor);
3758 : :
3759 : : /* For descriptorless arrays use the array size. */
3760 : 476611 : tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
3761 : 476611 : if (tmp != NULL_TREE)
3762 : : return tmp;
3763 : :
3764 : 106482 : tmp = gfc_conv_descriptor_stride_get (descriptor, gfc_rank_cst[dim]);
3765 : 106482 : return tmp;
3766 : : }
3767 : :
3768 : :
3769 : : /* Like gfc_conv_array_stride, but for the lower bound. */
3770 : :
3771 : : tree
3772 : 314610 : gfc_conv_array_lbound (tree descriptor, int dim)
3773 : : {
3774 : 314610 : tree tmp;
3775 : 314610 : tree type;
3776 : :
3777 : 314610 : type = TREE_TYPE (descriptor);
3778 : :
3779 : 314610 : tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
3780 : 314610 : if (tmp != NULL_TREE)
3781 : : return tmp;
3782 : :
3783 : 18350 : tmp = gfc_conv_descriptor_lbound_get (descriptor, gfc_rank_cst[dim]);
3784 : 18350 : return tmp;
3785 : : }
3786 : :
3787 : :
3788 : : /* Like gfc_conv_array_stride, but for the upper bound. */
3789 : :
3790 : : tree
3791 : 203980 : gfc_conv_array_ubound (tree descriptor, int dim)
3792 : : {
3793 : 203980 : tree tmp;
3794 : 203980 : tree type;
3795 : :
3796 : 203980 : type = TREE_TYPE (descriptor);
3797 : :
3798 : 203980 : tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
3799 : 203980 : if (tmp != NULL_TREE)
3800 : : return tmp;
3801 : :
3802 : : /* This should only ever happen when passing an assumed shape array
3803 : : as an actual parameter. The value will never be used. */
3804 : 7807 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
3805 : 553 : return gfc_index_zero_node;
3806 : :
3807 : 7254 : tmp = gfc_conv_descriptor_ubound_get (descriptor, gfc_rank_cst[dim]);
3808 : 7254 : return tmp;
3809 : : }
3810 : :
3811 : :
3812 : : /* Generate abridged name of a part-ref for use in bounds-check message.
3813 : : Cases:
3814 : : (1) for an ordinary array variable x return "x"
3815 : : (2) for z a DT scalar and array component x (at level 1) return "z%%x"
3816 : : (3) for z a DT scalar and array component x (at level > 1) or
3817 : : for z a DT array and array x (at any number of levels): "z...%%x"
3818 : : */
3819 : :
3820 : : static char *
3821 : 36147 : abridged_ref_name (gfc_expr * expr, gfc_array_ref * ar)
3822 : : {
3823 : 36147 : gfc_ref *ref;
3824 : 36147 : gfc_symbol *sym;
3825 : 36147 : char *ref_name = NULL;
3826 : 36147 : const char *comp_name = NULL;
3827 : 36147 : int len_sym, last_len = 0, level = 0;
3828 : 36147 : bool sym_is_array;
3829 : :
3830 : 36147 : gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->ref != NULL);
3831 : :
3832 : 36147 : sym = expr->symtree->n.sym;
3833 : 72027 : sym_is_array = (sym->ts.type != BT_CLASS
3834 : 36147 : ? sym->as != NULL
3835 : 267 : : IS_CLASS_ARRAY (sym));
3836 : 36147 : len_sym = strlen (sym->name);
3837 : :
3838 : : /* Scan ref chain to get name of the array component (when ar != NULL) or
3839 : : array section, determine depth and remember its component name. */
3840 : 51289 : for (ref = expr->ref; ref; ref = ref->next)
3841 : : {
3842 : 37272 : if (ref->type == REF_COMPONENT
3843 : 808 : && strcmp (ref->u.c.component->name, "_data") != 0)
3844 : : {
3845 : 678 : level++;
3846 : 678 : comp_name = ref->u.c.component->name;
3847 : 678 : continue;
3848 : : }
3849 : :
3850 : 36594 : if (ref->type != REF_ARRAY)
3851 : 150 : continue;
3852 : :
3853 : 36444 : if (ar)
3854 : : {
3855 : 15555 : if (&ref->u.ar == ar)
3856 : : break;
3857 : : }
3858 : 20889 : else if (ref->u.ar.type == AR_SECTION)
3859 : : break;
3860 : : }
3861 : :
3862 : 36147 : if (level > 0)
3863 : 644 : last_len = strlen (comp_name);
3864 : :
3865 : : /* Provide a buffer sufficiently large to hold "x...%%z". */
3866 : 36147 : ref_name = XNEWVEC (char, len_sym + last_len + 6);
3867 : 36147 : strcpy (ref_name, sym->name);
3868 : :
3869 : 36147 : if (level == 1 && !sym_is_array)
3870 : : {
3871 : 352 : strcat (ref_name, "%%");
3872 : 352 : strcat (ref_name, comp_name);
3873 : : }
3874 : 35795 : else if (level > 0)
3875 : : {
3876 : 292 : strcat (ref_name, "...%%");
3877 : 292 : strcat (ref_name, comp_name);
3878 : : }
3879 : :
3880 : 36147 : return ref_name;
3881 : : }
3882 : :
3883 : :
3884 : : /* Generate code to perform an array index bound check. */
3885 : :
3886 : : static tree
3887 : 4998 : trans_array_bound_check (gfc_se * se, gfc_ss *ss, tree index, int n,
3888 : : locus * where, bool check_upper,
3889 : : const char *compname = NULL)
3890 : : {
3891 : 4998 : tree fault;
3892 : 4998 : tree tmp_lo, tmp_up;
3893 : 4998 : tree descriptor;
3894 : 4998 : char *msg;
3895 : 4998 : char *ref_name = NULL;
3896 : 4998 : const char * name = NULL;
3897 : 4998 : gfc_expr *expr;
3898 : :
3899 : 4998 : if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
3900 : : return index;
3901 : :
3902 : 234 : descriptor = ss->info->data.array.descriptor;
3903 : :
3904 : 234 : index = gfc_evaluate_now (index, &se->pre);
3905 : :
3906 : : /* We find a name for the error message. */
3907 : 234 : name = ss->info->expr->symtree->n.sym->name;
3908 : 234 : gcc_assert (name != NULL);
3909 : :
3910 : : /* When we have a component ref, get name of the array section.
3911 : : Note that there can only be one part ref. */
3912 : 234 : expr = ss->info->expr;
3913 : 234 : if (expr->ref && !compname)
3914 : 160 : name = ref_name = abridged_ref_name (expr, NULL);
3915 : :
3916 : 234 : if (VAR_P (descriptor))
3917 : 156 : name = IDENTIFIER_POINTER (DECL_NAME (descriptor));
3918 : :
3919 : : /* Use given (array component) name. */
3920 : 234 : if (compname)
3921 : 74 : name = compname;
3922 : :
3923 : : /* If upper bound is present, include both bounds in the error message. */
3924 : 234 : if (check_upper)
3925 : : {
3926 : 207 : tmp_lo = gfc_conv_array_lbound (descriptor, n);
3927 : 207 : tmp_up = gfc_conv_array_ubound (descriptor, n);
3928 : :
3929 : 207 : if (name)
3930 : 207 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3931 : : "outside of expected range (%%ld:%%ld)", n+1, name);
3932 : : else
3933 : 0 : msg = xasprintf ("Index '%%ld' of dimension %d "
3934 : : "outside of expected range (%%ld:%%ld)", n+1);
3935 : :
3936 : 207 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
3937 : : index, tmp_lo);
3938 : 207 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3939 : : fold_convert (long_integer_type_node, index),
3940 : : fold_convert (long_integer_type_node, tmp_lo),
3941 : : fold_convert (long_integer_type_node, tmp_up));
3942 : 207 : fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3943 : : index, tmp_up);
3944 : 207 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3945 : : fold_convert (long_integer_type_node, index),
3946 : : fold_convert (long_integer_type_node, tmp_lo),
3947 : : fold_convert (long_integer_type_node, tmp_up));
3948 : 207 : free (msg);
3949 : : }
3950 : : else
3951 : : {
3952 : 27 : tmp_lo = gfc_conv_array_lbound (descriptor, n);
3953 : :
3954 : 27 : if (name)
3955 : 27 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3956 : : "below lower bound of %%ld", n+1, name);
3957 : : else
3958 : 0 : msg = xasprintf ("Index '%%ld' of dimension %d "
3959 : : "below lower bound of %%ld", n+1);
3960 : :
3961 : 27 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
3962 : : index, tmp_lo);
3963 : 27 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3964 : : fold_convert (long_integer_type_node, index),
3965 : : fold_convert (long_integer_type_node, tmp_lo));
3966 : 27 : free (msg);
3967 : : }
3968 : :
3969 : 234 : free (ref_name);
3970 : 234 : return index;
3971 : : }
3972 : :
3973 : :
3974 : : /* Generate code for bounds checking for elemental dimensions. */
3975 : :
3976 : : static void
3977 : 6667 : array_bound_check_elemental (gfc_se * se, gfc_ss * ss, gfc_expr * expr)
3978 : : {
3979 : 6667 : gfc_array_ref *ar;
3980 : 6667 : gfc_ref *ref;
3981 : 6667 : char *var_name = NULL;
3982 : 6667 : int dim;
3983 : :
3984 : 6667 : if (expr->expr_type == EXPR_VARIABLE)
3985 : : {
3986 : 12469 : for (ref = expr->ref; ref; ref = ref->next)
3987 : : {
3988 : 6259 : if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3989 : : {
3990 : 3935 : ar = &ref->u.ar;
3991 : 3935 : var_name = abridged_ref_name (expr, ar);
3992 : 8104 : for (dim = 0; dim < ar->dimen; dim++)
3993 : : {
3994 : 4169 : if (ar->dimen_type[dim] == DIMEN_ELEMENT)
3995 : : {
3996 : 74 : gfc_se indexse;
3997 : 74 : gfc_init_se (&indexse, NULL);
3998 : 74 : gfc_conv_expr_type (&indexse, ar->start[dim],
3999 : : gfc_array_index_type);
4000 : 74 : trans_array_bound_check (se, ss, indexse.expr, dim,
4001 : : &ar->where,
4002 : 74 : ar->as->type != AS_ASSUMED_SIZE
4003 : 74 : || dim < ar->dimen - 1,
4004 : : var_name);
4005 : : }
4006 : : }
4007 : 3935 : free (var_name);
4008 : : }
4009 : : }
4010 : : }
4011 : 6667 : }
4012 : :
4013 : :
4014 : : /* Return the offset for an index. Performs bound checking for elemental
4015 : : dimensions. Single element references are processed separately.
4016 : : DIM is the array dimension, I is the loop dimension. */
4017 : :
4018 : : static tree
4019 : 244977 : conv_array_index_offset (gfc_se * se, gfc_ss * ss, int dim, int i,
4020 : : gfc_array_ref * ar, tree stride)
4021 : : {
4022 : 244977 : gfc_array_info *info;
4023 : 244977 : tree index;
4024 : 244977 : tree desc;
4025 : 244977 : tree data;
4026 : :
4027 : 244977 : info = &ss->info->data.array;
4028 : :
4029 : : /* Get the index into the array for this dimension. */
4030 : 244977 : if (ar)
4031 : : {
4032 : 173796 : gcc_assert (ar->type != AR_ELEMENT);
4033 : 173796 : switch (ar->dimen_type[dim])
4034 : : {
4035 : 0 : case DIMEN_THIS_IMAGE:
4036 : 0 : gcc_unreachable ();
4037 : 4175 : break;
4038 : 4175 : case DIMEN_ELEMENT:
4039 : : /* Elemental dimension. */
4040 : 4175 : gcc_assert (info->subscript[dim]
4041 : : && info->subscript[dim]->info->type == GFC_SS_SCALAR);
4042 : : /* We've already translated this value outside the loop. */
4043 : 4175 : index = info->subscript[dim]->info->data.scalar.value;
4044 : :
4045 : 8350 : index = trans_array_bound_check (se, ss, index, dim, &ar->where,
4046 : 4175 : ar->as->type != AS_ASSUMED_SIZE
4047 : 4175 : || dim < ar->dimen - 1);
4048 : 4175 : break;
4049 : :
4050 : 749 : case DIMEN_VECTOR:
4051 : 749 : gcc_assert (info && se->loop);
4052 : 749 : gcc_assert (info->subscript[dim]
4053 : : && info->subscript[dim]->info->type == GFC_SS_VECTOR);
4054 : 749 : desc = info->subscript[dim]->info->data.array.descriptor;
4055 : :
4056 : : /* Get a zero-based index into the vector. */
4057 : 749 : index = fold_build2_loc (input_location, MINUS_EXPR,
4058 : : gfc_array_index_type,
4059 : : se->loop->loopvar[i], se->loop->from[i]);
4060 : :
4061 : : /* Multiply the index by the stride. */
4062 : 749 : index = fold_build2_loc (input_location, MULT_EXPR,
4063 : : gfc_array_index_type,
4064 : : index, gfc_conv_array_stride (desc, 0));
4065 : :
4066 : : /* Read the vector to get an index into info->descriptor. */
4067 : 749 : data = build_fold_indirect_ref_loc (input_location,
4068 : : gfc_conv_array_data (desc));
4069 : 749 : index = gfc_build_array_ref (data, index, NULL);
4070 : 749 : index = gfc_evaluate_now (index, &se->pre);
4071 : 749 : index = fold_convert (gfc_array_index_type, index);
4072 : :
4073 : : /* Do any bounds checking on the final info->descriptor index. */
4074 : 1498 : index = trans_array_bound_check (se, ss, index, dim, &ar->where,
4075 : 749 : ar->as->type != AS_ASSUMED_SIZE
4076 : 749 : || dim < ar->dimen - 1);
4077 : 749 : break;
4078 : :
4079 : 168872 : case DIMEN_RANGE:
4080 : : /* Scalarized dimension. */
4081 : 168872 : gcc_assert (info && se->loop);
4082 : :
4083 : : /* Multiply the loop variable by the stride and delta. */
4084 : 168872 : index = se->loop->loopvar[i];
4085 : 168872 : if (!integer_onep (info->stride[dim]))
4086 : 6457 : index = fold_build2_loc (input_location, MULT_EXPR,
4087 : : gfc_array_index_type, index,
4088 : : info->stride[dim]);
4089 : 168872 : if (!integer_zerop (info->delta[dim]))
4090 : 63637 : index = fold_build2_loc (input_location, PLUS_EXPR,
4091 : : gfc_array_index_type, index,
4092 : : info->delta[dim]);
4093 : : break;
4094 : :
4095 : 0 : default:
4096 : 0 : gcc_unreachable ();
4097 : : }
4098 : : }
4099 : : else
4100 : : {
4101 : : /* Temporary array or derived type component. */
4102 : 71181 : gcc_assert (se->loop);
4103 : 71181 : index = se->loop->loopvar[se->loop->order[i]];
4104 : :
4105 : : /* Pointer functions can have stride[0] different from unity.
4106 : : Use the stride returned by the function call and stored in
4107 : : the descriptor for the temporary. */
4108 : 71181 : if (se->ss && se->ss->info->type == GFC_SS_FUNCTION
4109 : 7942 : && se->ss->info->expr
4110 : 7942 : && se->ss->info->expr->symtree
4111 : 7942 : && se->ss->info->expr->symtree->n.sym->result
4112 : 7529 : && se->ss->info->expr->symtree->n.sym->result->attr.pointer)
4113 : 144 : stride = gfc_conv_descriptor_stride_get (info->descriptor,
4114 : : gfc_rank_cst[dim]);
4115 : :
4116 : 71181 : if (info->delta[dim] && !integer_zerop (info->delta[dim]))
4117 : 788 : index = fold_build2_loc (input_location, PLUS_EXPR,
4118 : : gfc_array_index_type, index, info->delta[dim]);
4119 : : }
4120 : :
4121 : : /* Multiply by the stride. */
4122 : 244977 : if (stride != NULL && !integer_onep (stride))
4123 : 74202 : index = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4124 : : index, stride);
4125 : :
4126 : 244977 : return index;
4127 : : }
4128 : :
4129 : :
4130 : : /* Build a scalarized array reference using the vptr 'size'. */
4131 : :
4132 : : static bool
4133 : 187487 : build_class_array_ref (gfc_se *se, tree base, tree index)
4134 : : {
4135 : 187487 : tree size;
4136 : 187487 : tree decl = NULL_TREE;
4137 : 187487 : tree tmp;
4138 : 187487 : gfc_expr *expr = se->ss->info->expr;
4139 : 187487 : gfc_expr *class_expr;
4140 : 187487 : gfc_typespec *ts;
4141 : 187487 : gfc_symbol *sym;
4142 : :
4143 : 187487 : tmp = !VAR_P (base) ? gfc_get_class_from_expr (base) : NULL_TREE;
4144 : :
4145 : 86875 : if (tmp != NULL_TREE)
4146 : : decl = tmp;
4147 : : else
4148 : : {
4149 : : /* The base expression does not contain a class component, either
4150 : : because it is a temporary array or array descriptor. Class
4151 : : array functions are correctly resolved above. */
4152 : 184283 : if (!expr
4153 : 184283 : || (expr->ts.type != BT_CLASS
4154 : 170856 : && !gfc_is_class_array_ref (expr, NULL)))
4155 : 183896 : return false;
4156 : :
4157 : : /* Obtain the expression for the class entity or component that is
4158 : : followed by an array reference, which is not an element, so that
4159 : : the span of the array can be obtained. */
4160 : 387 : class_expr = gfc_find_and_cut_at_last_class_ref (expr, false, &ts);
4161 : :
4162 : 387 : if (!ts)
4163 : : return false;
4164 : :
4165 : 362 : sym = (!class_expr && expr) ? expr->symtree->n.sym : NULL;
4166 : 0 : if (sym && sym->attr.function
4167 : 0 : && sym == sym->result
4168 : 0 : && sym->backend_decl == current_function_decl)
4169 : : /* The temporary is the data field of the class data component
4170 : : of the current function. */
4171 : 0 : decl = gfc_get_fake_result_decl (sym, 0);
4172 : 362 : else if (sym)
4173 : : {
4174 : 0 : if (decl == NULL_TREE)
4175 : 0 : decl = expr->symtree->n.sym->backend_decl;
4176 : : /* For class arrays the tree containing the class is stored in
4177 : : GFC_DECL_SAVED_DESCRIPTOR of the sym's backend_decl.
4178 : : For all others it's sym's backend_decl directly. */
4179 : 0 : if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
4180 : 0 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
4181 : : }
4182 : : else
4183 : 362 : decl = gfc_get_class_from_gfc_expr (class_expr);
4184 : :
4185 : 362 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
4186 : 0 : decl = build_fold_indirect_ref_loc (input_location, decl);
4187 : :
4188 : 362 : if (!GFC_CLASS_TYPE_P (TREE_TYPE (decl)))
4189 : : return false;
4190 : : }
4191 : :
4192 : 3566 : se->class_vptr = gfc_evaluate_now (gfc_class_vptr_get (decl), &se->pre);
4193 : :
4194 : 3566 : size = gfc_class_vtab_size_get (decl);
4195 : : /* For unlimited polymorphic entities then _len component needs to be
4196 : : multiplied with the size. */
4197 : 3566 : size = gfc_resize_class_size_with_len (&se->pre, decl, size);
4198 : 3566 : size = fold_convert (TREE_TYPE (index), size);
4199 : :
4200 : : /* Return the element in the se expression. */
4201 : 3566 : se->expr = gfc_build_spanned_array_ref (base, index, size);
4202 : 3566 : return true;
4203 : : }
4204 : :
4205 : :
4206 : : /* Indicates that the tree EXPR is a reference to an array that can’t
4207 : : have any negative stride. */
4208 : :
4209 : : static bool
4210 : 298689 : non_negative_strides_array_p (tree expr)
4211 : : {
4212 : 311214 : if (expr == NULL_TREE)
4213 : : return false;
4214 : :
4215 : 311214 : tree type = TREE_TYPE (expr);
4216 : 311214 : if (POINTER_TYPE_P (type))
4217 : 65256 : type = TREE_TYPE (type);
4218 : :
4219 : 311214 : if (TYPE_LANG_SPECIFIC (type))
4220 : : {
4221 : 311214 : gfc_array_kind array_kind = GFC_TYPE_ARRAY_AKIND (type);
4222 : :
4223 : 311214 : if (array_kind == GFC_ARRAY_ALLOCATABLE
4224 : 311214 : || array_kind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
4225 : : return true;
4226 : : }
4227 : :
4228 : : /* An array with descriptor can have negative strides.
4229 : : We try to be conservative and return false by default here
4230 : : if we don’t recognize a contiguous array instead of
4231 : : returning false if we can identify a non-contiguous one. */
4232 : 258589 : if (!GFC_ARRAY_TYPE_P (type))
4233 : : return false;
4234 : :
4235 : : /* If the array was originally a dummy with a descriptor, strides can be
4236 : : negative. */
4237 : 226527 : if (DECL_P (expr)
4238 : 217611 : && DECL_LANG_SPECIFIC (expr)
4239 : 45730 : && GFC_DECL_SAVED_DESCRIPTOR (expr)
4240 : 239071 : && GFC_DECL_SAVED_DESCRIPTOR (expr) != expr)
4241 : 12525 : return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (expr));
4242 : :
4243 : : return true;
4244 : : }
4245 : :
4246 : :
4247 : : /* Build a scalarized reference to an array. */
4248 : :
4249 : : static void
4250 : 187487 : gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar,
4251 : : bool tmp_array = false)
4252 : : {
4253 : 187487 : gfc_array_info *info;
4254 : 187487 : tree decl = NULL_TREE;
4255 : 187487 : tree index;
4256 : 187487 : tree base;
4257 : 187487 : gfc_ss *ss;
4258 : 187487 : gfc_expr *expr;
4259 : 187487 : int n;
4260 : :
4261 : 187487 : ss = se->ss;
4262 : 187487 : expr = ss->info->expr;
4263 : 187487 : info = &ss->info->data.array;
4264 : 187487 : if (ar)
4265 : 127968 : n = se->loop->order[0];
4266 : : else
4267 : : n = 0;
4268 : :
4269 : 187487 : index = conv_array_index_offset (se, ss, ss->dim[n], n, ar, info->stride0);
4270 : : /* Add the offset for this dimension to the stored offset for all other
4271 : : dimensions. */
4272 : 187487 : if (info->offset && !integer_zerop (info->offset))
4273 : 137169 : index = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4274 : : index, info->offset);
4275 : :
4276 : 187487 : base = build_fold_indirect_ref_loc (input_location, info->data);
4277 : :
4278 : : /* Use the vptr 'size' field to access the element of a class array. */
4279 : 187487 : if (build_class_array_ref (se, base, index))
4280 : 3566 : return;
4281 : :
4282 : 183921 : if (get_CFI_desc (NULL, expr, &decl, ar))
4283 : 442 : decl = build_fold_indirect_ref_loc (input_location, decl);
4284 : :
4285 : : /* A pointer array component can be detected from its field decl. Fix
4286 : : the descriptor, mark the resulting variable decl and pass it to
4287 : : gfc_build_array_ref. */
4288 : 183921 : if (is_pointer_array (info->descriptor)
4289 : 183921 : || (expr && expr->ts.deferred && info->descriptor
4290 : 2527 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (info->descriptor))))
4291 : : {
4292 : 8390 : if (TREE_CODE (info->descriptor) == COMPONENT_REF)
4293 : 1468 : decl = info->descriptor;
4294 : 6922 : else if (INDIRECT_REF_P (info->descriptor))
4295 : 1427 : decl = TREE_OPERAND (info->descriptor, 0);
4296 : :
4297 : 8390 : if (decl == NULL_TREE)
4298 : 5495 : decl = info->descriptor;
4299 : : }
4300 : :
4301 : 183921 : bool non_negative_stride = tmp_array
4302 : 183921 : || non_negative_strides_array_p (info->descriptor);
4303 : 183921 : se->expr = gfc_build_array_ref (base, index, decl,
4304 : : non_negative_stride);
4305 : : }
4306 : :
4307 : :
4308 : : /* Translate access of temporary array. */
4309 : :
4310 : : void
4311 : 59519 : gfc_conv_tmp_array_ref (gfc_se * se)
4312 : : {
4313 : 59519 : se->string_length = se->ss->info->string_length;
4314 : 59519 : gfc_conv_scalarized_array_ref (se, NULL, true);
4315 : 59519 : gfc_advance_se_ss_chain (se);
4316 : 59519 : }
4317 : :
4318 : : /* Add T to the offset pair *OFFSET, *CST_OFFSET. */
4319 : :
4320 : : static void
4321 : 258695 : add_to_offset (tree *cst_offset, tree *offset, tree t)
4322 : : {
4323 : 258695 : if (TREE_CODE (t) == INTEGER_CST)
4324 : 129897 : *cst_offset = int_const_binop (PLUS_EXPR, *cst_offset, t);
4325 : : else
4326 : : {
4327 : 128798 : if (!integer_zerop (*offset))
4328 : 46844 : *offset = fold_build2_loc (input_location, PLUS_EXPR,
4329 : : gfc_array_index_type, *offset, t);
4330 : : else
4331 : 81954 : *offset = t;
4332 : : }
4333 : 258695 : }
4334 : :
4335 : :
4336 : : static tree
4337 : 173899 : build_array_ref (tree desc, tree offset, tree decl, tree vptr)
4338 : : {
4339 : 173899 : tree tmp;
4340 : 173899 : tree type;
4341 : 173899 : tree cdesc;
4342 : :
4343 : : /* For class arrays the class declaration is stored in the saved
4344 : : descriptor. */
4345 : 173899 : if (INDIRECT_REF_P (desc)
4346 : 7265 : && DECL_LANG_SPECIFIC (TREE_OPERAND (desc, 0))
4347 : 176163 : && GFC_DECL_SAVED_DESCRIPTOR (TREE_OPERAND (desc, 0)))
4348 : 834 : cdesc = gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (
4349 : : TREE_OPERAND (desc, 0)));
4350 : : else
4351 : : cdesc = desc;
4352 : :
4353 : : /* Class container types do not always have the GFC_CLASS_TYPE_P
4354 : : but the canonical type does. */
4355 : 173899 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (cdesc))
4356 : 173899 : && TREE_CODE (cdesc) == COMPONENT_REF)
4357 : : {
4358 : 9249 : type = TREE_TYPE (TREE_OPERAND (cdesc, 0));
4359 : 9249 : if (TYPE_CANONICAL (type)
4360 : 9249 : && GFC_CLASS_TYPE_P (TYPE_CANONICAL (type)))
4361 : 3435 : vptr = gfc_class_vptr_get (TREE_OPERAND (cdesc, 0));
4362 : : }
4363 : :
4364 : 173899 : tmp = gfc_conv_array_data (desc);
4365 : 173899 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
4366 : 173899 : tmp = gfc_build_array_ref (tmp, offset, decl,
4367 : 173899 : non_negative_strides_array_p (desc),
4368 : : vptr);
4369 : 173899 : return tmp;
4370 : : }
4371 : :
4372 : :
4373 : : /* Build an array reference. se->expr already holds the array descriptor.
4374 : : This should be either a variable, indirect variable reference or component
4375 : : reference. For arrays which do not have a descriptor, se->expr will be
4376 : : the data pointer.
4377 : : a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
4378 : :
4379 : : void
4380 : 247732 : gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_expr *expr,
4381 : : locus * where)
4382 : : {
4383 : 247732 : int n;
4384 : 247732 : tree offset, cst_offset;
4385 : 247732 : tree tmp;
4386 : 247732 : tree stride;
4387 : 247732 : tree decl = NULL_TREE;
4388 : 247732 : gfc_se indexse;
4389 : 247732 : gfc_se tmpse;
4390 : 247732 : gfc_symbol * sym = expr->symtree->n.sym;
4391 : 247732 : char *var_name = NULL;
4392 : :
4393 : 247732 : if (ar->stat)
4394 : : {
4395 : 3 : gfc_se statse;
4396 : :
4397 : 3 : gfc_init_se (&statse, NULL);
4398 : 3 : gfc_conv_expr_lhs (&statse, ar->stat);
4399 : 3 : gfc_add_block_to_block (&se->pre, &statse.pre);
4400 : 3 : gfc_add_modify (&se->pre, statse.expr, integer_zero_node);
4401 : : }
4402 : 247732 : if (ar->dimen == 0)
4403 : : {
4404 : 3677 : gcc_assert (ar->codimen || sym->attr.select_rank_temporary
4405 : : || (ar->as && ar->as->corank));
4406 : :
4407 : 3677 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
4408 : 812 : se->expr = build_fold_indirect_ref (gfc_conv_array_data (se->expr));
4409 : : else
4410 : : {
4411 : 2865 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr))
4412 : 2865 : && TREE_CODE (TREE_TYPE (se->expr)) == POINTER_TYPE)
4413 : 1935 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
4414 : :
4415 : : /* Use the actual tree type and not the wrapped coarray. */
4416 : 2865 : if (!se->want_pointer)
4417 : 2042 : se->expr = fold_convert (TYPE_MAIN_VARIANT (TREE_TYPE (se->expr)),
4418 : : se->expr);
4419 : : }
4420 : :
4421 : 131645 : return;
4422 : : }
4423 : :
4424 : : /* Handle scalarized references separately. */
4425 : 244055 : if (ar->type != AR_ELEMENT)
4426 : : {
4427 : 127968 : gfc_conv_scalarized_array_ref (se, ar);
4428 : 127968 : gfc_advance_se_ss_chain (se);
4429 : 127968 : return;
4430 : : }
4431 : :
4432 : 116087 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
4433 : 11493 : var_name = abridged_ref_name (expr, ar);
4434 : :
4435 : 116087 : decl = se->expr;
4436 : 116087 : if (UNLIMITED_POLY(sym)
4437 : 104 : && IS_CLASS_ARRAY (sym)
4438 : 103 : && sym->attr.dummy
4439 : 60 : && ar->as->type != AS_DEFERRED)
4440 : 48 : decl = sym->backend_decl;
4441 : :
4442 : 116087 : cst_offset = offset = gfc_index_zero_node;
4443 : 116087 : add_to_offset (&cst_offset, &offset, gfc_conv_array_offset (decl));
4444 : :
4445 : : /* Calculate the offsets from all the dimensions. Make sure to associate
4446 : : the final offset so that we form a chain of loop invariant summands. */
4447 : 258695 : for (n = ar->dimen - 1; n >= 0; n--)
4448 : : {
4449 : : /* Calculate the index for this dimension. */
4450 : 142608 : gfc_init_se (&indexse, se);
4451 : 142608 : gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
4452 : 142608 : gfc_add_block_to_block (&se->pre, &indexse.pre);
4453 : :
4454 : 142608 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && ! expr->no_bounds_check)
4455 : : {
4456 : : /* Check array bounds. */
4457 : 15035 : tree cond;
4458 : 15035 : char *msg;
4459 : :
4460 : : /* Evaluate the indexse.expr only once. */
4461 : 15035 : indexse.expr = save_expr (indexse.expr);
4462 : :
4463 : : /* Lower bound. */
4464 : 15035 : tmp = gfc_conv_array_lbound (decl, n);
4465 : 15035 : if (sym->attr.temporary)
4466 : : {
4467 : 18 : gfc_init_se (&tmpse, se);
4468 : 18 : gfc_conv_expr_type (&tmpse, ar->as->lower[n],
4469 : : gfc_array_index_type);
4470 : 18 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
4471 : 18 : tmp = tmpse.expr;
4472 : : }
4473 : :
4474 : 15035 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
4475 : : indexse.expr, tmp);
4476 : 15035 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4477 : : "below lower bound of %%ld", n+1, var_name);
4478 : 15035 : gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
4479 : : fold_convert (long_integer_type_node,
4480 : : indexse.expr),
4481 : : fold_convert (long_integer_type_node, tmp));
4482 : 15035 : free (msg);
4483 : :
4484 : : /* Upper bound, but not for the last dimension of assumed-size
4485 : : arrays. */
4486 : 15035 : if (n < ar->dimen - 1 || ar->as->type != AS_ASSUMED_SIZE)
4487 : : {
4488 : 13302 : tmp = gfc_conv_array_ubound (decl, n);
4489 : 13302 : if (sym->attr.temporary)
4490 : : {
4491 : 18 : gfc_init_se (&tmpse, se);
4492 : 18 : gfc_conv_expr_type (&tmpse, ar->as->upper[n],
4493 : : gfc_array_index_type);
4494 : 18 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
4495 : 18 : tmp = tmpse.expr;
4496 : : }
4497 : :
4498 : 13302 : cond = fold_build2_loc (input_location, GT_EXPR,
4499 : : logical_type_node, indexse.expr, tmp);
4500 : 13302 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4501 : : "above upper bound of %%ld", n+1, var_name);
4502 : 13302 : gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
4503 : : fold_convert (long_integer_type_node,
4504 : : indexse.expr),
4505 : : fold_convert (long_integer_type_node, tmp));
4506 : 13302 : free (msg);
4507 : : }
4508 : : }
4509 : :
4510 : : /* Multiply the index by the stride. */
4511 : 142608 : stride = gfc_conv_array_stride (decl, n);
4512 : 142608 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4513 : : indexse.expr, stride);
4514 : :
4515 : : /* And add it to the total. */
4516 : 142608 : add_to_offset (&cst_offset, &offset, tmp);
4517 : : }
4518 : :
4519 : 116087 : if (!integer_zerop (cst_offset))
4520 : 62140 : offset = fold_build2_loc (input_location, PLUS_EXPR,
4521 : : gfc_array_index_type, offset, cst_offset);
4522 : :
4523 : : /* A pointer array component can be detected from its field decl. Fix
4524 : : the descriptor, mark the resulting variable decl and pass it to
4525 : : build_array_ref. */
4526 : 116087 : decl = NULL_TREE;
4527 : 116087 : if (get_CFI_desc (sym, expr, &decl, ar))
4528 : 3589 : decl = build_fold_indirect_ref_loc (input_location, decl);
4529 : 115327 : if (!expr->ts.deferred && !sym->attr.codimension
4530 : 229712 : && is_pointer_array (se->expr))
4531 : : {
4532 : 4879 : if (TREE_CODE (se->expr) == COMPONENT_REF)
4533 : 1454 : decl = se->expr;
4534 : 3425 : else if (INDIRECT_REF_P (se->expr))
4535 : 983 : decl = TREE_OPERAND (se->expr, 0);
4536 : : else
4537 : 2442 : decl = se->expr;
4538 : : }
4539 : 111208 : else if (expr->ts.deferred
4540 : 110448 : || (sym->ts.type == BT_CHARACTER
4541 : 14510 : && sym->attr.select_type_temporary))
4542 : : {
4543 : 2464 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
4544 : : {
4545 : 2428 : decl = se->expr;
4546 : 2428 : if (INDIRECT_REF_P (decl))
4547 : 20 : decl = TREE_OPERAND (decl, 0);
4548 : : }
4549 : : else
4550 : 36 : decl = sym->backend_decl;
4551 : : }
4552 : 108744 : else if (sym->ts.type == BT_CLASS)
4553 : : {
4554 : 2078 : if (UNLIMITED_POLY (sym))
4555 : : {
4556 : 104 : gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
4557 : 104 : gfc_init_se (&tmpse, NULL);
4558 : 104 : gfc_conv_expr (&tmpse, class_expr);
4559 : 104 : if (!se->class_vptr)
4560 : 104 : se->class_vptr = gfc_class_vptr_get (tmpse.expr);
4561 : 104 : gfc_free_expr (class_expr);
4562 : 104 : decl = tmpse.expr;
4563 : 104 : }
4564 : : else
4565 : 1974 : decl = NULL_TREE;
4566 : : }
4567 : :
4568 : 116087 : free (var_name);
4569 : 116087 : se->expr = build_array_ref (se->expr, offset, decl, se->class_vptr);
4570 : : }
4571 : :
4572 : :
4573 : : /* Add the offset corresponding to array's ARRAY_DIM dimension and loop's
4574 : : LOOP_DIM dimension (if any) to array's offset. */
4575 : :
4576 : : static void
4577 : 57490 : add_array_offset (stmtblock_t *pblock, gfc_loopinfo *loop, gfc_ss *ss,
4578 : : gfc_array_ref *ar, int array_dim, int loop_dim)
4579 : : {
4580 : 57490 : gfc_se se;
4581 : 57490 : gfc_array_info *info;
4582 : 57490 : tree stride, index;
4583 : :
4584 : 57490 : info = &ss->info->data.array;
4585 : :
4586 : 57490 : gfc_init_se (&se, NULL);
4587 : 57490 : se.loop = loop;
4588 : 57490 : se.expr = info->descriptor;
4589 : 57490 : stride = gfc_conv_array_stride (info->descriptor, array_dim);
4590 : 57490 : index = conv_array_index_offset (&se, ss, array_dim, loop_dim, ar, stride);
4591 : 57490 : gfc_add_block_to_block (pblock, &se.pre);
4592 : :
4593 : 57490 : info->offset = fold_build2_loc (input_location, PLUS_EXPR,
4594 : : gfc_array_index_type,
4595 : : info->offset, index);
4596 : 57490 : info->offset = gfc_evaluate_now (info->offset, pblock);
4597 : 57490 : }
4598 : :
4599 : :
4600 : : /* Generate the code to be executed immediately before entering a
4601 : : scalarization loop. */
4602 : :
4603 : : static void
4604 : 141929 : gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
4605 : : stmtblock_t * pblock)
4606 : : {
4607 : 141929 : tree stride;
4608 : 141929 : gfc_ss_info *ss_info;
4609 : 141929 : gfc_array_info *info;
4610 : 141929 : gfc_ss_type ss_type;
4611 : 141929 : gfc_ss *ss, *pss;
4612 : 141929 : gfc_loopinfo *ploop;
4613 : 141929 : gfc_array_ref *ar;
4614 : :
4615 : : /* This code will be executed before entering the scalarization loop
4616 : : for this dimension. */
4617 : 432174 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4618 : : {
4619 : 290245 : ss_info = ss->info;
4620 : :
4621 : 290245 : if ((ss_info->useflags & flag) == 0)
4622 : 1468 : continue;
4623 : :
4624 : 288777 : ss_type = ss_info->type;
4625 : 352422 : if (ss_type != GFC_SS_SECTION
4626 : : && ss_type != GFC_SS_FUNCTION
4627 : 288777 : && ss_type != GFC_SS_CONSTRUCTOR
4628 : 288777 : && ss_type != GFC_SS_COMPONENT)
4629 : 63645 : continue;
4630 : :
4631 : 225132 : info = &ss_info->data.array;
4632 : :
4633 : 225132 : gcc_assert (dim < ss->dimen);
4634 : 225132 : gcc_assert (ss->dimen == loop->dimen);
4635 : :
4636 : 225132 : if (info->ref)
4637 : 158471 : ar = &info->ref->u.ar;
4638 : : else
4639 : : ar = NULL;
4640 : :
4641 : 225132 : if (dim == loop->dimen - 1 && loop->parent != NULL)
4642 : : {
4643 : : /* If we are in the outermost dimension of this loop, the previous
4644 : : dimension shall be in the parent loop. */
4645 : 4687 : gcc_assert (ss->parent != NULL);
4646 : :
4647 : 4687 : pss = ss->parent;
4648 : 4687 : ploop = loop->parent;
4649 : :
4650 : : /* ss and ss->parent are about the same array. */
4651 : 4687 : gcc_assert (ss_info == pss->info);
4652 : : }
4653 : : else
4654 : : {
4655 : : ploop = loop;
4656 : : pss = ss;
4657 : : }
4658 : :
4659 : 225132 : if (dim == loop->dimen - 1 && loop->parent == NULL)
4660 : : {
4661 : 171817 : gcc_assert (0 == ploop->order[0]);
4662 : :
4663 : 343634 : stride = gfc_conv_array_stride (info->descriptor,
4664 : 171817 : innermost_ss (ss)->dim[0]);
4665 : :
4666 : : /* Calculate the stride of the innermost loop. Hopefully this will
4667 : : allow the backend optimizers to do their stuff more effectively.
4668 : : */
4669 : 171817 : info->stride0 = gfc_evaluate_now (stride, pblock);
4670 : :
4671 : : /* For the outermost loop calculate the offset due to any
4672 : : elemental dimensions. It will have been initialized with the
4673 : : base offset of the array. */
4674 : 171817 : if (info->ref)
4675 : : {
4676 : 277091 : for (int i = 0; i < ar->dimen; i++)
4677 : : {
4678 : 160273 : if (ar->dimen_type[i] != DIMEN_ELEMENT)
4679 : 156098 : continue;
4680 : :
4681 : 4175 : add_array_offset (pblock, loop, ss, ar, i, /* unused */ -1);
4682 : : }
4683 : : }
4684 : : }
4685 : : else
4686 : : {
4687 : 53315 : int i;
4688 : :
4689 : 53315 : if (dim == loop->dimen - 1)
4690 : : i = 0;
4691 : : else
4692 : 48628 : i = dim + 1;
4693 : :
4694 : : /* For the time being, there is no loop reordering. */
4695 : 53315 : gcc_assert (i == ploop->order[i]);
4696 : 53315 : i = ploop->order[i];
4697 : :
4698 : : /* Add the offset for the previous loop dimension. */
4699 : 53315 : add_array_offset (pblock, ploop, ss, ar, pss->dim[i], i);
4700 : : }
4701 : :
4702 : : /* Remember this offset for the second loop. */
4703 : 225132 : if (dim == loop->temp_dim - 1 && loop->parent == NULL)
4704 : 53979 : info->saved_offset = info->offset;
4705 : : }
4706 : 141929 : }
4707 : :
4708 : :
4709 : : /* Start a scalarized expression. Creates a scope and declares loop
4710 : : variables. */
4711 : :
4712 : : void
4713 : 112442 : gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
4714 : : {
4715 : 112442 : int dim;
4716 : 112442 : int n;
4717 : 112442 : int flags;
4718 : :
4719 : 112442 : gcc_assert (!loop->array_parameter);
4720 : :
4721 : 252792 : for (dim = loop->dimen - 1; dim >= 0; dim--)
4722 : : {
4723 : 140350 : n = loop->order[dim];
4724 : :
4725 : 140350 : gfc_start_block (&loop->code[n]);
4726 : :
4727 : : /* Create the loop variable. */
4728 : 140350 : loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
4729 : :
4730 : 140350 : if (dim < loop->temp_dim)
4731 : : flags = 3;
4732 : : else
4733 : 94737 : flags = 1;
4734 : : /* Calculate values that will be constant within this loop. */
4735 : 140350 : gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
4736 : : }
4737 : 112442 : gfc_start_block (pbody);
4738 : 112442 : }
4739 : :
4740 : :
4741 : : /* Generates the actual loop code for a scalarization loop. */
4742 : :
4743 : : static void
4744 : 154308 : gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
4745 : : stmtblock_t * pbody)
4746 : : {
4747 : 154308 : stmtblock_t block;
4748 : 154308 : tree cond;
4749 : 154308 : tree tmp;
4750 : 154308 : tree loopbody;
4751 : 154308 : tree exit_label;
4752 : 154308 : tree stmt;
4753 : 154308 : tree init;
4754 : 154308 : tree incr;
4755 : :
4756 : 154308 : if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS
4757 : : | OMPWS_SCALARIZER_BODY))
4758 : : == (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS)
4759 : 108 : && n == loop->dimen - 1)
4760 : : {
4761 : : /* We create an OMP_FOR construct for the outermost scalarized loop. */
4762 : 80 : init = make_tree_vec (1);
4763 : 80 : cond = make_tree_vec (1);
4764 : 80 : incr = make_tree_vec (1);
4765 : :
4766 : : /* Cycle statement is implemented with a goto. Exit statement must not
4767 : : be present for this loop. */
4768 : 80 : exit_label = gfc_build_label_decl (NULL_TREE);
4769 : 80 : TREE_USED (exit_label) = 1;
4770 : :
4771 : : /* Label for cycle statements (if needed). */
4772 : 80 : tmp = build1_v (LABEL_EXPR, exit_label);
4773 : 80 : gfc_add_expr_to_block (pbody, tmp);
4774 : :
4775 : 80 : stmt = make_node (OMP_FOR);
4776 : :
4777 : 80 : TREE_TYPE (stmt) = void_type_node;
4778 : 80 : OMP_FOR_BODY (stmt) = loopbody = gfc_finish_block (pbody);
4779 : :
4780 : 80 : OMP_FOR_CLAUSES (stmt) = build_omp_clause (input_location,
4781 : : OMP_CLAUSE_SCHEDULE);
4782 : 80 : OMP_CLAUSE_SCHEDULE_KIND (OMP_FOR_CLAUSES (stmt))
4783 : 80 : = OMP_CLAUSE_SCHEDULE_STATIC;
4784 : 80 : if (ompws_flags & OMPWS_NOWAIT)
4785 : 33 : OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (stmt))
4786 : 66 : = build_omp_clause (input_location, OMP_CLAUSE_NOWAIT);
4787 : :
4788 : : /* Initialize the loopvar. */
4789 : 80 : TREE_VEC_ELT (init, 0) = build2_v (MODIFY_EXPR, loop->loopvar[n],
4790 : : loop->from[n]);
4791 : 80 : OMP_FOR_INIT (stmt) = init;
4792 : : /* The exit condition. */
4793 : 80 : TREE_VEC_ELT (cond, 0) = build2_loc (input_location, LE_EXPR,
4794 : : logical_type_node,
4795 : : loop->loopvar[n], loop->to[n]);
4796 : 80 : SET_EXPR_LOCATION (TREE_VEC_ELT (cond, 0), input_location);
4797 : 80 : OMP_FOR_COND (stmt) = cond;
4798 : : /* Increment the loopvar. */
4799 : 80 : tmp = build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4800 : : loop->loopvar[n], gfc_index_one_node);
4801 : 80 : TREE_VEC_ELT (incr, 0) = fold_build2_loc (input_location, MODIFY_EXPR,
4802 : : void_type_node, loop->loopvar[n], tmp);
4803 : 80 : OMP_FOR_INCR (stmt) = incr;
4804 : :
4805 : 80 : ompws_flags &= ~OMPWS_CURR_SINGLEUNIT;
4806 : 80 : gfc_add_expr_to_block (&loop->code[n], stmt);
4807 : : }
4808 : : else
4809 : : {
4810 : 308456 : bool reverse_loop = (loop->reverse[n] == GFC_REVERSE_SET)
4811 : 154228 : && (loop->temp_ss == NULL);
4812 : :
4813 : 154228 : loopbody = gfc_finish_block (pbody);
4814 : :
4815 : 154228 : if (reverse_loop)
4816 : 202 : std::swap (loop->from[n], loop->to[n]);
4817 : :
4818 : : /* Initialize the loopvar. */
4819 : 154228 : if (loop->loopvar[n] != loop->from[n])
4820 : 153408 : gfc_add_modify (&loop->code[n], loop->loopvar[n], loop->from[n]);
4821 : :
4822 : 154228 : exit_label = gfc_build_label_decl (NULL_TREE);
4823 : :
4824 : : /* Generate the loop body. */
4825 : 154228 : gfc_init_block (&block);
4826 : :
4827 : : /* The exit condition. */
4828 : 308254 : cond = fold_build2_loc (input_location, reverse_loop ? LT_EXPR : GT_EXPR,
4829 : : logical_type_node, loop->loopvar[n], loop->to[n]);
4830 : 154228 : tmp = build1_v (GOTO_EXPR, exit_label);
4831 : 154228 : TREE_USED (exit_label) = 1;
4832 : 154228 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
4833 : 154228 : gfc_add_expr_to_block (&block, tmp);
4834 : :
4835 : : /* The main body. */
4836 : 154228 : gfc_add_expr_to_block (&block, loopbody);
4837 : :
4838 : : /* Increment the loopvar. */
4839 : 308254 : tmp = fold_build2_loc (input_location,
4840 : : reverse_loop ? MINUS_EXPR : PLUS_EXPR,
4841 : : gfc_array_index_type, loop->loopvar[n],
4842 : : gfc_index_one_node);
4843 : :
4844 : 154228 : gfc_add_modify (&block, loop->loopvar[n], tmp);
4845 : :
4846 : : /* Build the loop. */
4847 : 154228 : tmp = gfc_finish_block (&block);
4848 : 154228 : tmp = build1_v (LOOP_EXPR, tmp);
4849 : 154228 : gfc_add_expr_to_block (&loop->code[n], tmp);
4850 : :
4851 : : /* Add the exit label. */
4852 : 154228 : tmp = build1_v (LABEL_EXPR, exit_label);
4853 : 154228 : gfc_add_expr_to_block (&loop->code[n], tmp);
4854 : : }
4855 : :
4856 : 154308 : }
4857 : :
4858 : :
4859 : : /* Finishes and generates the loops for a scalarized expression. */
4860 : :
4861 : : void
4862 : 116715 : gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
4863 : : {
4864 : 116715 : int dim;
4865 : 116715 : int n;
4866 : 116715 : gfc_ss *ss;
4867 : 116715 : stmtblock_t *pblock;
4868 : 116715 : tree tmp;
4869 : :
4870 : 116715 : pblock = body;
4871 : : /* Generate the loops. */
4872 : 261329 : for (dim = 0; dim < loop->dimen; dim++)
4873 : : {
4874 : 144614 : n = loop->order[dim];
4875 : 144614 : gfc_trans_scalarized_loop_end (loop, n, pblock);
4876 : 144614 : loop->loopvar[n] = NULL_TREE;
4877 : 144614 : pblock = &loop->code[n];
4878 : : }
4879 : :
4880 : 116715 : tmp = gfc_finish_block (pblock);
4881 : 116715 : gfc_add_expr_to_block (&loop->pre, tmp);
4882 : :
4883 : : /* Clear all the used flags. */
4884 : 344334 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4885 : 227619 : if (ss->parent == NULL)
4886 : 222869 : ss->info->useflags = 0;
4887 : 116715 : }
4888 : :
4889 : :
4890 : : /* Finish the main body of a scalarized expression, and start the secondary
4891 : : copying body. */
4892 : :
4893 : : void
4894 : 8115 : gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
4895 : : {
4896 : 8115 : int dim;
4897 : 8115 : int n;
4898 : 8115 : stmtblock_t *pblock;
4899 : 8115 : gfc_ss *ss;
4900 : :
4901 : 8115 : pblock = body;
4902 : : /* We finish as many loops as are used by the temporary. */
4903 : 9694 : for (dim = 0; dim < loop->temp_dim - 1; dim++)
4904 : : {
4905 : 1579 : n = loop->order[dim];
4906 : 1579 : gfc_trans_scalarized_loop_end (loop, n, pblock);
4907 : 1579 : loop->loopvar[n] = NULL_TREE;
4908 : 1579 : pblock = &loop->code[n];
4909 : : }
4910 : :
4911 : : /* We don't want to finish the outermost loop entirely. */
4912 : 8115 : n = loop->order[loop->temp_dim - 1];
4913 : 8115 : gfc_trans_scalarized_loop_end (loop, n, pblock);
4914 : :
4915 : : /* Restore the initial offsets. */
4916 : 23127 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4917 : : {
4918 : 15012 : gfc_ss_type ss_type;
4919 : 15012 : gfc_ss_info *ss_info;
4920 : :
4921 : 15012 : ss_info = ss->info;
4922 : :
4923 : 15012 : if ((ss_info->useflags & 2) == 0)
4924 : 4423 : continue;
4925 : :
4926 : 10589 : ss_type = ss_info->type;
4927 : 10743 : if (ss_type != GFC_SS_SECTION
4928 : : && ss_type != GFC_SS_FUNCTION
4929 : 10589 : && ss_type != GFC_SS_CONSTRUCTOR
4930 : 10589 : && ss_type != GFC_SS_COMPONENT)
4931 : 154 : continue;
4932 : :
4933 : 10435 : ss_info->data.array.offset = ss_info->data.array.saved_offset;
4934 : : }
4935 : :
4936 : : /* Restart all the inner loops we just finished. */
4937 : 9694 : for (dim = loop->temp_dim - 2; dim >= 0; dim--)
4938 : : {
4939 : 1579 : n = loop->order[dim];
4940 : :
4941 : 1579 : gfc_start_block (&loop->code[n]);
4942 : :
4943 : 1579 : loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
4944 : :
4945 : 1579 : gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
4946 : : }
4947 : :
4948 : : /* Start a block for the secondary copying code. */
4949 : 8115 : gfc_start_block (body);
4950 : 8115 : }
4951 : :
4952 : :
4953 : : /* Precalculate (either lower or upper) bound of an array section.
4954 : : BLOCK: Block in which the (pre)calculation code will go.
4955 : : BOUNDS[DIM]: Where the bound value will be stored once evaluated.
4956 : : VALUES[DIM]: Specified bound (NULL <=> unspecified).
4957 : : DESC: Array descriptor from which the bound will be picked if unspecified
4958 : : (either lower or upper bound according to LBOUND). */
4959 : :
4960 : : static void
4961 : 502497 : evaluate_bound (stmtblock_t *block, tree *bounds, gfc_expr ** values,
4962 : : tree desc, int dim, bool lbound, bool deferred, bool save_value)
4963 : : {
4964 : 502497 : gfc_se se;
4965 : 502497 : gfc_expr * input_val = values[dim];
4966 : 502497 : tree *output = &bounds[dim];
4967 : :
4968 : 502497 : if (input_val)
4969 : : {
4970 : : /* Specified section bound. */
4971 : 45829 : gfc_init_se (&se, NULL);
4972 : 45829 : gfc_conv_expr_type (&se, input_val, gfc_array_index_type);
4973 : 45829 : gfc_add_block_to_block (block, &se.pre);
4974 : 45829 : *output = se.expr;
4975 : : }
4976 : 456668 : else if (deferred && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
4977 : : {
4978 : : /* The gfc_conv_array_lbound () routine returns a constant zero for
4979 : : deferred length arrays, which in the scalarizer wreaks havoc, when
4980 : : copying to a (newly allocated) one-based array.
4981 : : Keep returning the actual result in sync for both bounds. */
4982 : 180815 : *output = lbound ? gfc_conv_descriptor_lbound_get (desc,
4983 : : gfc_rank_cst[dim]):
4984 : 60334 : gfc_conv_descriptor_ubound_get (desc,
4985 : : gfc_rank_cst[dim]);
4986 : : }
4987 : : else
4988 : : {
4989 : : /* No specific bound specified so use the bound of the array. */
4990 : 500962 : *output = lbound ? gfc_conv_array_lbound (desc, dim) :
4991 : 164775 : gfc_conv_array_ubound (desc, dim);
4992 : : }
4993 : 502497 : if (save_value)
4994 : 485211 : *output = gfc_evaluate_now (*output, block);
4995 : 502497 : }
4996 : :
4997 : :
4998 : : /* Calculate the lower bound of an array section. */
4999 : :
5000 : : static void
5001 : 251736 : gfc_conv_section_startstride (stmtblock_t * block, gfc_ss * ss, int dim)
5002 : : {
5003 : 251736 : gfc_expr *stride = NULL;
5004 : 251736 : tree desc;
5005 : 251736 : gfc_se se;
5006 : 251736 : gfc_array_info *info;
5007 : 251736 : gfc_array_ref *ar;
5008 : :
5009 : 251736 : gcc_assert (ss->info->type == GFC_SS_SECTION);
5010 : :
5011 : 251736 : info = &ss->info->data.array;
5012 : 251736 : ar = &info->ref->u.ar;
5013 : :
5014 : 251736 : if (ar->dimen_type[dim] == DIMEN_VECTOR)
5015 : : {
5016 : : /* We use a zero-based index to access the vector. */
5017 : 752 : info->start[dim] = gfc_index_zero_node;
5018 : 752 : info->end[dim] = NULL;
5019 : 752 : info->stride[dim] = gfc_index_one_node;
5020 : 752 : return;
5021 : : }
5022 : :
5023 : 250984 : gcc_assert (ar->dimen_type[dim] == DIMEN_RANGE
5024 : : || ar->dimen_type[dim] == DIMEN_THIS_IMAGE);
5025 : 250984 : desc = info->descriptor;
5026 : 250984 : stride = ar->stride[dim];
5027 : 250984 : bool save_value = !ss->is_alloc_lhs;
5028 : :
5029 : : /* Calculate the start of the range. For vector subscripts this will
5030 : : be the range of the vector. */
5031 : 250984 : evaluate_bound (block, info->start, ar->start, desc, dim, true,
5032 : 250984 : ar->as->type == AS_DEFERRED, save_value);
5033 : :
5034 : : /* Similarly calculate the end. Although this is not used in the
5035 : : scalarizer, it is needed when checking bounds and where the end
5036 : : is an expression with side-effects. */
5037 : 250984 : evaluate_bound (block, info->end, ar->end, desc, dim, false,
5038 : 250984 : ar->as->type == AS_DEFERRED, save_value);
5039 : :
5040 : :
5041 : : /* Calculate the stride. */
5042 : 250984 : if (stride == NULL)
5043 : 238934 : info->stride[dim] = gfc_index_one_node;
5044 : : else
5045 : : {
5046 : 12050 : gfc_init_se (&se, NULL);
5047 : 12050 : gfc_conv_expr_type (&se, stride, gfc_array_index_type);
5048 : 12050 : gfc_add_block_to_block (block, &se.pre);
5049 : 12050 : tree value = se.expr;
5050 : 12050 : if (save_value)
5051 : 12050 : info->stride[dim] = gfc_evaluate_now (value, block);
5052 : : else
5053 : 0 : info->stride[dim] = value;
5054 : : }
5055 : : }
5056 : :
5057 : :
5058 : : /* Generate in INNER the bounds checking code along the dimension DIM for
5059 : : the array associated with SS_INFO. */
5060 : :
5061 : : static void
5062 : 23989 : add_check_section_in_array_bounds (stmtblock_t *inner, gfc_ss_info *ss_info,
5063 : : int dim)
5064 : : {
5065 : 23989 : gfc_expr *expr = ss_info->expr;
5066 : 23989 : locus *expr_loc = &expr->where;
5067 : 23989 : const char *expr_name = expr->symtree->name;
5068 : :
5069 : 23989 : gfc_array_info *info = &ss_info->data.array;
5070 : :
5071 : 23989 : bool check_upper;
5072 : 23989 : if (dim == info->ref->u.ar.dimen - 1
5073 : 20386 : && info->ref->u.ar.as->type == AS_ASSUMED_SIZE)
5074 : : check_upper = false;
5075 : : else
5076 : 23693 : check_upper = true;
5077 : :
5078 : : /* Zero stride is not allowed. */
5079 : 23989 : tree tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5080 : : info->stride[dim], gfc_index_zero_node);
5081 : 23989 : char * msg = xasprintf ("Zero stride is not allowed, for dimension %d "
5082 : : "of array '%s'", dim + 1, expr_name);
5083 : 23989 : gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg);
5084 : 23989 : free (msg);
5085 : :
5086 : 23989 : tree desc = info->descriptor;
5087 : :
5088 : : /* This is the run-time equivalent of resolve.cc's
5089 : : check_dimension. The logical is more readable there
5090 : : than it is here, with all the trees. */
5091 : 23989 : tree lbound = gfc_conv_array_lbound (desc, dim);
5092 : 23989 : tree end = info->end[dim];
5093 : 23989 : tree ubound = check_upper ? gfc_conv_array_ubound (desc, dim) : NULL_TREE;
5094 : :
5095 : : /* non_zerosized is true when the selected range is not
5096 : : empty. */
5097 : 23989 : tree stride_pos = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
5098 : : info->stride[dim], gfc_index_zero_node);
5099 : 23989 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
5100 : : info->start[dim], end);
5101 : 23989 : stride_pos = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5102 : : logical_type_node, stride_pos, tmp);
5103 : :
5104 : 23989 : tree stride_neg = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
5105 : : info->stride[dim], gfc_index_zero_node);
5106 : 23989 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
5107 : : info->start[dim], end);
5108 : 23989 : stride_neg = fold_build2_loc (input_location, TRUTH_AND_EXPR,
5109 : : logical_type_node, stride_neg, tmp);
5110 : 23989 : tree non_zerosized = fold_build2_loc (input_location, TRUTH_OR_EXPR,
5111 : : logical_type_node, stride_pos,
5112 : : stride_neg);
5113 : :
5114 : : /* Check the start of the range against the lower and upper
5115 : : bounds of the array, if the range is not empty.
5116 : : If upper bound is present, include both bounds in the
5117 : : error message. */
5118 : 23989 : if (check_upper)
5119 : : {
5120 : 23693 : tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
5121 : : info->start[dim], lbound);
5122 : 23693 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
5123 : : non_zerosized, tmp);
5124 : 23693 : tree tmp2 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
5125 : : info->start[dim], ubound);
5126 : 23693 : tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
5127 : : non_zerosized, tmp2);
5128 : 23693 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
5129 : : "expected range (%%ld:%%ld)", dim + 1, expr_name);
5130 : 23693 : gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg,
5131 : : fold_convert (long_integer_type_node, info->start[dim]),
5132 : : fold_convert (long_integer_type_node, lbound),
5133 : : fold_convert (long_integer_type_node, ubound));
5134 : 23693 : gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
5135 : : fold_convert (long_integer_type_node, info->start[dim]),
5136 : : fold_convert (long_integer_type_node, lbound),
5137 : : fold_convert (long_integer_type_node, ubound));
5138 : 23693 : free (msg);
5139 : : }
5140 : : else
5141 : : {
5142 : 296 : tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
5143 : : info->start[dim], lbound);
5144 : 296 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
5145 : : non_zerosized, tmp);
5146 : 296 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' below "
5147 : : "lower bound of %%ld", dim + 1, expr_name);
5148 : 296 : gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg,
5149 : : fold_convert (long_integer_type_node, info->start[dim]),
5150 : : fold_convert (long_integer_type_node, lbound));
5151 : 296 : free (msg);
5152 : : }
5153 : :
5154 : : /* Compute the last element of the range, which is not
5155 : : necessarily "end" (think 0:5:3, which doesn't contain 5)
5156 : : and check it against both lower and upper bounds. */
5157 : :
5158 : 23989 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5159 : : end, info->start[dim]);
5160 : 23989 : tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR, gfc_array_index_type,
5161 : : tmp, info->stride[dim]);
5162 : 23989 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5163 : : end, tmp);
5164 : 23989 : tree tmp2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
5165 : : tmp, lbound);
5166 : 23989 : tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
5167 : : non_zerosized, tmp2);
5168 : 23989 : if (check_upper)
5169 : : {
5170 : 23693 : tree tmp3 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
5171 : : tmp, ubound);
5172 : 23693 : tmp3 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
5173 : : non_zerosized, tmp3);
5174 : 23693 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
5175 : : "expected range (%%ld:%%ld)", dim + 1, expr_name);
5176 : 23693 : gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
5177 : : fold_convert (long_integer_type_node, tmp),
5178 : : fold_convert (long_integer_type_node, ubound),
5179 : : fold_convert (long_integer_type_node, lbound));
5180 : 23693 : gfc_trans_runtime_check (true, false, tmp3, inner, expr_loc, msg,
5181 : : fold_convert (long_integer_type_node, tmp),
5182 : : fold_convert (long_integer_type_node, ubound),
5183 : : fold_convert (long_integer_type_node, lbound));
5184 : 23693 : free (msg);
5185 : : }
5186 : : else
5187 : : {
5188 : 296 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' below "
5189 : : "lower bound of %%ld", dim + 1, expr_name);
5190 : 296 : gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
5191 : : fold_convert (long_integer_type_node, tmp),
5192 : : fold_convert (long_integer_type_node, lbound));
5193 : 296 : free (msg);
5194 : : }
5195 : 23989 : }
5196 : :
5197 : :
5198 : : /* Tells whether we need to generate bounds checking code for the array
5199 : : associated with SS. */
5200 : :
5201 : : bool
5202 : 24943 : bounds_check_needed (gfc_ss *ss)
5203 : : {
5204 : : /* Catch allocatable lhs in f2003. */
5205 : 24943 : if (flag_realloc_lhs && ss->no_bounds_check)
5206 : : return false;
5207 : :
5208 : 24669 : gfc_ss_info *ss_info = ss->info;
5209 : 24669 : if (ss_info->type == GFC_SS_SECTION)
5210 : : return true;
5211 : :
5212 : 4110 : if (!(ss_info->type == GFC_SS_INTRINSIC
5213 : 227 : && ss_info->expr
5214 : 227 : && ss_info->expr->expr_type == EXPR_FUNCTION))
5215 : : return false;
5216 : :
5217 : 227 : gfc_intrinsic_sym *isym = ss_info->expr->value.function.isym;
5218 : 227 : if (!(isym
5219 : 227 : && (isym->id == GFC_ISYM_MAXLOC
5220 : 203 : || isym->id == GFC_ISYM_MINLOC)))
5221 : : return false;
5222 : :
5223 : 34 : return gfc_inline_intrinsic_function_p (ss_info->expr);
5224 : : }
5225 : :
5226 : :
5227 : : /* Calculates the range start and stride for a SS chain. Also gets the
5228 : : descriptor and data pointer. The range of vector subscripts is the size
5229 : : of the vector. Array bounds are also checked. */
5230 : :
5231 : : void
5232 : 178167 : gfc_conv_ss_startstride (gfc_loopinfo * loop)
5233 : : {
5234 : 178167 : int n;
5235 : 178167 : tree tmp;
5236 : 178167 : gfc_ss *ss;
5237 : :
5238 : 178167 : gfc_loopinfo * const outer_loop = outermost_loop (loop);
5239 : :
5240 : 178167 : loop->dimen = 0;
5241 : : /* Determine the rank of the loop. */
5242 : 197530 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5243 : : {
5244 : 197530 : switch (ss->info->type)
5245 : : {
5246 : 167071 : case GFC_SS_SECTION:
5247 : 167071 : case GFC_SS_CONSTRUCTOR:
5248 : 167071 : case GFC_SS_FUNCTION:
5249 : 167071 : case GFC_SS_COMPONENT:
5250 : 167071 : loop->dimen = ss->dimen;
5251 : 167071 : goto done;
5252 : :
5253 : : /* As usual, lbound and ubound are exceptions!. */
5254 : 11096 : case GFC_SS_INTRINSIC:
5255 : 11096 : switch (ss->info->expr->value.function.isym->id)
5256 : : {
5257 : 11096 : case GFC_ISYM_LBOUND:
5258 : 11096 : case GFC_ISYM_UBOUND:
5259 : 11096 : case GFC_ISYM_LCOBOUND:
5260 : 11096 : case GFC_ISYM_UCOBOUND:
5261 : 11096 : case GFC_ISYM_MAXLOC:
5262 : 11096 : case GFC_ISYM_MINLOC:
5263 : 11096 : case GFC_ISYM_SHAPE:
5264 : 11096 : case GFC_ISYM_THIS_IMAGE:
5265 : 11096 : loop->dimen = ss->dimen;
5266 : 11096 : goto done;
5267 : :
5268 : : default:
5269 : : break;
5270 : : }
5271 : :
5272 : 19363 : default:
5273 : 19363 : break;
5274 : : }
5275 : : }
5276 : :
5277 : : /* We should have determined the rank of the expression by now. If
5278 : : not, that's bad news. */
5279 : 0 : gcc_unreachable ();
5280 : :
5281 : : done:
5282 : : /* Loop over all the SS in the chain. */
5283 : 462369 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5284 : : {
5285 : 284202 : gfc_ss_info *ss_info;
5286 : 284202 : gfc_array_info *info;
5287 : 284202 : gfc_expr *expr;
5288 : :
5289 : 284202 : ss_info = ss->info;
5290 : 284202 : expr = ss_info->expr;
5291 : 284202 : info = &ss_info->data.array;
5292 : :
5293 : 284202 : if (expr && expr->shape && !info->shape)
5294 : 166866 : info->shape = expr->shape;
5295 : :
5296 : 284202 : switch (ss_info->type)
5297 : : {
5298 : 180587 : case GFC_SS_SECTION:
5299 : : /* Get the descriptor for the array. If it is a cross loops array,
5300 : : we got the descriptor already in the outermost loop. */
5301 : 180587 : if (ss->parent == NULL)
5302 : 175951 : gfc_conv_ss_descriptor (&outer_loop->pre, ss,
5303 : 175951 : !loop->array_parameter);
5304 : :
5305 : 431769 : for (n = 0; n < ss->dimen; n++)
5306 : 251182 : gfc_conv_section_startstride (&outer_loop->pre, ss, ss->dim[n]);
5307 : : break;
5308 : :
5309 : 11333 : case GFC_SS_INTRINSIC:
5310 : 11333 : switch (expr->value.function.isym->id)
5311 : : {
5312 : 3281 : case GFC_ISYM_MINLOC:
5313 : 3281 : case GFC_ISYM_MAXLOC:
5314 : 3281 : {
5315 : 3281 : gfc_se se;
5316 : 3281 : gfc_init_se (&se, nullptr);
5317 : 3281 : se.loop = loop;
5318 : 3281 : se.ss = ss;
5319 : 3281 : gfc_conv_intrinsic_function (&se, expr);
5320 : 3281 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
5321 : 3281 : gfc_add_block_to_block (&outer_loop->post, &se.post);
5322 : :
5323 : 3281 : info->descriptor = se.expr;
5324 : :
5325 : 3281 : info->data = gfc_conv_array_data (info->descriptor);
5326 : 3281 : info->data = gfc_evaluate_now (info->data, &outer_loop->pre);
5327 : :
5328 : 3281 : gfc_expr *array = expr->value.function.actual->expr;
5329 : 3281 : tree rank = build_int_cst (gfc_array_index_type, array->rank);
5330 : :
5331 : 3281 : tree tmp = fold_build2_loc (input_location, MINUS_EXPR,
5332 : : gfc_array_index_type, rank,
5333 : : gfc_index_one_node);
5334 : :
5335 : 3281 : info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
5336 : 3281 : info->start[0] = gfc_index_zero_node;
5337 : 3281 : info->stride[0] = gfc_index_one_node;
5338 : 3281 : info->offset = gfc_index_zero_node;
5339 : 3281 : continue;
5340 : 3281 : }
5341 : :
5342 : : /* Fall through to supply start and stride. */
5343 : 3004 : case GFC_ISYM_LBOUND:
5344 : 3004 : case GFC_ISYM_UBOUND:
5345 : : /* This is the variant without DIM=... */
5346 : 3004 : gcc_assert (expr->value.function.actual->next->expr == NULL);
5347 : : /* Fall through. */
5348 : :
5349 : 7827 : case GFC_ISYM_SHAPE:
5350 : 7827 : {
5351 : 7827 : gfc_expr *arg;
5352 : :
5353 : 7827 : arg = expr->value.function.actual->expr;
5354 : 7827 : if (arg->rank == -1)
5355 : : {
5356 : 1157 : gfc_se se;
5357 : 1157 : tree rank, tmp;
5358 : :
5359 : : /* The rank (hence the return value's shape) is unknown,
5360 : : we have to retrieve it. */
5361 : 1157 : gfc_init_se (&se, NULL);
5362 : 1157 : se.descriptor_only = 1;
5363 : 1157 : gfc_conv_expr (&se, arg);
5364 : : /* This is a bare variable, so there is no preliminary
5365 : : or cleanup code unless -std=f202y and bounds checking
5366 : : is on. */
5367 : 1157 : if (!((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
5368 : 0 : && (gfc_option.allow_std & GFC_STD_F202Y)))
5369 : 1157 : gcc_assert (se.pre.head == NULL_TREE
5370 : : && se.post.head == NULL_TREE);
5371 : 1157 : rank = gfc_conv_descriptor_rank (se.expr);
5372 : 1157 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5373 : : gfc_array_index_type,
5374 : : fold_convert (gfc_array_index_type,
5375 : : rank),
5376 : : gfc_index_one_node);
5377 : 1157 : info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
5378 : 1157 : info->start[0] = gfc_index_zero_node;
5379 : 1157 : info->stride[0] = gfc_index_one_node;
5380 : 1157 : continue;
5381 : 1157 : }
5382 : : /* Otherwise fall through GFC_SS_FUNCTION. */
5383 : : gcc_fallthrough ();
5384 : : }
5385 : : case GFC_ISYM_LCOBOUND:
5386 : : case GFC_ISYM_UCOBOUND:
5387 : : case GFC_ISYM_THIS_IMAGE:
5388 : : break;
5389 : :
5390 : 0 : default:
5391 : 0 : continue;
5392 : 0 : }
5393 : :
5394 : : /* FALLTHRU */
5395 : : case GFC_SS_CONSTRUCTOR:
5396 : : case GFC_SS_FUNCTION:
5397 : 125051 : for (n = 0; n < ss->dimen; n++)
5398 : : {
5399 : 67462 : int dim = ss->dim[n];
5400 : :
5401 : 67462 : info->start[dim] = gfc_index_zero_node;
5402 : 67462 : if (ss_info->type != GFC_SS_FUNCTION)
5403 : 53268 : info->end[dim] = gfc_index_zero_node;
5404 : 67462 : info->stride[dim] = gfc_index_one_node;
5405 : : }
5406 : : break;
5407 : :
5408 : : default:
5409 : : break;
5410 : : }
5411 : : }
5412 : :
5413 : : /* The rest is just runtime bounds checking. */
5414 : 178167 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
5415 : : {
5416 : 16884 : stmtblock_t block;
5417 : 16884 : tree size[GFC_MAX_DIMENSIONS];
5418 : 16884 : tree tmp3;
5419 : 16884 : gfc_array_info *info;
5420 : 16884 : char *msg;
5421 : 16884 : int dim;
5422 : :
5423 : 16884 : gfc_start_block (&block);
5424 : :
5425 : 54068 : for (n = 0; n < loop->dimen; n++)
5426 : 20300 : size[n] = NULL_TREE;
5427 : :
5428 : : /* If there is a constructor involved, derive size[] from its shape. */
5429 : 39004 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5430 : : {
5431 : 24598 : gfc_ss_info *ss_info;
5432 : :
5433 : 24598 : ss_info = ss->info;
5434 : 24598 : info = &ss_info->data.array;
5435 : :
5436 : 24598 : if (ss_info->type == GFC_SS_CONSTRUCTOR && info->shape)
5437 : : {
5438 : 5220 : for (n = 0; n < loop->dimen; n++)
5439 : : {
5440 : 2742 : if (size[n] == NULL)
5441 : : {
5442 : 2742 : gcc_assert (info->shape[n]);
5443 : 2742 : size[n] = gfc_conv_mpz_to_tree (info->shape[n],
5444 : : gfc_index_integer_kind);
5445 : : }
5446 : : }
5447 : : break;
5448 : : }
5449 : : }
5450 : :
5451 : 41827 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5452 : : {
5453 : 24943 : stmtblock_t inner;
5454 : 24943 : gfc_ss_info *ss_info;
5455 : 24943 : gfc_expr *expr;
5456 : 24943 : locus *expr_loc;
5457 : 24943 : const char *expr_name;
5458 : 24943 : char *ref_name = NULL;
5459 : :
5460 : 24943 : if (!bounds_check_needed (ss))
5461 : 4350 : continue;
5462 : :
5463 : 20593 : ss_info = ss->info;
5464 : 20593 : expr = ss_info->expr;
5465 : 20593 : expr_loc = &expr->where;
5466 : 20593 : if (expr->ref)
5467 : 20559 : expr_name = ref_name = abridged_ref_name (expr, NULL);
5468 : : else
5469 : 34 : expr_name = expr->symtree->name;
5470 : :
5471 : 20593 : gfc_start_block (&inner);
5472 : :
5473 : : /* TODO: range checking for mapped dimensions. */
5474 : 20593 : info = &ss_info->data.array;
5475 : :
5476 : : /* This code only checks ranges. Elemental and vector
5477 : : dimensions are checked later. */
5478 : 65223 : for (n = 0; n < loop->dimen; n++)
5479 : : {
5480 : 24037 : dim = ss->dim[n];
5481 : 24037 : if (ss_info->type == GFC_SS_SECTION)
5482 : : {
5483 : 24003 : if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
5484 : 14 : continue;
5485 : :
5486 : 23989 : add_check_section_in_array_bounds (&inner, ss_info, dim);
5487 : : }
5488 : :
5489 : : /* Check the section sizes match. */
5490 : 24023 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5491 : : gfc_array_index_type, info->end[dim],
5492 : : info->start[dim]);
5493 : 24023 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
5494 : : gfc_array_index_type, tmp,
5495 : : info->stride[dim]);
5496 : 24023 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5497 : : gfc_array_index_type,
5498 : : gfc_index_one_node, tmp);
5499 : 24023 : tmp = fold_build2_loc (input_location, MAX_EXPR,
5500 : : gfc_array_index_type, tmp,
5501 : : build_int_cst (gfc_array_index_type, 0));
5502 : : /* We remember the size of the first section, and check all the
5503 : : others against this. */
5504 : 24023 : if (size[n])
5505 : : {
5506 : 7167 : tmp3 = fold_build2_loc (input_location, NE_EXPR,
5507 : : logical_type_node, tmp, size[n]);
5508 : 7167 : if (ss_info->type == GFC_SS_INTRINSIC)
5509 : 0 : msg = xasprintf ("Extent mismatch for dimension %d of the "
5510 : : "result of intrinsic '%s' (%%ld/%%ld)",
5511 : : dim + 1, expr_name);
5512 : : else
5513 : 7167 : msg = xasprintf ("Array bound mismatch for dimension %d "
5514 : : "of array '%s' (%%ld/%%ld)",
5515 : : dim + 1, expr_name);
5516 : :
5517 : 7167 : gfc_trans_runtime_check (true, false, tmp3, &inner,
5518 : : expr_loc, msg,
5519 : : fold_convert (long_integer_type_node, tmp),
5520 : : fold_convert (long_integer_type_node, size[n]));
5521 : :
5522 : 7167 : free (msg);
5523 : : }
5524 : : else
5525 : 16856 : size[n] = gfc_evaluate_now (tmp, &inner);
5526 : : }
5527 : :
5528 : 20593 : tmp = gfc_finish_block (&inner);
5529 : :
5530 : : /* For optional arguments, only check bounds if the argument is
5531 : : present. */
5532 : 20593 : if ((expr->symtree->n.sym->attr.optional
5533 : 20285 : || expr->symtree->n.sym->attr.not_always_present)
5534 : 308 : && expr->symtree->n.sym->attr.dummy)
5535 : 307 : tmp = build3_v (COND_EXPR,
5536 : : gfc_conv_expr_present (expr->symtree->n.sym),
5537 : : tmp, build_empty_stmt (input_location));
5538 : :
5539 : 20593 : gfc_add_expr_to_block (&block, tmp);
5540 : :
5541 : 20593 : free (ref_name);
5542 : : }
5543 : :
5544 : 16884 : tmp = gfc_finish_block (&block);
5545 : 16884 : gfc_add_expr_to_block (&outer_loop->pre, tmp);
5546 : : }
5547 : :
5548 : 181531 : for (loop = loop->nested; loop; loop = loop->next)
5549 : 3364 : gfc_conv_ss_startstride (loop);
5550 : 178167 : }
5551 : :
5552 : : /* Return true if both symbols could refer to the same data object. Does
5553 : : not take account of aliasing due to equivalence statements. */
5554 : :
5555 : : static bool
5556 : 13245 : symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym, bool lsym_pointer,
5557 : : bool lsym_target, bool rsym_pointer, bool rsym_target)
5558 : : {
5559 : : /* Aliasing isn't possible if the symbols have different base types,
5560 : : except for complex types where an inquiry reference (%RE, %IM) could
5561 : : alias with a real type with the same kind parameter. */
5562 : 13245 : if (!gfc_compare_types (&lsym->ts, &rsym->ts)
5563 : 13245 : && !(((lsym->ts.type == BT_COMPLEX && rsym->ts.type == BT_REAL)
5564 : 4701 : || (lsym->ts.type == BT_REAL && rsym->ts.type == BT_COMPLEX))
5565 : 76 : && lsym->ts.kind == rsym->ts.kind))
5566 : : return false;
5567 : :
5568 : : /* Pointers can point to other pointers and target objects. */
5569 : :
5570 : 8557 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5571 : 8348 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5572 : : return true;
5573 : :
5574 : : /* Special case: Argument association, cf. F90 12.4.1.6, F2003 12.4.1.7
5575 : : and F2008 12.5.2.13 items 3b and 4b. The pointer case (a) is already
5576 : : checked above. */
5577 : 8434 : if (lsym_target && rsym_target
5578 : 14 : && ((lsym->attr.dummy && !lsym->attr.contiguous
5579 : 0 : && (!lsym->attr.dimension || lsym->as->type == AS_ASSUMED_SHAPE))
5580 : 14 : || (rsym->attr.dummy && !rsym->attr.contiguous
5581 : 6 : && (!rsym->attr.dimension
5582 : 6 : || rsym->as->type == AS_ASSUMED_SHAPE))))
5583 : 6 : return true;
5584 : :
5585 : : return false;
5586 : : }
5587 : :
5588 : :
5589 : : /* Return true if the two SS could be aliased, i.e. both point to the same data
5590 : : object. */
5591 : : /* TODO: resolve aliases based on frontend expressions. */
5592 : :
5593 : : static int
5594 : 11228 : gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
5595 : : {
5596 : 11228 : gfc_ref *lref;
5597 : 11228 : gfc_ref *rref;
5598 : 11228 : gfc_expr *lexpr, *rexpr;
5599 : 11228 : gfc_symbol *lsym;
5600 : 11228 : gfc_symbol *rsym;
5601 : 11228 : bool lsym_pointer, lsym_target, rsym_pointer, rsym_target;
5602 : :
5603 : 11228 : lexpr = lss->info->expr;
5604 : 11228 : rexpr = rss->info->expr;
5605 : :
5606 : 11228 : lsym = lexpr->symtree->n.sym;
5607 : 11228 : rsym = rexpr->symtree->n.sym;
5608 : :
5609 : 11228 : lsym_pointer = lsym->attr.pointer;
5610 : 11228 : lsym_target = lsym->attr.target;
5611 : 11228 : rsym_pointer = rsym->attr.pointer;
5612 : 11228 : rsym_target = rsym->attr.target;
5613 : :
5614 : 11228 : if (symbols_could_alias (lsym, rsym, lsym_pointer, lsym_target,
5615 : : rsym_pointer, rsym_target))
5616 : : return 1;
5617 : :
5618 : 11137 : if (rsym->ts.type != BT_DERIVED && rsym->ts.type != BT_CLASS
5619 : 9975 : && lsym->ts.type != BT_DERIVED && lsym->ts.type != BT_CLASS)
5620 : : return 0;
5621 : :
5622 : : /* For derived types we must check all the component types. We can ignore
5623 : : array references as these will have the same base type as the previous
5624 : : component ref. */
5625 : 2458 : for (lref = lexpr->ref; lref != lss->info->data.array.ref; lref = lref->next)
5626 : : {
5627 : 903 : if (lref->type != REF_COMPONENT)
5628 : 89 : continue;
5629 : :
5630 : 814 : lsym_pointer = lsym_pointer || lref->u.c.sym->attr.pointer;
5631 : 814 : lsym_target = lsym_target || lref->u.c.sym->attr.target;
5632 : :
5633 : 814 : if (symbols_could_alias (lref->u.c.sym, rsym, lsym_pointer, lsym_target,
5634 : : rsym_pointer, rsym_target))
5635 : : return 1;
5636 : :
5637 : 814 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5638 : 799 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5639 : : {
5640 : 6 : if (gfc_compare_types (&lref->u.c.component->ts,
5641 : : &rsym->ts))
5642 : : return 1;
5643 : : }
5644 : :
5645 : 1237 : for (rref = rexpr->ref; rref != rss->info->data.array.ref;
5646 : 429 : rref = rref->next)
5647 : : {
5648 : 430 : if (rref->type != REF_COMPONENT)
5649 : 36 : continue;
5650 : :
5651 : 394 : rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
5652 : 394 : rsym_target = lsym_target || rref->u.c.sym->attr.target;
5653 : :
5654 : 394 : if (symbols_could_alias (lref->u.c.sym, rref->u.c.sym,
5655 : : lsym_pointer, lsym_target,
5656 : : rsym_pointer, rsym_target))
5657 : : return 1;
5658 : :
5659 : 393 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5660 : 389 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5661 : : {
5662 : 0 : if (gfc_compare_types (&lref->u.c.component->ts,
5663 : 0 : &rref->u.c.sym->ts))
5664 : : return 1;
5665 : 0 : if (gfc_compare_types (&lref->u.c.sym->ts,
5666 : 0 : &rref->u.c.component->ts))
5667 : : return 1;
5668 : 0 : if (gfc_compare_types (&lref->u.c.component->ts,
5669 : 0 : &rref->u.c.component->ts))
5670 : : return 1;
5671 : : }
5672 : : }
5673 : : }
5674 : :
5675 : 1555 : lsym_pointer = lsym->attr.pointer;
5676 : 1555 : lsym_target = lsym->attr.target;
5677 : :
5678 : 2358 : for (rref = rexpr->ref; rref != rss->info->data.array.ref; rref = rref->next)
5679 : : {
5680 : 956 : if (rref->type != REF_COMPONENT)
5681 : : break;
5682 : :
5683 : 809 : rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
5684 : 809 : rsym_target = lsym_target || rref->u.c.sym->attr.target;
5685 : :
5686 : 809 : if (symbols_could_alias (rref->u.c.sym, lsym,
5687 : : lsym_pointer, lsym_target,
5688 : : rsym_pointer, rsym_target))
5689 : : return 1;
5690 : :
5691 : 809 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5692 : 791 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5693 : : {
5694 : 6 : if (gfc_compare_types (&lsym->ts, &rref->u.c.component->ts))
5695 : : return 1;
5696 : : }
5697 : : }
5698 : :
5699 : : return 0;
5700 : : }
5701 : :
5702 : :
5703 : : /* Resolve array data dependencies. Creates a temporary if required. */
5704 : : /* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
5705 : : dependency.cc. */
5706 : :
5707 : : void
5708 : 36147 : gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
5709 : : gfc_ss * rss)
5710 : : {
5711 : 36147 : gfc_ss *ss;
5712 : 36147 : gfc_ref *lref;
5713 : 36147 : gfc_ref *rref;
5714 : 36147 : gfc_ss_info *ss_info;
5715 : 36147 : gfc_expr *dest_expr;
5716 : 36147 : gfc_expr *ss_expr;
5717 : 36147 : int nDepend = 0;
5718 : 36147 : int i, j;
5719 : :
5720 : 36147 : loop->temp_ss = NULL;
5721 : 36147 : dest_expr = dest->info->expr;
5722 : :
5723 : 78000 : for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
5724 : : {
5725 : 42945 : ss_info = ss->info;
5726 : 42945 : ss_expr = ss_info->expr;
5727 : :
5728 : 42945 : if (ss_info->array_outer_dependency)
5729 : : {
5730 : : nDepend = 1;
5731 : : break;
5732 : : }
5733 : :
5734 : 42835 : if (ss_info->type != GFC_SS_SECTION)
5735 : : {
5736 : 29088 : if (flag_realloc_lhs
5737 : 28170 : && dest_expr != ss_expr
5738 : 28170 : && gfc_is_reallocatable_lhs (dest_expr)
5739 : 35434 : && ss_expr->rank)
5740 : 2868 : nDepend = gfc_check_dependency (dest_expr, ss_expr, true);
5741 : :
5742 : : /* Check for cases like c(:)(1:2) = c(2)(2:3) */
5743 : 29088 : if (!nDepend && dest_expr->rank > 0
5744 : 28649 : && dest_expr->ts.type == BT_CHARACTER
5745 : 4181 : && ss_expr->expr_type == EXPR_VARIABLE)
5746 : :
5747 : 157 : nDepend = gfc_check_dependency (dest_expr, ss_expr, false);
5748 : :
5749 : 29088 : if (ss_info->type == GFC_SS_REFERENCE
5750 : 29088 : && gfc_check_dependency (dest_expr, ss_expr, false))
5751 : 182 : ss_info->data.scalar.needs_temporary = 1;
5752 : :
5753 : 29088 : if (nDepend)
5754 : : break;
5755 : : else
5756 : 28637 : continue;
5757 : : }
5758 : :
5759 : 13747 : if (dest_expr->symtree->n.sym != ss_expr->symtree->n.sym)
5760 : : {
5761 : 11228 : if (gfc_could_be_alias (dest, ss)
5762 : 11228 : || gfc_are_equivalenced_arrays (dest_expr, ss_expr))
5763 : : {
5764 : : nDepend = 1;
5765 : : break;
5766 : : }
5767 : : }
5768 : : else
5769 : : {
5770 : 2519 : lref = dest_expr->ref;
5771 : 2519 : rref = ss_expr->ref;
5772 : :
5773 : 2519 : nDepend = gfc_dep_resolver (lref, rref, &loop->reverse[0]);
5774 : :
5775 : 2519 : if (nDepend == 1)
5776 : : break;
5777 : :
5778 : 4919 : for (i = 0; i < dest->dimen; i++)
5779 : 6572 : for (j = 0; j < ss->dimen; j++)
5780 : 3886 : if (i != j
5781 : 1137 : && dest->dim[i] == ss->dim[j])
5782 : : {
5783 : : /* If we don't access array elements in the same order,
5784 : : there is a dependency. */
5785 : 63 : nDepend = 1;
5786 : 63 : goto temporary;
5787 : : }
5788 : : #if 0
5789 : : /* TODO : loop shifting. */
5790 : : if (nDepend == 1)
5791 : : {
5792 : : /* Mark the dimensions for LOOP SHIFTING */
5793 : : for (n = 0; n < loop->dimen; n++)
5794 : : {
5795 : : int dim = dest->data.info.dim[n];
5796 : :
5797 : : if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
5798 : : depends[n] = 2;
5799 : : else if (! gfc_is_same_range (&lref->u.ar,
5800 : : &rref->u.ar, dim, 0))
5801 : : depends[n] = 1;
5802 : : }
5803 : :
5804 : : /* Put all the dimensions with dependencies in the
5805 : : innermost loops. */
5806 : : dim = 0;
5807 : : for (n = 0; n < loop->dimen; n++)
5808 : : {
5809 : : gcc_assert (loop->order[n] == n);
5810 : : if (depends[n])
5811 : : loop->order[dim++] = n;
5812 : : }
5813 : : for (n = 0; n < loop->dimen; n++)
5814 : : {
5815 : : if (! depends[n])
5816 : : loop->order[dim++] = n;
5817 : : }
5818 : :
5819 : : gcc_assert (dim == loop->dimen);
5820 : : break;
5821 : : }
5822 : : #endif
5823 : : }
5824 : : }
5825 : :
5826 : 737 : temporary:
5827 : :
5828 : 36147 : if (nDepend == 1)
5829 : : {
5830 : 1092 : tree base_type = gfc_typenode_for_spec (&dest_expr->ts);
5831 : 1092 : if (GFC_ARRAY_TYPE_P (base_type)
5832 : 1092 : || GFC_DESCRIPTOR_TYPE_P (base_type))
5833 : 0 : base_type = gfc_get_element_type (base_type);
5834 : 1092 : loop->temp_ss = gfc_get_temp_ss (base_type, dest->info->string_length,
5835 : : loop->dimen);
5836 : 1092 : gfc_add_ss_to_loop (loop, loop->temp_ss);
5837 : : }
5838 : : else
5839 : 35055 : loop->temp_ss = NULL;
5840 : 36147 : }
5841 : :
5842 : :
5843 : : /* Browse through each array's information from the scalarizer and set the loop
5844 : : bounds according to the "best" one (per dimension), i.e. the one which
5845 : : provides the most information (constant bounds, shape, etc.). */
5846 : :
5847 : : static void
5848 : 178167 : set_loop_bounds (gfc_loopinfo *loop)
5849 : : {
5850 : 178167 : int n, dim, spec_dim;
5851 : 178167 : gfc_array_info *info;
5852 : 178167 : gfc_array_info *specinfo;
5853 : 178167 : gfc_ss *ss;
5854 : 178167 : tree tmp;
5855 : 178167 : gfc_ss **loopspec;
5856 : 178167 : bool dynamic[GFC_MAX_DIMENSIONS];
5857 : 178167 : mpz_t *cshape;
5858 : 178167 : mpz_t i;
5859 : 178167 : bool nonoptional_arr;
5860 : :
5861 : 178167 : gfc_loopinfo * const outer_loop = outermost_loop (loop);
5862 : :
5863 : 178167 : loopspec = loop->specloop;
5864 : :
5865 : 178167 : mpz_init (i);
5866 : 421228 : for (n = 0; n < loop->dimen; n++)
5867 : : {
5868 : 243061 : loopspec[n] = NULL;
5869 : 243061 : dynamic[n] = false;
5870 : :
5871 : : /* If there are both optional and nonoptional array arguments, scalarize
5872 : : over the nonoptional; otherwise, it does not matter as then all
5873 : : (optional) arrays have to be present per F2008, 125.2.12p3(6). */
5874 : :
5875 : 243061 : nonoptional_arr = false;
5876 : :
5877 : 283169 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5878 : 283149 : if (ss->info->type != GFC_SS_SCALAR && ss->info->type != GFC_SS_TEMP
5879 : 249606 : && ss->info->type != GFC_SS_REFERENCE && !ss->info->can_be_null_ref)
5880 : : {
5881 : : nonoptional_arr = true;
5882 : : break;
5883 : : }
5884 : :
5885 : : /* We use one SS term, and use that to determine the bounds of the
5886 : : loop for this dimension. We try to pick the simplest term. */
5887 : 634894 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5888 : : {
5889 : 391833 : gfc_ss_type ss_type;
5890 : :
5891 : 391833 : ss_type = ss->info->type;
5892 : 459793 : if (ss_type == GFC_SS_SCALAR
5893 : 391833 : || ss_type == GFC_SS_TEMP
5894 : 333117 : || ss_type == GFC_SS_REFERENCE
5895 : 324150 : || (ss->info->can_be_null_ref && nonoptional_arr))
5896 : 67960 : continue;
5897 : :
5898 : 323873 : info = &ss->info->data.array;
5899 : 323873 : dim = ss->dim[n];
5900 : :
5901 : 323873 : if (loopspec[n] != NULL)
5902 : : {
5903 : 80812 : specinfo = &loopspec[n]->info->data.array;
5904 : 80812 : spec_dim = loopspec[n]->dim[n];
5905 : : }
5906 : : else
5907 : : {
5908 : : /* Silence uninitialized warnings. */
5909 : : specinfo = NULL;
5910 : : spec_dim = 0;
5911 : : }
5912 : :
5913 : 323873 : if (info->shape)
5914 : : {
5915 : : /* The frontend has worked out the size for us. */
5916 : 221324 : if (!loopspec[n]
5917 : 57990 : || !specinfo->shape
5918 : 267476 : || !integer_zerop (specinfo->start[spec_dim]))
5919 : : /* Prefer zero-based descriptors if possible. */
5920 : 204523 : loopspec[n] = ss;
5921 : 221324 : continue;
5922 : : }
5923 : :
5924 : 102549 : if (ss_type == GFC_SS_CONSTRUCTOR)
5925 : : {
5926 : 1300 : gfc_constructor_base base;
5927 : : /* An unknown size constructor will always be rank one.
5928 : : Higher rank constructors will either have known shape,
5929 : : or still be wrapped in a call to reshape. */
5930 : 1300 : gcc_assert (loop->dimen == 1);
5931 : :
5932 : : /* Always prefer to use the constructor bounds if the size
5933 : : can be determined at compile time. Prefer not to otherwise,
5934 : : since the general case involves realloc, and it's better to
5935 : : avoid that overhead if possible. */
5936 : 1300 : base = ss->info->expr->value.constructor;
5937 : 1300 : dynamic[n] = gfc_get_array_constructor_size (&i, base);
5938 : 1300 : if (!dynamic[n] || !loopspec[n])
5939 : 1113 : loopspec[n] = ss;
5940 : 1300 : continue;
5941 : 1300 : }
5942 : :
5943 : : /* Avoid using an allocatable lhs in an assignment, since
5944 : : there might be a reallocation coming. */
5945 : 101249 : if (loopspec[n] && ss->is_alloc_lhs)
5946 : 8643 : continue;
5947 : :
5948 : 92606 : if (!loopspec[n])
5949 : 78614 : loopspec[n] = ss;
5950 : : /* Criteria for choosing a loop specifier (most important first):
5951 : : doesn't need realloc
5952 : : stride of one
5953 : : known stride
5954 : : known lower bound
5955 : : known upper bound
5956 : : */
5957 : 13992 : else if (loopspec[n]->info->type == GFC_SS_CONSTRUCTOR && dynamic[n])
5958 : 174 : loopspec[n] = ss;
5959 : 13818 : else if (integer_onep (info->stride[dim])
5960 : 13818 : && !integer_onep (specinfo->stride[spec_dim]))
5961 : 120 : loopspec[n] = ss;
5962 : 13698 : else if (INTEGER_CST_P (info->stride[dim])
5963 : 13510 : && !INTEGER_CST_P (specinfo->stride[spec_dim]))
5964 : 0 : loopspec[n] = ss;
5965 : 13698 : else if (INTEGER_CST_P (info->start[dim])
5966 : 4262 : && !INTEGER_CST_P (specinfo->start[spec_dim])
5967 : 744 : && integer_onep (info->stride[dim])
5968 : 372 : == integer_onep (specinfo->stride[spec_dim])
5969 : 13698 : && INTEGER_CST_P (info->stride[dim])
5970 : 345 : == INTEGER_CST_P (specinfo->stride[spec_dim]))
5971 : 345 : loopspec[n] = ss;
5972 : : /* We don't work out the upper bound.
5973 : : else if (INTEGER_CST_P (info->finish[n])
5974 : : && ! INTEGER_CST_P (specinfo->finish[n]))
5975 : : loopspec[n] = ss; */
5976 : : }
5977 : :
5978 : : /* We should have found the scalarization loop specifier. If not,
5979 : : that's bad news. */
5980 : 243061 : gcc_assert (loopspec[n]);
5981 : :
5982 : 243061 : info = &loopspec[n]->info->data.array;
5983 : 243061 : dim = loopspec[n]->dim[n];
5984 : :
5985 : : /* Set the extents of this range. */
5986 : 243061 : cshape = info->shape;
5987 : 243061 : if (cshape && INTEGER_CST_P (info->start[dim])
5988 : 174973 : && INTEGER_CST_P (info->stride[dim]))
5989 : : {
5990 : 174973 : loop->from[n] = info->start[dim];
5991 : 174973 : mpz_set (i, cshape[get_array_ref_dim_for_loop_dim (loopspec[n], n)]);
5992 : 174973 : mpz_sub_ui (i, i, 1);
5993 : : /* To = from + (size - 1) * stride. */
5994 : 174973 : tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
5995 : 174973 : if (!integer_onep (info->stride[dim]))
5996 : 8328 : tmp = fold_build2_loc (input_location, MULT_EXPR,
5997 : : gfc_array_index_type, tmp,
5998 : : info->stride[dim]);
5999 : 174973 : loop->to[n] = fold_build2_loc (input_location, PLUS_EXPR,
6000 : : gfc_array_index_type,
6001 : : loop->from[n], tmp);
6002 : : }
6003 : : else
6004 : : {
6005 : 68088 : loop->from[n] = info->start[dim];
6006 : 68088 : switch (loopspec[n]->info->type)
6007 : : {
6008 : 838 : case GFC_SS_CONSTRUCTOR:
6009 : : /* The upper bound is calculated when we expand the
6010 : : constructor. */
6011 : 838 : gcc_assert (loop->to[n] == NULL_TREE);
6012 : : break;
6013 : :
6014 : 61821 : case GFC_SS_SECTION:
6015 : : /* Use the end expression if it exists and is not constant,
6016 : : so that it is only evaluated once. */
6017 : 61821 : loop->to[n] = info->end[dim];
6018 : 61821 : break;
6019 : :
6020 : 4662 : case GFC_SS_FUNCTION:
6021 : : /* The loop bound will be set when we generate the call. */
6022 : 4662 : gcc_assert (loop->to[n] == NULL_TREE);
6023 : : break;
6024 : :
6025 : 755 : case GFC_SS_INTRINSIC:
6026 : 755 : {
6027 : 755 : gfc_expr *expr = loopspec[n]->info->expr;
6028 : :
6029 : : /* The {l,u}bound of an assumed rank. */
6030 : 755 : if (expr->value.function.isym->id == GFC_ISYM_SHAPE)
6031 : 243 : gcc_assert (expr->value.function.actual->expr->rank == -1);
6032 : : else
6033 : 512 : gcc_assert ((expr->value.function.isym->id == GFC_ISYM_LBOUND
6034 : : || expr->value.function.isym->id == GFC_ISYM_UBOUND)
6035 : : && expr->value.function.actual->next->expr == NULL
6036 : : && expr->value.function.actual->expr->rank == -1);
6037 : :
6038 : 755 : loop->to[n] = info->end[dim];
6039 : 755 : break;
6040 : : }
6041 : :
6042 : 12 : case GFC_SS_COMPONENT:
6043 : 12 : {
6044 : 12 : if (info->end[dim] != NULL_TREE)
6045 : : {
6046 : 12 : loop->to[n] = info->end[dim];
6047 : 12 : break;
6048 : : }
6049 : : else
6050 : 0 : gcc_unreachable ();
6051 : : }
6052 : :
6053 : 0 : default:
6054 : 0 : gcc_unreachable ();
6055 : : }
6056 : : }
6057 : :
6058 : : /* Transform everything so we have a simple incrementing variable. */
6059 : 243061 : if (integer_onep (info->stride[dim]))
6060 : 232814 : info->delta[dim] = gfc_index_zero_node;
6061 : : else
6062 : : {
6063 : : /* Set the delta for this section. */
6064 : 10247 : info->delta[dim] = gfc_evaluate_now (loop->from[n], &outer_loop->pre);
6065 : : /* Number of iterations is (end - start + step) / step.
6066 : : with start = 0, this simplifies to
6067 : : last = end / step;
6068 : : for (i = 0; i<=last; i++){...}; */
6069 : 10247 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
6070 : : gfc_array_index_type, loop->to[n],
6071 : : loop->from[n]);
6072 : 10247 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
6073 : : gfc_array_index_type, tmp, info->stride[dim]);
6074 : 10247 : tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
6075 : : tmp, build_int_cst (gfc_array_index_type, -1));
6076 : 10247 : loop->to[n] = gfc_evaluate_now (tmp, &outer_loop->pre);
6077 : : /* Make the loop variable start at 0. */
6078 : 10247 : loop->from[n] = gfc_index_zero_node;
6079 : : }
6080 : : }
6081 : 178167 : mpz_clear (i);
6082 : :
6083 : 181531 : for (loop = loop->nested; loop; loop = loop->next)
6084 : 3364 : set_loop_bounds (loop);
6085 : 178167 : }
6086 : :
6087 : :
6088 : : /* Last attempt to set the loop bounds, in case they depend on an allocatable
6089 : : function result. */
6090 : :
6091 : : static void
6092 : 178167 : late_set_loop_bounds (gfc_loopinfo *loop)
6093 : : {
6094 : 178167 : int n, dim;
6095 : 178167 : gfc_array_info *info;
6096 : 178167 : gfc_ss **loopspec;
6097 : :
6098 : 178167 : loopspec = loop->specloop;
6099 : :
6100 : 421228 : for (n = 0; n < loop->dimen; n++)
6101 : : {
6102 : : /* Set the extents of this range. */
6103 : 243061 : if (loop->from[n] == NULL_TREE
6104 : 243061 : || loop->to[n] == NULL_TREE)
6105 : : {
6106 : : /* We should have found the scalarization loop specifier. If not,
6107 : : that's bad news. */
6108 : 418 : gcc_assert (loopspec[n]);
6109 : :
6110 : 418 : info = &loopspec[n]->info->data.array;
6111 : 418 : dim = loopspec[n]->dim[n];
6112 : :
6113 : 418 : if (loopspec[n]->info->type == GFC_SS_FUNCTION
6114 : 418 : && info->start[dim]
6115 : 418 : && info->end[dim])
6116 : : {
6117 : 153 : loop->from[n] = info->start[dim];
6118 : 153 : loop->to[n] = info->end[dim];
6119 : : }
6120 : : }
6121 : : }
6122 : :
6123 : 181531 : for (loop = loop->nested; loop; loop = loop->next)
6124 : 3364 : late_set_loop_bounds (loop);
6125 : 178167 : }
6126 : :
6127 : :
6128 : : /* Initialize the scalarization loop. Creates the loop variables. Determines
6129 : : the range of the loop variables. Creates a temporary if required.
6130 : : Also generates code for scalar expressions which have been
6131 : : moved outside the loop. */
6132 : :
6133 : : void
6134 : 174803 : gfc_conv_loop_setup (gfc_loopinfo * loop, locus * where)
6135 : : {
6136 : 174803 : gfc_ss *tmp_ss;
6137 : 174803 : tree tmp;
6138 : :
6139 : 174803 : set_loop_bounds (loop);
6140 : :
6141 : : /* Add all the scalar code that can be taken out of the loops.
6142 : : This may include calculating the loop bounds, so do it before
6143 : : allocating the temporary. */
6144 : 174803 : gfc_add_loop_ss_code (loop, loop->ss, false, where);
6145 : :
6146 : 174803 : late_set_loop_bounds (loop);
6147 : :
6148 : 174803 : tmp_ss = loop->temp_ss;
6149 : : /* If we want a temporary then create it. */
6150 : 174803 : if (tmp_ss != NULL)
6151 : : {
6152 : 11187 : gfc_ss_info *tmp_ss_info;
6153 : :
6154 : 11187 : tmp_ss_info = tmp_ss->info;
6155 : 11187 : gcc_assert (tmp_ss_info->type == GFC_SS_TEMP);
6156 : 11187 : gcc_assert (loop->parent == NULL);
6157 : :
6158 : : /* Make absolutely sure that this is a complete type. */
6159 : 11187 : if (tmp_ss_info->string_length)
6160 : 2666 : tmp_ss_info->data.temp.type
6161 : 2666 : = gfc_get_character_type_len_for_eltype
6162 : 2666 : (TREE_TYPE (tmp_ss_info->data.temp.type),
6163 : : tmp_ss_info->string_length);
6164 : :
6165 : 11187 : tmp = tmp_ss_info->data.temp.type;
6166 : 11187 : memset (&tmp_ss_info->data.array, 0, sizeof (gfc_array_info));
6167 : 11187 : tmp_ss_info->type = GFC_SS_SECTION;
6168 : :
6169 : 11187 : gcc_assert (tmp_ss->dimen != 0);
6170 : :
6171 : 11187 : gfc_trans_create_temp_array (&loop->pre, &loop->post, tmp_ss, tmp,
6172 : : NULL_TREE, false, true, false, where);
6173 : : }
6174 : :
6175 : : /* For array parameters we don't have loop variables, so don't calculate the
6176 : : translations. */
6177 : 174803 : if (!loop->array_parameter)
6178 : 109243 : gfc_set_delta (loop);
6179 : 174803 : }
6180 : :
6181 : :
6182 : : /* Calculates how to transform from loop variables to array indices for each
6183 : : array: once loop bounds are chosen, sets the difference (DELTA field) between
6184 : : loop bounds and array reference bounds, for each array info. */
6185 : :
6186 : : void
6187 : 113038 : gfc_set_delta (gfc_loopinfo *loop)
6188 : : {
6189 : 113038 : gfc_ss *ss, **loopspec;
6190 : 113038 : gfc_array_info *info;
6191 : 113038 : tree tmp;
6192 : 113038 : int n, dim;
6193 : :
6194 : 113038 : gfc_loopinfo * const outer_loop = outermost_loop (loop);
6195 : :
6196 : 113038 : loopspec = loop->specloop;
6197 : :
6198 : : /* Calculate the translation from loop variables to array indices. */
6199 : 342103 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
6200 : : {
6201 : 229065 : gfc_ss_type ss_type;
6202 : :
6203 : 229065 : ss_type = ss->info->type;
6204 : 59059 : if (!(ss_type == GFC_SS_SECTION
6205 : 229065 : || ss_type == GFC_SS_COMPONENT
6206 : 93608 : || ss_type == GFC_SS_CONSTRUCTOR
6207 : : || (ss_type == GFC_SS_FUNCTION
6208 : 8141 : && gfc_is_class_array_function (ss->info->expr))))
6209 : 58907 : continue;
6210 : :
6211 : 170158 : info = &ss->info->data.array;
6212 : :
6213 : 382967 : for (n = 0; n < ss->dimen; n++)
6214 : : {
6215 : : /* If we are specifying the range the delta is already set. */
6216 : 212809 : if (loopspec[n] != ss)
6217 : : {
6218 : 111319 : dim = ss->dim[n];
6219 : :
6220 : : /* Calculate the offset relative to the loop variable.
6221 : : First multiply by the stride. */
6222 : 111319 : tmp = loop->from[n];
6223 : 111319 : if (!integer_onep (info->stride[dim]))
6224 : 2933 : tmp = fold_build2_loc (input_location, MULT_EXPR,
6225 : : gfc_array_index_type,
6226 : : tmp, info->stride[dim]);
6227 : :
6228 : : /* Then subtract this from our starting value. */
6229 : 111319 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
6230 : : gfc_array_index_type,
6231 : : info->start[dim], tmp);
6232 : :
6233 : 111319 : if (ss->is_alloc_lhs)
6234 : 8643 : info->delta[dim] = tmp;
6235 : : else
6236 : 102676 : info->delta[dim] = gfc_evaluate_now (tmp, &outer_loop->pre);
6237 : : }
6238 : : }
6239 : : }
6240 : :
6241 : 116490 : for (loop = loop->nested; loop; loop = loop->next)
6242 : 3452 : gfc_set_delta (loop);
6243 : 113038 : }
6244 : :
6245 : :
6246 : : /* Calculate the size of a given array dimension from the bounds. This
6247 : : is simply (ubound - lbound + 1) if this expression is positive
6248 : : or 0 if it is negative (pick either one if it is zero). Optionally
6249 : : (if or_expr is present) OR the (expression != 0) condition to it. */
6250 : :
6251 : : tree
6252 : 22120 : gfc_conv_array_extent_dim (tree lbound, tree ubound, tree* or_expr)
6253 : : {
6254 : 22120 : tree res;
6255 : 22120 : tree cond;
6256 : :
6257 : : /* Calculate (ubound - lbound + 1). */
6258 : 22120 : res = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6259 : : ubound, lbound);
6260 : 22120 : res = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, res,
6261 : : gfc_index_one_node);
6262 : :
6263 : : /* Check whether the size for this dimension is negative. */
6264 : 22120 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, res,
6265 : : gfc_index_zero_node);
6266 : 22120 : res = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
6267 : : gfc_index_zero_node, res);
6268 : :
6269 : : /* Build OR expression. */
6270 : 22120 : if (or_expr)
6271 : 17205 : *or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
6272 : : logical_type_node, *or_expr, cond);
6273 : :
6274 : 22120 : return res;
6275 : : }
6276 : :
6277 : :
6278 : : /* For an array descriptor, get the total number of elements. This is just
6279 : : the product of the extents along from_dim to to_dim. */
6280 : :
6281 : : static tree
6282 : 1837 : gfc_conv_descriptor_size_1 (tree desc, int from_dim, int to_dim)
6283 : : {
6284 : 1837 : tree res;
6285 : 1837 : int dim;
6286 : :
6287 : 1837 : res = gfc_index_one_node;
6288 : :
6289 : 4474 : for (dim = from_dim; dim < to_dim; ++dim)
6290 : : {
6291 : 2637 : tree lbound;
6292 : 2637 : tree ubound;
6293 : 2637 : tree extent;
6294 : :
6295 : 2637 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
6296 : 2637 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
6297 : :
6298 : 2637 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
6299 : 2637 : res = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6300 : : res, extent);
6301 : : }
6302 : :
6303 : 1837 : return res;
6304 : : }
6305 : :
6306 : :
6307 : : /* Full size of an array. */
6308 : :
6309 : : tree
6310 : 1807 : gfc_conv_descriptor_size (tree desc, int rank)
6311 : : {
6312 : 1807 : return gfc_conv_descriptor_size_1 (desc, 0, rank);
6313 : : }
6314 : :
6315 : :
6316 : : /* Size of a coarray for all dimensions but the last. */
6317 : :
6318 : : tree
6319 : 30 : gfc_conv_descriptor_cosize (tree desc, int rank, int corank)
6320 : : {
6321 : 30 : return gfc_conv_descriptor_size_1 (desc, rank, rank + corank - 1);
6322 : : }
6323 : :
6324 : :
6325 : : /* Fills in an array descriptor, and returns the size of the array.
6326 : : The size will be a simple_val, ie a variable or a constant. Also
6327 : : calculates the offset of the base. The pointer argument overflow,
6328 : : which should be of integer type, will increase in value if overflow
6329 : : occurs during the size calculation. Returns the size of the array.
6330 : : {
6331 : : stride = 1;
6332 : : offset = 0;
6333 : : for (n = 0; n < rank; n++)
6334 : : {
6335 : : a.lbound[n] = specified_lower_bound;
6336 : : offset = offset + a.lbond[n] * stride;
6337 : : size = 1 - lbound;
6338 : : a.ubound[n] = specified_upper_bound;
6339 : : a.stride[n] = stride;
6340 : : size = size >= 0 ? ubound + size : 0; //size = ubound + 1 - lbound
6341 : : overflow += size == 0 ? 0: (MAX/size < stride ? 1: 0);
6342 : : stride = stride * size;
6343 : : }
6344 : : for (n = rank; n < rank+corank; n++)
6345 : : (Set lcobound/ucobound as above.)
6346 : : element_size = sizeof (array element);
6347 : : if (!rank)
6348 : : return element_size
6349 : : stride = (size_t) stride;
6350 : : overflow += element_size == 0 ? 0: (MAX/element_size < stride ? 1: 0);
6351 : : stride = stride * element_size;
6352 : : return (stride);
6353 : : } */
6354 : : /*GCC ARRAYS*/
6355 : :
6356 : : static tree
6357 : 11616 : gfc_array_init_size (tree descriptor, int rank, int corank, tree * poffset,
6358 : : gfc_expr ** lower, gfc_expr ** upper, stmtblock_t * pblock,
6359 : : stmtblock_t * descriptor_block, tree * overflow,
6360 : : tree expr3_elem_size, gfc_expr *expr3, tree expr3_desc,
6361 : : bool e3_has_nodescriptor, gfc_expr *expr,
6362 : : tree *element_size, bool explicit_ts)
6363 : : {
6364 : 11616 : tree type;
6365 : 11616 : tree tmp;
6366 : 11616 : tree size;
6367 : 11616 : tree offset;
6368 : 11616 : tree stride;
6369 : 11616 : tree or_expr;
6370 : 11616 : tree thencase;
6371 : 11616 : tree elsecase;
6372 : 11616 : tree cond;
6373 : 11616 : tree var;
6374 : 11616 : stmtblock_t thenblock;
6375 : 11616 : stmtblock_t elseblock;
6376 : 11616 : gfc_expr *ubound;
6377 : 11616 : gfc_se se;
6378 : 11616 : int n;
6379 : :
6380 : 11616 : type = TREE_TYPE (descriptor);
6381 : :
6382 : 11616 : stride = gfc_index_one_node;
6383 : 11616 : offset = gfc_index_zero_node;
6384 : :
6385 : : /* Set the dtype before the alloc, because registration of coarrays needs
6386 : : it initialized. */
6387 : 11616 : if (expr->ts.type == BT_CHARACTER
6388 : 1013 : && expr->ts.deferred
6389 : 506 : && VAR_P (expr->ts.u.cl->backend_decl))
6390 : : {
6391 : 327 : type = gfc_typenode_for_spec (&expr->ts);
6392 : 327 : tmp = gfc_conv_descriptor_dtype (descriptor);
6393 : 327 : gfc_add_modify (pblock, tmp, gfc_get_dtype_rank_type (rank, type));
6394 : : }
6395 : 11289 : else if (expr->ts.type == BT_CHARACTER
6396 : 686 : && expr->ts.deferred
6397 : 179 : && TREE_CODE (descriptor) == COMPONENT_REF)
6398 : : {
6399 : : /* Deferred character components have their string length tucked away
6400 : : in a hidden field of the derived type. Obtain that and use it to
6401 : : set the dtype. The charlen backend decl is zero because the field
6402 : : type is zero length. */
6403 : 161 : gfc_ref *ref;
6404 : 161 : tmp = NULL_TREE;
6405 : 161 : for (ref = expr->ref; ref; ref = ref->next)
6406 : 161 : if (ref->type == REF_COMPONENT
6407 : 161 : && gfc_deferred_strlen (ref->u.c.component, &tmp))
6408 : : break;
6409 : 161 : gcc_assert (tmp != NULL_TREE);
6410 : 161 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
6411 : 161 : TREE_OPERAND (descriptor, 0), tmp, NULL_TREE);
6412 : 161 : tmp = fold_convert (gfc_charlen_type_node, tmp);
6413 : 161 : type = gfc_get_character_type_len (expr->ts.kind, tmp);
6414 : 161 : tmp = gfc_conv_descriptor_dtype (descriptor);
6415 : 161 : gfc_add_modify (pblock, tmp, gfc_get_dtype_rank_type (rank, type));
6416 : 161 : }
6417 : 11128 : else if (expr3_desc && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr3_desc)))
6418 : : {
6419 : 889 : tmp = gfc_conv_descriptor_dtype (descriptor);
6420 : 889 : gfc_add_modify (pblock, tmp, gfc_conv_descriptor_dtype (expr3_desc));
6421 : : }
6422 : 10239 : else if (expr->ts.type == BT_CLASS && !explicit_ts
6423 : 1250 : && expr3 && expr3->ts.type != BT_CLASS
6424 : 343 : && expr3_elem_size != NULL_TREE && expr3_desc == NULL_TREE)
6425 : : {
6426 : 343 : tmp = gfc_conv_descriptor_elem_len (descriptor);
6427 : 343 : gfc_add_modify (pblock, tmp,
6428 : 343 : fold_convert (TREE_TYPE (tmp), expr3_elem_size));
6429 : : }
6430 : : else
6431 : : {
6432 : 9896 : tmp = gfc_conv_descriptor_dtype (descriptor);
6433 : 9896 : gfc_add_modify (pblock, tmp, gfc_get_dtype (type));
6434 : : }
6435 : :
6436 : 11616 : or_expr = logical_false_node;
6437 : :
6438 : 28821 : for (n = 0; n < rank; n++)
6439 : : {
6440 : 17205 : tree conv_lbound;
6441 : 17205 : tree conv_ubound;
6442 : :
6443 : : /* We have 3 possibilities for determining the size of the array:
6444 : : lower == NULL => lbound = 1, ubound = upper[n]
6445 : : upper[n] = NULL => lbound = 1, ubound = lower[n]
6446 : : upper[n] != NULL => lbound = lower[n], ubound = upper[n] */
6447 : 17205 : ubound = upper[n];
6448 : :
6449 : : /* Set lower bound. */
6450 : 17205 : gfc_init_se (&se, NULL);
6451 : 17205 : if (expr3_desc != NULL_TREE)
6452 : : {
6453 : 1358 : if (e3_has_nodescriptor)
6454 : : /* The lbound of nondescriptor arrays like array constructors,
6455 : : nonallocatable/nonpointer function results/variables,
6456 : : start at zero, but when allocating it, the standard expects
6457 : : the array to start at one. */
6458 : 869 : se.expr = gfc_index_one_node;
6459 : : else
6460 : 489 : se.expr = gfc_conv_descriptor_lbound_get (expr3_desc,
6461 : : gfc_rank_cst[n]);
6462 : : }
6463 : 15847 : else if (lower == NULL)
6464 : 12801 : se.expr = gfc_index_one_node;
6465 : : else
6466 : : {
6467 : 3046 : gcc_assert (lower[n]);
6468 : 3046 : if (ubound)
6469 : : {
6470 : 2381 : gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
6471 : 2381 : gfc_add_block_to_block (pblock, &se.pre);
6472 : : }
6473 : : else
6474 : : {
6475 : 665 : se.expr = gfc_index_one_node;
6476 : 665 : ubound = lower[n];
6477 : : }
6478 : : }
6479 : 17205 : gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
6480 : : gfc_rank_cst[n], se.expr);
6481 : 17205 : conv_lbound = se.expr;
6482 : :
6483 : : /* Work out the offset for this component. */
6484 : 17205 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6485 : : se.expr, stride);
6486 : 17205 : offset = fold_build2_loc (input_location, MINUS_EXPR,
6487 : : gfc_array_index_type, offset, tmp);
6488 : :
6489 : : /* Set upper bound. */
6490 : 17205 : gfc_init_se (&se, NULL);
6491 : 17205 : if (expr3_desc != NULL_TREE)
6492 : : {
6493 : 1358 : if (e3_has_nodescriptor)
6494 : : {
6495 : : /* The lbound of nondescriptor arrays like array constructors,
6496 : : nonallocatable/nonpointer function results/variables,
6497 : : start at zero, but when allocating it, the standard expects
6498 : : the array to start at one. Therefore fix the upper bound to be
6499 : : (desc.ubound - desc.lbound) + 1. */
6500 : 869 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
6501 : : gfc_array_index_type,
6502 : : gfc_conv_descriptor_ubound_get (
6503 : : expr3_desc, gfc_rank_cst[n]),
6504 : : gfc_conv_descriptor_lbound_get (
6505 : : expr3_desc, gfc_rank_cst[n]));
6506 : 869 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
6507 : : gfc_array_index_type, tmp,
6508 : : gfc_index_one_node);
6509 : 869 : se.expr = gfc_evaluate_now (tmp, pblock);
6510 : : }
6511 : : else
6512 : 489 : se.expr = gfc_conv_descriptor_ubound_get (expr3_desc,
6513 : : gfc_rank_cst[n]);
6514 : : }
6515 : : else
6516 : : {
6517 : 15847 : gcc_assert (ubound);
6518 : 15847 : gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
6519 : 15847 : gfc_add_block_to_block (pblock, &se.pre);
6520 : 15847 : if (ubound->expr_type == EXPR_FUNCTION)
6521 : 712 : se.expr = gfc_evaluate_now (se.expr, pblock);
6522 : : }
6523 : 17205 : gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
6524 : : gfc_rank_cst[n], se.expr);
6525 : 17205 : conv_ubound = se.expr;
6526 : :
6527 : : /* Store the stride. */
6528 : 17205 : gfc_conv_descriptor_stride_set (descriptor_block, descriptor,
6529 : : gfc_rank_cst[n], stride);
6530 : :
6531 : : /* Calculate size and check whether extent is negative. */
6532 : 17205 : size = gfc_conv_array_extent_dim (conv_lbound, conv_ubound, &or_expr);
6533 : 17205 : size = gfc_evaluate_now (size, pblock);
6534 : :
6535 : : /* Check whether multiplying the stride by the number of
6536 : : elements in this dimension would overflow. We must also check
6537 : : whether the current dimension has zero size in order to avoid
6538 : : division by zero.
6539 : : */
6540 : 17205 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6541 : : gfc_array_index_type,
6542 : 17205 : fold_convert (gfc_array_index_type,
6543 : : TYPE_MAX_VALUE (gfc_array_index_type)),
6544 : : size);
6545 : 17205 : cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
6546 : : logical_type_node, tmp, stride),
6547 : : PRED_FORTRAN_OVERFLOW);
6548 : 17205 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6549 : : integer_one_node, integer_zero_node);
6550 : 17205 : cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
6551 : : logical_type_node, size,
6552 : : gfc_index_zero_node),
6553 : : PRED_FORTRAN_SIZE_ZERO);
6554 : 17205 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6555 : : integer_zero_node, tmp);
6556 : 17205 : tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
6557 : : *overflow, tmp);
6558 : 17205 : *overflow = gfc_evaluate_now (tmp, pblock);
6559 : :
6560 : : /* Multiply the stride by the number of elements in this dimension. */
6561 : 17205 : stride = fold_build2_loc (input_location, MULT_EXPR,
6562 : : gfc_array_index_type, stride, size);
6563 : 17205 : stride = gfc_evaluate_now (stride, pblock);
6564 : : }
6565 : :
6566 : 12145 : for (n = rank; n < rank + corank; n++)
6567 : : {
6568 : 529 : ubound = upper[n];
6569 : :
6570 : : /* Set lower bound. */
6571 : 529 : gfc_init_se (&se, NULL);
6572 : 529 : if (lower == NULL || lower[n] == NULL)
6573 : : {
6574 : 305 : gcc_assert (n == rank + corank - 1);
6575 : 305 : se.expr = gfc_index_one_node;
6576 : : }
6577 : : else
6578 : : {
6579 : 224 : if (ubound || n == rank + corank - 1)
6580 : : {
6581 : 142 : gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
6582 : 142 : gfc_add_block_to_block (pblock, &se.pre);
6583 : : }
6584 : : else
6585 : : {
6586 : 82 : se.expr = gfc_index_one_node;
6587 : 82 : ubound = lower[n];
6588 : : }
6589 : : }
6590 : 529 : gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
6591 : : gfc_rank_cst[n], se.expr);
6592 : :
6593 : 529 : if (n < rank + corank - 1)
6594 : : {
6595 : 151 : gfc_init_se (&se, NULL);
6596 : 151 : gcc_assert (ubound);
6597 : 151 : gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
6598 : 151 : gfc_add_block_to_block (pblock, &se.pre);
6599 : 151 : gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
6600 : : gfc_rank_cst[n], se.expr);
6601 : : }
6602 : : }
6603 : :
6604 : : /* The stride is the number of elements in the array, so multiply by the
6605 : : size of an element to get the total size. Obviously, if there is a
6606 : : SOURCE expression (expr3) we must use its element size. */
6607 : 11616 : if (expr3_elem_size != NULL_TREE)
6608 : 2892 : tmp = expr3_elem_size;
6609 : 8724 : else if (expr3 != NULL)
6610 : : {
6611 : 0 : if (expr3->ts.type == BT_CLASS)
6612 : : {
6613 : 0 : gfc_se se_sz;
6614 : 0 : gfc_expr *sz = gfc_copy_expr (expr3);
6615 : 0 : gfc_add_vptr_component (sz);
6616 : 0 : gfc_add_size_component (sz);
6617 : 0 : gfc_init_se (&se_sz, NULL);
6618 : 0 : gfc_conv_expr (&se_sz, sz);
6619 : 0 : gfc_free_expr (sz);
6620 : 0 : tmp = se_sz.expr;
6621 : : }
6622 : : else
6623 : : {
6624 : 0 : tmp = gfc_typenode_for_spec (&expr3->ts);
6625 : 0 : tmp = TYPE_SIZE_UNIT (tmp);
6626 : : }
6627 : : }
6628 : : else
6629 : 8724 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
6630 : :
6631 : : /* Convert to size_t. */
6632 : 11616 : *element_size = fold_convert (size_type_node, tmp);
6633 : :
6634 : 11616 : if (rank == 0)
6635 : : return *element_size;
6636 : :
6637 : 11453 : stride = fold_convert (size_type_node, stride);
6638 : :
6639 : : /* First check for overflow. Since an array of type character can
6640 : : have zero element_size, we must check for that before
6641 : : dividing. */
6642 : 11453 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6643 : : size_type_node,
6644 : 11453 : TYPE_MAX_VALUE (size_type_node), *element_size);
6645 : 11453 : cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
6646 : : logical_type_node, tmp, stride),
6647 : : PRED_FORTRAN_OVERFLOW);
6648 : 11453 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6649 : : integer_one_node, integer_zero_node);
6650 : 11453 : cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
6651 : : logical_type_node, *element_size,
6652 : : build_int_cst (size_type_node, 0)),
6653 : : PRED_FORTRAN_SIZE_ZERO);
6654 : 11453 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6655 : : integer_zero_node, tmp);
6656 : 11453 : tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
6657 : : *overflow, tmp);
6658 : 11453 : *overflow = gfc_evaluate_now (tmp, pblock);
6659 : :
6660 : 11453 : size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6661 : : stride, *element_size);
6662 : :
6663 : 11453 : if (poffset != NULL)
6664 : : {
6665 : 11453 : offset = gfc_evaluate_now (offset, pblock);
6666 : 11453 : *poffset = offset;
6667 : : }
6668 : :
6669 : 11453 : if (integer_zerop (or_expr))
6670 : : return size;
6671 : 3480 : if (integer_onep (or_expr))
6672 : 599 : return build_int_cst (size_type_node, 0);
6673 : :
6674 : 2881 : var = gfc_create_var (TREE_TYPE (size), "size");
6675 : 2881 : gfc_start_block (&thenblock);
6676 : 2881 : gfc_add_modify (&thenblock, var, build_int_cst (size_type_node, 0));
6677 : 2881 : thencase = gfc_finish_block (&thenblock);
6678 : :
6679 : 2881 : gfc_start_block (&elseblock);
6680 : 2881 : gfc_add_modify (&elseblock, var, size);
6681 : 2881 : elsecase = gfc_finish_block (&elseblock);
6682 : :
6683 : 2881 : tmp = gfc_evaluate_now (or_expr, pblock);
6684 : 2881 : tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
6685 : 2881 : gfc_add_expr_to_block (pblock, tmp);
6686 : :
6687 : 2881 : return var;
6688 : : }
6689 : :
6690 : :
6691 : : /* Retrieve the last ref from the chain. This routine is specific to
6692 : : gfc_array_allocate ()'s needs. */
6693 : :
6694 : : bool
6695 : 17784 : retrieve_last_ref (gfc_ref **ref_in, gfc_ref **prev_ref_in)
6696 : : {
6697 : 17784 : gfc_ref *ref, *prev_ref;
6698 : :
6699 : 17784 : ref = *ref_in;
6700 : : /* Prevent warnings for uninitialized variables. */
6701 : 17784 : prev_ref = *prev_ref_in;
6702 : 24265 : while (ref && ref->next != NULL)
6703 : : {
6704 : 6481 : gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT
6705 : : || (ref->u.ar.dimen == 0 && ref->u.ar.codimen > 0));
6706 : : prev_ref = ref;
6707 : : ref = ref->next;
6708 : : }
6709 : :
6710 : 17784 : if (ref == NULL || ref->type != REF_ARRAY)
6711 : : return false;
6712 : :
6713 : 12764 : *ref_in = ref;
6714 : 12764 : *prev_ref_in = prev_ref;
6715 : 12764 : return true;
6716 : : }
6717 : :
6718 : : /* Initializes the descriptor and generates a call to _gfor_allocate. Does
6719 : : the work for an ALLOCATE statement. */
6720 : : /*GCC ARRAYS*/
6721 : :
6722 : : bool
6723 : 16636 : gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree status, tree errmsg,
6724 : : tree errlen, tree label_finish, tree expr3_elem_size,
6725 : : gfc_expr *expr3, tree e3_arr_desc, bool e3_has_nodescriptor,
6726 : : gfc_omp_namelist *omp_alloc, bool explicit_ts)
6727 : : {
6728 : 16636 : tree tmp;
6729 : 16636 : tree pointer;
6730 : 16636 : tree offset = NULL_TREE;
6731 : 16636 : tree token = NULL_TREE;
6732 : 16636 : tree size;
6733 : 16636 : tree msg;
6734 : 16636 : tree error = NULL_TREE;
6735 : 16636 : tree overflow; /* Boolean storing whether size calculation overflows. */
6736 : 16636 : tree var_overflow = NULL_TREE;
6737 : 16636 : tree cond;
6738 : 16636 : tree set_descriptor;
6739 : 16636 : tree not_prev_allocated = NULL_TREE;
6740 : 16636 : tree element_size = NULL_TREE;
6741 : 16636 : stmtblock_t set_descriptor_block;
6742 : 16636 : stmtblock_t elseblock;
6743 : 16636 : gfc_expr **lower;
6744 : 16636 : gfc_expr **upper;
6745 : 16636 : gfc_ref *ref, *prev_ref = NULL, *coref;
6746 : 16636 : bool allocatable, coarray, dimension, alloc_w_e3_arr_spec = false,
6747 : : non_ulimate_coarray_ptr_comp;
6748 : 16636 : tree omp_cond = NULL_TREE, omp_alt_alloc = NULL_TREE;
6749 : :
6750 : 16636 : ref = expr->ref;
6751 : :
6752 : : /* Find the last reference in the chain. */
6753 : 16636 : if (!retrieve_last_ref (&ref, &prev_ref))
6754 : : return false;
6755 : :
6756 : : /* Take the allocatable and coarray properties solely from the expr-ref's
6757 : : attributes and not from source=-expression. */
6758 : 11616 : if (!prev_ref)
6759 : : {
6760 : 8102 : allocatable = expr->symtree->n.sym->attr.allocatable;
6761 : 8102 : dimension = expr->symtree->n.sym->attr.dimension;
6762 : 8102 : non_ulimate_coarray_ptr_comp = false;
6763 : : }
6764 : : else
6765 : : {
6766 : 3514 : allocatable = prev_ref->u.c.component->attr.allocatable;
6767 : : /* Pointer components in coarrayed derived types must be treated
6768 : : specially in that they are registered without a check if the are
6769 : : already associated. This does not hold for ultimate coarray
6770 : : pointers. */
6771 : 7028 : non_ulimate_coarray_ptr_comp = (prev_ref->u.c.component->attr.pointer
6772 : 3514 : && !prev_ref->u.c.component->attr.codimension);
6773 : 3514 : dimension = prev_ref->u.c.component->attr.dimension;
6774 : : }
6775 : :
6776 : : /* For allocatable/pointer arrays in derived types, one of the refs has to be
6777 : : a coarray. In this case it does not matter whether we are on this_image
6778 : : or not. */
6779 : 11616 : coarray = false;
6780 : 27569 : for (coref = expr->ref; coref; coref = coref->next)
6781 : 16493 : if (coref->type == REF_ARRAY && coref->u.ar.codimen > 0)
6782 : : {
6783 : : coarray = true;
6784 : : break;
6785 : : }
6786 : :
6787 : 11616 : if (!dimension)
6788 : 163 : gcc_assert (coarray);
6789 : :
6790 : 11616 : if (ref->u.ar.type == AR_FULL && expr3 != NULL)
6791 : : {
6792 : 1148 : gfc_ref *old_ref = ref;
6793 : : /* F08:C633: Array shape from expr3. */
6794 : 1148 : ref = expr3->ref;
6795 : :
6796 : : /* Find the last reference in the chain. */
6797 : 1148 : if (!retrieve_last_ref (&ref, &prev_ref))
6798 : : {
6799 : 0 : if (expr3->expr_type == EXPR_FUNCTION
6800 : 0 : && gfc_expr_attr (expr3).dimension)
6801 : 0 : ref = old_ref;
6802 : : else
6803 : 0 : return false;
6804 : : }
6805 : : alloc_w_e3_arr_spec = true;
6806 : : }
6807 : :
6808 : : /* Figure out the size of the array. */
6809 : 11616 : switch (ref->u.ar.type)
6810 : : {
6811 : 8849 : case AR_ELEMENT:
6812 : 8849 : if (!coarray)
6813 : : {
6814 : 8355 : lower = NULL;
6815 : 8355 : upper = ref->u.ar.start;
6816 : 8355 : break;
6817 : : }
6818 : : /* Fall through. */
6819 : :
6820 : 2143 : case AR_SECTION:
6821 : 2143 : lower = ref->u.ar.start;
6822 : 2143 : upper = ref->u.ar.end;
6823 : 2143 : break;
6824 : :
6825 : 1118 : case AR_FULL:
6826 : 1118 : gcc_assert (ref->u.ar.as->type == AS_EXPLICIT
6827 : : || alloc_w_e3_arr_spec);
6828 : :
6829 : 1118 : lower = ref->u.ar.as->lower;
6830 : 1118 : upper = ref->u.ar.as->upper;
6831 : 1118 : break;
6832 : :
6833 : 0 : default:
6834 : 0 : gcc_unreachable ();
6835 : 11616 : break;
6836 : : }
6837 : :
6838 : 11616 : overflow = integer_zero_node;
6839 : :
6840 : 11616 : if (expr->ts.type == BT_CHARACTER
6841 : 1013 : && TREE_CODE (se->string_length) == COMPONENT_REF
6842 : 161 : && expr->ts.u.cl->backend_decl != se->string_length
6843 : 161 : && VAR_P (expr->ts.u.cl->backend_decl))
6844 : 0 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
6845 : 0 : fold_convert (TREE_TYPE (expr->ts.u.cl->backend_decl),
6846 : : se->string_length));
6847 : :
6848 : 11616 : gfc_init_block (&set_descriptor_block);
6849 : : /* Take the corank only from the actual ref and not from the coref. The
6850 : : later will mislead the generation of the array dimensions for allocatable/
6851 : : pointer components in derived types. */
6852 : 22624 : size = gfc_array_init_size (se->expr, alloc_w_e3_arr_spec ? expr->rank
6853 : 10468 : : ref->u.ar.as->rank,
6854 : 540 : coarray ? ref->u.ar.as->corank : 0,
6855 : : &offset, lower, upper,
6856 : : &se->pre, &set_descriptor_block, &overflow,
6857 : : expr3_elem_size, expr3, e3_arr_desc,
6858 : : e3_has_nodescriptor, expr, &element_size,
6859 : : explicit_ts);
6860 : :
6861 : 11616 : if (dimension)
6862 : : {
6863 : 11453 : var_overflow = gfc_create_var (integer_type_node, "overflow");
6864 : 11453 : gfc_add_modify (&se->pre, var_overflow, overflow);
6865 : :
6866 : 11453 : if (status == NULL_TREE)
6867 : : {
6868 : : /* Generate the block of code handling overflow. */
6869 : 11242 : msg = gfc_build_addr_expr (pchar_type_node,
6870 : : gfc_build_localized_cstring_const
6871 : : ("Integer overflow when calculating the amount of "
6872 : : "memory to allocate"));
6873 : 11242 : error = build_call_expr_loc (input_location,
6874 : : gfor_fndecl_runtime_error, 1, msg);
6875 : : }
6876 : : else
6877 : : {
6878 : 211 : tree status_type = TREE_TYPE (status);
6879 : 211 : stmtblock_t set_status_block;
6880 : :
6881 : 211 : gfc_start_block (&set_status_block);
6882 : 211 : gfc_add_modify (&set_status_block, status,
6883 : : build_int_cst (status_type, LIBERROR_ALLOCATION));
6884 : 211 : error = gfc_finish_block (&set_status_block);
6885 : : }
6886 : : }
6887 : :
6888 : : /* Allocate memory to store the data. */
6889 : 11616 : if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
6890 : 0 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
6891 : :
6892 : 11616 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
6893 : : {
6894 : 305 : pointer = non_ulimate_coarray_ptr_comp ? se->expr
6895 : 236 : : gfc_conv_descriptor_data_get (se->expr);
6896 : 305 : token = gfc_conv_descriptor_token (se->expr);
6897 : 305 : token = gfc_build_addr_expr (NULL_TREE, token);
6898 : : }
6899 : : else
6900 : : {
6901 : 11311 : pointer = gfc_conv_descriptor_data_get (se->expr);
6902 : 11311 : if (omp_alloc)
6903 : 33 : omp_cond = boolean_true_node;
6904 : : }
6905 : 11616 : STRIP_NOPS (pointer);
6906 : :
6907 : 11616 : if (allocatable)
6908 : : {
6909 : 9542 : not_prev_allocated = gfc_create_var (logical_type_node,
6910 : : "not_prev_allocated");
6911 : 9542 : tmp = fold_build2_loc (input_location, EQ_EXPR,
6912 : : logical_type_node, pointer,
6913 : 9542 : build_int_cst (TREE_TYPE (pointer), 0));
6914 : :
6915 : 9542 : gfc_add_modify (&se->pre, not_prev_allocated, tmp);
6916 : : }
6917 : :
6918 : 11616 : gfc_start_block (&elseblock);
6919 : :
6920 : 11616 : tree succ_add_expr = NULL_TREE;
6921 : 11616 : if (omp_cond)
6922 : : {
6923 : 33 : tree align, alloc, sz;
6924 : 33 : gfc_se se2;
6925 : 33 : if (omp_alloc->u2.allocator)
6926 : : {
6927 : 10 : gfc_init_se (&se2, NULL);
6928 : 10 : gfc_conv_expr (&se2, omp_alloc->u2.allocator);
6929 : 10 : gfc_add_block_to_block (&elseblock, &se2.pre);
6930 : 10 : alloc = gfc_evaluate_now (se2.expr, &elseblock);
6931 : 10 : gfc_add_block_to_block (&elseblock, &se2.post);
6932 : : }
6933 : : else
6934 : 23 : alloc = build_zero_cst (ptr_type_node);
6935 : 33 : tmp = TREE_TYPE (TREE_TYPE (pointer));
6936 : 33 : if (tmp == void_type_node)
6937 : 33 : tmp = gfc_typenode_for_spec (&expr->ts, 0);
6938 : 33 : if (omp_alloc->u.align)
6939 : : {
6940 : 17 : gfc_init_se (&se2, NULL);
6941 : 17 : gfc_conv_expr (&se2, omp_alloc->u.align);
6942 : 17 : gcc_assert (CONSTANT_CLASS_P (se2.expr)
6943 : : && se2.pre.head == NULL
6944 : : && se2.post.head == NULL);
6945 : 17 : align = build_int_cst (size_type_node,
6946 : 17 : MAX (tree_to_uhwi (se2.expr),
6947 : : TYPE_ALIGN_UNIT (tmp)));
6948 : : }
6949 : : else
6950 : 16 : align = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (tmp));
6951 : 33 : sz = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
6952 : : fold_convert (size_type_node, size),
6953 : : build_int_cst (size_type_node, 1));
6954 : 33 : omp_alt_alloc = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
6955 : 33 : DECL_ATTRIBUTES (omp_alt_alloc)
6956 : 33 : = tree_cons (get_identifier ("omp allocator"),
6957 : : build_tree_list (NULL_TREE, alloc),
6958 : 33 : DECL_ATTRIBUTES (omp_alt_alloc));
6959 : 33 : omp_alt_alloc = build_call_expr (omp_alt_alloc, 3, align, sz, alloc);
6960 : 33 : succ_add_expr = fold_build2_loc (input_location, MODIFY_EXPR,
6961 : : void_type_node,
6962 : : gfc_conv_descriptor_version (se->expr),
6963 : : build_int_cst (integer_type_node, 1));
6964 : : }
6965 : :
6966 : : /* The allocatable variant takes the old pointer as first argument. */
6967 : 11616 : if (allocatable)
6968 : 10010 : gfc_allocate_allocatable (&elseblock, pointer, size, token,
6969 : : status, errmsg, errlen, label_finish, expr,
6970 : 468 : coref != NULL ? coref->u.ar.as->corank : 0,
6971 : : omp_cond, omp_alt_alloc, succ_add_expr);
6972 : 2074 : else if (non_ulimate_coarray_ptr_comp && token)
6973 : : /* The token is set only for GFC_FCOARRAY_LIB mode. */
6974 : 69 : gfc_allocate_using_caf_lib (&elseblock, pointer, size, token, status,
6975 : : errmsg, errlen,
6976 : : GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY);
6977 : : else
6978 : 2005 : gfc_allocate_using_malloc (&elseblock, pointer, size, status,
6979 : : omp_cond, omp_alt_alloc, succ_add_expr);
6980 : :
6981 : 11616 : if (dimension)
6982 : : {
6983 : 11453 : cond = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
6984 : : logical_type_node, var_overflow, integer_zero_node),
6985 : : PRED_FORTRAN_OVERFLOW);
6986 : 11453 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6987 : : error, gfc_finish_block (&elseblock));
6988 : : }
6989 : : else
6990 : 163 : tmp = gfc_finish_block (&elseblock);
6991 : :
6992 : 11616 : gfc_add_expr_to_block (&se->pre, tmp);
6993 : :
6994 : : /* Update the array descriptor with the offset and the span. */
6995 : 11616 : if (dimension)
6996 : : {
6997 : 11453 : gfc_conv_descriptor_offset_set (&set_descriptor_block, se->expr, offset);
6998 : 11453 : tmp = fold_convert (gfc_array_index_type, element_size);
6999 : 11453 : gfc_conv_descriptor_span_set (&set_descriptor_block, se->expr, tmp);
7000 : : }
7001 : :
7002 : 11616 : set_descriptor = gfc_finish_block (&set_descriptor_block);
7003 : 11616 : if (status != NULL_TREE)
7004 : : {
7005 : 225 : cond = fold_build2_loc (input_location, EQ_EXPR,
7006 : : logical_type_node, status,
7007 : 225 : build_int_cst (TREE_TYPE (status), 0));
7008 : :
7009 : 225 : if (not_prev_allocated != NULL_TREE)
7010 : 209 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
7011 : : logical_type_node, cond, not_prev_allocated);
7012 : :
7013 : 225 : gfc_add_expr_to_block (&se->pre,
7014 : : fold_build3_loc (input_location, COND_EXPR, void_type_node,
7015 : : cond,
7016 : : set_descriptor,
7017 : : build_empty_stmt (input_location)));
7018 : : }
7019 : : else
7020 : 11391 : gfc_add_expr_to_block (&se->pre, set_descriptor);
7021 : :
7022 : : return true;
7023 : : }
7024 : :
7025 : :
7026 : : /* Create an array constructor from an initialization expression.
7027 : : We assume the frontend already did any expansions and conversions. */
7028 : :
7029 : : tree
7030 : 7444 : gfc_conv_array_initializer (tree type, gfc_expr * expr)
7031 : : {
7032 : 7444 : gfc_constructor *c;
7033 : 7444 : tree tmp;
7034 : 7444 : gfc_se se;
7035 : 7444 : tree index, range;
7036 : 7444 : vec<constructor_elt, va_gc> *v = NULL;
7037 : :
7038 : 7444 : if (expr->expr_type == EXPR_VARIABLE
7039 : 0 : && expr->symtree->n.sym->attr.flavor == FL_PARAMETER
7040 : 0 : && expr->symtree->n.sym->value)
7041 : 7444 : expr = expr->symtree->n.sym->value;
7042 : :
7043 : 7444 : switch (expr->expr_type)
7044 : : {
7045 : 1091 : case EXPR_CONSTANT:
7046 : 1091 : case EXPR_STRUCTURE:
7047 : : /* A single scalar or derived type value. Create an array with all
7048 : : elements equal to that value. */
7049 : 1091 : gfc_init_se (&se, NULL);
7050 : :
7051 : 1091 : if (expr->expr_type == EXPR_CONSTANT)
7052 : 377 : gfc_conv_constant (&se, expr);
7053 : : else
7054 : 714 : gfc_conv_structure (&se, expr, 1);
7055 : :
7056 : 2182 : if (tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7057 : 1091 : TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
7058 : : break;
7059 : 2158 : else if (tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
7060 : 1079 : TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
7061 : 144 : range = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7062 : : else
7063 : 1870 : range = build2 (RANGE_EXPR, gfc_array_index_type,
7064 : 935 : TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
7065 : 935 : TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
7066 : 1079 : CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
7067 : 1079 : break;
7068 : :
7069 : 6353 : case EXPR_ARRAY:
7070 : : /* Create a vector of all the elements. */
7071 : 6353 : for (c = gfc_constructor_first (expr->value.constructor);
7072 : 163354 : c && c->expr; c = gfc_constructor_next (c))
7073 : : {
7074 : 157001 : if (c->iterator)
7075 : : {
7076 : : /* Problems occur when we get something like
7077 : : integer :: a(lots) = (/(i, i=1, lots)/) */
7078 : 0 : gfc_fatal_error ("The number of elements in the array "
7079 : : "constructor at %L requires an increase of "
7080 : : "the allowed %d upper limit. See "
7081 : : "%<-fmax-array-constructor%> option",
7082 : : &expr->where, flag_max_array_constructor);
7083 : : return NULL_TREE;
7084 : : }
7085 : 157001 : if (mpz_cmp_si (c->offset, 0) != 0)
7086 : 150905 : index = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
7087 : : else
7088 : : index = NULL_TREE;
7089 : :
7090 : 157001 : if (mpz_cmp_si (c->repeat, 1) > 0)
7091 : : {
7092 : 127 : tree tmp1, tmp2;
7093 : 127 : mpz_t maxval;
7094 : :
7095 : 127 : mpz_init (maxval);
7096 : 127 : mpz_add (maxval, c->offset, c->repeat);
7097 : 127 : mpz_sub_ui (maxval, maxval, 1);
7098 : 127 : tmp2 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
7099 : 127 : if (mpz_cmp_si (c->offset, 0) != 0)
7100 : : {
7101 : 27 : mpz_add_ui (maxval, c->offset, 1);
7102 : 27 : tmp1 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
7103 : : }
7104 : : else
7105 : 100 : tmp1 = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
7106 : :
7107 : 127 : range = fold_build2 (RANGE_EXPR, gfc_array_index_type, tmp1, tmp2);
7108 : 127 : mpz_clear (maxval);
7109 : : }
7110 : : else
7111 : : range = NULL;
7112 : :
7113 : 157001 : gfc_init_se (&se, NULL);
7114 : 157001 : switch (c->expr->expr_type)
7115 : : {
7116 : 155717 : case EXPR_CONSTANT:
7117 : 155717 : gfc_conv_constant (&se, c->expr);
7118 : :
7119 : : /* See gfortran.dg/charlen_15.f90 for instance. */
7120 : 155717 : if (TREE_CODE (se.expr) == STRING_CST
7121 : 5050 : && TREE_CODE (type) == ARRAY_TYPE)
7122 : : {
7123 : : tree atype = type;
7124 : 10100 : while (TREE_CODE (TREE_TYPE (atype)) == ARRAY_TYPE)
7125 : 5050 : atype = TREE_TYPE (atype);
7126 : 5050 : gcc_checking_assert (TREE_CODE (TREE_TYPE (atype))
7127 : : == INTEGER_TYPE);
7128 : 5050 : gcc_checking_assert (TREE_TYPE (TREE_TYPE (se.expr))
7129 : : == TREE_TYPE (atype));
7130 : 5050 : if (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (se.expr)))
7131 : 5050 : > tree_to_uhwi (TYPE_SIZE_UNIT (atype)))
7132 : : {
7133 : 0 : unsigned HOST_WIDE_INT size
7134 : 0 : = tree_to_uhwi (TYPE_SIZE_UNIT (atype));
7135 : 0 : const char *p = TREE_STRING_POINTER (se.expr);
7136 : :
7137 : 0 : se.expr = build_string (size, p);
7138 : : }
7139 : 5050 : TREE_TYPE (se.expr) = atype;
7140 : : }
7141 : : break;
7142 : :
7143 : 1284 : case EXPR_STRUCTURE:
7144 : 1284 : gfc_conv_structure (&se, c->expr, 1);
7145 : 1284 : break;
7146 : :
7147 : 0 : default:
7148 : : /* Catch those occasional beasts that do not simplify
7149 : : for one reason or another, assuming that if they are
7150 : : standard defying the frontend will catch them. */
7151 : 0 : gfc_conv_expr (&se, c->expr);
7152 : 0 : break;
7153 : : }
7154 : :
7155 : 157001 : if (range == NULL_TREE)
7156 : 156874 : CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
7157 : : else
7158 : : {
7159 : 127 : if (index != NULL_TREE)
7160 : 27 : CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
7161 : 157128 : CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
7162 : : }
7163 : : }
7164 : : break;
7165 : :
7166 : 0 : case EXPR_NULL:
7167 : 0 : return gfc_build_null_descriptor (type);
7168 : :
7169 : 0 : default:
7170 : 0 : gcc_unreachable ();
7171 : : }
7172 : :
7173 : : /* Create a constructor from the list of elements. */
7174 : 7444 : tmp = build_constructor (type, v);
7175 : 7444 : TREE_CONSTANT (tmp) = 1;
7176 : 7444 : return tmp;
7177 : : }
7178 : :
7179 : :
7180 : : /* Generate code to evaluate non-constant coarray cobounds. */
7181 : :
7182 : : void
7183 : 19497 : gfc_trans_array_cobounds (tree type, stmtblock_t * pblock,
7184 : : const gfc_symbol *sym)
7185 : : {
7186 : 19497 : int dim;
7187 : 19497 : tree ubound;
7188 : 19497 : tree lbound;
7189 : 19497 : gfc_se se;
7190 : 19497 : gfc_array_spec *as;
7191 : :
7192 : 19497 : as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
7193 : :
7194 : 20260 : for (dim = as->rank; dim < as->rank + as->corank; dim++)
7195 : : {
7196 : : /* Evaluate non-constant array bound expressions.
7197 : : F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
7198 : : references a function, the result is finalized before execution of the
7199 : : executable constructs in the scoping unit.
7200 : : Adding the finalblocks enables this. */
7201 : 763 : lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
7202 : 763 : if (as->lower[dim] && !INTEGER_CST_P (lbound))
7203 : : {
7204 : 96 : gfc_init_se (&se, NULL);
7205 : 96 : gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
7206 : 96 : gfc_add_block_to_block (pblock, &se.pre);
7207 : 96 : gfc_add_block_to_block (pblock, &se.finalblock);
7208 : 96 : gfc_add_modify (pblock, lbound, se.expr);
7209 : : }
7210 : 763 : ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
7211 : 763 : if (as->upper[dim] && !INTEGER_CST_P (ubound))
7212 : : {
7213 : 50 : gfc_init_se (&se, NULL);
7214 : 50 : gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
7215 : 50 : gfc_add_block_to_block (pblock, &se.pre);
7216 : 50 : gfc_add_block_to_block (pblock, &se.finalblock);
7217 : 50 : gfc_add_modify (pblock, ubound, se.expr);
7218 : : }
7219 : : }
7220 : 19497 : }
7221 : :
7222 : :
7223 : : /* Generate code to evaluate non-constant array bounds. Sets *poffset and
7224 : : returns the size (in elements) of the array. */
7225 : :
7226 : : tree
7227 : 12588 : gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
7228 : : stmtblock_t * pblock)
7229 : : {
7230 : 12588 : gfc_array_spec *as;
7231 : 12588 : tree size;
7232 : 12588 : tree stride;
7233 : 12588 : tree offset;
7234 : 12588 : tree ubound;
7235 : 12588 : tree lbound;
7236 : 12588 : tree tmp;
7237 : 12588 : gfc_se se;
7238 : :
7239 : 12588 : int dim;
7240 : :
7241 : 12588 : as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
7242 : :
7243 : 12588 : size = gfc_index_one_node;
7244 : 12588 : offset = gfc_index_zero_node;
7245 : 12588 : stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
7246 : 12588 : if (stride && VAR_P (stride))
7247 : 124 : gfc_add_modify (pblock, stride, gfc_index_one_node);
7248 : 28412 : for (dim = 0; dim < as->rank; dim++)
7249 : : {
7250 : : /* Evaluate non-constant array bound expressions.
7251 : : F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
7252 : : references a function, the result is finalized before execution of the
7253 : : executable constructs in the scoping unit.
7254 : : Adding the finalblocks enables this. */
7255 : 15824 : lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
7256 : 15824 : if (as->lower[dim] && !INTEGER_CST_P (lbound))
7257 : : {
7258 : 469 : gfc_init_se (&se, NULL);
7259 : 469 : gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
7260 : 469 : gfc_add_block_to_block (pblock, &se.pre);
7261 : 469 : gfc_add_block_to_block (pblock, &se.finalblock);
7262 : 469 : gfc_add_modify (pblock, lbound, se.expr);
7263 : : }
7264 : 15824 : ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
7265 : 15824 : if (as->upper[dim] && !INTEGER_CST_P (ubound))
7266 : : {
7267 : 9470 : gfc_init_se (&se, NULL);
7268 : 9470 : gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
7269 : 9470 : gfc_add_block_to_block (pblock, &se.pre);
7270 : 9470 : gfc_add_block_to_block (pblock, &se.finalblock);
7271 : 9470 : gfc_add_modify (pblock, ubound, se.expr);
7272 : : }
7273 : : /* The offset of this dimension. offset = offset - lbound * stride. */
7274 : 15824 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7275 : : lbound, size);
7276 : 15824 : offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7277 : : offset, tmp);
7278 : :
7279 : : /* The size of this dimension, and the stride of the next. */
7280 : 15824 : if (dim + 1 < as->rank)
7281 : 3383 : stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
7282 : : else
7283 : 12441 : stride = GFC_TYPE_ARRAY_SIZE (type);
7284 : :
7285 : 15824 : if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
7286 : : {
7287 : : /* Calculate stride = size * (ubound + 1 - lbound). */
7288 : 9654 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7289 : : gfc_array_index_type,
7290 : : gfc_index_one_node, lbound);
7291 : 9654 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7292 : : gfc_array_index_type, ubound, tmp);
7293 : 9654 : tmp = fold_build2_loc (input_location, MULT_EXPR,
7294 : : gfc_array_index_type, size, tmp);
7295 : 9654 : if (stride)
7296 : 9654 : gfc_add_modify (pblock, stride, tmp);
7297 : : else
7298 : 0 : stride = gfc_evaluate_now (tmp, pblock);
7299 : :
7300 : : /* Make sure that negative size arrays are translated
7301 : : to being zero size. */
7302 : 9654 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
7303 : : stride, gfc_index_zero_node);
7304 : 9654 : tmp = fold_build3_loc (input_location, COND_EXPR,
7305 : : gfc_array_index_type, tmp,
7306 : : stride, gfc_index_zero_node);
7307 : 9654 : gfc_add_modify (pblock, stride, tmp);
7308 : : }
7309 : :
7310 : : size = stride;
7311 : : }
7312 : :
7313 : 12588 : gfc_trans_array_cobounds (type, pblock, sym);
7314 : 12588 : gfc_trans_vla_type_sizes (sym, pblock);
7315 : :
7316 : 12588 : *poffset = offset;
7317 : 12588 : return size;
7318 : : }
7319 : :
7320 : :
7321 : : /* Generate code to initialize/allocate an array variable. */
7322 : :
7323 : : void
7324 : 30357 : gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym,
7325 : : gfc_wrapped_block * block)
7326 : : {
7327 : 30357 : stmtblock_t init;
7328 : 30357 : tree type;
7329 : 30357 : tree tmp = NULL_TREE;
7330 : 30357 : tree size;
7331 : 30357 : tree offset;
7332 : 30357 : tree space;
7333 : 30357 : tree inittree;
7334 : 30357 : bool onstack;
7335 : 30357 : bool back;
7336 : :
7337 : 30357 : gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
7338 : :
7339 : : /* Do nothing for USEd variables. */
7340 : 30357 : if (sym->attr.use_assoc)
7341 : 25350 : return;
7342 : :
7343 : 30317 : type = TREE_TYPE (decl);
7344 : 30317 : gcc_assert (GFC_ARRAY_TYPE_P (type));
7345 : 30317 : onstack = TREE_CODE (type) != POINTER_TYPE;
7346 : :
7347 : : /* In the case of non-dummy symbols with dependencies on an old-fashioned
7348 : : function result (ie. proc_name = proc_name->result), gfc_add_init_cleanup
7349 : : must be called with the last, optional argument false so that the alloc-
7350 : : ation occurs after the processing of the result. */
7351 : 30317 : back = sym->fn_result_dep;
7352 : :
7353 : 30317 : gfc_init_block (&init);
7354 : :
7355 : : /* Evaluate character string length. */
7356 : 30317 : if (sym->ts.type == BT_CHARACTER
7357 : 2965 : && onstack && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
7358 : : {
7359 : 43 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7360 : :
7361 : 43 : gfc_trans_vla_type_sizes (sym, &init);
7362 : :
7363 : : /* Emit a DECL_EXPR for this variable, which will cause the
7364 : : gimplifier to allocate storage, and all that good stuff. */
7365 : 43 : tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
7366 : 43 : gfc_add_expr_to_block (&init, tmp);
7367 : 43 : if (sym->attr.omp_allocate)
7368 : : {
7369 : : /* Save location of size calculation to ensure GOMP_alloc is placed
7370 : : after it. */
7371 : 0 : tree omp_alloc = lookup_attribute ("omp allocate",
7372 : 0 : DECL_ATTRIBUTES (decl));
7373 : 0 : TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
7374 : 0 : = build_tree_list (NULL_TREE, tsi_stmt (tsi_last (init.head)));
7375 : : }
7376 : : }
7377 : :
7378 : 30115 : if (onstack)
7379 : : {
7380 : 25170 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
7381 : : back);
7382 : 25170 : return;
7383 : : }
7384 : :
7385 : 5147 : type = TREE_TYPE (type);
7386 : :
7387 : 5147 : gcc_assert (!sym->attr.use_assoc);
7388 : 5147 : gcc_assert (!sym->module);
7389 : :
7390 : 5147 : if (sym->ts.type == BT_CHARACTER
7391 : 202 : && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
7392 : 94 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7393 : :
7394 : 5147 : size = gfc_trans_array_bounds (type, sym, &offset, &init);
7395 : :
7396 : : /* Don't actually allocate space for Cray Pointees. */
7397 : 5147 : if (sym->attr.cray_pointee)
7398 : : {
7399 : 140 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7400 : 49 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7401 : :
7402 : 140 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
7403 : 140 : return;
7404 : : }
7405 : 5007 : if (sym->attr.omp_allocate)
7406 : : {
7407 : : /* The size is the number of elements in the array, so multiply by the
7408 : : size of an element to get the total size. */
7409 : 7 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
7410 : 7 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7411 : : size, fold_convert (gfc_array_index_type, tmp));
7412 : 7 : size = gfc_evaluate_now (size, &init);
7413 : :
7414 : 7 : tree omp_alloc = lookup_attribute ("omp allocate",
7415 : 7 : DECL_ATTRIBUTES (decl));
7416 : 7 : TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
7417 : 7 : = build_tree_list (size, NULL_TREE);
7418 : 7 : space = NULL_TREE;
7419 : : }
7420 : 5000 : else if (flag_stack_arrays)
7421 : : {
7422 : 11 : gcc_assert (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE);
7423 : 11 : space = build_decl (gfc_get_location (&sym->declared_at),
7424 : : VAR_DECL, create_tmp_var_name ("A"),
7425 : 11 : TREE_TYPE (TREE_TYPE (decl)));
7426 : 11 : gfc_trans_vla_type_sizes (sym, &init);
7427 : : }
7428 : : else
7429 : : {
7430 : : /* The size is the number of elements in the array, so multiply by the
7431 : : size of an element to get the total size. */
7432 : 4989 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
7433 : 4989 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7434 : : size, fold_convert (gfc_array_index_type, tmp));
7435 : :
7436 : : /* Allocate memory to hold the data. */
7437 : 4989 : tmp = gfc_call_malloc (&init, TREE_TYPE (decl), size);
7438 : 4989 : gfc_add_modify (&init, decl, tmp);
7439 : :
7440 : : /* Free the temporary. */
7441 : 4989 : tmp = gfc_call_free (decl);
7442 : 4989 : space = NULL_TREE;
7443 : : }
7444 : :
7445 : : /* Set offset of the array. */
7446 : 5007 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7447 : 367 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7448 : :
7449 : : /* Automatic arrays should not have initializers. */
7450 : 5007 : gcc_assert (!sym->value);
7451 : :
7452 : 5007 : inittree = gfc_finish_block (&init);
7453 : :
7454 : 5007 : if (space)
7455 : : {
7456 : 11 : tree addr;
7457 : 11 : pushdecl (space);
7458 : :
7459 : : /* Don't create new scope, emit the DECL_EXPR in exactly the scope
7460 : : where also space is located. */
7461 : 11 : gfc_init_block (&init);
7462 : 11 : tmp = fold_build1_loc (input_location, DECL_EXPR,
7463 : 11 : TREE_TYPE (space), space);
7464 : 11 : gfc_add_expr_to_block (&init, tmp);
7465 : 11 : addr = fold_build1_loc (gfc_get_location (&sym->declared_at),
7466 : 11 : ADDR_EXPR, TREE_TYPE (decl), space);
7467 : 11 : gfc_add_modify (&init, decl, addr);
7468 : 11 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
7469 : : back);
7470 : 11 : tmp = NULL_TREE;
7471 : : }
7472 : 5007 : gfc_add_init_cleanup (block, inittree, tmp, back);
7473 : : }
7474 : :
7475 : :
7476 : : /* Generate entry and exit code for g77 calling convention arrays. */
7477 : :
7478 : : void
7479 : 7187 : gfc_trans_g77_array (gfc_symbol * sym, gfc_wrapped_block * block)
7480 : : {
7481 : 7187 : tree parm;
7482 : 7187 : tree type;
7483 : 7187 : tree offset;
7484 : 7187 : tree tmp;
7485 : 7187 : tree stmt;
7486 : 7187 : stmtblock_t init;
7487 : :
7488 : 7187 : location_t loc = input_location;
7489 : 7187 : input_location = gfc_get_location (&sym->declared_at);
7490 : :
7491 : : /* Descriptor type. */
7492 : 7187 : parm = sym->backend_decl;
7493 : 7187 : type = TREE_TYPE (parm);
7494 : 7187 : gcc_assert (GFC_ARRAY_TYPE_P (type));
7495 : :
7496 : 7187 : gfc_start_block (&init);
7497 : :
7498 : 7187 : if (sym->ts.type == BT_CHARACTER
7499 : 661 : && VAR_P (sym->ts.u.cl->backend_decl))
7500 : 79 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7501 : :
7502 : : /* Evaluate the bounds of the array. */
7503 : 7187 : gfc_trans_array_bounds (type, sym, &offset, &init);
7504 : :
7505 : : /* Set the offset. */
7506 : 7187 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7507 : 1206 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7508 : :
7509 : : /* Set the pointer itself if we aren't using the parameter directly. */
7510 : 7187 : if (TREE_CODE (parm) != PARM_DECL)
7511 : : {
7512 : 559 : tmp = GFC_DECL_SAVED_DESCRIPTOR (parm);
7513 : 559 : if (sym->ts.type == BT_CLASS)
7514 : : {
7515 : 239 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
7516 : 239 : tmp = gfc_class_data_get (tmp);
7517 : 239 : tmp = gfc_conv_descriptor_data_get (tmp);
7518 : : }
7519 : 559 : tmp = convert (TREE_TYPE (parm), tmp);
7520 : 559 : gfc_add_modify (&init, parm, tmp);
7521 : : }
7522 : 7187 : stmt = gfc_finish_block (&init);
7523 : :
7524 : 7187 : input_location = loc;
7525 : :
7526 : : /* Add the initialization code to the start of the function. */
7527 : :
7528 : 7187 : if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
7529 : 7187 : || sym->attr.optional
7530 : 6705 : || sym->attr.not_always_present)
7531 : : {
7532 : 539 : tree nullify;
7533 : 539 : if (TREE_CODE (parm) != PARM_DECL)
7534 : 105 : nullify = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7535 : : parm, null_pointer_node);
7536 : : else
7537 : 434 : nullify = build_empty_stmt (input_location);
7538 : 539 : tmp = gfc_conv_expr_present (sym, true);
7539 : 539 : stmt = build3_v (COND_EXPR, tmp, stmt, nullify);
7540 : : }
7541 : :
7542 : 7187 : gfc_add_init_cleanup (block, stmt, NULL_TREE);
7543 : 7187 : }
7544 : :
7545 : :
7546 : : /* Modify the descriptor of an array parameter so that it has the
7547 : : correct lower bound. Also move the upper bound accordingly.
7548 : : If the array is not packed, it will be copied into a temporary.
7549 : : For each dimension we set the new lower and upper bounds. Then we copy the
7550 : : stride and calculate the offset for this dimension. We also work out
7551 : : what the stride of a packed array would be, and see it the two match.
7552 : : If the array need repacking, we set the stride to the values we just
7553 : : calculated, recalculate the offset and copy the array data.
7554 : : Code is also added to copy the data back at the end of the function.
7555 : : */
7556 : :
7557 : : void
7558 : 12459 : gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc,
7559 : : gfc_wrapped_block * block)
7560 : : {
7561 : 12459 : tree size;
7562 : 12459 : tree type;
7563 : 12459 : tree offset;
7564 : 12459 : stmtblock_t init;
7565 : 12459 : tree stmtInit, stmtCleanup;
7566 : 12459 : tree lbound;
7567 : 12459 : tree ubound;
7568 : 12459 : tree dubound;
7569 : 12459 : tree dlbound;
7570 : 12459 : tree dumdesc;
7571 : 12459 : tree tmp;
7572 : 12459 : tree stride, stride2;
7573 : 12459 : tree stmt_packed;
7574 : 12459 : tree stmt_unpacked;
7575 : 12459 : tree partial;
7576 : 12459 : gfc_se se;
7577 : 12459 : int n;
7578 : 12459 : int checkparm;
7579 : 12459 : int no_repack;
7580 : 12459 : bool optional_arg;
7581 : 12459 : gfc_array_spec *as;
7582 : 12459 : bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
7583 : :
7584 : : /* Do nothing for pointer and allocatable arrays. */
7585 : 12459 : if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
7586 : 12362 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
7587 : 12362 : || sym->attr.allocatable
7588 : 12256 : || (is_classarray && CLASS_DATA (sym)->attr.allocatable))
7589 : 5872 : return;
7590 : :
7591 : 742 : if ((!is_classarray
7592 : 742 : || (is_classarray && CLASS_DATA (sym)->as->type == AS_EXPLICIT))
7593 : 11694 : && sym->attr.dummy && !sym->attr.elemental && gfc_is_nodesc_array (sym))
7594 : : {
7595 : 5669 : gfc_trans_g77_array (sym, block);
7596 : 5669 : return;
7597 : : }
7598 : :
7599 : 6587 : location_t loc = input_location;
7600 : 6587 : input_location = gfc_get_location (&sym->declared_at);
7601 : :
7602 : : /* Descriptor type. */
7603 : 6587 : type = TREE_TYPE (tmpdesc);
7604 : 6587 : gcc_assert (GFC_ARRAY_TYPE_P (type));
7605 : 6587 : dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
7606 : 6587 : if (is_classarray)
7607 : : /* For a class array the dummy array descriptor is in the _class
7608 : : component. */
7609 : 581 : dumdesc = gfc_class_data_get (dumdesc);
7610 : : else
7611 : 6006 : dumdesc = build_fold_indirect_ref_loc (input_location, dumdesc);
7612 : 6587 : as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
7613 : 6587 : gfc_start_block (&init);
7614 : :
7615 : 6587 : if (sym->ts.type == BT_CHARACTER
7616 : 765 : && VAR_P (sym->ts.u.cl->backend_decl))
7617 : 87 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7618 : :
7619 : : /* TODO: Fix the exclusion of class arrays from extent checking. */
7620 : 1047 : checkparm = (as->type == AS_EXPLICIT && !is_classarray
7621 : 7615 : && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS));
7622 : :
7623 : 6587 : no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
7624 : 6586 : || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
7625 : :
7626 : 6587 : if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
7627 : : {
7628 : : /* For non-constant shape arrays we only check if the first dimension
7629 : : is contiguous. Repacking higher dimensions wouldn't gain us
7630 : : anything as we still don't know the array stride. */
7631 : 1 : partial = gfc_create_var (logical_type_node, "partial");
7632 : 1 : TREE_USED (partial) = 1;
7633 : 1 : tmp = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
7634 : 1 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
7635 : : gfc_index_one_node);
7636 : 1 : gfc_add_modify (&init, partial, tmp);
7637 : : }
7638 : : else
7639 : : partial = NULL_TREE;
7640 : :
7641 : : /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
7642 : : here, however I think it does the right thing. */
7643 : 6587 : if (no_repack)
7644 : : {
7645 : : /* Set the first stride. */
7646 : 6585 : stride = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
7647 : 6585 : stride = gfc_evaluate_now (stride, &init);
7648 : :
7649 : 6585 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7650 : : stride, gfc_index_zero_node);
7651 : 6585 : tmp = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
7652 : : tmp, gfc_index_one_node, stride);
7653 : 6585 : stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
7654 : 6585 : gfc_add_modify (&init, stride, tmp);
7655 : :
7656 : : /* Allow the user to disable array repacking. */
7657 : 6585 : stmt_unpacked = NULL_TREE;
7658 : : }
7659 : : else
7660 : : {
7661 : 2 : gcc_assert (integer_onep (GFC_TYPE_ARRAY_STRIDE (type, 0)));
7662 : : /* A library call to repack the array if necessary. */
7663 : 2 : tmp = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
7664 : 2 : stmt_unpacked = build_call_expr_loc (input_location,
7665 : : gfor_fndecl_in_pack, 1, tmp);
7666 : :
7667 : 2 : stride = gfc_index_one_node;
7668 : :
7669 : 2 : if (warn_array_temporaries)
7670 : : {
7671 : 1 : locus where;
7672 : 1 : gfc_locus_from_location (&where, loc);
7673 : 1 : gfc_warning (OPT_Warray_temporaries,
7674 : : "Creating array temporary at %L", &where);
7675 : : }
7676 : : }
7677 : :
7678 : : /* This is for the case where the array data is used directly without
7679 : : calling the repack function. */
7680 : 6587 : if (no_repack || partial != NULL_TREE)
7681 : 6586 : stmt_packed = gfc_conv_descriptor_data_get (dumdesc);
7682 : : else
7683 : : stmt_packed = NULL_TREE;
7684 : :
7685 : : /* Assign the data pointer. */
7686 : 6587 : if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
7687 : : {
7688 : : /* Don't repack unknown shape arrays when the first stride is 1. */
7689 : 1 : tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (stmt_packed),
7690 : : partial, stmt_packed, stmt_unpacked);
7691 : : }
7692 : : else
7693 : 6586 : tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
7694 : 6587 : gfc_add_modify (&init, tmpdesc, fold_convert (type, tmp));
7695 : :
7696 : 6587 : offset = gfc_index_zero_node;
7697 : 6587 : size = gfc_index_one_node;
7698 : :
7699 : : /* Evaluate the bounds of the array. */
7700 : 15445 : for (n = 0; n < as->rank; n++)
7701 : : {
7702 : 8858 : if (checkparm || !as->upper[n])
7703 : : {
7704 : : /* Get the bounds of the actual parameter. */
7705 : 7576 : dubound = gfc_conv_descriptor_ubound_get (dumdesc, gfc_rank_cst[n]);
7706 : 7576 : dlbound = gfc_conv_descriptor_lbound_get (dumdesc, gfc_rank_cst[n]);
7707 : : }
7708 : : else
7709 : : {
7710 : : dubound = NULL_TREE;
7711 : : dlbound = NULL_TREE;
7712 : : }
7713 : :
7714 : 8858 : lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
7715 : 8858 : if (!INTEGER_CST_P (lbound))
7716 : : {
7717 : 45 : gfc_init_se (&se, NULL);
7718 : 45 : gfc_conv_expr_type (&se, as->lower[n],
7719 : : gfc_array_index_type);
7720 : 45 : gfc_add_block_to_block (&init, &se.pre);
7721 : 45 : gfc_add_modify (&init, lbound, se.expr);
7722 : : }
7723 : :
7724 : 8858 : ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
7725 : : /* Set the desired upper bound. */
7726 : 8858 : if (as->upper[n])
7727 : : {
7728 : : /* We know what we want the upper bound to be. */
7729 : 1340 : if (!INTEGER_CST_P (ubound))
7730 : : {
7731 : 620 : gfc_init_se (&se, NULL);
7732 : 620 : gfc_conv_expr_type (&se, as->upper[n],
7733 : : gfc_array_index_type);
7734 : 620 : gfc_add_block_to_block (&init, &se.pre);
7735 : 620 : gfc_add_modify (&init, ubound, se.expr);
7736 : : }
7737 : :
7738 : : /* Check the sizes match. */
7739 : 1340 : if (checkparm)
7740 : : {
7741 : : /* Check (ubound(a) - lbound(a) == ubound(b) - lbound(b)). */
7742 : 58 : char * msg;
7743 : 58 : tree temp;
7744 : 58 : locus where;
7745 : :
7746 : 58 : gfc_locus_from_location (&where, loc);
7747 : 58 : temp = fold_build2_loc (input_location, MINUS_EXPR,
7748 : : gfc_array_index_type, ubound, lbound);
7749 : 58 : temp = fold_build2_loc (input_location, PLUS_EXPR,
7750 : : gfc_array_index_type,
7751 : : gfc_index_one_node, temp);
7752 : 58 : stride2 = fold_build2_loc (input_location, MINUS_EXPR,
7753 : : gfc_array_index_type, dubound,
7754 : : dlbound);
7755 : 58 : stride2 = fold_build2_loc (input_location, PLUS_EXPR,
7756 : : gfc_array_index_type,
7757 : : gfc_index_one_node, stride2);
7758 : 58 : tmp = fold_build2_loc (input_location, NE_EXPR,
7759 : : gfc_array_index_type, temp, stride2);
7760 : 58 : msg = xasprintf ("Dimension %d of array '%s' has extent "
7761 : : "%%ld instead of %%ld", n+1, sym->name);
7762 : :
7763 : 58 : gfc_trans_runtime_check (true, false, tmp, &init, &where, msg,
7764 : : fold_convert (long_integer_type_node, temp),
7765 : : fold_convert (long_integer_type_node, stride2));
7766 : :
7767 : 58 : free (msg);
7768 : : }
7769 : : }
7770 : : else
7771 : : {
7772 : : /* For assumed shape arrays move the upper bound by the same amount
7773 : : as the lower bound. */
7774 : 7518 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7775 : : gfc_array_index_type, dubound, dlbound);
7776 : 7518 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7777 : : gfc_array_index_type, tmp, lbound);
7778 : 7518 : gfc_add_modify (&init, ubound, tmp);
7779 : : }
7780 : : /* The offset of this dimension. offset = offset - lbound * stride. */
7781 : 8858 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7782 : : lbound, stride);
7783 : 8858 : offset = fold_build2_loc (input_location, MINUS_EXPR,
7784 : : gfc_array_index_type, offset, tmp);
7785 : :
7786 : : /* The size of this dimension, and the stride of the next. */
7787 : 8858 : if (n + 1 < as->rank)
7788 : : {
7789 : 2271 : stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
7790 : :
7791 : 2271 : if (no_repack || partial != NULL_TREE)
7792 : 2270 : stmt_unpacked =
7793 : 2270 : gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[n+1]);
7794 : :
7795 : : /* Figure out the stride if not a known constant. */
7796 : 2271 : if (!INTEGER_CST_P (stride))
7797 : : {
7798 : 2270 : if (no_repack)
7799 : : stmt_packed = NULL_TREE;
7800 : : else
7801 : : {
7802 : : /* Calculate stride = size * (ubound + 1 - lbound). */
7803 : 0 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7804 : : gfc_array_index_type,
7805 : : gfc_index_one_node, lbound);
7806 : 0 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7807 : : gfc_array_index_type, ubound, tmp);
7808 : 0 : size = fold_build2_loc (input_location, MULT_EXPR,
7809 : : gfc_array_index_type, size, tmp);
7810 : 0 : stmt_packed = size;
7811 : : }
7812 : :
7813 : : /* Assign the stride. */
7814 : 2270 : if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
7815 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR,
7816 : : gfc_array_index_type, partial,
7817 : : stmt_unpacked, stmt_packed);
7818 : : else
7819 : 2270 : tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
7820 : 2270 : gfc_add_modify (&init, stride, tmp);
7821 : : }
7822 : : }
7823 : : else
7824 : : {
7825 : 6587 : stride = GFC_TYPE_ARRAY_SIZE (type);
7826 : :
7827 : 6587 : if (stride && !INTEGER_CST_P (stride))
7828 : : {
7829 : : /* Calculate size = stride * (ubound + 1 - lbound). */
7830 : 6586 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7831 : : gfc_array_index_type,
7832 : : gfc_index_one_node, lbound);
7833 : 6586 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7834 : : gfc_array_index_type,
7835 : : ubound, tmp);
7836 : 19758 : tmp = fold_build2_loc (input_location, MULT_EXPR,
7837 : : gfc_array_index_type,
7838 : 6586 : GFC_TYPE_ARRAY_STRIDE (type, n), tmp);
7839 : 6586 : gfc_add_modify (&init, stride, tmp);
7840 : : }
7841 : : }
7842 : : }
7843 : :
7844 : 6587 : gfc_trans_array_cobounds (type, &init, sym);
7845 : :
7846 : : /* Set the offset. */
7847 : 6587 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7848 : 6585 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7849 : :
7850 : 6587 : gfc_trans_vla_type_sizes (sym, &init);
7851 : :
7852 : 6587 : stmtInit = gfc_finish_block (&init);
7853 : :
7854 : : /* Only do the entry/initialization code if the arg is present. */
7855 : 6587 : dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
7856 : 6587 : optional_arg = (sym->attr.optional
7857 : 6587 : || (sym->ns->proc_name->attr.entry_master
7858 : 79 : && sym->attr.dummy));
7859 : : if (optional_arg)
7860 : : {
7861 : 716 : tree zero_init = fold_convert (TREE_TYPE (tmpdesc), null_pointer_node);
7862 : 716 : zero_init = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7863 : : tmpdesc, zero_init);
7864 : 716 : tmp = gfc_conv_expr_present (sym, true);
7865 : 716 : stmtInit = build3_v (COND_EXPR, tmp, stmtInit, zero_init);
7866 : : }
7867 : :
7868 : : /* Cleanup code. */
7869 : 6587 : if (no_repack)
7870 : : stmtCleanup = NULL_TREE;
7871 : : else
7872 : : {
7873 : 2 : stmtblock_t cleanup;
7874 : 2 : gfc_start_block (&cleanup);
7875 : :
7876 : 2 : if (sym->attr.intent != INTENT_IN)
7877 : : {
7878 : : /* Copy the data back. */
7879 : 2 : tmp = build_call_expr_loc (input_location,
7880 : : gfor_fndecl_in_unpack, 2, dumdesc, tmpdesc);
7881 : 2 : gfc_add_expr_to_block (&cleanup, tmp);
7882 : : }
7883 : :
7884 : : /* Free the temporary. */
7885 : 2 : tmp = gfc_call_free (tmpdesc);
7886 : 2 : gfc_add_expr_to_block (&cleanup, tmp);
7887 : :
7888 : 2 : stmtCleanup = gfc_finish_block (&cleanup);
7889 : :
7890 : : /* Only do the cleanup if the array was repacked. */
7891 : 2 : if (is_classarray)
7892 : : /* For a class array the dummy array descriptor is in the _class
7893 : : component. */
7894 : 1 : tmp = gfc_class_data_get (dumdesc);
7895 : : else
7896 : 1 : tmp = build_fold_indirect_ref_loc (input_location, dumdesc);
7897 : 2 : tmp = gfc_conv_descriptor_data_get (tmp);
7898 : 2 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
7899 : : tmp, tmpdesc);
7900 : 2 : stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
7901 : : build_empty_stmt (input_location));
7902 : :
7903 : 2 : if (optional_arg)
7904 : : {
7905 : 0 : tmp = gfc_conv_expr_present (sym);
7906 : 0 : stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
7907 : : build_empty_stmt (input_location));
7908 : : }
7909 : : }
7910 : :
7911 : : /* We don't need to free any memory allocated by internal_pack as it will
7912 : : be freed at the end of the function by pop_context. */
7913 : 6587 : gfc_add_init_cleanup (block, stmtInit, stmtCleanup);
7914 : :
7915 : 6587 : input_location = loc;
7916 : : }
7917 : :
7918 : :
7919 : : /* Calculate the overall offset, including subreferences. */
7920 : : void
7921 : 58726 : gfc_get_dataptr_offset (stmtblock_t *block, tree parm, tree desc, tree offset,
7922 : : bool subref, gfc_expr *expr)
7923 : : {
7924 : 58726 : tree tmp;
7925 : 58726 : tree field;
7926 : 58726 : tree stride;
7927 : 58726 : tree index;
7928 : 58726 : gfc_ref *ref;
7929 : 58726 : gfc_se start;
7930 : 58726 : int n;
7931 : :
7932 : : /* If offset is NULL and this is not a subreferenced array, there is
7933 : : nothing to do. */
7934 : 58726 : if (offset == NULL_TREE)
7935 : : {
7936 : 1029 : if (subref)
7937 : 115 : offset = gfc_index_zero_node;
7938 : : else
7939 : 914 : return;
7940 : : }
7941 : :
7942 : 57812 : tmp = build_array_ref (desc, offset, NULL, NULL);
7943 : :
7944 : : /* Offset the data pointer for pointer assignments from arrays with
7945 : : subreferences; e.g. my_integer => my_type(:)%integer_component. */
7946 : 57812 : if (subref)
7947 : : {
7948 : : /* Go past the array reference. */
7949 : 771 : for (ref = expr->ref; ref; ref = ref->next)
7950 : 771 : if (ref->type == REF_ARRAY &&
7951 : 684 : ref->u.ar.type != AR_ELEMENT)
7952 : : {
7953 : 660 : ref = ref->next;
7954 : 660 : break;
7955 : : }
7956 : :
7957 : : /* Calculate the offset for each subsequent subreference. */
7958 : 1292 : for (; ref; ref = ref->next)
7959 : : {
7960 : 632 : switch (ref->type)
7961 : : {
7962 : 264 : case REF_COMPONENT:
7963 : 264 : field = ref->u.c.component->backend_decl;
7964 : 264 : gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
7965 : 528 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
7966 : 264 : TREE_TYPE (field),
7967 : : tmp, field, NULL_TREE);
7968 : 264 : break;
7969 : :
7970 : 320 : case REF_SUBSTRING:
7971 : 320 : gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
7972 : 320 : gfc_init_se (&start, NULL);
7973 : 320 : gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
7974 : 320 : gfc_add_block_to_block (block, &start.pre);
7975 : 320 : tmp = gfc_build_array_ref (tmp, start.expr, NULL);
7976 : 320 : break;
7977 : :
7978 : 24 : case REF_ARRAY:
7979 : 24 : gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE
7980 : : && ref->u.ar.type == AR_ELEMENT);
7981 : :
7982 : : /* TODO - Add bounds checking. */
7983 : 24 : stride = gfc_index_one_node;
7984 : 24 : index = gfc_index_zero_node;
7985 : 55 : for (n = 0; n < ref->u.ar.dimen; n++)
7986 : : {
7987 : 31 : tree itmp;
7988 : 31 : tree jtmp;
7989 : :
7990 : : /* Update the index. */
7991 : 31 : gfc_init_se (&start, NULL);
7992 : 31 : gfc_conv_expr_type (&start, ref->u.ar.start[n], gfc_array_index_type);
7993 : 31 : itmp = gfc_evaluate_now (start.expr, block);
7994 : 31 : gfc_init_se (&start, NULL);
7995 : 31 : gfc_conv_expr_type (&start, ref->u.ar.as->lower[n], gfc_array_index_type);
7996 : 31 : jtmp = gfc_evaluate_now (start.expr, block);
7997 : 31 : itmp = fold_build2_loc (input_location, MINUS_EXPR,
7998 : : gfc_array_index_type, itmp, jtmp);
7999 : 31 : itmp = fold_build2_loc (input_location, MULT_EXPR,
8000 : : gfc_array_index_type, itmp, stride);
8001 : 31 : index = fold_build2_loc (input_location, PLUS_EXPR,
8002 : : gfc_array_index_type, itmp, index);
8003 : 31 : index = gfc_evaluate_now (index, block);
8004 : :
8005 : : /* Update the stride. */
8006 : 31 : gfc_init_se (&start, NULL);
8007 : 31 : gfc_conv_expr_type (&start, ref->u.ar.as->upper[n], gfc_array_index_type);
8008 : 31 : itmp = fold_build2_loc (input_location, MINUS_EXPR,
8009 : : gfc_array_index_type, start.expr,
8010 : : jtmp);
8011 : 31 : itmp = fold_build2_loc (input_location, PLUS_EXPR,
8012 : : gfc_array_index_type,
8013 : : gfc_index_one_node, itmp);
8014 : 31 : stride = fold_build2_loc (input_location, MULT_EXPR,
8015 : : gfc_array_index_type, stride, itmp);
8016 : 31 : stride = gfc_evaluate_now (stride, block);
8017 : : }
8018 : :
8019 : : /* Apply the index to obtain the array element. */
8020 : 24 : tmp = gfc_build_array_ref (tmp, index, NULL);
8021 : 24 : break;
8022 : :
8023 : 24 : case REF_INQUIRY:
8024 : 24 : switch (ref->u.i)
8025 : : {
8026 : 18 : case INQUIRY_RE:
8027 : 36 : tmp = fold_build1_loc (input_location, REALPART_EXPR,
8028 : 18 : TREE_TYPE (TREE_TYPE (tmp)), tmp);
8029 : 18 : break;
8030 : :
8031 : 6 : case INQUIRY_IM:
8032 : 12 : tmp = fold_build1_loc (input_location, IMAGPART_EXPR,
8033 : 6 : TREE_TYPE (TREE_TYPE (tmp)), tmp);
8034 : 6 : break;
8035 : :
8036 : : default:
8037 : : break;
8038 : : }
8039 : : break;
8040 : :
8041 : 0 : default:
8042 : 0 : gcc_unreachable ();
8043 : 632 : break;
8044 : : }
8045 : : }
8046 : : }
8047 : :
8048 : : /* Set the target data pointer. */
8049 : 57812 : offset = gfc_build_addr_expr (gfc_array_dataptr_type (desc), tmp);
8050 : :
8051 : : /* Check for optional dummy argument being present. Arguments of BIND(C)
8052 : : procedures are excepted here since they are handled differently. */
8053 : 57812 : if (expr->expr_type == EXPR_VARIABLE
8054 : 51213 : && expr->symtree->n.sym->attr.dummy
8055 : 6111 : && expr->symtree->n.sym->attr.optional
8056 : 58803 : && !is_CFI_desc (NULL, expr))
8057 : 1622 : offset = build3_loc (input_location, COND_EXPR, TREE_TYPE (offset),
8058 : 811 : gfc_conv_expr_present (expr->symtree->n.sym), offset,
8059 : 811 : fold_convert (TREE_TYPE (offset), gfc_index_zero_node));
8060 : :
8061 : 57812 : gfc_conv_descriptor_data_set (block, parm, offset);
8062 : : }
8063 : :
8064 : :
8065 : : /* gfc_conv_expr_descriptor needs the string length an expression
8066 : : so that the size of the temporary can be obtained. This is done
8067 : : by adding up the string lengths of all the elements in the
8068 : : expression. Function with non-constant expressions have their
8069 : : string lengths mapped onto the actual arguments using the
8070 : : interface mapping machinery in trans-expr.cc. */
8071 : : static void
8072 : 1509 : get_array_charlen (gfc_expr *expr, gfc_se *se)
8073 : : {
8074 : 1509 : gfc_interface_mapping mapping;
8075 : 1509 : gfc_formal_arglist *formal;
8076 : 1509 : gfc_actual_arglist *arg;
8077 : 1509 : gfc_se tse;
8078 : 1509 : gfc_expr *e;
8079 : :
8080 : 1509 : if (expr->ts.u.cl->length
8081 : 1509 : && gfc_is_constant_expr (expr->ts.u.cl->length))
8082 : : {
8083 : 1165 : if (!expr->ts.u.cl->backend_decl)
8084 : 447 : gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
8085 : 1297 : return;
8086 : : }
8087 : :
8088 : 344 : switch (expr->expr_type)
8089 : : {
8090 : 130 : case EXPR_ARRAY:
8091 : :
8092 : : /* This is somewhat brutal. The expression for the first
8093 : : element of the array is evaluated and assigned to a
8094 : : new string length for the original expression. */
8095 : 130 : e = gfc_constructor_first (expr->value.constructor)->expr;
8096 : :
8097 : 130 : gfc_init_se (&tse, NULL);
8098 : :
8099 : : /* Avoid evaluating trailing array references since all we need is
8100 : : the string length. */
8101 : 130 : if (e->rank)
8102 : 38 : tse.descriptor_only = 1;
8103 : 130 : if (e->rank && e->expr_type != EXPR_VARIABLE)
8104 : 1 : gfc_conv_expr_descriptor (&tse, e);
8105 : : else
8106 : 129 : gfc_conv_expr (&tse, e);
8107 : :
8108 : 130 : gfc_add_block_to_block (&se->pre, &tse.pre);
8109 : 130 : gfc_add_block_to_block (&se->post, &tse.post);
8110 : :
8111 : 130 : if (!expr->ts.u.cl->backend_decl || !VAR_P (expr->ts.u.cl->backend_decl))
8112 : : {
8113 : 87 : expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
8114 : 87 : expr->ts.u.cl->backend_decl =
8115 : 87 : gfc_create_var (gfc_charlen_type_node, "sln");
8116 : : }
8117 : :
8118 : 130 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
8119 : : tse.string_length);
8120 : :
8121 : : /* Make sure that deferred length components point to the hidden
8122 : : string_length component. */
8123 : 130 : if (TREE_CODE (tse.expr) == COMPONENT_REF
8124 : 25 : && TREE_CODE (tse.string_length) == COMPONENT_REF
8125 : 149 : && TREE_OPERAND (tse.expr, 0) == TREE_OPERAND (tse.string_length, 0))
8126 : 19 : e->ts.u.cl->backend_decl = expr->ts.u.cl->backend_decl;
8127 : :
8128 : : return;
8129 : :
8130 : 90 : case EXPR_OP:
8131 : 90 : get_array_charlen (expr->value.op.op1, se);
8132 : :
8133 : : /* For parentheses the expression ts.u.cl should be identical. */
8134 : 90 : if (expr->value.op.op == INTRINSIC_PARENTHESES)
8135 : : {
8136 : 2 : if (expr->value.op.op1->ts.u.cl != expr->ts.u.cl)
8137 : 2 : expr->ts.u.cl->backend_decl
8138 : 2 : = expr->value.op.op1->ts.u.cl->backend_decl;
8139 : 2 : return;
8140 : : }
8141 : :
8142 : 176 : expr->ts.u.cl->backend_decl =
8143 : 88 : gfc_create_var (gfc_charlen_type_node, "sln");
8144 : :
8145 : 88 : if (expr->value.op.op2)
8146 : : {
8147 : 88 : get_array_charlen (expr->value.op.op2, se);
8148 : :
8149 : 88 : gcc_assert (expr->value.op.op == INTRINSIC_CONCAT);
8150 : :
8151 : : /* Add the string lengths and assign them to the expression
8152 : : string length backend declaration. */
8153 : 88 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
8154 : : fold_build2_loc (input_location, PLUS_EXPR,
8155 : : gfc_charlen_type_node,
8156 : 88 : expr->value.op.op1->ts.u.cl->backend_decl,
8157 : 88 : expr->value.op.op2->ts.u.cl->backend_decl));
8158 : : }
8159 : : else
8160 : 0 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
8161 : 0 : expr->value.op.op1->ts.u.cl->backend_decl);
8162 : : break;
8163 : :
8164 : 43 : case EXPR_FUNCTION:
8165 : 43 : if (expr->value.function.esym == NULL
8166 : 37 : || expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8167 : : {
8168 : 6 : gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
8169 : 6 : break;
8170 : : }
8171 : :
8172 : : /* Map expressions involving the dummy arguments onto the actual
8173 : : argument expressions. */
8174 : 37 : gfc_init_interface_mapping (&mapping);
8175 : 37 : formal = gfc_sym_get_dummy_args (expr->symtree->n.sym);
8176 : 37 : arg = expr->value.function.actual;
8177 : :
8178 : : /* Set se = NULL in the calls to the interface mapping, to suppress any
8179 : : backend stuff. */
8180 : 113 : for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
8181 : : {
8182 : 38 : if (!arg->expr)
8183 : 0 : continue;
8184 : 38 : if (formal->sym)
8185 : 38 : gfc_add_interface_mapping (&mapping, formal->sym, NULL, arg->expr);
8186 : : }
8187 : :
8188 : 37 : gfc_init_se (&tse, NULL);
8189 : :
8190 : : /* Build the expression for the character length and convert it. */
8191 : 37 : gfc_apply_interface_mapping (&mapping, &tse, expr->ts.u.cl->length);
8192 : :
8193 : 37 : gfc_add_block_to_block (&se->pre, &tse.pre);
8194 : 37 : gfc_add_block_to_block (&se->post, &tse.post);
8195 : 37 : tse.expr = fold_convert (gfc_charlen_type_node, tse.expr);
8196 : 74 : tse.expr = fold_build2_loc (input_location, MAX_EXPR,
8197 : 37 : TREE_TYPE (tse.expr), tse.expr,
8198 : 37 : build_zero_cst (TREE_TYPE (tse.expr)));
8199 : 37 : expr->ts.u.cl->backend_decl = tse.expr;
8200 : 37 : gfc_free_interface_mapping (&mapping);
8201 : 37 : break;
8202 : :
8203 : 81 : default:
8204 : 81 : gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
8205 : 81 : break;
8206 : : }
8207 : : }
8208 : :
8209 : :
8210 : : /* Helper function to check dimensions. */
8211 : : static bool
8212 : 144 : transposed_dims (gfc_ss *ss)
8213 : : {
8214 : 144 : int n;
8215 : :
8216 : 168526 : for (n = 0; n < ss->dimen; n++)
8217 : 86926 : if (ss->dim[n] != n)
8218 : : return true;
8219 : : return false;
8220 : : }
8221 : :
8222 : :
8223 : : /* Convert the last ref of a scalar coarray from an AR_ELEMENT to an
8224 : : AR_FULL, suitable for the scalarizer. */
8225 : :
8226 : : static gfc_ss *
8227 : 1170 : walk_coarray (gfc_expr *e)
8228 : : {
8229 : 1170 : gfc_ss *ss;
8230 : :
8231 : 1170 : ss = gfc_walk_expr (e);
8232 : :
8233 : : /* Fix scalar coarray. */
8234 : 1170 : if (ss == gfc_ss_terminator)
8235 : : {
8236 : 251 : gfc_ref *ref;
8237 : :
8238 : 251 : ref = e->ref;
8239 : 368 : while (ref)
8240 : : {
8241 : 368 : if (ref->type == REF_ARRAY
8242 : 251 : && ref->u.ar.codimen > 0)
8243 : : break;
8244 : :
8245 : 117 : ref = ref->next;
8246 : : }
8247 : :
8248 : 251 : gcc_assert (ref != NULL);
8249 : 251 : if (ref->u.ar.type == AR_ELEMENT)
8250 : 237 : ref->u.ar.type = AR_SECTION;
8251 : 251 : ss = gfc_reverse_ss (gfc_walk_array_ref (ss, e, ref, false));
8252 : : }
8253 : :
8254 : 1170 : return ss;
8255 : : }
8256 : :
8257 : : gfc_array_spec *
8258 : 1774 : get_coarray_as (const gfc_expr *e)
8259 : : {
8260 : 1774 : gfc_array_spec *as;
8261 : 1774 : gfc_symbol *sym = e->symtree->n.sym;
8262 : 1774 : gfc_component *comp;
8263 : :
8264 : 1774 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.codimension)
8265 : 544 : as = CLASS_DATA (sym)->as;
8266 : 1230 : else if (sym->attr.codimension)
8267 : 1184 : as = sym->as;
8268 : : else
8269 : : as = nullptr;
8270 : :
8271 : 4192 : for (gfc_ref *ref = e->ref; ref; ref = ref->next)
8272 : : {
8273 : 2418 : switch (ref->type)
8274 : : {
8275 : 644 : case REF_COMPONENT:
8276 : 644 : comp = ref->u.c.component;
8277 : 644 : if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.codimension)
8278 : 18 : as = CLASS_DATA (comp)->as;
8279 : 626 : else if (comp->ts.type != BT_CLASS && comp->attr.codimension)
8280 : 590 : as = comp->as;
8281 : : break;
8282 : :
8283 : : case REF_ARRAY:
8284 : : case REF_SUBSTRING:
8285 : : case REF_INQUIRY:
8286 : : break;
8287 : : }
8288 : : }
8289 : :
8290 : 1774 : return as;
8291 : : }
8292 : :
8293 : : bool
8294 : 137180 : is_explicit_coarray (gfc_expr *expr)
8295 : : {
8296 : 137180 : if (!gfc_is_coarray (expr))
8297 : : return false;
8298 : :
8299 : 1774 : gfc_array_spec *cas = get_coarray_as (expr);
8300 : 1774 : return cas && cas->cotype == AS_EXPLICIT;
8301 : : }
8302 : :
8303 : : /* Convert an array for passing as an actual argument. Expressions and
8304 : : vector subscripts are evaluated and stored in a temporary, which is then
8305 : : passed. For whole arrays the descriptor is passed. For array sections
8306 : : a modified copy of the descriptor is passed, but using the original data.
8307 : :
8308 : : This function is also used for array pointer assignments, and there
8309 : : are three cases:
8310 : :
8311 : : - se->want_pointer && !se->direct_byref
8312 : : EXPR is an actual argument. On exit, se->expr contains a
8313 : : pointer to the array descriptor.
8314 : :
8315 : : - !se->want_pointer && !se->direct_byref
8316 : : EXPR is an actual argument to an intrinsic function or the
8317 : : left-hand side of a pointer assignment. On exit, se->expr
8318 : : contains the descriptor for EXPR.
8319 : :
8320 : : - !se->want_pointer && se->direct_byref
8321 : : EXPR is the right-hand side of a pointer assignment and
8322 : : se->expr is the descriptor for the previously-evaluated
8323 : : left-hand side. The function creates an assignment from
8324 : : EXPR to se->expr.
8325 : :
8326 : :
8327 : : The se->force_tmp flag disables the non-copying descriptor optimization
8328 : : that is used for transpose. It may be used in cases where there is an
8329 : : alias between the transpose argument and another argument in the same
8330 : : function call. */
8331 : :
8332 : : void
8333 : 153001 : gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr)
8334 : : {
8335 : 153001 : gfc_ss *ss;
8336 : 153001 : gfc_ss_type ss_type;
8337 : 153001 : gfc_ss_info *ss_info;
8338 : 153001 : gfc_loopinfo loop;
8339 : 153001 : gfc_array_info *info;
8340 : 153001 : int need_tmp;
8341 : 153001 : int n;
8342 : 153001 : tree tmp;
8343 : 153001 : tree desc;
8344 : 153001 : stmtblock_t block;
8345 : 153001 : tree start;
8346 : 153001 : int full;
8347 : 153001 : bool subref_array_target = false;
8348 : 153001 : bool deferred_array_component = false;
8349 : 153001 : bool substr = false;
8350 : 153001 : gfc_expr *arg, *ss_expr;
8351 : :
8352 : 153001 : if (se->want_coarray || expr->rank == 0)
8353 : 1170 : ss = walk_coarray (expr);
8354 : : else
8355 : 151831 : ss = gfc_walk_expr (expr);
8356 : :
8357 : 153001 : gcc_assert (ss != NULL);
8358 : 153001 : gcc_assert (ss != gfc_ss_terminator);
8359 : :
8360 : 153001 : ss_info = ss->info;
8361 : 153001 : ss_type = ss_info->type;
8362 : 153001 : ss_expr = ss_info->expr;
8363 : :
8364 : : /* Special case: TRANSPOSE which needs no temporary. */
8365 : 158215 : while (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym
8366 : 157949 : && (arg = gfc_get_noncopying_intrinsic_argument (expr)) != NULL)
8367 : : {
8368 : : /* This is a call to transpose which has already been handled by the
8369 : : scalarizer, so that we just need to get its argument's descriptor. */
8370 : 408 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
8371 : 408 : expr = expr->value.function.actual->expr;
8372 : : }
8373 : :
8374 : 153001 : if (!se->direct_byref)
8375 : 294294 : se->unlimited_polymorphic = UNLIMITED_POLY (expr);
8376 : :
8377 : : /* Special case things we know we can pass easily. */
8378 : 153001 : switch (expr->expr_type)
8379 : : {
8380 : 137354 : case EXPR_VARIABLE:
8381 : : /* If we have a linear array section, we can pass it directly.
8382 : : Otherwise we need to copy it into a temporary. */
8383 : :
8384 : 137354 : gcc_assert (ss_type == GFC_SS_SECTION);
8385 : 137354 : gcc_assert (ss_expr == expr);
8386 : 137354 : info = &ss_info->data.array;
8387 : :
8388 : : /* Get the descriptor for the array. */
8389 : 137354 : gfc_conv_ss_descriptor (&se->pre, ss, 0);
8390 : 137354 : desc = info->descriptor;
8391 : :
8392 : : /* The charlen backend decl for deferred character components cannot
8393 : : be used because it is fixed at zero. Instead, the hidden string
8394 : : length component is used. */
8395 : 137354 : if (expr->ts.type == BT_CHARACTER
8396 : 19910 : && expr->ts.deferred
8397 : 2770 : && TREE_CODE (desc) == COMPONENT_REF)
8398 : 137354 : deferred_array_component = true;
8399 : :
8400 : 137354 : substr = info->ref && info->ref->next
8401 : 138064 : && info->ref->next->type == REF_SUBSTRING;
8402 : :
8403 : 137354 : subref_array_target = (is_subref_array (expr)
8404 : 137354 : && (se->direct_byref
8405 : 2274 : || expr->ts.type == BT_CHARACTER));
8406 : 137354 : need_tmp = (gfc_ref_needs_temporary_p (expr->ref)
8407 : 137354 : && !subref_array_target);
8408 : :
8409 : 137354 : if (se->force_tmp)
8410 : : need_tmp = 1;
8411 : 137284 : else if (se->force_no_tmp)
8412 : : need_tmp = 0;
8413 : :
8414 : 131141 : if (need_tmp)
8415 : : full = 0;
8416 : 137180 : else if (is_explicit_coarray (expr))
8417 : : full = 0;
8418 : 136544 : else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
8419 : : {
8420 : : /* Create a new descriptor if the array doesn't have one. */
8421 : : full = 0;
8422 : : }
8423 : 87653 : else if (info->ref->u.ar.type == AR_FULL || se->descriptor_only)
8424 : : full = 1;
8425 : 7861 : else if (se->direct_byref)
8426 : : full = 0;
8427 : 7529 : else if (info->ref->u.ar.dimen == 0 && !info->ref->next)
8428 : : full = 1;
8429 : 7423 : else if (info->ref->u.ar.type == AR_SECTION && se->want_pointer)
8430 : : full = 0;
8431 : : else
8432 : 3552 : full = gfc_full_array_ref_p (info->ref, NULL);
8433 : :
8434 : 163588 : if (full && !transposed_dims (ss))
8435 : : {
8436 : 80081 : if (se->direct_byref && !se->byref_noassign)
8437 : : {
8438 : 1017 : struct lang_type *lhs_ls
8439 : 1017 : = TYPE_LANG_SPECIFIC (TREE_TYPE (se->expr)),
8440 : 1017 : *rhs_ls = TYPE_LANG_SPECIFIC (TREE_TYPE (desc));
8441 : : /* When only the array_kind differs, do a view_convert. */
8442 : 1381 : tmp = lhs_ls && rhs_ls && lhs_ls->rank == rhs_ls->rank
8443 : 1017 : && lhs_ls->akind != rhs_ls->akind
8444 : 1381 : ? build1 (VIEW_CONVERT_EXPR, TREE_TYPE (se->expr), desc)
8445 : : : desc;
8446 : : /* Copy the descriptor for pointer assignments. */
8447 : 1017 : gfc_add_modify (&se->pre, se->expr, tmp);
8448 : :
8449 : : /* Add any offsets from subreferences. */
8450 : 1017 : gfc_get_dataptr_offset (&se->pre, se->expr, desc, NULL_TREE,
8451 : : subref_array_target, expr);
8452 : :
8453 : : /* ....and set the span field. */
8454 : 1017 : if (ss_info->expr->ts.type == BT_CHARACTER)
8455 : 141 : tmp = gfc_conv_descriptor_span_get (desc);
8456 : : else
8457 : 876 : tmp = gfc_get_array_span (desc, expr);
8458 : 1017 : gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
8459 : 1017 : }
8460 : 79064 : else if (se->want_pointer)
8461 : : {
8462 : : /* We pass full arrays directly. This means that pointers and
8463 : : allocatable arrays should also work. */
8464 : 13499 : se->expr = gfc_build_addr_expr (NULL_TREE, desc);
8465 : : }
8466 : : else
8467 : : {
8468 : 65565 : se->expr = desc;
8469 : : }
8470 : :
8471 : 80081 : if (expr->ts.type == BT_CHARACTER && !deferred_array_component)
8472 : 8279 : se->string_length = gfc_get_expr_charlen (expr);
8473 : : /* The ss_info string length is returned set to the value of the
8474 : : hidden string length component. */
8475 : 71539 : else if (deferred_array_component)
8476 : 263 : se->string_length = ss_info->string_length;
8477 : :
8478 : 80081 : se->class_container = ss_info->class_container;
8479 : :
8480 : 80081 : gfc_free_ss_chain (ss);
8481 : 160288 : return;
8482 : : }
8483 : : break;
8484 : :
8485 : 4806 : case EXPR_FUNCTION:
8486 : : /* A transformational function return value will be a temporary
8487 : : array descriptor. We still need to go through the scalarizer
8488 : : to create the descriptor. Elemental functions are handled as
8489 : : arbitrary expressions, i.e. copy to a temporary. */
8490 : :
8491 : 4806 : if (se->direct_byref)
8492 : : {
8493 : 126 : gcc_assert (ss_type == GFC_SS_FUNCTION && ss_expr == expr);
8494 : :
8495 : : /* For pointer assignments pass the descriptor directly. */
8496 : 126 : if (se->ss == NULL)
8497 : 126 : se->ss = ss;
8498 : : else
8499 : 0 : gcc_assert (se->ss == ss);
8500 : :
8501 : 126 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
8502 : 126 : gfc_conv_expr (se, expr);
8503 : :
8504 : 126 : gfc_free_ss_chain (ss);
8505 : 126 : return;
8506 : : }
8507 : :
8508 : 4680 : if (ss_expr != expr || ss_type != GFC_SS_FUNCTION)
8509 : : {
8510 : 3286 : if (ss_expr != expr)
8511 : : /* Elemental function. */
8512 : 2561 : gcc_assert ((expr->value.function.esym != NULL
8513 : : && expr->value.function.esym->attr.elemental)
8514 : : || (expr->value.function.isym != NULL
8515 : : && expr->value.function.isym->elemental)
8516 : : || (gfc_expr_attr (expr).proc_pointer
8517 : : && gfc_expr_attr (expr).elemental)
8518 : : || gfc_inline_intrinsic_function_p (expr));
8519 : :
8520 : 3286 : need_tmp = 1;
8521 : 3286 : if (expr->ts.type == BT_CHARACTER
8522 : 35 : && expr->ts.u.cl->length
8523 : 29 : && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8524 : 13 : get_array_charlen (expr, se);
8525 : :
8526 : : info = NULL;
8527 : : }
8528 : : else
8529 : : {
8530 : : /* Transformational function. */
8531 : 1394 : info = &ss_info->data.array;
8532 : 1394 : need_tmp = 0;
8533 : : }
8534 : : break;
8535 : :
8536 : 10140 : case EXPR_ARRAY:
8537 : : /* Constant array constructors don't need a temporary. */
8538 : 10140 : if (ss_type == GFC_SS_CONSTRUCTOR
8539 : 10140 : && expr->ts.type != BT_CHARACTER
8540 : 19093 : && gfc_constant_array_constructor_p (expr->value.constructor))
8541 : : {
8542 : 6586 : need_tmp = 0;
8543 : 6586 : info = &ss_info->data.array;
8544 : : }
8545 : : else
8546 : : {
8547 : : need_tmp = 1;
8548 : : info = NULL;
8549 : : }
8550 : : break;
8551 : :
8552 : : default:
8553 : : /* Something complicated. Copy it into a temporary. */
8554 : : need_tmp = 1;
8555 : : info = NULL;
8556 : : break;
8557 : : }
8558 : :
8559 : : /* If we are creating a temporary, we don't need to bother about aliases
8560 : : anymore. */
8561 : 65253 : if (need_tmp)
8562 : 7715 : se->force_tmp = 0;
8563 : :
8564 : 72794 : gfc_init_loopinfo (&loop);
8565 : :
8566 : : /* Associate the SS with the loop. */
8567 : 72794 : gfc_add_ss_to_loop (&loop, ss);
8568 : :
8569 : : /* Tell the scalarizer not to bother creating loop variables, etc. */
8570 : 72794 : if (!need_tmp)
8571 : 65079 : loop.array_parameter = 1;
8572 : : else
8573 : : /* The right-hand side of a pointer assignment mustn't use a temporary. */
8574 : 7715 : gcc_assert (!se->direct_byref);
8575 : :
8576 : : /* Do we need bounds checking or not? */
8577 : 72794 : ss->no_bounds_check = expr->no_bounds_check;
8578 : :
8579 : : /* Setup the scalarizing loops and bounds. */
8580 : 72794 : gfc_conv_ss_startstride (&loop);
8581 : :
8582 : : /* Add bounds-checking for elemental dimensions. */
8583 : 72794 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !expr->no_bounds_check)
8584 : 6667 : array_bound_check_elemental (se, ss, expr);
8585 : :
8586 : 72794 : if (need_tmp)
8587 : : {
8588 : 7715 : if (expr->ts.type == BT_CHARACTER
8589 : 1398 : && (!expr->ts.u.cl->backend_decl || expr->expr_type == EXPR_ARRAY))
8590 : 1318 : get_array_charlen (expr, se);
8591 : :
8592 : : /* Tell the scalarizer to make a temporary. */
8593 : 7715 : loop.temp_ss = gfc_get_temp_ss (gfc_typenode_for_spec (&expr->ts),
8594 : 7715 : ((expr->ts.type == BT_CHARACTER)
8595 : 1398 : ? expr->ts.u.cl->backend_decl
8596 : : : NULL),
8597 : : loop.dimen);
8598 : :
8599 : 7715 : se->string_length = loop.temp_ss->info->string_length;
8600 : 7715 : gcc_assert (loop.temp_ss->dimen == loop.dimen);
8601 : 7715 : gfc_add_ss_to_loop (&loop, loop.temp_ss);
8602 : : }
8603 : :
8604 : 72794 : gfc_conv_loop_setup (&loop, & expr->where);
8605 : :
8606 : 72794 : if (need_tmp)
8607 : : {
8608 : : /* Copy into a temporary and pass that. We don't need to copy the data
8609 : : back because expressions and vector subscripts must be INTENT_IN. */
8610 : : /* TODO: Optimize passing function return values. */
8611 : 7715 : gfc_se lse;
8612 : 7715 : gfc_se rse;
8613 : 7715 : bool deep_copy;
8614 : :
8615 : : /* Start the copying loops. */
8616 : 7715 : gfc_mark_ss_chain_used (loop.temp_ss, 1);
8617 : 7715 : gfc_mark_ss_chain_used (ss, 1);
8618 : 7715 : gfc_start_scalarized_body (&loop, &block);
8619 : :
8620 : : /* Copy each data element. */
8621 : 7715 : gfc_init_se (&lse, NULL);
8622 : 7715 : gfc_copy_loopinfo_to_se (&lse, &loop);
8623 : 7715 : gfc_init_se (&rse, NULL);
8624 : 7715 : gfc_copy_loopinfo_to_se (&rse, &loop);
8625 : :
8626 : 7715 : lse.ss = loop.temp_ss;
8627 : 7715 : rse.ss = ss;
8628 : :
8629 : 7715 : gfc_conv_tmp_array_ref (&lse);
8630 : 7715 : if (expr->ts.type == BT_CHARACTER)
8631 : : {
8632 : 1398 : gfc_conv_expr (&rse, expr);
8633 : 1398 : if (POINTER_TYPE_P (TREE_TYPE (rse.expr)))
8634 : 1099 : rse.expr = build_fold_indirect_ref_loc (input_location,
8635 : : rse.expr);
8636 : : }
8637 : : else
8638 : 6317 : gfc_conv_expr_val (&rse, expr);
8639 : :
8640 : 7715 : gfc_add_block_to_block (&block, &rse.pre);
8641 : 7715 : gfc_add_block_to_block (&block, &lse.pre);
8642 : :
8643 : 7715 : lse.string_length = rse.string_length;
8644 : :
8645 : 15430 : deep_copy = !se->data_not_needed
8646 : 7715 : && (expr->expr_type == EXPR_VARIABLE
8647 : 7288 : || expr->expr_type == EXPR_ARRAY);
8648 : 7715 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
8649 : : deep_copy, false);
8650 : 7715 : gfc_add_expr_to_block (&block, tmp);
8651 : :
8652 : : /* Finish the copying loops. */
8653 : 7715 : gfc_trans_scalarizing_loops (&loop, &block);
8654 : :
8655 : 7715 : desc = loop.temp_ss->info->data.array.descriptor;
8656 : : }
8657 : 66473 : else if (expr->expr_type == EXPR_FUNCTION && !transposed_dims (ss))
8658 : : {
8659 : 1381 : desc = info->descriptor;
8660 : 1381 : se->string_length = ss_info->string_length;
8661 : : }
8662 : : else
8663 : : {
8664 : : /* We pass sections without copying to a temporary. Make a new
8665 : : descriptor and point it at the section we want. The loop variable
8666 : : limits will be the limits of the section.
8667 : : A function may decide to repack the array to speed up access, but
8668 : : we're not bothered about that here. */
8669 : 63698 : int dim, ndim, codim;
8670 : 63698 : tree parm;
8671 : 63698 : tree parmtype;
8672 : 63698 : tree dtype;
8673 : 63698 : tree stride;
8674 : 63698 : tree from;
8675 : 63698 : tree to;
8676 : 63698 : tree base;
8677 : 63698 : tree offset;
8678 : :
8679 : 63698 : ndim = info->ref ? info->ref->u.ar.dimen : ss->dimen;
8680 : :
8681 : 63698 : if (se->want_coarray)
8682 : : {
8683 : 529 : gfc_array_ref *ar = &info->ref->u.ar;
8684 : :
8685 : 529 : codim = expr->corank;
8686 : 1083 : for (n = 0; n < codim - 1; n++)
8687 : : {
8688 : : /* Make sure we are not lost somehow. */
8689 : 554 : gcc_assert (ar->dimen_type[n + ndim] == DIMEN_THIS_IMAGE);
8690 : :
8691 : : /* Make sure the call to gfc_conv_section_startstride won't
8692 : : generate unnecessary code to calculate stride. */
8693 : 554 : gcc_assert (ar->stride[n + ndim] == NULL);
8694 : :
8695 : 554 : gfc_conv_section_startstride (&loop.pre, ss, n + ndim);
8696 : 554 : loop.from[n + loop.dimen] = info->start[n + ndim];
8697 : 554 : loop.to[n + loop.dimen] = info->end[n + ndim];
8698 : : }
8699 : :
8700 : 529 : gcc_assert (n == codim - 1);
8701 : 529 : evaluate_bound (&loop.pre, info->start, ar->start,
8702 : : info->descriptor, n + ndim, true,
8703 : 529 : ar->as->type == AS_DEFERRED, true);
8704 : 529 : loop.from[n + loop.dimen] = info->start[n + ndim];
8705 : : }
8706 : : else
8707 : : codim = 0;
8708 : :
8709 : : /* Set the string_length for a character array. */
8710 : 63698 : if (expr->ts.type == BT_CHARACTER)
8711 : : {
8712 : 11351 : if (deferred_array_component && !substr)
8713 : 37 : se->string_length = ss_info->string_length;
8714 : : else
8715 : 11314 : se->string_length = gfc_get_expr_charlen (expr);
8716 : :
8717 : 11351 : if (VAR_P (se->string_length)
8718 : 978 : && expr->ts.u.cl->backend_decl == se->string_length)
8719 : 972 : tmp = ss_info->string_length;
8720 : : else
8721 : : tmp = se->string_length;
8722 : :
8723 : 11351 : if (expr->ts.deferred && expr->ts.u.cl->backend_decl
8724 : 205 : && VAR_P (expr->ts.u.cl->backend_decl))
8725 : 144 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl, tmp);
8726 : : else
8727 : 11207 : expr->ts.u.cl->backend_decl = tmp;
8728 : : }
8729 : :
8730 : : /* If we have an array section, are assigning or passing an array
8731 : : section argument make sure that the lower bound is 1. References
8732 : : to the full array should otherwise keep the original bounds. */
8733 : 63698 : if (!info->ref || info->ref->u.ar.type != AR_FULL)
8734 : 81763 : for (dim = 0; dim < loop.dimen; dim++)
8735 : 49888 : if (!integer_onep (loop.from[dim]))
8736 : : {
8737 : 26564 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
8738 : : gfc_array_index_type, gfc_index_one_node,
8739 : : loop.from[dim]);
8740 : 26564 : loop.to[dim] = fold_build2_loc (input_location, PLUS_EXPR,
8741 : : gfc_array_index_type,
8742 : : loop.to[dim], tmp);
8743 : 26564 : loop.from[dim] = gfc_index_one_node;
8744 : : }
8745 : :
8746 : 63698 : desc = info->descriptor;
8747 : 63698 : if (se->direct_byref && !se->byref_noassign)
8748 : : {
8749 : : /* For pointer assignments we fill in the destination. */
8750 : 2594 : parm = se->expr;
8751 : 2594 : parmtype = TREE_TYPE (parm);
8752 : : }
8753 : : else
8754 : : {
8755 : : /* Otherwise make a new one. */
8756 : 61104 : if (expr->ts.type == BT_CHARACTER)
8757 : 10723 : parmtype = gfc_typenode_for_spec (&expr->ts);
8758 : : else
8759 : 50381 : parmtype = gfc_get_element_type (TREE_TYPE (desc));
8760 : :
8761 : 61104 : parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, codim,
8762 : : loop.from, loop.to, 0,
8763 : : GFC_ARRAY_UNKNOWN, false);
8764 : 61104 : parm = gfc_create_var (parmtype, "parm");
8765 : :
8766 : : /* When expression is a class object, then add the class' handle to
8767 : : the parm_decl. */
8768 : 61104 : if (expr->ts.type == BT_CLASS && expr->expr_type == EXPR_VARIABLE)
8769 : : {
8770 : 1134 : gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
8771 : 1134 : gfc_se classse;
8772 : :
8773 : : /* class_expr can be NULL, when no _class ref is in expr.
8774 : : We must not fix this here with a gfc_fix_class_ref (). */
8775 : 1134 : if (class_expr)
8776 : : {
8777 : 1124 : gfc_init_se (&classse, NULL);
8778 : 1124 : gfc_conv_expr (&classse, class_expr);
8779 : 1124 : gfc_free_expr (class_expr);
8780 : :
8781 : 1124 : gcc_assert (classse.pre.head == NULL_TREE
8782 : : && classse.post.head == NULL_TREE);
8783 : 1124 : gfc_allocate_lang_decl (parm);
8784 : 1124 : GFC_DECL_SAVED_DESCRIPTOR (parm) = classse.expr;
8785 : : }
8786 : : }
8787 : : }
8788 : :
8789 : 63698 : if (expr->ts.type == BT_CHARACTER
8790 : 63698 : && VAR_P (TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)))))
8791 : : {
8792 : 0 : tree elem_len = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)));
8793 : 0 : gfc_add_modify (&loop.pre, elem_len,
8794 : 0 : fold_convert (TREE_TYPE (elem_len),
8795 : : gfc_get_array_span (desc, expr)));
8796 : : }
8797 : :
8798 : : /* Set the span field. */
8799 : 63698 : tmp = NULL_TREE;
8800 : 63698 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
8801 : 7601 : tmp = gfc_conv_descriptor_span_get (desc);
8802 : : else
8803 : 56097 : tmp = gfc_get_array_span (desc, expr);
8804 : 63698 : if (tmp)
8805 : 63618 : gfc_conv_descriptor_span_set (&loop.pre, parm, tmp);
8806 : :
8807 : : /* The following can be somewhat confusing. We have two
8808 : : descriptors, a new one and the original array.
8809 : : {parm, parmtype, dim} refer to the new one.
8810 : : {desc, type, n, loop} refer to the original, which maybe
8811 : : a descriptorless array.
8812 : : The bounds of the scalarization are the bounds of the section.
8813 : : We don't have to worry about numeric overflows when calculating
8814 : : the offsets because all elements are within the array data. */
8815 : :
8816 : : /* Set the dtype. */
8817 : 63698 : tmp = gfc_conv_descriptor_dtype (parm);
8818 : 63698 : if (se->unlimited_polymorphic)
8819 : 577 : dtype = gfc_get_dtype (TREE_TYPE (desc), &loop.dimen);
8820 : 63121 : else if (expr->ts.type == BT_ASSUMED)
8821 : : {
8822 : 127 : tree tmp2 = desc;
8823 : 127 : if (DECL_LANG_SPECIFIC (tmp2) && GFC_DECL_SAVED_DESCRIPTOR (tmp2))
8824 : 127 : tmp2 = GFC_DECL_SAVED_DESCRIPTOR (tmp2);
8825 : 127 : if (POINTER_TYPE_P (TREE_TYPE (tmp2)))
8826 : 127 : tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
8827 : 127 : dtype = gfc_conv_descriptor_dtype (tmp2);
8828 : : }
8829 : : else
8830 : 62994 : dtype = gfc_get_dtype (parmtype);
8831 : 63698 : gfc_add_modify (&loop.pre, tmp, dtype);
8832 : :
8833 : : /* The 1st element in the section. */
8834 : 63698 : base = gfc_index_zero_node;
8835 : 63698 : if (expr->ts.type == BT_CHARACTER && expr->rank == 0 && codim)
8836 : 6 : base = gfc_index_one_node;
8837 : :
8838 : : /* The offset from the 1st element in the section. */
8839 : : offset = gfc_index_zero_node;
8840 : :
8841 : 164579 : for (n = 0; n < ndim; n++)
8842 : : {
8843 : 100881 : stride = gfc_conv_array_stride (desc, n);
8844 : :
8845 : : /* Work out the 1st element in the section. */
8846 : 100881 : if (info->ref
8847 : 93969 : && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
8848 : : {
8849 : 1142 : gcc_assert (info->subscript[n]
8850 : : && info->subscript[n]->info->type == GFC_SS_SCALAR);
8851 : 1142 : start = info->subscript[n]->info->data.scalar.value;
8852 : : }
8853 : : else
8854 : : {
8855 : : /* Evaluate and remember the start of the section. */
8856 : 99739 : start = info->start[n];
8857 : 99739 : stride = gfc_evaluate_now (stride, &loop.pre);
8858 : : }
8859 : :
8860 : 100881 : tmp = gfc_conv_array_lbound (desc, n);
8861 : 100881 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
8862 : : start, tmp);
8863 : 100881 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
8864 : : tmp, stride);
8865 : 100881 : base = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
8866 : : base, tmp);
8867 : :
8868 : 100881 : if (info->ref
8869 : 93969 : && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
8870 : : {
8871 : : /* For elemental dimensions, we only need the 1st
8872 : : element in the section. */
8873 : 1142 : continue;
8874 : : }
8875 : :
8876 : : /* Vector subscripts need copying and are handled elsewhere. */
8877 : 99739 : if (info->ref)
8878 : 92827 : gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
8879 : :
8880 : : /* look for the corresponding scalarizer dimension: dim. */
8881 : 150852 : for (dim = 0; dim < ndim; dim++)
8882 : 150852 : if (ss->dim[dim] == n)
8883 : : break;
8884 : :
8885 : : /* loop exited early: the DIM being looked for has been found. */
8886 : 99739 : gcc_assert (dim < ndim);
8887 : :
8888 : : /* Set the new lower bound. */
8889 : 99739 : from = loop.from[dim];
8890 : 99739 : to = loop.to[dim];
8891 : :
8892 : 99739 : gfc_conv_descriptor_lbound_set (&loop.pre, parm,
8893 : : gfc_rank_cst[dim], from);
8894 : :
8895 : : /* Set the new upper bound. */
8896 : 99739 : gfc_conv_descriptor_ubound_set (&loop.pre, parm,
8897 : : gfc_rank_cst[dim], to);
8898 : :
8899 : : /* Multiply the stride by the section stride to get the
8900 : : total stride. */
8901 : 99739 : stride = fold_build2_loc (input_location, MULT_EXPR,
8902 : : gfc_array_index_type,
8903 : : stride, info->stride[n]);
8904 : :
8905 : 99739 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8906 : 99739 : TREE_TYPE (offset), stride, from);
8907 : 99739 : offset = fold_build2_loc (input_location, MINUS_EXPR,
8908 : 99739 : TREE_TYPE (offset), offset, tmp);
8909 : :
8910 : : /* Store the new stride. */
8911 : 99739 : gfc_conv_descriptor_stride_set (&loop.pre, parm,
8912 : : gfc_rank_cst[dim], stride);
8913 : : }
8914 : :
8915 : 64781 : for (n = loop.dimen; n < loop.dimen + codim; n++)
8916 : : {
8917 : 1083 : from = loop.from[n];
8918 : 1083 : to = loop.to[n];
8919 : 1083 : gfc_conv_descriptor_lbound_set (&loop.pre, parm,
8920 : : gfc_rank_cst[n], from);
8921 : 1083 : if (n < loop.dimen + codim - 1)
8922 : 554 : gfc_conv_descriptor_ubound_set (&loop.pre, parm,
8923 : : gfc_rank_cst[n], to);
8924 : : }
8925 : :
8926 : 63698 : if (se->data_not_needed)
8927 : 6001 : gfc_conv_descriptor_data_set (&loop.pre, parm,
8928 : : gfc_index_zero_node);
8929 : : else
8930 : : /* Point the data pointer at the 1st element in the section. */
8931 : 57697 : gfc_get_dataptr_offset (&loop.pre, parm, desc, base,
8932 : : subref_array_target, expr);
8933 : :
8934 : 63698 : gfc_conv_descriptor_offset_set (&loop.pre, parm, offset);
8935 : :
8936 : 63698 : if (flag_coarray == GFC_FCOARRAY_LIB && expr->corank)
8937 : : {
8938 : 215 : tmp = INDIRECT_REF_P (desc) ? TREE_OPERAND (desc, 0) : desc;
8939 : 215 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8940 : : {
8941 : 12 : tmp = gfc_conv_descriptor_token (tmp);
8942 : : }
8943 : 203 : else if (DECL_P (tmp) && DECL_LANG_SPECIFIC (tmp)
8944 : 245 : && GFC_DECL_TOKEN (tmp) != NULL_TREE)
8945 : 34 : tmp = GFC_DECL_TOKEN (tmp);
8946 : : else
8947 : : {
8948 : 169 : tmp = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (tmp));
8949 : : }
8950 : :
8951 : 215 : gfc_add_modify (&loop.pre, gfc_conv_descriptor_token (parm), tmp);
8952 : : }
8953 : : desc = parm;
8954 : : }
8955 : :
8956 : : /* For class arrays add the class tree into the saved descriptor to
8957 : : enable getting of _vptr and the like. */
8958 : 72794 : if (expr->expr_type == EXPR_VARIABLE && VAR_P (desc)
8959 : 56382 : && IS_CLASS_ARRAY (expr->symtree->n.sym))
8960 : : {
8961 : 1109 : gfc_allocate_lang_decl (desc);
8962 : 1109 : GFC_DECL_SAVED_DESCRIPTOR (desc) =
8963 : 1109 : DECL_LANG_SPECIFIC (expr->symtree->n.sym->backend_decl) ?
8964 : 1023 : GFC_DECL_SAVED_DESCRIPTOR (expr->symtree->n.sym->backend_decl)
8965 : : : expr->symtree->n.sym->backend_decl;
8966 : : }
8967 : 71685 : else if (expr->expr_type == EXPR_ARRAY && VAR_P (desc)
8968 : 10140 : && IS_CLASS_ARRAY (expr))
8969 : : {
8970 : 12 : tree vtype;
8971 : 12 : gfc_allocate_lang_decl (desc);
8972 : 12 : tmp = gfc_create_var (expr->ts.u.derived->backend_decl, "class");
8973 : 12 : GFC_DECL_SAVED_DESCRIPTOR (desc) = tmp;
8974 : 12 : vtype = gfc_class_vptr_get (tmp);
8975 : 12 : gfc_add_modify (&se->pre, vtype,
8976 : 12 : gfc_build_addr_expr (TREE_TYPE (vtype),
8977 : 12 : gfc_find_vtab (&expr->ts)->backend_decl));
8978 : : }
8979 : 72794 : if (!se->direct_byref || se->byref_noassign)
8980 : : {
8981 : : /* Get a pointer to the new descriptor. */
8982 : 70200 : if (se->want_pointer)
8983 : 40331 : se->expr = gfc_build_addr_expr (NULL_TREE, desc);
8984 : : else
8985 : 29869 : se->expr = desc;
8986 : : }
8987 : :
8988 : 72794 : gfc_add_block_to_block (&se->pre, &loop.pre);
8989 : 72794 : gfc_add_block_to_block (&se->post, &loop.post);
8990 : :
8991 : : /* Cleanup the scalarizer. */
8992 : 72794 : gfc_cleanup_loop (&loop);
8993 : : }
8994 : :
8995 : :
8996 : : /* Calculate the array size (number of elements); if dim != NULL_TREE,
8997 : : return size for that dim (dim=0..rank-1; only for GFC_DESCRIPTOR_TYPE_P).
8998 : : If !expr && descriptor array, the rank is taken from the descriptor. */
8999 : : tree
9000 : 14794 : gfc_tree_array_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree dim)
9001 : : {
9002 : 14794 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
9003 : : {
9004 : 34 : gcc_assert (dim == NULL_TREE);
9005 : 34 : return GFC_TYPE_ARRAY_SIZE (TREE_TYPE (desc));
9006 : : }
9007 : 14760 : tree size, tmp, rank = NULL_TREE, cond = NULL_TREE;
9008 : 14760 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
9009 : 14760 : enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (TREE_TYPE (desc));
9010 : 14760 : if (expr == NULL || expr->rank < 0)
9011 : 3071 : rank = fold_convert (signed_char_type_node,
9012 : : gfc_conv_descriptor_rank (desc));
9013 : : else
9014 : 11689 : rank = build_int_cst (signed_char_type_node, expr->rank);
9015 : :
9016 : 14760 : if (dim || (expr && expr->rank == 1))
9017 : : {
9018 : 4430 : if (!dim)
9019 : 4430 : dim = gfc_index_zero_node;
9020 : 13107 : tree ubound = gfc_conv_descriptor_ubound_get (desc, dim);
9021 : 13107 : tree lbound = gfc_conv_descriptor_lbound_get (desc, dim);
9022 : :
9023 : 13107 : size = fold_build2_loc (input_location, MINUS_EXPR,
9024 : : gfc_array_index_type, ubound, lbound);
9025 : 13107 : size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
9026 : : size, gfc_index_one_node);
9027 : : /* if (!allocatable && !pointer && assumed rank)
9028 : : size = (idx == rank && ubound[rank-1] == -1 ? -1 : size;
9029 : : else
9030 : : size = max (0, size); */
9031 : 13107 : size = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
9032 : : size, gfc_index_zero_node);
9033 : 13107 : if (akind == GFC_ARRAY_ASSUMED_RANK_CONT
9034 : 13107 : || akind == GFC_ARRAY_ASSUMED_RANK)
9035 : : {
9036 : 2384 : tmp = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
9037 : : rank, build_int_cst (signed_char_type_node, 1));
9038 : 2384 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
9039 : : fold_convert (signed_char_type_node, dim),
9040 : : tmp);
9041 : 2384 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
9042 : : gfc_conv_descriptor_ubound_get (desc, dim),
9043 : : build_int_cst (gfc_array_index_type, -1));
9044 : 2384 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
9045 : : cond, tmp);
9046 : 2384 : tmp = build_int_cst (gfc_array_index_type, -1);
9047 : 2384 : size = build3_loc (input_location, COND_EXPR, gfc_array_index_type,
9048 : : cond, tmp, size);
9049 : : }
9050 : 13107 : return size;
9051 : : }
9052 : :
9053 : : /* size = 1. */
9054 : 1653 : size = gfc_create_var (gfc_array_index_type, "size");
9055 : 1653 : gfc_add_modify (block, size, build_int_cst (TREE_TYPE (size), 1));
9056 : 1653 : tree extent = gfc_create_var (gfc_array_index_type, "extent");
9057 : :
9058 : 1653 : stmtblock_t cond_block, loop_body;
9059 : 1653 : gfc_init_block (&cond_block);
9060 : 1653 : gfc_init_block (&loop_body);
9061 : :
9062 : : /* Loop: for (i = 0; i < rank; ++i). */
9063 : 1653 : tree idx = gfc_create_var (signed_char_type_node, "idx");
9064 : : /* Loop body. */
9065 : : /* #if (assumed-rank + !allocatable && !pointer)
9066 : : if (idx == rank - 1 && dim[idx].ubound == -1)
9067 : : extent = -1;
9068 : : else
9069 : : #endif
9070 : : extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1
9071 : : if (extent < 0)
9072 : : extent = 0
9073 : : size *= extent. */
9074 : 1653 : cond = NULL_TREE;
9075 : 1653 : if (akind == GFC_ARRAY_ASSUMED_RANK_CONT || akind == GFC_ARRAY_ASSUMED_RANK)
9076 : : {
9077 : 459 : tmp = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
9078 : : rank, build_int_cst (signed_char_type_node, 1));
9079 : 459 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
9080 : : idx, tmp);
9081 : 459 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
9082 : : gfc_conv_descriptor_ubound_get (desc, idx),
9083 : : build_int_cst (gfc_array_index_type, -1));
9084 : 459 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
9085 : : cond, tmp);
9086 : : }
9087 : 1653 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9088 : : gfc_conv_descriptor_ubound_get (desc, idx),
9089 : : gfc_conv_descriptor_lbound_get (desc, idx));
9090 : 1653 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
9091 : : tmp, gfc_index_one_node);
9092 : 1653 : gfc_add_modify (&cond_block, extent, tmp);
9093 : 1653 : tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
9094 : : extent, gfc_index_zero_node);
9095 : 1653 : tmp = build3_v (COND_EXPR, tmp,
9096 : : fold_build2_loc (input_location, MODIFY_EXPR,
9097 : : gfc_array_index_type,
9098 : : extent, gfc_index_zero_node),
9099 : : build_empty_stmt (input_location));
9100 : 1653 : gfc_add_expr_to_block (&cond_block, tmp);
9101 : 1653 : tmp = gfc_finish_block (&cond_block);
9102 : 1653 : if (cond)
9103 : 459 : tmp = build3_v (COND_EXPR, cond,
9104 : : fold_build2_loc (input_location, MODIFY_EXPR,
9105 : : gfc_array_index_type, extent,
9106 : : build_int_cst (gfc_array_index_type, -1)),
9107 : : tmp);
9108 : 1653 : gfc_add_expr_to_block (&loop_body, tmp);
9109 : : /* size *= extent. */
9110 : 1653 : gfc_add_modify (&loop_body, size,
9111 : : fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9112 : : size, extent));
9113 : : /* Generate loop. */
9114 : 3306 : gfc_simple_for_loop (block, idx, build_int_cst (TREE_TYPE (idx), 0), rank, LT_EXPR,
9115 : 1653 : build_int_cst (TREE_TYPE (idx), 1),
9116 : : gfc_finish_block (&loop_body));
9117 : 1653 : return size;
9118 : : }
9119 : :
9120 : : /* Helper function for gfc_conv_array_parameter if array size needs to be
9121 : : computed. */
9122 : :
9123 : : static void
9124 : 102 : array_parameter_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree *size)
9125 : : {
9126 : 102 : tree elem;
9127 : 102 : *size = gfc_tree_array_size (block, desc, expr, NULL);
9128 : 102 : elem = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
9129 : 102 : *size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9130 : : *size, fold_convert (gfc_array_index_type, elem));
9131 : 102 : }
9132 : :
9133 : : /* Helper function - return true if the argument is a pointer. */
9134 : :
9135 : : static bool
9136 : 733 : is_pointer (gfc_expr *e)
9137 : : {
9138 : 733 : gfc_symbol *sym;
9139 : :
9140 : 733 : if (e->expr_type != EXPR_VARIABLE || e->symtree == NULL)
9141 : : return false;
9142 : :
9143 : 733 : sym = e->symtree->n.sym;
9144 : 733 : if (sym == NULL)
9145 : : return false;
9146 : :
9147 : 733 : return sym->attr.pointer || sym->attr.proc_pointer;
9148 : : }
9149 : :
9150 : : /* Convert an array for passing as an actual parameter. */
9151 : :
9152 : : void
9153 : 65272 : gfc_conv_array_parameter (gfc_se *se, gfc_expr *expr, bool g77,
9154 : : const gfc_symbol *fsym, const char *proc_name,
9155 : : tree *size, tree *lbshift, tree *packed)
9156 : : {
9157 : 65272 : tree ptr;
9158 : 65272 : tree desc;
9159 : 65272 : tree tmp = NULL_TREE;
9160 : 65272 : tree stmt;
9161 : 65272 : tree parent = DECL_CONTEXT (current_function_decl);
9162 : 65272 : tree ctree;
9163 : 65272 : tree pack_attr = NULL_TREE; /* Set when packing class arrays. */
9164 : 65272 : bool full_array_var;
9165 : 65272 : bool this_array_result;
9166 : 65272 : bool contiguous;
9167 : 65272 : bool no_pack;
9168 : 65272 : bool array_constructor;
9169 : 65272 : bool good_allocatable;
9170 : 65272 : bool ultimate_ptr_comp;
9171 : 65272 : bool ultimate_alloc_comp;
9172 : 65272 : bool readonly;
9173 : 65272 : gfc_symbol *sym;
9174 : 65272 : stmtblock_t block;
9175 : 65272 : gfc_ref *ref;
9176 : :
9177 : 65272 : ultimate_ptr_comp = false;
9178 : 65272 : ultimate_alloc_comp = false;
9179 : :
9180 : 65972 : for (ref = expr->ref; ref; ref = ref->next)
9181 : : {
9182 : 54202 : if (ref->next == NULL)
9183 : : break;
9184 : :
9185 : 700 : if (ref->type == REF_COMPONENT)
9186 : : {
9187 : 622 : ultimate_ptr_comp = ref->u.c.component->attr.pointer;
9188 : 622 : ultimate_alloc_comp = ref->u.c.component->attr.allocatable;
9189 : : }
9190 : : }
9191 : :
9192 : 65272 : full_array_var = false;
9193 : 65272 : contiguous = false;
9194 : :
9195 : 65272 : if (expr->expr_type == EXPR_VARIABLE && ref && !ultimate_ptr_comp)
9196 : 53410 : full_array_var = gfc_full_array_ref_p (ref, &contiguous);
9197 : :
9198 : 53410 : sym = full_array_var ? expr->symtree->n.sym : NULL;
9199 : :
9200 : : /* The symbol should have an array specification. */
9201 : 62470 : gcc_assert (!sym || sym->as || ref->u.ar.as);
9202 : :
9203 : 65272 : if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_CHARACTER)
9204 : : {
9205 : 678 : get_array_ctor_strlen (&se->pre, expr->value.constructor, &tmp);
9206 : 678 : expr->ts.u.cl->backend_decl = tmp;
9207 : 678 : se->string_length = tmp;
9208 : : }
9209 : :
9210 : : /* Is this the result of the enclosing procedure? */
9211 : 65272 : this_array_result = (full_array_var && sym->attr.flavor == FL_PROCEDURE);
9212 : 58 : if (this_array_result
9213 : 58 : && (sym->backend_decl != current_function_decl)
9214 : 0 : && (sym->backend_decl != parent))
9215 : 65272 : this_array_result = false;
9216 : :
9217 : : /* Passing an optional dummy argument as actual to an optional dummy? */
9218 : 65272 : bool pass_optional;
9219 : 65272 : pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
9220 : :
9221 : : /* Passing address of the array if it is not pointer or assumed-shape. */
9222 : 65272 : if (full_array_var && g77 && !this_array_result
9223 : 15360 : && sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
9224 : : {
9225 : 12319 : tmp = gfc_get_symbol_decl (sym);
9226 : :
9227 : 12319 : if (sym->ts.type == BT_CHARACTER)
9228 : 2741 : se->string_length = sym->ts.u.cl->backend_decl;
9229 : :
9230 : 12319 : if (!sym->attr.pointer
9231 : 11814 : && sym->as
9232 : 11814 : && sym->as->type != AS_ASSUMED_SHAPE
9233 : 11570 : && sym->as->type != AS_DEFERRED
9234 : 10168 : && sym->as->type != AS_ASSUMED_RANK
9235 : 10092 : && !sym->attr.allocatable)
9236 : : {
9237 : : /* Some variables are declared directly, others are declared as
9238 : : pointers and allocated on the heap. */
9239 : 9640 : if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
9240 : 2488 : se->expr = tmp;
9241 : : else
9242 : 7152 : se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
9243 : 9640 : if (size)
9244 : 34 : array_parameter_size (&se->pre, tmp, expr, size);
9245 : 16230 : return;
9246 : : }
9247 : :
9248 : 2679 : if (sym->attr.allocatable)
9249 : : {
9250 : 1734 : if (sym->attr.dummy || sym->attr.result)
9251 : : {
9252 : 1092 : gfc_conv_expr_descriptor (se, expr);
9253 : 1092 : tmp = se->expr;
9254 : : }
9255 : 1734 : if (size)
9256 : 6 : array_parameter_size (&se->pre, tmp, expr, size);
9257 : 1734 : se->expr = gfc_conv_array_data (tmp);
9258 : 1734 : if (pass_optional)
9259 : : {
9260 : 18 : tree cond = gfc_conv_expr_present (sym);
9261 : 36 : se->expr = build3_loc (input_location, COND_EXPR,
9262 : 18 : TREE_TYPE (se->expr), cond, se->expr,
9263 : 18 : fold_convert (TREE_TYPE (se->expr),
9264 : : null_pointer_node));
9265 : : }
9266 : 1734 : return;
9267 : : }
9268 : : }
9269 : :
9270 : : /* A convenient reduction in scope. */
9271 : 53898 : contiguous = g77 && !this_array_result && contiguous;
9272 : :
9273 : : /* There is no need to pack and unpack the array, if it is contiguous
9274 : : and not a deferred- or assumed-shape array, or if it is simply
9275 : : contiguous. */
9276 : 53898 : no_pack = false;
9277 : : // clang-format off
9278 : 53898 : if (sym)
9279 : : {
9280 : 39324 : symbol_attribute *attr = &(IS_CLASS_ARRAY (sym)
9281 : : ? CLASS_DATA (sym)->attr : sym->attr);
9282 : 39324 : gfc_array_spec *as = IS_CLASS_ARRAY (sym)
9283 : 39324 : ? CLASS_DATA (sym)->as : sym->as;
9284 : 39324 : no_pack = (as
9285 : 39058 : && !attr->pointer
9286 : 35839 : && as->type != AS_DEFERRED
9287 : 26304 : && as->type != AS_ASSUMED_RANK
9288 : 63023 : && as->type != AS_ASSUMED_SHAPE);
9289 : : }
9290 : 53898 : if (ref && ref->u.ar.as)
9291 : 42126 : no_pack = no_pack
9292 : 42126 : || (ref->u.ar.as->type != AS_DEFERRED
9293 : : && ref->u.ar.as->type != AS_ASSUMED_RANK
9294 : : && ref->u.ar.as->type != AS_ASSUMED_SHAPE);
9295 : 107796 : no_pack = contiguous
9296 : 53898 : && (no_pack || gfc_is_simply_contiguous (expr, false, true));
9297 : : // clang-format on
9298 : :
9299 : : /* If we have an EXPR_OP or a function returning an explicit-shaped
9300 : : or allocatable array, an array temporary will be generated which
9301 : : does not need to be packed / unpacked if passed to an
9302 : : explicit-shape dummy array. */
9303 : :
9304 : 53898 : if (g77)
9305 : : {
9306 : 5919 : if (expr->expr_type == EXPR_OP)
9307 : : no_pack = 1;
9308 : 5842 : else if (expr->expr_type == EXPR_FUNCTION && expr->value.function.esym)
9309 : : {
9310 : 41 : gfc_symbol *result = expr->value.function.esym->result;
9311 : 41 : if (result->attr.dimension
9312 : 41 : && (result->as->type == AS_EXPLICIT
9313 : 14 : || result->attr.allocatable
9314 : 7 : || result->attr.contiguous))
9315 : 112 : no_pack = 1;
9316 : : }
9317 : : }
9318 : :
9319 : : /* Array constructors are always contiguous and do not need packing. */
9320 : 53898 : array_constructor = g77 && !this_array_result && expr->expr_type == EXPR_ARRAY;
9321 : :
9322 : : /* Same is true of contiguous sections from allocatable variables. */
9323 : 107796 : good_allocatable = contiguous
9324 : 4103 : && expr->symtree
9325 : 58001 : && expr->symtree->n.sym->attr.allocatable;
9326 : :
9327 : : /* Or ultimate allocatable components. */
9328 : 53898 : ultimate_alloc_comp = contiguous && ultimate_alloc_comp;
9329 : :
9330 : 53898 : if (no_pack || array_constructor || good_allocatable || ultimate_alloc_comp)
9331 : : {
9332 : 4470 : gfc_conv_expr_descriptor (se, expr);
9333 : : /* Deallocate the allocatable components of structures that are
9334 : : not variable. */
9335 : 4470 : if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
9336 : 2956 : && expr->ts.u.derived->attr.alloc_comp
9337 : 1675 : && expr->expr_type != EXPR_VARIABLE)
9338 : : {
9339 : 2 : tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, se->expr, expr->rank);
9340 : :
9341 : : /* The components shall be deallocated before their containing entity. */
9342 : 2 : gfc_prepend_expr_to_block (&se->post, tmp);
9343 : : }
9344 : 4470 : if (expr->ts.type == BT_CHARACTER && expr->expr_type != EXPR_FUNCTION)
9345 : 279 : se->string_length = expr->ts.u.cl->backend_decl;
9346 : 4470 : if (size)
9347 : 32 : array_parameter_size (&se->pre, se->expr, expr, size);
9348 : 4470 : se->expr = gfc_conv_array_data (se->expr);
9349 : 4470 : return;
9350 : : }
9351 : :
9352 : 49428 : if (fsym && fsym->ts.type == BT_CLASS)
9353 : : {
9354 : 1211 : gcc_assert (se->expr);
9355 : : ctree = se->expr;
9356 : : }
9357 : : else
9358 : : ctree = NULL_TREE;
9359 : :
9360 : 49428 : if (this_array_result)
9361 : : {
9362 : : /* Result of the enclosing function. */
9363 : 58 : gfc_conv_expr_descriptor (se, expr);
9364 : 58 : if (size)
9365 : 0 : array_parameter_size (&se->pre, se->expr, expr, size);
9366 : 58 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
9367 : :
9368 : 18 : if (g77 && TREE_TYPE (TREE_TYPE (se->expr)) != NULL_TREE
9369 : 76 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
9370 : 18 : se->expr = gfc_conv_array_data (build_fold_indirect_ref_loc (input_location,
9371 : : se->expr));
9372 : :
9373 : 58 : return;
9374 : : }
9375 : : else
9376 : : {
9377 : : /* Every other type of array. */
9378 : 49370 : se->want_pointer = (ctree) ? 0 : 1;
9379 : 49370 : se->want_coarray = expr->corank;
9380 : 49370 : gfc_conv_expr_descriptor (se, expr);
9381 : :
9382 : 49370 : if (size)
9383 : 30 : array_parameter_size (&se->pre,
9384 : : build_fold_indirect_ref_loc (input_location,
9385 : : se->expr),
9386 : : expr, size);
9387 : 49370 : if (ctree)
9388 : : {
9389 : 1211 : stmtblock_t block;
9390 : :
9391 : 1211 : gfc_init_block (&block);
9392 : 1211 : if (lbshift && *lbshift)
9393 : : {
9394 : : /* Apply a shift of the lbound when supplied. */
9395 : 98 : for (int dim = 0; dim < expr->rank; ++dim)
9396 : 49 : gfc_conv_shift_descriptor_lbound (&block, se->expr, dim,
9397 : : *lbshift);
9398 : : }
9399 : 1211 : tmp = gfc_class_data_get (ctree);
9400 : 1211 : if (expr->rank > 1 && CLASS_DATA (fsym)->as->rank != expr->rank
9401 : 84 : && CLASS_DATA (fsym)->as->type == AS_EXPLICIT && !no_pack)
9402 : : {
9403 : 36 : tree arr = gfc_create_var (TREE_TYPE (tmp), "parm");
9404 : 36 : gfc_conv_descriptor_data_set (&block, arr,
9405 : : gfc_conv_descriptor_data_get (
9406 : : se->expr));
9407 : 36 : gfc_conv_descriptor_lbound_set (&block, arr, gfc_index_zero_node,
9408 : : gfc_index_zero_node);
9409 : 36 : gfc_conv_descriptor_ubound_set (
9410 : : &block, arr, gfc_index_zero_node,
9411 : : gfc_conv_descriptor_size (se->expr, expr->rank));
9412 : 36 : gfc_conv_descriptor_stride_set (
9413 : : &block, arr, gfc_index_zero_node,
9414 : : gfc_conv_descriptor_stride_get (se->expr, gfc_index_zero_node));
9415 : 36 : gfc_add_modify (&block, gfc_conv_descriptor_dtype (arr),
9416 : : gfc_conv_descriptor_dtype (se->expr));
9417 : 36 : gfc_add_modify (&block, gfc_conv_descriptor_rank (arr),
9418 : : build_int_cst (signed_char_type_node, 1));
9419 : 36 : gfc_conv_descriptor_span_set (&block, arr,
9420 : : gfc_conv_descriptor_span_get (arr));
9421 : 36 : gfc_conv_descriptor_offset_set (&block, arr, gfc_index_zero_node);
9422 : 36 : se->expr = arr;
9423 : : }
9424 : 1211 : gfc_class_array_data_assign (&block, tmp, se->expr, true);
9425 : :
9426 : : /* Handle optional. */
9427 : 1211 : if (fsym && fsym->attr.optional && sym && sym->attr.optional)
9428 : 348 : tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
9429 : : gfc_finish_block (&block),
9430 : : build_empty_stmt (input_location));
9431 : : else
9432 : 863 : tmp = gfc_finish_block (&block);
9433 : :
9434 : 1211 : gfc_add_expr_to_block (&se->pre, tmp);
9435 : : }
9436 : : }
9437 : :
9438 : : /* Deallocate the allocatable components of structures that are
9439 : : not variable, for descriptorless arguments.
9440 : : Arguments with a descriptor are handled in gfc_conv_procedure_call. */
9441 : 49370 : if (g77 && (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
9442 : 75 : && expr->ts.u.derived->attr.alloc_comp
9443 : 21 : && expr->expr_type != EXPR_VARIABLE)
9444 : : {
9445 : 0 : tmp = build_fold_indirect_ref_loc (input_location, se->expr);
9446 : 0 : tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
9447 : :
9448 : : /* The components shall be deallocated before their containing entity. */
9449 : 0 : gfc_prepend_expr_to_block (&se->post, tmp);
9450 : : }
9451 : :
9452 : 47939 : if (g77 || (fsym && fsym->attr.contiguous
9453 : 1527 : && !gfc_is_simply_contiguous (expr, false, true)))
9454 : : {
9455 : 1575 : tree origptr = NULL_TREE, packedptr = NULL_TREE;
9456 : :
9457 : 1575 : desc = se->expr;
9458 : :
9459 : : /* For contiguous arrays, save the original value of the descriptor. */
9460 : 1575 : if (!g77 && !ctree)
9461 : : {
9462 : 48 : origptr = gfc_create_var (pvoid_type_node, "origptr");
9463 : 48 : tmp = build_fold_indirect_ref_loc (input_location, desc);
9464 : 48 : tmp = gfc_conv_array_data (tmp);
9465 : 96 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
9466 : 48 : TREE_TYPE (origptr), origptr,
9467 : 48 : fold_convert (TREE_TYPE (origptr), tmp));
9468 : 48 : gfc_add_expr_to_block (&se->pre, tmp);
9469 : : }
9470 : :
9471 : : /* Repack the array. */
9472 : 1575 : if (warn_array_temporaries)
9473 : : {
9474 : 28 : if (fsym)
9475 : 18 : gfc_warning (OPT_Warray_temporaries,
9476 : : "Creating array temporary at %L for argument %qs",
9477 : 18 : &expr->where, fsym->name);
9478 : : else
9479 : 10 : gfc_warning (OPT_Warray_temporaries,
9480 : : "Creating array temporary at %L", &expr->where);
9481 : : }
9482 : :
9483 : : /* When optimizing, we can use gfc_conv_subref_array_arg for
9484 : : making the packing and unpacking operation visible to the
9485 : : optimizers. */
9486 : :
9487 : 1431 : if (g77 && flag_inline_arg_packing && expr->expr_type == EXPR_VARIABLE
9488 : 733 : && !is_pointer (expr) && ! gfc_has_dimen_vector_ref (expr)
9489 : 349 : && !(expr->symtree->n.sym->as
9490 : 320 : && expr->symtree->n.sym->as->type == AS_ASSUMED_RANK)
9491 : 1924 : && (fsym == NULL || fsym->ts.type != BT_ASSUMED))
9492 : : {
9493 : 328 : gfc_conv_subref_array_arg (se, expr, g77,
9494 : 137 : fsym ? fsym->attr.intent : INTENT_INOUT,
9495 : : false, fsym, proc_name, sym, true);
9496 : 328 : return;
9497 : : }
9498 : :
9499 : 1247 : if (ctree)
9500 : : {
9501 : 96 : packedptr
9502 : 96 : = gfc_build_addr_expr (NULL_TREE, gfc_create_var (TREE_TYPE (ctree),
9503 : : "packed"));
9504 : 96 : if (fsym)
9505 : : {
9506 : 96 : int pack_mask = 0;
9507 : :
9508 : : /* Set bit 0 to the mask, when this is an unlimited_poly
9509 : : class. */
9510 : 96 : if (CLASS_DATA (fsym)->ts.u.derived->attr.unlimited_polymorphic)
9511 : 36 : pack_mask = 1 << 0;
9512 : 96 : pack_attr = build_int_cst (integer_type_node, pack_mask);
9513 : : }
9514 : : else
9515 : 0 : pack_attr = integer_zero_node;
9516 : :
9517 : 96 : gfc_add_expr_to_block (
9518 : : &se->pre,
9519 : : build_call_expr_loc (input_location, gfor_fndecl_in_pack_class, 4,
9520 : : packedptr,
9521 : : gfc_build_addr_expr (NULL_TREE, ctree),
9522 : 96 : size_in_bytes (TREE_TYPE (ctree)), pack_attr));
9523 : 96 : ptr = gfc_conv_array_data (gfc_class_data_get (packedptr));
9524 : 96 : se->expr = packedptr;
9525 : 96 : if (packed)
9526 : 96 : *packed = packedptr;
9527 : : }
9528 : : else
9529 : : {
9530 : 1151 : ptr = build_call_expr_loc (input_location, gfor_fndecl_in_pack, 1,
9531 : : desc);
9532 : :
9533 : 1151 : if (fsym && fsym->attr.optional && sym && sym->attr.optional)
9534 : : {
9535 : 11 : tmp = gfc_conv_expr_present (sym);
9536 : 22 : ptr = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
9537 : 11 : tmp, fold_convert (TREE_TYPE (se->expr), ptr),
9538 : 11 : fold_convert (TREE_TYPE (se->expr),
9539 : : null_pointer_node));
9540 : : }
9541 : :
9542 : 1151 : ptr = gfc_evaluate_now (ptr, &se->pre);
9543 : : }
9544 : :
9545 : : /* Use the packed data for the actual argument, except for contiguous arrays,
9546 : : where the descriptor's data component is set. */
9547 : 1247 : if (g77)
9548 : 1103 : se->expr = ptr;
9549 : : else
9550 : : {
9551 : 144 : tmp = build_fold_indirect_ref_loc (input_location, desc);
9552 : :
9553 : 144 : gfc_ss * ss = gfc_walk_expr (expr);
9554 : 288 : if (!transposed_dims (ss))
9555 : : {
9556 : 138 : if (!ctree)
9557 : 48 : gfc_conv_descriptor_data_set (&se->pre, tmp, ptr);
9558 : : }
9559 : 6 : else if (!ctree)
9560 : : {
9561 : 0 : tree old_field, new_field;
9562 : :
9563 : : /* The original descriptor has transposed dims so we can't reuse
9564 : : it directly; we have to create a new one. */
9565 : 0 : tree old_desc = tmp;
9566 : 0 : tree new_desc = gfc_create_var (TREE_TYPE (old_desc), "arg_desc");
9567 : :
9568 : 0 : old_field = gfc_conv_descriptor_dtype (old_desc);
9569 : 0 : new_field = gfc_conv_descriptor_dtype (new_desc);
9570 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9571 : :
9572 : 0 : old_field = gfc_conv_descriptor_offset_get (old_desc);
9573 : 0 : gfc_conv_descriptor_offset_set (&se->pre, new_desc, old_field);
9574 : :
9575 : 0 : for (int i = 0; i < expr->rank; i++)
9576 : : {
9577 : 0 : old_field = gfc_conv_descriptor_dimension (old_desc,
9578 : 0 : gfc_rank_cst[get_array_ref_dim_for_loop_dim (ss, i)]);
9579 : 0 : new_field = gfc_conv_descriptor_dimension (new_desc,
9580 : : gfc_rank_cst[i]);
9581 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9582 : : }
9583 : :
9584 : 0 : if (flag_coarray == GFC_FCOARRAY_LIB
9585 : 0 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (old_desc))
9586 : 0 : && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (old_desc))
9587 : : == GFC_ARRAY_ALLOCATABLE)
9588 : : {
9589 : 0 : old_field = gfc_conv_descriptor_token (old_desc);
9590 : 0 : new_field = gfc_conv_descriptor_token (new_desc);
9591 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9592 : : }
9593 : :
9594 : 0 : gfc_conv_descriptor_data_set (&se->pre, new_desc, ptr);
9595 : 0 : se->expr = gfc_build_addr_expr (NULL_TREE, new_desc);
9596 : : }
9597 : 144 : gfc_free_ss (ss);
9598 : : }
9599 : :
9600 : 1247 : if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
9601 : : {
9602 : 8 : char * msg;
9603 : :
9604 : 8 : if (fsym && proc_name)
9605 : 8 : msg = xasprintf ("An array temporary was created for argument "
9606 : 8 : "'%s' of procedure '%s'", fsym->name, proc_name);
9607 : : else
9608 : 0 : msg = xasprintf ("An array temporary was created");
9609 : :
9610 : 8 : tmp = build_fold_indirect_ref_loc (input_location,
9611 : : desc);
9612 : 8 : tmp = gfc_conv_array_data (tmp);
9613 : 8 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9614 : 8 : fold_convert (TREE_TYPE (tmp), ptr), tmp);
9615 : :
9616 : 8 : if (pass_optional)
9617 : 6 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9618 : : logical_type_node,
9619 : : gfc_conv_expr_present (sym), tmp);
9620 : :
9621 : 8 : gfc_trans_runtime_check (false, true, tmp, &se->pre,
9622 : : &expr->where, msg);
9623 : 8 : free (msg);
9624 : : }
9625 : :
9626 : 1247 : gfc_start_block (&block);
9627 : :
9628 : : /* Copy the data back. If input expr is read-only, e.g. a PARAMETER
9629 : : array, copying back modified values is undefined behavior. */
9630 : 2494 : readonly = (expr->expr_type == EXPR_VARIABLE
9631 : 854 : && expr->symtree
9632 : 2101 : && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
9633 : :
9634 : 1247 : if ((fsym == NULL || fsym->attr.intent != INTENT_IN) && !readonly)
9635 : : {
9636 : 1114 : if (ctree)
9637 : : {
9638 : 66 : tmp = gfc_build_addr_expr (NULL_TREE, ctree);
9639 : 66 : tmp = build_call_expr_loc (input_location,
9640 : : gfor_fndecl_in_unpack_class, 4, tmp,
9641 : : packedptr,
9642 : 66 : size_in_bytes (TREE_TYPE (ctree)),
9643 : : pack_attr);
9644 : : }
9645 : : else
9646 : 1048 : tmp = build_call_expr_loc (input_location, gfor_fndecl_in_unpack, 2,
9647 : : desc, ptr);
9648 : 1114 : gfc_add_expr_to_block (&block, tmp);
9649 : : }
9650 : 133 : else if (ctree && fsym->attr.intent == INTENT_IN)
9651 : : {
9652 : : /* Need to free the memory for class arrays, that got packed. */
9653 : 30 : gfc_add_expr_to_block (&block, gfc_call_free (ptr));
9654 : : }
9655 : :
9656 : : /* Free the temporary. */
9657 : 1144 : if (!ctree)
9658 : 1151 : gfc_add_expr_to_block (&block, gfc_call_free (ptr));
9659 : :
9660 : 1247 : stmt = gfc_finish_block (&block);
9661 : :
9662 : 1247 : gfc_init_block (&block);
9663 : : /* Only if it was repacked. This code needs to be executed before the
9664 : : loop cleanup code. */
9665 : 1247 : tmp = (ctree) ? desc : build_fold_indirect_ref_loc (input_location, desc);
9666 : 1247 : tmp = gfc_conv_array_data (tmp);
9667 : 1247 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9668 : 1247 : fold_convert (TREE_TYPE (tmp), ptr), tmp);
9669 : :
9670 : 1247 : if (pass_optional)
9671 : 11 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9672 : : logical_type_node,
9673 : : gfc_conv_expr_present (sym), tmp);
9674 : :
9675 : 1247 : tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
9676 : :
9677 : 1247 : gfc_add_expr_to_block (&block, tmp);
9678 : 1247 : gfc_add_block_to_block (&block, &se->post);
9679 : :
9680 : 1247 : gfc_init_block (&se->post);
9681 : :
9682 : : /* Reset the descriptor pointer. */
9683 : 1247 : if (!g77 && !ctree)
9684 : : {
9685 : 48 : tmp = build_fold_indirect_ref_loc (input_location, desc);
9686 : 48 : gfc_conv_descriptor_data_set (&se->post, tmp, origptr);
9687 : : }
9688 : :
9689 : 1247 : gfc_add_block_to_block (&se->post, &block);
9690 : : }
9691 : : }
9692 : :
9693 : :
9694 : : /* This helper function calculates the size in words of a full array. */
9695 : :
9696 : : tree
9697 : 15581 : gfc_full_array_size (stmtblock_t *block, tree decl, int rank)
9698 : : {
9699 : 15581 : tree idx;
9700 : 15581 : tree nelems;
9701 : 15581 : tree tmp;
9702 : 15581 : if (rank < 0)
9703 : 0 : idx = gfc_conv_descriptor_rank (decl);
9704 : : else
9705 : 15581 : idx = gfc_rank_cst[rank - 1];
9706 : 15581 : nelems = gfc_conv_descriptor_ubound_get (decl, idx);
9707 : 15581 : tmp = gfc_conv_descriptor_lbound_get (decl, idx);
9708 : 15581 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9709 : : nelems, tmp);
9710 : 15581 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
9711 : : tmp, gfc_index_one_node);
9712 : 15581 : tmp = gfc_evaluate_now (tmp, block);
9713 : :
9714 : 15581 : nelems = gfc_conv_descriptor_stride_get (decl, idx);
9715 : 15581 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9716 : : nelems, tmp);
9717 : 15581 : return gfc_evaluate_now (tmp, block);
9718 : : }
9719 : :
9720 : :
9721 : : /* Allocate dest to the same size as src, and copy src -> dest.
9722 : : If no_malloc is set, only the copy is done. */
9723 : :
9724 : : static tree
9725 : 6744 : duplicate_allocatable (tree dest, tree src, tree type, int rank,
9726 : : bool no_malloc, bool no_memcpy, tree str_sz,
9727 : : tree add_when_allocated)
9728 : : {
9729 : 6744 : tree tmp;
9730 : 6744 : tree eltype;
9731 : 6744 : tree size;
9732 : 6744 : tree nelems;
9733 : 6744 : tree null_cond;
9734 : 6744 : tree null_data;
9735 : 6744 : stmtblock_t block;
9736 : :
9737 : : /* If the source is null, set the destination to null. Then,
9738 : : allocate memory to the destination. */
9739 : 6744 : gfc_init_block (&block);
9740 : :
9741 : 6744 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
9742 : : {
9743 : 1968 : gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
9744 : 1968 : null_data = gfc_finish_block (&block);
9745 : :
9746 : 1968 : gfc_init_block (&block);
9747 : 1968 : eltype = TREE_TYPE (type);
9748 : 1968 : if (str_sz != NULL_TREE)
9749 : : size = str_sz;
9750 : : else
9751 : 1673 : size = TYPE_SIZE_UNIT (eltype);
9752 : :
9753 : 1968 : if (!no_malloc)
9754 : : {
9755 : 1968 : tmp = gfc_call_malloc (&block, type, size);
9756 : 1968 : gfc_add_modify (&block, dest, fold_convert (type, tmp));
9757 : : }
9758 : :
9759 : 1968 : if (!no_memcpy)
9760 : : {
9761 : 1543 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9762 : 1543 : tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
9763 : : fold_convert (size_type_node, size));
9764 : 1543 : gfc_add_expr_to_block (&block, tmp);
9765 : : }
9766 : : }
9767 : : else
9768 : : {
9769 : 4776 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9770 : 4776 : null_data = gfc_finish_block (&block);
9771 : :
9772 : 4776 : gfc_init_block (&block);
9773 : 4776 : if (rank)
9774 : 4765 : nelems = gfc_full_array_size (&block, src, rank);
9775 : : else
9776 : 11 : nelems = gfc_index_one_node;
9777 : :
9778 : : /* If type is not the array type, then it is the element type. */
9779 : 4776 : if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
9780 : 4746 : eltype = gfc_get_element_type (type);
9781 : : else
9782 : : eltype = type;
9783 : :
9784 : 4776 : if (str_sz != NULL_TREE)
9785 : 43 : tmp = fold_convert (gfc_array_index_type, str_sz);
9786 : : else
9787 : 4733 : tmp = fold_convert (gfc_array_index_type,
9788 : : TYPE_SIZE_UNIT (eltype));
9789 : :
9790 : 4776 : tmp = gfc_evaluate_now (tmp, &block);
9791 : 4776 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9792 : : nelems, tmp);
9793 : 4776 : if (!no_malloc)
9794 : : {
9795 : 4725 : tmp = TREE_TYPE (gfc_conv_descriptor_data_get (src));
9796 : 4725 : tmp = gfc_call_malloc (&block, tmp, size);
9797 : 4725 : gfc_conv_descriptor_data_set (&block, dest, tmp);
9798 : : }
9799 : :
9800 : : /* We know the temporary and the value will be the same length,
9801 : : so can use memcpy. */
9802 : 4776 : if (!no_memcpy)
9803 : : {
9804 : 4346 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9805 : 4346 : tmp = build_call_expr_loc (input_location, tmp, 3,
9806 : : gfc_conv_descriptor_data_get (dest),
9807 : : gfc_conv_descriptor_data_get (src),
9808 : : fold_convert (size_type_node, size));
9809 : 4346 : gfc_add_expr_to_block (&block, tmp);
9810 : : }
9811 : : }
9812 : :
9813 : 6744 : gfc_add_expr_to_block (&block, add_when_allocated);
9814 : 6744 : tmp = gfc_finish_block (&block);
9815 : :
9816 : : /* Null the destination if the source is null; otherwise do
9817 : : the allocate and copy. */
9818 : 6744 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
9819 : : null_cond = src;
9820 : : else
9821 : 4776 : null_cond = gfc_conv_descriptor_data_get (src);
9822 : :
9823 : 6744 : null_cond = convert (pvoid_type_node, null_cond);
9824 : 6744 : null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9825 : : null_cond, null_pointer_node);
9826 : 6744 : return build3_v (COND_EXPR, null_cond, tmp, null_data);
9827 : : }
9828 : :
9829 : :
9830 : : /* Allocate dest to the same size as src, and copy data src -> dest. */
9831 : :
9832 : : tree
9833 : 5414 : gfc_duplicate_allocatable (tree dest, tree src, tree type, int rank,
9834 : : tree add_when_allocated)
9835 : : {
9836 : 5414 : return duplicate_allocatable (dest, src, type, rank, false, false,
9837 : 5414 : NULL_TREE, add_when_allocated);
9838 : : }
9839 : :
9840 : :
9841 : : /* Copy data src -> dest. */
9842 : :
9843 : : tree
9844 : 51 : gfc_copy_allocatable_data (tree dest, tree src, tree type, int rank)
9845 : : {
9846 : 51 : return duplicate_allocatable (dest, src, type, rank, true, false,
9847 : 51 : NULL_TREE, NULL_TREE);
9848 : : }
9849 : :
9850 : : /* Allocate dest to the same size as src, but don't copy anything. */
9851 : :
9852 : : tree
9853 : 855 : gfc_duplicate_allocatable_nocopy (tree dest, tree src, tree type, int rank)
9854 : : {
9855 : 855 : return duplicate_allocatable (dest, src, type, rank, false, true,
9856 : 855 : NULL_TREE, NULL_TREE);
9857 : : }
9858 : :
9859 : : static tree
9860 : 55 : duplicate_allocatable_coarray (tree dest, tree dest_tok, tree src, tree type,
9861 : : int rank, tree add_when_allocated)
9862 : : {
9863 : 55 : tree tmp;
9864 : 55 : tree size;
9865 : 55 : tree nelems;
9866 : 55 : tree null_cond;
9867 : 55 : tree null_data;
9868 : 55 : stmtblock_t block, globalblock;
9869 : :
9870 : : /* If the source is null, set the destination to null. Then,
9871 : : allocate memory to the destination. */
9872 : 55 : gfc_init_block (&block);
9873 : 55 : gfc_init_block (&globalblock);
9874 : :
9875 : 55 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
9876 : : {
9877 : 15 : gfc_se se;
9878 : 15 : symbol_attribute attr;
9879 : 15 : tree dummy_desc;
9880 : :
9881 : 15 : gfc_init_se (&se, NULL);
9882 : 15 : gfc_clear_attr (&attr);
9883 : 15 : attr.allocatable = 1;
9884 : 15 : dummy_desc = gfc_conv_scalar_to_descriptor (&se, dest, attr);
9885 : 15 : gfc_add_block_to_block (&globalblock, &se.pre);
9886 : 15 : size = TYPE_SIZE_UNIT (TREE_TYPE (type));
9887 : :
9888 : 15 : gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
9889 : 15 : gfc_allocate_using_caf_lib (&block, dummy_desc, size,
9890 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9891 : : NULL_TREE, NULL_TREE, NULL_TREE,
9892 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
9893 : 15 : gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
9894 : 15 : null_data = gfc_finish_block (&block);
9895 : :
9896 : 15 : gfc_init_block (&block);
9897 : :
9898 : 15 : gfc_allocate_using_caf_lib (&block, dummy_desc,
9899 : : fold_convert (size_type_node, size),
9900 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9901 : : NULL_TREE, NULL_TREE, NULL_TREE,
9902 : : GFC_CAF_COARRAY_ALLOC);
9903 : 15 : gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
9904 : :
9905 : 15 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9906 : 15 : tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
9907 : : fold_convert (size_type_node, size));
9908 : 15 : gfc_add_expr_to_block (&block, tmp);
9909 : : }
9910 : : else
9911 : : {
9912 : : /* Set the rank or unitialized memory access may be reported. */
9913 : 40 : tmp = gfc_conv_descriptor_rank (dest);
9914 : 40 : gfc_add_modify (&globalblock, tmp, build_int_cst (TREE_TYPE (tmp), rank));
9915 : :
9916 : 40 : if (rank)
9917 : 40 : nelems = gfc_full_array_size (&globalblock, src, rank);
9918 : : else
9919 : 0 : nelems = integer_one_node;
9920 : :
9921 : 40 : tmp = fold_convert (size_type_node,
9922 : : TYPE_SIZE_UNIT (gfc_get_element_type (type)));
9923 : 40 : size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
9924 : : fold_convert (size_type_node, nelems), tmp);
9925 : :
9926 : 40 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9927 : 40 : gfc_allocate_using_caf_lib (&block, dest, fold_convert (size_type_node,
9928 : : size),
9929 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9930 : : NULL_TREE, NULL_TREE, NULL_TREE,
9931 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
9932 : 40 : null_data = gfc_finish_block (&block);
9933 : :
9934 : 40 : gfc_init_block (&block);
9935 : 40 : gfc_allocate_using_caf_lib (&block, dest,
9936 : : fold_convert (size_type_node, size),
9937 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9938 : : NULL_TREE, NULL_TREE, NULL_TREE,
9939 : : GFC_CAF_COARRAY_ALLOC);
9940 : :
9941 : 40 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9942 : 40 : tmp = build_call_expr_loc (input_location, tmp, 3,
9943 : : gfc_conv_descriptor_data_get (dest),
9944 : : gfc_conv_descriptor_data_get (src),
9945 : : fold_convert (size_type_node, size));
9946 : 40 : gfc_add_expr_to_block (&block, tmp);
9947 : : }
9948 : 55 : gfc_add_expr_to_block (&block, add_when_allocated);
9949 : 55 : tmp = gfc_finish_block (&block);
9950 : :
9951 : : /* Null the destination if the source is null; otherwise do
9952 : : the register and copy. */
9953 : 55 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
9954 : : null_cond = src;
9955 : : else
9956 : 40 : null_cond = gfc_conv_descriptor_data_get (src);
9957 : :
9958 : 55 : null_cond = convert (pvoid_type_node, null_cond);
9959 : 55 : null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9960 : : null_cond, null_pointer_node);
9961 : 55 : gfc_add_expr_to_block (&globalblock, build3_v (COND_EXPR, null_cond, tmp,
9962 : : null_data));
9963 : 55 : return gfc_finish_block (&globalblock);
9964 : : }
9965 : :
9966 : :
9967 : : /* Helper function to abstract whether coarray processing is enabled. */
9968 : :
9969 : : static bool
9970 : 73 : caf_enabled (int caf_mode)
9971 : : {
9972 : 73 : return (caf_mode & GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY)
9973 : 73 : == GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY;
9974 : : }
9975 : :
9976 : :
9977 : : /* Helper function to abstract whether coarray processing is enabled
9978 : : and we are in a derived type coarray. */
9979 : :
9980 : : static bool
9981 : 8083 : caf_in_coarray (int caf_mode)
9982 : : {
9983 : 8083 : static const int pat = GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
9984 : : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY;
9985 : 8083 : return (caf_mode & pat) == pat;
9986 : : }
9987 : :
9988 : :
9989 : : /* Helper function to abstract whether coarray is to deallocate only. */
9990 : :
9991 : : bool
9992 : 311 : gfc_caf_is_dealloc_only (int caf_mode)
9993 : : {
9994 : 311 : return (caf_mode & GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY)
9995 : 311 : == GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY;
9996 : : }
9997 : :
9998 : :
9999 : : /* Recursively traverse an object of derived type, generating code to
10000 : : deallocate, nullify or copy allocatable components. This is the work horse
10001 : : function for the functions named in this enum. */
10002 : :
10003 : : enum {DEALLOCATE_ALLOC_COMP = 1, NULLIFY_ALLOC_COMP,
10004 : : COPY_ALLOC_COMP, COPY_ONLY_ALLOC_COMP, REASSIGN_CAF_COMP,
10005 : : ALLOCATE_PDT_COMP, DEALLOCATE_PDT_COMP, CHECK_PDT_DUMMY,
10006 : : BCAST_ALLOC_COMP};
10007 : :
10008 : : static gfc_actual_arglist *pdt_param_list;
10009 : :
10010 : : static tree
10011 : 16429 : structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest,
10012 : : int rank, int purpose, int caf_mode,
10013 : : gfc_co_subroutines_args *args,
10014 : : bool no_finalization = false)
10015 : : {
10016 : 16429 : gfc_component *c;
10017 : 16429 : gfc_loopinfo loop;
10018 : 16429 : stmtblock_t fnblock;
10019 : 16429 : stmtblock_t loopbody;
10020 : 16429 : stmtblock_t tmpblock;
10021 : 16429 : tree decl_type;
10022 : 16429 : tree tmp;
10023 : 16429 : tree comp;
10024 : 16429 : tree dcmp;
10025 : 16429 : tree nelems;
10026 : 16429 : tree index;
10027 : 16429 : tree var;
10028 : 16429 : tree cdecl;
10029 : 16429 : tree ctype;
10030 : 16429 : tree vref, dref;
10031 : 16429 : tree null_cond = NULL_TREE;
10032 : 16429 : tree add_when_allocated;
10033 : 16429 : tree dealloc_fndecl;
10034 : 16429 : tree caf_token;
10035 : 16429 : gfc_symbol *vtab;
10036 : 16429 : int caf_dereg_mode;
10037 : 16429 : symbol_attribute *attr;
10038 : 16429 : bool deallocate_called;
10039 : 16429 : static hash_set<gfc_symbol *> seen_derived_types;
10040 : :
10041 : 16429 : gfc_init_block (&fnblock);
10042 : :
10043 : 16429 : decl_type = TREE_TYPE (decl);
10044 : :
10045 : 16429 : if ((POINTER_TYPE_P (decl_type))
10046 : : || (TREE_CODE (decl_type) == REFERENCE_TYPE && rank == 0))
10047 : : {
10048 : 1259 : decl = build_fold_indirect_ref_loc (input_location, decl);
10049 : : /* Deref dest in sync with decl, but only when it is not NULL. */
10050 : 1259 : if (dest)
10051 : 91 : dest = build_fold_indirect_ref_loc (input_location, dest);
10052 : :
10053 : : /* Update the decl_type because it got dereferenced. */
10054 : 1259 : decl_type = TREE_TYPE (decl);
10055 : : }
10056 : :
10057 : : /* If this is an array of derived types with allocatable components
10058 : : build a loop and recursively call this function. */
10059 : 16429 : if (TREE_CODE (decl_type) == ARRAY_TYPE
10060 : 16429 : || (GFC_DESCRIPTOR_TYPE_P (decl_type) && rank != 0))
10061 : : {
10062 : 2675 : tmp = gfc_conv_array_data (decl);
10063 : 2675 : var = build_fold_indirect_ref_loc (input_location, tmp);
10064 : :
10065 : : /* Get the number of elements - 1 and set the counter. */
10066 : 2675 : if (GFC_DESCRIPTOR_TYPE_P (decl_type))
10067 : : {
10068 : : /* Use the descriptor for an allocatable array. Since this
10069 : : is a full array reference, we only need the descriptor
10070 : : information from dimension = rank. */
10071 : 1853 : tmp = gfc_full_array_size (&fnblock, decl, rank);
10072 : 1853 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
10073 : : gfc_array_index_type, tmp,
10074 : : gfc_index_one_node);
10075 : :
10076 : 1853 : null_cond = gfc_conv_descriptor_data_get (decl);
10077 : 1853 : null_cond = fold_build2_loc (input_location, NE_EXPR,
10078 : : logical_type_node, null_cond,
10079 : 1853 : build_int_cst (TREE_TYPE (null_cond), 0));
10080 : : }
10081 : : else
10082 : : {
10083 : : /* Otherwise use the TYPE_DOMAIN information. */
10084 : 822 : tmp = array_type_nelts_minus_one (decl_type);
10085 : 822 : tmp = fold_convert (gfc_array_index_type, tmp);
10086 : : }
10087 : :
10088 : : /* Remember that this is, in fact, the no. of elements - 1. */
10089 : 2675 : nelems = gfc_evaluate_now (tmp, &fnblock);
10090 : 2675 : index = gfc_create_var (gfc_array_index_type, "S");
10091 : :
10092 : : /* Build the body of the loop. */
10093 : 2675 : gfc_init_block (&loopbody);
10094 : :
10095 : 2675 : vref = gfc_build_array_ref (var, index, NULL);
10096 : :
10097 : 2675 : if (purpose == COPY_ALLOC_COMP || purpose == COPY_ONLY_ALLOC_COMP)
10098 : : {
10099 : 491 : tmp = build_fold_indirect_ref_loc (input_location,
10100 : : gfc_conv_array_data (dest));
10101 : 491 : dref = gfc_build_array_ref (tmp, index, NULL);
10102 : 491 : tmp = structure_alloc_comps (der_type, vref, dref, rank,
10103 : : COPY_ALLOC_COMP, caf_mode, args,
10104 : : no_finalization);
10105 : : }
10106 : : else
10107 : 2184 : tmp = structure_alloc_comps (der_type, vref, NULL_TREE, rank, purpose,
10108 : : caf_mode, args, no_finalization);
10109 : :
10110 : 2675 : gfc_add_expr_to_block (&loopbody, tmp);
10111 : :
10112 : : /* Build the loop and return. */
10113 : 2675 : gfc_init_loopinfo (&loop);
10114 : 2675 : loop.dimen = 1;
10115 : 2675 : loop.from[0] = gfc_index_zero_node;
10116 : 2675 : loop.loopvar[0] = index;
10117 : 2675 : loop.to[0] = nelems;
10118 : 2675 : gfc_trans_scalarizing_loops (&loop, &loopbody);
10119 : 2675 : gfc_add_block_to_block (&fnblock, &loop.pre);
10120 : :
10121 : 2675 : tmp = gfc_finish_block (&fnblock);
10122 : : /* When copying allocateable components, the above implements the
10123 : : deep copy. Nevertheless is a deep copy only allowed, when the current
10124 : : component is allocated, for which code will be generated in
10125 : : gfc_duplicate_allocatable (), where the deep copy code is just added
10126 : : into the if's body, by adding tmp (the deep copy code) as last
10127 : : argument to gfc_duplicate_allocatable (). */
10128 : 2675 : if (purpose == COPY_ALLOC_COMP && caf_mode == 0
10129 : 2675 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
10130 : 414 : tmp = gfc_duplicate_allocatable (dest, decl, decl_type, rank,
10131 : : tmp);
10132 : 2261 : else if (null_cond != NULL_TREE)
10133 : 1439 : tmp = build3_v (COND_EXPR, null_cond, tmp,
10134 : : build_empty_stmt (input_location));
10135 : :
10136 : 2675 : return tmp;
10137 : : }
10138 : :
10139 : 13754 : if (purpose == DEALLOCATE_ALLOC_COMP && der_type->attr.pdt_type)
10140 : : {
10141 : 23 : tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10142 : : DEALLOCATE_PDT_COMP, 0, args,
10143 : : no_finalization);
10144 : 23 : gfc_add_expr_to_block (&fnblock, tmp);
10145 : : }
10146 : 13731 : else if (purpose == ALLOCATE_PDT_COMP && der_type->attr.alloc_comp)
10147 : : {
10148 : 2 : tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10149 : : NULLIFY_ALLOC_COMP, 0, args,
10150 : : no_finalization);
10151 : 2 : gfc_add_expr_to_block (&fnblock, tmp);
10152 : : }
10153 : :
10154 : : /* Still having a descriptor array of rank == 0 here, indicates an
10155 : : allocatable coarrays. Dereference it correctly. */
10156 : 13754 : if (GFC_DESCRIPTOR_TYPE_P (decl_type))
10157 : : {
10158 : 4 : decl = build_fold_indirect_ref (gfc_conv_array_data (decl));
10159 : : }
10160 : : /* Otherwise, act on the components or recursively call self to
10161 : : act on a chain of components. */
10162 : 13754 : seen_derived_types.add (der_type);
10163 : 38271 : for (c = der_type->components; c; c = c->next)
10164 : : {
10165 : 24517 : bool cmp_has_alloc_comps = (c->ts.type == BT_DERIVED
10166 : 24517 : || c->ts.type == BT_CLASS)
10167 : 24517 : && c->ts.u.derived->attr.alloc_comp;
10168 : 24517 : bool same_type
10169 : : = (c->ts.type == BT_DERIVED
10170 : 4663 : && seen_derived_types.contains (c->ts.u.derived))
10171 : 28558 : || (c->ts.type == BT_CLASS
10172 : 2184 : && seen_derived_types.contains (CLASS_DATA (c)->ts.u.derived));
10173 : :
10174 : 49034 : bool is_pdt_type = c->ts.type == BT_DERIVED
10175 : 24517 : && c->ts.u.derived->attr.pdt_type;
10176 : :
10177 : 24517 : cdecl = c->backend_decl;
10178 : 24517 : ctype = TREE_TYPE (cdecl);
10179 : :
10180 : 24517 : switch (purpose)
10181 : : {
10182 : :
10183 : 3 : case BCAST_ALLOC_COMP:
10184 : :
10185 : 3 : tree ubound;
10186 : 3 : tree cdesc;
10187 : 3 : stmtblock_t derived_type_block;
10188 : :
10189 : 3 : gfc_init_block (&tmpblock);
10190 : :
10191 : 3 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10192 : : decl, cdecl, NULL_TREE);
10193 : :
10194 : : /* Shortcut to get the attributes of the component. */
10195 : 3 : if (c->ts.type == BT_CLASS)
10196 : : {
10197 : 0 : attr = &CLASS_DATA (c)->attr;
10198 : 0 : if (attr->class_pointer)
10199 : 0 : continue;
10200 : : }
10201 : : else
10202 : : {
10203 : 3 : attr = &c->attr;
10204 : 3 : if (attr->pointer)
10205 : 0 : continue;
10206 : : }
10207 : :
10208 : : /* Do not broadcast a caf_token. These are local to the image. */
10209 : 3 : if (attr->caf_token)
10210 : 1 : continue;
10211 : :
10212 : 2 : add_when_allocated = NULL_TREE;
10213 : 2 : if (cmp_has_alloc_comps
10214 : 0 : && !c->attr.pointer && !c->attr.proc_pointer)
10215 : : {
10216 : 0 : if (c->ts.type == BT_CLASS)
10217 : : {
10218 : 0 : rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
10219 : 0 : add_when_allocated
10220 : 0 : = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
10221 : : comp, NULL_TREE, rank, purpose,
10222 : : caf_mode, args, no_finalization);
10223 : : }
10224 : : else
10225 : : {
10226 : 0 : rank = c->as ? c->as->rank : 0;
10227 : 0 : add_when_allocated = structure_alloc_comps (c->ts.u.derived,
10228 : : comp, NULL_TREE,
10229 : : rank, purpose,
10230 : : caf_mode, args,
10231 : : no_finalization);
10232 : : }
10233 : : }
10234 : :
10235 : 2 : gfc_init_block (&derived_type_block);
10236 : 2 : if (add_when_allocated)
10237 : 0 : gfc_add_expr_to_block (&derived_type_block, add_when_allocated);
10238 : 2 : tmp = gfc_finish_block (&derived_type_block);
10239 : 2 : gfc_add_expr_to_block (&tmpblock, tmp);
10240 : :
10241 : : /* Convert the component into a rank 1 descriptor type. */
10242 : 2 : if (attr->dimension)
10243 : : {
10244 : 0 : tmp = gfc_get_element_type (TREE_TYPE (comp));
10245 : 0 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
10246 : 0 : ubound = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (comp));
10247 : : else
10248 : 0 : ubound = gfc_full_array_size (&tmpblock, comp,
10249 : 0 : c->ts.type == BT_CLASS
10250 : 0 : ? CLASS_DATA (c)->as->rank
10251 : 0 : : c->as->rank);
10252 : : }
10253 : : else
10254 : : {
10255 : 2 : tmp = TREE_TYPE (comp);
10256 : 2 : ubound = build_int_cst (gfc_array_index_type, 1);
10257 : : }
10258 : :
10259 : : /* Treat strings like arrays. Or the other way around, do not
10260 : : * generate an additional array layer for scalar components. */
10261 : 2 : if (attr->dimension || c->ts.type == BT_CHARACTER)
10262 : : {
10263 : 0 : cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
10264 : : &ubound, 1,
10265 : : GFC_ARRAY_ALLOCATABLE, false);
10266 : :
10267 : 0 : cdesc = gfc_create_var (cdesc, "cdesc");
10268 : 0 : DECL_ARTIFICIAL (cdesc) = 1;
10269 : :
10270 : 0 : gfc_add_modify (&tmpblock, gfc_conv_descriptor_dtype (cdesc),
10271 : : gfc_get_dtype_rank_type (1, tmp));
10272 : 0 : gfc_conv_descriptor_lbound_set (&tmpblock, cdesc,
10273 : : gfc_index_zero_node,
10274 : : gfc_index_one_node);
10275 : 0 : gfc_conv_descriptor_stride_set (&tmpblock, cdesc,
10276 : : gfc_index_zero_node,
10277 : : gfc_index_one_node);
10278 : 0 : gfc_conv_descriptor_ubound_set (&tmpblock, cdesc,
10279 : : gfc_index_zero_node, ubound);
10280 : : }
10281 : : else
10282 : : /* Prevent warning. */
10283 : : cdesc = NULL_TREE;
10284 : :
10285 : 2 : if (attr->dimension)
10286 : : {
10287 : 0 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
10288 : 0 : comp = gfc_conv_descriptor_data_get (comp);
10289 : : else
10290 : 0 : comp = gfc_build_addr_expr (NULL_TREE, comp);
10291 : : }
10292 : : else
10293 : : {
10294 : 2 : gfc_se se;
10295 : :
10296 : 2 : gfc_init_se (&se, NULL);
10297 : :
10298 : 2 : comp = gfc_conv_scalar_to_descriptor (&se, comp,
10299 : 2 : c->ts.type == BT_CLASS
10300 : 2 : ? CLASS_DATA (c)->attr
10301 : : : c->attr);
10302 : 2 : if (c->ts.type == BT_CHARACTER)
10303 : 0 : comp = gfc_build_addr_expr (NULL_TREE, comp);
10304 : 2 : gfc_add_block_to_block (&tmpblock, &se.pre);
10305 : : }
10306 : :
10307 : 2 : if (attr->dimension || c->ts.type == BT_CHARACTER)
10308 : 0 : gfc_conv_descriptor_data_set (&tmpblock, cdesc, comp);
10309 : : else
10310 : 2 : cdesc = comp;
10311 : :
10312 : 2 : tree fndecl;
10313 : :
10314 : 2 : fndecl = build_call_expr_loc (input_location,
10315 : : gfor_fndecl_co_broadcast, 5,
10316 : : gfc_build_addr_expr (pvoid_type_node,cdesc),
10317 : : args->image_index,
10318 : : null_pointer_node, null_pointer_node,
10319 : : null_pointer_node);
10320 : :
10321 : 2 : gfc_add_expr_to_block (&tmpblock, fndecl);
10322 : 2 : gfc_add_block_to_block (&fnblock, &tmpblock);
10323 : :
10324 : 20237 : break;
10325 : :
10326 : 9756 : case DEALLOCATE_ALLOC_COMP:
10327 : :
10328 : 9756 : gfc_init_block (&tmpblock);
10329 : :
10330 : 9756 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10331 : : decl, cdecl, NULL_TREE);
10332 : :
10333 : : /* Shortcut to get the attributes of the component. */
10334 : 9756 : if (c->ts.type == BT_CLASS)
10335 : : {
10336 : 1008 : attr = &CLASS_DATA (c)->attr;
10337 : 1008 : if (attr->class_pointer || c->attr.proc_pointer)
10338 : 18 : continue;
10339 : : }
10340 : : else
10341 : : {
10342 : 8748 : attr = &c->attr;
10343 : 8748 : if (attr->pointer || attr->proc_pointer)
10344 : 130 : continue;
10345 : : }
10346 : :
10347 : 9608 : if (!no_finalization && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
10348 : 7460 : || (c->ts.type == BT_CLASS && !CLASS_DATA (c)->attr.class_pointer)))
10349 : : /* Call the finalizer, which will free the memory and nullify the
10350 : : pointer of an array. */
10351 : 2756 : deallocate_called = gfc_add_comp_finalizer_call (&tmpblock, comp, c,
10352 : 2756 : caf_enabled (caf_mode))
10353 : 2756 : && attr->dimension;
10354 : : else
10355 : : deallocate_called = false;
10356 : :
10357 : : /* Add the _class ref for classes. */
10358 : 9608 : if (c->ts.type == BT_CLASS && attr->allocatable)
10359 : 990 : comp = gfc_class_data_get (comp);
10360 : :
10361 : 9608 : add_when_allocated = NULL_TREE;
10362 : 9608 : if (cmp_has_alloc_comps
10363 : 1905 : && !c->attr.pointer && !c->attr.proc_pointer
10364 : : && !same_type
10365 : 1905 : && !deallocate_called)
10366 : : {
10367 : : /* Add checked deallocation of the components. This code is
10368 : : obviously added because the finalizer is not trusted to free
10369 : : all memory. */
10370 : 1265 : if (c->ts.type == BT_CLASS)
10371 : : {
10372 : 241 : rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
10373 : 241 : add_when_allocated
10374 : 241 : = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
10375 : : comp, NULL_TREE, rank, purpose,
10376 : : caf_mode, args, no_finalization);
10377 : : }
10378 : : else
10379 : : {
10380 : 1024 : rank = c->as ? c->as->rank : 0;
10381 : 1024 : add_when_allocated = structure_alloc_comps (c->ts.u.derived,
10382 : : comp, NULL_TREE,
10383 : : rank, purpose,
10384 : : caf_mode, args,
10385 : : no_finalization);
10386 : : }
10387 : : }
10388 : :
10389 : 6674 : if (attr->allocatable && !same_type
10390 : 15822 : && (!attr->codimension || caf_enabled (caf_mode)))
10391 : : {
10392 : : /* Handle all types of components besides components of the
10393 : : same_type as the current one, because those would create an
10394 : : endless loop. */
10395 : 45 : caf_dereg_mode = (caf_in_coarray (caf_mode)
10396 : 51 : && (attr->dimension || c->caf_token))
10397 : 6158 : || attr->codimension
10398 : 6279 : ? (gfc_caf_is_dealloc_only (caf_mode)
10399 : : ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
10400 : : : GFC_CAF_COARRAY_DEREGISTER)
10401 : : : GFC_CAF_COARRAY_NOCOARRAY;
10402 : :
10403 : 6208 : caf_token = NULL_TREE;
10404 : : /* Coarray components are handled directly by
10405 : : deallocate_with_status. */
10406 : 6208 : if (!attr->codimension
10407 : 6187 : && caf_dereg_mode != GFC_CAF_COARRAY_NOCOARRAY)
10408 : : {
10409 : 50 : if (c->caf_token)
10410 : 16 : caf_token
10411 : 16 : = fold_build3_loc (input_location, COMPONENT_REF,
10412 : 16 : TREE_TYPE (gfc_comp_caf_token (c)),
10413 : : decl, gfc_comp_caf_token (c),
10414 : : NULL_TREE);
10415 : 34 : else if (attr->dimension && !attr->proc_pointer)
10416 : 34 : caf_token = gfc_conv_descriptor_token (comp);
10417 : : }
10418 : :
10419 : 6208 : tmp = gfc_deallocate_with_status (comp, NULL_TREE, NULL_TREE,
10420 : : NULL_TREE, NULL_TREE, true,
10421 : : NULL, caf_dereg_mode, NULL_TREE,
10422 : : add_when_allocated, caf_token);
10423 : :
10424 : 6208 : gfc_add_expr_to_block (&tmpblock, tmp);
10425 : : }
10426 : 3400 : else if (attr->allocatable && !attr->codimension
10427 : 460 : && !deallocate_called)
10428 : : {
10429 : : /* Case of recursive allocatable derived types. */
10430 : 460 : tree is_allocated;
10431 : 460 : tree ubound;
10432 : 460 : tree cdesc;
10433 : 460 : stmtblock_t dealloc_block;
10434 : :
10435 : 460 : gfc_init_block (&dealloc_block);
10436 : 460 : if (add_when_allocated)
10437 : 0 : gfc_add_expr_to_block (&dealloc_block, add_when_allocated);
10438 : :
10439 : : /* Convert the component into a rank 1 descriptor type. */
10440 : 460 : if (attr->dimension)
10441 : : {
10442 : 52 : tmp = gfc_get_element_type (TREE_TYPE (comp));
10443 : 52 : ubound = gfc_full_array_size (&dealloc_block, comp,
10444 : 52 : c->ts.type == BT_CLASS
10445 : 0 : ? CLASS_DATA (c)->as->rank
10446 : 52 : : c->as->rank);
10447 : : }
10448 : : else
10449 : : {
10450 : 408 : tmp = TREE_TYPE (comp);
10451 : 408 : ubound = build_int_cst (gfc_array_index_type, 1);
10452 : : }
10453 : :
10454 : 460 : cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
10455 : : &ubound, 1,
10456 : : GFC_ARRAY_ALLOCATABLE, false);
10457 : :
10458 : 460 : cdesc = gfc_create_var (cdesc, "cdesc");
10459 : 460 : DECL_ARTIFICIAL (cdesc) = 1;
10460 : :
10461 : 460 : gfc_add_modify (&dealloc_block, gfc_conv_descriptor_dtype (cdesc),
10462 : : gfc_get_dtype_rank_type (1, tmp));
10463 : 460 : gfc_conv_descriptor_lbound_set (&dealloc_block, cdesc,
10464 : : gfc_index_zero_node,
10465 : : gfc_index_one_node);
10466 : 460 : gfc_conv_descriptor_stride_set (&dealloc_block, cdesc,
10467 : : gfc_index_zero_node,
10468 : : gfc_index_one_node);
10469 : 460 : gfc_conv_descriptor_ubound_set (&dealloc_block, cdesc,
10470 : : gfc_index_zero_node, ubound);
10471 : :
10472 : 460 : if (attr->dimension)
10473 : 52 : comp = gfc_conv_descriptor_data_get (comp);
10474 : :
10475 : 460 : gfc_conv_descriptor_data_set (&dealloc_block, cdesc, comp);
10476 : :
10477 : : /* Now call the deallocator. */
10478 : 460 : vtab = gfc_find_vtab (&c->ts);
10479 : 460 : if (vtab->backend_decl == NULL)
10480 : 28 : gfc_get_symbol_decl (vtab);
10481 : 460 : tmp = gfc_build_addr_expr (NULL_TREE, vtab->backend_decl);
10482 : 460 : dealloc_fndecl = gfc_vptr_deallocate_get (tmp);
10483 : 460 : dealloc_fndecl = build_fold_indirect_ref_loc (input_location,
10484 : : dealloc_fndecl);
10485 : 460 : tmp = build_int_cst (TREE_TYPE (comp), 0);
10486 : 460 : is_allocated = fold_build2_loc (input_location, NE_EXPR,
10487 : : logical_type_node, tmp,
10488 : : comp);
10489 : 460 : cdesc = gfc_build_addr_expr (NULL_TREE, cdesc);
10490 : :
10491 : 460 : tmp = build_call_expr_loc (input_location,
10492 : : dealloc_fndecl, 1,
10493 : : cdesc);
10494 : 460 : gfc_add_expr_to_block (&dealloc_block, tmp);
10495 : :
10496 : 460 : tmp = gfc_finish_block (&dealloc_block);
10497 : :
10498 : 460 : tmp = fold_build3_loc (input_location, COND_EXPR,
10499 : : void_type_node, is_allocated, tmp,
10500 : : build_empty_stmt (input_location));
10501 : :
10502 : 460 : gfc_add_expr_to_block (&tmpblock, tmp);
10503 : 460 : }
10504 : 2940 : else if (add_when_allocated)
10505 : 525 : gfc_add_expr_to_block (&tmpblock, add_when_allocated);
10506 : :
10507 : 990 : if (c->ts.type == BT_CLASS && attr->allocatable
10508 : 10598 : && (!attr->codimension || !caf_enabled (caf_mode)))
10509 : : {
10510 : : /* Finally, reset the vptr to the declared type vtable and, if
10511 : : necessary reset the _len field.
10512 : :
10513 : : First recover the reference to the component and obtain
10514 : : the vptr. */
10515 : 975 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10516 : : decl, cdecl, NULL_TREE);
10517 : 975 : tmp = gfc_class_vptr_get (comp);
10518 : :
10519 : 975 : if (UNLIMITED_POLY (c))
10520 : : {
10521 : : /* Both vptr and _len field should be nulled. */
10522 : 211 : gfc_add_modify (&tmpblock, tmp,
10523 : 211 : build_int_cst (TREE_TYPE (tmp), 0));
10524 : 211 : tmp = gfc_class_len_get (comp);
10525 : 211 : gfc_add_modify (&tmpblock, tmp,
10526 : 211 : build_int_cst (TREE_TYPE (tmp), 0));
10527 : : }
10528 : : else
10529 : : {
10530 : : /* Build the vtable address and set the vptr with it. */
10531 : 764 : gfc_reset_vptr (&tmpblock, nullptr, tmp, c->ts.u.derived);
10532 : : }
10533 : : }
10534 : :
10535 : : /* Now add the deallocation of this component. */
10536 : 9608 : gfc_add_block_to_block (&fnblock, &tmpblock);
10537 : 9608 : break;
10538 : :
10539 : 3893 : case NULLIFY_ALLOC_COMP:
10540 : : /* Nullify
10541 : : - allocatable components (regular or in class)
10542 : : - components that have allocatable components
10543 : : - pointer components when in a coarray.
10544 : : Skip everything else especially proc_pointers, which may come
10545 : : coupled with the regular pointer attribute. */
10546 : 5120 : if (c->attr.proc_pointer
10547 : 3893 : || !(c->attr.allocatable || (c->ts.type == BT_CLASS
10548 : 424 : && CLASS_DATA (c)->attr.allocatable)
10549 : 1545 : || (cmp_has_alloc_comps
10550 : 234 : && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
10551 : 18 : || (c->ts.type == BT_CLASS
10552 : 12 : && !CLASS_DATA (c)->attr.class_pointer)))
10553 : 1329 : || (caf_in_coarray (caf_mode) && c->attr.pointer)))
10554 : 1227 : continue;
10555 : :
10556 : : /* Process class components first, because they always have the
10557 : : pointer-attribute set which would be caught wrong else. */
10558 : 2666 : if (c->ts.type == BT_CLASS
10559 : 411 : && (CLASS_DATA (c)->attr.allocatable
10560 : 0 : || CLASS_DATA (c)->attr.class_pointer))
10561 : : {
10562 : 411 : tree class_ref;
10563 : :
10564 : : /* Allocatable CLASS components. */
10565 : 411 : class_ref = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10566 : : decl, cdecl, NULL_TREE);
10567 : :
10568 : 411 : comp = gfc_class_data_get (class_ref);
10569 : 411 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
10570 : 215 : gfc_conv_descriptor_data_set (&fnblock, comp,
10571 : : null_pointer_node);
10572 : : else
10573 : : {
10574 : 196 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
10575 : : void_type_node, comp,
10576 : 196 : build_int_cst (TREE_TYPE (comp), 0));
10577 : 196 : gfc_add_expr_to_block (&fnblock, tmp);
10578 : : }
10579 : :
10580 : : /* The dynamic type of a disassociated pointer or unallocated
10581 : : allocatable variable is its declared type. An unlimited
10582 : : polymorphic entity has no declared type. */
10583 : 411 : gfc_reset_vptr (&fnblock, nullptr, class_ref, c->ts.u.derived);
10584 : :
10585 : 411 : cmp_has_alloc_comps = false;
10586 : 411 : }
10587 : : /* Coarrays need the component to be nulled before the api-call
10588 : : is made. */
10589 : 2255 : else if (c->attr.pointer || c->attr.allocatable)
10590 : : {
10591 : 2039 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10592 : : decl, cdecl, NULL_TREE);
10593 : 2039 : if (c->attr.dimension || c->attr.codimension)
10594 : 1446 : gfc_conv_descriptor_data_set (&fnblock, comp,
10595 : : null_pointer_node);
10596 : : else
10597 : 593 : gfc_add_modify (&fnblock, comp,
10598 : 593 : build_int_cst (TREE_TYPE (comp), 0));
10599 : 2039 : if (gfc_deferred_strlen (c, &comp))
10600 : : {
10601 : 250 : comp = fold_build3_loc (input_location, COMPONENT_REF,
10602 : 250 : TREE_TYPE (comp),
10603 : : decl, comp, NULL_TREE);
10604 : 500 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
10605 : 250 : TREE_TYPE (comp), comp,
10606 : 250 : build_int_cst (TREE_TYPE (comp), 0));
10607 : 250 : gfc_add_expr_to_block (&fnblock, tmp);
10608 : : }
10609 : : cmp_has_alloc_comps = false;
10610 : : }
10611 : :
10612 : 2666 : if (flag_coarray == GFC_FCOARRAY_LIB && caf_in_coarray (caf_mode))
10613 : : {
10614 : : /* Register a component of a derived type coarray with the
10615 : : coarray library. Do not register ultimate component
10616 : : coarrays here. They are treated like regular coarrays and
10617 : : are either allocated on all images or on none. */
10618 : 126 : tree token;
10619 : :
10620 : 126 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10621 : : decl, cdecl, NULL_TREE);
10622 : 126 : if (c->attr.dimension)
10623 : : {
10624 : : /* Set the dtype, because caf_register needs it. */
10625 : 100 : gfc_add_modify (&fnblock, gfc_conv_descriptor_dtype (comp),
10626 : 100 : gfc_get_dtype (TREE_TYPE (comp)));
10627 : 100 : tmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10628 : : decl, cdecl, NULL_TREE);
10629 : 100 : token = gfc_conv_descriptor_token (tmp);
10630 : : }
10631 : : else
10632 : : {
10633 : 26 : gfc_se se;
10634 : :
10635 : 26 : gfc_init_se (&se, NULL);
10636 : 52 : token = fold_build3_loc (input_location, COMPONENT_REF,
10637 : : pvoid_type_node, decl,
10638 : 26 : gfc_comp_caf_token (c), NULL_TREE);
10639 : 26 : comp = gfc_conv_scalar_to_descriptor (&se, comp,
10640 : 26 : c->ts.type == BT_CLASS
10641 : 26 : ? CLASS_DATA (c)->attr
10642 : : : c->attr);
10643 : 26 : gfc_add_block_to_block (&fnblock, &se.pre);
10644 : : }
10645 : :
10646 : 126 : gfc_allocate_using_caf_lib (&fnblock, comp, size_zero_node,
10647 : : gfc_build_addr_expr (NULL_TREE,
10648 : : token),
10649 : : NULL_TREE, NULL_TREE, NULL_TREE,
10650 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
10651 : : }
10652 : :
10653 : 2666 : if (cmp_has_alloc_comps)
10654 : : {
10655 : 216 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10656 : : decl, cdecl, NULL_TREE);
10657 : 216 : rank = c->as ? c->as->rank : 0;
10658 : 216 : tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE,
10659 : : rank, purpose, caf_mode, args,
10660 : : no_finalization);
10661 : 216 : gfc_add_expr_to_block (&fnblock, tmp);
10662 : : }
10663 : : break;
10664 : :
10665 : 29 : case REASSIGN_CAF_COMP:
10666 : 29 : if (caf_enabled (caf_mode)
10667 : 29 : && (c->attr.codimension
10668 : 23 : || (c->ts.type == BT_CLASS
10669 : 2 : && (CLASS_DATA (c)->attr.coarray_comp
10670 : 2 : || caf_in_coarray (caf_mode)))
10671 : 21 : || (c->ts.type == BT_DERIVED
10672 : 7 : && (c->ts.u.derived->attr.coarray_comp
10673 : 6 : || caf_in_coarray (caf_mode))))
10674 : 44 : && !same_type)
10675 : : {
10676 : 13 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10677 : : decl, cdecl, NULL_TREE);
10678 : 13 : dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10679 : : dest, cdecl, NULL_TREE);
10680 : :
10681 : 13 : if (c->attr.codimension)
10682 : : {
10683 : 6 : if (c->ts.type == BT_CLASS)
10684 : : {
10685 : 0 : comp = gfc_class_data_get (comp);
10686 : 0 : dcmp = gfc_class_data_get (dcmp);
10687 : : }
10688 : 6 : gfc_conv_descriptor_data_set (&fnblock, dcmp,
10689 : : gfc_conv_descriptor_data_get (comp));
10690 : : }
10691 : : else
10692 : : {
10693 : 7 : tmp = structure_alloc_comps (c->ts.u.derived, comp, dcmp,
10694 : : rank, purpose, caf_mode
10695 : : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY,
10696 : : args, no_finalization);
10697 : 7 : gfc_add_expr_to_block (&fnblock, tmp);
10698 : : }
10699 : : }
10700 : : break;
10701 : :
10702 : 8065 : case COPY_ALLOC_COMP:
10703 : 8065 : if (c->attr.pointer || c->attr.proc_pointer)
10704 : 152 : continue;
10705 : :
10706 : : /* We need source and destination components. */
10707 : 7913 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype, decl,
10708 : : cdecl, NULL_TREE);
10709 : 7913 : dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype, dest,
10710 : : cdecl, NULL_TREE);
10711 : 7913 : dcmp = fold_convert (TREE_TYPE (comp), dcmp);
10712 : :
10713 : 7913 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_type
10714 : 35 : && !c->attr.allocatable)
10715 : : {
10716 : 34 : tmp = gfc_copy_alloc_comp (c->ts.u.derived, comp, dcmp,
10717 : : 0, 0);
10718 : 34 : gfc_add_expr_to_block (&fnblock, tmp);
10719 : 34 : continue;
10720 : : }
10721 : :
10722 : 7879 : if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
10723 : : {
10724 : 722 : tree ftn_tree;
10725 : 722 : tree size;
10726 : 722 : tree dst_data;
10727 : 722 : tree src_data;
10728 : 722 : tree null_data;
10729 : :
10730 : 722 : dst_data = gfc_class_data_get (dcmp);
10731 : 722 : src_data = gfc_class_data_get (comp);
10732 : 722 : size = fold_convert (size_type_node,
10733 : : gfc_class_vtab_size_get (comp));
10734 : :
10735 : 722 : if (CLASS_DATA (c)->attr.dimension)
10736 : : {
10737 : 704 : nelems = gfc_conv_descriptor_size (src_data,
10738 : 352 : CLASS_DATA (c)->as->rank);
10739 : 352 : size = fold_build2_loc (input_location, MULT_EXPR,
10740 : : size_type_node, size,
10741 : : fold_convert (size_type_node,
10742 : : nelems));
10743 : : }
10744 : : else
10745 : 370 : nelems = build_int_cst (size_type_node, 1);
10746 : :
10747 : 722 : if (CLASS_DATA (c)->attr.dimension
10748 : 370 : || CLASS_DATA (c)->attr.codimension)
10749 : : {
10750 : 360 : src_data = gfc_conv_descriptor_data_get (src_data);
10751 : 360 : dst_data = gfc_conv_descriptor_data_get (dst_data);
10752 : : }
10753 : :
10754 : 722 : gfc_init_block (&tmpblock);
10755 : :
10756 : 722 : gfc_add_modify (&tmpblock, gfc_class_vptr_get (dcmp),
10757 : : gfc_class_vptr_get (comp));
10758 : :
10759 : : /* Copy the unlimited '_len' field. If it is greater than zero
10760 : : (ie. a character(_len)), multiply it by size and use this
10761 : : for the malloc call. */
10762 : 722 : if (UNLIMITED_POLY (c))
10763 : : {
10764 : 135 : gfc_add_modify (&tmpblock, gfc_class_len_get (dcmp),
10765 : : gfc_class_len_get (comp));
10766 : 135 : size = gfc_resize_class_size_with_len (&tmpblock, comp, size);
10767 : : }
10768 : :
10769 : : /* Coarray component have to have the same allocation status and
10770 : : shape/type-parameter/effective-type on the LHS and RHS of an
10771 : : intrinsic assignment. Hence, we did not deallocated them - and
10772 : : do not allocate them here. */
10773 : 722 : if (!CLASS_DATA (c)->attr.codimension)
10774 : : {
10775 : 707 : ftn_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
10776 : 707 : tmp = build_call_expr_loc (input_location, ftn_tree, 1, size);
10777 : 707 : gfc_add_modify (&tmpblock, dst_data,
10778 : 707 : fold_convert (TREE_TYPE (dst_data), tmp));
10779 : : }
10780 : :
10781 : 1429 : tmp = gfc_copy_class_to_class (comp, dcmp, nelems,
10782 : 722 : UNLIMITED_POLY (c));
10783 : 722 : gfc_add_expr_to_block (&tmpblock, tmp);
10784 : 722 : tmp = gfc_finish_block (&tmpblock);
10785 : :
10786 : 722 : gfc_init_block (&tmpblock);
10787 : 722 : gfc_add_modify (&tmpblock, dst_data,
10788 : 722 : fold_convert (TREE_TYPE (dst_data),
10789 : : null_pointer_node));
10790 : 722 : null_data = gfc_finish_block (&tmpblock);
10791 : :
10792 : 722 : null_cond = fold_build2_loc (input_location, NE_EXPR,
10793 : : logical_type_node, src_data,
10794 : : null_pointer_node);
10795 : :
10796 : 722 : gfc_add_expr_to_block (&fnblock, build3_v (COND_EXPR, null_cond,
10797 : : tmp, null_data));
10798 : 722 : continue;
10799 : 722 : }
10800 : :
10801 : : /* To implement guarded deep copy, i.e., deep copy only allocatable
10802 : : components that are really allocated, the deep copy code has to
10803 : : be generated first and then added to the if-block in
10804 : : gfc_duplicate_allocatable (). */
10805 : 7157 : if (cmp_has_alloc_comps && !c->attr.proc_pointer && !same_type)
10806 : : {
10807 : 1126 : rank = c->as ? c->as->rank : 0;
10808 : 1126 : tmp = fold_convert (TREE_TYPE (dcmp), comp);
10809 : 1126 : gfc_add_modify (&fnblock, dcmp, tmp);
10810 : 1126 : add_when_allocated = structure_alloc_comps (c->ts.u.derived,
10811 : : comp, dcmp,
10812 : : rank, purpose,
10813 : : caf_mode, args,
10814 : : no_finalization);
10815 : : }
10816 : : else
10817 : : add_when_allocated = NULL_TREE;
10818 : :
10819 : 7157 : if (gfc_deferred_strlen (c, &tmp))
10820 : : {
10821 : 338 : tree len, size;
10822 : 338 : len = tmp;
10823 : 338 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
10824 : 338 : TREE_TYPE (len),
10825 : : decl, len, NULL_TREE);
10826 : 338 : len = fold_build3_loc (input_location, COMPONENT_REF,
10827 : 338 : TREE_TYPE (len),
10828 : : dest, len, NULL_TREE);
10829 : 338 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
10830 : 338 : TREE_TYPE (len), len, tmp);
10831 : 338 : gfc_add_expr_to_block (&fnblock, tmp);
10832 : 338 : size = size_of_string_in_bytes (c->ts.kind, len);
10833 : : /* This component cannot have allocatable components,
10834 : : therefore add_when_allocated of duplicate_allocatable ()
10835 : : is always NULL. */
10836 : 338 : rank = c->as ? c->as->rank : 0;
10837 : 338 : tmp = duplicate_allocatable (dcmp, comp, ctype, rank,
10838 : : false, false, size, NULL_TREE);
10839 : 338 : gfc_add_expr_to_block (&fnblock, tmp);
10840 : : }
10841 : 6819 : else if (c->attr.pdt_array
10842 : 86 : && !c->attr.allocatable && !c->attr.pointer)
10843 : : {
10844 : 86 : tmp = duplicate_allocatable (dcmp, comp, ctype,
10845 : 86 : c->as ? c->as->rank : 0,
10846 : : false, false, NULL_TREE, NULL_TREE);
10847 : 86 : gfc_add_expr_to_block (&fnblock, tmp);
10848 : : }
10849 : 3675 : else if (c->attr.allocatable && !c->attr.proc_pointer
10850 : 10408 : && (!(cmp_has_alloc_comps && c->as) || c->attr.codimension
10851 : 319 : || caf_in_coarray (caf_mode)))
10852 : : {
10853 : 3360 : rank = c->as ? c->as->rank : 0;
10854 : 3360 : if (c->attr.codimension)
10855 : 15 : tmp = gfc_copy_allocatable_data (dcmp, comp, ctype, rank);
10856 : 3345 : else if (flag_coarray == GFC_FCOARRAY_LIB
10857 : 3345 : && caf_in_coarray (caf_mode))
10858 : : {
10859 : 55 : tree dst_tok;
10860 : 55 : if (c->as)
10861 : 40 : dst_tok = gfc_conv_descriptor_token (dcmp);
10862 : : else
10863 : : {
10864 : 15 : dst_tok
10865 : 15 : = fold_build3_loc (input_location, COMPONENT_REF,
10866 : : pvoid_type_node, dest,
10867 : 15 : gfc_comp_caf_token (c), NULL_TREE);
10868 : : }
10869 : 55 : tmp
10870 : 55 : = duplicate_allocatable_coarray (dcmp, dst_tok, comp, ctype,
10871 : : rank, add_when_allocated);
10872 : : }
10873 : : else
10874 : 3290 : tmp = gfc_duplicate_allocatable (dcmp, comp, ctype, rank,
10875 : : add_when_allocated);
10876 : 3360 : gfc_add_expr_to_block (&fnblock, tmp);
10877 : : }
10878 : : else
10879 : 3373 : if (cmp_has_alloc_comps || is_pdt_type)
10880 : 1078 : gfc_add_expr_to_block (&fnblock, add_when_allocated);
10881 : :
10882 : : break;
10883 : :
10884 : 1095 : case ALLOCATE_PDT_COMP:
10885 : :
10886 : 1095 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10887 : : decl, cdecl, NULL_TREE);
10888 : :
10889 : : /* Set the PDT KIND and LEN fields. */
10890 : 1095 : if (c->attr.pdt_kind || c->attr.pdt_len)
10891 : : {
10892 : 537 : gfc_se tse;
10893 : 537 : gfc_expr *c_expr = NULL;
10894 : 537 : gfc_actual_arglist *param = pdt_param_list;
10895 : 537 : gfc_init_se (&tse, NULL);
10896 : 2135 : for (; param; param = param->next)
10897 : 1061 : if (param->name && !strcmp (c->name, param->name))
10898 : 513 : c_expr = param->expr;
10899 : :
10900 : 537 : if (!c_expr)
10901 : 24 : c_expr = c->initializer;
10902 : :
10903 : 24 : if (c_expr)
10904 : : {
10905 : 537 : gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
10906 : 537 : gfc_add_block_to_block (&fnblock, &tse.pre);
10907 : 537 : gfc_add_modify (&fnblock, comp, tse.expr);
10908 : 537 : gfc_add_block_to_block (&fnblock, &tse.post);
10909 : : }
10910 : 537 : }
10911 : 558 : else if (c->initializer && !c->attr.pdt_string && !c->attr.pdt_array
10912 : 18 : && !c->as && !(c->ts.type == BT_DERIVED
10913 : 12 : && c->ts.u.derived->attr.pdt_type)) /* Take care of arrays. */
10914 : : {
10915 : 6 : gfc_se tse;
10916 : 6 : gfc_expr *c_expr;
10917 : 6 : c_expr = c->initializer;
10918 : 6 : gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
10919 : 6 : gfc_add_block_to_block (&fnblock, &tse.pre);
10920 : 6 : gfc_add_modify (&fnblock, comp, tse.expr);
10921 : 6 : gfc_add_block_to_block (&fnblock, &tse.post);
10922 : : }
10923 : :
10924 : 1095 : if (c->attr.pdt_string)
10925 : : {
10926 : 72 : gfc_se tse;
10927 : 72 : gfc_init_se (&tse, NULL);
10928 : 72 : tree strlen = NULL_TREE;
10929 : 72 : gfc_expr *e = gfc_copy_expr (c->ts.u.cl->length);
10930 : : /* Convert the parameterized string length to its value. The
10931 : : string length is stored in a hidden field in the same way as
10932 : : deferred string lengths. */
10933 : 72 : gfc_insert_parameter_exprs (e, pdt_param_list);
10934 : 72 : if (gfc_deferred_strlen (c, &strlen) && strlen != NULL_TREE)
10935 : : {
10936 : 72 : gfc_conv_expr_type (&tse, e,
10937 : 72 : TREE_TYPE (strlen));
10938 : 72 : strlen = fold_build3_loc (input_location, COMPONENT_REF,
10939 : 72 : TREE_TYPE (strlen),
10940 : : decl, strlen, NULL_TREE);
10941 : 72 : gfc_add_block_to_block (&fnblock, &tse.pre);
10942 : 72 : gfc_add_modify (&fnblock, strlen, tse.expr);
10943 : 72 : gfc_add_block_to_block (&fnblock, &tse.post);
10944 : 72 : c->ts.u.cl->backend_decl = strlen;
10945 : : }
10946 : 72 : gfc_free_expr (e);
10947 : :
10948 : : /* Scalar parameterized strings can be allocated now. */
10949 : 72 : if (!c->as)
10950 : : {
10951 : 72 : tmp = fold_convert (gfc_array_index_type, strlen);
10952 : 72 : tmp = size_of_string_in_bytes (c->ts.kind, tmp);
10953 : 72 : tmp = gfc_evaluate_now (tmp, &fnblock);
10954 : 72 : tmp = gfc_call_malloc (&fnblock, TREE_TYPE (comp), tmp);
10955 : 72 : gfc_add_modify (&fnblock, comp, tmp);
10956 : : }
10957 : : }
10958 : :
10959 : : /* Allocate parameterized arrays of parameterized derived types. */
10960 : 1095 : if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
10961 : 881 : && !((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10962 : 56 : && (c->ts.u.derived && c->ts.u.derived->attr.pdt_type)))
10963 : 831 : continue;
10964 : :
10965 : 264 : if (c->ts.type == BT_CLASS)
10966 : 0 : comp = gfc_class_data_get (comp);
10967 : :
10968 : 264 : if (c->attr.pdt_array)
10969 : : {
10970 : 214 : gfc_se tse;
10971 : 214 : int i;
10972 : 214 : tree size = gfc_index_one_node;
10973 : 214 : tree offset = gfc_index_zero_node;
10974 : 214 : tree lower, upper;
10975 : 214 : gfc_expr *e;
10976 : :
10977 : : /* This chunk takes the expressions for 'lower' and 'upper'
10978 : : in the arrayspec and substitutes in the expressions for
10979 : : the parameters from 'pdt_param_list'. The descriptor
10980 : : fields can then be filled from the values so obtained. */
10981 : 214 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)));
10982 : 524 : for (i = 0; i < c->as->rank; i++)
10983 : : {
10984 : 310 : gfc_init_se (&tse, NULL);
10985 : 310 : e = gfc_copy_expr (c->as->lower[i]);
10986 : 310 : gfc_insert_parameter_exprs (e, pdt_param_list);
10987 : 310 : gfc_conv_expr_type (&tse, e, gfc_array_index_type);
10988 : 310 : gfc_free_expr (e);
10989 : 310 : lower = tse.expr;
10990 : 310 : gfc_add_block_to_block (&fnblock, &tse.pre);
10991 : 310 : gfc_conv_descriptor_lbound_set (&fnblock, comp,
10992 : : gfc_rank_cst[i],
10993 : : lower);
10994 : 310 : gfc_add_block_to_block (&fnblock, &tse.post);
10995 : 310 : e = gfc_copy_expr (c->as->upper[i]);
10996 : 310 : gfc_insert_parameter_exprs (e, pdt_param_list);
10997 : 310 : gfc_conv_expr_type (&tse, e, gfc_array_index_type);
10998 : 310 : gfc_free_expr (e);
10999 : 310 : upper = tse.expr;
11000 : 310 : gfc_add_block_to_block (&fnblock, &tse.pre);
11001 : 310 : gfc_conv_descriptor_ubound_set (&fnblock, comp,
11002 : : gfc_rank_cst[i],
11003 : : upper);
11004 : 310 : gfc_add_block_to_block (&fnblock, &tse.post);
11005 : 310 : gfc_conv_descriptor_stride_set (&fnblock, comp,
11006 : : gfc_rank_cst[i],
11007 : : size);
11008 : 310 : size = gfc_evaluate_now (size, &fnblock);
11009 : 310 : offset = fold_build2_loc (input_location,
11010 : : MINUS_EXPR,
11011 : : gfc_array_index_type,
11012 : : offset, size);
11013 : 310 : offset = gfc_evaluate_now (offset, &fnblock);
11014 : 310 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11015 : : gfc_array_index_type,
11016 : : upper, lower);
11017 : 310 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11018 : : gfc_array_index_type,
11019 : : tmp, gfc_index_one_node);
11020 : 310 : size = fold_build2_loc (input_location, MULT_EXPR,
11021 : : gfc_array_index_type, size, tmp);
11022 : : }
11023 : 214 : gfc_conv_descriptor_offset_set (&fnblock, comp, offset);
11024 : 214 : if (c->ts.type == BT_CLASS)
11025 : : {
11026 : 0 : tmp = gfc_get_vptr_from_expr (comp);
11027 : 0 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
11028 : 0 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
11029 : 0 : tmp = gfc_vptr_size_get (tmp);
11030 : : }
11031 : : else
11032 : 214 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (ctype));
11033 : 214 : tmp = fold_convert (gfc_array_index_type, tmp);
11034 : 214 : size = fold_build2_loc (input_location, MULT_EXPR,
11035 : : gfc_array_index_type, size, tmp);
11036 : 214 : size = gfc_evaluate_now (size, &fnblock);
11037 : 214 : tmp = gfc_call_malloc (&fnblock, NULL, size);
11038 : 214 : gfc_conv_descriptor_data_set (&fnblock, comp, tmp);
11039 : 214 : tmp = gfc_conv_descriptor_dtype (comp);
11040 : 214 : gfc_add_modify (&fnblock, tmp, gfc_get_dtype (ctype));
11041 : :
11042 : 214 : if (c->initializer && c->initializer->rank)
11043 : : {
11044 : 0 : gfc_init_se (&tse, NULL);
11045 : 0 : e = gfc_copy_expr (c->initializer);
11046 : 0 : gfc_insert_parameter_exprs (e, pdt_param_list);
11047 : 0 : gfc_conv_expr_descriptor (&tse, e);
11048 : 0 : gfc_add_block_to_block (&fnblock, &tse.pre);
11049 : 0 : gfc_free_expr (e);
11050 : 0 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
11051 : 0 : tmp = build_call_expr_loc (input_location, tmp, 3,
11052 : : gfc_conv_descriptor_data_get (comp),
11053 : : gfc_conv_descriptor_data_get (tse.expr),
11054 : : fold_convert (size_type_node, size));
11055 : 0 : gfc_add_expr_to_block (&fnblock, tmp);
11056 : 0 : gfc_add_block_to_block (&fnblock, &tse.post);
11057 : : }
11058 : : }
11059 : :
11060 : : /* Recurse in to PDT components. */
11061 : 264 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11062 : 62 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
11063 : 62 : && !(c->attr.pointer || c->attr.allocatable))
11064 : : {
11065 : 54 : bool is_deferred = false;
11066 : 54 : gfc_actual_arglist *tail = c->param_list;
11067 : :
11068 : 138 : for (; tail; tail = tail->next)
11069 : 84 : if (!tail->expr)
11070 : 24 : is_deferred = true;
11071 : :
11072 : 54 : tail = is_deferred ? pdt_param_list : c->param_list;
11073 : 54 : tmp = gfc_allocate_pdt_comp (c->ts.u.derived, comp,
11074 : 54 : c->as ? c->as->rank : 0,
11075 : : tail);
11076 : 54 : gfc_add_expr_to_block (&fnblock, tmp);
11077 : : }
11078 : :
11079 : : break;
11080 : :
11081 : 1412 : case DEALLOCATE_PDT_COMP:
11082 : : /* Deallocate array or parameterized string length components
11083 : : of parameterized derived types. */
11084 : 1412 : if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
11085 : 1090 : && !c->attr.pdt_string
11086 : 1006 : && !((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11087 : 55 : && (c->ts.u.derived && c->ts.u.derived->attr.pdt_type)))
11088 : 951 : continue;
11089 : :
11090 : 461 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
11091 : : decl, cdecl, NULL_TREE);
11092 : 461 : if (c->ts.type == BT_CLASS)
11093 : 0 : comp = gfc_class_data_get (comp);
11094 : :
11095 : : /* Recurse in to PDT components. */
11096 : 461 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11097 : 67 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
11098 : 67 : && (!c->attr.pointer && !c->attr.allocatable))
11099 : : {
11100 : 60 : tmp = gfc_deallocate_pdt_comp (c->ts.u.derived, comp,
11101 : 60 : c->as ? c->as->rank : 0);
11102 : 60 : gfc_add_expr_to_block (&fnblock, tmp);
11103 : : }
11104 : :
11105 : 461 : if (c->attr.pdt_array || c->attr.pdt_string)
11106 : : {
11107 : 406 : tmp = comp;
11108 : 406 : if (c->attr.pdt_array)
11109 : 322 : tmp = gfc_conv_descriptor_data_get (comp);
11110 : 406 : null_cond = fold_build2_loc (input_location, NE_EXPR,
11111 : : logical_type_node, tmp,
11112 : 406 : build_int_cst (TREE_TYPE (tmp), 0));
11113 : 406 : if (flag_openmp_allocators)
11114 : : {
11115 : 0 : tree cd, t;
11116 : 0 : if (c->attr.pdt_array)
11117 : 0 : cd = fold_build2_loc (input_location, EQ_EXPR,
11118 : : boolean_type_node,
11119 : : gfc_conv_descriptor_version (comp),
11120 : : build_int_cst (integer_type_node, 1));
11121 : : else
11122 : 0 : cd = gfc_omp_call_is_alloc (tmp);
11123 : 0 : t = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
11124 : 0 : t = build_call_expr_loc (input_location, t, 1, tmp);
11125 : :
11126 : 0 : stmtblock_t tblock;
11127 : 0 : gfc_init_block (&tblock);
11128 : 0 : gfc_add_expr_to_block (&tblock, t);
11129 : 0 : if (c->attr.pdt_array)
11130 : 0 : gfc_add_modify (&tblock, gfc_conv_descriptor_version (comp),
11131 : : integer_zero_node);
11132 : 0 : tmp = build3_loc (input_location, COND_EXPR, void_type_node,
11133 : : cd, gfc_finish_block (&tblock),
11134 : : gfc_call_free (tmp));
11135 : : }
11136 : : else
11137 : 406 : tmp = gfc_call_free (tmp);
11138 : 406 : tmp = build3_v (COND_EXPR, null_cond, tmp,
11139 : : build_empty_stmt (input_location));
11140 : 406 : gfc_add_expr_to_block (&fnblock, tmp);
11141 : :
11142 : 406 : if (c->attr.pdt_array)
11143 : 322 : gfc_conv_descriptor_data_set (&fnblock, comp, null_pointer_node);
11144 : : else
11145 : : {
11146 : 84 : tmp = fold_convert (TREE_TYPE (comp), null_pointer_node);
11147 : 84 : gfc_add_modify (&fnblock, comp, tmp);
11148 : : }
11149 : : }
11150 : :
11151 : : break;
11152 : :
11153 : 264 : case CHECK_PDT_DUMMY:
11154 : :
11155 : 264 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
11156 : : decl, cdecl, NULL_TREE);
11157 : 264 : if (c->ts.type == BT_CLASS)
11158 : 0 : comp = gfc_class_data_get (comp);
11159 : :
11160 : : /* Recurse in to PDT components. */
11161 : 264 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11162 : 0 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_type)
11163 : : {
11164 : 0 : tmp = gfc_check_pdt_dummy (c->ts.u.derived, comp,
11165 : 0 : c->as ? c->as->rank : 0,
11166 : : pdt_param_list);
11167 : 0 : gfc_add_expr_to_block (&fnblock, tmp);
11168 : : }
11169 : :
11170 : 264 : if (!c->attr.pdt_len)
11171 : 216 : continue;
11172 : : else
11173 : : {
11174 : 48 : gfc_se tse;
11175 : 48 : gfc_expr *c_expr = NULL;
11176 : 48 : gfc_actual_arglist *param = pdt_param_list;
11177 : :
11178 : 48 : gfc_init_se (&tse, NULL);
11179 : 186 : for (; param; param = param->next)
11180 : 90 : if (!strcmp (c->name, param->name)
11181 : 48 : && param->spec_type == SPEC_EXPLICIT)
11182 : 30 : c_expr = param->expr;
11183 : :
11184 : 48 : if (c_expr)
11185 : : {
11186 : 30 : tree error, cond, cname;
11187 : 30 : gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
11188 : 30 : cond = fold_build2_loc (input_location, NE_EXPR,
11189 : : logical_type_node,
11190 : : comp, tse.expr);
11191 : 30 : cname = gfc_build_cstring_const (c->name);
11192 : 30 : cname = gfc_build_addr_expr (pchar_type_node, cname);
11193 : 30 : error = gfc_trans_runtime_error (true, NULL,
11194 : : "The value of the PDT LEN "
11195 : : "parameter '%s' does not "
11196 : : "agree with that in the "
11197 : : "dummy declaration",
11198 : : cname);
11199 : 30 : tmp = fold_build3_loc (input_location, COND_EXPR,
11200 : : void_type_node, cond, error,
11201 : : build_empty_stmt (input_location));
11202 : 30 : gfc_add_expr_to_block (&fnblock, tmp);
11203 : : }
11204 : : }
11205 : 48 : break;
11206 : :
11207 : 0 : default:
11208 : 0 : gcc_unreachable ();
11209 : 4282 : break;
11210 : : }
11211 : : }
11212 : 13754 : seen_derived_types.remove (der_type);
11213 : :
11214 : 13754 : return gfc_finish_block (&fnblock);
11215 : : }
11216 : :
11217 : : /* Recursively traverse an object of derived type, generating code to
11218 : : nullify allocatable components. */
11219 : :
11220 : : tree
11221 : 2272 : gfc_nullify_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
11222 : : int caf_mode)
11223 : : {
11224 : 2272 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11225 : : NULLIFY_ALLOC_COMP,
11226 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
11227 : 2272 : NULL);
11228 : : }
11229 : :
11230 : :
11231 : : /* Recursively traverse an object of derived type, generating code to
11232 : : deallocate allocatable components. */
11233 : :
11234 : : tree
11235 : 2453 : gfc_deallocate_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
11236 : : int caf_mode)
11237 : : {
11238 : 2453 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11239 : : DEALLOCATE_ALLOC_COMP,
11240 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
11241 : 2453 : NULL);
11242 : : }
11243 : :
11244 : : tree
11245 : 1 : gfc_bcast_alloc_comp (gfc_symbol *derived, gfc_expr *expr, int rank,
11246 : : tree image_index, tree stat, tree errmsg,
11247 : : tree errmsg_len)
11248 : : {
11249 : 1 : tree tmp, array;
11250 : 1 : gfc_se argse;
11251 : 1 : stmtblock_t block, post_block;
11252 : 1 : gfc_co_subroutines_args args;
11253 : :
11254 : 1 : args.image_index = image_index;
11255 : 1 : args.stat = stat;
11256 : 1 : args.errmsg = errmsg;
11257 : 1 : args.errmsg_len = errmsg_len;
11258 : :
11259 : 1 : if (rank == 0)
11260 : : {
11261 : 1 : gfc_start_block (&block);
11262 : 1 : gfc_init_block (&post_block);
11263 : 1 : gfc_init_se (&argse, NULL);
11264 : 1 : gfc_conv_expr (&argse, expr);
11265 : 1 : gfc_add_block_to_block (&block, &argse.pre);
11266 : 1 : gfc_add_block_to_block (&post_block, &argse.post);
11267 : 1 : array = argse.expr;
11268 : : }
11269 : : else
11270 : : {
11271 : 0 : gfc_init_se (&argse, NULL);
11272 : 0 : argse.want_pointer = 1;
11273 : 0 : gfc_conv_expr_descriptor (&argse, expr);
11274 : 0 : array = argse.expr;
11275 : : }
11276 : :
11277 : 1 : tmp = structure_alloc_comps (derived, array, NULL_TREE, rank,
11278 : : BCAST_ALLOC_COMP,
11279 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
11280 : : &args);
11281 : 1 : return tmp;
11282 : : }
11283 : :
11284 : : /* Recursively traverse an object of derived type, generating code to
11285 : : deallocate allocatable components. But do not deallocate coarrays.
11286 : : To be used for intrinsic assignment, which may not change the allocation
11287 : : status of coarrays. */
11288 : :
11289 : : tree
11290 : 1870 : gfc_deallocate_alloc_comp_no_caf (gfc_symbol * der_type, tree decl, int rank,
11291 : : bool no_finalization)
11292 : : {
11293 : 1870 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11294 : : DEALLOCATE_ALLOC_COMP, 0, NULL,
11295 : 1870 : no_finalization);
11296 : : }
11297 : :
11298 : :
11299 : : tree
11300 : 4 : gfc_reassign_alloc_comp_caf (gfc_symbol *der_type, tree decl, tree dest)
11301 : : {
11302 : 4 : return structure_alloc_comps (der_type, decl, dest, 0, REASSIGN_CAF_COMP,
11303 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
11304 : 4 : NULL);
11305 : : }
11306 : :
11307 : :
11308 : : /* Recursively traverse an object of derived type, generating code to
11309 : : copy it and its allocatable components. */
11310 : :
11311 : : tree
11312 : 3591 : gfc_copy_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank,
11313 : : int caf_mode)
11314 : : {
11315 : 3591 : return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
11316 : 3591 : caf_mode, NULL);
11317 : : }
11318 : :
11319 : :
11320 : : /* Recursively traverse an object of derived type, generating code to
11321 : : copy it and its allocatable components, while suppressing any
11322 : : finalization that might occur. This is used in the finalization of
11323 : : function results. */
11324 : :
11325 : : tree
11326 : 37 : gfc_copy_alloc_comp_no_fini (gfc_symbol * der_type, tree decl, tree dest,
11327 : : int rank, int caf_mode)
11328 : : {
11329 : 37 : return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
11330 : 37 : caf_mode, NULL, true);
11331 : : }
11332 : :
11333 : :
11334 : : /* Recursively traverse an object of derived type, generating code to
11335 : : copy only its allocatable components. */
11336 : :
11337 : : tree
11338 : 0 : gfc_copy_only_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
11339 : : {
11340 : 0 : return structure_alloc_comps (der_type, decl, dest, rank,
11341 : 0 : COPY_ONLY_ALLOC_COMP, 0, NULL);
11342 : : }
11343 : :
11344 : :
11345 : : /* Recursively traverse an object of parameterized derived type, generating
11346 : : code to allocate parameterized components. */
11347 : :
11348 : : tree
11349 : 365 : gfc_allocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank,
11350 : : gfc_actual_arglist *param_list)
11351 : : {
11352 : 365 : tree res;
11353 : 365 : gfc_actual_arglist *old_param_list = pdt_param_list;
11354 : 365 : pdt_param_list = param_list;
11355 : 365 : res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11356 : : ALLOCATE_PDT_COMP, 0, NULL);
11357 : 365 : pdt_param_list = old_param_list;
11358 : 365 : return res;
11359 : : }
11360 : :
11361 : : /* Recursively traverse an object of parameterized derived type, generating
11362 : : code to deallocate parameterized components. */
11363 : :
11364 : : tree
11365 : 474 : gfc_deallocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank)
11366 : : {
11367 : 474 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11368 : 474 : DEALLOCATE_PDT_COMP, 0, NULL);
11369 : : }
11370 : :
11371 : :
11372 : : /* Recursively traverse a dummy of parameterized derived type to check the
11373 : : values of LEN parameters. */
11374 : :
11375 : : tree
11376 : 48 : gfc_check_pdt_dummy (gfc_symbol * der_type, tree decl, int rank,
11377 : : gfc_actual_arglist *param_list)
11378 : : {
11379 : 48 : tree res;
11380 : 48 : gfc_actual_arglist *old_param_list = pdt_param_list;
11381 : 48 : pdt_param_list = param_list;
11382 : 48 : res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11383 : : CHECK_PDT_DUMMY, 0, NULL);
11384 : 48 : pdt_param_list = old_param_list;
11385 : 48 : return res;
11386 : : }
11387 : :
11388 : :
11389 : : /* Returns the value of LBOUND for an expression. This could be broken out
11390 : : from gfc_conv_intrinsic_bound but this seemed to be simpler. This is
11391 : : called by gfc_alloc_allocatable_for_assignment. */
11392 : : static tree
11393 : 977 : get_std_lbound (gfc_expr *expr, tree desc, int dim, bool assumed_size)
11394 : : {
11395 : 977 : tree lbound;
11396 : 977 : tree ubound;
11397 : 977 : tree stride;
11398 : 977 : tree cond, cond1, cond3, cond4;
11399 : 977 : tree tmp;
11400 : 977 : gfc_ref *ref;
11401 : :
11402 : 977 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
11403 : : {
11404 : 492 : tmp = gfc_rank_cst[dim];
11405 : 492 : lbound = gfc_conv_descriptor_lbound_get (desc, tmp);
11406 : 492 : ubound = gfc_conv_descriptor_ubound_get (desc, tmp);
11407 : 492 : stride = gfc_conv_descriptor_stride_get (desc, tmp);
11408 : 492 : cond1 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
11409 : : ubound, lbound);
11410 : 492 : cond3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
11411 : : stride, gfc_index_zero_node);
11412 : 492 : cond3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
11413 : : logical_type_node, cond3, cond1);
11414 : 492 : cond4 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
11415 : : stride, gfc_index_zero_node);
11416 : 492 : if (assumed_size)
11417 : 0 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11418 : : tmp, build_int_cst (gfc_array_index_type,
11419 : 0 : expr->rank - 1));
11420 : : else
11421 : 492 : cond = logical_false_node;
11422 : :
11423 : 492 : cond1 = fold_build2_loc (input_location, TRUTH_OR_EXPR,
11424 : : logical_type_node, cond3, cond4);
11425 : 492 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
11426 : : logical_type_node, cond, cond1);
11427 : :
11428 : 492 : return fold_build3_loc (input_location, COND_EXPR,
11429 : : gfc_array_index_type, cond,
11430 : 492 : lbound, gfc_index_one_node);
11431 : : }
11432 : :
11433 : 485 : if (expr->expr_type == EXPR_FUNCTION)
11434 : : {
11435 : : /* A conversion function, so use the argument. */
11436 : 7 : gcc_assert (expr->value.function.isym
11437 : : && expr->value.function.isym->conversion);
11438 : 7 : expr = expr->value.function.actual->expr;
11439 : : }
11440 : :
11441 : 485 : if (expr->expr_type == EXPR_VARIABLE)
11442 : : {
11443 : 485 : tmp = TREE_TYPE (expr->symtree->n.sym->backend_decl);
11444 : 1283 : for (ref = expr->ref; ref; ref = ref->next)
11445 : : {
11446 : 798 : if (ref->type == REF_COMPONENT
11447 : 264 : && ref->u.c.component->as
11448 : 215 : && ref->next
11449 : 215 : && ref->next->u.ar.type == AR_FULL)
11450 : 173 : tmp = TREE_TYPE (ref->u.c.component->backend_decl);
11451 : : }
11452 : 485 : return GFC_TYPE_ARRAY_LBOUND(tmp, dim);
11453 : : }
11454 : :
11455 : 0 : return gfc_index_one_node;
11456 : : }
11457 : :
11458 : :
11459 : : /* Returns true if an expression represents an lhs that can be reallocated
11460 : : on assignment. */
11461 : :
11462 : : bool
11463 : 333045 : gfc_is_reallocatable_lhs (gfc_expr *expr)
11464 : : {
11465 : 333045 : gfc_ref * ref;
11466 : 333045 : gfc_symbol *sym;
11467 : :
11468 : 333045 : if (!flag_realloc_lhs)
11469 : : return false;
11470 : :
11471 : 333045 : if (!expr->ref)
11472 : : return false;
11473 : :
11474 : 116644 : sym = expr->symtree->n.sym;
11475 : :
11476 : 116644 : if (sym->attr.associate_var && !expr->ref)
11477 : : return false;
11478 : :
11479 : : /* An allocatable class variable with no reference. */
11480 : 116644 : if (sym->ts.type == BT_CLASS
11481 : 3448 : && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
11482 : 3361 : && CLASS_DATA (sym)->attr.allocatable
11483 : : && expr->ref
11484 : 2163 : && ((expr->ref->type == REF_ARRAY && expr->ref->u.ar.type == AR_FULL
11485 : 0 : && expr->ref->next == NULL)
11486 : 2163 : || (expr->ref->type == REF_COMPONENT
11487 : 2163 : && strcmp (expr->ref->u.c.component->name, "_data") == 0
11488 : 1787 : && (expr->ref->next == NULL
11489 : 1787 : || (expr->ref->next->type == REF_ARRAY
11490 : 1787 : && expr->ref->next->u.ar.type == AR_FULL
11491 : 1503 : && expr->ref->next->next == NULL)))))
11492 : : return true;
11493 : :
11494 : : /* An allocatable variable. */
11495 : 115281 : if (sym->attr.allocatable
11496 : 28758 : && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
11497 : : && expr->ref
11498 : 28758 : && expr->ref->type == REF_ARRAY
11499 : 27741 : && expr->ref->u.ar.type == AR_FULL)
11500 : : return true;
11501 : :
11502 : : /* All that can be left are allocatable components. */
11503 : 96878 : if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
11504 : : return false;
11505 : :
11506 : : /* Find a component ref followed by an array reference. */
11507 : 45733 : for (ref = expr->ref; ref; ref = ref->next)
11508 : 32280 : if (ref->next
11509 : 18827 : && ref->type == REF_COMPONENT
11510 : 11238 : && ref->next->type == REF_ARRAY
11511 : 8776 : && !ref->next->next)
11512 : : break;
11513 : :
11514 : 20279 : if (!ref)
11515 : : return false;
11516 : :
11517 : : /* Return true if valid reallocatable lhs. */
11518 : 6826 : if (ref->u.c.component->attr.allocatable
11519 : 2998 : && ref->next->u.ar.type == AR_FULL)
11520 : 2196 : return true;
11521 : :
11522 : : return false;
11523 : : }
11524 : :
11525 : :
11526 : : static tree
11527 : 56 : concat_str_length (gfc_expr* expr)
11528 : : {
11529 : 56 : tree type;
11530 : 56 : tree len1;
11531 : 56 : tree len2;
11532 : 56 : gfc_se se;
11533 : :
11534 : 56 : type = gfc_typenode_for_spec (&expr->value.op.op1->ts);
11535 : 56 : len1 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
11536 : 56 : if (len1 == NULL_TREE)
11537 : : {
11538 : 56 : if (expr->value.op.op1->expr_type == EXPR_OP)
11539 : 31 : len1 = concat_str_length (expr->value.op.op1);
11540 : 25 : else if (expr->value.op.op1->expr_type == EXPR_CONSTANT)
11541 : 25 : len1 = build_int_cst (gfc_charlen_type_node,
11542 : 25 : expr->value.op.op1->value.character.length);
11543 : 0 : else if (expr->value.op.op1->ts.u.cl->length)
11544 : : {
11545 : 0 : gfc_init_se (&se, NULL);
11546 : 0 : gfc_conv_expr (&se, expr->value.op.op1->ts.u.cl->length);
11547 : 0 : len1 = se.expr;
11548 : : }
11549 : : else
11550 : : {
11551 : : /* Last resort! */
11552 : 0 : gfc_init_se (&se, NULL);
11553 : 0 : se.want_pointer = 1;
11554 : 0 : se.descriptor_only = 1;
11555 : 0 : gfc_conv_expr (&se, expr->value.op.op1);
11556 : 0 : len1 = se.string_length;
11557 : : }
11558 : : }
11559 : :
11560 : 56 : type = gfc_typenode_for_spec (&expr->value.op.op2->ts);
11561 : 56 : len2 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
11562 : 56 : if (len2 == NULL_TREE)
11563 : : {
11564 : 31 : if (expr->value.op.op2->expr_type == EXPR_OP)
11565 : 0 : len2 = concat_str_length (expr->value.op.op2);
11566 : 31 : else if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
11567 : 25 : len2 = build_int_cst (gfc_charlen_type_node,
11568 : 25 : expr->value.op.op2->value.character.length);
11569 : 6 : else if (expr->value.op.op2->ts.u.cl->length)
11570 : : {
11571 : 6 : gfc_init_se (&se, NULL);
11572 : 6 : gfc_conv_expr (&se, expr->value.op.op2->ts.u.cl->length);
11573 : 6 : len2 = se.expr;
11574 : : }
11575 : : else
11576 : : {
11577 : : /* Last resort! */
11578 : 0 : gfc_init_se (&se, NULL);
11579 : 0 : se.want_pointer = 1;
11580 : 0 : se.descriptor_only = 1;
11581 : 0 : gfc_conv_expr (&se, expr->value.op.op2);
11582 : 0 : len2 = se.string_length;
11583 : : }
11584 : : }
11585 : :
11586 : 56 : gcc_assert(len1 && len2);
11587 : 56 : len1 = fold_convert (gfc_charlen_type_node, len1);
11588 : 56 : len2 = fold_convert (gfc_charlen_type_node, len2);
11589 : :
11590 : 56 : return fold_build2_loc (input_location, PLUS_EXPR,
11591 : 56 : gfc_charlen_type_node, len1, len2);
11592 : : }
11593 : :
11594 : :
11595 : : /* Among the scalarization chain of LOOP, find the element associated with an
11596 : : allocatable array on the lhs of an assignment and evaluate its fields
11597 : : (bounds, offset, etc) to new variables, putting the new code in BLOCK. This
11598 : : function is to be called after putting the reallocation code in BLOCK and
11599 : : before the beginning of the scalarization loop body.
11600 : :
11601 : : The fields to be saved are expected to hold on entry to the function
11602 : : expressions referencing the array descriptor. Especially the expressions
11603 : : shouldn't be already temporary variable references as the value saved before
11604 : : reallocation would be incorrect after reallocation.
11605 : : At the end of the function, the expressions have been replaced with variable
11606 : : references. */
11607 : :
11608 : : static void
11609 : 5857 : update_reallocated_descriptor (stmtblock_t *block, gfc_loopinfo *loop)
11610 : : {
11611 : 20724 : for (gfc_ss *s = loop->ss; s != gfc_ss_terminator; s = s->loop_chain)
11612 : : {
11613 : 14867 : if (!s->is_alloc_lhs)
11614 : 9010 : continue;
11615 : :
11616 : 5857 : gcc_assert (s->info->type == GFC_SS_SECTION);
11617 : 5857 : gfc_array_info *info = &s->info->data.array;
11618 : :
11619 : : #define SAVE_VALUE(value) \
11620 : : do \
11621 : : { \
11622 : : value = gfc_evaluate_now (value, block); \
11623 : : } \
11624 : : while (0)
11625 : :
11626 : 5857 : if (save_descriptor_data (info->descriptor, info->data))
11627 : 5150 : SAVE_VALUE (info->data);
11628 : 5857 : SAVE_VALUE (info->offset);
11629 : 5857 : info->saved_offset = info->offset;
11630 : 14765 : for (int i = 0; i < s->dimen; i++)
11631 : : {
11632 : 8908 : int dim = s->dim[i];
11633 : 8908 : SAVE_VALUE (info->start[dim]);
11634 : 8908 : SAVE_VALUE (info->end[dim]);
11635 : 8908 : SAVE_VALUE (info->stride[dim]);
11636 : 8908 : SAVE_VALUE (info->delta[dim]);
11637 : : }
11638 : :
11639 : : #undef SAVE_VALUE
11640 : : }
11641 : 5857 : }
11642 : :
11643 : :
11644 : : /* Allocate the lhs of an assignment to an allocatable array, otherwise
11645 : : reallocate it. */
11646 : :
11647 : : tree
11648 : 5857 : gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop,
11649 : : gfc_expr *expr1,
11650 : : gfc_expr *expr2)
11651 : : {
11652 : 5857 : stmtblock_t realloc_block;
11653 : 5857 : stmtblock_t alloc_block;
11654 : 5857 : stmtblock_t fblock;
11655 : 5857 : stmtblock_t loop_pre_block;
11656 : 5857 : gfc_ref *ref;
11657 : 5857 : gfc_ss *rss;
11658 : 5857 : gfc_ss *lss;
11659 : 5857 : gfc_array_info *linfo;
11660 : 5857 : tree realloc_expr;
11661 : 5857 : tree alloc_expr;
11662 : 5857 : tree size1;
11663 : 5857 : tree size2;
11664 : 5857 : tree elemsize1;
11665 : 5857 : tree elemsize2;
11666 : 5857 : tree array1;
11667 : 5857 : tree cond_null;
11668 : 5857 : tree cond;
11669 : 5857 : tree tmp;
11670 : 5857 : tree tmp2;
11671 : 5857 : tree lbound;
11672 : 5857 : tree ubound;
11673 : 5857 : tree desc;
11674 : 5857 : tree old_desc;
11675 : 5857 : tree desc2;
11676 : 5857 : tree offset;
11677 : 5857 : tree jump_label1;
11678 : 5857 : tree jump_label2;
11679 : 5857 : tree lbd;
11680 : 5857 : tree class_expr2 = NULL_TREE;
11681 : 5857 : int n;
11682 : 5857 : gfc_array_spec * as;
11683 : 5857 : bool coarray = (flag_coarray == GFC_FCOARRAY_LIB
11684 : 5857 : && gfc_caf_attr (expr1, true).codimension);
11685 : 5857 : tree token;
11686 : 5857 : gfc_se caf_se;
11687 : :
11688 : : /* x = f(...) with x allocatable. In this case, expr1 is the rhs.
11689 : : Find the lhs expression in the loop chain and set expr1 and
11690 : : expr2 accordingly. */
11691 : 5857 : if (expr1->expr_type == EXPR_FUNCTION && expr2 == NULL)
11692 : : {
11693 : 166 : expr2 = expr1;
11694 : : /* Find the ss for the lhs. */
11695 : 166 : lss = loop->ss;
11696 : 332 : for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
11697 : 332 : if (lss->info->expr && lss->info->expr->expr_type == EXPR_VARIABLE)
11698 : : break;
11699 : 166 : if (lss == gfc_ss_terminator)
11700 : : return NULL_TREE;
11701 : 166 : expr1 = lss->info->expr;
11702 : : }
11703 : :
11704 : : /* Bail out if this is not a valid allocate on assignment. */
11705 : 5857 : if (!gfc_is_reallocatable_lhs (expr1)
11706 : 5857 : || (expr2 && !expr2->rank))
11707 : : return NULL_TREE;
11708 : :
11709 : : /* Find the ss for the lhs. */
11710 : 5857 : lss = loop->ss;
11711 : 14867 : for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
11712 : 14867 : if (lss->info->expr == expr1)
11713 : : break;
11714 : :
11715 : 5857 : if (lss == gfc_ss_terminator)
11716 : : return NULL_TREE;
11717 : :
11718 : 5857 : linfo = &lss->info->data.array;
11719 : :
11720 : : /* Find an ss for the rhs. For operator expressions, we see the
11721 : : ss's for the operands. Any one of these will do. */
11722 : 5857 : rss = loop->ss;
11723 : 6364 : for (; rss && rss != gfc_ss_terminator; rss = rss->loop_chain)
11724 : 6364 : if (rss->info->expr != expr1 && rss != loop->temp_ss)
11725 : : break;
11726 : :
11727 : 5857 : if (expr2 && rss == gfc_ss_terminator)
11728 : : return NULL_TREE;
11729 : :
11730 : : /* Ensure that the string length from the current scope is used. */
11731 : 5857 : if (expr2->ts.type == BT_CHARACTER
11732 : 879 : && expr2->expr_type == EXPR_FUNCTION
11733 : 130 : && !expr2->value.function.isym)
11734 : 21 : expr2->ts.u.cl->backend_decl = rss->info->string_length;
11735 : :
11736 : : /* Since the lhs is allocatable, this must be a descriptor type.
11737 : : Get the data and array size. */
11738 : 5857 : desc = linfo->descriptor;
11739 : 5857 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
11740 : 5857 : array1 = gfc_conv_descriptor_data_get (desc);
11741 : :
11742 : : /* If the data is null, set the descriptor bounds and offset. This suppresses
11743 : : the maybe used uninitialized warning. Note that the always false variable
11744 : : prevents this block from ever being executed, and makes sure that the
11745 : : optimizers are able to remove it. Component references are not subject to
11746 : : the warnings, so we don't uselessly complicate the generated code for them.
11747 : : */
11748 : 10677 : for (ref = expr1->ref; ref; ref = ref->next)
11749 : 6052 : if (ref->type == REF_COMPONENT)
11750 : : break;
11751 : :
11752 : 5857 : if (!ref)
11753 : : {
11754 : 4625 : stmtblock_t unalloc_init_block;
11755 : 4625 : gfc_init_block (&unalloc_init_block);
11756 : 4625 : tree guard = gfc_create_var (logical_type_node, "unallocated_init_guard");
11757 : 4625 : gfc_add_modify (&unalloc_init_block, guard, logical_false_node);
11758 : :
11759 : 4625 : gfc_start_block (&loop_pre_block);
11760 : 16702 : for (n = 0; n < expr1->rank; n++)
11761 : : {
11762 : 7452 : gfc_conv_descriptor_lbound_set (&loop_pre_block, desc,
11763 : : gfc_rank_cst[n],
11764 : : gfc_index_one_node);
11765 : 7452 : gfc_conv_descriptor_ubound_set (&loop_pre_block, desc,
11766 : : gfc_rank_cst[n],
11767 : : gfc_index_zero_node);
11768 : 7452 : gfc_conv_descriptor_stride_set (&loop_pre_block, desc,
11769 : : gfc_rank_cst[n],
11770 : : gfc_index_zero_node);
11771 : : }
11772 : :
11773 : 4625 : gfc_conv_descriptor_offset_set (&loop_pre_block, desc,
11774 : : gfc_index_zero_node);
11775 : :
11776 : 4625 : tmp = fold_build2_loc (input_location, EQ_EXPR,
11777 : : logical_type_node, array1,
11778 : 4625 : build_int_cst (TREE_TYPE (array1), 0));
11779 : 4625 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
11780 : : logical_type_node, tmp, guard);
11781 : 4625 : tmp = build3_v (COND_EXPR, tmp,
11782 : : gfc_finish_block (&loop_pre_block),
11783 : : build_empty_stmt (input_location));
11784 : 4625 : gfc_prepend_expr_to_block (&loop->pre, tmp);
11785 : 4625 : gfc_prepend_expr_to_block (&loop->pre,
11786 : : gfc_finish_block (&unalloc_init_block));
11787 : : }
11788 : :
11789 : 5857 : gfc_start_block (&fblock);
11790 : :
11791 : 5857 : if (expr2)
11792 : 5857 : desc2 = rss->info->data.array.descriptor;
11793 : : else
11794 : : desc2 = NULL_TREE;
11795 : :
11796 : : /* Get the old lhs element size for deferred character and class expr1. */
11797 : 5857 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11798 : : {
11799 : 579 : if (expr1->ts.u.cl->backend_decl
11800 : 579 : && VAR_P (expr1->ts.u.cl->backend_decl))
11801 : : elemsize1 = expr1->ts.u.cl->backend_decl;
11802 : : else
11803 : 64 : elemsize1 = lss->info->string_length;
11804 : 579 : tree unit_size = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
11805 : 1158 : elemsize1 = fold_build2_loc (input_location, MULT_EXPR,
11806 : 579 : TREE_TYPE (elemsize1), elemsize1,
11807 : 579 : fold_convert (TREE_TYPE (elemsize1), unit_size));
11808 : :
11809 : 579 : }
11810 : 5278 : else if (expr1->ts.type == BT_CLASS)
11811 : : {
11812 : : /* Unfortunately, the lhs vptr is set too early in many cases.
11813 : : Play it safe by using the descriptor element length. */
11814 : 561 : tmp = gfc_conv_descriptor_elem_len (desc);
11815 : 561 : elemsize1 = fold_convert (gfc_array_index_type, tmp);
11816 : : }
11817 : : else
11818 : : elemsize1 = NULL_TREE;
11819 : 1140 : if (elemsize1 != NULL_TREE)
11820 : 1140 : elemsize1 = gfc_evaluate_now (elemsize1, &fblock);
11821 : :
11822 : : /* Get the new lhs size in bytes. */
11823 : 5857 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11824 : : {
11825 : 579 : if (expr2->ts.deferred)
11826 : : {
11827 : 183 : if (expr2->ts.u.cl->backend_decl
11828 : 183 : && VAR_P (expr2->ts.u.cl->backend_decl))
11829 : : tmp = expr2->ts.u.cl->backend_decl;
11830 : : else
11831 : 0 : tmp = rss->info->string_length;
11832 : : }
11833 : : else
11834 : : {
11835 : 396 : tmp = expr2->ts.u.cl->backend_decl;
11836 : 396 : if (!tmp && expr2->expr_type == EXPR_OP
11837 : 25 : && expr2->value.op.op == INTRINSIC_CONCAT)
11838 : : {
11839 : 25 : tmp = concat_str_length (expr2);
11840 : 25 : expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
11841 : : }
11842 : 12 : else if (!tmp && expr2->ts.u.cl->length)
11843 : : {
11844 : 12 : gfc_se tmpse;
11845 : 12 : gfc_init_se (&tmpse, NULL);
11846 : 12 : gfc_conv_expr_type (&tmpse, expr2->ts.u.cl->length,
11847 : : gfc_charlen_type_node);
11848 : 12 : tmp = tmpse.expr;
11849 : 12 : expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
11850 : : }
11851 : 396 : tmp = fold_convert (TREE_TYPE (expr1->ts.u.cl->backend_decl), tmp);
11852 : : }
11853 : :
11854 : 579 : if (expr1->ts.u.cl->backend_decl
11855 : 579 : && VAR_P (expr1->ts.u.cl->backend_decl))
11856 : 515 : gfc_add_modify (&fblock, expr1->ts.u.cl->backend_decl, tmp);
11857 : : else
11858 : 64 : gfc_add_modify (&fblock, lss->info->string_length, tmp);
11859 : :
11860 : 579 : if (expr1->ts.kind > 1)
11861 : 12 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11862 : 6 : TREE_TYPE (tmp),
11863 : 6 : tmp, build_int_cst (TREE_TYPE (tmp),
11864 : 6 : expr1->ts.kind));
11865 : : }
11866 : 5278 : else if (expr1->ts.type == BT_CHARACTER && expr1->ts.u.cl->backend_decl)
11867 : : {
11868 : 251 : tmp = TYPE_SIZE_UNIT (TREE_TYPE (gfc_typenode_for_spec (&expr1->ts)));
11869 : 251 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
11870 : : fold_convert (gfc_array_index_type, tmp),
11871 : 251 : expr1->ts.u.cl->backend_decl);
11872 : : }
11873 : 5027 : else if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
11874 : 116 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
11875 : 4911 : else if (expr1->ts.type == BT_CLASS && expr2->ts.type == BT_CLASS)
11876 : : {
11877 : 268 : tmp = expr2->rank ? gfc_get_class_from_expr (desc2) : NULL_TREE;
11878 : 268 : if (tmp == NULL_TREE && expr2->expr_type == EXPR_VARIABLE)
11879 : 24 : tmp = class_expr2 = gfc_get_class_from_gfc_expr (expr2);
11880 : :
11881 : 31 : if (tmp != NULL_TREE)
11882 : 261 : tmp = gfc_class_vtab_size_get (tmp);
11883 : : else
11884 : 7 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
11885 : : }
11886 : : else
11887 : 4643 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
11888 : 5857 : elemsize2 = fold_convert (gfc_array_index_type, tmp);
11889 : 5857 : elemsize2 = gfc_evaluate_now (elemsize2, &fblock);
11890 : :
11891 : : /* 7.4.1.3 "If variable is an allocated allocatable variable, it is
11892 : : deallocated if expr is an array of different shape or any of the
11893 : : corresponding length type parameter values of variable and expr
11894 : : differ." This assures F95 compatibility. */
11895 : 5857 : jump_label1 = gfc_build_label_decl (NULL_TREE);
11896 : 5857 : jump_label2 = gfc_build_label_decl (NULL_TREE);
11897 : :
11898 : : /* Allocate if data is NULL. */
11899 : 5857 : cond_null = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11900 : 5857 : array1, build_int_cst (TREE_TYPE (array1), 0));
11901 : 5857 : cond_null= gfc_evaluate_now (cond_null, &fblock);
11902 : :
11903 : 5857 : tmp = build3_v (COND_EXPR, cond_null,
11904 : : build1_v (GOTO_EXPR, jump_label1),
11905 : : build_empty_stmt (input_location));
11906 : 5857 : gfc_add_expr_to_block (&fblock, tmp);
11907 : :
11908 : : /* Get arrayspec if expr is a full array. */
11909 : 5857 : if (expr2 && expr2->expr_type == EXPR_FUNCTION
11910 : 2736 : && expr2->value.function.isym
11911 : 2290 : && expr2->value.function.isym->conversion)
11912 : : {
11913 : : /* For conversion functions, take the arg. */
11914 : 243 : gfc_expr *arg = expr2->value.function.actual->expr;
11915 : 243 : as = gfc_get_full_arrayspec_from_expr (arg);
11916 : 243 : }
11917 : : else if (expr2)
11918 : 5614 : as = gfc_get_full_arrayspec_from_expr (expr2);
11919 : : else
11920 : : as = NULL;
11921 : :
11922 : : /* If the lhs shape is not the same as the rhs jump to setting the
11923 : : bounds and doing the reallocation....... */
11924 : 14765 : for (n = 0; n < expr1->rank; n++)
11925 : : {
11926 : : /* Check the shape. */
11927 : 8908 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11928 : 8908 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
11929 : 8908 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11930 : : gfc_array_index_type,
11931 : : loop->to[n], loop->from[n]);
11932 : 8908 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11933 : : gfc_array_index_type,
11934 : : tmp, lbound);
11935 : 8908 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11936 : : gfc_array_index_type,
11937 : : tmp, ubound);
11938 : 8908 : cond = fold_build2_loc (input_location, NE_EXPR,
11939 : : logical_type_node,
11940 : : tmp, gfc_index_zero_node);
11941 : 8908 : tmp = build3_v (COND_EXPR, cond,
11942 : : build1_v (GOTO_EXPR, jump_label1),
11943 : : build_empty_stmt (input_location));
11944 : 8908 : gfc_add_expr_to_block (&fblock, tmp);
11945 : : }
11946 : :
11947 : : /* ...else if the element lengths are not the same also go to
11948 : : setting the bounds and doing the reallocation.... */
11949 : 5857 : if (elemsize1 != NULL_TREE)
11950 : : {
11951 : 1140 : cond = fold_build2_loc (input_location, NE_EXPR,
11952 : : logical_type_node,
11953 : : elemsize1, elemsize2);
11954 : 1140 : tmp = build3_v (COND_EXPR, cond,
11955 : : build1_v (GOTO_EXPR, jump_label1),
11956 : : build_empty_stmt (input_location));
11957 : 1140 : gfc_add_expr_to_block (&fblock, tmp);
11958 : : }
11959 : :
11960 : : /* ....else jump past the (re)alloc code. */
11961 : 5857 : tmp = build1_v (GOTO_EXPR, jump_label2);
11962 : 5857 : gfc_add_expr_to_block (&fblock, tmp);
11963 : :
11964 : : /* Add the label to start automatic (re)allocation. */
11965 : 5857 : tmp = build1_v (LABEL_EXPR, jump_label1);
11966 : 5857 : gfc_add_expr_to_block (&fblock, tmp);
11967 : :
11968 : : /* Get the rhs size and fix it. */
11969 : 5857 : size2 = gfc_index_one_node;
11970 : 14765 : for (n = 0; n < expr2->rank; n++)
11971 : : {
11972 : 8908 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11973 : : gfc_array_index_type,
11974 : : loop->to[n], loop->from[n]);
11975 : 8908 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11976 : : gfc_array_index_type,
11977 : : tmp, gfc_index_one_node);
11978 : 8908 : size2 = fold_build2_loc (input_location, MULT_EXPR,
11979 : : gfc_array_index_type,
11980 : : tmp, size2);
11981 : : }
11982 : 5857 : size2 = gfc_evaluate_now (size2, &fblock);
11983 : :
11984 : : /* Deallocation of allocatable components will have to occur on
11985 : : reallocation. Fix the old descriptor now. */
11986 : 5857 : if ((expr1->ts.type == BT_DERIVED)
11987 : 276 : && expr1->ts.u.derived->attr.alloc_comp)
11988 : 103 : old_desc = gfc_evaluate_now (desc, &fblock);
11989 : : else
11990 : : old_desc = NULL_TREE;
11991 : :
11992 : : /* Now modify the lhs descriptor and the associated scalarizer
11993 : : variables. F2003 7.4.1.3: "If variable is or becomes an
11994 : : unallocated allocatable variable, then it is allocated with each
11995 : : deferred type parameter equal to the corresponding type parameters
11996 : : of expr , with the shape of expr , and with each lower bound equal
11997 : : to the corresponding element of LBOUND(expr)."
11998 : : Reuse size1 to keep a dimension-by-dimension track of the
11999 : : stride of the new array. */
12000 : 5857 : size1 = gfc_index_one_node;
12001 : 5857 : offset = gfc_index_zero_node;
12002 : :
12003 : 14765 : for (n = 0; n < expr2->rank; n++)
12004 : : {
12005 : 8908 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
12006 : : gfc_array_index_type,
12007 : : loop->to[n], loop->from[n]);
12008 : 8908 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
12009 : : gfc_array_index_type,
12010 : : tmp, gfc_index_one_node);
12011 : :
12012 : 8908 : lbound = gfc_index_one_node;
12013 : 8908 : ubound = tmp;
12014 : :
12015 : 8908 : if (as)
12016 : : {
12017 : 1954 : lbd = get_std_lbound (expr2, desc2, n,
12018 : 977 : as->type == AS_ASSUMED_SIZE);
12019 : 977 : ubound = fold_build2_loc (input_location,
12020 : : MINUS_EXPR,
12021 : : gfc_array_index_type,
12022 : : ubound, lbound);
12023 : 977 : ubound = fold_build2_loc (input_location,
12024 : : PLUS_EXPR,
12025 : : gfc_array_index_type,
12026 : : ubound, lbd);
12027 : 977 : lbound = lbd;
12028 : : }
12029 : :
12030 : 8908 : gfc_conv_descriptor_lbound_set (&fblock, desc,
12031 : : gfc_rank_cst[n],
12032 : : lbound);
12033 : 8908 : gfc_conv_descriptor_ubound_set (&fblock, desc,
12034 : : gfc_rank_cst[n],
12035 : : ubound);
12036 : 8908 : gfc_conv_descriptor_stride_set (&fblock, desc,
12037 : : gfc_rank_cst[n],
12038 : : size1);
12039 : 8908 : lbound = gfc_conv_descriptor_lbound_get (desc,
12040 : : gfc_rank_cst[n]);
12041 : 8908 : tmp2 = fold_build2_loc (input_location, MULT_EXPR,
12042 : : gfc_array_index_type,
12043 : : lbound, size1);
12044 : 8908 : offset = fold_build2_loc (input_location, MINUS_EXPR,
12045 : : gfc_array_index_type,
12046 : : offset, tmp2);
12047 : 8908 : size1 = fold_build2_loc (input_location, MULT_EXPR,
12048 : : gfc_array_index_type,
12049 : : tmp, size1);
12050 : : }
12051 : :
12052 : : /* Set the lhs descriptor and scalarizer offsets. For rank > 1,
12053 : : the array offset is saved and the info.offset is used for a
12054 : : running offset. Use the saved_offset instead. */
12055 : 5857 : tmp = gfc_conv_descriptor_offset (desc);
12056 : 5857 : gfc_add_modify (&fblock, tmp, offset);
12057 : :
12058 : : /* Take into account _len of unlimited polymorphic entities, so that span
12059 : : for array descriptors and allocation sizes are computed correctly. */
12060 : 5857 : if (UNLIMITED_POLY (expr2))
12061 : : {
12062 : 92 : tree len = gfc_class_len_get (TREE_OPERAND (desc2, 0));
12063 : 92 : len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
12064 : : fold_convert (size_type_node, len),
12065 : : size_one_node);
12066 : 92 : elemsize2 = fold_build2_loc (input_location, MULT_EXPR,
12067 : : gfc_array_index_type, elemsize2,
12068 : : fold_convert (gfc_array_index_type, len));
12069 : : }
12070 : :
12071 : 5857 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
12072 : 5857 : gfc_conv_descriptor_span_set (&fblock, desc, elemsize2);
12073 : :
12074 : 5857 : size2 = fold_build2_loc (input_location, MULT_EXPR,
12075 : : gfc_array_index_type,
12076 : : elemsize2, size2);
12077 : 5857 : size2 = fold_convert (size_type_node, size2);
12078 : 5857 : size2 = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
12079 : : size2, size_one_node);
12080 : 5857 : size2 = gfc_evaluate_now (size2, &fblock);
12081 : :
12082 : : /* For deferred character length, the 'size' field of the dtype might
12083 : : have changed so set the dtype. */
12084 : 5857 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
12085 : 5857 : && expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12086 : : {
12087 : 579 : tree type;
12088 : 579 : tmp = gfc_conv_descriptor_dtype (desc);
12089 : 579 : if (expr2->ts.u.cl->backend_decl)
12090 : 579 : type = gfc_typenode_for_spec (&expr2->ts);
12091 : : else
12092 : 0 : type = gfc_typenode_for_spec (&expr1->ts);
12093 : :
12094 : 579 : gfc_add_modify (&fblock, tmp,
12095 : : gfc_get_dtype_rank_type (expr1->rank,type));
12096 : : }
12097 : 5278 : else if (expr1->ts.type == BT_CLASS)
12098 : : {
12099 : 561 : tree type;
12100 : 561 : tmp = gfc_conv_descriptor_dtype (desc);
12101 : :
12102 : 561 : if (expr2->ts.type != BT_CLASS)
12103 : 293 : type = gfc_typenode_for_spec (&expr2->ts);
12104 : : else
12105 : 268 : type = gfc_get_character_type_len (1, elemsize2);
12106 : :
12107 : 561 : gfc_add_modify (&fblock, tmp,
12108 : : gfc_get_dtype_rank_type (expr2->rank,type));
12109 : : /* Set the _len field as well... */
12110 : 561 : if (UNLIMITED_POLY (expr1))
12111 : : {
12112 : 208 : tmp = gfc_class_len_get (TREE_OPERAND (desc, 0));
12113 : 208 : if (expr2->ts.type == BT_CHARACTER)
12114 : 49 : gfc_add_modify (&fblock, tmp,
12115 : 49 : fold_convert (TREE_TYPE (tmp),
12116 : : TYPE_SIZE_UNIT (type)));
12117 : 159 : else if (UNLIMITED_POLY (expr2))
12118 : 92 : gfc_add_modify (&fblock, tmp,
12119 : 92 : gfc_class_len_get (TREE_OPERAND (desc2, 0)));
12120 : : else
12121 : 67 : gfc_add_modify (&fblock, tmp,
12122 : 67 : build_int_cst (TREE_TYPE (tmp), 0));
12123 : : }
12124 : : /* ...and the vptr. */
12125 : 561 : tmp = gfc_class_vptr_get (TREE_OPERAND (desc, 0));
12126 : 561 : if (expr2->ts.type == BT_CLASS && !VAR_P (desc2)
12127 : 261 : && TREE_CODE (desc2) == COMPONENT_REF)
12128 : : {
12129 : 237 : tmp2 = gfc_get_class_from_expr (desc2);
12130 : 237 : tmp2 = gfc_class_vptr_get (tmp2);
12131 : : }
12132 : 324 : else if (expr2->ts.type == BT_CLASS && class_expr2 != NULL_TREE)
12133 : 24 : tmp2 = gfc_class_vptr_get (class_expr2);
12134 : : else
12135 : : {
12136 : 300 : tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
12137 : 300 : tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
12138 : : }
12139 : :
12140 : 561 : gfc_add_modify (&fblock, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
12141 : : }
12142 : 4717 : else if (coarray && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
12143 : : {
12144 : 38 : gfc_add_modify (&fblock, gfc_conv_descriptor_dtype (desc),
12145 : 38 : gfc_get_dtype (TREE_TYPE (desc)));
12146 : : }
12147 : :
12148 : : /* Realloc expression. Note that the scalarizer uses desc.data
12149 : : in the array reference - (*desc.data)[<element>]. */
12150 : 5857 : gfc_init_block (&realloc_block);
12151 : 5857 : gfc_init_se (&caf_se, NULL);
12152 : :
12153 : 5857 : if (coarray)
12154 : : {
12155 : 38 : token = gfc_get_ultimate_alloc_ptr_comps_caf_token (&caf_se, expr1);
12156 : 38 : if (token == NULL_TREE)
12157 : : {
12158 : 8 : tmp = gfc_get_tree_for_caf_expr (expr1);
12159 : 8 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
12160 : 6 : tmp = build_fold_indirect_ref (tmp);
12161 : 8 : gfc_get_caf_token_offset (&caf_se, &token, NULL, tmp, NULL_TREE,
12162 : : expr1);
12163 : 8 : token = gfc_build_addr_expr (NULL_TREE, token);
12164 : : }
12165 : :
12166 : 38 : gfc_add_block_to_block (&realloc_block, &caf_se.pre);
12167 : : }
12168 : 5857 : if ((expr1->ts.type == BT_DERIVED)
12169 : 276 : && expr1->ts.u.derived->attr.alloc_comp)
12170 : : {
12171 : 103 : tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, old_desc,
12172 : : expr1->rank, true);
12173 : 103 : gfc_add_expr_to_block (&realloc_block, tmp);
12174 : : }
12175 : :
12176 : 5857 : if (!coarray)
12177 : : {
12178 : 5819 : tmp = build_call_expr_loc (input_location,
12179 : : builtin_decl_explicit (BUILT_IN_REALLOC), 2,
12180 : : fold_convert (pvoid_type_node, array1),
12181 : : size2);
12182 : 5819 : if (flag_openmp_allocators)
12183 : : {
12184 : 2 : tree cond, omp_tmp;
12185 : 2 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
12186 : : gfc_conv_descriptor_version (desc),
12187 : : build_int_cst (integer_type_node, 1));
12188 : 2 : omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
12189 : 2 : omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
12190 : : fold_convert (pvoid_type_node, array1), size2,
12191 : : build_zero_cst (ptr_type_node),
12192 : : build_zero_cst (ptr_type_node));
12193 : 2 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
12194 : : omp_tmp, tmp);
12195 : : }
12196 : :
12197 : 5819 : gfc_conv_descriptor_data_set (&realloc_block, desc, tmp);
12198 : : }
12199 : : else
12200 : : {
12201 : 38 : tmp = build_call_expr_loc (input_location,
12202 : : gfor_fndecl_caf_deregister, 5, token,
12203 : : build_int_cst (integer_type_node,
12204 : : GFC_CAF_COARRAY_DEALLOCATE_ONLY),
12205 : : null_pointer_node, null_pointer_node,
12206 : : integer_zero_node);
12207 : 38 : gfc_add_expr_to_block (&realloc_block, tmp);
12208 : 38 : tmp = build_call_expr_loc (input_location,
12209 : : gfor_fndecl_caf_register,
12210 : : 7, size2,
12211 : : build_int_cst (integer_type_node,
12212 : : GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY),
12213 : : token, gfc_build_addr_expr (NULL_TREE, desc),
12214 : : null_pointer_node, null_pointer_node,
12215 : : integer_zero_node);
12216 : 38 : gfc_add_expr_to_block (&realloc_block, tmp);
12217 : : }
12218 : :
12219 : 5857 : if ((expr1->ts.type == BT_DERIVED)
12220 : 276 : && expr1->ts.u.derived->attr.alloc_comp)
12221 : : {
12222 : 103 : tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
12223 : : expr1->rank);
12224 : 103 : gfc_add_expr_to_block (&realloc_block, tmp);
12225 : : }
12226 : :
12227 : 5857 : gfc_add_block_to_block (&realloc_block, &caf_se.post);
12228 : 5857 : realloc_expr = gfc_finish_block (&realloc_block);
12229 : :
12230 : : /* Malloc expression. */
12231 : 5857 : gfc_init_block (&alloc_block);
12232 : 5857 : if (!coarray)
12233 : : {
12234 : 5819 : tmp = build_call_expr_loc (input_location,
12235 : : builtin_decl_explicit (BUILT_IN_MALLOC),
12236 : : 1, size2);
12237 : 5819 : gfc_conv_descriptor_data_set (&alloc_block,
12238 : : desc, tmp);
12239 : : }
12240 : : else
12241 : : {
12242 : 38 : tmp = build_call_expr_loc (input_location,
12243 : : gfor_fndecl_caf_register,
12244 : : 7, size2,
12245 : : build_int_cst (integer_type_node,
12246 : : GFC_CAF_COARRAY_ALLOC),
12247 : : token, gfc_build_addr_expr (NULL_TREE, desc),
12248 : : null_pointer_node, null_pointer_node,
12249 : : integer_zero_node);
12250 : 38 : gfc_add_expr_to_block (&alloc_block, tmp);
12251 : : }
12252 : :
12253 : :
12254 : : /* We already set the dtype in the case of deferred character
12255 : : length arrays and class lvalues. */
12256 : 5857 : if (!(GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
12257 : 5857 : && ((expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12258 : 5278 : || coarray))
12259 : 11097 : && expr1->ts.type != BT_CLASS)
12260 : : {
12261 : 4679 : tmp = gfc_conv_descriptor_dtype (desc);
12262 : 4679 : gfc_add_modify (&alloc_block, tmp, gfc_get_dtype (TREE_TYPE (desc)));
12263 : : }
12264 : :
12265 : 5857 : if ((expr1->ts.type == BT_DERIVED)
12266 : 276 : && expr1->ts.u.derived->attr.alloc_comp)
12267 : : {
12268 : 103 : tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
12269 : : expr1->rank);
12270 : 103 : gfc_add_expr_to_block (&alloc_block, tmp);
12271 : : }
12272 : 5857 : alloc_expr = gfc_finish_block (&alloc_block);
12273 : :
12274 : : /* Malloc if not allocated; realloc otherwise. */
12275 : 5857 : tmp = build3_v (COND_EXPR, cond_null, alloc_expr, realloc_expr);
12276 : 5857 : gfc_add_expr_to_block (&fblock, tmp);
12277 : :
12278 : : /* Add the label for same shape lhs and rhs. */
12279 : 5857 : tmp = build1_v (LABEL_EXPR, jump_label2);
12280 : 5857 : gfc_add_expr_to_block (&fblock, tmp);
12281 : :
12282 : 5857 : tree realloc_code = gfc_finish_block (&fblock);
12283 : :
12284 : 5857 : stmtblock_t result_block;
12285 : 5857 : gfc_init_block (&result_block);
12286 : 5857 : gfc_add_expr_to_block (&result_block, realloc_code);
12287 : 5857 : update_reallocated_descriptor (&result_block, loop);
12288 : :
12289 : 5857 : return gfc_finish_block (&result_block);
12290 : : }
12291 : :
12292 : :
12293 : : /* Initialize class descriptor's TKR information. */
12294 : :
12295 : : void
12296 : 2813 : gfc_trans_class_array (gfc_symbol * sym, gfc_wrapped_block * block)
12297 : : {
12298 : 2813 : tree type, etype;
12299 : 2813 : tree tmp;
12300 : 2813 : tree descriptor;
12301 : 2813 : stmtblock_t init;
12302 : 2813 : int rank;
12303 : :
12304 : : /* Make sure the frontend gets these right. */
12305 : 2813 : gcc_assert (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
12306 : : && (CLASS_DATA (sym)->attr.class_pointer
12307 : : || CLASS_DATA (sym)->attr.allocatable));
12308 : :
12309 : 2813 : gcc_assert (VAR_P (sym->backend_decl)
12310 : : || TREE_CODE (sym->backend_decl) == PARM_DECL);
12311 : :
12312 : 2813 : if (sym->attr.dummy)
12313 : 1408 : return;
12314 : :
12315 : 2813 : descriptor = gfc_class_data_get (sym->backend_decl);
12316 : 2813 : type = TREE_TYPE (descriptor);
12317 : :
12318 : 2813 : if (type == NULL || !GFC_DESCRIPTOR_TYPE_P (type))
12319 : : return;
12320 : :
12321 : 1405 : location_t loc = input_location;
12322 : 1405 : input_location = gfc_get_location (&sym->declared_at);
12323 : 1405 : gfc_init_block (&init);
12324 : :
12325 : 1405 : rank = CLASS_DATA (sym)->as ? (CLASS_DATA (sym)->as->rank) : (0);
12326 : 1405 : gcc_assert (rank>=0);
12327 : 1405 : tmp = gfc_conv_descriptor_dtype (descriptor);
12328 : 1405 : etype = gfc_get_element_type (type);
12329 : 1405 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp), tmp,
12330 : : gfc_get_dtype_rank_type (rank, etype));
12331 : 1405 : gfc_add_expr_to_block (&init, tmp);
12332 : :
12333 : 1405 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
12334 : 1405 : input_location = loc;
12335 : : }
12336 : :
12337 : :
12338 : : /* NULLIFY an allocatable/pointer array on function entry, free it on exit.
12339 : : Do likewise, recursively if necessary, with the allocatable components of
12340 : : derived types. This function is also called for assumed-rank arrays, which
12341 : : are always dummy arguments. */
12342 : :
12343 : : void
12344 : 16727 : gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
12345 : : {
12346 : 16727 : tree type;
12347 : 16727 : tree tmp;
12348 : 16727 : tree descriptor;
12349 : 16727 : stmtblock_t init;
12350 : 16727 : stmtblock_t cleanup;
12351 : 16727 : int rank;
12352 : 16727 : bool sym_has_alloc_comp, has_finalizer;
12353 : :
12354 : 33454 : sym_has_alloc_comp = (sym->ts.type == BT_DERIVED
12355 : 10597 : || sym->ts.type == BT_CLASS)
12356 : 16727 : && sym->ts.u.derived->attr.alloc_comp;
12357 : 16727 : has_finalizer = gfc_may_be_finalized (sym->ts);
12358 : :
12359 : : /* Make sure the frontend gets these right. */
12360 : 16727 : gcc_assert (sym->attr.pointer || sym->attr.allocatable || sym_has_alloc_comp
12361 : : || has_finalizer
12362 : : || (sym->as->type == AS_ASSUMED_RANK && sym->attr.dummy));
12363 : :
12364 : 16727 : location_t loc = input_location;
12365 : 16727 : input_location = gfc_get_location (&sym->declared_at);
12366 : 16727 : gfc_init_block (&init);
12367 : :
12368 : 16727 : gcc_assert (VAR_P (sym->backend_decl)
12369 : : || TREE_CODE (sym->backend_decl) == PARM_DECL);
12370 : :
12371 : 16727 : if (sym->ts.type == BT_CHARACTER
12372 : 1277 : && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
12373 : : {
12374 : 748 : if (sym->ts.deferred && !sym->ts.u.cl->length && !sym->attr.dummy)
12375 : : {
12376 : 556 : tree len_expr = sym->ts.u.cl->backend_decl;
12377 : 556 : tree init_val = build_zero_cst (TREE_TYPE (len_expr));
12378 : 556 : if (VAR_P (len_expr)
12379 : 556 : && sym->attr.save
12380 : 581 : && !DECL_INITIAL (len_expr))
12381 : 25 : DECL_INITIAL (len_expr) = init_val;
12382 : : else
12383 : 531 : gfc_add_modify (&init, len_expr, init_val);
12384 : : }
12385 : 748 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
12386 : 748 : gfc_trans_vla_type_sizes (sym, &init);
12387 : :
12388 : : /* Presence check of optional deferred-length character dummy. */
12389 : 748 : if (sym->ts.deferred && sym->attr.dummy && sym->attr.optional)
12390 : : {
12391 : 43 : tmp = gfc_finish_block (&init);
12392 : 43 : tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
12393 : : tmp, build_empty_stmt (input_location));
12394 : 43 : gfc_add_expr_to_block (&init, tmp);
12395 : : }
12396 : : }
12397 : :
12398 : : /* Dummy, use associated and result variables don't need anything special. */
12399 : 16727 : if (sym->attr.dummy || sym->attr.use_assoc || sym->attr.result)
12400 : : {
12401 : 705 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
12402 : 705 : input_location = loc;
12403 : 925 : return;
12404 : : }
12405 : :
12406 : 16022 : descriptor = sym->backend_decl;
12407 : :
12408 : : /* Although static, derived types with default initializers and
12409 : : allocatable components must not be nulled wholesale; instead they
12410 : : are treated component by component. */
12411 : 16022 : if (TREE_STATIC (descriptor) && !sym_has_alloc_comp && !has_finalizer)
12412 : : {
12413 : : /* SAVEd variables are not freed on exit. */
12414 : 220 : gfc_trans_static_array_pointer (sym);
12415 : :
12416 : 220 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
12417 : 220 : input_location = loc;
12418 : 220 : return;
12419 : : }
12420 : :
12421 : : /* Get the descriptor type. */
12422 : 15802 : type = TREE_TYPE (sym->backend_decl);
12423 : :
12424 : 15802 : if ((sym_has_alloc_comp || (has_finalizer && sym->ts.type != BT_CLASS))
12425 : 4746 : && !(sym->attr.pointer || sym->attr.allocatable))
12426 : : {
12427 : 2593 : if (!sym->attr.save
12428 : 2227 : && !(TREE_STATIC (sym->backend_decl) && sym->attr.is_main_program))
12429 : : {
12430 : 2227 : if (sym->value == NULL
12431 : 2227 : || !gfc_has_default_initializer (sym->ts.u.derived))
12432 : : {
12433 : 1913 : rank = sym->as ? sym->as->rank : 0;
12434 : 1913 : tmp = gfc_nullify_alloc_comp (sym->ts.u.derived,
12435 : : descriptor, rank);
12436 : 1913 : gfc_add_expr_to_block (&init, tmp);
12437 : : }
12438 : : else
12439 : 314 : gfc_init_default_dt (sym, &init, false);
12440 : : }
12441 : : }
12442 : 13209 : else if (!GFC_DESCRIPTOR_TYPE_P (type))
12443 : : {
12444 : : /* If the backend_decl is not a descriptor, we must have a pointer
12445 : : to one. */
12446 : 1668 : descriptor = build_fold_indirect_ref_loc (input_location,
12447 : : sym->backend_decl);
12448 : 1668 : type = TREE_TYPE (descriptor);
12449 : : }
12450 : :
12451 : : /* NULLIFY the data pointer for non-saved allocatables, or for non-saved
12452 : : pointers when -fcheck=pointer is specified. */
12453 : 27343 : if (GFC_DESCRIPTOR_TYPE_P (type) && !sym->attr.save
12454 : 27330 : && (sym->attr.allocatable
12455 : 3246 : || (sym->attr.pointer && (gfc_option.rtcheck & GFC_RTCHECK_POINTER))))
12456 : : {
12457 : 8325 : gfc_conv_descriptor_data_set (&init, descriptor, null_pointer_node);
12458 : 8325 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
12459 : : {
12460 : : /* Declare the variable static so its array descriptor stays present
12461 : : after leaving the scope. It may still be accessed through another
12462 : : image. This may happen, for example, with the caf_mpi
12463 : : implementation. */
12464 : 104 : TREE_STATIC (descriptor) = 1;
12465 : 104 : tmp = gfc_conv_descriptor_token (descriptor);
12466 : 104 : gfc_add_modify (&init, tmp, fold_convert (TREE_TYPE (tmp),
12467 : : null_pointer_node));
12468 : : }
12469 : : }
12470 : :
12471 : : /* Set initial TKR for pointers and allocatables */
12472 : 15802 : if (GFC_DESCRIPTOR_TYPE_P (type)
12473 : 15802 : && (sym->attr.pointer || sym->attr.allocatable))
12474 : : {
12475 : 11541 : tree etype;
12476 : :
12477 : 11541 : gcc_assert (sym->as && sym->as->rank>=0);
12478 : 11541 : tmp = gfc_conv_descriptor_dtype (descriptor);
12479 : 11541 : etype = gfc_get_element_type (type);
12480 : 11541 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
12481 : 11541 : TREE_TYPE (tmp), tmp,
12482 : 11541 : gfc_get_dtype_rank_type (sym->as->rank, etype));
12483 : 11541 : gfc_add_expr_to_block (&init, tmp);
12484 : : }
12485 : 15802 : input_location = loc;
12486 : 15802 : gfc_init_block (&cleanup);
12487 : :
12488 : : /* Allocatable arrays need to be freed when they go out of scope.
12489 : : The allocatable components of pointers must not be touched. */
12490 : 15802 : if (!sym->attr.allocatable && has_finalizer && sym->ts.type != BT_CLASS
12491 : 486 : && !sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
12492 : 236 : && !sym->ns->proc_name->attr.is_main_program)
12493 : : {
12494 : 198 : gfc_expr *e;
12495 : 198 : sym->attr.referenced = 1;
12496 : 198 : e = gfc_lval_expr_from_sym (sym);
12497 : 198 : gfc_add_finalizer_call (&cleanup, e);
12498 : 198 : gfc_free_expr (e);
12499 : 198 : }
12500 : 15604 : else if ((!sym->attr.allocatable || !has_finalizer)
12501 : 15493 : && sym_has_alloc_comp && !(sym->attr.function || sym->attr.result)
12502 : 4292 : && !sym->attr.pointer && !sym->attr.save
12503 : 2232 : && !(sym->attr.artificial && sym->name[0] == '_')
12504 : 2177 : && !sym->ns->proc_name->attr.is_main_program)
12505 : : {
12506 : 586 : int rank;
12507 : 586 : rank = sym->as ? sym->as->rank : 0;
12508 : 586 : tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank,
12509 : 586 : (sym->attr.codimension
12510 : 2 : && flag_coarray == GFC_FCOARRAY_LIB)
12511 : : ? GFC_STRUCTURE_CAF_MODE_IN_COARRAY
12512 : : : 0);
12513 : 586 : gfc_add_expr_to_block (&cleanup, tmp);
12514 : : }
12515 : :
12516 : 15802 : if (sym->attr.allocatable && (sym->attr.dimension || sym->attr.codimension)
12517 : 8289 : && !sym->attr.save && !sym->attr.result
12518 : 8282 : && !sym->ns->proc_name->attr.is_main_program)
12519 : : {
12520 : 4446 : gfc_expr *e;
12521 : 4446 : e = has_finalizer ? gfc_lval_expr_from_sym (sym) : NULL;
12522 : 8892 : tmp = gfc_deallocate_with_status (sym->backend_decl, NULL_TREE, NULL_TREE,
12523 : : NULL_TREE, NULL_TREE, true, e,
12524 : 4446 : sym->attr.codimension
12525 : : ? GFC_CAF_COARRAY_DEREGISTER
12526 : : : GFC_CAF_COARRAY_NOCOARRAY,
12527 : : NULL_TREE, gfc_finish_block (&cleanup));
12528 : 4446 : if (e)
12529 : 32 : gfc_free_expr (e);
12530 : 4446 : gfc_init_block (&cleanup);
12531 : 4446 : gfc_add_expr_to_block (&cleanup, tmp);
12532 : : }
12533 : :
12534 : 15802 : gfc_add_init_cleanup (block, gfc_finish_block (&init),
12535 : : gfc_finish_block (&cleanup));
12536 : : }
12537 : :
12538 : : /************ Expression Walking Functions ******************/
12539 : :
12540 : : /* Walk a variable reference.
12541 : :
12542 : : Possible extension - multiple component subscripts.
12543 : : x(:,:) = foo%a(:)%b(:)
12544 : : Transforms to
12545 : : forall (i=..., j=...)
12546 : : x(i,j) = foo%a(j)%b(i)
12547 : : end forall
12548 : : This adds a fair amount of complexity because you need to deal with more
12549 : : than one ref. Maybe handle in a similar manner to vector subscripts.
12550 : : Maybe not worth the effort. */
12551 : :
12552 : :
12553 : : static gfc_ss *
12554 : 660742 : gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
12555 : : {
12556 : 660742 : gfc_ref *ref;
12557 : :
12558 : 660742 : gfc_fix_class_refs (expr);
12559 : :
12560 : 768137 : for (ref = expr->ref; ref; ref = ref->next)
12561 : 424220 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
12562 : : break;
12563 : :
12564 : 660742 : return gfc_walk_array_ref (ss, expr, ref);
12565 : : }
12566 : :
12567 : : gfc_ss *
12568 : 660993 : gfc_walk_array_ref (gfc_ss *ss, gfc_expr *expr, gfc_ref *ref, bool array_only)
12569 : : {
12570 : 660993 : gfc_array_ref *ar;
12571 : 660993 : gfc_ss *newss;
12572 : 660993 : int n;
12573 : :
12574 : 985782 : for (; ref; ref = ref->next)
12575 : : {
12576 : 324789 : if (ref->type == REF_SUBSTRING)
12577 : : {
12578 : 1302 : ss = gfc_get_scalar_ss (ss, ref->u.ss.start);
12579 : 1302 : if (ref->u.ss.end)
12580 : 1276 : ss = gfc_get_scalar_ss (ss, ref->u.ss.end);
12581 : : }
12582 : :
12583 : : /* We're only interested in array sections from now on. */
12584 : 324789 : if (ref->type != REF_ARRAY
12585 : 317334 : || (array_only && ref->u.ar.as && ref->u.ar.as->rank == 0))
12586 : 7552 : continue;
12587 : :
12588 : 317237 : ar = &ref->u.ar;
12589 : :
12590 : 317237 : switch (ar->type)
12591 : : {
12592 : 188 : case AR_ELEMENT:
12593 : 423 : for (n = ar->dimen - 1; n >= 0; n--)
12594 : 235 : ss = gfc_get_scalar_ss (ss, ar->start[n]);
12595 : : break;
12596 : :
12597 : 262623 : case AR_FULL:
12598 : : /* Assumed shape arrays from interface mapping need this fix. */
12599 : 262623 : if (!ar->as && expr->symtree->n.sym->as)
12600 : : {
12601 : 6 : ar->as = gfc_get_array_spec();
12602 : 6 : *ar->as = *expr->symtree->n.sym->as;
12603 : : }
12604 : 262623 : newss = gfc_get_array_ss (ss, expr, ar->as->rank, GFC_SS_SECTION);
12605 : 262623 : newss->info->data.array.ref = ref;
12606 : :
12607 : : /* Make sure array is the same as array(:,:), this way
12608 : : we don't need to special case all the time. */
12609 : 262623 : ar->dimen = ar->as->rank;
12610 : 611104 : for (n = 0; n < ar->dimen; n++)
12611 : : {
12612 : 348481 : ar->dimen_type[n] = DIMEN_RANGE;
12613 : :
12614 : 348481 : gcc_assert (ar->start[n] == NULL);
12615 : 348481 : gcc_assert (ar->end[n] == NULL);
12616 : 348481 : gcc_assert (ar->stride[n] == NULL);
12617 : : }
12618 : : ss = newss;
12619 : : break;
12620 : :
12621 : 54426 : case AR_SECTION:
12622 : 54426 : newss = gfc_get_array_ss (ss, expr, 0, GFC_SS_SECTION);
12623 : 54426 : newss->info->data.array.ref = ref;
12624 : :
12625 : : /* We add SS chains for all the subscripts in the section. */
12626 : 140442 : for (n = 0; n < ar->dimen; n++)
12627 : : {
12628 : 86016 : gfc_ss *indexss;
12629 : :
12630 : 86016 : switch (ar->dimen_type[n])
12631 : : {
12632 : 6116 : case DIMEN_ELEMENT:
12633 : : /* Add SS for elemental (scalar) subscripts. */
12634 : 6116 : gcc_assert (ar->start[n]);
12635 : 6116 : indexss = gfc_get_scalar_ss (gfc_ss_terminator, ar->start[n]);
12636 : 6116 : indexss->loop_chain = gfc_ss_terminator;
12637 : 6116 : newss->info->data.array.subscript[n] = indexss;
12638 : 6116 : break;
12639 : :
12640 : 79094 : case DIMEN_RANGE:
12641 : : /* We don't add anything for sections, just remember this
12642 : : dimension for later. */
12643 : 79094 : newss->dim[newss->dimen] = n;
12644 : 79094 : newss->dimen++;
12645 : 79094 : break;
12646 : :
12647 : 806 : case DIMEN_VECTOR:
12648 : : /* Create a GFC_SS_VECTOR index in which we can store
12649 : : the vector's descriptor. */
12650 : 806 : indexss = gfc_get_array_ss (gfc_ss_terminator, ar->start[n],
12651 : : 1, GFC_SS_VECTOR);
12652 : 806 : indexss->loop_chain = gfc_ss_terminator;
12653 : 806 : newss->info->data.array.subscript[n] = indexss;
12654 : 806 : newss->dim[newss->dimen] = n;
12655 : 806 : newss->dimen++;
12656 : 806 : break;
12657 : :
12658 : 0 : default:
12659 : : /* We should know what sort of section it is by now. */
12660 : 0 : gcc_unreachable ();
12661 : : }
12662 : : }
12663 : : /* We should have at least one non-elemental dimension,
12664 : : unless we are creating a descriptor for a (scalar) coarray. */
12665 : 54426 : gcc_assert (newss->dimen > 0
12666 : : || newss->info->data.array.ref->u.ar.as->corank > 0);
12667 : : ss = newss;
12668 : : break;
12669 : :
12670 : 0 : default:
12671 : : /* We should know what sort of section it is by now. */
12672 : 0 : gcc_unreachable ();
12673 : : }
12674 : :
12675 : : }
12676 : 660993 : return ss;
12677 : : }
12678 : :
12679 : :
12680 : : /* Walk an expression operator. If only one operand of a binary expression is
12681 : : scalar, we must also add the scalar term to the SS chain. */
12682 : :
12683 : : static gfc_ss *
12684 : 54591 : gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
12685 : : {
12686 : 54591 : gfc_ss *head;
12687 : 54591 : gfc_ss *head2;
12688 : :
12689 : 54591 : head = gfc_walk_subexpr (ss, expr->value.op.op1);
12690 : 54591 : if (expr->value.op.op2 == NULL)
12691 : : head2 = head;
12692 : : else
12693 : 52640 : head2 = gfc_walk_subexpr (head, expr->value.op.op2);
12694 : :
12695 : : /* All operands are scalar. Pass back and let the caller deal with it. */
12696 : 54591 : if (head2 == ss)
12697 : : return head2;
12698 : :
12699 : : /* All operands require scalarization. */
12700 : 50090 : if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
12701 : : return head2;
12702 : :
12703 : : /* One of the operands needs scalarization, the other is scalar.
12704 : : Create a gfc_ss for the scalar expression. */
12705 : 18731 : if (head == ss)
12706 : : {
12707 : : /* First operand is scalar. We build the chain in reverse order, so
12708 : : add the scalar SS after the second operand. */
12709 : : head = head2;
12710 : 2266 : while (head && head->next != ss)
12711 : : head = head->next;
12712 : : /* Check we haven't somehow broken the chain. */
12713 : 2023 : gcc_assert (head);
12714 : 2023 : head->next = gfc_get_scalar_ss (ss, expr->value.op.op1);
12715 : : }
12716 : : else /* head2 == head */
12717 : : {
12718 : 16708 : gcc_assert (head2 == head);
12719 : : /* Second operand is scalar. */
12720 : 16708 : head2 = gfc_get_scalar_ss (head2, expr->value.op.op2);
12721 : : }
12722 : :
12723 : : return head2;
12724 : : }
12725 : :
12726 : : static gfc_ss *
12727 : 6 : gfc_walk_conditional_expr (gfc_ss *ss, gfc_expr *expr)
12728 : : {
12729 : 6 : gfc_ss *head;
12730 : :
12731 : 6 : head = gfc_walk_subexpr (ss, expr->value.conditional.true_expr);
12732 : 6 : head = gfc_walk_subexpr (head, expr->value.conditional.false_expr);
12733 : 6 : return head;
12734 : : }
12735 : :
12736 : : /* Reverse a SS chain. */
12737 : :
12738 : : gfc_ss *
12739 : 833485 : gfc_reverse_ss (gfc_ss * ss)
12740 : : {
12741 : 833485 : gfc_ss *next;
12742 : 833485 : gfc_ss *head;
12743 : :
12744 : 833485 : gcc_assert (ss != NULL);
12745 : :
12746 : : head = gfc_ss_terminator;
12747 : 1257065 : while (ss != gfc_ss_terminator)
12748 : : {
12749 : 423580 : next = ss->next;
12750 : : /* Check we didn't somehow break the chain. */
12751 : 423580 : gcc_assert (next != NULL);
12752 : 423580 : ss->next = head;
12753 : 423580 : head = ss;
12754 : 423580 : ss = next;
12755 : : }
12756 : :
12757 : 833485 : return (head);
12758 : : }
12759 : :
12760 : :
12761 : : /* Given an expression referring to a procedure, return the symbol of its
12762 : : interface. We can't get the procedure symbol directly as we have to handle
12763 : : the case of (deferred) type-bound procedures. */
12764 : :
12765 : : gfc_symbol *
12766 : 160 : gfc_get_proc_ifc_for_expr (gfc_expr *procedure_ref)
12767 : : {
12768 : 160 : gfc_symbol *sym;
12769 : 160 : gfc_ref *ref;
12770 : :
12771 : 160 : if (procedure_ref == NULL)
12772 : : return NULL;
12773 : :
12774 : : /* Normal procedure case. */
12775 : 160 : if (procedure_ref->expr_type == EXPR_FUNCTION
12776 : 160 : && procedure_ref->value.function.esym)
12777 : : sym = procedure_ref->value.function.esym;
12778 : : else
12779 : 24 : sym = procedure_ref->symtree->n.sym;
12780 : :
12781 : : /* Typebound procedure case. */
12782 : 208 : for (ref = procedure_ref->ref; ref; ref = ref->next)
12783 : : {
12784 : 48 : if (ref->type == REF_COMPONENT
12785 : 48 : && ref->u.c.component->attr.proc_pointer)
12786 : 24 : sym = ref->u.c.component->ts.interface;
12787 : : else
12788 : : sym = NULL;
12789 : : }
12790 : :
12791 : : return sym;
12792 : : }
12793 : :
12794 : :
12795 : : /* Given an expression referring to an intrinsic function call,
12796 : : return the intrinsic symbol. */
12797 : :
12798 : : gfc_intrinsic_sym *
12799 : 7771 : gfc_get_intrinsic_for_expr (gfc_expr *call)
12800 : : {
12801 : 7771 : if (call == NULL)
12802 : : return NULL;
12803 : :
12804 : : /* Normal procedure case. */
12805 : 2341 : if (call->expr_type == EXPR_FUNCTION)
12806 : 2235 : return call->value.function.isym;
12807 : : else
12808 : : return NULL;
12809 : : }
12810 : :
12811 : :
12812 : : /* Indicates whether an argument to an intrinsic function should be used in
12813 : : scalarization. It is usually the case, except for some intrinsics
12814 : : requiring the value to be constant, and using the value at compile time only.
12815 : : As the value is not used at runtime in those cases, we don’t produce code
12816 : : for it, and it should not be visible to the scalarizer.
12817 : : FUNCTION is the intrinsic function being called, ACTUAL_ARG is the actual
12818 : : argument being examined in that call, and ARG_NUM the index number
12819 : : of ACTUAL_ARG in the list of arguments.
12820 : : The intrinsic procedure’s dummy argument associated with ACTUAL_ARG is
12821 : : identified using the name in ACTUAL_ARG if it is present (that is: if it’s
12822 : : a keyword argument), otherwise using ARG_NUM. */
12823 : :
12824 : : static bool
12825 : 38027 : arg_evaluated_for_scalarization (gfc_intrinsic_sym *function,
12826 : : gfc_dummy_arg *dummy_arg)
12827 : : {
12828 : 38027 : if (function != NULL && dummy_arg != NULL)
12829 : : {
12830 : 12369 : switch (function->id)
12831 : : {
12832 : 241 : case GFC_ISYM_INDEX:
12833 : 241 : case GFC_ISYM_LEN_TRIM:
12834 : 241 : case GFC_ISYM_MASKL:
12835 : 241 : case GFC_ISYM_MASKR:
12836 : 241 : case GFC_ISYM_SCAN:
12837 : 241 : case GFC_ISYM_VERIFY:
12838 : 241 : if (strcmp ("kind", gfc_dummy_arg_get_name (*dummy_arg)) == 0)
12839 : : return false;
12840 : : /* Fallthrough. */
12841 : :
12842 : : default:
12843 : : break;
12844 : : }
12845 : : }
12846 : :
12847 : : return true;
12848 : : }
12849 : :
12850 : :
12851 : : /* Walk the arguments of an elemental function.
12852 : : PROC_EXPR is used to check whether an argument is permitted to be absent. If
12853 : : it is NULL, we don't do the check and the argument is assumed to be present.
12854 : : */
12855 : :
12856 : : gfc_ss *
12857 : 27243 : gfc_walk_elemental_function_args (gfc_ss * ss, gfc_actual_arglist *arg,
12858 : : gfc_intrinsic_sym *intrinsic_sym,
12859 : : gfc_ss_type type)
12860 : : {
12861 : 27243 : int scalar;
12862 : 27243 : gfc_ss *head;
12863 : 27243 : gfc_ss *tail;
12864 : 27243 : gfc_ss *newss;
12865 : :
12866 : 27243 : head = gfc_ss_terminator;
12867 : 27243 : tail = NULL;
12868 : :
12869 : 27243 : scalar = 1;
12870 : 66732 : for (; arg; arg = arg->next)
12871 : : {
12872 : 39489 : gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
12873 : 40984 : if (!arg->expr
12874 : 38177 : || arg->expr->expr_type == EXPR_NULL
12875 : 77516 : || !arg_evaluated_for_scalarization (intrinsic_sym, dummy_arg))
12876 : 1495 : continue;
12877 : :
12878 : 37994 : newss = gfc_walk_subexpr (head, arg->expr);
12879 : 37994 : if (newss == head)
12880 : : {
12881 : : /* Scalar argument. */
12882 : 18432 : gcc_assert (type == GFC_SS_SCALAR || type == GFC_SS_REFERENCE);
12883 : 18432 : newss = gfc_get_scalar_ss (head, arg->expr);
12884 : 18432 : newss->info->type = type;
12885 : 18432 : if (dummy_arg)
12886 : 15448 : newss->info->data.scalar.dummy_arg = dummy_arg;
12887 : : }
12888 : : else
12889 : : scalar = 0;
12890 : :
12891 : 35010 : if (dummy_arg != NULL
12892 : 25828 : && gfc_dummy_arg_is_optional (*dummy_arg)
12893 : 2538 : && arg->expr->expr_type == EXPR_VARIABLE
12894 : 36714 : && (gfc_expr_attr (arg->expr).optional
12895 : 1223 : || gfc_expr_attr (arg->expr).allocatable
12896 : 37941 : || gfc_expr_attr (arg->expr).pointer))
12897 : 1005 : newss->info->can_be_null_ref = true;
12898 : :
12899 : 37994 : head = newss;
12900 : 37994 : if (!tail)
12901 : : {
12902 : : tail = head;
12903 : 33839 : while (tail->next != gfc_ss_terminator)
12904 : : tail = tail->next;
12905 : : }
12906 : : }
12907 : :
12908 : 27243 : if (scalar)
12909 : : {
12910 : : /* If all the arguments are scalar we don't need the argument SS. */
12911 : 10219 : gfc_free_ss_chain (head);
12912 : : /* Pass it back. */
12913 : 10219 : return ss;
12914 : : }
12915 : :
12916 : : /* Add it onto the existing chain. */
12917 : 17024 : tail->next = ss;
12918 : 17024 : return head;
12919 : : }
12920 : :
12921 : :
12922 : : /* Walk a function call. Scalar functions are passed back, and taken out of
12923 : : scalarization loops. For elemental functions we walk their arguments.
12924 : : The result of functions returning arrays is stored in a temporary outside
12925 : : the loop, so that the function is only called once. Hence we do not need
12926 : : to walk their arguments. */
12927 : :
12928 : : static gfc_ss *
12929 : 63586 : gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
12930 : : {
12931 : 63586 : gfc_intrinsic_sym *isym;
12932 : 63586 : gfc_symbol *sym;
12933 : 63586 : gfc_component *comp = NULL;
12934 : :
12935 : 63586 : isym = expr->value.function.isym;
12936 : :
12937 : : /* Handle intrinsic functions separately. */
12938 : 63586 : if (isym)
12939 : 55977 : return gfc_walk_intrinsic_function (ss, expr, isym);
12940 : :
12941 : 7609 : sym = expr->value.function.esym;
12942 : 7609 : if (!sym)
12943 : 546 : sym = expr->symtree->n.sym;
12944 : :
12945 : 7609 : if (gfc_is_class_array_function (expr))
12946 : 234 : return gfc_get_array_ss (ss, expr,
12947 : 234 : CLASS_DATA (expr->value.function.esym->result)->as->rank,
12948 : 234 : GFC_SS_FUNCTION);
12949 : :
12950 : : /* A function that returns arrays. */
12951 : 7375 : comp = gfc_get_proc_ptr_comp (expr);
12952 : 6977 : if ((!comp && gfc_return_by_reference (sym) && sym->result->attr.dimension)
12953 : 7375 : || (comp && comp->attr.dimension))
12954 : 2606 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
12955 : :
12956 : : /* Walk the parameters of an elemental function. For now we always pass
12957 : : by reference. */
12958 : 4769 : if (sym->attr.elemental || (comp && comp->attr.elemental))
12959 : : {
12960 : 2199 : gfc_ss *old_ss = ss;
12961 : :
12962 : 2199 : ss = gfc_walk_elemental_function_args (old_ss,
12963 : : expr->value.function.actual,
12964 : : gfc_get_intrinsic_for_expr (expr),
12965 : : GFC_SS_REFERENCE);
12966 : 2199 : if (ss != old_ss
12967 : 1163 : && (comp
12968 : 1102 : || sym->attr.proc_pointer
12969 : 1102 : || sym->attr.if_source != IFSRC_DECL
12970 : 987 : || sym->attr.array_outer_dependency))
12971 : 224 : ss->info->array_outer_dependency = 1;
12972 : : }
12973 : :
12974 : : /* Scalar functions are OK as these are evaluated outside the scalarization
12975 : : loop. Pass back and let the caller deal with it. */
12976 : : return ss;
12977 : : }
12978 : :
12979 : :
12980 : : /* An array temporary is constructed for array constructors. */
12981 : :
12982 : : static gfc_ss *
12983 : 49074 : gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
12984 : : {
12985 : 0 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_CONSTRUCTOR);
12986 : : }
12987 : :
12988 : :
12989 : : /* Walk an expression. Add walked expressions to the head of the SS chain.
12990 : : A wholly scalar expression will not be added. */
12991 : :
12992 : : gfc_ss *
12993 : 983170 : gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
12994 : : {
12995 : 983170 : gfc_ss *head;
12996 : :
12997 : 983170 : switch (expr->expr_type)
12998 : : {
12999 : 660742 : case EXPR_VARIABLE:
13000 : 660742 : head = gfc_walk_variable_expr (ss, expr);
13001 : 660742 : return head;
13002 : :
13003 : 54591 : case EXPR_OP:
13004 : 54591 : head = gfc_walk_op_expr (ss, expr);
13005 : 54591 : return head;
13006 : :
13007 : 6 : case EXPR_CONDITIONAL:
13008 : 6 : head = gfc_walk_conditional_expr (ss, expr);
13009 : 6 : return head;
13010 : :
13011 : 63586 : case EXPR_FUNCTION:
13012 : 63586 : head = gfc_walk_function_expr (ss, expr);
13013 : 63586 : return head;
13014 : :
13015 : : case EXPR_CONSTANT:
13016 : : case EXPR_NULL:
13017 : : case EXPR_STRUCTURE:
13018 : : /* Pass back and let the caller deal with it. */
13019 : : break;
13020 : :
13021 : 49074 : case EXPR_ARRAY:
13022 : 49074 : head = gfc_walk_array_constructor (ss, expr);
13023 : 49074 : return head;
13024 : :
13025 : : case EXPR_SUBSTRING:
13026 : : /* Pass back and let the caller deal with it. */
13027 : : break;
13028 : :
13029 : 0 : default:
13030 : 0 : gfc_internal_error ("bad expression type during walk (%d)",
13031 : : expr->expr_type);
13032 : : }
13033 : : return ss;
13034 : : }
13035 : :
13036 : :
13037 : : /* Entry point for expression walking.
13038 : : A return value equal to the passed chain means this is
13039 : : a scalar expression. It is up to the caller to take whatever action is
13040 : : necessary to translate these. */
13041 : :
13042 : : gfc_ss *
13043 : 831018 : gfc_walk_expr (gfc_expr * expr)
13044 : : {
13045 : 831018 : gfc_ss *res;
13046 : :
13047 : 831018 : res = gfc_walk_subexpr (gfc_ss_terminator, expr);
13048 : 831018 : return gfc_reverse_ss (res);
13049 : : }
|