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 : 57810 : gfc_array_dataptr_type (tree desc)
105 : : {
106 : 57810 : 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 : 1923853 : gfc_get_descriptor_field (tree desc, unsigned field_idx)
246 : : {
247 : 1923853 : tree type = TREE_TYPE (desc);
248 : 1923853 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
249 : :
250 : 1923853 : tree field = gfc_advance_chain (TYPE_FIELDS (type), field_idx);
251 : 1923853 : gcc_assert (field != NULL_TREE);
252 : :
253 : 1923853 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
254 : 1923853 : 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 : 264130 : gfc_conv_descriptor_data_get (tree desc)
262 : : {
263 : 264130 : tree type = TREE_TYPE (desc);
264 : 264130 : if (TREE_CODE (type) == REFERENCE_TYPE)
265 : 0 : gcc_unreachable ();
266 : :
267 : 264130 : tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
268 : 264130 : 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 : 145442 : gfc_conv_descriptor_data_set (stmtblock_t *block, tree desc, tree value)
281 : : {
282 : 145442 : tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
283 : 145442 : gfc_add_modify (block, field, fold_convert (TREE_TYPE (field), value));
284 : 145442 : }
285 : :
286 : :
287 : : /* This provides address access to the data field. This should only be
288 : : used by array allocation, passing this on to the runtime. */
289 : :
290 : : tree
291 : 1066 : gfc_conv_descriptor_data_addr (tree desc)
292 : : {
293 : 1066 : tree field = gfc_get_descriptor_field (desc, DATA_FIELD);
294 : 1066 : return gfc_build_addr_expr (NULL_TREE, field);
295 : : }
296 : :
297 : : static tree
298 : 196017 : gfc_conv_descriptor_offset (tree desc)
299 : : {
300 : 196017 : tree field = gfc_get_descriptor_field (desc, OFFSET_FIELD);
301 : 196017 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
302 : 196017 : return field;
303 : : }
304 : :
305 : : tree
306 : 71676 : gfc_conv_descriptor_offset_get (tree desc)
307 : : {
308 : 71676 : return gfc_conv_descriptor_offset (desc);
309 : : }
310 : :
311 : : void
312 : 116143 : gfc_conv_descriptor_offset_set (stmtblock_t *block, tree desc,
313 : : tree value)
314 : : {
315 : 116143 : tree t = gfc_conv_descriptor_offset (desc);
316 : 116143 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
317 : 116143 : }
318 : :
319 : :
320 : : tree
321 : 166538 : gfc_conv_descriptor_dtype (tree desc)
322 : : {
323 : 166538 : tree field = gfc_get_descriptor_field (desc, DTYPE_FIELD);
324 : 166538 : gcc_assert (TREE_TYPE (field) == get_dtype_type_node ());
325 : 166538 : return field;
326 : : }
327 : :
328 : : static tree
329 : 153397 : gfc_conv_descriptor_span (tree desc)
330 : : {
331 : 153397 : tree field = gfc_get_descriptor_field (desc, SPAN_FIELD);
332 : 153397 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
333 : 153397 : return field;
334 : : }
335 : :
336 : : tree
337 : 32661 : gfc_conv_descriptor_span_get (tree desc)
338 : : {
339 : 32661 : return gfc_conv_descriptor_span (desc);
340 : : }
341 : :
342 : : void
343 : 120736 : gfc_conv_descriptor_span_set (stmtblock_t *block, tree desc,
344 : : tree value)
345 : : {
346 : 120736 : tree t = gfc_conv_descriptor_span (desc);
347 : 120736 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
348 : 120736 : }
349 : :
350 : :
351 : : tree
352 : 19113 : gfc_conv_descriptor_rank (tree desc)
353 : : {
354 : 19113 : tree tmp;
355 : 19113 : tree dtype;
356 : :
357 : 19113 : dtype = gfc_conv_descriptor_dtype (desc);
358 : 19113 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_RANK);
359 : 19113 : gcc_assert (tmp != NULL_TREE
360 : : && TREE_TYPE (tmp) == signed_char_type_node);
361 : 19113 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
362 : 19113 : dtype, tmp, NULL_TREE);
363 : : }
364 : :
365 : :
366 : : tree
367 : 127 : gfc_conv_descriptor_version (tree desc)
368 : : {
369 : 127 : tree tmp;
370 : 127 : tree dtype;
371 : :
372 : 127 : dtype = gfc_conv_descriptor_dtype (desc);
373 : 127 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_VERSION);
374 : 127 : gcc_assert (tmp != NULL_TREE
375 : : && TREE_TYPE (tmp) == integer_type_node);
376 : 127 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
377 : 127 : dtype, tmp, NULL_TREE);
378 : : }
379 : :
380 : :
381 : : /* Return the element length from the descriptor dtype field. */
382 : :
383 : : tree
384 : 8270 : gfc_conv_descriptor_elem_len (tree desc)
385 : : {
386 : 8270 : tree tmp;
387 : 8270 : tree dtype;
388 : :
389 : 8270 : dtype = gfc_conv_descriptor_dtype (desc);
390 : 8270 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
391 : : GFC_DTYPE_ELEM_LEN);
392 : 8270 : gcc_assert (tmp != NULL_TREE
393 : : && TREE_TYPE (tmp) == size_type_node);
394 : 8270 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
395 : 8270 : dtype, tmp, NULL_TREE);
396 : : }
397 : :
398 : :
399 : : tree
400 : 0 : gfc_conv_descriptor_attribute (tree desc)
401 : : {
402 : 0 : tree tmp;
403 : 0 : tree dtype;
404 : :
405 : 0 : dtype = gfc_conv_descriptor_dtype (desc);
406 : 0 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
407 : : GFC_DTYPE_ATTRIBUTE);
408 : 0 : gcc_assert (tmp!= NULL_TREE
409 : : && TREE_TYPE (tmp) == short_integer_type_node);
410 : 0 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
411 : 0 : dtype, tmp, NULL_TREE);
412 : : }
413 : :
414 : : tree
415 : 73 : gfc_conv_descriptor_type (tree desc)
416 : : {
417 : 73 : tree tmp;
418 : 73 : tree dtype;
419 : :
420 : 73 : dtype = gfc_conv_descriptor_dtype (desc);
421 : 73 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_TYPE);
422 : 73 : gcc_assert (tmp!= NULL_TREE
423 : : && TREE_TYPE (tmp) == signed_char_type_node);
424 : 73 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
425 : 73 : dtype, tmp, NULL_TREE);
426 : : }
427 : :
428 : : tree
429 : 995646 : gfc_get_descriptor_dimension (tree desc)
430 : : {
431 : 995646 : tree field = gfc_get_descriptor_field (desc, DIMENSION_FIELD);
432 : 995646 : gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
433 : : && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == RECORD_TYPE);
434 : 995646 : return field;
435 : : }
436 : :
437 : :
438 : : static tree
439 : 991670 : gfc_conv_descriptor_dimension (tree desc, tree dim)
440 : : {
441 : 991670 : tree tmp;
442 : :
443 : 991670 : tmp = gfc_get_descriptor_dimension (desc);
444 : :
445 : 991670 : return gfc_build_array_ref (tmp, dim, NULL_TREE, true);
446 : : }
447 : :
448 : :
449 : : tree
450 : 1617 : gfc_conv_descriptor_token (tree desc)
451 : : {
452 : 1617 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
453 : 1617 : tree field = gfc_get_descriptor_field (desc, CAF_TOKEN_FIELD);
454 : : /* Should be a restricted pointer - except in the finalization wrapper. */
455 : 1617 : gcc_assert (TREE_TYPE (field) == prvoid_type_node
456 : : || TREE_TYPE (field) == pvoid_type_node);
457 : 1617 : return field;
458 : : }
459 : :
460 : : static tree
461 : 991670 : gfc_conv_descriptor_subfield (tree desc, tree dim, unsigned field_idx)
462 : : {
463 : 991670 : tree tmp = gfc_conv_descriptor_dimension (desc, dim);
464 : 991670 : tree field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (tmp)), field_idx);
465 : 991670 : gcc_assert (field != NULL_TREE);
466 : :
467 : 991670 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
468 : 991670 : tmp, field, NULL_TREE);
469 : : }
470 : :
471 : : static tree
472 : 264778 : gfc_conv_descriptor_stride (tree desc, tree dim)
473 : : {
474 : 264778 : tree field = gfc_conv_descriptor_subfield (desc, dim, STRIDE_SUBFIELD);
475 : 264778 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
476 : 264778 : return field;
477 : : }
478 : :
479 : : tree
480 : 157656 : gfc_conv_descriptor_stride_get (tree desc, tree dim)
481 : : {
482 : 157656 : tree type = TREE_TYPE (desc);
483 : 157656 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
484 : 157656 : if (integer_zerop (dim)
485 : 157656 : && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
486 : 41533 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT
487 : 40465 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT
488 : 40315 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
489 : 40165 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT
490 : 40165 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT))
491 : 63913 : return gfc_index_one_node;
492 : :
493 : 93743 : return gfc_conv_descriptor_stride (desc, dim);
494 : : }
495 : :
496 : : void
497 : 171035 : gfc_conv_descriptor_stride_set (stmtblock_t *block, tree desc,
498 : : tree dim, tree value)
499 : : {
500 : 171035 : tree t = gfc_conv_descriptor_stride (desc, dim);
501 : 171035 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
502 : 171035 : }
503 : :
504 : : static tree
505 : 381583 : gfc_conv_descriptor_lbound (tree desc, tree dim)
506 : : {
507 : 381583 : tree field = gfc_conv_descriptor_subfield (desc, dim, LBOUND_SUBFIELD);
508 : 381583 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
509 : 381583 : return field;
510 : : }
511 : :
512 : : tree
513 : 206880 : gfc_conv_descriptor_lbound_get (tree desc, tree dim)
514 : : {
515 : 206880 : return gfc_conv_descriptor_lbound (desc, dim);
516 : : }
517 : :
518 : : void
519 : 174703 : gfc_conv_descriptor_lbound_set (stmtblock_t *block, tree desc,
520 : : tree dim, tree value)
521 : : {
522 : 174703 : tree t = gfc_conv_descriptor_lbound (desc, dim);
523 : 174703 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
524 : 174703 : }
525 : :
526 : : static tree
527 : 345309 : gfc_conv_descriptor_ubound (tree desc, tree dim)
528 : : {
529 : 345309 : tree field = gfc_conv_descriptor_subfield (desc, dim, UBOUND_SUBFIELD);
530 : 345309 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
531 : 345309 : return field;
532 : : }
533 : :
534 : : tree
535 : 169711 : gfc_conv_descriptor_ubound_get (tree desc, tree dim)
536 : : {
537 : 169711 : return gfc_conv_descriptor_ubound (desc, dim);
538 : : }
539 : :
540 : : void
541 : 175598 : gfc_conv_descriptor_ubound_set (stmtblock_t *block, tree desc,
542 : : tree dim, tree value)
543 : : {
544 : 175598 : tree t = gfc_conv_descriptor_ubound (desc, dim);
545 : 175598 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
546 : 175598 : }
547 : :
548 : : /* Build a null array descriptor constructor. */
549 : :
550 : : tree
551 : 964 : gfc_build_null_descriptor (tree type)
552 : : {
553 : 964 : tree field;
554 : 964 : tree tmp;
555 : :
556 : 964 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
557 : 964 : gcc_assert (DATA_FIELD == 0);
558 : 964 : field = TYPE_FIELDS (type);
559 : :
560 : : /* Set a NULL data pointer. */
561 : 964 : tmp = build_constructor_single (type, field, null_pointer_node);
562 : 964 : TREE_CONSTANT (tmp) = 1;
563 : : /* All other fields are ignored. */
564 : :
565 : 964 : return tmp;
566 : : }
567 : :
568 : :
569 : : /* Modify a descriptor such that the lbound of a given dimension is the value
570 : : specified. This also updates ubound and offset accordingly. */
571 : :
572 : : void
573 : 870 : gfc_conv_shift_descriptor_lbound (stmtblock_t* block, tree desc,
574 : : int dim, tree new_lbound)
575 : : {
576 : 870 : tree offs, ubound, lbound, stride;
577 : 870 : tree diff, offs_diff;
578 : :
579 : 870 : new_lbound = fold_convert (gfc_array_index_type, new_lbound);
580 : :
581 : 870 : offs = gfc_conv_descriptor_offset_get (desc);
582 : 870 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
583 : 870 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
584 : 870 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[dim]);
585 : :
586 : : /* Get difference (new - old) by which to shift stuff. */
587 : 870 : diff = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
588 : : new_lbound, lbound);
589 : :
590 : : /* Shift ubound and offset accordingly. This has to be done before
591 : : updating the lbound, as they depend on the lbound expression! */
592 : 870 : ubound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
593 : : ubound, diff);
594 : 870 : gfc_conv_descriptor_ubound_set (block, desc, gfc_rank_cst[dim], ubound);
595 : 870 : offs_diff = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
596 : : diff, stride);
597 : 870 : offs = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
598 : : offs, offs_diff);
599 : 870 : gfc_conv_descriptor_offset_set (block, desc, offs);
600 : :
601 : : /* Finally set lbound to value we want. */
602 : 870 : gfc_conv_descriptor_lbound_set (block, desc, gfc_rank_cst[dim], new_lbound);
603 : 870 : }
604 : :
605 : :
606 : : /* Obtain offsets for trans-types.cc(gfc_get_array_descr_info). */
607 : :
608 : : void
609 : 267211 : gfc_get_descriptor_offsets_for_info (const_tree desc_type, tree *data_off,
610 : : tree *dtype_off, tree *span_off,
611 : : tree *dim_off, tree *dim_size,
612 : : tree *stride_suboff, tree *lower_suboff,
613 : : tree *upper_suboff)
614 : : {
615 : 267211 : tree field;
616 : 267211 : tree type;
617 : :
618 : 267211 : type = TYPE_MAIN_VARIANT (desc_type);
619 : 267211 : field = gfc_advance_chain (TYPE_FIELDS (type), DATA_FIELD);
620 : 267211 : *data_off = byte_position (field);
621 : 267211 : field = gfc_advance_chain (TYPE_FIELDS (type), DTYPE_FIELD);
622 : 267211 : *dtype_off = byte_position (field);
623 : 267211 : field = gfc_advance_chain (TYPE_FIELDS (type), SPAN_FIELD);
624 : 267211 : *span_off = byte_position (field);
625 : 267211 : field = gfc_advance_chain (TYPE_FIELDS (type), DIMENSION_FIELD);
626 : 267211 : *dim_off = byte_position (field);
627 : 267211 : type = TREE_TYPE (TREE_TYPE (field));
628 : 267211 : *dim_size = TYPE_SIZE_UNIT (type);
629 : 267211 : field = gfc_advance_chain (TYPE_FIELDS (type), STRIDE_SUBFIELD);
630 : 267211 : *stride_suboff = byte_position (field);
631 : 267211 : field = gfc_advance_chain (TYPE_FIELDS (type), LBOUND_SUBFIELD);
632 : 267211 : *lower_suboff = byte_position (field);
633 : 267211 : field = gfc_advance_chain (TYPE_FIELDS (type), UBOUND_SUBFIELD);
634 : 267211 : *upper_suboff = byte_position (field);
635 : 267211 : }
636 : :
637 : :
638 : : /* Cleanup those #defines. */
639 : :
640 : : #undef DATA_FIELD
641 : : #undef OFFSET_FIELD
642 : : #undef DTYPE_FIELD
643 : : #undef SPAN_FIELD
644 : : #undef DIMENSION_FIELD
645 : : #undef CAF_TOKEN_FIELD
646 : : #undef STRIDE_SUBFIELD
647 : : #undef LBOUND_SUBFIELD
648 : : #undef UBOUND_SUBFIELD
649 : :
650 : :
651 : : /* Mark a SS chain as used. Flags specifies in which loops the SS is used.
652 : : flags & 1 = Main loop body.
653 : : flags & 2 = temp copy loop. */
654 : :
655 : : void
656 : 166368 : gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
657 : : {
658 : 391212 : for (; ss != gfc_ss_terminator; ss = ss->next)
659 : 224844 : ss->info->useflags = flags;
660 : 166368 : }
661 : :
662 : :
663 : : /* Free a gfc_ss chain. */
664 : :
665 : : void
666 : 172240 : gfc_free_ss_chain (gfc_ss * ss)
667 : : {
668 : 172240 : gfc_ss *next;
669 : :
670 : 352646 : while (ss != gfc_ss_terminator)
671 : : {
672 : 180406 : gcc_assert (ss != NULL);
673 : 180406 : next = ss->next;
674 : 180406 : gfc_free_ss (ss);
675 : 180406 : ss = next;
676 : : }
677 : 172240 : }
678 : :
679 : :
680 : : static void
681 : 473903 : free_ss_info (gfc_ss_info *ss_info)
682 : : {
683 : 473903 : int n;
684 : :
685 : 473903 : ss_info->refcount--;
686 : 473903 : if (ss_info->refcount > 0)
687 : : return;
688 : :
689 : 469156 : gcc_assert (ss_info->refcount == 0);
690 : :
691 : 469156 : switch (ss_info->type)
692 : : {
693 : : case GFC_SS_SECTION:
694 : 5185440 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
695 : 4861350 : if (ss_info->data.array.subscript[n])
696 : 6822 : gfc_free_ss_chain (ss_info->data.array.subscript[n]);
697 : : break;
698 : :
699 : : default:
700 : : break;
701 : : }
702 : :
703 : 469156 : free (ss_info);
704 : : }
705 : :
706 : :
707 : : /* Free a SS. */
708 : :
709 : : void
710 : 473903 : gfc_free_ss (gfc_ss * ss)
711 : : {
712 : 473903 : free_ss_info (ss->info);
713 : 473903 : free (ss);
714 : 473903 : }
715 : :
716 : :
717 : : /* Creates and initializes an array type gfc_ss struct. */
718 : :
719 : : gfc_ss *
720 : 397852 : gfc_get_array_ss (gfc_ss *next, gfc_expr *expr, int dimen, gfc_ss_type type)
721 : : {
722 : 397852 : gfc_ss *ss;
723 : 397852 : gfc_ss_info *ss_info;
724 : 397852 : int i;
725 : :
726 : 397852 : ss_info = gfc_get_ss_info ();
727 : 397852 : ss_info->refcount++;
728 : 397852 : ss_info->type = type;
729 : 397852 : ss_info->expr = expr;
730 : :
731 : 397852 : ss = gfc_get_ss ();
732 : 397852 : ss->info = ss_info;
733 : 397852 : ss->next = next;
734 : 397852 : ss->dimen = dimen;
735 : 844833 : for (i = 0; i < ss->dimen; i++)
736 : 446981 : ss->dim[i] = i;
737 : :
738 : 397852 : return ss;
739 : : }
740 : :
741 : :
742 : : /* Creates and initializes a temporary type gfc_ss struct. */
743 : :
744 : : gfc_ss *
745 : 11133 : gfc_get_temp_ss (tree type, tree string_length, int dimen)
746 : : {
747 : 11133 : gfc_ss *ss;
748 : 11133 : gfc_ss_info *ss_info;
749 : 11133 : int i;
750 : :
751 : 11133 : ss_info = gfc_get_ss_info ();
752 : 11133 : ss_info->refcount++;
753 : 11133 : ss_info->type = GFC_SS_TEMP;
754 : 11133 : ss_info->string_length = string_length;
755 : 11133 : ss_info->data.temp.type = type;
756 : :
757 : 11133 : ss = gfc_get_ss ();
758 : 11133 : ss->info = ss_info;
759 : 11133 : ss->next = gfc_ss_terminator;
760 : 11133 : ss->dimen = dimen;
761 : 24907 : for (i = 0; i < ss->dimen; i++)
762 : 13774 : ss->dim[i] = i;
763 : :
764 : 11133 : return ss;
765 : : }
766 : :
767 : :
768 : : /* Creates and initializes a scalar type gfc_ss struct. */
769 : :
770 : : gfc_ss *
771 : 64168 : gfc_get_scalar_ss (gfc_ss *next, gfc_expr *expr)
772 : : {
773 : 64168 : gfc_ss *ss;
774 : 64168 : gfc_ss_info *ss_info;
775 : :
776 : 64168 : ss_info = gfc_get_ss_info ();
777 : 64168 : ss_info->refcount++;
778 : 64168 : ss_info->type = GFC_SS_SCALAR;
779 : 64168 : ss_info->expr = expr;
780 : :
781 : 64168 : ss = gfc_get_ss ();
782 : 64168 : ss->info = ss_info;
783 : 64168 : ss->next = next;
784 : :
785 : 64168 : return ss;
786 : : }
787 : :
788 : :
789 : : /* Free all the SS associated with a loop. */
790 : :
791 : : void
792 : 177670 : gfc_cleanup_loop (gfc_loopinfo * loop)
793 : : {
794 : 177670 : gfc_loopinfo *loop_next, **ploop;
795 : 177670 : gfc_ss *ss;
796 : 177670 : gfc_ss *next;
797 : :
798 : 177670 : ss = loop->ss;
799 : 470680 : while (ss != gfc_ss_terminator)
800 : : {
801 : 293010 : gcc_assert (ss != NULL);
802 : 293010 : next = ss->loop_chain;
803 : 293010 : gfc_free_ss (ss);
804 : 293010 : ss = next;
805 : : }
806 : :
807 : : /* Remove reference to self in the parent loop. */
808 : 177670 : if (loop->parent)
809 : 3364 : for (ploop = &loop->parent->nested; *ploop; ploop = &(*ploop)->next)
810 : 3364 : if (*ploop == loop)
811 : : {
812 : 3364 : *ploop = loop->next;
813 : 3364 : break;
814 : : }
815 : :
816 : : /* Free non-freed nested loops. */
817 : 181034 : for (loop = loop->nested; loop; loop = loop_next)
818 : : {
819 : 3364 : loop_next = loop->next;
820 : 3364 : gfc_cleanup_loop (loop);
821 : 3364 : free (loop);
822 : : }
823 : 177670 : }
824 : :
825 : :
826 : : static void
827 : 240334 : set_ss_loop (gfc_ss *ss, gfc_loopinfo *loop)
828 : : {
829 : 240334 : int n;
830 : :
831 : 541628 : for (; ss != gfc_ss_terminator; ss = ss->next)
832 : : {
833 : 301294 : ss->loop = loop;
834 : :
835 : 301294 : if (ss->info->type == GFC_SS_SCALAR
836 : : || ss->info->type == GFC_SS_REFERENCE
837 : 254910 : || ss->info->type == GFC_SS_TEMP)
838 : 57517 : continue;
839 : :
840 : 3900432 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
841 : 3656655 : if (ss->info->data.array.subscript[n] != NULL)
842 : 6672 : set_ss_loop (ss->info->data.array.subscript[n], loop);
843 : : }
844 : 240334 : }
845 : :
846 : :
847 : : /* Associate a SS chain with a loop. */
848 : :
849 : : void
850 : 233662 : gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
851 : : {
852 : 233662 : gfc_ss *ss;
853 : 233662 : gfc_loopinfo *nested_loop;
854 : :
855 : 233662 : if (head == gfc_ss_terminator)
856 : : return;
857 : :
858 : 233662 : set_ss_loop (head, loop);
859 : :
860 : 233662 : ss = head;
861 : 761946 : for (; ss && ss != gfc_ss_terminator; ss = ss->next)
862 : : {
863 : 294622 : if (ss->nested_ss)
864 : : {
865 : 4740 : nested_loop = ss->nested_ss->loop;
866 : :
867 : : /* More than one ss can belong to the same loop. Hence, we add the
868 : : loop to the chain only if it is different from the previously
869 : : added one, to avoid duplicate nested loops. */
870 : 4740 : if (nested_loop != loop->nested)
871 : : {
872 : 3364 : gcc_assert (nested_loop->parent == NULL);
873 : 3364 : nested_loop->parent = loop;
874 : :
875 : 3364 : gcc_assert (nested_loop->next == NULL);
876 : 3364 : nested_loop->next = loop->nested;
877 : 3364 : loop->nested = nested_loop;
878 : : }
879 : : else
880 : 1376 : gcc_assert (nested_loop->parent == loop);
881 : : }
882 : :
883 : 294622 : if (ss->next == gfc_ss_terminator)
884 : 233662 : ss->loop_chain = loop->ss;
885 : : else
886 : 60960 : ss->loop_chain = ss->next;
887 : : }
888 : 233662 : gcc_assert (ss == gfc_ss_terminator);
889 : 233662 : loop->ss = head;
890 : : }
891 : :
892 : :
893 : : /* Returns true if the expression is an array pointer. */
894 : :
895 : : static bool
896 : 353443 : is_pointer_array (tree expr)
897 : : {
898 : 353443 : if (expr == NULL_TREE
899 : 353443 : || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr))
900 : 445429 : || GFC_CLASS_TYPE_P (TREE_TYPE (expr)))
901 : : return false;
902 : :
903 : 91986 : if (VAR_P (expr)
904 : 91986 : && GFC_DECL_PTR_ARRAY_P (expr))
905 : : return true;
906 : :
907 : 85896 : if (TREE_CODE (expr) == PARM_DECL
908 : 85896 : && GFC_DECL_PTR_ARRAY_P (expr))
909 : : return true;
910 : :
911 : 85896 : if (INDIRECT_REF_P (expr)
912 : 85896 : && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 0)))
913 : : return true;
914 : :
915 : : /* The field declaration is marked as an pointer array. */
916 : 83498 : if (TREE_CODE (expr) == COMPONENT_REF
917 : 12133 : && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 1))
918 : 86333 : && !GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
919 : 2835 : return true;
920 : :
921 : : return false;
922 : : }
923 : :
924 : :
925 : : /* If the symbol or expression reference a CFI descriptor, return the
926 : : pointer to the converted gfc descriptor. If an array reference is
927 : : present as the last argument, check that it is the one applied to
928 : : the CFI descriptor in the expression. Note that the CFI object is
929 : : always the symbol in the expression! */
930 : :
931 : : static bool
932 : 355222 : get_CFI_desc (gfc_symbol *sym, gfc_expr *expr,
933 : : tree *desc, gfc_array_ref *ar)
934 : : {
935 : 355222 : tree tmp;
936 : :
937 : 355222 : if (!is_CFI_desc (sym, expr))
938 : : return false;
939 : :
940 : 4727 : if (expr && ar)
941 : : {
942 : 4061 : if (!(expr->ref && expr->ref->type == REF_ARRAY)
943 : 4043 : || (&expr->ref->u.ar != ar))
944 : : return false;
945 : : }
946 : :
947 : 4697 : if (sym == NULL)
948 : 1108 : tmp = expr->symtree->n.sym->backend_decl;
949 : : else
950 : 3589 : tmp = sym->backend_decl;
951 : :
952 : 4697 : if (tmp && DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
953 : 0 : tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
954 : :
955 : 4697 : *desc = tmp;
956 : 4697 : return true;
957 : : }
958 : :
959 : :
960 : : /* A helper function for gfc_get_array_span that returns the array element size
961 : : of a class entity. */
962 : : static tree
963 : 1060 : class_array_element_size (tree decl, bool unlimited)
964 : : {
965 : : /* Class dummys usually require extraction from the saved descriptor,
966 : : which gfc_class_vptr_get does for us if necessary. This, of course,
967 : : will be a component of the class object. */
968 : 1060 : tree vptr = gfc_class_vptr_get (decl);
969 : : /* If this is an unlimited polymorphic entity with a character payload,
970 : : the element size will be corrected for the string length. */
971 : 1060 : if (unlimited)
972 : 950 : return gfc_resize_class_size_with_len (NULL,
973 : 475 : TREE_OPERAND (vptr, 0),
974 : 475 : gfc_vptr_size_get (vptr));
975 : : else
976 : 585 : return gfc_vptr_size_get (vptr);
977 : : }
978 : :
979 : :
980 : : /* Return the span of an array. */
981 : :
982 : : tree
983 : 56956 : gfc_get_array_span (tree desc, gfc_expr *expr)
984 : : {
985 : 56956 : tree tmp;
986 : 56956 : gfc_symbol *sym = (expr && expr->expr_type == EXPR_VARIABLE) ?
987 : 50310 : expr->symtree->n.sym : NULL;
988 : :
989 : 56956 : if (is_pointer_array (desc)
990 : 56956 : || (get_CFI_desc (NULL, expr, &desc, NULL)
991 : 1332 : && (POINTER_TYPE_P (TREE_TYPE (desc))
992 : 666 : ? GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc)))
993 : 0 : : GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))))
994 : : {
995 : 557 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
996 : 0 : desc = build_fold_indirect_ref_loc (input_location, desc);
997 : :
998 : : /* This will have the span field set. */
999 : 557 : tmp = gfc_conv_descriptor_span_get (desc);
1000 : : }
1001 : 56399 : else if (expr->ts.type == BT_ASSUMED)
1002 : : {
1003 : 127 : if (DECL_LANG_SPECIFIC (desc) && GFC_DECL_SAVED_DESCRIPTOR (desc))
1004 : 127 : desc = GFC_DECL_SAVED_DESCRIPTOR (desc);
1005 : 127 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
1006 : 127 : desc = build_fold_indirect_ref_loc (input_location, desc);
1007 : 127 : tmp = gfc_conv_descriptor_span_get (desc);
1008 : : }
1009 : 56272 : else if (TREE_CODE (desc) == COMPONENT_REF
1010 : 475 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
1011 : 56391 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (desc, 0))))
1012 : : /* The descriptor is the _data field of a class object. */
1013 : 50 : tmp = class_array_element_size (TREE_OPERAND (desc, 0),
1014 : 50 : UNLIMITED_POLY (expr));
1015 : 56222 : else if (sym && sym->ts.type == BT_CLASS
1016 : 1057 : && expr->ref->type == REF_COMPONENT
1017 : 1057 : && expr->ref->next->type == REF_ARRAY
1018 : 1057 : && expr->ref->next->next == NULL
1019 : 1033 : && CLASS_DATA (sym)->attr.dimension)
1020 : : /* Having escaped the above, this can only be a class array dummy. */
1021 : 1010 : tmp = class_array_element_size (sym->backend_decl,
1022 : 1010 : UNLIMITED_POLY (sym));
1023 : : else
1024 : : {
1025 : : /* If none of the fancy stuff works, the span is the element
1026 : : size of the array. Attempt to deal with unbounded character
1027 : : types if possible. Otherwise, return NULL_TREE. */
1028 : 55212 : tmp = gfc_get_element_type (TREE_TYPE (desc));
1029 : 55212 : if (tmp && TREE_CODE (tmp) == ARRAY_TYPE && TYPE_STRING_FLAG (tmp))
1030 : : {
1031 : 10921 : gcc_assert (expr->ts.type == BT_CHARACTER);
1032 : :
1033 : 10921 : tmp = gfc_get_character_len_in_bytes (tmp);
1034 : :
1035 : 10921 : if (tmp == NULL_TREE || integer_zerop (tmp))
1036 : : {
1037 : 80 : tree bs;
1038 : :
1039 : 80 : tmp = gfc_get_expr_charlen (expr);
1040 : 80 : tmp = fold_convert (gfc_array_index_type, tmp);
1041 : 80 : bs = build_int_cst (gfc_array_index_type, expr->ts.kind);
1042 : 80 : tmp = fold_build2_loc (input_location, MULT_EXPR,
1043 : : gfc_array_index_type, tmp, bs);
1044 : : }
1045 : :
1046 : 21762 : tmp = (tmp && !integer_zerop (tmp))
1047 : 21762 : ? (fold_convert (gfc_array_index_type, tmp)) : (NULL_TREE);
1048 : : }
1049 : : else
1050 : 44291 : tmp = fold_convert (gfc_array_index_type,
1051 : : size_in_bytes (tmp));
1052 : : }
1053 : 56956 : return tmp;
1054 : : }
1055 : :
1056 : :
1057 : : /* Generate an initializer for a static pointer or allocatable array. */
1058 : :
1059 : : void
1060 : 220 : gfc_trans_static_array_pointer (gfc_symbol * sym)
1061 : : {
1062 : 220 : tree type;
1063 : :
1064 : 220 : gcc_assert (TREE_STATIC (sym->backend_decl));
1065 : : /* Just zero the data member. */
1066 : 220 : type = TREE_TYPE (sym->backend_decl);
1067 : 220 : DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
1068 : 220 : }
1069 : :
1070 : :
1071 : : /* If the bounds of SE's loop have not yet been set, see if they can be
1072 : : determined from array spec AS, which is the array spec of a called
1073 : : function. MAPPING maps the callee's dummy arguments to the values
1074 : : that the caller is passing. Add any initialization and finalization
1075 : : code to SE. */
1076 : :
1077 : : void
1078 : 8520 : gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
1079 : : gfc_se * se, gfc_array_spec * as)
1080 : : {
1081 : 8520 : int n, dim, total_dim;
1082 : 8520 : gfc_se tmpse;
1083 : 8520 : gfc_ss *ss;
1084 : 8520 : tree lower;
1085 : 8520 : tree upper;
1086 : 8520 : tree tmp;
1087 : :
1088 : 8520 : total_dim = 0;
1089 : :
1090 : 8520 : if (!as || as->type != AS_EXPLICIT)
1091 : 7402 : return;
1092 : :
1093 : 2261 : for (ss = se->ss; ss; ss = ss->parent)
1094 : : {
1095 : 1143 : total_dim += ss->loop->dimen;
1096 : 2641 : for (n = 0; n < ss->loop->dimen; n++)
1097 : : {
1098 : : /* The bound is known, nothing to do. */
1099 : 1498 : if (ss->loop->to[n] != NULL_TREE)
1100 : 485 : continue;
1101 : :
1102 : 1013 : dim = ss->dim[n];
1103 : 1013 : gcc_assert (dim < as->rank);
1104 : 1013 : gcc_assert (ss->loop->dimen <= as->rank);
1105 : :
1106 : : /* Evaluate the lower bound. */
1107 : 1013 : gfc_init_se (&tmpse, NULL);
1108 : 1013 : gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
1109 : 1013 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
1110 : 1013 : gfc_add_block_to_block (&se->post, &tmpse.post);
1111 : 1013 : lower = fold_convert (gfc_array_index_type, tmpse.expr);
1112 : :
1113 : : /* ...and the upper bound. */
1114 : 1013 : gfc_init_se (&tmpse, NULL);
1115 : 1013 : gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
1116 : 1013 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
1117 : 1013 : gfc_add_block_to_block (&se->post, &tmpse.post);
1118 : 1013 : upper = fold_convert (gfc_array_index_type, tmpse.expr);
1119 : :
1120 : : /* Set the upper bound of the loop to UPPER - LOWER. */
1121 : 1013 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
1122 : : gfc_array_index_type, upper, lower);
1123 : 1013 : tmp = gfc_evaluate_now (tmp, &se->pre);
1124 : 1013 : ss->loop->to[n] = tmp;
1125 : : }
1126 : : }
1127 : :
1128 : 1118 : gcc_assert (total_dim == as->rank);
1129 : : }
1130 : :
1131 : :
1132 : : /* Generate code to allocate an array temporary, or create a variable to
1133 : : hold the data. If size is NULL, zero the descriptor so that the
1134 : : callee will allocate the array. If DEALLOC is true, also generate code to
1135 : : free the array afterwards.
1136 : :
1137 : : If INITIAL is not NULL, it is packed using internal_pack and the result used
1138 : : as data instead of allocating a fresh, unitialized area of memory.
1139 : :
1140 : : Initialization code is added to PRE and finalization code to POST.
1141 : : DYNAMIC is true if the caller may want to extend the array later
1142 : : using realloc. This prevents us from putting the array on the stack. */
1143 : :
1144 : : static void
1145 : 27810 : gfc_trans_allocate_array_storage (stmtblock_t * pre, stmtblock_t * post,
1146 : : gfc_array_info * info, tree size, tree nelem,
1147 : : tree initial, bool dynamic, bool dealloc)
1148 : : {
1149 : 27810 : tree tmp;
1150 : 27810 : tree desc;
1151 : 27810 : bool onstack;
1152 : :
1153 : 27810 : desc = info->descriptor;
1154 : 27810 : info->offset = gfc_index_zero_node;
1155 : 27810 : if (size == NULL_TREE || (dynamic && integer_zerop (size)))
1156 : : {
1157 : : /* A callee allocated array. */
1158 : 2670 : gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
1159 : 2670 : onstack = false;
1160 : : }
1161 : : else
1162 : : {
1163 : : /* Allocate the temporary. */
1164 : 50280 : onstack = !dynamic && initial == NULL_TREE
1165 : 25140 : && (flag_stack_arrays
1166 : 24807 : || gfc_can_put_var_on_stack (size));
1167 : :
1168 : 25140 : if (onstack)
1169 : : {
1170 : : /* Make a temporary variable to hold the data. */
1171 : 20412 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (nelem),
1172 : : nelem, gfc_index_one_node);
1173 : 20412 : tmp = gfc_evaluate_now (tmp, pre);
1174 : 20412 : tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1175 : : tmp);
1176 : 20412 : tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
1177 : : tmp);
1178 : 20412 : tmp = gfc_create_var (tmp, "A");
1179 : : /* If we're here only because of -fstack-arrays we have to
1180 : : emit a DECL_EXPR to make the gimplifier emit alloca calls. */
1181 : 20412 : if (!gfc_can_put_var_on_stack (size))
1182 : 17 : gfc_add_expr_to_block (pre,
1183 : : fold_build1_loc (input_location,
1184 : 17 : DECL_EXPR, TREE_TYPE (tmp),
1185 : : tmp));
1186 : 20412 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1187 : 20412 : gfc_conv_descriptor_data_set (pre, desc, tmp);
1188 : : }
1189 : : else
1190 : : {
1191 : : /* Allocate memory to hold the data or call internal_pack. */
1192 : 4728 : if (initial == NULL_TREE)
1193 : : {
1194 : 4627 : tmp = gfc_call_malloc (pre, NULL, size);
1195 : 4627 : tmp = gfc_evaluate_now (tmp, pre);
1196 : : }
1197 : : else
1198 : : {
1199 : 101 : tree packed;
1200 : 101 : tree source_data;
1201 : 101 : tree was_packed;
1202 : 101 : stmtblock_t do_copying;
1203 : :
1204 : 101 : tmp = TREE_TYPE (initial); /* Pointer to descriptor. */
1205 : 101 : gcc_assert (TREE_CODE (tmp) == POINTER_TYPE);
1206 : 101 : tmp = TREE_TYPE (tmp); /* The descriptor itself. */
1207 : 101 : tmp = gfc_get_element_type (tmp);
1208 : 101 : packed = gfc_create_var (build_pointer_type (tmp), "data");
1209 : :
1210 : 101 : tmp = build_call_expr_loc (input_location,
1211 : : gfor_fndecl_in_pack, 1, initial);
1212 : 101 : tmp = fold_convert (TREE_TYPE (packed), tmp);
1213 : 101 : gfc_add_modify (pre, packed, tmp);
1214 : :
1215 : 101 : tmp = build_fold_indirect_ref_loc (input_location,
1216 : : initial);
1217 : 101 : source_data = gfc_conv_descriptor_data_get (tmp);
1218 : :
1219 : : /* internal_pack may return source->data without any allocation
1220 : : or copying if it is already packed. If that's the case, we
1221 : : need to allocate and copy manually. */
1222 : :
1223 : 101 : gfc_start_block (&do_copying);
1224 : 101 : tmp = gfc_call_malloc (&do_copying, NULL, size);
1225 : 101 : tmp = fold_convert (TREE_TYPE (packed), tmp);
1226 : 101 : gfc_add_modify (&do_copying, packed, tmp);
1227 : 101 : tmp = gfc_build_memcpy_call (packed, source_data, size);
1228 : 101 : gfc_add_expr_to_block (&do_copying, tmp);
1229 : :
1230 : 101 : was_packed = fold_build2_loc (input_location, EQ_EXPR,
1231 : : logical_type_node, packed,
1232 : : source_data);
1233 : 101 : tmp = gfc_finish_block (&do_copying);
1234 : 101 : tmp = build3_v (COND_EXPR, was_packed, tmp,
1235 : : build_empty_stmt (input_location));
1236 : 101 : gfc_add_expr_to_block (pre, tmp);
1237 : :
1238 : 101 : tmp = fold_convert (pvoid_type_node, packed);
1239 : : }
1240 : :
1241 : 4728 : gfc_conv_descriptor_data_set (pre, desc, tmp);
1242 : : }
1243 : : }
1244 : 27810 : info->data = gfc_conv_descriptor_data_get (desc);
1245 : :
1246 : : /* The offset is zero because we create temporaries with a zero
1247 : : lower bound. */
1248 : 27810 : gfc_conv_descriptor_offset_set (pre, desc, gfc_index_zero_node);
1249 : :
1250 : 27810 : if (dealloc && !onstack)
1251 : : {
1252 : : /* Free the temporary. */
1253 : 7158 : tmp = gfc_conv_descriptor_data_get (desc);
1254 : 7158 : tmp = gfc_call_free (tmp);
1255 : 7158 : gfc_add_expr_to_block (post, tmp);
1256 : : }
1257 : 27810 : }
1258 : :
1259 : :
1260 : : /* Get the scalarizer array dimension corresponding to actual array dimension
1261 : : given by ARRAY_DIM.
1262 : :
1263 : : For example, if SS represents the array ref a(1,:,:,1), it is a
1264 : : bidimensional scalarizer array, and the result would be 0 for ARRAY_DIM=1,
1265 : : and 1 for ARRAY_DIM=2.
1266 : : If SS represents transpose(a(:,1,1,:)), it is again a bidimensional
1267 : : scalarizer array, and the result would be 1 for ARRAY_DIM=0 and 0 for
1268 : : ARRAY_DIM=3.
1269 : : If SS represents sum(a(:,:,:,1), dim=1), it is a 2+1-dimensional scalarizer
1270 : : array. If called on the inner ss, the result would be respectively 0,1,2 for
1271 : : ARRAY_DIM=0,1,2. If called on the outer ss, the result would be 0,1
1272 : : for ARRAY_DIM=1,2. */
1273 : :
1274 : : static int
1275 : 255975 : get_scalarizer_dim_for_array_dim (gfc_ss *ss, int array_dim)
1276 : : {
1277 : 255975 : int array_ref_dim;
1278 : 255975 : int n;
1279 : :
1280 : 255975 : array_ref_dim = 0;
1281 : :
1282 : 518083 : for (; ss; ss = ss->parent)
1283 : 677178 : for (n = 0; n < ss->dimen; n++)
1284 : 415070 : if (ss->dim[n] < array_dim)
1285 : 76907 : array_ref_dim++;
1286 : :
1287 : 255975 : return array_ref_dim;
1288 : : }
1289 : :
1290 : :
1291 : : static gfc_ss *
1292 : 215804 : innermost_ss (gfc_ss *ss)
1293 : : {
1294 : 395182 : while (ss->nested_ss != NULL)
1295 : : ss = ss->nested_ss;
1296 : :
1297 : 386974 : return ss;
1298 : : }
1299 : :
1300 : :
1301 : :
1302 : : /* Get the array reference dimension corresponding to the given loop dimension.
1303 : : It is different from the true array dimension given by the dim array in
1304 : : the case of a partial array reference (i.e. a(:,:,1,:) for example)
1305 : : It is different from the loop dimension in the case of a transposed array.
1306 : : */
1307 : :
1308 : : static int
1309 : 215804 : get_array_ref_dim_for_loop_dim (gfc_ss *ss, int loop_dim)
1310 : : {
1311 : 215804 : return get_scalarizer_dim_for_array_dim (innermost_ss (ss),
1312 : 215804 : ss->dim[loop_dim]);
1313 : : }
1314 : :
1315 : :
1316 : : /* Use the information in the ss to obtain the required information about
1317 : : the type and size of an array temporary, when the lhs in an assignment
1318 : : is a class expression. */
1319 : :
1320 : : static tree
1321 : 303 : get_class_info_from_ss (stmtblock_t * pre, gfc_ss *ss, tree *eltype,
1322 : : gfc_ss **fcnss)
1323 : : {
1324 : 303 : gfc_ss *loop_ss = ss->loop->ss;
1325 : 303 : gfc_ss *lhs_ss;
1326 : 303 : gfc_ss *rhs_ss;
1327 : 303 : gfc_ss *fcn_ss = NULL;
1328 : 303 : tree tmp;
1329 : 303 : tree tmp2;
1330 : 303 : tree vptr;
1331 : 303 : tree class_expr = NULL_TREE;
1332 : 303 : tree lhs_class_expr = NULL_TREE;
1333 : 303 : bool unlimited_rhs = false;
1334 : 303 : bool unlimited_lhs = false;
1335 : 303 : bool rhs_function = false;
1336 : 303 : bool unlimited_arg1 = false;
1337 : 303 : gfc_symbol *vtab;
1338 : 303 : tree cntnr = NULL_TREE;
1339 : :
1340 : : /* The second element in the loop chain contains the source for the
1341 : : class temporary created in gfc_trans_create_temp_array. */
1342 : 303 : rhs_ss = loop_ss->loop_chain;
1343 : :
1344 : 303 : if (rhs_ss != gfc_ss_terminator
1345 : 279 : && rhs_ss->info
1346 : 279 : && rhs_ss->info->expr
1347 : 279 : && rhs_ss->info->expr->ts.type == BT_CLASS
1348 : 170 : && rhs_ss->info->data.array.descriptor)
1349 : : {
1350 : 158 : if (rhs_ss->info->expr->expr_type != EXPR_VARIABLE)
1351 : 56 : class_expr
1352 : 56 : = gfc_get_class_from_expr (rhs_ss->info->data.array.descriptor);
1353 : : else
1354 : 102 : class_expr = gfc_get_class_from_gfc_expr (rhs_ss->info->expr);
1355 : 158 : unlimited_rhs = UNLIMITED_POLY (rhs_ss->info->expr);
1356 : 158 : if (rhs_ss->info->expr->expr_type == EXPR_FUNCTION)
1357 : : rhs_function = true;
1358 : : }
1359 : :
1360 : : /* Usually, ss points to the function. When the function call is an actual
1361 : : argument, it is instead rhs_ss because the ss chain is shifted by one. */
1362 : 303 : *fcnss = fcn_ss = rhs_function ? rhs_ss : ss;
1363 : :
1364 : : /* If this is a transformational function with a class result, the info
1365 : : class_container field points to the class container of arg1. */
1366 : 303 : if (class_expr != NULL_TREE
1367 : 139 : && fcn_ss->info && fcn_ss->info->expr
1368 : 91 : && fcn_ss->info->expr->expr_type == EXPR_FUNCTION
1369 : 91 : && fcn_ss->info->expr->value.function.isym
1370 : 60 : && fcn_ss->info->expr->value.function.isym->transformational)
1371 : : {
1372 : 60 : cntnr = ss->info->class_container;
1373 : 60 : unlimited_arg1
1374 : 60 : = UNLIMITED_POLY (fcn_ss->info->expr->value.function.actual->expr);
1375 : : }
1376 : :
1377 : : /* For an assignment the lhs is the next element in the loop chain.
1378 : : If we have a class rhs, this had better be a class variable
1379 : : expression! Otherwise, the class container from arg1 can be used
1380 : : to set the vptr and len fields of the result class container. */
1381 : 303 : lhs_ss = rhs_ss->loop_chain;
1382 : 303 : if (lhs_ss && lhs_ss != gfc_ss_terminator
1383 : 213 : && lhs_ss->info && lhs_ss->info->expr
1384 : 213 : && lhs_ss->info->expr->expr_type ==EXPR_VARIABLE
1385 : 213 : && lhs_ss->info->expr->ts.type == BT_CLASS)
1386 : : {
1387 : 213 : tmp = lhs_ss->info->data.array.descriptor;
1388 : 213 : unlimited_lhs = UNLIMITED_POLY (rhs_ss->info->expr);
1389 : : }
1390 : 90 : else if (cntnr != NULL_TREE)
1391 : : {
1392 : 54 : tmp = gfc_class_vptr_get (class_expr);
1393 : 54 : gfc_add_modify (pre, tmp, fold_convert (TREE_TYPE (tmp),
1394 : : gfc_class_vptr_get (cntnr)));
1395 : 54 : if (unlimited_rhs)
1396 : : {
1397 : 6 : tmp = gfc_class_len_get (class_expr);
1398 : 6 : if (unlimited_arg1)
1399 : 6 : gfc_add_modify (pre, tmp, gfc_class_len_get (cntnr));
1400 : : }
1401 : : tmp = NULL_TREE;
1402 : : }
1403 : : else
1404 : : tmp = NULL_TREE;
1405 : :
1406 : : /* Get the lhs class expression. */
1407 : 213 : if (tmp != NULL_TREE && lhs_ss->loop_chain == gfc_ss_terminator)
1408 : 201 : lhs_class_expr = gfc_get_class_from_expr (tmp);
1409 : : else
1410 : 102 : return class_expr;
1411 : :
1412 : 201 : gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (lhs_class_expr)));
1413 : :
1414 : : /* Set the lhs vptr and, if necessary, the _len field. */
1415 : 201 : if (class_expr)
1416 : : {
1417 : : /* Both lhs and rhs are class expressions. */
1418 : 79 : tmp = gfc_class_vptr_get (lhs_class_expr);
1419 : 158 : gfc_add_modify (pre, tmp,
1420 : 79 : fold_convert (TREE_TYPE (tmp),
1421 : : gfc_class_vptr_get (class_expr)));
1422 : 79 : if (unlimited_lhs)
1423 : : {
1424 : 31 : gcc_assert (unlimited_rhs);
1425 : 31 : tmp = gfc_class_len_get (lhs_class_expr);
1426 : 31 : tmp2 = gfc_class_len_get (class_expr);
1427 : 31 : gfc_add_modify (pre, tmp, tmp2);
1428 : : }
1429 : :
1430 : 79 : if (rhs_function)
1431 : : {
1432 : 37 : tmp = gfc_class_data_get (class_expr);
1433 : 37 : gfc_conv_descriptor_offset_set (pre, tmp, gfc_index_zero_node);
1434 : : }
1435 : : }
1436 : 122 : else if (rhs_ss->info->data.array.descriptor)
1437 : : {
1438 : : /* lhs is class and rhs is intrinsic or derived type. */
1439 : 116 : *eltype = TREE_TYPE (rhs_ss->info->data.array.descriptor);
1440 : 116 : *eltype = gfc_get_element_type (*eltype);
1441 : 116 : vtab = gfc_find_vtab (&rhs_ss->info->expr->ts);
1442 : 116 : vptr = vtab->backend_decl;
1443 : 116 : if (vptr == NULL_TREE)
1444 : 18 : vptr = gfc_get_symbol_decl (vtab);
1445 : 116 : vptr = gfc_build_addr_expr (NULL_TREE, vptr);
1446 : 116 : tmp = gfc_class_vptr_get (lhs_class_expr);
1447 : 116 : gfc_add_modify (pre, tmp,
1448 : 116 : fold_convert (TREE_TYPE (tmp), vptr));
1449 : :
1450 : 116 : if (unlimited_lhs)
1451 : : {
1452 : 0 : tmp = gfc_class_len_get (lhs_class_expr);
1453 : 0 : if (rhs_ss->info
1454 : 0 : && rhs_ss->info->expr
1455 : 0 : && rhs_ss->info->expr->ts.type == BT_CHARACTER)
1456 : 0 : tmp2 = build_int_cst (TREE_TYPE (tmp),
1457 : 0 : rhs_ss->info->expr->ts.kind);
1458 : : else
1459 : 0 : tmp2 = build_int_cst (TREE_TYPE (tmp), 0);
1460 : 0 : gfc_add_modify (pre, tmp, tmp2);
1461 : : }
1462 : : }
1463 : :
1464 : : return class_expr;
1465 : : }
1466 : :
1467 : :
1468 : :
1469 : : /* Generate code to create and initialize the descriptor for a temporary
1470 : : array. This is used for both temporaries needed by the scalarizer, and
1471 : : functions returning arrays. Adjusts the loop variables to be
1472 : : zero-based, and calculates the loop bounds for callee allocated arrays.
1473 : : Allocate the array unless it's callee allocated (we have a callee
1474 : : allocated array if 'callee_alloc' is true, or if loop->to[n] is
1475 : : NULL_TREE for any n). Also fills in the descriptor, data and offset
1476 : : fields of info if known. Returns the size of the array, or NULL for a
1477 : : callee allocated array.
1478 : :
1479 : : 'eltype' == NULL signals that the temporary should be a class object.
1480 : : The 'initial' expression is used to obtain the size of the dynamic
1481 : : type; otherwise the allocation and initialization proceeds as for any
1482 : : other expression
1483 : :
1484 : : PRE, POST, INITIAL, DYNAMIC and DEALLOC are as for
1485 : : gfc_trans_allocate_array_storage. */
1486 : :
1487 : : tree
1488 : 27810 : gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post, gfc_ss * ss,
1489 : : tree eltype, tree initial, bool dynamic,
1490 : : bool dealloc, bool callee_alloc, locus * where)
1491 : : {
1492 : 27810 : gfc_loopinfo *loop;
1493 : 27810 : gfc_ss *s;
1494 : 27810 : gfc_array_info *info;
1495 : 27810 : tree from[GFC_MAX_DIMENSIONS], to[GFC_MAX_DIMENSIONS];
1496 : 27810 : tree type;
1497 : 27810 : tree desc;
1498 : 27810 : tree tmp;
1499 : 27810 : tree size;
1500 : 27810 : tree nelem;
1501 : 27810 : tree cond;
1502 : 27810 : tree or_expr;
1503 : 27810 : tree elemsize;
1504 : 27810 : tree class_expr = NULL_TREE;
1505 : 27810 : gfc_ss *fcn_ss = NULL;
1506 : 27810 : int n, dim, tmp_dim;
1507 : 27810 : int total_dim = 0;
1508 : :
1509 : : /* This signals a class array for which we need the size of the
1510 : : dynamic type. Generate an eltype and then the class expression. */
1511 : 27810 : if (eltype == NULL_TREE && initial)
1512 : : {
1513 : 6 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (initial)));
1514 : 6 : class_expr = build_fold_indirect_ref_loc (input_location, initial);
1515 : : /* Obtain the structure (class) expression. */
1516 : 6 : class_expr = gfc_get_class_from_expr (class_expr);
1517 : 6 : gcc_assert (class_expr);
1518 : : }
1519 : :
1520 : : /* Otherwise, some expressions, such as class functions, arising from
1521 : : dependency checking in assignments come here with class element type.
1522 : : The descriptor can be obtained from the ss->info and then converted
1523 : : to the class object. */
1524 : 27804 : if (class_expr == NULL_TREE && GFC_CLASS_TYPE_P (eltype))
1525 : 303 : class_expr = get_class_info_from_ss (pre, ss, &eltype, &fcn_ss);
1526 : :
1527 : : /* If the dynamic type is not available, use the declared type. */
1528 : 27810 : if (eltype && GFC_CLASS_TYPE_P (eltype))
1529 : 187 : eltype = gfc_get_element_type (TREE_TYPE (TYPE_FIELDS (eltype)));
1530 : :
1531 : 27810 : if (class_expr == NULL_TREE)
1532 : 27665 : elemsize = fold_convert (gfc_array_index_type,
1533 : : TYPE_SIZE_UNIT (eltype));
1534 : : else
1535 : : {
1536 : : /* Unlimited polymorphic entities are initialised with NULL vptr. They
1537 : : can be tested for by checking if the len field is present. If so
1538 : : test the vptr before using the vtable size. */
1539 : 145 : tmp = gfc_class_vptr_get (class_expr);
1540 : 145 : tmp = fold_build2_loc (input_location, NE_EXPR,
1541 : : logical_type_node,
1542 : 145 : tmp, build_int_cst (TREE_TYPE (tmp), 0));
1543 : 145 : elemsize = fold_build3_loc (input_location, COND_EXPR,
1544 : : gfc_array_index_type,
1545 : : tmp,
1546 : : gfc_class_vtab_size_get (class_expr),
1547 : : gfc_index_zero_node);
1548 : 145 : elemsize = gfc_evaluate_now (elemsize, pre);
1549 : 145 : elemsize = gfc_resize_class_size_with_len (pre, class_expr, elemsize);
1550 : : /* Casting the data as a character of the dynamic length ensures that
1551 : : assignment of elements works when needed. */
1552 : 145 : eltype = gfc_get_character_type_len (1, elemsize);
1553 : : }
1554 : :
1555 : 27810 : memset (from, 0, sizeof (from));
1556 : 27810 : memset (to, 0, sizeof (to));
1557 : :
1558 : 27810 : info = &ss->info->data.array;
1559 : :
1560 : 27810 : gcc_assert (ss->dimen > 0);
1561 : 27810 : gcc_assert (ss->loop->dimen == ss->dimen);
1562 : :
1563 : 27810 : if (warn_array_temporaries && where)
1564 : 205 : gfc_warning (OPT_Warray_temporaries,
1565 : : "Creating array temporary at %L", where);
1566 : :
1567 : : /* Set the lower bound to zero. */
1568 : 55655 : for (s = ss; s; s = s->parent)
1569 : : {
1570 : 27845 : loop = s->loop;
1571 : :
1572 : 27845 : total_dim += loop->dimen;
1573 : 64794 : for (n = 0; n < loop->dimen; n++)
1574 : : {
1575 : 36949 : dim = s->dim[n];
1576 : :
1577 : : /* Callee allocated arrays may not have a known bound yet. */
1578 : 36949 : if (loop->to[n])
1579 : 33732 : loop->to[n] = gfc_evaluate_now (
1580 : : fold_build2_loc (input_location, MINUS_EXPR,
1581 : : gfc_array_index_type,
1582 : : loop->to[n], loop->from[n]),
1583 : : pre);
1584 : 36949 : loop->from[n] = gfc_index_zero_node;
1585 : :
1586 : : /* We have just changed the loop bounds, we must clear the
1587 : : corresponding specloop, so that delta calculation is not skipped
1588 : : later in gfc_set_delta. */
1589 : 36949 : loop->specloop[n] = NULL;
1590 : :
1591 : : /* We are constructing the temporary's descriptor based on the loop
1592 : : dimensions. As the dimensions may be accessed in arbitrary order
1593 : : (think of transpose) the size taken from the n'th loop may not map
1594 : : to the n'th dimension of the array. We need to reconstruct loop
1595 : : infos in the right order before using it to set the descriptor
1596 : : bounds. */
1597 : 36949 : tmp_dim = get_scalarizer_dim_for_array_dim (ss, dim);
1598 : 36949 : from[tmp_dim] = loop->from[n];
1599 : 36949 : to[tmp_dim] = loop->to[n];
1600 : :
1601 : 36949 : info->delta[dim] = gfc_index_zero_node;
1602 : 36949 : info->start[dim] = gfc_index_zero_node;
1603 : 36949 : info->end[dim] = gfc_index_zero_node;
1604 : 36949 : info->stride[dim] = gfc_index_one_node;
1605 : : }
1606 : : }
1607 : :
1608 : : /* Initialize the descriptor. */
1609 : 27810 : type =
1610 : 27810 : gfc_get_array_type_bounds (eltype, total_dim, 0, from, to, 1,
1611 : : GFC_ARRAY_UNKNOWN, true);
1612 : 27810 : desc = gfc_create_var (type, "atmp");
1613 : 27810 : GFC_DECL_PACKED_ARRAY (desc) = 1;
1614 : :
1615 : : /* Emit a DECL_EXPR for the variable sized array type in
1616 : : GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
1617 : : sizes works correctly. */
1618 : 27810 : tree arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (type));
1619 : 27810 : if (! TYPE_NAME (arraytype))
1620 : 27810 : TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
1621 : : NULL_TREE, arraytype);
1622 : 27810 : gfc_add_expr_to_block (pre, build1 (DECL_EXPR,
1623 : 27810 : arraytype, TYPE_NAME (arraytype)));
1624 : :
1625 : 27810 : if (fcn_ss && fcn_ss->info && fcn_ss->info->class_container)
1626 : : {
1627 : 90 : suppress_warning (desc);
1628 : 90 : TREE_USED (desc) = 0;
1629 : : }
1630 : :
1631 : 27810 : if (class_expr != NULL_TREE
1632 : 27665 : || (fcn_ss && fcn_ss->info && fcn_ss->info->class_container))
1633 : : {
1634 : 175 : tree class_data;
1635 : 175 : tree dtype;
1636 : 175 : gfc_expr *expr1 = fcn_ss ? fcn_ss->info->expr : NULL;
1637 : 169 : bool rank_changer;
1638 : :
1639 : : /* Pick out these transformational functions because they change the rank
1640 : : or shape of the first argument. This requires that the class type be
1641 : : changed, the dtype updated and the correct rank used. */
1642 : 121 : rank_changer = expr1 && expr1->expr_type == EXPR_FUNCTION
1643 : 121 : && expr1->value.function.isym
1644 : 259 : && (expr1->value.function.isym->id == GFC_ISYM_RESHAPE
1645 : : || expr1->value.function.isym->id == GFC_ISYM_SPREAD
1646 : : || expr1->value.function.isym->id == GFC_ISYM_PACK
1647 : : || expr1->value.function.isym->id == GFC_ISYM_UNPACK);
1648 : :
1649 : : /* Create a class temporary for the result using the lhs class object. */
1650 : 175 : if (class_expr != NULL_TREE && !rank_changer)
1651 : : {
1652 : 97 : tmp = gfc_create_var (TREE_TYPE (class_expr), "ctmp");
1653 : 97 : gfc_add_modify (pre, tmp, class_expr);
1654 : : }
1655 : : else
1656 : : {
1657 : 78 : tree vptr;
1658 : 78 : class_expr = fcn_ss->info->class_container;
1659 : 78 : gcc_assert (expr1);
1660 : :
1661 : : /* Build a new class container using the arg1 class object. The class
1662 : : typespec must be rebuilt because the rank might have changed. */
1663 : 78 : gfc_typespec ts = CLASS_DATA (expr1)->ts;
1664 : 78 : symbol_attribute attr = CLASS_DATA (expr1)->attr;
1665 : 78 : gfc_change_class (&ts, &attr, NULL, expr1->rank, 0);
1666 : 78 : tmp = gfc_create_var (gfc_typenode_for_spec (&ts), "ctmp");
1667 : 78 : fcn_ss->info->class_container = tmp;
1668 : :
1669 : : /* Set the vptr and obtain the element size. */
1670 : 78 : vptr = gfc_class_vptr_get (tmp);
1671 : 156 : gfc_add_modify (pre, vptr,
1672 : 78 : fold_convert (TREE_TYPE (vptr),
1673 : : gfc_class_vptr_get (class_expr)));
1674 : 78 : elemsize = gfc_class_vtab_size_get (class_expr);
1675 : :
1676 : : /* Set the _len field, if necessary. */
1677 : 78 : if (UNLIMITED_POLY (expr1))
1678 : : {
1679 : 18 : gfc_add_modify (pre, gfc_class_len_get (tmp),
1680 : : gfc_class_len_get (class_expr));
1681 : 18 : elemsize = gfc_resize_class_size_with_len (pre, class_expr,
1682 : : elemsize);
1683 : : }
1684 : :
1685 : 78 : elemsize = gfc_evaluate_now (elemsize, pre);
1686 : : }
1687 : :
1688 : 175 : class_data = gfc_class_data_get (tmp);
1689 : :
1690 : 175 : if (rank_changer)
1691 : : {
1692 : : /* Take the dtype from the class expression. */
1693 : 72 : dtype = gfc_conv_descriptor_dtype (gfc_class_data_get (class_expr));
1694 : 72 : tmp = gfc_conv_descriptor_dtype (desc);
1695 : 72 : gfc_add_modify (pre, tmp, dtype);
1696 : :
1697 : : /* These transformational functions change the rank. */
1698 : 72 : tmp = gfc_conv_descriptor_rank (desc);
1699 : 72 : gfc_add_modify (pre, tmp,
1700 : 72 : build_int_cst (TREE_TYPE (tmp), ss->loop->dimen));
1701 : 72 : fcn_ss->info->class_container = NULL_TREE;
1702 : : }
1703 : :
1704 : : /* Assign the new descriptor to the _data field. This allows the
1705 : : vptr _copy to be used for scalarized assignment since the class
1706 : : temporary can be found from the descriptor. */
1707 : 175 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1708 : 175 : TREE_TYPE (desc), desc);
1709 : 175 : gfc_add_modify (pre, class_data, tmp);
1710 : :
1711 : : /* Point desc to the class _data field. */
1712 : 175 : desc = class_data;
1713 : 175 : }
1714 : : else
1715 : : {
1716 : : /* Fill in the array dtype. */
1717 : 27635 : tmp = gfc_conv_descriptor_dtype (desc);
1718 : 27635 : gfc_add_modify (pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
1719 : : }
1720 : :
1721 : 27810 : info->descriptor = desc;
1722 : 27810 : size = gfc_index_one_node;
1723 : :
1724 : : /*
1725 : : Fill in the bounds and stride. This is a packed array, so:
1726 : :
1727 : : size = 1;
1728 : : for (n = 0; n < rank; n++)
1729 : : {
1730 : : stride[n] = size
1731 : : delta = ubound[n] + 1 - lbound[n];
1732 : : size = size * delta;
1733 : : }
1734 : : size = size * sizeof(element);
1735 : : */
1736 : :
1737 : 27810 : or_expr = NULL_TREE;
1738 : :
1739 : : /* If there is at least one null loop->to[n], it is a callee allocated
1740 : : array. */
1741 : 61542 : for (n = 0; n < total_dim; n++)
1742 : 35617 : if (to[n] == NULL_TREE)
1743 : : {
1744 : : size = NULL_TREE;
1745 : : break;
1746 : : }
1747 : :
1748 : 27810 : if (size == NULL_TREE)
1749 : 3780 : for (s = ss; s; s = s->parent)
1750 : 5117 : for (n = 0; n < s->loop->dimen; n++)
1751 : : {
1752 : 3222 : dim = get_scalarizer_dim_for_array_dim (ss, s->dim[n]);
1753 : :
1754 : : /* For a callee allocated array express the loop bounds in terms
1755 : : of the descriptor fields. */
1756 : 3222 : tmp = fold_build2_loc (input_location,
1757 : : MINUS_EXPR, gfc_array_index_type,
1758 : : gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]),
1759 : : gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]));
1760 : 3222 : s->loop->to[n] = tmp;
1761 : : }
1762 : : else
1763 : : {
1764 : 59652 : for (n = 0; n < total_dim; n++)
1765 : : {
1766 : : /* Store the stride and bound components in the descriptor. */
1767 : 33727 : gfc_conv_descriptor_stride_set (pre, desc, gfc_rank_cst[n], size);
1768 : :
1769 : 33727 : gfc_conv_descriptor_lbound_set (pre, desc, gfc_rank_cst[n],
1770 : : gfc_index_zero_node);
1771 : :
1772 : 33727 : gfc_conv_descriptor_ubound_set (pre, desc, gfc_rank_cst[n], to[n]);
1773 : :
1774 : 33727 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
1775 : : gfc_array_index_type,
1776 : : to[n], gfc_index_one_node);
1777 : :
1778 : : /* Check whether the size for this dimension is negative. */
1779 : 33727 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
1780 : : tmp, gfc_index_zero_node);
1781 : 33727 : cond = gfc_evaluate_now (cond, pre);
1782 : :
1783 : 33727 : if (n == 0)
1784 : : or_expr = cond;
1785 : : else
1786 : 7802 : or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
1787 : : logical_type_node, or_expr, cond);
1788 : :
1789 : 33727 : size = fold_build2_loc (input_location, MULT_EXPR,
1790 : : gfc_array_index_type, size, tmp);
1791 : 33727 : size = gfc_evaluate_now (size, pre);
1792 : : }
1793 : : }
1794 : :
1795 : : /* Get the size of the array. */
1796 : 27810 : if (size && !callee_alloc)
1797 : : {
1798 : : /* If or_expr is true, then the extent in at least one
1799 : : dimension is zero and the size is set to zero. */
1800 : 25735 : size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
1801 : : or_expr, gfc_index_zero_node, size);
1802 : :
1803 : 25735 : nelem = size;
1804 : 25735 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1805 : : size, elemsize);
1806 : : }
1807 : : else
1808 : : {
1809 : : nelem = size;
1810 : : size = NULL_TREE;
1811 : : }
1812 : :
1813 : : /* Set the span. */
1814 : 27810 : tmp = fold_convert (gfc_array_index_type, elemsize);
1815 : 27810 : gfc_conv_descriptor_span_set (pre, desc, tmp);
1816 : :
1817 : 27810 : gfc_trans_allocate_array_storage (pre, post, info, size, nelem, initial,
1818 : : dynamic, dealloc);
1819 : :
1820 : 55655 : while (ss->parent)
1821 : : ss = ss->parent;
1822 : :
1823 : 27810 : if (ss->dimen > ss->loop->temp_dim)
1824 : 23769 : ss->loop->temp_dim = ss->dimen;
1825 : :
1826 : 27810 : return size;
1827 : : }
1828 : :
1829 : :
1830 : : /* Return the number of iterations in a loop that starts at START,
1831 : : ends at END, and has step STEP. */
1832 : :
1833 : : static tree
1834 : 1018 : gfc_get_iteration_count (tree start, tree end, tree step)
1835 : : {
1836 : 1018 : tree tmp;
1837 : 1018 : tree type;
1838 : :
1839 : 1018 : type = TREE_TYPE (step);
1840 : 1018 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type, end, start);
1841 : 1018 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, tmp, step);
1842 : 1018 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp,
1843 : : build_int_cst (type, 1));
1844 : 1018 : tmp = fold_build2_loc (input_location, MAX_EXPR, type, tmp,
1845 : : build_int_cst (type, 0));
1846 : 1018 : return fold_convert (gfc_array_index_type, tmp);
1847 : : }
1848 : :
1849 : :
1850 : : /* Extend the data in array DESC by EXTRA elements. */
1851 : :
1852 : : static void
1853 : 1006 : gfc_grow_array (stmtblock_t * pblock, tree desc, tree extra)
1854 : : {
1855 : 1006 : tree arg0, arg1;
1856 : 1006 : tree tmp;
1857 : 1006 : tree size;
1858 : 1006 : tree ubound;
1859 : :
1860 : 1006 : if (integer_zerop (extra))
1861 : : return;
1862 : :
1863 : 976 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[0]);
1864 : :
1865 : : /* Add EXTRA to the upper bound. */
1866 : 976 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1867 : : ubound, extra);
1868 : 976 : gfc_conv_descriptor_ubound_set (pblock, desc, gfc_rank_cst[0], tmp);
1869 : :
1870 : : /* Get the value of the current data pointer. */
1871 : 976 : arg0 = gfc_conv_descriptor_data_get (desc);
1872 : :
1873 : : /* Calculate the new array size. */
1874 : 976 : size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
1875 : 976 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1876 : : ubound, gfc_index_one_node);
1877 : 976 : arg1 = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
1878 : : fold_convert (size_type_node, tmp),
1879 : : fold_convert (size_type_node, size));
1880 : :
1881 : : /* Call the realloc() function. */
1882 : 976 : tmp = gfc_call_realloc (pblock, arg0, arg1);
1883 : 976 : gfc_conv_descriptor_data_set (pblock, desc, tmp);
1884 : : }
1885 : :
1886 : :
1887 : : /* Return true if the bounds of iterator I can only be determined
1888 : : at run time. */
1889 : :
1890 : : static inline bool
1891 : 2108 : gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
1892 : : {
1893 : 2108 : return (i->start->expr_type != EXPR_CONSTANT
1894 : 1690 : || i->end->expr_type != EXPR_CONSTANT
1895 : 2276 : || i->step->expr_type != EXPR_CONSTANT);
1896 : : }
1897 : :
1898 : :
1899 : : /* Split the size of constructor element EXPR into the sum of two terms,
1900 : : one of which can be determined at compile time and one of which must
1901 : : be calculated at run time. Set *SIZE to the former and return true
1902 : : if the latter might be nonzero. */
1903 : :
1904 : : static bool
1905 : 3146 : gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
1906 : : {
1907 : 3146 : if (expr->expr_type == EXPR_ARRAY)
1908 : 630 : return gfc_get_array_constructor_size (size, expr->value.constructor);
1909 : 2516 : else if (expr->rank > 0)
1910 : : {
1911 : : /* Calculate everything at run time. */
1912 : 1013 : mpz_set_ui (*size, 0);
1913 : 1013 : return true;
1914 : : }
1915 : : else
1916 : : {
1917 : : /* A single element. */
1918 : 1503 : mpz_set_ui (*size, 1);
1919 : 1503 : return false;
1920 : : }
1921 : : }
1922 : :
1923 : :
1924 : : /* Like gfc_get_array_constructor_element_size, but applied to the whole
1925 : : of array constructor C. */
1926 : :
1927 : : static bool
1928 : 2744 : gfc_get_array_constructor_size (mpz_t * size, gfc_constructor_base base)
1929 : : {
1930 : 2744 : gfc_constructor *c;
1931 : 2744 : gfc_iterator *i;
1932 : 2744 : mpz_t val;
1933 : 2744 : mpz_t len;
1934 : 2744 : bool dynamic;
1935 : :
1936 : 2744 : mpz_set_ui (*size, 0);
1937 : 2744 : mpz_init (len);
1938 : 2744 : mpz_init (val);
1939 : :
1940 : 2744 : dynamic = false;
1941 : 6830 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1942 : : {
1943 : 4086 : i = c->iterator;
1944 : 4086 : if (i && gfc_iterator_has_dynamic_bounds (i))
1945 : : dynamic = true;
1946 : : else
1947 : : {
1948 : 2646 : dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
1949 : 2646 : if (i)
1950 : : {
1951 : : /* Multiply the static part of the element size by the
1952 : : number of iterations. */
1953 : 124 : mpz_sub (val, i->end->value.integer, i->start->value.integer);
1954 : 124 : mpz_fdiv_q (val, val, i->step->value.integer);
1955 : 124 : mpz_add_ui (val, val, 1);
1956 : 124 : if (mpz_sgn (val) > 0)
1957 : 88 : mpz_mul (len, len, val);
1958 : : else
1959 : 36 : mpz_set_ui (len, 0);
1960 : : }
1961 : 2646 : mpz_add (*size, *size, len);
1962 : : }
1963 : : }
1964 : 2744 : mpz_clear (len);
1965 : 2744 : mpz_clear (val);
1966 : 2744 : return dynamic;
1967 : : }
1968 : :
1969 : :
1970 : : /* Make sure offset is a variable. */
1971 : :
1972 : : static void
1973 : 4734 : gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
1974 : : tree * offsetvar)
1975 : : {
1976 : : /* We should have already created the offset variable. We cannot
1977 : : create it here because we may be in an inner scope. */
1978 : 4734 : gcc_assert (*offsetvar != NULL_TREE);
1979 : 4734 : gfc_add_modify (pblock, *offsetvar, *poffset);
1980 : 4734 : *poffset = *offsetvar;
1981 : 4734 : TREE_USED (*offsetvar) = 1;
1982 : 4734 : }
1983 : :
1984 : :
1985 : : /* Variables needed for bounds-checking. */
1986 : : static bool first_len;
1987 : : static tree first_len_val;
1988 : : static bool typespec_chararray_ctor;
1989 : :
1990 : : static void
1991 : 12481 : gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
1992 : : tree offset, gfc_se * se, gfc_expr * expr)
1993 : : {
1994 : 12481 : tree tmp, offset_eval;
1995 : :
1996 : 12481 : gfc_conv_expr (se, expr);
1997 : :
1998 : : /* Store the value. */
1999 : 12481 : tmp = build_fold_indirect_ref_loc (input_location,
2000 : : gfc_conv_descriptor_data_get (desc));
2001 : : /* The offset may change, so get its value now and use that to free memory.
2002 : : */
2003 : 12481 : offset_eval = gfc_evaluate_now (offset, &se->pre);
2004 : 12481 : tmp = gfc_build_array_ref (tmp, offset_eval, NULL);
2005 : :
2006 : 12481 : if (expr->expr_type == EXPR_FUNCTION && expr->ts.type == BT_DERIVED
2007 : 53 : && expr->ts.u.derived->attr.alloc_comp)
2008 : 21 : gfc_add_expr_to_block (&se->finalblock,
2009 : : gfc_deallocate_alloc_comp_no_caf (expr->ts.u.derived,
2010 : : tmp, expr->rank,
2011 : : true));
2012 : :
2013 : 12481 : if (expr->ts.type == BT_CHARACTER)
2014 : : {
2015 : 1996 : int i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
2016 : 1996 : tree esize;
2017 : :
2018 : 1996 : esize = size_in_bytes (gfc_get_element_type (TREE_TYPE (desc)));
2019 : 1996 : esize = fold_convert (gfc_charlen_type_node, esize);
2020 : 3992 : esize = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
2021 : 1996 : TREE_TYPE (esize), esize,
2022 : 1996 : build_int_cst (TREE_TYPE (esize),
2023 : 1996 : gfc_character_kinds[i].bit_size / 8));
2024 : :
2025 : 1996 : gfc_conv_string_parameter (se);
2026 : 1996 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
2027 : : {
2028 : : /* The temporary is an array of pointers. */
2029 : 6 : se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
2030 : 6 : gfc_add_modify (&se->pre, tmp, se->expr);
2031 : : }
2032 : : else
2033 : : {
2034 : : /* The temporary is an array of string values. */
2035 : 1990 : tmp = gfc_build_addr_expr (gfc_get_pchar_type (expr->ts.kind), tmp);
2036 : : /* We know the temporary and the value will be the same length,
2037 : : so can use memcpy. */
2038 : 1990 : gfc_trans_string_copy (&se->pre, esize, tmp, expr->ts.kind,
2039 : : se->string_length, se->expr, expr->ts.kind);
2040 : : }
2041 : 1996 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !typespec_chararray_ctor)
2042 : : {
2043 : 310 : if (first_len)
2044 : : {
2045 : 130 : gfc_add_modify (&se->pre, first_len_val,
2046 : 130 : fold_convert (TREE_TYPE (first_len_val),
2047 : : se->string_length));
2048 : 130 : first_len = false;
2049 : : }
2050 : : else
2051 : : {
2052 : : /* Verify that all constructor elements are of the same
2053 : : length. */
2054 : 180 : tree rhs = fold_convert (TREE_TYPE (first_len_val),
2055 : : se->string_length);
2056 : 180 : tree cond = fold_build2_loc (input_location, NE_EXPR,
2057 : : logical_type_node, first_len_val,
2058 : : rhs);
2059 : 180 : gfc_trans_runtime_check
2060 : 180 : (true, false, cond, &se->pre, &expr->where,
2061 : : "Different CHARACTER lengths (%ld/%ld) in array constructor",
2062 : : fold_convert (long_integer_type_node, first_len_val),
2063 : : fold_convert (long_integer_type_node, se->string_length));
2064 : : }
2065 : : }
2066 : : }
2067 : 10485 : else if (GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
2068 : 10485 : && !GFC_CLASS_TYPE_P (gfc_get_element_type (TREE_TYPE (desc))))
2069 : : {
2070 : : /* Assignment of a CLASS array constructor to a derived type array. */
2071 : 24 : if (expr->expr_type == EXPR_FUNCTION)
2072 : 18 : se->expr = gfc_evaluate_now (se->expr, pblock);
2073 : 24 : se->expr = gfc_class_data_get (se->expr);
2074 : 24 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
2075 : 24 : se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
2076 : 24 : gfc_add_modify (&se->pre, tmp, se->expr);
2077 : : }
2078 : : else
2079 : : {
2080 : : /* TODO: Should the frontend already have done this conversion? */
2081 : 10461 : se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
2082 : 10461 : gfc_add_modify (&se->pre, tmp, se->expr);
2083 : : }
2084 : :
2085 : 12481 : gfc_add_block_to_block (pblock, &se->pre);
2086 : 12481 : gfc_add_block_to_block (pblock, &se->post);
2087 : 12481 : }
2088 : :
2089 : :
2090 : : /* Add the contents of an array to the constructor. DYNAMIC is as for
2091 : : gfc_trans_array_constructor_value. */
2092 : :
2093 : : static void
2094 : 1925 : gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
2095 : : tree type ATTRIBUTE_UNUSED,
2096 : : tree desc, gfc_expr * expr,
2097 : : tree * poffset, tree * offsetvar,
2098 : : bool dynamic)
2099 : : {
2100 : 1925 : gfc_se se;
2101 : 1925 : gfc_ss *ss;
2102 : 1925 : gfc_loopinfo loop;
2103 : 1925 : stmtblock_t body;
2104 : 1925 : tree tmp;
2105 : 1925 : tree size;
2106 : 1925 : int n;
2107 : :
2108 : : /* We need this to be a variable so we can increment it. */
2109 : 1925 : gfc_put_offset_into_var (pblock, poffset, offsetvar);
2110 : :
2111 : 1925 : gfc_init_se (&se, NULL);
2112 : :
2113 : : /* Walk the array expression. */
2114 : 1925 : ss = gfc_walk_expr (expr);
2115 : 1925 : gcc_assert (ss != gfc_ss_terminator);
2116 : :
2117 : : /* Initialize the scalarizer. */
2118 : 1925 : gfc_init_loopinfo (&loop);
2119 : 1925 : gfc_add_ss_to_loop (&loop, ss);
2120 : :
2121 : : /* Initialize the loop. */
2122 : 1925 : gfc_conv_ss_startstride (&loop);
2123 : 1925 : gfc_conv_loop_setup (&loop, &expr->where);
2124 : :
2125 : : /* Make sure the constructed array has room for the new data. */
2126 : 1925 : if (dynamic)
2127 : : {
2128 : : /* Set SIZE to the total number of elements in the subarray. */
2129 : 506 : size = gfc_index_one_node;
2130 : 1024 : for (n = 0; n < loop.dimen; n++)
2131 : : {
2132 : 518 : tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
2133 : : gfc_index_one_node);
2134 : 518 : size = fold_build2_loc (input_location, MULT_EXPR,
2135 : : gfc_array_index_type, size, tmp);
2136 : : }
2137 : :
2138 : : /* Grow the constructed array by SIZE elements. */
2139 : 506 : gfc_grow_array (&loop.pre, desc, size);
2140 : : }
2141 : :
2142 : : /* Make the loop body. */
2143 : 1925 : gfc_mark_ss_chain_used (ss, 1);
2144 : 1925 : gfc_start_scalarized_body (&loop, &body);
2145 : 1925 : gfc_copy_loopinfo_to_se (&se, &loop);
2146 : 1925 : se.ss = ss;
2147 : :
2148 : 1925 : gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
2149 : 1925 : gcc_assert (se.ss == gfc_ss_terminator);
2150 : :
2151 : : /* Increment the offset. */
2152 : 1925 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2153 : : *poffset, gfc_index_one_node);
2154 : 1925 : gfc_add_modify (&body, *poffset, tmp);
2155 : :
2156 : : /* Finish the loop. */
2157 : 1925 : gfc_trans_scalarizing_loops (&loop, &body);
2158 : 1925 : gfc_add_block_to_block (&loop.pre, &loop.post);
2159 : 1925 : tmp = gfc_finish_block (&loop.pre);
2160 : 1925 : gfc_add_expr_to_block (pblock, tmp);
2161 : :
2162 : 1925 : gfc_cleanup_loop (&loop);
2163 : 1925 : }
2164 : :
2165 : :
2166 : : /* Assign the values to the elements of an array constructor. DYNAMIC
2167 : : is true if descriptor DESC only contains enough data for the static
2168 : : size calculated by gfc_get_array_constructor_size. When true, memory
2169 : : for the dynamic parts must be allocated using realloc. */
2170 : :
2171 : : static void
2172 : 8494 : gfc_trans_array_constructor_value (stmtblock_t * pblock,
2173 : : stmtblock_t * finalblock,
2174 : : tree type, tree desc,
2175 : : gfc_constructor_base base, tree * poffset,
2176 : : tree * offsetvar, bool dynamic)
2177 : : {
2178 : 8494 : tree tmp;
2179 : 8494 : tree start = NULL_TREE;
2180 : 8494 : tree end = NULL_TREE;
2181 : 8494 : tree step = NULL_TREE;
2182 : 8494 : stmtblock_t body;
2183 : 8494 : gfc_se se;
2184 : 8494 : mpz_t size;
2185 : 8494 : gfc_constructor *c;
2186 : 8494 : gfc_typespec ts;
2187 : 8494 : int ctr = 0;
2188 : :
2189 : 8494 : tree shadow_loopvar = NULL_TREE;
2190 : 8494 : gfc_saved_var saved_loopvar;
2191 : :
2192 : 8494 : ts.type = BT_UNKNOWN;
2193 : 8494 : mpz_init (size);
2194 : 22385 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
2195 : : {
2196 : 13891 : ctr++;
2197 : : /* If this is an iterator or an array, the offset must be a variable. */
2198 : 13891 : if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
2199 : 2809 : gfc_put_offset_into_var (pblock, poffset, offsetvar);
2200 : :
2201 : : /* Shadowing the iterator avoids changing its value and saves us from
2202 : : keeping track of it. Further, it makes sure that there's always a
2203 : : backend-decl for the symbol, even if there wasn't one before,
2204 : : e.g. in the case of an iterator that appears in a specification
2205 : : expression in an interface mapping. */
2206 : 13891 : if (c->iterator)
2207 : : {
2208 : 1295 : gfc_symbol *sym;
2209 : 1295 : tree type;
2210 : :
2211 : : /* Evaluate loop bounds before substituting the loop variable
2212 : : in case they depend on it. Such a case is invalid, but it is
2213 : : not more expensive to do the right thing here.
2214 : : See PR 44354. */
2215 : 1295 : gfc_init_se (&se, NULL);
2216 : 1295 : gfc_conv_expr_val (&se, c->iterator->start);
2217 : 1295 : gfc_add_block_to_block (pblock, &se.pre);
2218 : 1295 : start = gfc_evaluate_now (se.expr, pblock);
2219 : :
2220 : 1295 : gfc_init_se (&se, NULL);
2221 : 1295 : gfc_conv_expr_val (&se, c->iterator->end);
2222 : 1295 : gfc_add_block_to_block (pblock, &se.pre);
2223 : 1295 : end = gfc_evaluate_now (se.expr, pblock);
2224 : :
2225 : 1295 : gfc_init_se (&se, NULL);
2226 : 1295 : gfc_conv_expr_val (&se, c->iterator->step);
2227 : 1295 : gfc_add_block_to_block (pblock, &se.pre);
2228 : 1295 : step = gfc_evaluate_now (se.expr, pblock);
2229 : :
2230 : 1295 : sym = c->iterator->var->symtree->n.sym;
2231 : 1295 : type = gfc_typenode_for_spec (&sym->ts);
2232 : :
2233 : 1295 : shadow_loopvar = gfc_create_var (type, "shadow_loopvar");
2234 : 1295 : gfc_shadow_sym (sym, shadow_loopvar, &saved_loopvar);
2235 : : }
2236 : :
2237 : 13891 : gfc_start_block (&body);
2238 : :
2239 : 13891 : if (c->expr->expr_type == EXPR_ARRAY)
2240 : : {
2241 : : /* Array constructors can be nested. */
2242 : 1305 : gfc_trans_array_constructor_value (&body, finalblock, type,
2243 : : desc, c->expr->value.constructor,
2244 : : poffset, offsetvar, dynamic);
2245 : : }
2246 : 12586 : else if (c->expr->rank > 0)
2247 : : {
2248 : 1925 : gfc_trans_array_constructor_subarray (&body, type, desc, c->expr,
2249 : : poffset, offsetvar, dynamic);
2250 : : }
2251 : : else
2252 : : {
2253 : : /* This code really upsets the gimplifier so don't bother for now. */
2254 : : gfc_constructor *p;
2255 : : HOST_WIDE_INT n;
2256 : : HOST_WIDE_INT size;
2257 : :
2258 : : p = c;
2259 : : n = 0;
2260 : 12448 : while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
2261 : : {
2262 : 1787 : p = gfc_constructor_next (p);
2263 : 1787 : n++;
2264 : : }
2265 : : /* Constructor with few constant elements, or element size not
2266 : : known at compile time (e.g. deferred-length character). */
2267 : 10661 : if (n < 4 || !INTEGER_CST_P (TYPE_SIZE_UNIT (type)))
2268 : : {
2269 : : /* Scalar values. */
2270 : 10556 : gfc_init_se (&se, NULL);
2271 : 10556 : gfc_trans_array_ctor_element (&body, desc, *poffset,
2272 : : &se, c->expr);
2273 : :
2274 : 10556 : *poffset = fold_build2_loc (input_location, PLUS_EXPR,
2275 : : gfc_array_index_type,
2276 : : *poffset, gfc_index_one_node);
2277 : 10556 : if (finalblock)
2278 : 953 : gfc_add_block_to_block (finalblock, &se.finalblock);
2279 : : }
2280 : : else
2281 : : {
2282 : : /* Collect multiple scalar constants into a constructor. */
2283 : 105 : vec<constructor_elt, va_gc> *v = NULL;
2284 : 105 : tree init;
2285 : 105 : tree bound;
2286 : 105 : tree tmptype;
2287 : 105 : HOST_WIDE_INT idx = 0;
2288 : :
2289 : 105 : p = c;
2290 : : /* Count the number of consecutive scalar constants. */
2291 : 837 : while (p && !(p->iterator
2292 : 745 : || p->expr->expr_type != EXPR_CONSTANT))
2293 : : {
2294 : 732 : gfc_init_se (&se, NULL);
2295 : 732 : gfc_conv_constant (&se, p->expr);
2296 : :
2297 : 732 : if (c->expr->ts.type != BT_CHARACTER)
2298 : 660 : se.expr = fold_convert (type, se.expr);
2299 : : /* For constant character array constructors we build
2300 : : an array of pointers. */
2301 : 72 : else if (POINTER_TYPE_P (type))
2302 : 0 : se.expr = gfc_build_addr_expr
2303 : 0 : (gfc_get_pchar_type (p->expr->ts.kind),
2304 : : se.expr);
2305 : :
2306 : 732 : CONSTRUCTOR_APPEND_ELT (v,
2307 : : build_int_cst (gfc_array_index_type,
2308 : : idx++),
2309 : : se.expr);
2310 : 732 : c = p;
2311 : 732 : p = gfc_constructor_next (p);
2312 : : }
2313 : :
2314 : 105 : bound = size_int (n - 1);
2315 : : /* Create an array type to hold them. */
2316 : 105 : tmptype = build_range_type (gfc_array_index_type,
2317 : : gfc_index_zero_node, bound);
2318 : 105 : tmptype = build_array_type (type, tmptype);
2319 : :
2320 : 105 : init = build_constructor (tmptype, v);
2321 : 105 : TREE_CONSTANT (init) = 1;
2322 : 105 : TREE_STATIC (init) = 1;
2323 : : /* Create a static variable to hold the data. */
2324 : 105 : tmp = gfc_create_var (tmptype, "data");
2325 : 105 : TREE_STATIC (tmp) = 1;
2326 : 105 : TREE_CONSTANT (tmp) = 1;
2327 : 105 : TREE_READONLY (tmp) = 1;
2328 : 105 : DECL_INITIAL (tmp) = init;
2329 : 105 : init = tmp;
2330 : :
2331 : : /* Use BUILTIN_MEMCPY to assign the values. */
2332 : 105 : tmp = gfc_conv_descriptor_data_get (desc);
2333 : 105 : tmp = build_fold_indirect_ref_loc (input_location,
2334 : : tmp);
2335 : 105 : tmp = gfc_build_array_ref (tmp, *poffset, NULL);
2336 : 105 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
2337 : 105 : init = gfc_build_addr_expr (NULL_TREE, init);
2338 : :
2339 : 105 : size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2340 : 105 : bound = build_int_cst (size_type_node, n * size);
2341 : 105 : tmp = build_call_expr_loc (input_location,
2342 : : builtin_decl_explicit (BUILT_IN_MEMCPY),
2343 : : 3, tmp, init, bound);
2344 : 105 : gfc_add_expr_to_block (&body, tmp);
2345 : :
2346 : 105 : *poffset = fold_build2_loc (input_location, PLUS_EXPR,
2347 : : gfc_array_index_type, *poffset,
2348 : 105 : build_int_cst (gfc_array_index_type, n));
2349 : : }
2350 : 10661 : if (!INTEGER_CST_P (*poffset))
2351 : : {
2352 : 1610 : gfc_add_modify (&body, *offsetvar, *poffset);
2353 : 1610 : *poffset = *offsetvar;
2354 : : }
2355 : :
2356 : 10661 : if (!c->iterator)
2357 : 10661 : ts = c->expr->ts;
2358 : : }
2359 : :
2360 : : /* The frontend should already have done any expansions
2361 : : at compile-time. */
2362 : 13891 : if (!c->iterator)
2363 : : {
2364 : : /* Pass the code as is. */
2365 : 12596 : tmp = gfc_finish_block (&body);
2366 : 12596 : gfc_add_expr_to_block (pblock, tmp);
2367 : : }
2368 : : else
2369 : : {
2370 : : /* Build the implied do-loop. */
2371 : 1295 : stmtblock_t implied_do_block;
2372 : 1295 : tree cond;
2373 : 1295 : tree exit_label;
2374 : 1295 : tree loopbody;
2375 : 1295 : tree tmp2;
2376 : :
2377 : 1295 : loopbody = gfc_finish_block (&body);
2378 : :
2379 : : /* Create a new block that holds the implied-do loop. A temporary
2380 : : loop-variable is used. */
2381 : 1295 : gfc_start_block(&implied_do_block);
2382 : :
2383 : : /* Initialize the loop. */
2384 : 1295 : gfc_add_modify (&implied_do_block, shadow_loopvar, start);
2385 : :
2386 : : /* If this array expands dynamically, and the number of iterations
2387 : : is not constant, we won't have allocated space for the static
2388 : : part of C->EXPR's size. Do that now. */
2389 : 1295 : if (dynamic && gfc_iterator_has_dynamic_bounds (c->iterator))
2390 : : {
2391 : : /* Get the number of iterations. */
2392 : 500 : tmp = gfc_get_iteration_count (shadow_loopvar, end, step);
2393 : :
2394 : : /* Get the static part of C->EXPR's size. */
2395 : 500 : gfc_get_array_constructor_element_size (&size, c->expr);
2396 : 500 : tmp2 = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
2397 : :
2398 : : /* Grow the array by TMP * TMP2 elements. */
2399 : 500 : tmp = fold_build2_loc (input_location, MULT_EXPR,
2400 : : gfc_array_index_type, tmp, tmp2);
2401 : 500 : gfc_grow_array (&implied_do_block, desc, tmp);
2402 : : }
2403 : :
2404 : : /* Generate the loop body. */
2405 : 1295 : exit_label = gfc_build_label_decl (NULL_TREE);
2406 : 1295 : gfc_start_block (&body);
2407 : :
2408 : : /* Generate the exit condition. Depending on the sign of
2409 : : the step variable we have to generate the correct
2410 : : comparison. */
2411 : 1295 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2412 : 1295 : step, build_int_cst (TREE_TYPE (step), 0));
2413 : 1295 : cond = fold_build3_loc (input_location, COND_EXPR,
2414 : : logical_type_node, tmp,
2415 : : fold_build2_loc (input_location, GT_EXPR,
2416 : : logical_type_node, shadow_loopvar, end),
2417 : : fold_build2_loc (input_location, LT_EXPR,
2418 : : logical_type_node, shadow_loopvar, end));
2419 : 1295 : tmp = build1_v (GOTO_EXPR, exit_label);
2420 : 1295 : TREE_USED (exit_label) = 1;
2421 : 1295 : tmp = build3_v (COND_EXPR, cond, tmp,
2422 : : build_empty_stmt (input_location));
2423 : 1295 : gfc_add_expr_to_block (&body, tmp);
2424 : :
2425 : : /* The main loop body. */
2426 : 1295 : gfc_add_expr_to_block (&body, loopbody);
2427 : :
2428 : : /* Increase loop variable by step. */
2429 : 1295 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
2430 : 1295 : TREE_TYPE (shadow_loopvar), shadow_loopvar,
2431 : : step);
2432 : 1295 : gfc_add_modify (&body, shadow_loopvar, tmp);
2433 : :
2434 : : /* Finish the loop. */
2435 : 1295 : tmp = gfc_finish_block (&body);
2436 : 1295 : tmp = build1_v (LOOP_EXPR, tmp);
2437 : 1295 : gfc_add_expr_to_block (&implied_do_block, tmp);
2438 : :
2439 : : /* Add the exit label. */
2440 : 1295 : tmp = build1_v (LABEL_EXPR, exit_label);
2441 : 1295 : gfc_add_expr_to_block (&implied_do_block, tmp);
2442 : :
2443 : : /* Finish the implied-do loop. */
2444 : 1295 : tmp = gfc_finish_block(&implied_do_block);
2445 : 1295 : gfc_add_expr_to_block(pblock, tmp);
2446 : :
2447 : 1295 : gfc_restore_sym (c->iterator->var->symtree->n.sym, &saved_loopvar);
2448 : : }
2449 : : }
2450 : :
2451 : : /* F2008 4.5.6.3 para 5: If an executable construct references a structure
2452 : : constructor or array constructor, the entity created by the constructor is
2453 : : finalized after execution of the innermost executable construct containing
2454 : : the reference. This, in fact, was later deleted by the Combined Techical
2455 : : Corrigenda 1 TO 4 for fortran 2008 (f08/0011).
2456 : :
2457 : : Transmit finalization of this constructor through 'finalblock'. */
2458 : 8494 : if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
2459 : 8494 : && !(gfc_option.allow_std & GFC_STD_GNU)
2460 : 70 : && finalblock != NULL
2461 : 24 : && gfc_may_be_finalized (ts)
2462 : 18 : && ctr > 0 && desc != NULL_TREE
2463 : 8512 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
2464 : : {
2465 : 18 : symbol_attribute attr;
2466 : 18 : gfc_se fse;
2467 : 18 : locus loc;
2468 : 18 : gfc_locus_from_location (&loc, input_location);
2469 : 18 : gfc_warning (0, "The structure constructor at %L has been"
2470 : : " finalized. This feature was removed by f08/0011."
2471 : : " Use -std=f2018 or -std=gnu to eliminate the"
2472 : : " finalization.", &loc);
2473 : 18 : attr.pointer = attr.allocatable = 0;
2474 : 18 : gfc_init_se (&fse, NULL);
2475 : 18 : fse.expr = desc;
2476 : 18 : gfc_finalize_tree_expr (&fse, ts.u.derived, attr, 1);
2477 : 18 : gfc_add_block_to_block (finalblock, &fse.pre);
2478 : 18 : gfc_add_block_to_block (finalblock, &fse.finalblock);
2479 : 18 : gfc_add_block_to_block (finalblock, &fse.post);
2480 : : }
2481 : :
2482 : 8494 : mpz_clear (size);
2483 : 8494 : }
2484 : :
2485 : :
2486 : : /* The array constructor code can create a string length with an operand
2487 : : in the form of a temporary variable. This variable will retain its
2488 : : context (current_function_decl). If we store this length tree in a
2489 : : gfc_charlen structure which is shared by a variable in another
2490 : : context, the resulting gfc_charlen structure with a variable in a
2491 : : different context, we could trip the assertion in expand_expr_real_1
2492 : : when it sees that a variable has been created in one context and
2493 : : referenced in another.
2494 : :
2495 : : If this might be the case, we create a new gfc_charlen structure and
2496 : : link it into the current namespace. */
2497 : :
2498 : : static void
2499 : 8085 : store_backend_decl (gfc_charlen **clp, tree len, bool force_new_cl)
2500 : : {
2501 : 8085 : if (force_new_cl)
2502 : : {
2503 : 8058 : gfc_charlen *new_cl = gfc_new_charlen (gfc_current_ns, *clp);
2504 : 8058 : *clp = new_cl;
2505 : : }
2506 : 8085 : (*clp)->backend_decl = len;
2507 : 8085 : }
2508 : :
2509 : : /* A catch-all to obtain the string length for anything that is not
2510 : : a substring of non-constant length, a constant, array or variable. */
2511 : :
2512 : : static void
2513 : 318 : get_array_ctor_all_strlen (stmtblock_t *block, gfc_expr *e, tree *len)
2514 : : {
2515 : 318 : gfc_se se;
2516 : :
2517 : : /* Don't bother if we already know the length is a constant. */
2518 : 318 : if (*len && INTEGER_CST_P (*len))
2519 : 40 : return;
2520 : :
2521 : 278 : if (!e->ref && e->ts.u.cl && e->ts.u.cl->length
2522 : 29 : && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2523 : : {
2524 : : /* This is easy. */
2525 : 1 : gfc_conv_const_charlen (e->ts.u.cl);
2526 : 1 : *len = e->ts.u.cl->backend_decl;
2527 : : }
2528 : : else
2529 : : {
2530 : : /* Otherwise, be brutal even if inefficient. */
2531 : 277 : gfc_init_se (&se, NULL);
2532 : :
2533 : : /* No function call, in case of side effects. */
2534 : 277 : se.no_function_call = 1;
2535 : 277 : if (e->rank == 0)
2536 : 134 : gfc_conv_expr (&se, e);
2537 : : else
2538 : 143 : gfc_conv_expr_descriptor (&se, e);
2539 : :
2540 : : /* Fix the value. */
2541 : 277 : *len = gfc_evaluate_now (se.string_length, &se.pre);
2542 : :
2543 : 277 : gfc_add_block_to_block (block, &se.pre);
2544 : 277 : gfc_add_block_to_block (block, &se.post);
2545 : :
2546 : 277 : store_backend_decl (&e->ts.u.cl, *len, true);
2547 : : }
2548 : : }
2549 : :
2550 : :
2551 : : /* Figure out the string length of a variable reference expression.
2552 : : Used by get_array_ctor_strlen. */
2553 : :
2554 : : static void
2555 : 918 : get_array_ctor_var_strlen (stmtblock_t *block, gfc_expr * expr, tree * len)
2556 : : {
2557 : 918 : gfc_ref *ref;
2558 : 918 : gfc_typespec *ts;
2559 : 918 : mpz_t char_len;
2560 : 918 : gfc_se se;
2561 : :
2562 : : /* Don't bother if we already know the length is a constant. */
2563 : 918 : if (*len && INTEGER_CST_P (*len))
2564 : 545 : return;
2565 : :
2566 : 468 : ts = &expr->symtree->n.sym->ts;
2567 : 747 : for (ref = expr->ref; ref; ref = ref->next)
2568 : : {
2569 : 374 : switch (ref->type)
2570 : : {
2571 : 234 : case REF_ARRAY:
2572 : : /* Array references don't change the string length. */
2573 : 234 : if (ts->deferred)
2574 : 136 : get_array_ctor_all_strlen (block, expr, len);
2575 : : break;
2576 : :
2577 : 45 : case REF_COMPONENT:
2578 : : /* Use the length of the component. */
2579 : 45 : ts = &ref->u.c.component->ts;
2580 : 45 : break;
2581 : :
2582 : 95 : case REF_SUBSTRING:
2583 : 95 : if (ref->u.ss.end == NULL
2584 : 83 : || ref->u.ss.start->expr_type != EXPR_CONSTANT
2585 : 64 : || ref->u.ss.end->expr_type != EXPR_CONSTANT)
2586 : : {
2587 : : /* Note that this might evaluate expr. */
2588 : 64 : get_array_ctor_all_strlen (block, expr, len);
2589 : 64 : return;
2590 : : }
2591 : 31 : mpz_init_set_ui (char_len, 1);
2592 : 31 : mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
2593 : 31 : mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
2594 : 31 : *len = gfc_conv_mpz_to_tree_type (char_len, gfc_charlen_type_node);
2595 : 31 : mpz_clear (char_len);
2596 : 31 : return;
2597 : :
2598 : : case REF_INQUIRY:
2599 : : break;
2600 : :
2601 : 0 : default:
2602 : 0 : gcc_unreachable ();
2603 : : }
2604 : : }
2605 : :
2606 : : /* A last ditch attempt that is sometimes needed for deferred characters. */
2607 : 373 : if (!ts->u.cl->backend_decl)
2608 : : {
2609 : 19 : gfc_init_se (&se, NULL);
2610 : 19 : if (expr->rank)
2611 : 12 : gfc_conv_expr_descriptor (&se, expr);
2612 : : else
2613 : 7 : gfc_conv_expr (&se, expr);
2614 : 19 : gcc_assert (se.string_length != NULL_TREE);
2615 : 19 : gfc_add_block_to_block (block, &se.pre);
2616 : 19 : ts->u.cl->backend_decl = se.string_length;
2617 : : }
2618 : :
2619 : 373 : *len = ts->u.cl->backend_decl;
2620 : : }
2621 : :
2622 : :
2623 : : /* Figure out the string length of a character array constructor.
2624 : : If len is NULL, don't calculate the length; this happens for recursive calls
2625 : : when a sub-array-constructor is an element but not at the first position,
2626 : : so when we're not interested in the length.
2627 : : Returns TRUE if all elements are character constants. */
2628 : :
2629 : : bool
2630 : 8504 : get_array_ctor_strlen (stmtblock_t *block, gfc_constructor_base base, tree * len)
2631 : : {
2632 : 8504 : gfc_constructor *c;
2633 : 8504 : bool is_const;
2634 : :
2635 : 8504 : is_const = true;
2636 : :
2637 : 8504 : if (gfc_constructor_first (base) == NULL)
2638 : : {
2639 : 303 : if (len)
2640 : 303 : *len = build_int_cstu (gfc_charlen_type_node, 0);
2641 : 303 : return is_const;
2642 : : }
2643 : :
2644 : : /* Loop over all constructor elements to find out is_const, but in len we
2645 : : want to store the length of the first, not the last, element. We can
2646 : : of course exit the loop as soon as is_const is found to be false. */
2647 : 8201 : for (c = gfc_constructor_first (base);
2648 : 45200 : c && is_const; c = gfc_constructor_next (c))
2649 : : {
2650 : 36999 : switch (c->expr->expr_type)
2651 : : {
2652 : 35860 : case EXPR_CONSTANT:
2653 : 35860 : if (len && !(*len && INTEGER_CST_P (*len)))
2654 : 392 : *len = build_int_cstu (gfc_charlen_type_node,
2655 : 392 : c->expr->value.character.length);
2656 : : break;
2657 : :
2658 : 43 : case EXPR_ARRAY:
2659 : 43 : if (!get_array_ctor_strlen (block, c->expr->value.constructor, len))
2660 : 1127 : is_const = false;
2661 : : break;
2662 : :
2663 : 978 : case EXPR_VARIABLE:
2664 : 978 : is_const = false;
2665 : 978 : if (len)
2666 : 918 : get_array_ctor_var_strlen (block, c->expr, len);
2667 : : break;
2668 : :
2669 : 118 : default:
2670 : 118 : is_const = false;
2671 : 118 : if (len)
2672 : 118 : get_array_ctor_all_strlen (block, c->expr, len);
2673 : : break;
2674 : : }
2675 : :
2676 : : /* After the first iteration, we don't want the length modified. */
2677 : 36999 : len = NULL;
2678 : : }
2679 : :
2680 : : return is_const;
2681 : : }
2682 : :
2683 : : /* Check whether the array constructor C consists entirely of constant
2684 : : elements, and if so returns the number of those elements, otherwise
2685 : : return zero. Note, an empty or NULL array constructor returns zero. */
2686 : :
2687 : : unsigned HOST_WIDE_INT
2688 : 56800 : gfc_constant_array_constructor_p (gfc_constructor_base base)
2689 : : {
2690 : 56800 : unsigned HOST_WIDE_INT nelem = 0;
2691 : :
2692 : 56800 : gfc_constructor *c = gfc_constructor_first (base);
2693 : 456942 : while (c)
2694 : : {
2695 : 351485 : if (c->iterator
2696 : 350119 : || c->expr->rank > 0
2697 : 347695 : || c->expr->expr_type != EXPR_CONSTANT)
2698 : : return 0;
2699 : 343342 : c = gfc_constructor_next (c);
2700 : 343342 : nelem++;
2701 : : }
2702 : : return nelem;
2703 : : }
2704 : :
2705 : :
2706 : : /* Given EXPR, the constant array constructor specified by an EXPR_ARRAY,
2707 : : and the tree type of it's elements, TYPE, return a static constant
2708 : : variable that is compile-time initialized. */
2709 : :
2710 : : tree
2711 : 38911 : gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
2712 : : {
2713 : 38911 : tree tmptype, init, tmp;
2714 : 38911 : HOST_WIDE_INT nelem;
2715 : 38911 : gfc_constructor *c;
2716 : 38911 : gfc_array_spec as;
2717 : 38911 : gfc_se se;
2718 : 38911 : int i;
2719 : 38911 : vec<constructor_elt, va_gc> *v = NULL;
2720 : :
2721 : : /* First traverse the constructor list, converting the constants
2722 : : to tree to build an initializer. */
2723 : 38911 : nelem = 0;
2724 : 38911 : c = gfc_constructor_first (expr->value.constructor);
2725 : 356052 : while (c)
2726 : : {
2727 : 278230 : gfc_init_se (&se, NULL);
2728 : 278230 : gfc_conv_constant (&se, c->expr);
2729 : 278230 : if (c->expr->ts.type != BT_CHARACTER)
2730 : 243272 : se.expr = fold_convert (type, se.expr);
2731 : 34958 : else if (POINTER_TYPE_P (type))
2732 : 34958 : se.expr = gfc_build_addr_expr (gfc_get_pchar_type (c->expr->ts.kind),
2733 : : se.expr);
2734 : 278230 : CONSTRUCTOR_APPEND_ELT (v, build_int_cst (gfc_array_index_type, nelem),
2735 : : se.expr);
2736 : 278230 : c = gfc_constructor_next (c);
2737 : 278230 : nelem++;
2738 : : }
2739 : :
2740 : : /* Next determine the tree type for the array. We use the gfortran
2741 : : front-end's gfc_get_nodesc_array_type in order to create a suitable
2742 : : GFC_ARRAY_TYPE_P that may be used by the scalarizer. */
2743 : :
2744 : 38911 : memset (&as, 0, sizeof (gfc_array_spec));
2745 : :
2746 : 38911 : as.rank = expr->rank;
2747 : 38911 : as.type = AS_EXPLICIT;
2748 : 38911 : if (!expr->shape)
2749 : : {
2750 : 3 : as.lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2751 : 3 : as.upper[0] = gfc_get_int_expr (gfc_default_integer_kind,
2752 : : NULL, nelem - 1);
2753 : : }
2754 : : else
2755 : 83411 : for (i = 0; i < expr->rank; i++)
2756 : : {
2757 : 44503 : int tmp = (int) mpz_get_si (expr->shape[i]);
2758 : 44503 : as.lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2759 : 44503 : as.upper[i] = gfc_get_int_expr (gfc_default_integer_kind,
2760 : 44503 : NULL, tmp - 1);
2761 : : }
2762 : :
2763 : 38911 : tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
2764 : :
2765 : : /* as is not needed anymore. */
2766 : 122328 : for (i = 0; i < as.rank + as.corank; i++)
2767 : : {
2768 : 44506 : gfc_free_expr (as.lower[i]);
2769 : 44506 : gfc_free_expr (as.upper[i]);
2770 : : }
2771 : :
2772 : 38911 : init = build_constructor (tmptype, v);
2773 : :
2774 : 38911 : TREE_CONSTANT (init) = 1;
2775 : 38911 : TREE_STATIC (init) = 1;
2776 : :
2777 : 38911 : tmp = build_decl (input_location, VAR_DECL, create_tmp_var_name ("A"),
2778 : : tmptype);
2779 : 38911 : DECL_ARTIFICIAL (tmp) = 1;
2780 : 38911 : DECL_IGNORED_P (tmp) = 1;
2781 : 38911 : TREE_STATIC (tmp) = 1;
2782 : 38911 : TREE_CONSTANT (tmp) = 1;
2783 : 38911 : TREE_READONLY (tmp) = 1;
2784 : 38911 : DECL_INITIAL (tmp) = init;
2785 : 38911 : pushdecl (tmp);
2786 : :
2787 : 38911 : return tmp;
2788 : : }
2789 : :
2790 : :
2791 : : /* Translate a constant EXPR_ARRAY array constructor for the scalarizer.
2792 : : This mostly initializes the scalarizer state info structure with the
2793 : : appropriate values to directly use the array created by the function
2794 : : gfc_build_constant_array_constructor. */
2795 : :
2796 : : static void
2797 : 33855 : trans_constant_array_constructor (gfc_ss * ss, tree type)
2798 : : {
2799 : 33855 : gfc_array_info *info;
2800 : 33855 : tree tmp;
2801 : 33855 : int i;
2802 : :
2803 : 33855 : tmp = gfc_build_constant_array_constructor (ss->info->expr, type);
2804 : :
2805 : 33855 : info = &ss->info->data.array;
2806 : :
2807 : 33855 : info->descriptor = tmp;
2808 : 33855 : info->data = gfc_build_addr_expr (NULL_TREE, tmp);
2809 : 33855 : info->offset = gfc_index_zero_node;
2810 : :
2811 : 71050 : for (i = 0; i < ss->dimen; i++)
2812 : : {
2813 : 37195 : info->delta[i] = gfc_index_zero_node;
2814 : 37195 : info->start[i] = gfc_index_zero_node;
2815 : 37195 : info->end[i] = gfc_index_zero_node;
2816 : 37195 : info->stride[i] = gfc_index_one_node;
2817 : : }
2818 : 33855 : }
2819 : :
2820 : :
2821 : : static int
2822 : 33861 : get_rank (gfc_loopinfo *loop)
2823 : : {
2824 : 33861 : int rank;
2825 : :
2826 : 33861 : rank = 0;
2827 : 147246 : for (; loop; loop = loop->parent)
2828 : 73629 : rank += loop->dimen;
2829 : :
2830 : 39756 : return rank;
2831 : : }
2832 : :
2833 : :
2834 : : /* Helper routine of gfc_trans_array_constructor to determine if the
2835 : : bounds of the loop specified by LOOP are constant and simple enough
2836 : : to use with trans_constant_array_constructor. Returns the
2837 : : iteration count of the loop if suitable, and NULL_TREE otherwise. */
2838 : :
2839 : : static tree
2840 : 33861 : constant_array_constructor_loop_size (gfc_loopinfo * l)
2841 : : {
2842 : 33861 : gfc_loopinfo *loop;
2843 : 33861 : tree size = gfc_index_one_node;
2844 : 33861 : tree tmp;
2845 : 33861 : int i, total_dim;
2846 : :
2847 : 33861 : total_dim = get_rank (l);
2848 : :
2849 : 67722 : for (loop = l; loop; loop = loop->parent)
2850 : : {
2851 : 71074 : for (i = 0; i < loop->dimen; i++)
2852 : : {
2853 : : /* If the bounds aren't constant, return NULL_TREE. */
2854 : 37213 : if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
2855 : : return NULL_TREE;
2856 : 37207 : if (!integer_zerop (loop->from[i]))
2857 : : {
2858 : : /* Only allow nonzero "from" in one-dimensional arrays. */
2859 : 0 : if (total_dim != 1)
2860 : : return NULL_TREE;
2861 : 0 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2862 : : gfc_array_index_type,
2863 : : loop->to[i], loop->from[i]);
2864 : : }
2865 : : else
2866 : 37207 : tmp = loop->to[i];
2867 : 37207 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
2868 : : gfc_array_index_type, tmp, gfc_index_one_node);
2869 : 37207 : size = fold_build2_loc (input_location, MULT_EXPR,
2870 : : gfc_array_index_type, size, tmp);
2871 : : }
2872 : : }
2873 : :
2874 : : return size;
2875 : : }
2876 : :
2877 : :
2878 : : static tree *
2879 : 41044 : get_loop_upper_bound_for_array (gfc_ss *array, int array_dim)
2880 : : {
2881 : 41044 : gfc_ss *ss;
2882 : 41044 : int n;
2883 : :
2884 : 41044 : gcc_assert (array->nested_ss == NULL);
2885 : :
2886 : 41044 : for (ss = array; ss; ss = ss->parent)
2887 : 41044 : for (n = 0; n < ss->loop->dimen; n++)
2888 : 41044 : if (array_dim == get_array_ref_dim_for_loop_dim (ss, n))
2889 : 41044 : return &(ss->loop->to[n]);
2890 : :
2891 : 0 : gcc_unreachable ();
2892 : : }
2893 : :
2894 : :
2895 : : static gfc_loopinfo *
2896 : 684595 : outermost_loop (gfc_loopinfo * loop)
2897 : : {
2898 : 887398 : while (loop->parent != NULL)
2899 : : loop = loop->parent;
2900 : :
2901 : 684595 : return loop;
2902 : : }
2903 : :
2904 : :
2905 : : /* Array constructors are handled by constructing a temporary, then using that
2906 : : within the scalarization loop. This is not optimal, but seems by far the
2907 : : simplest method. */
2908 : :
2909 : : static void
2910 : 41044 : trans_array_constructor (gfc_ss * ss, locus * where)
2911 : : {
2912 : 41044 : gfc_constructor_base c;
2913 : 41044 : tree offset;
2914 : 41044 : tree offsetvar;
2915 : 41044 : tree desc;
2916 : 41044 : tree type;
2917 : 41044 : tree tmp;
2918 : 41044 : tree *loop_ubound0;
2919 : 41044 : bool dynamic;
2920 : 41044 : bool old_first_len, old_typespec_chararray_ctor;
2921 : 41044 : tree old_first_len_val;
2922 : 41044 : gfc_loopinfo *loop, *outer_loop;
2923 : 41044 : gfc_ss_info *ss_info;
2924 : 41044 : gfc_expr *expr;
2925 : 41044 : gfc_ss *s;
2926 : 41044 : tree neg_len;
2927 : 41044 : char *msg;
2928 : 41044 : stmtblock_t finalblock;
2929 : 41044 : bool finalize_required;
2930 : :
2931 : : /* Save the old values for nested checking. */
2932 : 41044 : old_first_len = first_len;
2933 : 41044 : old_first_len_val = first_len_val;
2934 : 41044 : old_typespec_chararray_ctor = typespec_chararray_ctor;
2935 : :
2936 : 41044 : loop = ss->loop;
2937 : 41044 : outer_loop = outermost_loop (loop);
2938 : 41044 : ss_info = ss->info;
2939 : 41044 : expr = ss_info->expr;
2940 : :
2941 : : /* Do bounds-checking here and in gfc_trans_array_ctor_element only if no
2942 : : typespec was given for the array constructor. */
2943 : 82088 : typespec_chararray_ctor = (expr->ts.type == BT_CHARACTER
2944 : 7808 : && expr->ts.u.cl
2945 : 48852 : && expr->ts.u.cl->length_from_typespec);
2946 : :
2947 : 41044 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2948 : 2538 : && expr->ts.type == BT_CHARACTER && !typespec_chararray_ctor)
2949 : : {
2950 : 1468 : first_len_val = gfc_create_var (gfc_charlen_type_node, "len");
2951 : 1468 : first_len = true;
2952 : : }
2953 : :
2954 : 41044 : gcc_assert (ss->dimen == ss->loop->dimen);
2955 : :
2956 : 41044 : c = expr->value.constructor;
2957 : 41044 : if (expr->ts.type == BT_CHARACTER)
2958 : : {
2959 : 7808 : bool const_string;
2960 : 7808 : bool force_new_cl = false;
2961 : :
2962 : : /* get_array_ctor_strlen walks the elements of the constructor, if a
2963 : : typespec was given, we already know the string length and want the one
2964 : : specified there. */
2965 : 7808 : if (typespec_chararray_ctor && expr->ts.u.cl->length
2966 : 344 : && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2967 : : {
2968 : 27 : gfc_se length_se;
2969 : :
2970 : 27 : const_string = false;
2971 : 27 : gfc_init_se (&length_se, NULL);
2972 : 27 : gfc_conv_expr_type (&length_se, expr->ts.u.cl->length,
2973 : : gfc_charlen_type_node);
2974 : 27 : ss_info->string_length = length_se.expr;
2975 : :
2976 : : /* Check if the character length is negative. If it is, then
2977 : : set LEN = 0. */
2978 : 27 : neg_len = fold_build2_loc (input_location, LT_EXPR,
2979 : : logical_type_node, ss_info->string_length,
2980 : 27 : build_zero_cst (TREE_TYPE
2981 : : (ss_info->string_length)));
2982 : : /* Print a warning if bounds checking is enabled. */
2983 : 27 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2984 : : {
2985 : 18 : msg = xasprintf ("Negative character length treated as LEN = 0");
2986 : 18 : gfc_trans_runtime_check (false, true, neg_len, &length_se.pre,
2987 : : where, msg);
2988 : 18 : free (msg);
2989 : : }
2990 : :
2991 : 27 : ss_info->string_length
2992 : 27 : = fold_build3_loc (input_location, COND_EXPR,
2993 : : gfc_charlen_type_node, neg_len,
2994 : : build_zero_cst
2995 : 27 : (TREE_TYPE (ss_info->string_length)),
2996 : : ss_info->string_length);
2997 : 27 : ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
2998 : : &length_se.pre);
2999 : 27 : gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
3000 : 27 : gfc_add_block_to_block (&outer_loop->post, &length_se.post);
3001 : 27 : }
3002 : : else
3003 : : {
3004 : 7781 : const_string = get_array_ctor_strlen (&outer_loop->pre, c,
3005 : : &ss_info->string_length);
3006 : 7781 : force_new_cl = true;
3007 : :
3008 : : /* Initialize "len" with string length for bounds checking. */
3009 : 7781 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3010 : 1486 : && !typespec_chararray_ctor
3011 : 1468 : && ss_info->string_length)
3012 : : {
3013 : 1468 : gfc_se length_se;
3014 : :
3015 : 1468 : gfc_init_se (&length_se, NULL);
3016 : 1468 : gfc_add_modify (&length_se.pre, first_len_val,
3017 : 1468 : fold_convert (TREE_TYPE (first_len_val),
3018 : : ss_info->string_length));
3019 : 1468 : ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
3020 : : &length_se.pre);
3021 : 1468 : gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
3022 : 1468 : gfc_add_block_to_block (&outer_loop->post, &length_se.post);
3023 : : }
3024 : : }
3025 : :
3026 : : /* Complex character array constructors should have been taken care of
3027 : : and not end up here. */
3028 : 7808 : gcc_assert (ss_info->string_length);
3029 : :
3030 : 7808 : store_backend_decl (&expr->ts.u.cl, ss_info->string_length, force_new_cl);
3031 : :
3032 : 7808 : type = gfc_get_character_type_len (expr->ts.kind, ss_info->string_length);
3033 : 7808 : if (const_string)
3034 : 6881 : type = build_pointer_type (type);
3035 : : }
3036 : : else
3037 : 33261 : type = gfc_typenode_for_spec (expr->ts.type == BT_CLASS
3038 : 25 : ? &CLASS_DATA (expr)->ts : &expr->ts);
3039 : :
3040 : : /* See if the constructor determines the loop bounds. */
3041 : 41044 : dynamic = false;
3042 : :
3043 : 41044 : loop_ubound0 = get_loop_upper_bound_for_array (ss, 0);
3044 : :
3045 : 80800 : if (expr->shape && get_rank (loop) > 1 && *loop_ubound0 == NULL_TREE)
3046 : : {
3047 : : /* We have a multidimensional parameter. */
3048 : 0 : for (s = ss; s; s = s->parent)
3049 : : {
3050 : : int n;
3051 : 0 : for (n = 0; n < s->loop->dimen; n++)
3052 : : {
3053 : 0 : s->loop->from[n] = gfc_index_zero_node;
3054 : 0 : s->loop->to[n] = gfc_conv_mpz_to_tree (expr->shape[s->dim[n]],
3055 : : gfc_index_integer_kind);
3056 : 0 : s->loop->to[n] = fold_build2_loc (input_location, MINUS_EXPR,
3057 : : gfc_array_index_type,
3058 : 0 : s->loop->to[n],
3059 : : gfc_index_one_node);
3060 : : }
3061 : : }
3062 : : }
3063 : :
3064 : 41044 : if (*loop_ubound0 == NULL_TREE)
3065 : : {
3066 : 826 : mpz_t size;
3067 : :
3068 : : /* We should have a 1-dimensional, zero-based loop. */
3069 : 826 : gcc_assert (loop->parent == NULL && loop->nested == NULL);
3070 : 826 : gcc_assert (loop->dimen == 1);
3071 : 826 : gcc_assert (integer_zerop (loop->from[0]));
3072 : :
3073 : : /* Split the constructor size into a static part and a dynamic part.
3074 : : Allocate the static size up-front and record whether the dynamic
3075 : : size might be nonzero. */
3076 : 826 : mpz_init (size);
3077 : 826 : dynamic = gfc_get_array_constructor_size (&size, c);
3078 : 826 : mpz_sub_ui (size, size, 1);
3079 : 826 : loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
3080 : 826 : mpz_clear (size);
3081 : : }
3082 : :
3083 : : /* Special case constant array constructors. */
3084 : 826 : if (!dynamic)
3085 : : {
3086 : 40236 : unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
3087 : 40236 : if (nelem > 0)
3088 : : {
3089 : 33861 : tree size = constant_array_constructor_loop_size (loop);
3090 : 33861 : if (size && compare_tree_int (size, nelem) == 0)
3091 : : {
3092 : 33855 : trans_constant_array_constructor (ss, type);
3093 : 33855 : goto finish;
3094 : : }
3095 : : }
3096 : : }
3097 : :
3098 : 7189 : gfc_trans_create_temp_array (&outer_loop->pre, &outer_loop->post, ss, type,
3099 : : NULL_TREE, dynamic, true, false, where);
3100 : :
3101 : 7189 : desc = ss_info->data.array.descriptor;
3102 : 7189 : offset = gfc_index_zero_node;
3103 : 7189 : offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
3104 : 7189 : suppress_warning (offsetvar);
3105 : 7189 : TREE_USED (offsetvar) = 0;
3106 : :
3107 : 7189 : gfc_init_block (&finalblock);
3108 : 7189 : finalize_required = expr->must_finalize;
3109 : 7189 : if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->attr.alloc_comp)
3110 : : finalize_required = true;
3111 : 7219 : gfc_trans_array_constructor_value (&outer_loop->pre,
3112 : : finalize_required ? &finalblock : NULL,
3113 : : type, desc, c, &offset, &offsetvar,
3114 : : dynamic);
3115 : :
3116 : : /* If the array grows dynamically, the upper bound of the loop variable
3117 : : is determined by the array's final upper bound. */
3118 : 7189 : if (dynamic)
3119 : : {
3120 : 808 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
3121 : : gfc_array_index_type,
3122 : : offsetvar, gfc_index_one_node);
3123 : 808 : tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
3124 : 808 : gfc_conv_descriptor_ubound_set (&loop->pre, desc, gfc_rank_cst[0], tmp);
3125 : 808 : if (*loop_ubound0 && VAR_P (*loop_ubound0))
3126 : 0 : gfc_add_modify (&outer_loop->pre, *loop_ubound0, tmp);
3127 : : else
3128 : 808 : *loop_ubound0 = tmp;
3129 : : }
3130 : :
3131 : 7189 : if (TREE_USED (offsetvar))
3132 : 2809 : pushdecl (offsetvar);
3133 : : else
3134 : 4380 : gcc_assert (INTEGER_CST_P (offset));
3135 : :
3136 : : #if 0
3137 : : /* Disable bound checking for now because it's probably broken. */
3138 : : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3139 : : {
3140 : : gcc_unreachable ();
3141 : : }
3142 : : #endif
3143 : :
3144 : 4380 : finish:
3145 : : /* Restore old values of globals. */
3146 : 41044 : first_len = old_first_len;
3147 : 41044 : first_len_val = old_first_len_val;
3148 : 41044 : typespec_chararray_ctor = old_typespec_chararray_ctor;
3149 : :
3150 : : /* F2008 4.5.6.3 para 5: If an executable construct references a structure
3151 : : constructor or array constructor, the entity created by the constructor is
3152 : : finalized after execution of the innermost executable construct containing
3153 : : the reference. */
3154 : 41044 : if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
3155 : 1547 : && finalblock.head != NULL_TREE)
3156 : 36 : gfc_prepend_expr_to_block (&loop->post, finalblock.head);
3157 : 41044 : }
3158 : :
3159 : :
3160 : : /* INFO describes a GFC_SS_SECTION in loop LOOP, and this function is
3161 : : called after evaluating all of INFO's vector dimensions. Go through
3162 : : each such vector dimension and see if we can now fill in any missing
3163 : : loop bounds. */
3164 : :
3165 : : static void
3166 : 175290 : set_vector_loop_bounds (gfc_ss * ss)
3167 : : {
3168 : 175290 : gfc_loopinfo *loop, *outer_loop;
3169 : 175290 : gfc_array_info *info;
3170 : 175290 : gfc_se se;
3171 : 175290 : tree tmp;
3172 : 175290 : tree desc;
3173 : 175290 : tree zero;
3174 : 175290 : int n;
3175 : 175290 : int dim;
3176 : :
3177 : 175290 : outer_loop = outermost_loop (ss->loop);
3178 : :
3179 : 175290 : info = &ss->info->data.array;
3180 : :
3181 : 355216 : for (; ss; ss = ss->parent)
3182 : : {
3183 : 179926 : loop = ss->loop;
3184 : :
3185 : 430227 : for (n = 0; n < loop->dimen; n++)
3186 : : {
3187 : 250301 : dim = ss->dim[n];
3188 : 250301 : if (info->ref->u.ar.dimen_type[dim] != DIMEN_VECTOR
3189 : 752 : || loop->to[n] != NULL)
3190 : 250134 : continue;
3191 : :
3192 : : /* Loop variable N indexes vector dimension DIM, and we don't
3193 : : yet know the upper bound of loop variable N. Set it to the
3194 : : difference between the vector's upper and lower bounds. */
3195 : 167 : gcc_assert (loop->from[n] == gfc_index_zero_node);
3196 : 167 : gcc_assert (info->subscript[dim]
3197 : : && info->subscript[dim]->info->type == GFC_SS_VECTOR);
3198 : :
3199 : 167 : gfc_init_se (&se, NULL);
3200 : 167 : desc = info->subscript[dim]->info->data.array.descriptor;
3201 : 167 : zero = gfc_rank_cst[0];
3202 : 167 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
3203 : : gfc_array_index_type,
3204 : : gfc_conv_descriptor_ubound_get (desc, zero),
3205 : : gfc_conv_descriptor_lbound_get (desc, zero));
3206 : 167 : tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
3207 : 167 : loop->to[n] = tmp;
3208 : : }
3209 : : }
3210 : 175290 : }
3211 : :
3212 : :
3213 : : /* Tells whether a scalar argument to an elemental procedure is saved out
3214 : : of a scalarization loop as a value or as a reference. */
3215 : :
3216 : : bool
3217 : 43895 : gfc_scalar_elemental_arg_saved_as_reference (gfc_ss_info * ss_info)
3218 : : {
3219 : 43895 : if (ss_info->type != GFC_SS_REFERENCE)
3220 : : return false;
3221 : :
3222 : 10242 : if (ss_info->data.scalar.needs_temporary)
3223 : : return false;
3224 : :
3225 : : /* If the actual argument can be absent (in other words, it can
3226 : : be a NULL reference), don't try to evaluate it; pass instead
3227 : : the reference directly. */
3228 : 9878 : if (ss_info->can_be_null_ref)
3229 : : return true;
3230 : :
3231 : : /* If the expression is of polymorphic type, it's actual size is not known,
3232 : : so we avoid copying it anywhere. */
3233 : 9202 : if (ss_info->data.scalar.dummy_arg
3234 : 1400 : && gfc_dummy_arg_get_typespec (*ss_info->data.scalar.dummy_arg).type
3235 : : == BT_CLASS
3236 : 9324 : && ss_info->expr->ts.type == BT_CLASS)
3237 : : return true;
3238 : :
3239 : : /* If the expression is a data reference of aggregate type,
3240 : : and the data reference is not used on the left hand side,
3241 : : avoid a copy by saving a reference to the content. */
3242 : 9178 : if (!ss_info->data.scalar.needs_temporary
3243 : 9178 : && (ss_info->expr->ts.type == BT_DERIVED
3244 : 8230 : || ss_info->expr->ts.type == BT_CLASS)
3245 : 10174 : && gfc_expr_is_variable (ss_info->expr))
3246 : : return true;
3247 : :
3248 : : /* Otherwise the expression is evaluated to a temporary variable before the
3249 : : scalarization loop. */
3250 : : return false;
3251 : : }
3252 : :
3253 : :
3254 : : /* Add the pre and post chains for all the scalar expressions in a SS chain
3255 : : to loop. This is called after the loop parameters have been calculated,
3256 : : but before the actual scalarizing loops. */
3257 : :
3258 : : static void
3259 : 184421 : gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
3260 : : locus * where)
3261 : : {
3262 : 184421 : gfc_loopinfo *nested_loop, *outer_loop;
3263 : 184421 : gfc_se se;
3264 : 184421 : gfc_ss_info *ss_info;
3265 : 184421 : gfc_array_info *info;
3266 : 184421 : gfc_expr *expr;
3267 : 184421 : int n;
3268 : :
3269 : : /* Don't evaluate the arguments for realloc_lhs_loop_for_fcn_call; otherwise,
3270 : : arguments could get evaluated multiple times. */
3271 : 184421 : if (ss->is_alloc_lhs)
3272 : 166 : return;
3273 : :
3274 : 485159 : outer_loop = outermost_loop (loop);
3275 : :
3276 : : /* TODO: This can generate bad code if there are ordering dependencies,
3277 : : e.g., a callee allocated function and an unknown size constructor. */
3278 : : gcc_assert (ss != NULL);
3279 : :
3280 : 485159 : for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
3281 : : {
3282 : 300904 : gcc_assert (ss);
3283 : :
3284 : : /* Cross loop arrays are handled from within the most nested loop. */
3285 : 300904 : if (ss->nested_ss != NULL)
3286 : 4740 : continue;
3287 : :
3288 : 296164 : ss_info = ss->info;
3289 : 296164 : expr = ss_info->expr;
3290 : 296164 : info = &ss_info->data.array;
3291 : :
3292 : 296164 : switch (ss_info->type)
3293 : : {
3294 : 41200 : case GFC_SS_SCALAR:
3295 : : /* Scalar expression. Evaluate this now. This includes elemental
3296 : : dimension indices, but not array section bounds. */
3297 : 41200 : gfc_init_se (&se, NULL);
3298 : 41200 : gfc_conv_expr (&se, expr);
3299 : 41200 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3300 : :
3301 : 41200 : if (expr->ts.type != BT_CHARACTER
3302 : 41200 : && !gfc_is_alloc_class_scalar_function (expr))
3303 : : {
3304 : : /* Move the evaluation of scalar expressions outside the
3305 : : scalarization loop, except for WHERE assignments. */
3306 : 37552 : if (subscript)
3307 : 5882 : se.expr = convert(gfc_array_index_type, se.expr);
3308 : 37552 : if (!ss_info->where)
3309 : 37138 : se.expr = gfc_evaluate_now (se.expr, &outer_loop->pre);
3310 : 37552 : gfc_add_block_to_block (&outer_loop->pre, &se.post);
3311 : : }
3312 : : else
3313 : 3648 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3314 : :
3315 : 41200 : ss_info->data.scalar.value = se.expr;
3316 : 41200 : ss_info->string_length = se.string_length;
3317 : 41200 : break;
3318 : :
3319 : 5121 : case GFC_SS_REFERENCE:
3320 : : /* Scalar argument to elemental procedure. */
3321 : 5121 : gfc_init_se (&se, NULL);
3322 : 5121 : if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
3323 : 824 : gfc_conv_expr_reference (&se, expr);
3324 : : else
3325 : : {
3326 : : /* Evaluate the argument outside the loop and pass
3327 : : a reference to the value. */
3328 : 4297 : gfc_conv_expr (&se, expr);
3329 : : }
3330 : :
3331 : : /* Ensure that a pointer to the string is stored. */
3332 : 5121 : if (expr->ts.type == BT_CHARACTER)
3333 : 174 : gfc_conv_string_parameter (&se);
3334 : :
3335 : 5121 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3336 : 5121 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3337 : 5121 : if (gfc_is_class_scalar_expr (expr))
3338 : : /* This is necessary because the dynamic type will always be
3339 : : large than the declared type. In consequence, assigning
3340 : : the value to a temporary could segfault.
3341 : : OOP-TODO: see if this is generally correct or is the value
3342 : : has to be written to an allocated temporary, whose address
3343 : : is passed via ss_info. */
3344 : 48 : ss_info->data.scalar.value = se.expr;
3345 : : else
3346 : 5073 : ss_info->data.scalar.value = gfc_evaluate_now (se.expr,
3347 : : &outer_loop->pre);
3348 : :
3349 : 5121 : ss_info->string_length = se.string_length;
3350 : 5121 : break;
3351 : :
3352 : : case GFC_SS_SECTION:
3353 : : /* Add the expressions for scalar and vector subscripts. */
3354 : 2804640 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
3355 : 2629350 : if (info->subscript[n])
3356 : 6634 : gfc_add_loop_ss_code (loop, info->subscript[n], true, where);
3357 : :
3358 : 175290 : set_vector_loop_bounds (ss);
3359 : 175290 : break;
3360 : :
3361 : 752 : case GFC_SS_VECTOR:
3362 : : /* Get the vector's descriptor and store it in SS. */
3363 : 752 : gfc_init_se (&se, NULL);
3364 : 752 : gfc_conv_expr_descriptor (&se, expr);
3365 : 752 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3366 : 752 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3367 : 752 : info->descriptor = se.expr;
3368 : 752 : break;
3369 : :
3370 : 11297 : case GFC_SS_INTRINSIC:
3371 : 11297 : gfc_add_intrinsic_ss_code (loop, ss);
3372 : 11297 : break;
3373 : :
3374 : 9322 : case GFC_SS_FUNCTION:
3375 : : /* Array function return value. We call the function and save its
3376 : : result in a temporary for use inside the loop. */
3377 : 9322 : gfc_init_se (&se, NULL);
3378 : 9322 : se.loop = loop;
3379 : 9322 : se.ss = ss;
3380 : 9322 : if (gfc_is_class_array_function (expr))
3381 : 177 : expr->must_finalize = 1;
3382 : 9322 : gfc_conv_expr (&se, expr);
3383 : 9322 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3384 : 9322 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3385 : 9322 : gfc_add_block_to_block (&outer_loop->post, &se.finalblock);
3386 : 9322 : ss_info->string_length = se.string_length;
3387 : 9322 : break;
3388 : :
3389 : 41044 : case GFC_SS_CONSTRUCTOR:
3390 : 41044 : if (expr->ts.type == BT_CHARACTER
3391 : 7808 : && ss_info->string_length == NULL
3392 : 7808 : && expr->ts.u.cl
3393 : 7808 : && expr->ts.u.cl->length
3394 : 7464 : && expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
3395 : : {
3396 : 7413 : gfc_init_se (&se, NULL);
3397 : 7413 : gfc_conv_expr_type (&se, expr->ts.u.cl->length,
3398 : : gfc_charlen_type_node);
3399 : 7413 : ss_info->string_length = se.expr;
3400 : 7413 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
3401 : 7413 : gfc_add_block_to_block (&outer_loop->post, &se.post);
3402 : : }
3403 : 41044 : trans_array_constructor (ss, where);
3404 : 41044 : break;
3405 : :
3406 : : case GFC_SS_TEMP:
3407 : : case GFC_SS_COMPONENT:
3408 : : /* Do nothing. These are handled elsewhere. */
3409 : : break;
3410 : :
3411 : 0 : default:
3412 : 0 : gcc_unreachable ();
3413 : : }
3414 : : }
3415 : :
3416 : 184255 : if (!subscript)
3417 : 180985 : for (nested_loop = loop->nested; nested_loop;
3418 : 3364 : nested_loop = nested_loop->next)
3419 : 3364 : gfc_add_loop_ss_code (nested_loop, nested_loop->ss, subscript, where);
3420 : : }
3421 : :
3422 : :
3423 : : /* Translate expressions for the descriptor and data pointer of a SS. */
3424 : : /*GCC ARRAYS*/
3425 : :
3426 : : static void
3427 : 312488 : gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
3428 : : {
3429 : 312488 : gfc_se se;
3430 : 312488 : gfc_ss_info *ss_info;
3431 : 312488 : gfc_array_info *info;
3432 : 312488 : tree tmp;
3433 : :
3434 : 312488 : ss_info = ss->info;
3435 : 312488 : info = &ss_info->data.array;
3436 : :
3437 : : /* Get the descriptor for the array to be scalarized. */
3438 : 312488 : gcc_assert (ss_info->expr->expr_type == EXPR_VARIABLE);
3439 : 312488 : gfc_init_se (&se, NULL);
3440 : 312488 : se.descriptor_only = 1;
3441 : 312488 : gfc_conv_expr_lhs (&se, ss_info->expr);
3442 : 312488 : gfc_add_block_to_block (block, &se.pre);
3443 : 312488 : info->descriptor = se.expr;
3444 : 312488 : ss_info->string_length = se.string_length;
3445 : 312488 : ss_info->class_container = se.class_container;
3446 : :
3447 : 312488 : if (base)
3448 : : {
3449 : 117495 : if (ss_info->expr->ts.type == BT_CHARACTER && !ss_info->expr->ts.deferred
3450 : 21971 : && ss_info->expr->ts.u.cl->length == NULL)
3451 : : {
3452 : : /* Emit a DECL_EXPR for the variable sized array type in
3453 : : GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
3454 : : sizes works correctly. */
3455 : 1085 : tree arraytype = TREE_TYPE (
3456 : : GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (info->descriptor)));
3457 : 1085 : if (! TYPE_NAME (arraytype))
3458 : 893 : TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
3459 : : NULL_TREE, arraytype);
3460 : 1085 : gfc_add_expr_to_block (block, build1 (DECL_EXPR, arraytype,
3461 : 1085 : TYPE_NAME (arraytype)));
3462 : : }
3463 : : /* Also the data pointer. */
3464 : 117495 : tmp = gfc_conv_array_data (se.expr);
3465 : : /* If this is a variable or address or a class array, use it directly.
3466 : : Otherwise we must evaluate it now to avoid breaking dependency
3467 : : analysis by pulling the expressions for elemental array indices
3468 : : inside the loop. */
3469 : 117495 : if (!(DECL_P (tmp)
3470 : 107570 : || (TREE_CODE (tmp) == ADDR_EXPR
3471 : 69024 : && DECL_P (TREE_OPERAND (tmp, 0)))
3472 : 41861 : || (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
3473 : 38202 : && TREE_CODE (se.expr) == COMPONENT_REF
3474 : 7767 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (se.expr, 0))))))
3475 : 39037 : tmp = gfc_evaluate_now (tmp, block);
3476 : 117495 : info->data = tmp;
3477 : :
3478 : 117495 : tmp = gfc_conv_array_offset (se.expr);
3479 : 117495 : info->offset = gfc_evaluate_now (tmp, block);
3480 : :
3481 : : /* Make absolutely sure that the saved_offset is indeed saved
3482 : : so that the variable is still accessible after the loops
3483 : : are translated. */
3484 : 117495 : info->saved_offset = info->offset;
3485 : : }
3486 : 312488 : }
3487 : :
3488 : :
3489 : : /* Initialize a gfc_loopinfo structure. */
3490 : :
3491 : : void
3492 : 182609 : gfc_init_loopinfo (gfc_loopinfo * loop)
3493 : : {
3494 : 182609 : int n;
3495 : :
3496 : 182609 : memset (loop, 0, sizeof (gfc_loopinfo));
3497 : 182609 : gfc_init_block (&loop->pre);
3498 : 182609 : gfc_init_block (&loop->post);
3499 : :
3500 : : /* Initially scalarize in order and default to no loop reversal. */
3501 : 3104353 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
3502 : : {
3503 : 2739135 : loop->order[n] = n;
3504 : 2739135 : loop->reverse[n] = GFC_INHIBIT_REVERSE;
3505 : : }
3506 : :
3507 : 182609 : loop->ss = gfc_ss_terminator;
3508 : 182609 : }
3509 : :
3510 : :
3511 : : /* Copies the loop variable info to a gfc_se structure. Does not copy the SS
3512 : : chain. */
3513 : :
3514 : : void
3515 : 183136 : gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
3516 : : {
3517 : 183136 : se->loop = loop;
3518 : 183136 : }
3519 : :
3520 : :
3521 : : /* Return an expression for the data pointer of an array. */
3522 : :
3523 : : tree
3524 : 314819 : gfc_conv_array_data (tree descriptor)
3525 : : {
3526 : 314819 : tree type;
3527 : :
3528 : 314819 : type = TREE_TYPE (descriptor);
3529 : 314819 : if (GFC_ARRAY_TYPE_P (type))
3530 : : {
3531 : 224018 : if (TREE_CODE (type) == POINTER_TYPE)
3532 : : return descriptor;
3533 : : else
3534 : : {
3535 : : /* Descriptorless arrays. */
3536 : 171537 : return gfc_build_addr_expr (NULL_TREE, descriptor);
3537 : : }
3538 : : }
3539 : : else
3540 : 90801 : return gfc_conv_descriptor_data_get (descriptor);
3541 : : }
3542 : :
3543 : :
3544 : : /* Return an expression for the base offset of an array. */
3545 : :
3546 : : tree
3547 : 234073 : gfc_conv_array_offset (tree descriptor)
3548 : : {
3549 : 234073 : tree type;
3550 : :
3551 : 234073 : type = TREE_TYPE (descriptor);
3552 : 234073 : if (GFC_ARRAY_TYPE_P (type))
3553 : 168791 : return GFC_TYPE_ARRAY_OFFSET (type);
3554 : : else
3555 : 65282 : return gfc_conv_descriptor_offset_get (descriptor);
3556 : : }
3557 : :
3558 : :
3559 : : /* Get an expression for the array stride. */
3560 : :
3561 : : tree
3562 : 475242 : gfc_conv_array_stride (tree descriptor, int dim)
3563 : : {
3564 : 475242 : tree tmp;
3565 : 475242 : tree type;
3566 : :
3567 : 475242 : type = TREE_TYPE (descriptor);
3568 : :
3569 : : /* For descriptorless arrays use the array size. */
3570 : 475242 : tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
3571 : 475242 : if (tmp != NULL_TREE)
3572 : : return tmp;
3573 : :
3574 : 105993 : tmp = gfc_conv_descriptor_stride_get (descriptor, gfc_rank_cst[dim]);
3575 : 105993 : return tmp;
3576 : : }
3577 : :
3578 : :
3579 : : /* Like gfc_conv_array_stride, but for the lower bound. */
3580 : :
3581 : : tree
3582 : 313970 : gfc_conv_array_lbound (tree descriptor, int dim)
3583 : : {
3584 : 313970 : tree tmp;
3585 : 313970 : tree type;
3586 : :
3587 : 313970 : type = TREE_TYPE (descriptor);
3588 : :
3589 : 313970 : tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
3590 : 313970 : if (tmp != NULL_TREE)
3591 : : return tmp;
3592 : :
3593 : 18166 : tmp = gfc_conv_descriptor_lbound_get (descriptor, gfc_rank_cst[dim]);
3594 : 18166 : return tmp;
3595 : : }
3596 : :
3597 : :
3598 : : /* Like gfc_conv_array_stride, but for the upper bound. */
3599 : :
3600 : : tree
3601 : 203375 : gfc_conv_array_ubound (tree descriptor, int dim)
3602 : : {
3603 : 203375 : tree tmp;
3604 : 203375 : tree type;
3605 : :
3606 : 203375 : type = TREE_TYPE (descriptor);
3607 : :
3608 : 203375 : tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
3609 : 203375 : if (tmp != NULL_TREE)
3610 : : return tmp;
3611 : :
3612 : : /* This should only ever happen when passing an assumed shape array
3613 : : as an actual parameter. The value will never be used. */
3614 : 7437 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
3615 : 553 : return gfc_index_zero_node;
3616 : :
3617 : 6884 : tmp = gfc_conv_descriptor_ubound_get (descriptor, gfc_rank_cst[dim]);
3618 : 6884 : return tmp;
3619 : : }
3620 : :
3621 : :
3622 : : /* Generate abridged name of a part-ref for use in bounds-check message.
3623 : : Cases:
3624 : : (1) for an ordinary array variable x return "x"
3625 : : (2) for z a DT scalar and array component x (at level 1) return "z%%x"
3626 : : (3) for z a DT scalar and array component x (at level > 1) or
3627 : : for z a DT array and array x (at any number of levels): "z...%%x"
3628 : : */
3629 : :
3630 : : static char *
3631 : 36043 : abridged_ref_name (gfc_expr * expr, gfc_array_ref * ar)
3632 : : {
3633 : 36043 : gfc_ref *ref;
3634 : 36043 : gfc_symbol *sym;
3635 : 36043 : char *ref_name = NULL;
3636 : 36043 : const char *comp_name = NULL;
3637 : 36043 : int len_sym, last_len = 0, level = 0;
3638 : 36043 : bool sym_is_array;
3639 : :
3640 : 36043 : gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->ref != NULL);
3641 : :
3642 : 36043 : sym = expr->symtree->n.sym;
3643 : 71819 : sym_is_array = (sym->ts.type != BT_CLASS
3644 : 36043 : ? sym->as != NULL
3645 : 267 : : IS_CLASS_ARRAY (sym));
3646 : 36043 : len_sym = strlen (sym->name);
3647 : :
3648 : : /* Scan ref chain to get name of the array component (when ar != NULL) or
3649 : : array section, determine depth and remember its component name. */
3650 : 51081 : for (ref = expr->ref; ref; ref = ref->next)
3651 : : {
3652 : 37168 : if (ref->type == REF_COMPONENT
3653 : 808 : && strcmp (ref->u.c.component->name, "_data") != 0)
3654 : : {
3655 : 678 : level++;
3656 : 678 : comp_name = ref->u.c.component->name;
3657 : 678 : continue;
3658 : : }
3659 : :
3660 : 36490 : if (ref->type != REF_ARRAY)
3661 : 150 : continue;
3662 : :
3663 : 36340 : if (ar)
3664 : : {
3665 : 15555 : if (&ref->u.ar == ar)
3666 : : break;
3667 : : }
3668 : 20785 : else if (ref->u.ar.type == AR_SECTION)
3669 : : break;
3670 : : }
3671 : :
3672 : 36043 : if (level > 0)
3673 : 644 : last_len = strlen (comp_name);
3674 : :
3675 : : /* Provide a buffer sufficiently large to hold "x...%%z". */
3676 : 36043 : ref_name = XNEWVEC (char, len_sym + last_len + 6);
3677 : 36043 : strcpy (ref_name, sym->name);
3678 : :
3679 : 36043 : if (level == 1 && !sym_is_array)
3680 : : {
3681 : 352 : strcat (ref_name, "%%");
3682 : 352 : strcat (ref_name, comp_name);
3683 : : }
3684 : 35691 : else if (level > 0)
3685 : : {
3686 : 292 : strcat (ref_name, "...%%");
3687 : 292 : strcat (ref_name, comp_name);
3688 : : }
3689 : :
3690 : 36043 : return ref_name;
3691 : : }
3692 : :
3693 : :
3694 : : /* Generate code to perform an array index bound check. */
3695 : :
3696 : : static tree
3697 : 4998 : trans_array_bound_check (gfc_se * se, gfc_ss *ss, tree index, int n,
3698 : : locus * where, bool check_upper,
3699 : : const char *compname = NULL)
3700 : : {
3701 : 4998 : tree fault;
3702 : 4998 : tree tmp_lo, tmp_up;
3703 : 4998 : tree descriptor;
3704 : 4998 : char *msg;
3705 : 4998 : char *ref_name = NULL;
3706 : 4998 : const char * name = NULL;
3707 : 4998 : gfc_expr *expr;
3708 : :
3709 : 4998 : if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
3710 : : return index;
3711 : :
3712 : 234 : descriptor = ss->info->data.array.descriptor;
3713 : :
3714 : 234 : index = gfc_evaluate_now (index, &se->pre);
3715 : :
3716 : : /* We find a name for the error message. */
3717 : 234 : name = ss->info->expr->symtree->n.sym->name;
3718 : 234 : gcc_assert (name != NULL);
3719 : :
3720 : : /* When we have a component ref, get name of the array section.
3721 : : Note that there can only be one part ref. */
3722 : 234 : expr = ss->info->expr;
3723 : 234 : if (expr->ref && !compname)
3724 : 160 : name = ref_name = abridged_ref_name (expr, NULL);
3725 : :
3726 : 234 : if (VAR_P (descriptor))
3727 : 156 : name = IDENTIFIER_POINTER (DECL_NAME (descriptor));
3728 : :
3729 : : /* Use given (array component) name. */
3730 : 234 : if (compname)
3731 : 74 : name = compname;
3732 : :
3733 : : /* If upper bound is present, include both bounds in the error message. */
3734 : 234 : if (check_upper)
3735 : : {
3736 : 207 : tmp_lo = gfc_conv_array_lbound (descriptor, n);
3737 : 207 : tmp_up = gfc_conv_array_ubound (descriptor, n);
3738 : :
3739 : 207 : if (name)
3740 : 207 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3741 : : "outside of expected range (%%ld:%%ld)", n+1, name);
3742 : : else
3743 : 0 : msg = xasprintf ("Index '%%ld' of dimension %d "
3744 : : "outside of expected range (%%ld:%%ld)", n+1);
3745 : :
3746 : 207 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
3747 : : index, tmp_lo);
3748 : 207 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3749 : : fold_convert (long_integer_type_node, index),
3750 : : fold_convert (long_integer_type_node, tmp_lo),
3751 : : fold_convert (long_integer_type_node, tmp_up));
3752 : 207 : fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3753 : : index, tmp_up);
3754 : 207 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3755 : : fold_convert (long_integer_type_node, index),
3756 : : fold_convert (long_integer_type_node, tmp_lo),
3757 : : fold_convert (long_integer_type_node, tmp_up));
3758 : 207 : free (msg);
3759 : : }
3760 : : else
3761 : : {
3762 : 27 : tmp_lo = gfc_conv_array_lbound (descriptor, n);
3763 : :
3764 : 27 : if (name)
3765 : 27 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
3766 : : "below lower bound of %%ld", n+1, name);
3767 : : else
3768 : 0 : msg = xasprintf ("Index '%%ld' of dimension %d "
3769 : : "below lower bound of %%ld", n+1);
3770 : :
3771 : 27 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
3772 : : index, tmp_lo);
3773 : 27 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
3774 : : fold_convert (long_integer_type_node, index),
3775 : : fold_convert (long_integer_type_node, tmp_lo));
3776 : 27 : free (msg);
3777 : : }
3778 : :
3779 : 234 : free (ref_name);
3780 : 234 : return index;
3781 : : }
3782 : :
3783 : :
3784 : : /* Generate code for bounds checking for elemental dimensions. */
3785 : :
3786 : : static void
3787 : 6667 : array_bound_check_elemental (gfc_se * se, gfc_ss * ss, gfc_expr * expr)
3788 : : {
3789 : 6667 : gfc_array_ref *ar;
3790 : 6667 : gfc_ref *ref;
3791 : 6667 : char *var_name = NULL;
3792 : 6667 : int dim;
3793 : :
3794 : 6667 : if (expr->expr_type == EXPR_VARIABLE)
3795 : : {
3796 : 12469 : for (ref = expr->ref; ref; ref = ref->next)
3797 : : {
3798 : 6259 : if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3799 : : {
3800 : 3935 : ar = &ref->u.ar;
3801 : 3935 : var_name = abridged_ref_name (expr, ar);
3802 : 8104 : for (dim = 0; dim < ar->dimen; dim++)
3803 : : {
3804 : 4169 : if (ar->dimen_type[dim] == DIMEN_ELEMENT)
3805 : : {
3806 : 74 : gfc_se indexse;
3807 : 74 : gfc_init_se (&indexse, NULL);
3808 : 74 : gfc_conv_expr_type (&indexse, ar->start[dim],
3809 : : gfc_array_index_type);
3810 : 74 : trans_array_bound_check (se, ss, indexse.expr, dim,
3811 : : &ar->where,
3812 : 74 : ar->as->type != AS_ASSUMED_SIZE
3813 : 74 : || dim < ar->dimen - 1,
3814 : : var_name);
3815 : : }
3816 : : }
3817 : 3935 : free (var_name);
3818 : : }
3819 : : }
3820 : : }
3821 : 6667 : }
3822 : :
3823 : :
3824 : : /* Return the offset for an index. Performs bound checking for elemental
3825 : : dimensions. Single element references are processed separately.
3826 : : DIM is the array dimension, I is the loop dimension. */
3827 : :
3828 : : static tree
3829 : 244191 : conv_array_index_offset (gfc_se * se, gfc_ss * ss, int dim, int i,
3830 : : gfc_array_ref * ar, tree stride)
3831 : : {
3832 : 244191 : gfc_array_info *info;
3833 : 244191 : tree index;
3834 : 244191 : tree desc;
3835 : 244191 : tree data;
3836 : :
3837 : 244191 : info = &ss->info->data.array;
3838 : :
3839 : : /* Get the index into the array for this dimension. */
3840 : 244191 : if (ar)
3841 : : {
3842 : 173275 : gcc_assert (ar->type != AR_ELEMENT);
3843 : 173275 : switch (ar->dimen_type[dim])
3844 : : {
3845 : 0 : case DIMEN_THIS_IMAGE:
3846 : 0 : gcc_unreachable ();
3847 : 4175 : break;
3848 : 4175 : case DIMEN_ELEMENT:
3849 : : /* Elemental dimension. */
3850 : 4175 : gcc_assert (info->subscript[dim]
3851 : : && info->subscript[dim]->info->type == GFC_SS_SCALAR);
3852 : : /* We've already translated this value outside the loop. */
3853 : 4175 : index = info->subscript[dim]->info->data.scalar.value;
3854 : :
3855 : 8350 : index = trans_array_bound_check (se, ss, index, dim, &ar->where,
3856 : 4175 : ar->as->type != AS_ASSUMED_SIZE
3857 : 4175 : || dim < ar->dimen - 1);
3858 : 4175 : break;
3859 : :
3860 : 749 : case DIMEN_VECTOR:
3861 : 749 : gcc_assert (info && se->loop);
3862 : 749 : gcc_assert (info->subscript[dim]
3863 : : && info->subscript[dim]->info->type == GFC_SS_VECTOR);
3864 : 749 : desc = info->subscript[dim]->info->data.array.descriptor;
3865 : :
3866 : : /* Get a zero-based index into the vector. */
3867 : 749 : index = fold_build2_loc (input_location, MINUS_EXPR,
3868 : : gfc_array_index_type,
3869 : : se->loop->loopvar[i], se->loop->from[i]);
3870 : :
3871 : : /* Multiply the index by the stride. */
3872 : 749 : index = fold_build2_loc (input_location, MULT_EXPR,
3873 : : gfc_array_index_type,
3874 : : index, gfc_conv_array_stride (desc, 0));
3875 : :
3876 : : /* Read the vector to get an index into info->descriptor. */
3877 : 749 : data = build_fold_indirect_ref_loc (input_location,
3878 : : gfc_conv_array_data (desc));
3879 : 749 : index = gfc_build_array_ref (data, index, NULL);
3880 : 749 : index = gfc_evaluate_now (index, &se->pre);
3881 : 749 : index = fold_convert (gfc_array_index_type, index);
3882 : :
3883 : : /* Do any bounds checking on the final info->descriptor index. */
3884 : 1498 : index = trans_array_bound_check (se, ss, index, dim, &ar->where,
3885 : 749 : ar->as->type != AS_ASSUMED_SIZE
3886 : 749 : || dim < ar->dimen - 1);
3887 : 749 : break;
3888 : :
3889 : 168351 : case DIMEN_RANGE:
3890 : : /* Scalarized dimension. */
3891 : 168351 : gcc_assert (info && se->loop);
3892 : :
3893 : : /* Multiply the loop variable by the stride and delta. */
3894 : 168351 : index = se->loop->loopvar[i];
3895 : 168351 : if (!integer_onep (info->stride[dim]))
3896 : 6457 : index = fold_build2_loc (input_location, MULT_EXPR,
3897 : : gfc_array_index_type, index,
3898 : : info->stride[dim]);
3899 : 168351 : if (!integer_zerop (info->delta[dim]))
3900 : 63455 : index = fold_build2_loc (input_location, PLUS_EXPR,
3901 : : gfc_array_index_type, index,
3902 : : info->delta[dim]);
3903 : : break;
3904 : :
3905 : 0 : default:
3906 : 0 : gcc_unreachable ();
3907 : : }
3908 : : }
3909 : : else
3910 : : {
3911 : : /* Temporary array or derived type component. */
3912 : 70916 : gcc_assert (se->loop);
3913 : 70916 : index = se->loop->loopvar[se->loop->order[i]];
3914 : :
3915 : : /* Pointer functions can have stride[0] different from unity.
3916 : : Use the stride returned by the function call and stored in
3917 : : the descriptor for the temporary. */
3918 : 70916 : if (se->ss && se->ss->info->type == GFC_SS_FUNCTION
3919 : 7918 : && se->ss->info->expr
3920 : 7918 : && se->ss->info->expr->symtree
3921 : 7918 : && se->ss->info->expr->symtree->n.sym->result
3922 : 7505 : && se->ss->info->expr->symtree->n.sym->result->attr.pointer)
3923 : 144 : stride = gfc_conv_descriptor_stride_get (info->descriptor,
3924 : : gfc_rank_cst[dim]);
3925 : :
3926 : 70916 : if (info->delta[dim] && !integer_zerop (info->delta[dim]))
3927 : 702 : index = fold_build2_loc (input_location, PLUS_EXPR,
3928 : : gfc_array_index_type, index, info->delta[dim]);
3929 : : }
3930 : :
3931 : : /* Multiply by the stride. */
3932 : 244191 : if (stride != NULL && !integer_onep (stride))
3933 : 73931 : index = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
3934 : : index, stride);
3935 : :
3936 : 244191 : return index;
3937 : : }
3938 : :
3939 : :
3940 : : /* Build a scalarized array reference using the vptr 'size'. */
3941 : :
3942 : : static bool
3943 : 186816 : build_class_array_ref (gfc_se *se, tree base, tree index)
3944 : : {
3945 : 186816 : tree size;
3946 : 186816 : tree decl = NULL_TREE;
3947 : 186816 : tree tmp;
3948 : 186816 : gfc_expr *expr = se->ss->info->expr;
3949 : 186816 : gfc_expr *class_expr;
3950 : 186816 : gfc_typespec *ts;
3951 : 186816 : gfc_symbol *sym;
3952 : :
3953 : 186816 : tmp = !VAR_P (base) ? gfc_get_class_from_expr (base) : NULL_TREE;
3954 : :
3955 : 86404 : if (tmp != NULL_TREE)
3956 : : decl = tmp;
3957 : : else
3958 : : {
3959 : : /* The base expression does not contain a class component, either
3960 : : because it is a temporary array or array descriptor. Class
3961 : : array functions are correctly resolved above. */
3962 : 183636 : if (!expr
3963 : 183636 : || (expr->ts.type != BT_CLASS
3964 : 170293 : && !gfc_is_class_array_ref (expr, NULL)))
3965 : 183249 : return false;
3966 : :
3967 : : /* Obtain the expression for the class entity or component that is
3968 : : followed by an array reference, which is not an element, so that
3969 : : the span of the array can be obtained. */
3970 : 387 : class_expr = gfc_find_and_cut_at_last_class_ref (expr, false, &ts);
3971 : :
3972 : 387 : if (!ts)
3973 : : return false;
3974 : :
3975 : 362 : sym = (!class_expr && expr) ? expr->symtree->n.sym : NULL;
3976 : 0 : if (sym && sym->attr.function
3977 : 0 : && sym == sym->result
3978 : 0 : && sym->backend_decl == current_function_decl)
3979 : : /* The temporary is the data field of the class data component
3980 : : of the current function. */
3981 : 0 : decl = gfc_get_fake_result_decl (sym, 0);
3982 : 362 : else if (sym)
3983 : : {
3984 : 0 : if (decl == NULL_TREE)
3985 : 0 : decl = expr->symtree->n.sym->backend_decl;
3986 : : /* For class arrays the tree containing the class is stored in
3987 : : GFC_DECL_SAVED_DESCRIPTOR of the sym's backend_decl.
3988 : : For all others it's sym's backend_decl directly. */
3989 : 0 : if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
3990 : 0 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
3991 : : }
3992 : : else
3993 : 362 : decl = gfc_get_class_from_gfc_expr (class_expr);
3994 : :
3995 : 362 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
3996 : 0 : decl = build_fold_indirect_ref_loc (input_location, decl);
3997 : :
3998 : 362 : if (!GFC_CLASS_TYPE_P (TREE_TYPE (decl)))
3999 : : return false;
4000 : : }
4001 : :
4002 : 3542 : se->class_vptr = gfc_evaluate_now (gfc_class_vptr_get (decl), &se->pre);
4003 : :
4004 : 3542 : size = gfc_class_vtab_size_get (decl);
4005 : : /* For unlimited polymorphic entities then _len component needs to be
4006 : : multiplied with the size. */
4007 : 3542 : size = gfc_resize_class_size_with_len (&se->pre, decl, size);
4008 : 3542 : size = fold_convert (TREE_TYPE (index), size);
4009 : :
4010 : : /* Return the element in the se expression. */
4011 : 3542 : se->expr = gfc_build_spanned_array_ref (base, index, size);
4012 : 3542 : return true;
4013 : : }
4014 : :
4015 : :
4016 : : /* Indicates that the tree EXPR is a reference to an array that can’t
4017 : : have any negative stride. */
4018 : :
4019 : : static bool
4020 : 297749 : non_negative_strides_array_p (tree expr)
4021 : : {
4022 : 310253 : if (expr == NULL_TREE)
4023 : : return false;
4024 : :
4025 : 310253 : tree type = TREE_TYPE (expr);
4026 : 310253 : if (POINTER_TYPE_P (type))
4027 : 65206 : type = TREE_TYPE (type);
4028 : :
4029 : 310253 : if (TYPE_LANG_SPECIFIC (type))
4030 : : {
4031 : 310253 : gfc_array_kind array_kind = GFC_TYPE_ARRAY_AKIND (type);
4032 : :
4033 : 310253 : if (array_kind == GFC_ARRAY_ALLOCATABLE
4034 : 310253 : || array_kind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
4035 : : return true;
4036 : : }
4037 : :
4038 : : /* An array with descriptor can have negative strides.
4039 : : We try to be conservative and return false by default here
4040 : : if we don’t recognize a contiguous array instead of
4041 : : returning false if we can identify a non-contiguous one. */
4042 : 257811 : if (!GFC_ARRAY_TYPE_P (type))
4043 : : return false;
4044 : :
4045 : : /* If the array was originally a dummy with a descriptor, strides can be
4046 : : negative. */
4047 : 225944 : if (DECL_P (expr)
4048 : 217040 : && DECL_LANG_SPECIFIC (expr)
4049 : 45589 : && GFC_DECL_SAVED_DESCRIPTOR (expr)
4050 : 238467 : && GFC_DECL_SAVED_DESCRIPTOR (expr) != expr)
4051 : 12504 : return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (expr));
4052 : :
4053 : : return true;
4054 : : }
4055 : :
4056 : :
4057 : : /* Build a scalarized reference to an array. */
4058 : :
4059 : : static void
4060 : 186816 : gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar,
4061 : : bool tmp_array = false)
4062 : : {
4063 : 186816 : gfc_array_info *info;
4064 : 186816 : tree decl = NULL_TREE;
4065 : 186816 : tree index;
4066 : 186816 : tree base;
4067 : 186816 : gfc_ss *ss;
4068 : 186816 : gfc_expr *expr;
4069 : 186816 : int n;
4070 : :
4071 : 186816 : ss = se->ss;
4072 : 186816 : expr = ss->info->expr;
4073 : 186816 : info = &ss->info->data.array;
4074 : 186816 : if (ar)
4075 : 127550 : n = se->loop->order[0];
4076 : : else
4077 : : n = 0;
4078 : :
4079 : 186816 : index = conv_array_index_offset (se, ss, ss->dim[n], n, ar, info->stride0);
4080 : : /* Add the offset for this dimension to the stored offset for all other
4081 : : dimensions. */
4082 : 186816 : if (info->offset && !integer_zerop (info->offset))
4083 : 136727 : index = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4084 : : index, info->offset);
4085 : :
4086 : 186816 : base = build_fold_indirect_ref_loc (input_location, info->data);
4087 : :
4088 : : /* Use the vptr 'size' field to access the element of a class array. */
4089 : 186816 : if (build_class_array_ref (se, base, index))
4090 : 3542 : return;
4091 : :
4092 : 183274 : if (get_CFI_desc (NULL, expr, &decl, ar))
4093 : 442 : decl = build_fold_indirect_ref_loc (input_location, decl);
4094 : :
4095 : : /* A pointer array component can be detected from its field decl. Fix
4096 : : the descriptor, mark the resulting variable decl and pass it to
4097 : : gfc_build_array_ref. */
4098 : 183274 : if (is_pointer_array (info->descriptor)
4099 : 183274 : || (expr && expr->ts.deferred && info->descriptor
4100 : 2527 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (info->descriptor))))
4101 : : {
4102 : 8384 : if (TREE_CODE (info->descriptor) == COMPONENT_REF)
4103 : 1468 : decl = info->descriptor;
4104 : 6916 : else if (INDIRECT_REF_P (info->descriptor))
4105 : 1427 : decl = TREE_OPERAND (info->descriptor, 0);
4106 : :
4107 : 8384 : if (decl == NULL_TREE)
4108 : 5489 : decl = info->descriptor;
4109 : : }
4110 : :
4111 : 183274 : bool non_negative_stride = tmp_array
4112 : 183274 : || non_negative_strides_array_p (info->descriptor);
4113 : 183274 : se->expr = gfc_build_array_ref (base, index, decl,
4114 : : non_negative_stride);
4115 : : }
4116 : :
4117 : :
4118 : : /* Translate access of temporary array. */
4119 : :
4120 : : void
4121 : 59266 : gfc_conv_tmp_array_ref (gfc_se * se)
4122 : : {
4123 : 59266 : se->string_length = se->ss->info->string_length;
4124 : 59266 : gfc_conv_scalarized_array_ref (se, NULL, true);
4125 : 59266 : gfc_advance_se_ss_chain (se);
4126 : 59266 : }
4127 : :
4128 : : /* Add T to the offset pair *OFFSET, *CST_OFFSET. */
4129 : :
4130 : : static void
4131 : 257585 : add_to_offset (tree *cst_offset, tree *offset, tree t)
4132 : : {
4133 : 257585 : if (TREE_CODE (t) == INTEGER_CST)
4134 : 129195 : *cst_offset = int_const_binop (PLUS_EXPR, *cst_offset, t);
4135 : : else
4136 : : {
4137 : 128390 : if (!integer_zerop (*offset))
4138 : 46746 : *offset = fold_build2_loc (input_location, PLUS_EXPR,
4139 : : gfc_array_index_type, *offset, t);
4140 : : else
4141 : 81644 : *offset = t;
4142 : : }
4143 : 257585 : }
4144 : :
4145 : :
4146 : : static tree
4147 : 173359 : build_array_ref (tree desc, tree offset, tree decl, tree vptr)
4148 : : {
4149 : 173359 : tree tmp;
4150 : 173359 : tree type;
4151 : 173359 : tree cdesc;
4152 : :
4153 : : /* For class arrays the class declaration is stored in the saved
4154 : : descriptor. */
4155 : 173359 : if (INDIRECT_REF_P (desc)
4156 : 7253 : && DECL_LANG_SPECIFIC (TREE_OPERAND (desc, 0))
4157 : 175611 : && GFC_DECL_SAVED_DESCRIPTOR (TREE_OPERAND (desc, 0)))
4158 : 822 : cdesc = gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (
4159 : : TREE_OPERAND (desc, 0)));
4160 : : else
4161 : : cdesc = desc;
4162 : :
4163 : : /* Class container types do not always have the GFC_CLASS_TYPE_P
4164 : : but the canonical type does. */
4165 : 173359 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (cdesc))
4166 : 173359 : && TREE_CODE (cdesc) == COMPONENT_REF)
4167 : : {
4168 : 9225 : type = TREE_TYPE (TREE_OPERAND (cdesc, 0));
4169 : 9225 : if (TYPE_CANONICAL (type)
4170 : 9225 : && GFC_CLASS_TYPE_P (TYPE_CANONICAL (type)))
4171 : 3417 : vptr = gfc_class_vptr_get (TREE_OPERAND (cdesc, 0));
4172 : : }
4173 : :
4174 : 173359 : tmp = gfc_conv_array_data (desc);
4175 : 173359 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
4176 : 173359 : tmp = gfc_build_array_ref (tmp, offset, decl,
4177 : 173359 : non_negative_strides_array_p (desc),
4178 : : vptr);
4179 : 173359 : return tmp;
4180 : : }
4181 : :
4182 : :
4183 : : /* Build an array reference. se->expr already holds the array descriptor.
4184 : : This should be either a variable, indirect variable reference or component
4185 : : reference. For arrays which do not have a descriptor, se->expr will be
4186 : : the data pointer.
4187 : : a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
4188 : :
4189 : : void
4190 : 246776 : gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_expr *expr,
4191 : : locus * where)
4192 : : {
4193 : 246776 : int n;
4194 : 246776 : tree offset, cst_offset;
4195 : 246776 : tree tmp;
4196 : 246776 : tree stride;
4197 : 246776 : tree decl = NULL_TREE;
4198 : 246776 : gfc_se indexse;
4199 : 246776 : gfc_se tmpse;
4200 : 246776 : gfc_symbol * sym = expr->symtree->n.sym;
4201 : 246776 : char *var_name = NULL;
4202 : :
4203 : 246776 : if (ar->stat)
4204 : : {
4205 : 3 : gfc_se statse;
4206 : :
4207 : 3 : gfc_init_se (&statse, NULL);
4208 : 3 : gfc_conv_expr_lhs (&statse, ar->stat);
4209 : 3 : gfc_add_block_to_block (&se->pre, &statse.pre);
4210 : 3 : gfc_add_modify (&se->pre, statse.expr, integer_zero_node);
4211 : : }
4212 : 246776 : if (ar->dimen == 0)
4213 : : {
4214 : 3677 : gcc_assert (ar->codimen || sym->attr.select_rank_temporary
4215 : : || (ar->as && ar->as->corank));
4216 : :
4217 : 3677 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
4218 : 812 : se->expr = build_fold_indirect_ref (gfc_conv_array_data (se->expr));
4219 : : else
4220 : : {
4221 : 2865 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr))
4222 : 2865 : && TREE_CODE (TREE_TYPE (se->expr)) == POINTER_TYPE)
4223 : 1935 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
4224 : :
4225 : : /* Use the actual tree type and not the wrapped coarray. */
4226 : 2865 : if (!se->want_pointer)
4227 : 2042 : se->expr = fold_convert (TYPE_MAIN_VARIANT (TREE_TYPE (se->expr)),
4228 : : se->expr);
4229 : : }
4230 : :
4231 : 131227 : return;
4232 : : }
4233 : :
4234 : : /* Handle scalarized references separately. */
4235 : 243099 : if (ar->type != AR_ELEMENT)
4236 : : {
4237 : 127550 : gfc_conv_scalarized_array_ref (se, ar);
4238 : 127550 : gfc_advance_se_ss_chain (se);
4239 : 127550 : return;
4240 : : }
4241 : :
4242 : 115549 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
4243 : 11493 : var_name = abridged_ref_name (expr, ar);
4244 : :
4245 : 115549 : decl = se->expr;
4246 : 115549 : if (UNLIMITED_POLY(sym)
4247 : 98 : && IS_CLASS_ARRAY (sym)
4248 : 97 : && sym->attr.dummy
4249 : 54 : && ar->as->type != AS_DEFERRED)
4250 : 42 : decl = sym->backend_decl;
4251 : :
4252 : 115549 : cst_offset = offset = gfc_index_zero_node;
4253 : 115549 : add_to_offset (&cst_offset, &offset, gfc_conv_array_offset (decl));
4254 : :
4255 : : /* Calculate the offsets from all the dimensions. Make sure to associate
4256 : : the final offset so that we form a chain of loop invariant summands. */
4257 : 257585 : for (n = ar->dimen - 1; n >= 0; n--)
4258 : : {
4259 : : /* Calculate the index for this dimension. */
4260 : 142036 : gfc_init_se (&indexse, se);
4261 : 142036 : gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
4262 : 142036 : gfc_add_block_to_block (&se->pre, &indexse.pre);
4263 : :
4264 : 142036 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && ! expr->no_bounds_check)
4265 : : {
4266 : : /* Check array bounds. */
4267 : 15035 : tree cond;
4268 : 15035 : char *msg;
4269 : :
4270 : : /* Evaluate the indexse.expr only once. */
4271 : 15035 : indexse.expr = save_expr (indexse.expr);
4272 : :
4273 : : /* Lower bound. */
4274 : 15035 : tmp = gfc_conv_array_lbound (decl, n);
4275 : 15035 : if (sym->attr.temporary)
4276 : : {
4277 : 18 : gfc_init_se (&tmpse, se);
4278 : 18 : gfc_conv_expr_type (&tmpse, ar->as->lower[n],
4279 : : gfc_array_index_type);
4280 : 18 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
4281 : 18 : tmp = tmpse.expr;
4282 : : }
4283 : :
4284 : 15035 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
4285 : : indexse.expr, tmp);
4286 : 15035 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4287 : : "below lower bound of %%ld", n+1, var_name);
4288 : 15035 : gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
4289 : : fold_convert (long_integer_type_node,
4290 : : indexse.expr),
4291 : : fold_convert (long_integer_type_node, tmp));
4292 : 15035 : free (msg);
4293 : :
4294 : : /* Upper bound, but not for the last dimension of assumed-size
4295 : : arrays. */
4296 : 15035 : if (n < ar->dimen - 1 || ar->as->type != AS_ASSUMED_SIZE)
4297 : : {
4298 : 13302 : tmp = gfc_conv_array_ubound (decl, n);
4299 : 13302 : if (sym->attr.temporary)
4300 : : {
4301 : 18 : gfc_init_se (&tmpse, se);
4302 : 18 : gfc_conv_expr_type (&tmpse, ar->as->upper[n],
4303 : : gfc_array_index_type);
4304 : 18 : gfc_add_block_to_block (&se->pre, &tmpse.pre);
4305 : 18 : tmp = tmpse.expr;
4306 : : }
4307 : :
4308 : 13302 : cond = fold_build2_loc (input_location, GT_EXPR,
4309 : : logical_type_node, indexse.expr, tmp);
4310 : 13302 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
4311 : : "above upper bound of %%ld", n+1, var_name);
4312 : 13302 : gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
4313 : : fold_convert (long_integer_type_node,
4314 : : indexse.expr),
4315 : : fold_convert (long_integer_type_node, tmp));
4316 : 13302 : free (msg);
4317 : : }
4318 : : }
4319 : :
4320 : : /* Multiply the index by the stride. */
4321 : 142036 : stride = gfc_conv_array_stride (decl, n);
4322 : 142036 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4323 : : indexse.expr, stride);
4324 : :
4325 : : /* And add it to the total. */
4326 : 142036 : add_to_offset (&cst_offset, &offset, tmp);
4327 : : }
4328 : :
4329 : 115549 : if (!integer_zerop (cst_offset))
4330 : 61726 : offset = fold_build2_loc (input_location, PLUS_EXPR,
4331 : : gfc_array_index_type, offset, cst_offset);
4332 : :
4333 : : /* A pointer array component can be detected from its field decl. Fix
4334 : : the descriptor, mark the resulting variable decl and pass it to
4335 : : build_array_ref. */
4336 : 115549 : decl = NULL_TREE;
4337 : 115549 : if (get_CFI_desc (sym, expr, &decl, ar))
4338 : 3589 : decl = build_fold_indirect_ref_loc (input_location, decl);
4339 : 114789 : if (!expr->ts.deferred && !sym->attr.codimension
4340 : 228636 : && is_pointer_array (se->expr))
4341 : : {
4342 : 4855 : if (TREE_CODE (se->expr) == COMPONENT_REF)
4343 : 1454 : decl = se->expr;
4344 : 3401 : else if (INDIRECT_REF_P (se->expr))
4345 : 983 : decl = TREE_OPERAND (se->expr, 0);
4346 : : else
4347 : 2418 : decl = se->expr;
4348 : : }
4349 : 110694 : else if (expr->ts.deferred
4350 : 109934 : || (sym->ts.type == BT_CHARACTER
4351 : 14390 : && sym->attr.select_type_temporary))
4352 : : {
4353 : 2464 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
4354 : : {
4355 : 2428 : decl = se->expr;
4356 : 2428 : if (INDIRECT_REF_P (decl))
4357 : 20 : decl = TREE_OPERAND (decl, 0);
4358 : : }
4359 : : else
4360 : 36 : decl = sym->backend_decl;
4361 : : }
4362 : 108230 : else if (sym->ts.type == BT_CLASS)
4363 : : {
4364 : 2072 : if (UNLIMITED_POLY (sym))
4365 : : {
4366 : 98 : gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
4367 : 98 : gfc_init_se (&tmpse, NULL);
4368 : 98 : gfc_conv_expr (&tmpse, class_expr);
4369 : 98 : if (!se->class_vptr)
4370 : 98 : se->class_vptr = gfc_class_vptr_get (tmpse.expr);
4371 : 98 : gfc_free_expr (class_expr);
4372 : 98 : decl = tmpse.expr;
4373 : 98 : }
4374 : : else
4375 : 1974 : decl = NULL_TREE;
4376 : : }
4377 : :
4378 : 115549 : free (var_name);
4379 : 115549 : se->expr = build_array_ref (se->expr, offset, decl, se->class_vptr);
4380 : : }
4381 : :
4382 : :
4383 : : /* Add the offset corresponding to array's ARRAY_DIM dimension and loop's
4384 : : LOOP_DIM dimension (if any) to array's offset. */
4385 : :
4386 : : static void
4387 : 57375 : add_array_offset (stmtblock_t *pblock, gfc_loopinfo *loop, gfc_ss *ss,
4388 : : gfc_array_ref *ar, int array_dim, int loop_dim)
4389 : : {
4390 : 57375 : gfc_se se;
4391 : 57375 : gfc_array_info *info;
4392 : 57375 : tree stride, index;
4393 : :
4394 : 57375 : info = &ss->info->data.array;
4395 : :
4396 : 57375 : gfc_init_se (&se, NULL);
4397 : 57375 : se.loop = loop;
4398 : 57375 : se.expr = info->descriptor;
4399 : 57375 : stride = gfc_conv_array_stride (info->descriptor, array_dim);
4400 : 57375 : index = conv_array_index_offset (&se, ss, array_dim, loop_dim, ar, stride);
4401 : 57375 : gfc_add_block_to_block (pblock, &se.pre);
4402 : :
4403 : 57375 : info->offset = fold_build2_loc (input_location, PLUS_EXPR,
4404 : : gfc_array_index_type,
4405 : : info->offset, index);
4406 : 57375 : info->offset = gfc_evaluate_now (info->offset, pblock);
4407 : 57375 : }
4408 : :
4409 : :
4410 : : /* Generate the code to be executed immediately before entering a
4411 : : scalarization loop. */
4412 : :
4413 : : static void
4414 : 141505 : gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
4415 : : stmtblock_t * pblock)
4416 : : {
4417 : 141505 : tree stride;
4418 : 141505 : gfc_ss_info *ss_info;
4419 : 141505 : gfc_array_info *info;
4420 : 141505 : gfc_ss_type ss_type;
4421 : 141505 : gfc_ss *ss, *pss;
4422 : 141505 : gfc_loopinfo *ploop;
4423 : 141505 : gfc_array_ref *ar;
4424 : :
4425 : : /* This code will be executed before entering the scalarization loop
4426 : : for this dimension. */
4427 : 430858 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4428 : : {
4429 : 289353 : ss_info = ss->info;
4430 : :
4431 : 289353 : if ((ss_info->useflags & flag) == 0)
4432 : 1468 : continue;
4433 : :
4434 : 287885 : ss_type = ss_info->type;
4435 : 351400 : if (ss_type != GFC_SS_SECTION
4436 : : && ss_type != GFC_SS_FUNCTION
4437 : 287885 : && ss_type != GFC_SS_CONSTRUCTOR
4438 : 287885 : && ss_type != GFC_SS_COMPONENT)
4439 : 63515 : continue;
4440 : :
4441 : 224370 : info = &ss_info->data.array;
4442 : :
4443 : 224370 : gcc_assert (dim < ss->dimen);
4444 : 224370 : gcc_assert (ss->dimen == loop->dimen);
4445 : :
4446 : 224370 : if (info->ref)
4447 : 157950 : ar = &info->ref->u.ar;
4448 : : else
4449 : : ar = NULL;
4450 : :
4451 : 224370 : if (dim == loop->dimen - 1 && loop->parent != NULL)
4452 : : {
4453 : : /* If we are in the outermost dimension of this loop, the previous
4454 : : dimension shall be in the parent loop. */
4455 : 4687 : gcc_assert (ss->parent != NULL);
4456 : :
4457 : 4687 : pss = ss->parent;
4458 : 4687 : ploop = loop->parent;
4459 : :
4460 : : /* ss and ss->parent are about the same array. */
4461 : 4687 : gcc_assert (ss_info == pss->info);
4462 : : }
4463 : : else
4464 : : {
4465 : : ploop = loop;
4466 : : pss = ss;
4467 : : }
4468 : :
4469 : 224370 : if (dim == loop->dimen - 1 && loop->parent == NULL)
4470 : : {
4471 : 171170 : gcc_assert (0 == ploop->order[0]);
4472 : :
4473 : 342340 : stride = gfc_conv_array_stride (info->descriptor,
4474 : 171170 : innermost_ss (ss)->dim[0]);
4475 : :
4476 : : /* Calculate the stride of the innermost loop. Hopefully this will
4477 : : allow the backend optimizers to do their stuff more effectively.
4478 : : */
4479 : 171170 : info->stride0 = gfc_evaluate_now (stride, pblock);
4480 : :
4481 : : /* For the outermost loop calculate the offset due to any
4482 : : elemental dimensions. It will have been initialized with the
4483 : : base offset of the array. */
4484 : 171170 : if (info->ref)
4485 : : {
4486 : 276152 : for (int i = 0; i < ar->dimen; i++)
4487 : : {
4488 : 159752 : if (ar->dimen_type[i] != DIMEN_ELEMENT)
4489 : 155577 : continue;
4490 : :
4491 : 4175 : add_array_offset (pblock, loop, ss, ar, i, /* unused */ -1);
4492 : : }
4493 : : }
4494 : : }
4495 : : else
4496 : : {
4497 : 53200 : int i;
4498 : :
4499 : 53200 : if (dim == loop->dimen - 1)
4500 : : i = 0;
4501 : : else
4502 : 48513 : i = dim + 1;
4503 : :
4504 : : /* For the time being, there is no loop reordering. */
4505 : 53200 : gcc_assert (i == ploop->order[i]);
4506 : 53200 : i = ploop->order[i];
4507 : :
4508 : : /* Add the offset for the previous loop dimension. */
4509 : 53200 : add_array_offset (pblock, ploop, ss, ar, pss->dim[i], i);
4510 : : }
4511 : :
4512 : : /* Remember this offset for the second loop. */
4513 : 224370 : if (dim == loop->temp_dim - 1 && loop->parent == NULL)
4514 : 53813 : info->saved_offset = info->offset;
4515 : : }
4516 : 141505 : }
4517 : :
4518 : :
4519 : : /* Start a scalarized expression. Creates a scope and declares loop
4520 : : variables. */
4521 : :
4522 : : void
4523 : 112091 : gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
4524 : : {
4525 : 112091 : int dim;
4526 : 112091 : int n;
4527 : 112091 : int flags;
4528 : :
4529 : 112091 : gcc_assert (!loop->array_parameter);
4530 : :
4531 : 252017 : for (dim = loop->dimen - 1; dim >= 0; dim--)
4532 : : {
4533 : 139926 : n = loop->order[dim];
4534 : :
4535 : 139926 : gfc_start_block (&loop->code[n]);
4536 : :
4537 : : /* Create the loop variable. */
4538 : 139926 : loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
4539 : :
4540 : 139926 : if (dim < loop->temp_dim)
4541 : : flags = 3;
4542 : : else
4543 : 94384 : flags = 1;
4544 : : /* Calculate values that will be constant within this loop. */
4545 : 139926 : gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
4546 : : }
4547 : 112091 : gfc_start_block (pbody);
4548 : 112091 : }
4549 : :
4550 : :
4551 : : /* Generates the actual loop code for a scalarization loop. */
4552 : :
4553 : : static void
4554 : 153824 : gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
4555 : : stmtblock_t * pbody)
4556 : : {
4557 : 153824 : stmtblock_t block;
4558 : 153824 : tree cond;
4559 : 153824 : tree tmp;
4560 : 153824 : tree loopbody;
4561 : 153824 : tree exit_label;
4562 : 153824 : tree stmt;
4563 : 153824 : tree init;
4564 : 153824 : tree incr;
4565 : :
4566 : 153824 : if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS
4567 : : | OMPWS_SCALARIZER_BODY))
4568 : : == (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS)
4569 : 108 : && n == loop->dimen - 1)
4570 : : {
4571 : : /* We create an OMP_FOR construct for the outermost scalarized loop. */
4572 : 80 : init = make_tree_vec (1);
4573 : 80 : cond = make_tree_vec (1);
4574 : 80 : incr = make_tree_vec (1);
4575 : :
4576 : : /* Cycle statement is implemented with a goto. Exit statement must not
4577 : : be present for this loop. */
4578 : 80 : exit_label = gfc_build_label_decl (NULL_TREE);
4579 : 80 : TREE_USED (exit_label) = 1;
4580 : :
4581 : : /* Label for cycle statements (if needed). */
4582 : 80 : tmp = build1_v (LABEL_EXPR, exit_label);
4583 : 80 : gfc_add_expr_to_block (pbody, tmp);
4584 : :
4585 : 80 : stmt = make_node (OMP_FOR);
4586 : :
4587 : 80 : TREE_TYPE (stmt) = void_type_node;
4588 : 80 : OMP_FOR_BODY (stmt) = loopbody = gfc_finish_block (pbody);
4589 : :
4590 : 80 : OMP_FOR_CLAUSES (stmt) = build_omp_clause (input_location,
4591 : : OMP_CLAUSE_SCHEDULE);
4592 : 80 : OMP_CLAUSE_SCHEDULE_KIND (OMP_FOR_CLAUSES (stmt))
4593 : 80 : = OMP_CLAUSE_SCHEDULE_STATIC;
4594 : 80 : if (ompws_flags & OMPWS_NOWAIT)
4595 : 33 : OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (stmt))
4596 : 66 : = build_omp_clause (input_location, OMP_CLAUSE_NOWAIT);
4597 : :
4598 : : /* Initialize the loopvar. */
4599 : 80 : TREE_VEC_ELT (init, 0) = build2_v (MODIFY_EXPR, loop->loopvar[n],
4600 : : loop->from[n]);
4601 : 80 : OMP_FOR_INIT (stmt) = init;
4602 : : /* The exit condition. */
4603 : 80 : TREE_VEC_ELT (cond, 0) = build2_loc (input_location, LE_EXPR,
4604 : : logical_type_node,
4605 : : loop->loopvar[n], loop->to[n]);
4606 : 80 : SET_EXPR_LOCATION (TREE_VEC_ELT (cond, 0), input_location);
4607 : 80 : OMP_FOR_COND (stmt) = cond;
4608 : : /* Increment the loopvar. */
4609 : 80 : tmp = build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4610 : : loop->loopvar[n], gfc_index_one_node);
4611 : 80 : TREE_VEC_ELT (incr, 0) = fold_build2_loc (input_location, MODIFY_EXPR,
4612 : : void_type_node, loop->loopvar[n], tmp);
4613 : 80 : OMP_FOR_INCR (stmt) = incr;
4614 : :
4615 : 80 : ompws_flags &= ~OMPWS_CURR_SINGLEUNIT;
4616 : 80 : gfc_add_expr_to_block (&loop->code[n], stmt);
4617 : : }
4618 : : else
4619 : : {
4620 : 307488 : bool reverse_loop = (loop->reverse[n] == GFC_REVERSE_SET)
4621 : 153744 : && (loop->temp_ss == NULL);
4622 : :
4623 : 153744 : loopbody = gfc_finish_block (pbody);
4624 : :
4625 : 153744 : if (reverse_loop)
4626 : 202 : std::swap (loop->from[n], loop->to[n]);
4627 : :
4628 : : /* Initialize the loopvar. */
4629 : 153744 : if (loop->loopvar[n] != loop->from[n])
4630 : 152924 : gfc_add_modify (&loop->code[n], loop->loopvar[n], loop->from[n]);
4631 : :
4632 : 153744 : exit_label = gfc_build_label_decl (NULL_TREE);
4633 : :
4634 : : /* Generate the loop body. */
4635 : 153744 : gfc_init_block (&block);
4636 : :
4637 : : /* The exit condition. */
4638 : 307286 : cond = fold_build2_loc (input_location, reverse_loop ? LT_EXPR : GT_EXPR,
4639 : : logical_type_node, loop->loopvar[n], loop->to[n]);
4640 : 153744 : tmp = build1_v (GOTO_EXPR, exit_label);
4641 : 153744 : TREE_USED (exit_label) = 1;
4642 : 153744 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
4643 : 153744 : gfc_add_expr_to_block (&block, tmp);
4644 : :
4645 : : /* The main body. */
4646 : 153744 : gfc_add_expr_to_block (&block, loopbody);
4647 : :
4648 : : /* Increment the loopvar. */
4649 : 307286 : tmp = fold_build2_loc (input_location,
4650 : : reverse_loop ? MINUS_EXPR : PLUS_EXPR,
4651 : : gfc_array_index_type, loop->loopvar[n],
4652 : : gfc_index_one_node);
4653 : :
4654 : 153744 : gfc_add_modify (&block, loop->loopvar[n], tmp);
4655 : :
4656 : : /* Build the loop. */
4657 : 153744 : tmp = gfc_finish_block (&block);
4658 : 153744 : tmp = build1_v (LOOP_EXPR, tmp);
4659 : 153744 : gfc_add_expr_to_block (&loop->code[n], tmp);
4660 : :
4661 : : /* Add the exit label. */
4662 : 153744 : tmp = build1_v (LABEL_EXPR, exit_label);
4663 : 153744 : gfc_add_expr_to_block (&loop->code[n], tmp);
4664 : : }
4665 : :
4666 : 153824 : }
4667 : :
4668 : :
4669 : : /* Finishes and generates the loops for a scalarized expression. */
4670 : :
4671 : : void
4672 : 116328 : gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
4673 : : {
4674 : 116328 : int dim;
4675 : 116328 : int n;
4676 : 116328 : gfc_ss *ss;
4677 : 116328 : stmtblock_t *pblock;
4678 : 116328 : tree tmp;
4679 : :
4680 : 116328 : pblock = body;
4681 : : /* Generate the loops. */
4682 : 260482 : for (dim = 0; dim < loop->dimen; dim++)
4683 : : {
4684 : 144154 : n = loop->order[dim];
4685 : 144154 : gfc_trans_scalarized_loop_end (loop, n, pblock);
4686 : 144154 : loop->loopvar[n] = NULL_TREE;
4687 : 144154 : pblock = &loop->code[n];
4688 : : }
4689 : :
4690 : 116328 : tmp = gfc_finish_block (pblock);
4691 : 116328 : gfc_add_expr_to_block (&loop->pre, tmp);
4692 : :
4693 : : /* Clear all the used flags. */
4694 : 343189 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4695 : 226861 : if (ss->parent == NULL)
4696 : 222111 : ss->info->useflags = 0;
4697 : 116328 : }
4698 : :
4699 : :
4700 : : /* Finish the main body of a scalarized expression, and start the secondary
4701 : : copying body. */
4702 : :
4703 : : void
4704 : 8091 : gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
4705 : : {
4706 : 8091 : int dim;
4707 : 8091 : int n;
4708 : 8091 : stmtblock_t *pblock;
4709 : 8091 : gfc_ss *ss;
4710 : :
4711 : 8091 : pblock = body;
4712 : : /* We finish as many loops as are used by the temporary. */
4713 : 9670 : for (dim = 0; dim < loop->temp_dim - 1; dim++)
4714 : : {
4715 : 1579 : n = loop->order[dim];
4716 : 1579 : gfc_trans_scalarized_loop_end (loop, n, pblock);
4717 : 1579 : loop->loopvar[n] = NULL_TREE;
4718 : 1579 : pblock = &loop->code[n];
4719 : : }
4720 : :
4721 : : /* We don't want to finish the outermost loop entirely. */
4722 : 8091 : n = loop->order[loop->temp_dim - 1];
4723 : 8091 : gfc_trans_scalarized_loop_end (loop, n, pblock);
4724 : :
4725 : : /* Restore the initial offsets. */
4726 : 23031 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
4727 : : {
4728 : 14940 : gfc_ss_type ss_type;
4729 : 14940 : gfc_ss_info *ss_info;
4730 : :
4731 : 14940 : ss_info = ss->info;
4732 : :
4733 : 14940 : if ((ss_info->useflags & 2) == 0)
4734 : 4399 : continue;
4735 : :
4736 : 10541 : ss_type = ss_info->type;
4737 : 10695 : if (ss_type != GFC_SS_SECTION
4738 : : && ss_type != GFC_SS_FUNCTION
4739 : 10541 : && ss_type != GFC_SS_CONSTRUCTOR
4740 : 10541 : && ss_type != GFC_SS_COMPONENT)
4741 : 154 : continue;
4742 : :
4743 : 10387 : ss_info->data.array.offset = ss_info->data.array.saved_offset;
4744 : : }
4745 : :
4746 : : /* Restart all the inner loops we just finished. */
4747 : 9670 : for (dim = loop->temp_dim - 2; dim >= 0; dim--)
4748 : : {
4749 : 1579 : n = loop->order[dim];
4750 : :
4751 : 1579 : gfc_start_block (&loop->code[n]);
4752 : :
4753 : 1579 : loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
4754 : :
4755 : 1579 : gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
4756 : : }
4757 : :
4758 : : /* Start a block for the secondary copying code. */
4759 : 8091 : gfc_start_block (body);
4760 : 8091 : }
4761 : :
4762 : :
4763 : : /* Precalculate (either lower or upper) bound of an array section.
4764 : : BLOCK: Block in which the (pre)calculation code will go.
4765 : : BOUNDS[DIM]: Where the bound value will be stored once evaluated.
4766 : : VALUES[DIM]: Specified bound (NULL <=> unspecified).
4767 : : DESC: Array descriptor from which the bound will be picked if unspecified
4768 : : (either lower or upper bound according to LBOUND). */
4769 : :
4770 : : static void
4771 : 501265 : evaluate_bound (stmtblock_t *block, tree *bounds, gfc_expr ** values,
4772 : : tree desc, int dim, bool lbound, bool deferred)
4773 : : {
4774 : 501265 : gfc_se se;
4775 : 501265 : gfc_expr * input_val = values[dim];
4776 : 501265 : tree *output = &bounds[dim];
4777 : :
4778 : :
4779 : 501265 : if (input_val)
4780 : : {
4781 : : /* Specified section bound. */
4782 : 45793 : gfc_init_se (&se, NULL);
4783 : 45793 : gfc_conv_expr_type (&se, input_val, gfc_array_index_type);
4784 : 45793 : gfc_add_block_to_block (block, &se.pre);
4785 : 45793 : *output = se.expr;
4786 : : }
4787 : 455472 : else if (deferred && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
4788 : : {
4789 : : /* The gfc_conv_array_lbound () routine returns a constant zero for
4790 : : deferred length arrays, which in the scalarizer wreaks havoc, when
4791 : : copying to a (newly allocated) one-based array.
4792 : : Keep returning the actual result in sync for both bounds. */
4793 : 180266 : *output = lbound ? gfc_conv_descriptor_lbound_get (desc,
4794 : : gfc_rank_cst[dim]):
4795 : 60151 : gfc_conv_descriptor_ubound_get (desc,
4796 : : gfc_rank_cst[dim]);
4797 : : }
4798 : : else
4799 : : {
4800 : : /* No specific bound specified so use the bound of the array. */
4801 : 499717 : *output = lbound ? gfc_conv_array_lbound (desc, dim) :
4802 : 164360 : gfc_conv_array_ubound (desc, dim);
4803 : : }
4804 : 501265 : *output = gfc_evaluate_now (*output, block);
4805 : 501265 : }
4806 : :
4807 : :
4808 : : /* Calculate the lower bound of an array section. */
4809 : :
4810 : : static void
4811 : 251120 : gfc_conv_section_startstride (stmtblock_t * block, gfc_ss * ss, int dim)
4812 : : {
4813 : 251120 : gfc_expr *stride = NULL;
4814 : 251120 : tree desc;
4815 : 251120 : gfc_se se;
4816 : 251120 : gfc_array_info *info;
4817 : 251120 : gfc_array_ref *ar;
4818 : :
4819 : 251120 : gcc_assert (ss->info->type == GFC_SS_SECTION);
4820 : :
4821 : 251120 : info = &ss->info->data.array;
4822 : 251120 : ar = &info->ref->u.ar;
4823 : :
4824 : 251120 : if (ar->dimen_type[dim] == DIMEN_VECTOR)
4825 : : {
4826 : : /* We use a zero-based index to access the vector. */
4827 : 752 : info->start[dim] = gfc_index_zero_node;
4828 : 752 : info->end[dim] = NULL;
4829 : 752 : info->stride[dim] = gfc_index_one_node;
4830 : 752 : return;
4831 : : }
4832 : :
4833 : 250368 : gcc_assert (ar->dimen_type[dim] == DIMEN_RANGE
4834 : : || ar->dimen_type[dim] == DIMEN_THIS_IMAGE);
4835 : 250368 : desc = info->descriptor;
4836 : 250368 : stride = ar->stride[dim];
4837 : :
4838 : :
4839 : : /* Calculate the start of the range. For vector subscripts this will
4840 : : be the range of the vector. */
4841 : 250368 : evaluate_bound (block, info->start, ar->start, desc, dim, true,
4842 : 250368 : ar->as->type == AS_DEFERRED);
4843 : :
4844 : : /* Similarly calculate the end. Although this is not used in the
4845 : : scalarizer, it is needed when checking bounds and where the end
4846 : : is an expression with side-effects. */
4847 : 250368 : evaluate_bound (block, info->end, ar->end, desc, dim, false,
4848 : 250368 : ar->as->type == AS_DEFERRED);
4849 : :
4850 : :
4851 : : /* Calculate the stride. */
4852 : 250368 : if (stride == NULL)
4853 : 238318 : info->stride[dim] = gfc_index_one_node;
4854 : : else
4855 : : {
4856 : 12050 : gfc_init_se (&se, NULL);
4857 : 12050 : gfc_conv_expr_type (&se, stride, gfc_array_index_type);
4858 : 12050 : gfc_add_block_to_block (block, &se.pre);
4859 : 12050 : info->stride[dim] = gfc_evaluate_now (se.expr, block);
4860 : : }
4861 : : }
4862 : :
4863 : :
4864 : : /* Generate in INNER the bounds checking code along the dimension DIM for
4865 : : the array associated with SS_INFO. */
4866 : :
4867 : : static void
4868 : 23805 : add_check_section_in_array_bounds (stmtblock_t *inner, gfc_ss_info *ss_info,
4869 : : int dim)
4870 : : {
4871 : 23805 : gfc_expr *expr = ss_info->expr;
4872 : 23805 : locus *expr_loc = &expr->where;
4873 : 23805 : const char *expr_name = expr->symtree->name;
4874 : :
4875 : 23805 : gfc_array_info *info = &ss_info->data.array;
4876 : :
4877 : 23805 : bool check_upper;
4878 : 23805 : if (dim == info->ref->u.ar.dimen - 1
4879 : 20282 : && info->ref->u.ar.as->type == AS_ASSUMED_SIZE)
4880 : : check_upper = false;
4881 : : else
4882 : 23509 : check_upper = true;
4883 : :
4884 : : /* Zero stride is not allowed. */
4885 : 23805 : tree tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
4886 : : info->stride[dim], gfc_index_zero_node);
4887 : 23805 : char * msg = xasprintf ("Zero stride is not allowed, for dimension %d "
4888 : : "of array '%s'", dim + 1, expr_name);
4889 : 23805 : gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg);
4890 : 23805 : free (msg);
4891 : :
4892 : 23805 : tree desc = info->descriptor;
4893 : :
4894 : : /* This is the run-time equivalent of resolve.cc's
4895 : : check_dimension. The logical is more readable there
4896 : : than it is here, with all the trees. */
4897 : 23805 : tree lbound = gfc_conv_array_lbound (desc, dim);
4898 : 23805 : tree end = info->end[dim];
4899 : 23805 : tree ubound = check_upper ? gfc_conv_array_ubound (desc, dim) : NULL_TREE;
4900 : :
4901 : : /* non_zerosized is true when the selected range is not
4902 : : empty. */
4903 : 23805 : tree stride_pos = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
4904 : : info->stride[dim], gfc_index_zero_node);
4905 : 23805 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
4906 : : info->start[dim], end);
4907 : 23805 : stride_pos = fold_build2_loc (input_location, TRUTH_AND_EXPR,
4908 : : logical_type_node, stride_pos, tmp);
4909 : :
4910 : 23805 : tree stride_neg = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
4911 : : info->stride[dim], gfc_index_zero_node);
4912 : 23805 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
4913 : : info->start[dim], end);
4914 : 23805 : stride_neg = fold_build2_loc (input_location, TRUTH_AND_EXPR,
4915 : : logical_type_node, stride_neg, tmp);
4916 : 23805 : tree non_zerosized = fold_build2_loc (input_location, TRUTH_OR_EXPR,
4917 : : logical_type_node, stride_pos,
4918 : : stride_neg);
4919 : :
4920 : : /* Check the start of the range against the lower and upper
4921 : : bounds of the array, if the range is not empty.
4922 : : If upper bound is present, include both bounds in the
4923 : : error message. */
4924 : 23805 : if (check_upper)
4925 : : {
4926 : 23509 : tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
4927 : : info->start[dim], lbound);
4928 : 23509 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
4929 : : non_zerosized, tmp);
4930 : 23509 : tree tmp2 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
4931 : : info->start[dim], ubound);
4932 : 23509 : tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
4933 : : non_zerosized, tmp2);
4934 : 23509 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
4935 : : "expected range (%%ld:%%ld)", dim + 1, expr_name);
4936 : 23509 : gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg,
4937 : : fold_convert (long_integer_type_node, info->start[dim]),
4938 : : fold_convert (long_integer_type_node, lbound),
4939 : : fold_convert (long_integer_type_node, ubound));
4940 : 23509 : gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
4941 : : fold_convert (long_integer_type_node, info->start[dim]),
4942 : : fold_convert (long_integer_type_node, lbound),
4943 : : fold_convert (long_integer_type_node, ubound));
4944 : 23509 : free (msg);
4945 : : }
4946 : : else
4947 : : {
4948 : 296 : tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
4949 : : info->start[dim], lbound);
4950 : 296 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
4951 : : non_zerosized, tmp);
4952 : 296 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' below "
4953 : : "lower bound of %%ld", dim + 1, expr_name);
4954 : 296 : gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg,
4955 : : fold_convert (long_integer_type_node, info->start[dim]),
4956 : : fold_convert (long_integer_type_node, lbound));
4957 : 296 : free (msg);
4958 : : }
4959 : :
4960 : : /* Compute the last element of the range, which is not
4961 : : necessarily "end" (think 0:5:3, which doesn't contain 5)
4962 : : and check it against both lower and upper bounds. */
4963 : :
4964 : 23805 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4965 : : end, info->start[dim]);
4966 : 23805 : tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR, gfc_array_index_type,
4967 : : tmp, info->stride[dim]);
4968 : 23805 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4969 : : end, tmp);
4970 : 23805 : tree tmp2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
4971 : : tmp, lbound);
4972 : 23805 : tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
4973 : : non_zerosized, tmp2);
4974 : 23805 : if (check_upper)
4975 : : {
4976 : 23509 : tree tmp3 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
4977 : : tmp, ubound);
4978 : 23509 : tmp3 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
4979 : : non_zerosized, tmp3);
4980 : 23509 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
4981 : : "expected range (%%ld:%%ld)", dim + 1, expr_name);
4982 : 23509 : gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
4983 : : fold_convert (long_integer_type_node, tmp),
4984 : : fold_convert (long_integer_type_node, ubound),
4985 : : fold_convert (long_integer_type_node, lbound));
4986 : 23509 : gfc_trans_runtime_check (true, false, tmp3, inner, expr_loc, msg,
4987 : : fold_convert (long_integer_type_node, tmp),
4988 : : fold_convert (long_integer_type_node, ubound),
4989 : : fold_convert (long_integer_type_node, lbound));
4990 : 23509 : free (msg);
4991 : : }
4992 : : else
4993 : : {
4994 : 296 : msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' below "
4995 : : "lower bound of %%ld", dim + 1, expr_name);
4996 : 296 : gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
4997 : : fold_convert (long_integer_type_node, tmp),
4998 : : fold_convert (long_integer_type_node, lbound));
4999 : 296 : free (msg);
5000 : : }
5001 : 23805 : }
5002 : :
5003 : :
5004 : : /* Tells whether we need to generate bounds checking code for the array
5005 : : associated with SS. */
5006 : :
5007 : : bool
5008 : 24943 : bounds_check_needed (gfc_ss *ss)
5009 : : {
5010 : : /* Catch allocatable lhs in f2003. */
5011 : 24943 : if (flag_realloc_lhs && ss->no_bounds_check)
5012 : : return false;
5013 : :
5014 : 24565 : gfc_ss_info *ss_info = ss->info;
5015 : 24565 : if (ss_info->type == GFC_SS_SECTION)
5016 : : return true;
5017 : :
5018 : 4110 : if (!(ss_info->type == GFC_SS_INTRINSIC
5019 : 227 : && ss_info->expr
5020 : 227 : && ss_info->expr->expr_type == EXPR_FUNCTION))
5021 : : return false;
5022 : :
5023 : 227 : gfc_intrinsic_sym *isym = ss_info->expr->value.function.isym;
5024 : 227 : if (!(isym
5025 : 227 : && (isym->id == GFC_ISYM_MAXLOC
5026 : 203 : || isym->id == GFC_ISYM_MINLOC)))
5027 : : return false;
5028 : :
5029 : 34 : return gfc_inline_intrinsic_function_p (ss_info->expr);
5030 : : }
5031 : :
5032 : :
5033 : : /* Calculates the range start and stride for a SS chain. Also gets the
5034 : : descriptor and data pointer. The range of vector subscripts is the size
5035 : : of the vector. Array bounds are also checked. */
5036 : :
5037 : : void
5038 : 177787 : gfc_conv_ss_startstride (gfc_loopinfo * loop)
5039 : : {
5040 : 177787 : int n;
5041 : 177787 : tree tmp;
5042 : 177787 : gfc_ss *ss;
5043 : :
5044 : 177787 : gfc_loopinfo * const outer_loop = outermost_loop (loop);
5045 : :
5046 : 177787 : loop->dimen = 0;
5047 : : /* Determine the rank of the loop. */
5048 : 197125 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5049 : : {
5050 : 197125 : switch (ss->info->type)
5051 : : {
5052 : 166727 : case GFC_SS_SECTION:
5053 : 166727 : case GFC_SS_CONSTRUCTOR:
5054 : 166727 : case GFC_SS_FUNCTION:
5055 : 166727 : case GFC_SS_COMPONENT:
5056 : 166727 : loop->dimen = ss->dimen;
5057 : 166727 : goto done;
5058 : :
5059 : : /* As usual, lbound and ubound are exceptions!. */
5060 : 11060 : case GFC_SS_INTRINSIC:
5061 : 11060 : switch (ss->info->expr->value.function.isym->id)
5062 : : {
5063 : 11060 : case GFC_ISYM_LBOUND:
5064 : 11060 : case GFC_ISYM_UBOUND:
5065 : 11060 : case GFC_ISYM_LCOBOUND:
5066 : 11060 : case GFC_ISYM_UCOBOUND:
5067 : 11060 : case GFC_ISYM_MAXLOC:
5068 : 11060 : case GFC_ISYM_MINLOC:
5069 : 11060 : case GFC_ISYM_SHAPE:
5070 : 11060 : case GFC_ISYM_THIS_IMAGE:
5071 : 11060 : loop->dimen = ss->dimen;
5072 : 11060 : goto done;
5073 : :
5074 : : default:
5075 : : break;
5076 : : }
5077 : :
5078 : 19338 : default:
5079 : 19338 : break;
5080 : : }
5081 : : }
5082 : :
5083 : : /* We should have determined the rank of the expression by now. If
5084 : : not, that's bad news. */
5085 : 0 : gcc_unreachable ();
5086 : :
5087 : : done:
5088 : : /* Loop over all the SS in the chain. */
5089 : 461256 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5090 : : {
5091 : 283469 : gfc_ss_info *ss_info;
5092 : 283469 : gfc_array_info *info;
5093 : 283469 : gfc_expr *expr;
5094 : :
5095 : 283469 : ss_info = ss->info;
5096 : 283469 : expr = ss_info->expr;
5097 : 283469 : info = &ss_info->data.array;
5098 : :
5099 : 283469 : if (expr && expr->shape && !info->shape)
5100 : 166611 : info->shape = expr->shape;
5101 : :
5102 : 283469 : switch (ss_info->type)
5103 : : {
5104 : 180092 : case GFC_SS_SECTION:
5105 : : /* Get the descriptor for the array. If it is a cross loops array,
5106 : : we got the descriptor already in the outermost loop. */
5107 : 180092 : if (ss->parent == NULL)
5108 : 175456 : gfc_conv_ss_descriptor (&outer_loop->pre, ss,
5109 : 175456 : !loop->array_parameter);
5110 : :
5111 : 430658 : for (n = 0; n < ss->dimen; n++)
5112 : 250566 : gfc_conv_section_startstride (&outer_loop->pre, ss, ss->dim[n]);
5113 : : break;
5114 : :
5115 : 11297 : case GFC_SS_INTRINSIC:
5116 : 11297 : switch (expr->value.function.isym->id)
5117 : : {
5118 : 3281 : case GFC_ISYM_MINLOC:
5119 : 3281 : case GFC_ISYM_MAXLOC:
5120 : 3281 : {
5121 : 3281 : gfc_se se;
5122 : 3281 : gfc_init_se (&se, nullptr);
5123 : 3281 : se.loop = loop;
5124 : 3281 : se.ss = ss;
5125 : 3281 : gfc_conv_intrinsic_function (&se, expr);
5126 : 3281 : gfc_add_block_to_block (&outer_loop->pre, &se.pre);
5127 : 3281 : gfc_add_block_to_block (&outer_loop->post, &se.post);
5128 : :
5129 : 3281 : info->descriptor = se.expr;
5130 : :
5131 : 3281 : info->data = gfc_conv_array_data (info->descriptor);
5132 : 3281 : info->data = gfc_evaluate_now (info->data, &outer_loop->pre);
5133 : :
5134 : 3281 : gfc_expr *array = expr->value.function.actual->expr;
5135 : 3281 : tree rank = build_int_cst (gfc_array_index_type, array->rank);
5136 : :
5137 : 3281 : tree tmp = fold_build2_loc (input_location, MINUS_EXPR,
5138 : : gfc_array_index_type, rank,
5139 : : gfc_index_one_node);
5140 : :
5141 : 3281 : info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
5142 : 3281 : info->start[0] = gfc_index_zero_node;
5143 : 3281 : info->stride[0] = gfc_index_one_node;
5144 : 3281 : info->offset = gfc_index_zero_node;
5145 : 3281 : continue;
5146 : 3281 : }
5147 : :
5148 : : /* Fall through to supply start and stride. */
5149 : 2980 : case GFC_ISYM_LBOUND:
5150 : 2980 : case GFC_ISYM_UBOUND:
5151 : : /* This is the variant without DIM=... */
5152 : 2980 : gcc_assert (expr->value.function.actual->next->expr == NULL);
5153 : : /* Fall through. */
5154 : :
5155 : 7791 : case GFC_ISYM_SHAPE:
5156 : 7791 : {
5157 : 7791 : gfc_expr *arg;
5158 : :
5159 : 7791 : arg = expr->value.function.actual->expr;
5160 : 7791 : if (arg->rank == -1)
5161 : : {
5162 : 1157 : gfc_se se;
5163 : 1157 : tree rank, tmp;
5164 : :
5165 : : /* The rank (hence the return value's shape) is unknown,
5166 : : we have to retrieve it. */
5167 : 1157 : gfc_init_se (&se, NULL);
5168 : 1157 : se.descriptor_only = 1;
5169 : 1157 : gfc_conv_expr (&se, arg);
5170 : : /* This is a bare variable, so there is no preliminary
5171 : : or cleanup code unless -std=f202y and bounds checking
5172 : : is on. */
5173 : 1157 : if (!((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
5174 : 0 : && (gfc_option.allow_std & GFC_STD_F202Y)))
5175 : 1157 : gcc_assert (se.pre.head == NULL_TREE
5176 : : && se.post.head == NULL_TREE);
5177 : 1157 : rank = gfc_conv_descriptor_rank (se.expr);
5178 : 1157 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5179 : : gfc_array_index_type,
5180 : : fold_convert (gfc_array_index_type,
5181 : : rank),
5182 : : gfc_index_one_node);
5183 : 1157 : info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
5184 : 1157 : info->start[0] = gfc_index_zero_node;
5185 : 1157 : info->stride[0] = gfc_index_one_node;
5186 : 1157 : continue;
5187 : 1157 : }
5188 : : /* Otherwise fall through GFC_SS_FUNCTION. */
5189 : : gcc_fallthrough ();
5190 : : }
5191 : : case GFC_ISYM_LCOBOUND:
5192 : : case GFC_ISYM_UCOBOUND:
5193 : : case GFC_ISYM_THIS_IMAGE:
5194 : : break;
5195 : :
5196 : 0 : default:
5197 : 0 : continue;
5198 : 0 : }
5199 : :
5200 : : /* FALLTHRU */
5201 : : case GFC_SS_CONSTRUCTOR:
5202 : : case GFC_SS_FUNCTION:
5203 : 124707 : for (n = 0; n < ss->dimen; n++)
5204 : : {
5205 : 67275 : int dim = ss->dim[n];
5206 : :
5207 : 67275 : info->start[dim] = gfc_index_zero_node;
5208 : 67275 : info->end[dim] = gfc_index_zero_node;
5209 : 67275 : info->stride[dim] = gfc_index_one_node;
5210 : : }
5211 : : break;
5212 : :
5213 : : default:
5214 : : break;
5215 : : }
5216 : : }
5217 : :
5218 : : /* The rest is just runtime bounds checking. */
5219 : 177787 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
5220 : : {
5221 : 16884 : stmtblock_t block;
5222 : 16884 : tree size[GFC_MAX_DIMENSIONS];
5223 : 16884 : tree tmp3;
5224 : 16884 : gfc_array_info *info;
5225 : 16884 : char *msg;
5226 : 16884 : int dim;
5227 : :
5228 : 16884 : gfc_start_block (&block);
5229 : :
5230 : 54068 : for (n = 0; n < loop->dimen; n++)
5231 : 20300 : size[n] = NULL_TREE;
5232 : :
5233 : : /* If there is a constructor involved, derive size[] from its shape. */
5234 : 39004 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5235 : : {
5236 : 24598 : gfc_ss_info *ss_info;
5237 : :
5238 : 24598 : ss_info = ss->info;
5239 : 24598 : info = &ss_info->data.array;
5240 : :
5241 : 24598 : if (ss_info->type == GFC_SS_CONSTRUCTOR && info->shape)
5242 : : {
5243 : 5220 : for (n = 0; n < loop->dimen; n++)
5244 : : {
5245 : 2742 : if (size[n] == NULL)
5246 : : {
5247 : 2742 : gcc_assert (info->shape[n]);
5248 : 2742 : size[n] = gfc_conv_mpz_to_tree (info->shape[n],
5249 : : gfc_index_integer_kind);
5250 : : }
5251 : : }
5252 : : break;
5253 : : }
5254 : : }
5255 : :
5256 : 41827 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5257 : : {
5258 : 24943 : stmtblock_t inner;
5259 : 24943 : gfc_ss_info *ss_info;
5260 : 24943 : gfc_expr *expr;
5261 : 24943 : locus *expr_loc;
5262 : 24943 : const char *expr_name;
5263 : 24943 : char *ref_name = NULL;
5264 : :
5265 : 24943 : if (!bounds_check_needed (ss))
5266 : 4454 : continue;
5267 : :
5268 : 20489 : ss_info = ss->info;
5269 : 20489 : expr = ss_info->expr;
5270 : 20489 : expr_loc = &expr->where;
5271 : 20489 : if (expr->ref)
5272 : 20455 : expr_name = ref_name = abridged_ref_name (expr, NULL);
5273 : : else
5274 : 34 : expr_name = expr->symtree->name;
5275 : :
5276 : 20489 : gfc_start_block (&inner);
5277 : :
5278 : : /* TODO: range checking for mapped dimensions. */
5279 : 20489 : info = &ss_info->data.array;
5280 : :
5281 : : /* This code only checks ranges. Elemental and vector
5282 : : dimensions are checked later. */
5283 : 64831 : for (n = 0; n < loop->dimen; n++)
5284 : : {
5285 : 23853 : dim = ss->dim[n];
5286 : 23853 : if (ss_info->type == GFC_SS_SECTION)
5287 : : {
5288 : 23819 : if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
5289 : 14 : continue;
5290 : :
5291 : 23805 : add_check_section_in_array_bounds (&inner, ss_info, dim);
5292 : : }
5293 : :
5294 : : /* Check the section sizes match. */
5295 : 23839 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5296 : : gfc_array_index_type, info->end[dim],
5297 : : info->start[dim]);
5298 : 23839 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
5299 : : gfc_array_index_type, tmp,
5300 : : info->stride[dim]);
5301 : 23839 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5302 : : gfc_array_index_type,
5303 : : gfc_index_one_node, tmp);
5304 : 23839 : tmp = fold_build2_loc (input_location, MAX_EXPR,
5305 : : gfc_array_index_type, tmp,
5306 : : build_int_cst (gfc_array_index_type, 0));
5307 : : /* We remember the size of the first section, and check all the
5308 : : others against this. */
5309 : 23839 : if (size[n])
5310 : : {
5311 : 7167 : tmp3 = fold_build2_loc (input_location, NE_EXPR,
5312 : : logical_type_node, tmp, size[n]);
5313 : 7167 : if (ss_info->type == GFC_SS_INTRINSIC)
5314 : 0 : msg = xasprintf ("Extent mismatch for dimension %d of the "
5315 : : "result of intrinsic '%s' (%%ld/%%ld)",
5316 : : dim + 1, expr_name);
5317 : : else
5318 : 7167 : msg = xasprintf ("Array bound mismatch for dimension %d "
5319 : : "of array '%s' (%%ld/%%ld)",
5320 : : dim + 1, expr_name);
5321 : :
5322 : 7167 : gfc_trans_runtime_check (true, false, tmp3, &inner,
5323 : : expr_loc, msg,
5324 : : fold_convert (long_integer_type_node, tmp),
5325 : : fold_convert (long_integer_type_node, size[n]));
5326 : :
5327 : 7167 : free (msg);
5328 : : }
5329 : : else
5330 : 16672 : size[n] = gfc_evaluate_now (tmp, &inner);
5331 : : }
5332 : :
5333 : 20489 : tmp = gfc_finish_block (&inner);
5334 : :
5335 : : /* For optional arguments, only check bounds if the argument is
5336 : : present. */
5337 : 20489 : if ((expr->symtree->n.sym->attr.optional
5338 : 20181 : || expr->symtree->n.sym->attr.not_always_present)
5339 : 308 : && expr->symtree->n.sym->attr.dummy)
5340 : 307 : tmp = build3_v (COND_EXPR,
5341 : : gfc_conv_expr_present (expr->symtree->n.sym),
5342 : : tmp, build_empty_stmt (input_location));
5343 : :
5344 : 20489 : gfc_add_expr_to_block (&block, tmp);
5345 : :
5346 : 20489 : free (ref_name);
5347 : : }
5348 : :
5349 : 16884 : tmp = gfc_finish_block (&block);
5350 : 16884 : gfc_add_expr_to_block (&outer_loop->pre, tmp);
5351 : : }
5352 : :
5353 : 181151 : for (loop = loop->nested; loop; loop = loop->next)
5354 : 3364 : gfc_conv_ss_startstride (loop);
5355 : 177787 : }
5356 : :
5357 : : /* Return true if both symbols could refer to the same data object. Does
5358 : : not take account of aliasing due to equivalence statements. */
5359 : :
5360 : : static bool
5361 : 13089 : symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym, bool lsym_pointer,
5362 : : bool lsym_target, bool rsym_pointer, bool rsym_target)
5363 : : {
5364 : : /* Aliasing isn't possible if the symbols have different base types,
5365 : : except for complex types where an inquiry reference (%RE, %IM) could
5366 : : alias with a real type with the same kind parameter. */
5367 : 13089 : if (!gfc_compare_types (&lsym->ts, &rsym->ts)
5368 : 13089 : && !(((lsym->ts.type == BT_COMPLEX && rsym->ts.type == BT_REAL)
5369 : 4575 : || (lsym->ts.type == BT_REAL && rsym->ts.type == BT_COMPLEX))
5370 : 76 : && lsym->ts.kind == rsym->ts.kind))
5371 : : return false;
5372 : :
5373 : : /* Pointers can point to other pointers and target objects. */
5374 : :
5375 : 8527 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5376 : 8318 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5377 : : return true;
5378 : :
5379 : : /* Special case: Argument association, cf. F90 12.4.1.6, F2003 12.4.1.7
5380 : : and F2008 12.5.2.13 items 3b and 4b. The pointer case (a) is already
5381 : : checked above. */
5382 : 8404 : if (lsym_target && rsym_target
5383 : 14 : && ((lsym->attr.dummy && !lsym->attr.contiguous
5384 : 0 : && (!lsym->attr.dimension || lsym->as->type == AS_ASSUMED_SHAPE))
5385 : 14 : || (rsym->attr.dummy && !rsym->attr.contiguous
5386 : 6 : && (!rsym->attr.dimension
5387 : 6 : || rsym->as->type == AS_ASSUMED_SHAPE))))
5388 : 6 : return true;
5389 : :
5390 : : return false;
5391 : : }
5392 : :
5393 : :
5394 : : /* Return true if the two SS could be aliased, i.e. both point to the same data
5395 : : object. */
5396 : : /* TODO: resolve aliases based on frontend expressions. */
5397 : :
5398 : : static int
5399 : 11162 : gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
5400 : : {
5401 : 11162 : gfc_ref *lref;
5402 : 11162 : gfc_ref *rref;
5403 : 11162 : gfc_expr *lexpr, *rexpr;
5404 : 11162 : gfc_symbol *lsym;
5405 : 11162 : gfc_symbol *rsym;
5406 : 11162 : bool lsym_pointer, lsym_target, rsym_pointer, rsym_target;
5407 : :
5408 : 11162 : lexpr = lss->info->expr;
5409 : 11162 : rexpr = rss->info->expr;
5410 : :
5411 : 11162 : lsym = lexpr->symtree->n.sym;
5412 : 11162 : rsym = rexpr->symtree->n.sym;
5413 : :
5414 : 11162 : lsym_pointer = lsym->attr.pointer;
5415 : 11162 : lsym_target = lsym->attr.target;
5416 : 11162 : rsym_pointer = rsym->attr.pointer;
5417 : 11162 : rsym_target = rsym->attr.target;
5418 : :
5419 : 11162 : if (symbols_could_alias (lsym, rsym, lsym_pointer, lsym_target,
5420 : : rsym_pointer, rsym_target))
5421 : : return 1;
5422 : :
5423 : 11071 : if (rsym->ts.type != BT_DERIVED && rsym->ts.type != BT_CLASS
5424 : 9939 : && lsym->ts.type != BT_DERIVED && lsym->ts.type != BT_CLASS)
5425 : : return 0;
5426 : :
5427 : : /* For derived types we must check all the component types. We can ignore
5428 : : array references as these will have the same base type as the previous
5429 : : component ref. */
5430 : 2362 : for (lref = lexpr->ref; lref != lss->info->data.array.ref; lref = lref->next)
5431 : : {
5432 : 861 : if (lref->type != REF_COMPONENT)
5433 : 89 : continue;
5434 : :
5435 : 772 : lsym_pointer = lsym_pointer || lref->u.c.sym->attr.pointer;
5436 : 772 : lsym_target = lsym_target || lref->u.c.sym->attr.target;
5437 : :
5438 : 772 : if (symbols_could_alias (lref->u.c.sym, rsym, lsym_pointer, lsym_target,
5439 : : rsym_pointer, rsym_target))
5440 : : return 1;
5441 : :
5442 : 772 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5443 : 757 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5444 : : {
5445 : 6 : if (gfc_compare_types (&lref->u.c.component->ts,
5446 : : &rsym->ts))
5447 : : return 1;
5448 : : }
5449 : :
5450 : 1177 : for (rref = rexpr->ref; rref != rss->info->data.array.ref;
5451 : 411 : rref = rref->next)
5452 : : {
5453 : 412 : if (rref->type != REF_COMPONENT)
5454 : 36 : continue;
5455 : :
5456 : 376 : rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
5457 : 376 : rsym_target = lsym_target || rref->u.c.sym->attr.target;
5458 : :
5459 : 376 : if (symbols_could_alias (lref->u.c.sym, rref->u.c.sym,
5460 : : lsym_pointer, lsym_target,
5461 : : rsym_pointer, rsym_target))
5462 : : return 1;
5463 : :
5464 : 375 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5465 : 371 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5466 : : {
5467 : 0 : if (gfc_compare_types (&lref->u.c.component->ts,
5468 : 0 : &rref->u.c.sym->ts))
5469 : : return 1;
5470 : 0 : if (gfc_compare_types (&lref->u.c.sym->ts,
5471 : 0 : &rref->u.c.component->ts))
5472 : : return 1;
5473 : 0 : if (gfc_compare_types (&lref->u.c.component->ts,
5474 : 0 : &rref->u.c.component->ts))
5475 : : return 1;
5476 : : }
5477 : : }
5478 : : }
5479 : :
5480 : 1501 : lsym_pointer = lsym->attr.pointer;
5481 : 1501 : lsym_target = lsym->attr.target;
5482 : :
5483 : 2274 : for (rref = rexpr->ref; rref != rss->info->data.array.ref; rref = rref->next)
5484 : : {
5485 : 926 : if (rref->type != REF_COMPONENT)
5486 : : break;
5487 : :
5488 : 779 : rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
5489 : 779 : rsym_target = lsym_target || rref->u.c.sym->attr.target;
5490 : :
5491 : 779 : if (symbols_could_alias (rref->u.c.sym, lsym,
5492 : : lsym_pointer, lsym_target,
5493 : : rsym_pointer, rsym_target))
5494 : : return 1;
5495 : :
5496 : 779 : if ((lsym_pointer && (rsym_pointer || rsym_target))
5497 : 761 : || (rsym_pointer && (lsym_pointer || lsym_target)))
5498 : : {
5499 : 6 : if (gfc_compare_types (&lsym->ts, &rref->u.c.component->ts))
5500 : : return 1;
5501 : : }
5502 : : }
5503 : :
5504 : : return 0;
5505 : : }
5506 : :
5507 : :
5508 : : /* Resolve array data dependencies. Creates a temporary if required. */
5509 : : /* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
5510 : : dependency.cc. */
5511 : :
5512 : : void
5513 : 36000 : gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
5514 : : gfc_ss * rss)
5515 : : {
5516 : 36000 : gfc_ss *ss;
5517 : 36000 : gfc_ref *lref;
5518 : 36000 : gfc_ref *rref;
5519 : 36000 : gfc_ss_info *ss_info;
5520 : 36000 : gfc_expr *dest_expr;
5521 : 36000 : gfc_expr *ss_expr;
5522 : 36000 : int nDepend = 0;
5523 : 36000 : int i, j;
5524 : :
5525 : 36000 : loop->temp_ss = NULL;
5526 : 36000 : dest_expr = dest->info->expr;
5527 : :
5528 : 77704 : for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
5529 : : {
5530 : 42772 : ss_info = ss->info;
5531 : 42772 : ss_expr = ss_info->expr;
5532 : :
5533 : 42772 : if (ss_info->array_outer_dependency)
5534 : : {
5535 : : nDepend = 1;
5536 : : break;
5537 : : }
5538 : :
5539 : 42662 : if (ss_info->type != GFC_SS_SECTION)
5540 : : {
5541 : 28994 : if (flag_realloc_lhs
5542 : 28082 : && dest_expr != ss_expr
5543 : 28082 : && gfc_is_reallocatable_lhs (dest_expr)
5544 : 35295 : && ss_expr->rank)
5545 : 2831 : nDepend = gfc_check_dependency (dest_expr, ss_expr, true);
5546 : :
5547 : : /* Check for cases like c(:)(1:2) = c(2)(2:3) */
5548 : 28994 : if (!nDepend && dest_expr->rank > 0
5549 : 28579 : && dest_expr->ts.type == BT_CHARACTER
5550 : 4181 : && ss_expr->expr_type == EXPR_VARIABLE)
5551 : :
5552 : 157 : nDepend = gfc_check_dependency (dest_expr, ss_expr, false);
5553 : :
5554 : 28994 : if (ss_info->type == GFC_SS_REFERENCE
5555 : 28994 : && gfc_check_dependency (dest_expr, ss_expr, false))
5556 : 182 : ss_info->data.scalar.needs_temporary = 1;
5557 : :
5558 : 28994 : if (nDepend)
5559 : : break;
5560 : : else
5561 : 28567 : continue;
5562 : : }
5563 : :
5564 : 13668 : if (dest_expr->symtree->n.sym != ss_expr->symtree->n.sym)
5565 : : {
5566 : 11162 : if (gfc_could_be_alias (dest, ss)
5567 : 11162 : || gfc_are_equivalenced_arrays (dest_expr, ss_expr))
5568 : : {
5569 : : nDepend = 1;
5570 : : break;
5571 : : }
5572 : : }
5573 : : else
5574 : : {
5575 : 2506 : lref = dest_expr->ref;
5576 : 2506 : rref = ss_expr->ref;
5577 : :
5578 : 2506 : nDepend = gfc_dep_resolver (lref, rref, &loop->reverse[0]);
5579 : :
5580 : 2506 : if (nDepend == 1)
5581 : : break;
5582 : :
5583 : 4893 : for (i = 0; i < dest->dimen; i++)
5584 : 6546 : for (j = 0; j < ss->dimen; j++)
5585 : 3873 : if (i != j
5586 : 1137 : && dest->dim[i] == ss->dim[j])
5587 : : {
5588 : : /* If we don't access array elements in the same order,
5589 : : there is a dependency. */
5590 : 63 : nDepend = 1;
5591 : 63 : goto temporary;
5592 : : }
5593 : : #if 0
5594 : : /* TODO : loop shifting. */
5595 : : if (nDepend == 1)
5596 : : {
5597 : : /* Mark the dimensions for LOOP SHIFTING */
5598 : : for (n = 0; n < loop->dimen; n++)
5599 : : {
5600 : : int dim = dest->data.info.dim[n];
5601 : :
5602 : : if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
5603 : : depends[n] = 2;
5604 : : else if (! gfc_is_same_range (&lref->u.ar,
5605 : : &rref->u.ar, dim, 0))
5606 : : depends[n] = 1;
5607 : : }
5608 : :
5609 : : /* Put all the dimensions with dependencies in the
5610 : : innermost loops. */
5611 : : dim = 0;
5612 : : for (n = 0; n < loop->dimen; n++)
5613 : : {
5614 : : gcc_assert (loop->order[n] == n);
5615 : : if (depends[n])
5616 : : loop->order[dim++] = n;
5617 : : }
5618 : : for (n = 0; n < loop->dimen; n++)
5619 : : {
5620 : : if (! depends[n])
5621 : : loop->order[dim++] = n;
5622 : : }
5623 : :
5624 : : gcc_assert (dim == loop->dimen);
5625 : : break;
5626 : : }
5627 : : #endif
5628 : : }
5629 : : }
5630 : :
5631 : 713 : temporary:
5632 : :
5633 : 36000 : if (nDepend == 1)
5634 : : {
5635 : 1068 : tree base_type = gfc_typenode_for_spec (&dest_expr->ts);
5636 : 1068 : if (GFC_ARRAY_TYPE_P (base_type)
5637 : 1068 : || GFC_DESCRIPTOR_TYPE_P (base_type))
5638 : 0 : base_type = gfc_get_element_type (base_type);
5639 : 1068 : loop->temp_ss = gfc_get_temp_ss (base_type, dest->info->string_length,
5640 : : loop->dimen);
5641 : 1068 : gfc_add_ss_to_loop (loop, loop->temp_ss);
5642 : : }
5643 : : else
5644 : 34932 : loop->temp_ss = NULL;
5645 : 36000 : }
5646 : :
5647 : :
5648 : : /* Browse through each array's information from the scalarizer and set the loop
5649 : : bounds according to the "best" one (per dimension), i.e. the one which
5650 : : provides the most information (constant bounds, shape, etc.). */
5651 : :
5652 : : static void
5653 : 177787 : set_loop_bounds (gfc_loopinfo *loop)
5654 : : {
5655 : 177787 : int n, dim, spec_dim;
5656 : 177787 : gfc_array_info *info;
5657 : 177787 : gfc_array_info *specinfo;
5658 : 177787 : gfc_ss *ss;
5659 : 177787 : tree tmp;
5660 : 177787 : gfc_ss **loopspec;
5661 : 177787 : bool dynamic[GFC_MAX_DIMENSIONS];
5662 : 177787 : mpz_t *cshape;
5663 : 177787 : mpz_t i;
5664 : 177787 : bool nonoptional_arr;
5665 : :
5666 : 177787 : gfc_loopinfo * const outer_loop = outermost_loop (loop);
5667 : :
5668 : 177787 : loopspec = loop->specloop;
5669 : :
5670 : 177787 : mpz_init (i);
5671 : 420365 : for (n = 0; n < loop->dimen; n++)
5672 : : {
5673 : 242578 : loopspec[n] = NULL;
5674 : 242578 : dynamic[n] = false;
5675 : :
5676 : : /* If there are both optional and nonoptional array arguments, scalarize
5677 : : over the nonoptional; otherwise, it does not matter as then all
5678 : : (optional) arrays have to be present per F2008, 125.2.12p3(6). */
5679 : :
5680 : 242578 : nonoptional_arr = false;
5681 : :
5682 : 282600 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5683 : 282580 : if (ss->info->type != GFC_SS_SCALAR && ss->info->type != GFC_SS_TEMP
5684 : 249123 : && ss->info->type != GFC_SS_REFERENCE && !ss->info->can_be_null_ref)
5685 : : {
5686 : : nonoptional_arr = true;
5687 : : break;
5688 : : }
5689 : :
5690 : : /* We use one SS term, and use that to determine the bounds of the
5691 : : loop for this dimension. We try to pick the simplest term. */
5692 : 633460 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5693 : : {
5694 : 390882 : gfc_ss_type ss_type;
5695 : :
5696 : 390882 : ss_type = ss->info->type;
5697 : 458700 : if (ss_type == GFC_SS_SCALAR
5698 : 390882 : || ss_type == GFC_SS_TEMP
5699 : 332308 : || ss_type == GFC_SS_REFERENCE
5700 : 323341 : || (ss->info->can_be_null_ref && nonoptional_arr))
5701 : 67818 : continue;
5702 : :
5703 : 323064 : info = &ss->info->data.array;
5704 : 323064 : dim = ss->dim[n];
5705 : :
5706 : 323064 : if (loopspec[n] != NULL)
5707 : : {
5708 : 80486 : specinfo = &loopspec[n]->info->data.array;
5709 : 80486 : spec_dim = loopspec[n]->dim[n];
5710 : : }
5711 : : else
5712 : : {
5713 : : /* Silence uninitialized warnings. */
5714 : : specinfo = NULL;
5715 : : spec_dim = 0;
5716 : : }
5717 : :
5718 : 323064 : if (info->shape)
5719 : : {
5720 : : /* The frontend has worked out the size for us. */
5721 : 221045 : if (!loopspec[n]
5722 : 57852 : || !specinfo->shape
5723 : 267131 : || !integer_zerop (specinfo->start[spec_dim]))
5724 : : /* Prefer zero-based descriptors if possible. */
5725 : 204280 : loopspec[n] = ss;
5726 : 221045 : continue;
5727 : : }
5728 : :
5729 : 102019 : if (ss_type == GFC_SS_CONSTRUCTOR)
5730 : : {
5731 : 1288 : gfc_constructor_base base;
5732 : : /* An unknown size constructor will always be rank one.
5733 : : Higher rank constructors will either have known shape,
5734 : : or still be wrapped in a call to reshape. */
5735 : 1288 : gcc_assert (loop->dimen == 1);
5736 : :
5737 : : /* Always prefer to use the constructor bounds if the size
5738 : : can be determined at compile time. Prefer not to otherwise,
5739 : : since the general case involves realloc, and it's better to
5740 : : avoid that overhead if possible. */
5741 : 1288 : base = ss->info->expr->value.constructor;
5742 : 1288 : dynamic[n] = gfc_get_array_constructor_size (&i, base);
5743 : 1288 : if (!dynamic[n] || !loopspec[n])
5744 : 1101 : loopspec[n] = ss;
5745 : 1288 : continue;
5746 : 1288 : }
5747 : :
5748 : : /* Avoid using an allocatable lhs in an assignment, since
5749 : : there might be a reallocation coming. */
5750 : 100731 : if (loopspec[n] && ss->is_alloc_lhs)
5751 : 6388 : continue;
5752 : :
5753 : 94343 : if (!loopspec[n])
5754 : 78284 : loopspec[n] = ss;
5755 : : /* Criteria for choosing a loop specifier (most important first):
5756 : : doesn't need realloc
5757 : : stride of one
5758 : : known stride
5759 : : known lower bound
5760 : : known upper bound
5761 : : */
5762 : 16059 : else if (loopspec[n]->info->type == GFC_SS_CONSTRUCTOR && dynamic[n])
5763 : 174 : loopspec[n] = ss;
5764 : 15885 : else if (integer_onep (info->stride[dim])
5765 : 15885 : && !integer_onep (specinfo->stride[spec_dim]))
5766 : 120 : loopspec[n] = ss;
5767 : 15765 : else if (INTEGER_CST_P (info->stride[dim])
5768 : 15577 : && !INTEGER_CST_P (specinfo->stride[spec_dim]))
5769 : 0 : loopspec[n] = ss;
5770 : 15765 : else if (INTEGER_CST_P (info->start[dim])
5771 : 4184 : && !INTEGER_CST_P (specinfo->start[spec_dim])
5772 : 744 : && integer_onep (info->stride[dim])
5773 : 372 : == integer_onep (specinfo->stride[spec_dim])
5774 : 15765 : && INTEGER_CST_P (info->stride[dim])
5775 : 345 : == INTEGER_CST_P (specinfo->stride[spec_dim]))
5776 : 345 : loopspec[n] = ss;
5777 : : /* We don't work out the upper bound.
5778 : : else if (INTEGER_CST_P (info->finish[n])
5779 : : && ! INTEGER_CST_P (specinfo->finish[n]))
5780 : : loopspec[n] = ss; */
5781 : : }
5782 : :
5783 : : /* We should have found the scalarization loop specifier. If not,
5784 : : that's bad news. */
5785 : 242578 : gcc_assert (loopspec[n]);
5786 : :
5787 : 242578 : info = &loopspec[n]->info->data.array;
5788 : 242578 : dim = loopspec[n]->dim[n];
5789 : :
5790 : : /* Set the extents of this range. */
5791 : 242578 : cshape = info->shape;
5792 : 242578 : if (cshape && INTEGER_CST_P (info->start[dim])
5793 : 174760 : && INTEGER_CST_P (info->stride[dim]))
5794 : : {
5795 : 174760 : loop->from[n] = info->start[dim];
5796 : 174760 : mpz_set (i, cshape[get_array_ref_dim_for_loop_dim (loopspec[n], n)]);
5797 : 174760 : mpz_sub_ui (i, i, 1);
5798 : : /* To = from + (size - 1) * stride. */
5799 : 174760 : tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
5800 : 174760 : if (!integer_onep (info->stride[dim]))
5801 : 8328 : tmp = fold_build2_loc (input_location, MULT_EXPR,
5802 : : gfc_array_index_type, tmp,
5803 : : info->stride[dim]);
5804 : 174760 : loop->to[n] = fold_build2_loc (input_location, PLUS_EXPR,
5805 : : gfc_array_index_type,
5806 : : loop->from[n], tmp);
5807 : : }
5808 : : else
5809 : : {
5810 : 67818 : loop->from[n] = info->start[dim];
5811 : 67818 : switch (loopspec[n]->info->type)
5812 : : {
5813 : 826 : case GFC_SS_CONSTRUCTOR:
5814 : : /* The upper bound is calculated when we expand the
5815 : : constructor. */
5816 : 826 : gcc_assert (loop->to[n] == NULL_TREE);
5817 : : break;
5818 : :
5819 : 61611 : case GFC_SS_SECTION:
5820 : : /* Use the end expression if it exists and is not constant,
5821 : : so that it is only evaluated once. */
5822 : 61611 : loop->to[n] = info->end[dim];
5823 : 61611 : break;
5824 : :
5825 : 4620 : case GFC_SS_FUNCTION:
5826 : : /* The loop bound will be set when we generate the call. */
5827 : 4620 : gcc_assert (loop->to[n] == NULL_TREE);
5828 : : break;
5829 : :
5830 : 755 : case GFC_SS_INTRINSIC:
5831 : 755 : {
5832 : 755 : gfc_expr *expr = loopspec[n]->info->expr;
5833 : :
5834 : : /* The {l,u}bound of an assumed rank. */
5835 : 755 : if (expr->value.function.isym->id == GFC_ISYM_SHAPE)
5836 : 243 : gcc_assert (expr->value.function.actual->expr->rank == -1);
5837 : : else
5838 : 512 : gcc_assert ((expr->value.function.isym->id == GFC_ISYM_LBOUND
5839 : : || expr->value.function.isym->id == GFC_ISYM_UBOUND)
5840 : : && expr->value.function.actual->next->expr == NULL
5841 : : && expr->value.function.actual->expr->rank == -1);
5842 : :
5843 : 755 : loop->to[n] = info->end[dim];
5844 : 755 : break;
5845 : : }
5846 : :
5847 : 6 : case GFC_SS_COMPONENT:
5848 : 6 : {
5849 : 6 : if (info->end[dim] != NULL_TREE)
5850 : : {
5851 : 6 : loop->to[n] = info->end[dim];
5852 : 6 : break;
5853 : : }
5854 : : else
5855 : 0 : gcc_unreachable ();
5856 : : }
5857 : :
5858 : 0 : default:
5859 : 0 : gcc_unreachable ();
5860 : : }
5861 : : }
5862 : :
5863 : : /* Transform everything so we have a simple incrementing variable. */
5864 : 242578 : if (integer_onep (info->stride[dim]))
5865 : 232331 : info->delta[dim] = gfc_index_zero_node;
5866 : : else
5867 : : {
5868 : : /* Set the delta for this section. */
5869 : 10247 : info->delta[dim] = gfc_evaluate_now (loop->from[n], &outer_loop->pre);
5870 : : /* Number of iterations is (end - start + step) / step.
5871 : : with start = 0, this simplifies to
5872 : : last = end / step;
5873 : : for (i = 0; i<=last; i++){...}; */
5874 : 10247 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5875 : : gfc_array_index_type, loop->to[n],
5876 : : loop->from[n]);
5877 : 10247 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
5878 : : gfc_array_index_type, tmp, info->stride[dim]);
5879 : 10247 : tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
5880 : : tmp, build_int_cst (gfc_array_index_type, -1));
5881 : 10247 : loop->to[n] = gfc_evaluate_now (tmp, &outer_loop->pre);
5882 : : /* Make the loop variable start at 0. */
5883 : 10247 : loop->from[n] = gfc_index_zero_node;
5884 : : }
5885 : : }
5886 : 177787 : mpz_clear (i);
5887 : :
5888 : 181151 : for (loop = loop->nested; loop; loop = loop->next)
5889 : 3364 : set_loop_bounds (loop);
5890 : 177787 : }
5891 : :
5892 : :
5893 : : /* Initialize the scalarization loop. Creates the loop variables. Determines
5894 : : the range of the loop variables. Creates a temporary if required.
5895 : : Also generates code for scalar expressions which have been
5896 : : moved outside the loop. */
5897 : :
5898 : : void
5899 : 174423 : gfc_conv_loop_setup (gfc_loopinfo * loop, locus * where)
5900 : : {
5901 : 174423 : gfc_ss *tmp_ss;
5902 : 174423 : tree tmp;
5903 : :
5904 : 174423 : set_loop_bounds (loop);
5905 : :
5906 : : /* Add all the scalar code that can be taken out of the loops.
5907 : : This may include calculating the loop bounds, so do it before
5908 : : allocating the temporary. */
5909 : 174423 : gfc_add_loop_ss_code (loop, loop->ss, false, where);
5910 : :
5911 : 174423 : tmp_ss = loop->temp_ss;
5912 : : /* If we want a temporary then create it. */
5913 : 174423 : if (tmp_ss != NULL)
5914 : : {
5915 : 11133 : gfc_ss_info *tmp_ss_info;
5916 : :
5917 : 11133 : tmp_ss_info = tmp_ss->info;
5918 : 11133 : gcc_assert (tmp_ss_info->type == GFC_SS_TEMP);
5919 : 11133 : gcc_assert (loop->parent == NULL);
5920 : :
5921 : : /* Make absolutely sure that this is a complete type. */
5922 : 11133 : if (tmp_ss_info->string_length)
5923 : 2666 : tmp_ss_info->data.temp.type
5924 : 2666 : = gfc_get_character_type_len_for_eltype
5925 : 2666 : (TREE_TYPE (tmp_ss_info->data.temp.type),
5926 : : tmp_ss_info->string_length);
5927 : :
5928 : 11133 : tmp = tmp_ss_info->data.temp.type;
5929 : 11133 : memset (&tmp_ss_info->data.array, 0, sizeof (gfc_array_info));
5930 : 11133 : tmp_ss_info->type = GFC_SS_SECTION;
5931 : :
5932 : 11133 : gcc_assert (tmp_ss->dimen != 0);
5933 : :
5934 : 11133 : gfc_trans_create_temp_array (&loop->pre, &loop->post, tmp_ss, tmp,
5935 : : NULL_TREE, false, true, false, where);
5936 : : }
5937 : :
5938 : : /* For array parameters we don't have loop variables, so don't calculate the
5939 : : translations. */
5940 : 174423 : if (!loop->array_parameter)
5941 : 108892 : gfc_set_delta (loop);
5942 : 174423 : }
5943 : :
5944 : :
5945 : : /* Calculates how to transform from loop variables to array indices for each
5946 : : array: once loop bounds are chosen, sets the difference (DELTA field) between
5947 : : loop bounds and array reference bounds, for each array info. */
5948 : :
5949 : : void
5950 : 112687 : gfc_set_delta (gfc_loopinfo *loop)
5951 : : {
5952 : 112687 : gfc_ss *ss, **loopspec;
5953 : 112687 : gfc_array_info *info;
5954 : 112687 : tree tmp;
5955 : 112687 : int n, dim;
5956 : :
5957 : 112687 : gfc_loopinfo * const outer_loop = outermost_loop (loop);
5958 : :
5959 : 112687 : loopspec = loop->specloop;
5960 : :
5961 : : /* Calculate the translation from loop variables to array indices. */
5962 : 340994 : for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
5963 : : {
5964 : 228307 : gfc_ss_type ss_type;
5965 : :
5966 : 228307 : ss_type = ss->info->type;
5967 : 228307 : if (ss_type != GFC_SS_SECTION
5968 : 228307 : && ss_type != GFC_SS_COMPONENT
5969 : 93328 : && ss_type != GFC_SS_CONSTRUCTOR)
5970 : 58924 : continue;
5971 : :
5972 : : info = &ss->info->data.array;
5973 : :
5974 : 381290 : for (n = 0; n < ss->dimen; n++)
5975 : : {
5976 : : /* If we are specifying the range the delta is already set. */
5977 : 211907 : if (loopspec[n] != ss)
5978 : : {
5979 : 110812 : dim = ss->dim[n];
5980 : :
5981 : : /* Calculate the offset relative to the loop variable.
5982 : : First multiply by the stride. */
5983 : 110812 : tmp = loop->from[n];
5984 : 110812 : if (!integer_onep (info->stride[dim]))
5985 : 2927 : tmp = fold_build2_loc (input_location, MULT_EXPR,
5986 : : gfc_array_index_type,
5987 : : tmp, info->stride[dim]);
5988 : :
5989 : : /* Then subtract this from our starting value. */
5990 : 110812 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5991 : : gfc_array_index_type,
5992 : : info->start[dim], tmp);
5993 : :
5994 : 110812 : info->delta[dim] = gfc_evaluate_now (tmp, &outer_loop->pre);
5995 : : }
5996 : : }
5997 : : }
5998 : :
5999 : 116139 : for (loop = loop->nested; loop; loop = loop->next)
6000 : 3452 : gfc_set_delta (loop);
6001 : 112687 : }
6002 : :
6003 : :
6004 : : /* Calculate the size of a given array dimension from the bounds. This
6005 : : is simply (ubound - lbound + 1) if this expression is positive
6006 : : or 0 if it is negative (pick either one if it is zero). Optionally
6007 : : (if or_expr is present) OR the (expression != 0) condition to it. */
6008 : :
6009 : : tree
6010 : 22100 : gfc_conv_array_extent_dim (tree lbound, tree ubound, tree* or_expr)
6011 : : {
6012 : 22100 : tree res;
6013 : 22100 : tree cond;
6014 : :
6015 : : /* Calculate (ubound - lbound + 1). */
6016 : 22100 : res = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6017 : : ubound, lbound);
6018 : 22100 : res = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, res,
6019 : : gfc_index_one_node);
6020 : :
6021 : : /* Check whether the size for this dimension is negative. */
6022 : 22100 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, res,
6023 : : gfc_index_zero_node);
6024 : 22100 : res = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
6025 : : gfc_index_zero_node, res);
6026 : :
6027 : : /* Build OR expression. */
6028 : 22100 : if (or_expr)
6029 : 17193 : *or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
6030 : : logical_type_node, *or_expr, cond);
6031 : :
6032 : 22100 : return res;
6033 : : }
6034 : :
6035 : :
6036 : : /* For an array descriptor, get the total number of elements. This is just
6037 : : the product of the extents along from_dim to to_dim. */
6038 : :
6039 : : static tree
6040 : 1837 : gfc_conv_descriptor_size_1 (tree desc, int from_dim, int to_dim)
6041 : : {
6042 : 1837 : tree res;
6043 : 1837 : int dim;
6044 : :
6045 : 1837 : res = gfc_index_one_node;
6046 : :
6047 : 4474 : for (dim = from_dim; dim < to_dim; ++dim)
6048 : : {
6049 : 2637 : tree lbound;
6050 : 2637 : tree ubound;
6051 : 2637 : tree extent;
6052 : :
6053 : 2637 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
6054 : 2637 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
6055 : :
6056 : 2637 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
6057 : 2637 : res = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6058 : : res, extent);
6059 : : }
6060 : :
6061 : 1837 : return res;
6062 : : }
6063 : :
6064 : :
6065 : : /* Full size of an array. */
6066 : :
6067 : : tree
6068 : 1807 : gfc_conv_descriptor_size (tree desc, int rank)
6069 : : {
6070 : 1807 : return gfc_conv_descriptor_size_1 (desc, 0, rank);
6071 : : }
6072 : :
6073 : :
6074 : : /* Size of a coarray for all dimensions but the last. */
6075 : :
6076 : : tree
6077 : 30 : gfc_conv_descriptor_cosize (tree desc, int rank, int corank)
6078 : : {
6079 : 30 : return gfc_conv_descriptor_size_1 (desc, rank, rank + corank - 1);
6080 : : }
6081 : :
6082 : :
6083 : : /* Fills in an array descriptor, and returns the size of the array.
6084 : : The size will be a simple_val, ie a variable or a constant. Also
6085 : : calculates the offset of the base. The pointer argument overflow,
6086 : : which should be of integer type, will increase in value if overflow
6087 : : occurs during the size calculation. Returns the size of the array.
6088 : : {
6089 : : stride = 1;
6090 : : offset = 0;
6091 : : for (n = 0; n < rank; n++)
6092 : : {
6093 : : a.lbound[n] = specified_lower_bound;
6094 : : offset = offset + a.lbond[n] * stride;
6095 : : size = 1 - lbound;
6096 : : a.ubound[n] = specified_upper_bound;
6097 : : a.stride[n] = stride;
6098 : : size = size >= 0 ? ubound + size : 0; //size = ubound + 1 - lbound
6099 : : overflow += size == 0 ? 0: (MAX/size < stride ? 1: 0);
6100 : : stride = stride * size;
6101 : : }
6102 : : for (n = rank; n < rank+corank; n++)
6103 : : (Set lcobound/ucobound as above.)
6104 : : element_size = sizeof (array element);
6105 : : if (!rank)
6106 : : return element_size
6107 : : stride = (size_t) stride;
6108 : : overflow += element_size == 0 ? 0: (MAX/element_size < stride ? 1: 0);
6109 : : stride = stride * element_size;
6110 : : return (stride);
6111 : : } */
6112 : : /*GCC ARRAYS*/
6113 : :
6114 : : static tree
6115 : 11604 : gfc_array_init_size (tree descriptor, int rank, int corank, tree * poffset,
6116 : : gfc_expr ** lower, gfc_expr ** upper, stmtblock_t * pblock,
6117 : : stmtblock_t * descriptor_block, tree * overflow,
6118 : : tree expr3_elem_size, tree *nelems, gfc_expr *expr3,
6119 : : tree expr3_desc, bool e3_has_nodescriptor, gfc_expr *expr,
6120 : : tree *element_size, bool explicit_ts)
6121 : : {
6122 : 11604 : tree type;
6123 : 11604 : tree tmp;
6124 : 11604 : tree size;
6125 : 11604 : tree offset;
6126 : 11604 : tree stride;
6127 : 11604 : tree or_expr;
6128 : 11604 : tree thencase;
6129 : 11604 : tree elsecase;
6130 : 11604 : tree cond;
6131 : 11604 : tree var;
6132 : 11604 : stmtblock_t thenblock;
6133 : 11604 : stmtblock_t elseblock;
6134 : 11604 : gfc_expr *ubound;
6135 : 11604 : gfc_se se;
6136 : 11604 : int n;
6137 : :
6138 : 11604 : type = TREE_TYPE (descriptor);
6139 : :
6140 : 11604 : stride = gfc_index_one_node;
6141 : 11604 : offset = gfc_index_zero_node;
6142 : :
6143 : : /* Set the dtype before the alloc, because registration of coarrays needs
6144 : : it initialized. */
6145 : 11604 : if (expr->ts.type == BT_CHARACTER
6146 : 1013 : && expr->ts.deferred
6147 : 506 : && VAR_P (expr->ts.u.cl->backend_decl))
6148 : : {
6149 : 327 : type = gfc_typenode_for_spec (&expr->ts);
6150 : 327 : tmp = gfc_conv_descriptor_dtype (descriptor);
6151 : 327 : gfc_add_modify (pblock, tmp, gfc_get_dtype_rank_type (rank, type));
6152 : : }
6153 : 11277 : else if (expr->ts.type == BT_CHARACTER
6154 : 686 : && expr->ts.deferred
6155 : 179 : && TREE_CODE (descriptor) == COMPONENT_REF)
6156 : : {
6157 : : /* Deferred character components have their string length tucked away
6158 : : in a hidden field of the derived type. Obtain that and use it to
6159 : : set the dtype. The charlen backend decl is zero because the field
6160 : : type is zero length. */
6161 : 161 : gfc_ref *ref;
6162 : 161 : tmp = NULL_TREE;
6163 : 161 : for (ref = expr->ref; ref; ref = ref->next)
6164 : 161 : if (ref->type == REF_COMPONENT
6165 : 161 : && gfc_deferred_strlen (ref->u.c.component, &tmp))
6166 : : break;
6167 : 161 : gcc_assert (tmp != NULL_TREE);
6168 : 161 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
6169 : 161 : TREE_OPERAND (descriptor, 0), tmp, NULL_TREE);
6170 : 161 : tmp = fold_convert (gfc_charlen_type_node, tmp);
6171 : 161 : type = gfc_get_character_type_len (expr->ts.kind, tmp);
6172 : 161 : tmp = gfc_conv_descriptor_dtype (descriptor);
6173 : 161 : gfc_add_modify (pblock, tmp, gfc_get_dtype_rank_type (rank, type));
6174 : 161 : }
6175 : 11116 : else if (expr3_desc && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr3_desc)))
6176 : : {
6177 : 889 : tmp = gfc_conv_descriptor_dtype (descriptor);
6178 : 889 : gfc_add_modify (pblock, tmp, gfc_conv_descriptor_dtype (expr3_desc));
6179 : : }
6180 : 10227 : else if (expr->ts.type == BT_CLASS && !explicit_ts
6181 : 1244 : && expr3 && expr3->ts.type != BT_CLASS
6182 : 343 : && expr3_elem_size != NULL_TREE && expr3_desc == NULL_TREE)
6183 : : {
6184 : 343 : tmp = gfc_conv_descriptor_elem_len (descriptor);
6185 : 343 : gfc_add_modify (pblock, tmp,
6186 : 343 : fold_convert (TREE_TYPE (tmp), expr3_elem_size));
6187 : : }
6188 : : else
6189 : : {
6190 : 9884 : tmp = gfc_conv_descriptor_dtype (descriptor);
6191 : 9884 : gfc_add_modify (pblock, tmp, gfc_get_dtype (type));
6192 : : }
6193 : :
6194 : 11604 : or_expr = logical_false_node;
6195 : :
6196 : 28797 : for (n = 0; n < rank; n++)
6197 : : {
6198 : 17193 : tree conv_lbound;
6199 : 17193 : tree conv_ubound;
6200 : :
6201 : : /* We have 3 possibilities for determining the size of the array:
6202 : : lower == NULL => lbound = 1, ubound = upper[n]
6203 : : upper[n] = NULL => lbound = 1, ubound = lower[n]
6204 : : upper[n] != NULL => lbound = lower[n], ubound = upper[n] */
6205 : 17193 : ubound = upper[n];
6206 : :
6207 : : /* Set lower bound. */
6208 : 17193 : gfc_init_se (&se, NULL);
6209 : 17193 : if (expr3_desc != NULL_TREE)
6210 : : {
6211 : 1358 : if (e3_has_nodescriptor)
6212 : : /* The lbound of nondescriptor arrays like array constructors,
6213 : : nonallocatable/nonpointer function results/variables,
6214 : : start at zero, but when allocating it, the standard expects
6215 : : the array to start at one. */
6216 : 869 : se.expr = gfc_index_one_node;
6217 : : else
6218 : 489 : se.expr = gfc_conv_descriptor_lbound_get (expr3_desc,
6219 : : gfc_rank_cst[n]);
6220 : : }
6221 : 15835 : else if (lower == NULL)
6222 : 12795 : se.expr = gfc_index_one_node;
6223 : : else
6224 : : {
6225 : 3040 : gcc_assert (lower[n]);
6226 : 3040 : if (ubound)
6227 : : {
6228 : 2375 : gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
6229 : 2375 : gfc_add_block_to_block (pblock, &se.pre);
6230 : : }
6231 : : else
6232 : : {
6233 : 665 : se.expr = gfc_index_one_node;
6234 : 665 : ubound = lower[n];
6235 : : }
6236 : : }
6237 : 17193 : gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
6238 : : gfc_rank_cst[n], se.expr);
6239 : 17193 : conv_lbound = se.expr;
6240 : :
6241 : : /* Work out the offset for this component. */
6242 : 17193 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6243 : : se.expr, stride);
6244 : 17193 : offset = fold_build2_loc (input_location, MINUS_EXPR,
6245 : : gfc_array_index_type, offset, tmp);
6246 : :
6247 : : /* Set upper bound. */
6248 : 17193 : gfc_init_se (&se, NULL);
6249 : 17193 : if (expr3_desc != NULL_TREE)
6250 : : {
6251 : 1358 : if (e3_has_nodescriptor)
6252 : : {
6253 : : /* The lbound of nondescriptor arrays like array constructors,
6254 : : nonallocatable/nonpointer function results/variables,
6255 : : start at zero, but when allocating it, the standard expects
6256 : : the array to start at one. Therefore fix the upper bound to be
6257 : : (desc.ubound - desc.lbound) + 1. */
6258 : 869 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
6259 : : gfc_array_index_type,
6260 : : gfc_conv_descriptor_ubound_get (
6261 : : expr3_desc, gfc_rank_cst[n]),
6262 : : gfc_conv_descriptor_lbound_get (
6263 : : expr3_desc, gfc_rank_cst[n]));
6264 : 869 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
6265 : : gfc_array_index_type, tmp,
6266 : : gfc_index_one_node);
6267 : 869 : se.expr = gfc_evaluate_now (tmp, pblock);
6268 : : }
6269 : : else
6270 : 489 : se.expr = gfc_conv_descriptor_ubound_get (expr3_desc,
6271 : : gfc_rank_cst[n]);
6272 : : }
6273 : : else
6274 : : {
6275 : 15835 : gcc_assert (ubound);
6276 : 15835 : gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
6277 : 15835 : gfc_add_block_to_block (pblock, &se.pre);
6278 : 15835 : if (ubound->expr_type == EXPR_FUNCTION)
6279 : 712 : se.expr = gfc_evaluate_now (se.expr, pblock);
6280 : : }
6281 : 17193 : gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
6282 : : gfc_rank_cst[n], se.expr);
6283 : 17193 : conv_ubound = se.expr;
6284 : :
6285 : : /* Store the stride. */
6286 : 17193 : gfc_conv_descriptor_stride_set (descriptor_block, descriptor,
6287 : : gfc_rank_cst[n], stride);
6288 : :
6289 : : /* Calculate size and check whether extent is negative. */
6290 : 17193 : size = gfc_conv_array_extent_dim (conv_lbound, conv_ubound, &or_expr);
6291 : 17193 : size = gfc_evaluate_now (size, pblock);
6292 : :
6293 : : /* Check whether multiplying the stride by the number of
6294 : : elements in this dimension would overflow. We must also check
6295 : : whether the current dimension has zero size in order to avoid
6296 : : division by zero.
6297 : : */
6298 : 17193 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6299 : : gfc_array_index_type,
6300 : 17193 : fold_convert (gfc_array_index_type,
6301 : : TYPE_MAX_VALUE (gfc_array_index_type)),
6302 : : size);
6303 : 17193 : cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
6304 : : logical_type_node, tmp, stride),
6305 : : PRED_FORTRAN_OVERFLOW);
6306 : 17193 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6307 : : integer_one_node, integer_zero_node);
6308 : 17193 : cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
6309 : : logical_type_node, size,
6310 : : gfc_index_zero_node),
6311 : : PRED_FORTRAN_SIZE_ZERO);
6312 : 17193 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6313 : : integer_zero_node, tmp);
6314 : 17193 : tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
6315 : : *overflow, tmp);
6316 : 17193 : *overflow = gfc_evaluate_now (tmp, pblock);
6317 : :
6318 : : /* Multiply the stride by the number of elements in this dimension. */
6319 : 17193 : stride = fold_build2_loc (input_location, MULT_EXPR,
6320 : : gfc_array_index_type, stride, size);
6321 : 17193 : stride = gfc_evaluate_now (stride, pblock);
6322 : : }
6323 : :
6324 : 12133 : for (n = rank; n < rank + corank; n++)
6325 : : {
6326 : 529 : ubound = upper[n];
6327 : :
6328 : : /* Set lower bound. */
6329 : 529 : gfc_init_se (&se, NULL);
6330 : 529 : if (lower == NULL || lower[n] == NULL)
6331 : : {
6332 : 305 : gcc_assert (n == rank + corank - 1);
6333 : 305 : se.expr = gfc_index_one_node;
6334 : : }
6335 : : else
6336 : : {
6337 : 224 : if (ubound || n == rank + corank - 1)
6338 : : {
6339 : 142 : gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
6340 : 142 : gfc_add_block_to_block (pblock, &se.pre);
6341 : : }
6342 : : else
6343 : : {
6344 : 82 : se.expr = gfc_index_one_node;
6345 : 82 : ubound = lower[n];
6346 : : }
6347 : : }
6348 : 529 : gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
6349 : : gfc_rank_cst[n], se.expr);
6350 : :
6351 : 529 : if (n < rank + corank - 1)
6352 : : {
6353 : 151 : gfc_init_se (&se, NULL);
6354 : 151 : gcc_assert (ubound);
6355 : 151 : gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
6356 : 151 : gfc_add_block_to_block (pblock, &se.pre);
6357 : 151 : gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
6358 : : gfc_rank_cst[n], se.expr);
6359 : : }
6360 : : }
6361 : :
6362 : : /* The stride is the number of elements in the array, so multiply by the
6363 : : size of an element to get the total size. Obviously, if there is a
6364 : : SOURCE expression (expr3) we must use its element size. */
6365 : 11604 : if (expr3_elem_size != NULL_TREE)
6366 : 2892 : tmp = expr3_elem_size;
6367 : 8712 : else if (expr3 != NULL)
6368 : : {
6369 : 0 : if (expr3->ts.type == BT_CLASS)
6370 : : {
6371 : 0 : gfc_se se_sz;
6372 : 0 : gfc_expr *sz = gfc_copy_expr (expr3);
6373 : 0 : gfc_add_vptr_component (sz);
6374 : 0 : gfc_add_size_component (sz);
6375 : 0 : gfc_init_se (&se_sz, NULL);
6376 : 0 : gfc_conv_expr (&se_sz, sz);
6377 : 0 : gfc_free_expr (sz);
6378 : 0 : tmp = se_sz.expr;
6379 : : }
6380 : : else
6381 : : {
6382 : 0 : tmp = gfc_typenode_for_spec (&expr3->ts);
6383 : 0 : tmp = TYPE_SIZE_UNIT (tmp);
6384 : : }
6385 : : }
6386 : : else
6387 : 8712 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
6388 : :
6389 : : /* Convert to size_t. */
6390 : 11604 : *element_size = fold_convert (size_type_node, tmp);
6391 : :
6392 : 11604 : if (rank == 0)
6393 : : return *element_size;
6394 : :
6395 : 11441 : *nelems = gfc_evaluate_now (stride, pblock);
6396 : 11441 : stride = fold_convert (size_type_node, stride);
6397 : :
6398 : : /* First check for overflow. Since an array of type character can
6399 : : have zero element_size, we must check for that before
6400 : : dividing. */
6401 : 11441 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6402 : : size_type_node,
6403 : 11441 : TYPE_MAX_VALUE (size_type_node), *element_size);
6404 : 11441 : cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
6405 : : logical_type_node, tmp, stride),
6406 : : PRED_FORTRAN_OVERFLOW);
6407 : 11441 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6408 : : integer_one_node, integer_zero_node);
6409 : 11441 : cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
6410 : : logical_type_node, *element_size,
6411 : : build_int_cst (size_type_node, 0)),
6412 : : PRED_FORTRAN_SIZE_ZERO);
6413 : 11441 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
6414 : : integer_zero_node, tmp);
6415 : 11441 : tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
6416 : : *overflow, tmp);
6417 : 11441 : *overflow = gfc_evaluate_now (tmp, pblock);
6418 : :
6419 : 11441 : size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6420 : : stride, *element_size);
6421 : :
6422 : 11441 : if (poffset != NULL)
6423 : : {
6424 : 11441 : offset = gfc_evaluate_now (offset, pblock);
6425 : 11441 : *poffset = offset;
6426 : : }
6427 : :
6428 : 11441 : if (integer_zerop (or_expr))
6429 : : return size;
6430 : 3480 : if (integer_onep (or_expr))
6431 : 599 : return build_int_cst (size_type_node, 0);
6432 : :
6433 : 2881 : var = gfc_create_var (TREE_TYPE (size), "size");
6434 : 2881 : gfc_start_block (&thenblock);
6435 : 2881 : gfc_add_modify (&thenblock, var, build_int_cst (size_type_node, 0));
6436 : 2881 : thencase = gfc_finish_block (&thenblock);
6437 : :
6438 : 2881 : gfc_start_block (&elseblock);
6439 : 2881 : gfc_add_modify (&elseblock, var, size);
6440 : 2881 : elsecase = gfc_finish_block (&elseblock);
6441 : :
6442 : 2881 : tmp = gfc_evaluate_now (or_expr, pblock);
6443 : 2881 : tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
6444 : 2881 : gfc_add_expr_to_block (pblock, tmp);
6445 : :
6446 : 2881 : return var;
6447 : : }
6448 : :
6449 : :
6450 : : /* Retrieve the last ref from the chain. This routine is specific to
6451 : : gfc_array_allocate ()'s needs. */
6452 : :
6453 : : bool
6454 : 17772 : retrieve_last_ref (gfc_ref **ref_in, gfc_ref **prev_ref_in)
6455 : : {
6456 : 17772 : gfc_ref *ref, *prev_ref;
6457 : :
6458 : 17772 : ref = *ref_in;
6459 : : /* Prevent warnings for uninitialized variables. */
6460 : 17772 : prev_ref = *prev_ref_in;
6461 : 24247 : while (ref && ref->next != NULL)
6462 : : {
6463 : 6475 : gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT
6464 : : || (ref->u.ar.dimen == 0 && ref->u.ar.codimen > 0));
6465 : : prev_ref = ref;
6466 : : ref = ref->next;
6467 : : }
6468 : :
6469 : 17772 : if (ref == NULL || ref->type != REF_ARRAY)
6470 : : return false;
6471 : :
6472 : 12752 : *ref_in = ref;
6473 : 12752 : *prev_ref_in = prev_ref;
6474 : 12752 : return true;
6475 : : }
6476 : :
6477 : : /* Initializes the descriptor and generates a call to _gfor_allocate. Does
6478 : : the work for an ALLOCATE statement. */
6479 : : /*GCC ARRAYS*/
6480 : :
6481 : : bool
6482 : 16624 : gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree status, tree errmsg,
6483 : : tree errlen, tree label_finish, tree expr3_elem_size,
6484 : : tree *nelems, gfc_expr *expr3, tree e3_arr_desc,
6485 : : bool e3_has_nodescriptor, gfc_omp_namelist *omp_alloc,
6486 : : bool explicit_ts)
6487 : : {
6488 : 16624 : tree tmp;
6489 : 16624 : tree pointer;
6490 : 16624 : tree offset = NULL_TREE;
6491 : 16624 : tree token = NULL_TREE;
6492 : 16624 : tree size;
6493 : 16624 : tree msg;
6494 : 16624 : tree error = NULL_TREE;
6495 : 16624 : tree overflow; /* Boolean storing whether size calculation overflows. */
6496 : 16624 : tree var_overflow = NULL_TREE;
6497 : 16624 : tree cond;
6498 : 16624 : tree set_descriptor;
6499 : 16624 : tree not_prev_allocated = NULL_TREE;
6500 : 16624 : tree element_size = NULL_TREE;
6501 : 16624 : stmtblock_t set_descriptor_block;
6502 : 16624 : stmtblock_t elseblock;
6503 : 16624 : gfc_expr **lower;
6504 : 16624 : gfc_expr **upper;
6505 : 16624 : gfc_ref *ref, *prev_ref = NULL, *coref;
6506 : 16624 : bool allocatable, coarray, dimension, alloc_w_e3_arr_spec = false,
6507 : : non_ulimate_coarray_ptr_comp;
6508 : 16624 : tree omp_cond = NULL_TREE, omp_alt_alloc = NULL_TREE;
6509 : :
6510 : 16624 : ref = expr->ref;
6511 : :
6512 : : /* Find the last reference in the chain. */
6513 : 16624 : if (!retrieve_last_ref (&ref, &prev_ref))
6514 : : return false;
6515 : :
6516 : : /* Take the allocatable and coarray properties solely from the expr-ref's
6517 : : attributes and not from source=-expression. */
6518 : 11604 : if (!prev_ref)
6519 : : {
6520 : 8096 : allocatable = expr->symtree->n.sym->attr.allocatable;
6521 : 8096 : dimension = expr->symtree->n.sym->attr.dimension;
6522 : 8096 : non_ulimate_coarray_ptr_comp = false;
6523 : : }
6524 : : else
6525 : : {
6526 : 3508 : allocatable = prev_ref->u.c.component->attr.allocatable;
6527 : : /* Pointer components in coarrayed derived types must be treated
6528 : : specially in that they are registered without a check if the are
6529 : : already associated. This does not hold for ultimate coarray
6530 : : pointers. */
6531 : 7016 : non_ulimate_coarray_ptr_comp = (prev_ref->u.c.component->attr.pointer
6532 : 3508 : && !prev_ref->u.c.component->attr.codimension);
6533 : 3508 : dimension = prev_ref->u.c.component->attr.dimension;
6534 : : }
6535 : :
6536 : : /* For allocatable/pointer arrays in derived types, one of the refs has to be
6537 : : a coarray. In this case it does not matter whether we are on this_image
6538 : : or not. */
6539 : 11604 : coarray = false;
6540 : 27539 : for (coref = expr->ref; coref; coref = coref->next)
6541 : 16475 : if (coref->type == REF_ARRAY && coref->u.ar.codimen > 0)
6542 : : {
6543 : : coarray = true;
6544 : : break;
6545 : : }
6546 : :
6547 : 11604 : if (!dimension)
6548 : 163 : gcc_assert (coarray);
6549 : :
6550 : 11604 : if (ref->u.ar.type == AR_FULL && expr3 != NULL)
6551 : : {
6552 : 1148 : gfc_ref *old_ref = ref;
6553 : : /* F08:C633: Array shape from expr3. */
6554 : 1148 : ref = expr3->ref;
6555 : :
6556 : : /* Find the last reference in the chain. */
6557 : 1148 : if (!retrieve_last_ref (&ref, &prev_ref))
6558 : : {
6559 : 0 : if (expr3->expr_type == EXPR_FUNCTION
6560 : 0 : && gfc_expr_attr (expr3).dimension)
6561 : 0 : ref = old_ref;
6562 : : else
6563 : 0 : return false;
6564 : : }
6565 : : alloc_w_e3_arr_spec = true;
6566 : : }
6567 : :
6568 : : /* Figure out the size of the array. */
6569 : 11604 : switch (ref->u.ar.type)
6570 : : {
6571 : 8843 : case AR_ELEMENT:
6572 : 8843 : if (!coarray)
6573 : : {
6574 : 8349 : lower = NULL;
6575 : 8349 : upper = ref->u.ar.start;
6576 : 8349 : break;
6577 : : }
6578 : : /* Fall through. */
6579 : :
6580 : 2137 : case AR_SECTION:
6581 : 2137 : lower = ref->u.ar.start;
6582 : 2137 : upper = ref->u.ar.end;
6583 : 2137 : break;
6584 : :
6585 : 1118 : case AR_FULL:
6586 : 1118 : gcc_assert (ref->u.ar.as->type == AS_EXPLICIT
6587 : : || alloc_w_e3_arr_spec);
6588 : :
6589 : 1118 : lower = ref->u.ar.as->lower;
6590 : 1118 : upper = ref->u.ar.as->upper;
6591 : 1118 : break;
6592 : :
6593 : 0 : default:
6594 : 0 : gcc_unreachable ();
6595 : 11604 : break;
6596 : : }
6597 : :
6598 : 11604 : overflow = integer_zero_node;
6599 : :
6600 : 11604 : if (expr->ts.type == BT_CHARACTER
6601 : 1013 : && TREE_CODE (se->string_length) == COMPONENT_REF
6602 : 161 : && expr->ts.u.cl->backend_decl != se->string_length
6603 : 161 : && VAR_P (expr->ts.u.cl->backend_decl))
6604 : 0 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
6605 : 0 : fold_convert (TREE_TYPE (expr->ts.u.cl->backend_decl),
6606 : : se->string_length));
6607 : :
6608 : 11604 : gfc_init_block (&set_descriptor_block);
6609 : : /* Take the corank only from the actual ref and not from the coref. The
6610 : : later will mislead the generation of the array dimensions for allocatable/
6611 : : pointer components in derived types. */
6612 : 22600 : size = gfc_array_init_size (se->expr, alloc_w_e3_arr_spec ? expr->rank
6613 : 10456 : : ref->u.ar.as->rank,
6614 : 540 : coarray ? ref->u.ar.as->corank : 0,
6615 : : &offset, lower, upper,
6616 : : &se->pre, &set_descriptor_block, &overflow,
6617 : : expr3_elem_size, nelems, expr3, e3_arr_desc,
6618 : : e3_has_nodescriptor, expr, &element_size,
6619 : : explicit_ts);
6620 : :
6621 : 11604 : if (dimension)
6622 : : {
6623 : 11441 : var_overflow = gfc_create_var (integer_type_node, "overflow");
6624 : 11441 : gfc_add_modify (&se->pre, var_overflow, overflow);
6625 : :
6626 : 11441 : if (status == NULL_TREE)
6627 : : {
6628 : : /* Generate the block of code handling overflow. */
6629 : 11230 : msg = gfc_build_addr_expr (pchar_type_node,
6630 : : gfc_build_localized_cstring_const
6631 : : ("Integer overflow when calculating the amount of "
6632 : : "memory to allocate"));
6633 : 11230 : error = build_call_expr_loc (input_location,
6634 : : gfor_fndecl_runtime_error, 1, msg);
6635 : : }
6636 : : else
6637 : : {
6638 : 211 : tree status_type = TREE_TYPE (status);
6639 : 211 : stmtblock_t set_status_block;
6640 : :
6641 : 211 : gfc_start_block (&set_status_block);
6642 : 211 : gfc_add_modify (&set_status_block, status,
6643 : : build_int_cst (status_type, LIBERROR_ALLOCATION));
6644 : 211 : error = gfc_finish_block (&set_status_block);
6645 : : }
6646 : : }
6647 : :
6648 : : /* Allocate memory to store the data. */
6649 : 11604 : if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
6650 : 0 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
6651 : :
6652 : 11604 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
6653 : : {
6654 : 305 : pointer = non_ulimate_coarray_ptr_comp ? se->expr
6655 : 236 : : gfc_conv_descriptor_data_get (se->expr);
6656 : 305 : token = gfc_conv_descriptor_token (se->expr);
6657 : 305 : token = gfc_build_addr_expr (NULL_TREE, token);
6658 : : }
6659 : : else
6660 : : {
6661 : 11299 : pointer = gfc_conv_descriptor_data_get (se->expr);
6662 : 11299 : if (omp_alloc)
6663 : 33 : omp_cond = boolean_true_node;
6664 : : }
6665 : 11604 : STRIP_NOPS (pointer);
6666 : :
6667 : 11604 : if (allocatable)
6668 : : {
6669 : 9530 : not_prev_allocated = gfc_create_var (logical_type_node,
6670 : : "not_prev_allocated");
6671 : 9530 : tmp = fold_build2_loc (input_location, EQ_EXPR,
6672 : : logical_type_node, pointer,
6673 : 9530 : build_int_cst (TREE_TYPE (pointer), 0));
6674 : :
6675 : 9530 : gfc_add_modify (&se->pre, not_prev_allocated, tmp);
6676 : : }
6677 : :
6678 : 11604 : gfc_start_block (&elseblock);
6679 : :
6680 : 11604 : tree succ_add_expr = NULL_TREE;
6681 : 11604 : if (omp_cond)
6682 : : {
6683 : 33 : tree align, alloc, sz;
6684 : 33 : gfc_se se2;
6685 : 33 : if (omp_alloc->u2.allocator)
6686 : : {
6687 : 10 : gfc_init_se (&se2, NULL);
6688 : 10 : gfc_conv_expr (&se2, omp_alloc->u2.allocator);
6689 : 10 : gfc_add_block_to_block (&elseblock, &se2.pre);
6690 : 10 : alloc = gfc_evaluate_now (se2.expr, &elseblock);
6691 : 10 : gfc_add_block_to_block (&elseblock, &se2.post);
6692 : : }
6693 : : else
6694 : 23 : alloc = build_zero_cst (ptr_type_node);
6695 : 33 : tmp = TREE_TYPE (TREE_TYPE (pointer));
6696 : 33 : if (tmp == void_type_node)
6697 : 33 : tmp = gfc_typenode_for_spec (&expr->ts, 0);
6698 : 33 : if (omp_alloc->u.align)
6699 : : {
6700 : 17 : gfc_init_se (&se2, NULL);
6701 : 17 : gfc_conv_expr (&se2, omp_alloc->u.align);
6702 : 17 : gcc_assert (CONSTANT_CLASS_P (se2.expr)
6703 : : && se2.pre.head == NULL
6704 : : && se2.post.head == NULL);
6705 : 17 : align = build_int_cst (size_type_node,
6706 : 17 : MAX (tree_to_uhwi (se2.expr),
6707 : : TYPE_ALIGN_UNIT (tmp)));
6708 : : }
6709 : : else
6710 : 16 : align = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (tmp));
6711 : 33 : sz = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
6712 : : fold_convert (size_type_node, size),
6713 : : build_int_cst (size_type_node, 1));
6714 : 33 : omp_alt_alloc = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
6715 : 33 : DECL_ATTRIBUTES (omp_alt_alloc)
6716 : 33 : = tree_cons (get_identifier ("omp allocator"),
6717 : : build_tree_list (NULL_TREE, alloc),
6718 : 33 : DECL_ATTRIBUTES (omp_alt_alloc));
6719 : 33 : omp_alt_alloc = build_call_expr (omp_alt_alloc, 3, align, sz, alloc);
6720 : 33 : succ_add_expr = fold_build2_loc (input_location, MODIFY_EXPR,
6721 : : void_type_node,
6722 : : gfc_conv_descriptor_version (se->expr),
6723 : : build_int_cst (integer_type_node, 1));
6724 : : }
6725 : :
6726 : : /* The allocatable variant takes the old pointer as first argument. */
6727 : 11604 : if (allocatable)
6728 : 9998 : gfc_allocate_allocatable (&elseblock, pointer, size, token,
6729 : : status, errmsg, errlen, label_finish, expr,
6730 : 468 : coref != NULL ? coref->u.ar.as->corank : 0,
6731 : : omp_cond, omp_alt_alloc, succ_add_expr);
6732 : 2074 : else if (non_ulimate_coarray_ptr_comp && token)
6733 : : /* The token is set only for GFC_FCOARRAY_LIB mode. */
6734 : 69 : gfc_allocate_using_caf_lib (&elseblock, pointer, size, token, status,
6735 : : errmsg, errlen,
6736 : : GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY);
6737 : : else
6738 : 2005 : gfc_allocate_using_malloc (&elseblock, pointer, size, status,
6739 : : omp_cond, omp_alt_alloc, succ_add_expr);
6740 : :
6741 : 11604 : if (dimension)
6742 : : {
6743 : 11441 : cond = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
6744 : : logical_type_node, var_overflow, integer_zero_node),
6745 : : PRED_FORTRAN_OVERFLOW);
6746 : 11441 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6747 : : error, gfc_finish_block (&elseblock));
6748 : : }
6749 : : else
6750 : 163 : tmp = gfc_finish_block (&elseblock);
6751 : :
6752 : 11604 : gfc_add_expr_to_block (&se->pre, tmp);
6753 : :
6754 : : /* Update the array descriptor with the offset and the span. */
6755 : 11604 : if (dimension)
6756 : : {
6757 : 11441 : gfc_conv_descriptor_offset_set (&set_descriptor_block, se->expr, offset);
6758 : 11441 : tmp = fold_convert (gfc_array_index_type, element_size);
6759 : 11441 : gfc_conv_descriptor_span_set (&set_descriptor_block, se->expr, tmp);
6760 : : }
6761 : :
6762 : 11604 : set_descriptor = gfc_finish_block (&set_descriptor_block);
6763 : 11604 : if (status != NULL_TREE)
6764 : : {
6765 : 225 : cond = fold_build2_loc (input_location, EQ_EXPR,
6766 : : logical_type_node, status,
6767 : 225 : build_int_cst (TREE_TYPE (status), 0));
6768 : :
6769 : 225 : if (not_prev_allocated != NULL_TREE)
6770 : 209 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
6771 : : logical_type_node, cond, not_prev_allocated);
6772 : :
6773 : 225 : gfc_add_expr_to_block (&se->pre,
6774 : : fold_build3_loc (input_location, COND_EXPR, void_type_node,
6775 : : cond,
6776 : : set_descriptor,
6777 : : build_empty_stmt (input_location)));
6778 : : }
6779 : : else
6780 : 11379 : gfc_add_expr_to_block (&se->pre, set_descriptor);
6781 : :
6782 : 11604 : expr->symtree->n.sym->allocated_in_scope = 1;
6783 : :
6784 : 11604 : return true;
6785 : : }
6786 : :
6787 : :
6788 : : /* Create an array constructor from an initialization expression.
6789 : : We assume the frontend already did any expansions and conversions. */
6790 : :
6791 : : tree
6792 : 7408 : gfc_conv_array_initializer (tree type, gfc_expr * expr)
6793 : : {
6794 : 7408 : gfc_constructor *c;
6795 : 7408 : tree tmp;
6796 : 7408 : gfc_se se;
6797 : 7408 : tree index, range;
6798 : 7408 : vec<constructor_elt, va_gc> *v = NULL;
6799 : :
6800 : 7408 : if (expr->expr_type == EXPR_VARIABLE
6801 : 0 : && expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6802 : 0 : && expr->symtree->n.sym->value)
6803 : 7408 : expr = expr->symtree->n.sym->value;
6804 : :
6805 : 7408 : switch (expr->expr_type)
6806 : : {
6807 : 1091 : case EXPR_CONSTANT:
6808 : 1091 : case EXPR_STRUCTURE:
6809 : : /* A single scalar or derived type value. Create an array with all
6810 : : elements equal to that value. */
6811 : 1091 : gfc_init_se (&se, NULL);
6812 : :
6813 : 1091 : if (expr->expr_type == EXPR_CONSTANT)
6814 : 377 : gfc_conv_constant (&se, expr);
6815 : : else
6816 : 714 : gfc_conv_structure (&se, expr, 1);
6817 : :
6818 : 2182 : if (tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
6819 : 1091 : TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
6820 : : break;
6821 : 2158 : else if (tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
6822 : 1079 : TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
6823 : 144 : range = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
6824 : : else
6825 : 1870 : range = build2 (RANGE_EXPR, gfc_array_index_type,
6826 : 935 : TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
6827 : 935 : TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
6828 : 1079 : CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
6829 : 1079 : break;
6830 : :
6831 : 6317 : case EXPR_ARRAY:
6832 : : /* Create a vector of all the elements. */
6833 : 6317 : for (c = gfc_constructor_first (expr->value.constructor);
6834 : 163144 : c && c->expr; c = gfc_constructor_next (c))
6835 : : {
6836 : 156827 : if (c->iterator)
6837 : : {
6838 : : /* Problems occur when we get something like
6839 : : integer :: a(lots) = (/(i, i=1, lots)/) */
6840 : 0 : gfc_fatal_error ("The number of elements in the array "
6841 : : "constructor at %L requires an increase of "
6842 : : "the allowed %d upper limit. See "
6843 : : "%<-fmax-array-constructor%> option",
6844 : : &expr->where, flag_max_array_constructor);
6845 : : return NULL_TREE;
6846 : : }
6847 : 156827 : if (mpz_cmp_si (c->offset, 0) != 0)
6848 : 150767 : index = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
6849 : : else
6850 : : index = NULL_TREE;
6851 : :
6852 : 156827 : if (mpz_cmp_si (c->repeat, 1) > 0)
6853 : : {
6854 : 127 : tree tmp1, tmp2;
6855 : 127 : mpz_t maxval;
6856 : :
6857 : 127 : mpz_init (maxval);
6858 : 127 : mpz_add (maxval, c->offset, c->repeat);
6859 : 127 : mpz_sub_ui (maxval, maxval, 1);
6860 : 127 : tmp2 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
6861 : 127 : if (mpz_cmp_si (c->offset, 0) != 0)
6862 : : {
6863 : 27 : mpz_add_ui (maxval, c->offset, 1);
6864 : 27 : tmp1 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
6865 : : }
6866 : : else
6867 : 100 : tmp1 = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
6868 : :
6869 : 127 : range = fold_build2 (RANGE_EXPR, gfc_array_index_type, tmp1, tmp2);
6870 : 127 : mpz_clear (maxval);
6871 : : }
6872 : : else
6873 : : range = NULL;
6874 : :
6875 : 156827 : gfc_init_se (&se, NULL);
6876 : 156827 : switch (c->expr->expr_type)
6877 : : {
6878 : 155573 : case EXPR_CONSTANT:
6879 : 155573 : gfc_conv_constant (&se, c->expr);
6880 : :
6881 : : /* See gfortran.dg/charlen_15.f90 for instance. */
6882 : 155573 : if (TREE_CODE (se.expr) == STRING_CST
6883 : 4990 : && TREE_CODE (type) == ARRAY_TYPE)
6884 : : {
6885 : : tree atype = type;
6886 : 9980 : while (TREE_CODE (TREE_TYPE (atype)) == ARRAY_TYPE)
6887 : 4990 : atype = TREE_TYPE (atype);
6888 : 4990 : gcc_checking_assert (TREE_CODE (TREE_TYPE (atype))
6889 : : == INTEGER_TYPE);
6890 : 4990 : gcc_checking_assert (TREE_TYPE (TREE_TYPE (se.expr))
6891 : : == TREE_TYPE (atype));
6892 : 4990 : if (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (se.expr)))
6893 : 4990 : > tree_to_uhwi (TYPE_SIZE_UNIT (atype)))
6894 : : {
6895 : 0 : unsigned HOST_WIDE_INT size
6896 : 0 : = tree_to_uhwi (TYPE_SIZE_UNIT (atype));
6897 : 0 : const char *p = TREE_STRING_POINTER (se.expr);
6898 : :
6899 : 0 : se.expr = build_string (size, p);
6900 : : }
6901 : 4990 : TREE_TYPE (se.expr) = atype;
6902 : : }
6903 : : break;
6904 : :
6905 : 1254 : case EXPR_STRUCTURE:
6906 : 1254 : gfc_conv_structure (&se, c->expr, 1);
6907 : 1254 : break;
6908 : :
6909 : 0 : default:
6910 : : /* Catch those occasional beasts that do not simplify
6911 : : for one reason or another, assuming that if they are
6912 : : standard defying the frontend will catch them. */
6913 : 0 : gfc_conv_expr (&se, c->expr);
6914 : 0 : break;
6915 : : }
6916 : :
6917 : 156827 : if (range == NULL_TREE)
6918 : 156700 : CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
6919 : : else
6920 : : {
6921 : 127 : if (index != NULL_TREE)
6922 : 27 : CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
6923 : 156954 : CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
6924 : : }
6925 : : }
6926 : : break;
6927 : :
6928 : 0 : case EXPR_NULL:
6929 : 0 : return gfc_build_null_descriptor (type);
6930 : :
6931 : 0 : default:
6932 : 0 : gcc_unreachable ();
6933 : : }
6934 : :
6935 : : /* Create a constructor from the list of elements. */
6936 : 7408 : tmp = build_constructor (type, v);
6937 : 7408 : TREE_CONSTANT (tmp) = 1;
6938 : 7408 : return tmp;
6939 : : }
6940 : :
6941 : :
6942 : : /* Generate code to evaluate non-constant coarray cobounds. */
6943 : :
6944 : : void
6945 : 19446 : gfc_trans_array_cobounds (tree type, stmtblock_t * pblock,
6946 : : const gfc_symbol *sym)
6947 : : {
6948 : 19446 : int dim;
6949 : 19446 : tree ubound;
6950 : 19446 : tree lbound;
6951 : 19446 : gfc_se se;
6952 : 19446 : gfc_array_spec *as;
6953 : :
6954 : 19446 : as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
6955 : :
6956 : 20209 : for (dim = as->rank; dim < as->rank + as->corank; dim++)
6957 : : {
6958 : : /* Evaluate non-constant array bound expressions.
6959 : : F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
6960 : : references a function, the result is finalized before execution of the
6961 : : executable constructs in the scoping unit.
6962 : : Adding the finalblocks enables this. */
6963 : 763 : lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
6964 : 763 : if (as->lower[dim] && !INTEGER_CST_P (lbound))
6965 : : {
6966 : 96 : gfc_init_se (&se, NULL);
6967 : 96 : gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
6968 : 96 : gfc_add_block_to_block (pblock, &se.pre);
6969 : 96 : gfc_add_block_to_block (pblock, &se.finalblock);
6970 : 96 : gfc_add_modify (pblock, lbound, se.expr);
6971 : : }
6972 : 763 : ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
6973 : 763 : if (as->upper[dim] && !INTEGER_CST_P (ubound))
6974 : : {
6975 : 50 : gfc_init_se (&se, NULL);
6976 : 50 : gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
6977 : 50 : gfc_add_block_to_block (pblock, &se.pre);
6978 : 50 : gfc_add_block_to_block (pblock, &se.finalblock);
6979 : 50 : gfc_add_modify (pblock, ubound, se.expr);
6980 : : }
6981 : : }
6982 : 19446 : }
6983 : :
6984 : :
6985 : : /* Generate code to evaluate non-constant array bounds. Sets *poffset and
6986 : : returns the size (in elements) of the array. */
6987 : :
6988 : : tree
6989 : 12569 : gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
6990 : : stmtblock_t * pblock)
6991 : : {
6992 : 12569 : gfc_array_spec *as;
6993 : 12569 : tree size;
6994 : 12569 : tree stride;
6995 : 12569 : tree offset;
6996 : 12569 : tree ubound;
6997 : 12569 : tree lbound;
6998 : 12569 : tree tmp;
6999 : 12569 : gfc_se se;
7000 : :
7001 : 12569 : int dim;
7002 : :
7003 : 12569 : as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
7004 : :
7005 : 12569 : size = gfc_index_one_node;
7006 : 12569 : offset = gfc_index_zero_node;
7007 : 12569 : stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
7008 : 12569 : if (stride && VAR_P (stride))
7009 : 124 : gfc_add_modify (pblock, stride, gfc_index_one_node);
7010 : 28371 : for (dim = 0; dim < as->rank; dim++)
7011 : : {
7012 : : /* Evaluate non-constant array bound expressions.
7013 : : F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
7014 : : references a function, the result is finalized before execution of the
7015 : : executable constructs in the scoping unit.
7016 : : Adding the finalblocks enables this. */
7017 : 15802 : lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
7018 : 15802 : if (as->lower[dim] && !INTEGER_CST_P (lbound))
7019 : : {
7020 : 469 : gfc_init_se (&se, NULL);
7021 : 469 : gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
7022 : 469 : gfc_add_block_to_block (pblock, &se.pre);
7023 : 469 : gfc_add_block_to_block (pblock, &se.finalblock);
7024 : 469 : gfc_add_modify (pblock, lbound, se.expr);
7025 : : }
7026 : 15802 : ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
7027 : 15802 : if (as->upper[dim] && !INTEGER_CST_P (ubound))
7028 : : {
7029 : 9464 : gfc_init_se (&se, NULL);
7030 : 9464 : gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
7031 : 9464 : gfc_add_block_to_block (pblock, &se.pre);
7032 : 9464 : gfc_add_block_to_block (pblock, &se.finalblock);
7033 : 9464 : gfc_add_modify (pblock, ubound, se.expr);
7034 : : }
7035 : : /* The offset of this dimension. offset = offset - lbound * stride. */
7036 : 15802 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7037 : : lbound, size);
7038 : 15802 : offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7039 : : offset, tmp);
7040 : :
7041 : : /* The size of this dimension, and the stride of the next. */
7042 : 15802 : if (dim + 1 < as->rank)
7043 : 3380 : stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
7044 : : else
7045 : 12422 : stride = GFC_TYPE_ARRAY_SIZE (type);
7046 : :
7047 : 15802 : if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
7048 : : {
7049 : : /* Calculate stride = size * (ubound + 1 - lbound). */
7050 : 9648 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7051 : : gfc_array_index_type,
7052 : : gfc_index_one_node, lbound);
7053 : 9648 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7054 : : gfc_array_index_type, ubound, tmp);
7055 : 9648 : tmp = fold_build2_loc (input_location, MULT_EXPR,
7056 : : gfc_array_index_type, size, tmp);
7057 : 9648 : if (stride)
7058 : 9648 : gfc_add_modify (pblock, stride, tmp);
7059 : : else
7060 : 0 : stride = gfc_evaluate_now (tmp, pblock);
7061 : :
7062 : : /* Make sure that negative size arrays are translated
7063 : : to being zero size. */
7064 : 9648 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
7065 : : stride, gfc_index_zero_node);
7066 : 9648 : tmp = fold_build3_loc (input_location, COND_EXPR,
7067 : : gfc_array_index_type, tmp,
7068 : : stride, gfc_index_zero_node);
7069 : 9648 : gfc_add_modify (pblock, stride, tmp);
7070 : : }
7071 : :
7072 : : size = stride;
7073 : : }
7074 : :
7075 : 12569 : gfc_trans_array_cobounds (type, pblock, sym);
7076 : 12569 : gfc_trans_vla_type_sizes (sym, pblock);
7077 : :
7078 : 12569 : *poffset = offset;
7079 : 12569 : return size;
7080 : : }
7081 : :
7082 : :
7083 : : /* Generate code to initialize/allocate an array variable. */
7084 : :
7085 : : void
7086 : 30245 : gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym,
7087 : : gfc_wrapped_block * block)
7088 : : {
7089 : 30245 : stmtblock_t init;
7090 : 30245 : tree type;
7091 : 30245 : tree tmp = NULL_TREE;
7092 : 30245 : tree size;
7093 : 30245 : tree offset;
7094 : 30245 : tree space;
7095 : 30245 : tree inittree;
7096 : 30245 : bool onstack;
7097 : 30245 : bool back;
7098 : :
7099 : 30245 : gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
7100 : :
7101 : : /* Do nothing for USEd variables. */
7102 : 30245 : if (sym->attr.use_assoc)
7103 : 25238 : return;
7104 : :
7105 : 30205 : type = TREE_TYPE (decl);
7106 : 30205 : gcc_assert (GFC_ARRAY_TYPE_P (type));
7107 : 30205 : onstack = TREE_CODE (type) != POINTER_TYPE;
7108 : :
7109 : : /* In the case of non-dummy symbols with dependencies on an old-fashioned
7110 : : function result (ie. proc_name = proc_name->result), gfc_add_init_cleanup
7111 : : must be called with the last, optional argument false so that the alloc-
7112 : : ation occurs after the processing of the result. */
7113 : 30205 : back = sym->fn_result_dep;
7114 : :
7115 : 30205 : gfc_init_block (&init);
7116 : :
7117 : : /* Evaluate character string length. */
7118 : 30205 : if (sym->ts.type == BT_CHARACTER
7119 : 2953 : && onstack && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
7120 : : {
7121 : 43 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7122 : :
7123 : 43 : gfc_trans_vla_type_sizes (sym, &init);
7124 : :
7125 : : /* Emit a DECL_EXPR for this variable, which will cause the
7126 : : gimplifier to allocate storage, and all that good stuff. */
7127 : 43 : tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
7128 : 43 : gfc_add_expr_to_block (&init, tmp);
7129 : 43 : if (sym->attr.omp_allocate)
7130 : : {
7131 : : /* Save location of size calculation to ensure GOMP_alloc is placed
7132 : : after it. */
7133 : 0 : tree omp_alloc = lookup_attribute ("omp allocate",
7134 : 0 : DECL_ATTRIBUTES (decl));
7135 : 0 : TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
7136 : 0 : = build_tree_list (NULL_TREE, tsi_stmt (tsi_last (init.head)));
7137 : : }
7138 : : }
7139 : :
7140 : 30003 : if (onstack)
7141 : : {
7142 : 25058 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
7143 : : back);
7144 : 25058 : return;
7145 : : }
7146 : :
7147 : 5147 : type = TREE_TYPE (type);
7148 : :
7149 : 5147 : gcc_assert (!sym->attr.use_assoc);
7150 : 5147 : gcc_assert (!sym->module);
7151 : :
7152 : 5147 : if (sym->ts.type == BT_CHARACTER
7153 : 202 : && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
7154 : 94 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7155 : :
7156 : 5147 : size = gfc_trans_array_bounds (type, sym, &offset, &init);
7157 : :
7158 : : /* Don't actually allocate space for Cray Pointees. */
7159 : 5147 : if (sym->attr.cray_pointee)
7160 : : {
7161 : 140 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7162 : 49 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7163 : :
7164 : 140 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
7165 : 140 : return;
7166 : : }
7167 : 5007 : if (sym->attr.omp_allocate)
7168 : : {
7169 : : /* The size is the number of elements in the array, so multiply by the
7170 : : size of an element to get the total size. */
7171 : 7 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
7172 : 7 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7173 : : size, fold_convert (gfc_array_index_type, tmp));
7174 : 7 : size = gfc_evaluate_now (size, &init);
7175 : :
7176 : 7 : tree omp_alloc = lookup_attribute ("omp allocate",
7177 : 7 : DECL_ATTRIBUTES (decl));
7178 : 7 : TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
7179 : 7 : = build_tree_list (size, NULL_TREE);
7180 : 7 : space = NULL_TREE;
7181 : : }
7182 : 5000 : else if (flag_stack_arrays)
7183 : : {
7184 : 11 : gcc_assert (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE);
7185 : 11 : space = build_decl (gfc_get_location (&sym->declared_at),
7186 : : VAR_DECL, create_tmp_var_name ("A"),
7187 : 11 : TREE_TYPE (TREE_TYPE (decl)));
7188 : 11 : gfc_trans_vla_type_sizes (sym, &init);
7189 : : }
7190 : : else
7191 : : {
7192 : : /* The size is the number of elements in the array, so multiply by the
7193 : : size of an element to get the total size. */
7194 : 4989 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
7195 : 4989 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7196 : : size, fold_convert (gfc_array_index_type, tmp));
7197 : :
7198 : : /* Allocate memory to hold the data. */
7199 : 4989 : tmp = gfc_call_malloc (&init, TREE_TYPE (decl), size);
7200 : 4989 : gfc_add_modify (&init, decl, tmp);
7201 : :
7202 : : /* Free the temporary. */
7203 : 4989 : tmp = gfc_call_free (decl);
7204 : 4989 : space = NULL_TREE;
7205 : : }
7206 : :
7207 : : /* Set offset of the array. */
7208 : 5007 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7209 : 367 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7210 : :
7211 : : /* Automatic arrays should not have initializers. */
7212 : 5007 : gcc_assert (!sym->value);
7213 : :
7214 : 5007 : inittree = gfc_finish_block (&init);
7215 : :
7216 : 5007 : if (space)
7217 : : {
7218 : 11 : tree addr;
7219 : 11 : pushdecl (space);
7220 : :
7221 : : /* Don't create new scope, emit the DECL_EXPR in exactly the scope
7222 : : where also space is located. */
7223 : 11 : gfc_init_block (&init);
7224 : 11 : tmp = fold_build1_loc (input_location, DECL_EXPR,
7225 : 11 : TREE_TYPE (space), space);
7226 : 11 : gfc_add_expr_to_block (&init, tmp);
7227 : 11 : addr = fold_build1_loc (gfc_get_location (&sym->declared_at),
7228 : 11 : ADDR_EXPR, TREE_TYPE (decl), space);
7229 : 11 : gfc_add_modify (&init, decl, addr);
7230 : 11 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
7231 : : back);
7232 : 11 : tmp = NULL_TREE;
7233 : : }
7234 : 5007 : gfc_add_init_cleanup (block, inittree, tmp, back);
7235 : : }
7236 : :
7237 : :
7238 : : /* Generate entry and exit code for g77 calling convention arrays. */
7239 : :
7240 : : void
7241 : 7168 : gfc_trans_g77_array (gfc_symbol * sym, gfc_wrapped_block * block)
7242 : : {
7243 : 7168 : tree parm;
7244 : 7168 : tree type;
7245 : 7168 : tree offset;
7246 : 7168 : tree tmp;
7247 : 7168 : tree stmt;
7248 : 7168 : stmtblock_t init;
7249 : :
7250 : 7168 : location_t loc = input_location;
7251 : 7168 : input_location = gfc_get_location (&sym->declared_at);
7252 : :
7253 : : /* Descriptor type. */
7254 : 7168 : parm = sym->backend_decl;
7255 : 7168 : type = TREE_TYPE (parm);
7256 : 7168 : gcc_assert (GFC_ARRAY_TYPE_P (type));
7257 : :
7258 : 7168 : gfc_start_block (&init);
7259 : :
7260 : 7168 : if (sym->ts.type == BT_CHARACTER
7261 : 661 : && VAR_P (sym->ts.u.cl->backend_decl))
7262 : 79 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7263 : :
7264 : : /* Evaluate the bounds of the array. */
7265 : 7168 : gfc_trans_array_bounds (type, sym, &offset, &init);
7266 : :
7267 : : /* Set the offset. */
7268 : 7168 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7269 : 1203 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7270 : :
7271 : : /* Set the pointer itself if we aren't using the parameter directly. */
7272 : 7168 : if (TREE_CODE (parm) != PARM_DECL)
7273 : : {
7274 : 559 : tmp = GFC_DECL_SAVED_DESCRIPTOR (parm);
7275 : 559 : if (sym->ts.type == BT_CLASS)
7276 : : {
7277 : 239 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
7278 : 239 : tmp = gfc_class_data_get (tmp);
7279 : 239 : tmp = gfc_conv_descriptor_data_get (tmp);
7280 : : }
7281 : 559 : tmp = convert (TREE_TYPE (parm), tmp);
7282 : 559 : gfc_add_modify (&init, parm, tmp);
7283 : : }
7284 : 7168 : stmt = gfc_finish_block (&init);
7285 : :
7286 : 7168 : input_location = loc;
7287 : :
7288 : : /* Add the initialization code to the start of the function. */
7289 : :
7290 : 7168 : if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
7291 : 7168 : || sym->attr.optional
7292 : 6686 : || sym->attr.not_always_present)
7293 : : {
7294 : 539 : tree nullify;
7295 : 539 : if (TREE_CODE (parm) != PARM_DECL)
7296 : 105 : nullify = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7297 : : parm, null_pointer_node);
7298 : : else
7299 : 434 : nullify = build_empty_stmt (input_location);
7300 : 539 : tmp = gfc_conv_expr_present (sym, true);
7301 : 539 : stmt = build3_v (COND_EXPR, tmp, stmt, nullify);
7302 : : }
7303 : :
7304 : 7168 : gfc_add_init_cleanup (block, stmt, NULL_TREE);
7305 : 7168 : }
7306 : :
7307 : :
7308 : : /* Modify the descriptor of an array parameter so that it has the
7309 : : correct lower bound. Also move the upper bound accordingly.
7310 : : If the array is not packed, it will be copied into a temporary.
7311 : : For each dimension we set the new lower and upper bounds. Then we copy the
7312 : : stride and calculate the offset for this dimension. We also work out
7313 : : what the stride of a packed array would be, and see it the two match.
7314 : : If the array need repacking, we set the stride to the values we just
7315 : : calculated, recalculate the offset and copy the array data.
7316 : : Code is also added to copy the data back at the end of the function.
7317 : : */
7318 : :
7319 : : void
7320 : 12424 : gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc,
7321 : : gfc_wrapped_block * block)
7322 : : {
7323 : 12424 : tree size;
7324 : 12424 : tree type;
7325 : 12424 : tree offset;
7326 : 12424 : stmtblock_t init;
7327 : 12424 : tree stmtInit, stmtCleanup;
7328 : 12424 : tree lbound;
7329 : 12424 : tree ubound;
7330 : 12424 : tree dubound;
7331 : 12424 : tree dlbound;
7332 : 12424 : tree dumdesc;
7333 : 12424 : tree tmp;
7334 : 12424 : tree stride, stride2;
7335 : 12424 : tree stmt_packed;
7336 : 12424 : tree stmt_unpacked;
7337 : 12424 : tree partial;
7338 : 12424 : gfc_se se;
7339 : 12424 : int n;
7340 : 12424 : int checkparm;
7341 : 12424 : int no_repack;
7342 : 12424 : bool optional_arg;
7343 : 12424 : gfc_array_spec *as;
7344 : 12424 : bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
7345 : :
7346 : : /* Do nothing for pointer and allocatable arrays. */
7347 : 12424 : if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
7348 : 12327 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
7349 : 12327 : || sym->attr.allocatable
7350 : 12221 : || (is_classarray && CLASS_DATA (sym)->attr.allocatable))
7351 : 5869 : return;
7352 : :
7353 : 724 : if ((!is_classarray
7354 : 724 : || (is_classarray && CLASS_DATA (sym)->as->type == AS_EXPLICIT))
7355 : 11677 : && sym->attr.dummy && !sym->attr.elemental && gfc_is_nodesc_array (sym))
7356 : : {
7357 : 5666 : gfc_trans_g77_array (sym, block);
7358 : 5666 : return;
7359 : : }
7360 : :
7361 : 6555 : location_t loc = input_location;
7362 : 6555 : input_location = gfc_get_location (&sym->declared_at);
7363 : :
7364 : : /* Descriptor type. */
7365 : 6555 : type = TREE_TYPE (tmpdesc);
7366 : 6555 : gcc_assert (GFC_ARRAY_TYPE_P (type));
7367 : 6555 : dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
7368 : 6555 : if (is_classarray)
7369 : : /* For a class array the dummy array descriptor is in the _class
7370 : : component. */
7371 : 563 : dumdesc = gfc_class_data_get (dumdesc);
7372 : : else
7373 : 5992 : dumdesc = build_fold_indirect_ref_loc (input_location, dumdesc);
7374 : 6555 : as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
7375 : 6555 : gfc_start_block (&init);
7376 : :
7377 : 6555 : if (sym->ts.type == BT_CHARACTER
7378 : 765 : && VAR_P (sym->ts.u.cl->backend_decl))
7379 : 87 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
7380 : :
7381 : : /* TODO: Fix the exclusion of class arrays from extent checking. */
7382 : 1047 : checkparm = (as->type == AS_EXPLICIT && !is_classarray
7383 : 7583 : && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS));
7384 : :
7385 : 6555 : no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
7386 : 6554 : || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
7387 : :
7388 : 6555 : if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
7389 : : {
7390 : : /* For non-constant shape arrays we only check if the first dimension
7391 : : is contiguous. Repacking higher dimensions wouldn't gain us
7392 : : anything as we still don't know the array stride. */
7393 : 1 : partial = gfc_create_var (logical_type_node, "partial");
7394 : 1 : TREE_USED (partial) = 1;
7395 : 1 : tmp = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
7396 : 1 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
7397 : : gfc_index_one_node);
7398 : 1 : gfc_add_modify (&init, partial, tmp);
7399 : : }
7400 : : else
7401 : : partial = NULL_TREE;
7402 : :
7403 : : /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
7404 : : here, however I think it does the right thing. */
7405 : 6555 : if (no_repack)
7406 : : {
7407 : : /* Set the first stride. */
7408 : 6553 : stride = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
7409 : 6553 : stride = gfc_evaluate_now (stride, &init);
7410 : :
7411 : 6553 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7412 : : stride, gfc_index_zero_node);
7413 : 6553 : tmp = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
7414 : : tmp, gfc_index_one_node, stride);
7415 : 6553 : stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
7416 : 6553 : gfc_add_modify (&init, stride, tmp);
7417 : :
7418 : : /* Allow the user to disable array repacking. */
7419 : 6553 : stmt_unpacked = NULL_TREE;
7420 : : }
7421 : : else
7422 : : {
7423 : 2 : gcc_assert (integer_onep (GFC_TYPE_ARRAY_STRIDE (type, 0)));
7424 : : /* A library call to repack the array if necessary. */
7425 : 2 : tmp = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
7426 : 2 : stmt_unpacked = build_call_expr_loc (input_location,
7427 : : gfor_fndecl_in_pack, 1, tmp);
7428 : :
7429 : 2 : stride = gfc_index_one_node;
7430 : :
7431 : 2 : if (warn_array_temporaries)
7432 : : {
7433 : 1 : locus where;
7434 : 1 : gfc_locus_from_location (&where, loc);
7435 : 1 : gfc_warning (OPT_Warray_temporaries,
7436 : : "Creating array temporary at %L", &where);
7437 : : }
7438 : : }
7439 : :
7440 : : /* This is for the case where the array data is used directly without
7441 : : calling the repack function. */
7442 : 6555 : if (no_repack || partial != NULL_TREE)
7443 : 6554 : stmt_packed = gfc_conv_descriptor_data_get (dumdesc);
7444 : : else
7445 : : stmt_packed = NULL_TREE;
7446 : :
7447 : : /* Assign the data pointer. */
7448 : 6555 : if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
7449 : : {
7450 : : /* Don't repack unknown shape arrays when the first stride is 1. */
7451 : 1 : tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (stmt_packed),
7452 : : partial, stmt_packed, stmt_unpacked);
7453 : : }
7454 : : else
7455 : 6554 : tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
7456 : 6555 : gfc_add_modify (&init, tmpdesc, fold_convert (type, tmp));
7457 : :
7458 : 6555 : offset = gfc_index_zero_node;
7459 : 6555 : size = gfc_index_one_node;
7460 : :
7461 : : /* Evaluate the bounds of the array. */
7462 : 15369 : for (n = 0; n < as->rank; n++)
7463 : : {
7464 : 8814 : if (checkparm || !as->upper[n])
7465 : : {
7466 : : /* Get the bounds of the actual parameter. */
7467 : 7532 : dubound = gfc_conv_descriptor_ubound_get (dumdesc, gfc_rank_cst[n]);
7468 : 7532 : dlbound = gfc_conv_descriptor_lbound_get (dumdesc, gfc_rank_cst[n]);
7469 : : }
7470 : : else
7471 : : {
7472 : : dubound = NULL_TREE;
7473 : : dlbound = NULL_TREE;
7474 : : }
7475 : :
7476 : 8814 : lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
7477 : 8814 : if (!INTEGER_CST_P (lbound))
7478 : : {
7479 : 45 : gfc_init_se (&se, NULL);
7480 : 45 : gfc_conv_expr_type (&se, as->lower[n],
7481 : : gfc_array_index_type);
7482 : 45 : gfc_add_block_to_block (&init, &se.pre);
7483 : 45 : gfc_add_modify (&init, lbound, se.expr);
7484 : : }
7485 : :
7486 : 8814 : ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
7487 : : /* Set the desired upper bound. */
7488 : 8814 : if (as->upper[n])
7489 : : {
7490 : : /* We know what we want the upper bound to be. */
7491 : 1340 : if (!INTEGER_CST_P (ubound))
7492 : : {
7493 : 620 : gfc_init_se (&se, NULL);
7494 : 620 : gfc_conv_expr_type (&se, as->upper[n],
7495 : : gfc_array_index_type);
7496 : 620 : gfc_add_block_to_block (&init, &se.pre);
7497 : 620 : gfc_add_modify (&init, ubound, se.expr);
7498 : : }
7499 : :
7500 : : /* Check the sizes match. */
7501 : 1340 : if (checkparm)
7502 : : {
7503 : : /* Check (ubound(a) - lbound(a) == ubound(b) - lbound(b)). */
7504 : 58 : char * msg;
7505 : 58 : tree temp;
7506 : 58 : locus where;
7507 : :
7508 : 58 : gfc_locus_from_location (&where, loc);
7509 : 58 : temp = fold_build2_loc (input_location, MINUS_EXPR,
7510 : : gfc_array_index_type, ubound, lbound);
7511 : 58 : temp = fold_build2_loc (input_location, PLUS_EXPR,
7512 : : gfc_array_index_type,
7513 : : gfc_index_one_node, temp);
7514 : 58 : stride2 = fold_build2_loc (input_location, MINUS_EXPR,
7515 : : gfc_array_index_type, dubound,
7516 : : dlbound);
7517 : 58 : stride2 = fold_build2_loc (input_location, PLUS_EXPR,
7518 : : gfc_array_index_type,
7519 : : gfc_index_one_node, stride2);
7520 : 58 : tmp = fold_build2_loc (input_location, NE_EXPR,
7521 : : gfc_array_index_type, temp, stride2);
7522 : 58 : msg = xasprintf ("Dimension %d of array '%s' has extent "
7523 : : "%%ld instead of %%ld", n+1, sym->name);
7524 : :
7525 : 58 : gfc_trans_runtime_check (true, false, tmp, &init, &where, msg,
7526 : : fold_convert (long_integer_type_node, temp),
7527 : : fold_convert (long_integer_type_node, stride2));
7528 : :
7529 : 58 : free (msg);
7530 : : }
7531 : : }
7532 : : else
7533 : : {
7534 : : /* For assumed shape arrays move the upper bound by the same amount
7535 : : as the lower bound. */
7536 : 7474 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7537 : : gfc_array_index_type, dubound, dlbound);
7538 : 7474 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7539 : : gfc_array_index_type, tmp, lbound);
7540 : 7474 : gfc_add_modify (&init, ubound, tmp);
7541 : : }
7542 : : /* The offset of this dimension. offset = offset - lbound * stride. */
7543 : 8814 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7544 : : lbound, stride);
7545 : 8814 : offset = fold_build2_loc (input_location, MINUS_EXPR,
7546 : : gfc_array_index_type, offset, tmp);
7547 : :
7548 : : /* The size of this dimension, and the stride of the next. */
7549 : 8814 : if (n + 1 < as->rank)
7550 : : {
7551 : 2259 : stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
7552 : :
7553 : 2259 : if (no_repack || partial != NULL_TREE)
7554 : 2258 : stmt_unpacked =
7555 : 2258 : gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[n+1]);
7556 : :
7557 : : /* Figure out the stride if not a known constant. */
7558 : 2259 : if (!INTEGER_CST_P (stride))
7559 : : {
7560 : 2258 : if (no_repack)
7561 : : stmt_packed = NULL_TREE;
7562 : : else
7563 : : {
7564 : : /* Calculate stride = size * (ubound + 1 - lbound). */
7565 : 0 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7566 : : gfc_array_index_type,
7567 : : gfc_index_one_node, lbound);
7568 : 0 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7569 : : gfc_array_index_type, ubound, tmp);
7570 : 0 : size = fold_build2_loc (input_location, MULT_EXPR,
7571 : : gfc_array_index_type, size, tmp);
7572 : 0 : stmt_packed = size;
7573 : : }
7574 : :
7575 : : /* Assign the stride. */
7576 : 2258 : if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
7577 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR,
7578 : : gfc_array_index_type, partial,
7579 : : stmt_unpacked, stmt_packed);
7580 : : else
7581 : 2258 : tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
7582 : 2258 : gfc_add_modify (&init, stride, tmp);
7583 : : }
7584 : : }
7585 : : else
7586 : : {
7587 : 6555 : stride = GFC_TYPE_ARRAY_SIZE (type);
7588 : :
7589 : 6555 : if (stride && !INTEGER_CST_P (stride))
7590 : : {
7591 : : /* Calculate size = stride * (ubound + 1 - lbound). */
7592 : 6554 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
7593 : : gfc_array_index_type,
7594 : : gfc_index_one_node, lbound);
7595 : 6554 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
7596 : : gfc_array_index_type,
7597 : : ubound, tmp);
7598 : 19662 : tmp = fold_build2_loc (input_location, MULT_EXPR,
7599 : : gfc_array_index_type,
7600 : 6554 : GFC_TYPE_ARRAY_STRIDE (type, n), tmp);
7601 : 6554 : gfc_add_modify (&init, stride, tmp);
7602 : : }
7603 : : }
7604 : : }
7605 : :
7606 : 6555 : gfc_trans_array_cobounds (type, &init, sym);
7607 : :
7608 : : /* Set the offset. */
7609 : 6555 : if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
7610 : 6553 : gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
7611 : :
7612 : 6555 : gfc_trans_vla_type_sizes (sym, &init);
7613 : :
7614 : 6555 : stmtInit = gfc_finish_block (&init);
7615 : :
7616 : : /* Only do the entry/initialization code if the arg is present. */
7617 : 6555 : dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
7618 : 6555 : optional_arg = (sym->attr.optional
7619 : 6555 : || (sym->ns->proc_name->attr.entry_master
7620 : 79 : && sym->attr.dummy));
7621 : : if (optional_arg)
7622 : : {
7623 : 716 : tree zero_init = fold_convert (TREE_TYPE (tmpdesc), null_pointer_node);
7624 : 716 : zero_init = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7625 : : tmpdesc, zero_init);
7626 : 716 : tmp = gfc_conv_expr_present (sym, true);
7627 : 716 : stmtInit = build3_v (COND_EXPR, tmp, stmtInit, zero_init);
7628 : : }
7629 : :
7630 : : /* Cleanup code. */
7631 : 6555 : if (no_repack)
7632 : : stmtCleanup = NULL_TREE;
7633 : : else
7634 : : {
7635 : 2 : stmtblock_t cleanup;
7636 : 2 : gfc_start_block (&cleanup);
7637 : :
7638 : 2 : if (sym->attr.intent != INTENT_IN)
7639 : : {
7640 : : /* Copy the data back. */
7641 : 2 : tmp = build_call_expr_loc (input_location,
7642 : : gfor_fndecl_in_unpack, 2, dumdesc, tmpdesc);
7643 : 2 : gfc_add_expr_to_block (&cleanup, tmp);
7644 : : }
7645 : :
7646 : : /* Free the temporary. */
7647 : 2 : tmp = gfc_call_free (tmpdesc);
7648 : 2 : gfc_add_expr_to_block (&cleanup, tmp);
7649 : :
7650 : 2 : stmtCleanup = gfc_finish_block (&cleanup);
7651 : :
7652 : : /* Only do the cleanup if the array was repacked. */
7653 : 2 : if (is_classarray)
7654 : : /* For a class array the dummy array descriptor is in the _class
7655 : : component. */
7656 : 1 : tmp = gfc_class_data_get (dumdesc);
7657 : : else
7658 : 1 : tmp = build_fold_indirect_ref_loc (input_location, dumdesc);
7659 : 2 : tmp = gfc_conv_descriptor_data_get (tmp);
7660 : 2 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
7661 : : tmp, tmpdesc);
7662 : 2 : stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
7663 : : build_empty_stmt (input_location));
7664 : :
7665 : 2 : if (optional_arg)
7666 : : {
7667 : 0 : tmp = gfc_conv_expr_present (sym);
7668 : 0 : stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
7669 : : build_empty_stmt (input_location));
7670 : : }
7671 : : }
7672 : :
7673 : : /* We don't need to free any memory allocated by internal_pack as it will
7674 : : be freed at the end of the function by pop_context. */
7675 : 6555 : gfc_add_init_cleanup (block, stmtInit, stmtCleanup);
7676 : :
7677 : 6555 : input_location = loc;
7678 : : }
7679 : :
7680 : :
7681 : : /* Calculate the overall offset, including subreferences. */
7682 : : void
7683 : 58718 : gfc_get_dataptr_offset (stmtblock_t *block, tree parm, tree desc, tree offset,
7684 : : bool subref, gfc_expr *expr)
7685 : : {
7686 : 58718 : tree tmp;
7687 : 58718 : tree field;
7688 : 58718 : tree stride;
7689 : 58718 : tree index;
7690 : 58718 : gfc_ref *ref;
7691 : 58718 : gfc_se start;
7692 : 58718 : int n;
7693 : :
7694 : : /* If offset is NULL and this is not a subreferenced array, there is
7695 : : nothing to do. */
7696 : 58718 : if (offset == NULL_TREE)
7697 : : {
7698 : 1023 : if (subref)
7699 : 115 : offset = gfc_index_zero_node;
7700 : : else
7701 : 908 : return;
7702 : : }
7703 : :
7704 : 57810 : tmp = build_array_ref (desc, offset, NULL, NULL);
7705 : :
7706 : : /* Offset the data pointer for pointer assignments from arrays with
7707 : : subreferences; e.g. my_integer => my_type(:)%integer_component. */
7708 : 57810 : if (subref)
7709 : : {
7710 : : /* Go past the array reference. */
7711 : 771 : for (ref = expr->ref; ref; ref = ref->next)
7712 : 771 : if (ref->type == REF_ARRAY &&
7713 : 684 : ref->u.ar.type != AR_ELEMENT)
7714 : : {
7715 : 660 : ref = ref->next;
7716 : 660 : break;
7717 : : }
7718 : :
7719 : : /* Calculate the offset for each subsequent subreference. */
7720 : 1292 : for (; ref; ref = ref->next)
7721 : : {
7722 : 632 : switch (ref->type)
7723 : : {
7724 : 264 : case REF_COMPONENT:
7725 : 264 : field = ref->u.c.component->backend_decl;
7726 : 264 : gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
7727 : 528 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
7728 : 264 : TREE_TYPE (field),
7729 : : tmp, field, NULL_TREE);
7730 : 264 : break;
7731 : :
7732 : 320 : case REF_SUBSTRING:
7733 : 320 : gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
7734 : 320 : gfc_init_se (&start, NULL);
7735 : 320 : gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
7736 : 320 : gfc_add_block_to_block (block, &start.pre);
7737 : 320 : tmp = gfc_build_array_ref (tmp, start.expr, NULL);
7738 : 320 : break;
7739 : :
7740 : 24 : case REF_ARRAY:
7741 : 24 : gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE
7742 : : && ref->u.ar.type == AR_ELEMENT);
7743 : :
7744 : : /* TODO - Add bounds checking. */
7745 : 24 : stride = gfc_index_one_node;
7746 : 24 : index = gfc_index_zero_node;
7747 : 55 : for (n = 0; n < ref->u.ar.dimen; n++)
7748 : : {
7749 : 31 : tree itmp;
7750 : 31 : tree jtmp;
7751 : :
7752 : : /* Update the index. */
7753 : 31 : gfc_init_se (&start, NULL);
7754 : 31 : gfc_conv_expr_type (&start, ref->u.ar.start[n], gfc_array_index_type);
7755 : 31 : itmp = gfc_evaluate_now (start.expr, block);
7756 : 31 : gfc_init_se (&start, NULL);
7757 : 31 : gfc_conv_expr_type (&start, ref->u.ar.as->lower[n], gfc_array_index_type);
7758 : 31 : jtmp = gfc_evaluate_now (start.expr, block);
7759 : 31 : itmp = fold_build2_loc (input_location, MINUS_EXPR,
7760 : : gfc_array_index_type, itmp, jtmp);
7761 : 31 : itmp = fold_build2_loc (input_location, MULT_EXPR,
7762 : : gfc_array_index_type, itmp, stride);
7763 : 31 : index = fold_build2_loc (input_location, PLUS_EXPR,
7764 : : gfc_array_index_type, itmp, index);
7765 : 31 : index = gfc_evaluate_now (index, block);
7766 : :
7767 : : /* Update the stride. */
7768 : 31 : gfc_init_se (&start, NULL);
7769 : 31 : gfc_conv_expr_type (&start, ref->u.ar.as->upper[n], gfc_array_index_type);
7770 : 31 : itmp = fold_build2_loc (input_location, MINUS_EXPR,
7771 : : gfc_array_index_type, start.expr,
7772 : : jtmp);
7773 : 31 : itmp = fold_build2_loc (input_location, PLUS_EXPR,
7774 : : gfc_array_index_type,
7775 : : gfc_index_one_node, itmp);
7776 : 31 : stride = fold_build2_loc (input_location, MULT_EXPR,
7777 : : gfc_array_index_type, stride, itmp);
7778 : 31 : stride = gfc_evaluate_now (stride, block);
7779 : : }
7780 : :
7781 : : /* Apply the index to obtain the array element. */
7782 : 24 : tmp = gfc_build_array_ref (tmp, index, NULL);
7783 : 24 : break;
7784 : :
7785 : 24 : case REF_INQUIRY:
7786 : 24 : switch (ref->u.i)
7787 : : {
7788 : 18 : case INQUIRY_RE:
7789 : 36 : tmp = fold_build1_loc (input_location, REALPART_EXPR,
7790 : 18 : TREE_TYPE (TREE_TYPE (tmp)), tmp);
7791 : 18 : break;
7792 : :
7793 : 6 : case INQUIRY_IM:
7794 : 12 : tmp = fold_build1_loc (input_location, IMAGPART_EXPR,
7795 : 6 : TREE_TYPE (TREE_TYPE (tmp)), tmp);
7796 : 6 : break;
7797 : :
7798 : : default:
7799 : : break;
7800 : : }
7801 : : break;
7802 : :
7803 : 0 : default:
7804 : 0 : gcc_unreachable ();
7805 : 632 : break;
7806 : : }
7807 : : }
7808 : : }
7809 : :
7810 : : /* Set the target data pointer. */
7811 : 57810 : offset = gfc_build_addr_expr (gfc_array_dataptr_type (desc), tmp);
7812 : :
7813 : : /* Check for optional dummy argument being present. Arguments of BIND(C)
7814 : : procedures are excepted here since they are handled differently. */
7815 : 57810 : if (expr->expr_type == EXPR_VARIABLE
7816 : 51151 : && expr->symtree->n.sym->attr.dummy
7817 : 6087 : && expr->symtree->n.sym->attr.optional
7818 : 58801 : && !is_CFI_desc (NULL, expr))
7819 : 1622 : offset = build3_loc (input_location, COND_EXPR, TREE_TYPE (offset),
7820 : 811 : gfc_conv_expr_present (expr->symtree->n.sym), offset,
7821 : 811 : fold_convert (TREE_TYPE (offset), gfc_index_zero_node));
7822 : :
7823 : 57810 : gfc_conv_descriptor_data_set (block, parm, offset);
7824 : : }
7825 : :
7826 : :
7827 : : /* gfc_conv_expr_descriptor needs the string length an expression
7828 : : so that the size of the temporary can be obtained. This is done
7829 : : by adding up the string lengths of all the elements in the
7830 : : expression. Function with non-constant expressions have their
7831 : : string lengths mapped onto the actual arguments using the
7832 : : interface mapping machinery in trans-expr.cc. */
7833 : : static void
7834 : 1509 : get_array_charlen (gfc_expr *expr, gfc_se *se)
7835 : : {
7836 : 1509 : gfc_interface_mapping mapping;
7837 : 1509 : gfc_formal_arglist *formal;
7838 : 1509 : gfc_actual_arglist *arg;
7839 : 1509 : gfc_se tse;
7840 : 1509 : gfc_expr *e;
7841 : :
7842 : 1509 : if (expr->ts.u.cl->length
7843 : 1509 : && gfc_is_constant_expr (expr->ts.u.cl->length))
7844 : : {
7845 : 1165 : if (!expr->ts.u.cl->backend_decl)
7846 : 447 : gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
7847 : 1297 : return;
7848 : : }
7849 : :
7850 : 344 : switch (expr->expr_type)
7851 : : {
7852 : 130 : case EXPR_ARRAY:
7853 : :
7854 : : /* This is somewhat brutal. The expression for the first
7855 : : element of the array is evaluated and assigned to a
7856 : : new string length for the original expression. */
7857 : 130 : e = gfc_constructor_first (expr->value.constructor)->expr;
7858 : :
7859 : 130 : gfc_init_se (&tse, NULL);
7860 : :
7861 : : /* Avoid evaluating trailing array references since all we need is
7862 : : the string length. */
7863 : 130 : if (e->rank)
7864 : 38 : tse.descriptor_only = 1;
7865 : 130 : if (e->rank && e->expr_type != EXPR_VARIABLE)
7866 : 1 : gfc_conv_expr_descriptor (&tse, e);
7867 : : else
7868 : 129 : gfc_conv_expr (&tse, e);
7869 : :
7870 : 130 : gfc_add_block_to_block (&se->pre, &tse.pre);
7871 : 130 : gfc_add_block_to_block (&se->post, &tse.post);
7872 : :
7873 : 130 : if (!expr->ts.u.cl->backend_decl || !VAR_P (expr->ts.u.cl->backend_decl))
7874 : : {
7875 : 87 : expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7876 : 87 : expr->ts.u.cl->backend_decl =
7877 : 87 : gfc_create_var (gfc_charlen_type_node, "sln");
7878 : : }
7879 : :
7880 : 130 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
7881 : : tse.string_length);
7882 : :
7883 : : /* Make sure that deferred length components point to the hidden
7884 : : string_length component. */
7885 : 130 : if (TREE_CODE (tse.expr) == COMPONENT_REF
7886 : 25 : && TREE_CODE (tse.string_length) == COMPONENT_REF
7887 : 149 : && TREE_OPERAND (tse.expr, 0) == TREE_OPERAND (tse.string_length, 0))
7888 : 19 : e->ts.u.cl->backend_decl = expr->ts.u.cl->backend_decl;
7889 : :
7890 : : return;
7891 : :
7892 : 90 : case EXPR_OP:
7893 : 90 : get_array_charlen (expr->value.op.op1, se);
7894 : :
7895 : : /* For parentheses the expression ts.u.cl should be identical. */
7896 : 90 : if (expr->value.op.op == INTRINSIC_PARENTHESES)
7897 : : {
7898 : 2 : if (expr->value.op.op1->ts.u.cl != expr->ts.u.cl)
7899 : 2 : expr->ts.u.cl->backend_decl
7900 : 2 : = expr->value.op.op1->ts.u.cl->backend_decl;
7901 : 2 : return;
7902 : : }
7903 : :
7904 : 176 : expr->ts.u.cl->backend_decl =
7905 : 88 : gfc_create_var (gfc_charlen_type_node, "sln");
7906 : :
7907 : 88 : if (expr->value.op.op2)
7908 : : {
7909 : 88 : get_array_charlen (expr->value.op.op2, se);
7910 : :
7911 : 88 : gcc_assert (expr->value.op.op == INTRINSIC_CONCAT);
7912 : :
7913 : : /* Add the string lengths and assign them to the expression
7914 : : string length backend declaration. */
7915 : 88 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
7916 : : fold_build2_loc (input_location, PLUS_EXPR,
7917 : : gfc_charlen_type_node,
7918 : 88 : expr->value.op.op1->ts.u.cl->backend_decl,
7919 : 88 : expr->value.op.op2->ts.u.cl->backend_decl));
7920 : : }
7921 : : else
7922 : 0 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
7923 : 0 : expr->value.op.op1->ts.u.cl->backend_decl);
7924 : : break;
7925 : :
7926 : 43 : case EXPR_FUNCTION:
7927 : 43 : if (expr->value.function.esym == NULL
7928 : 37 : || expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7929 : : {
7930 : 6 : gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
7931 : 6 : break;
7932 : : }
7933 : :
7934 : : /* Map expressions involving the dummy arguments onto the actual
7935 : : argument expressions. */
7936 : 37 : gfc_init_interface_mapping (&mapping);
7937 : 37 : formal = gfc_sym_get_dummy_args (expr->symtree->n.sym);
7938 : 37 : arg = expr->value.function.actual;
7939 : :
7940 : : /* Set se = NULL in the calls to the interface mapping, to suppress any
7941 : : backend stuff. */
7942 : 113 : for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
7943 : : {
7944 : 38 : if (!arg->expr)
7945 : 0 : continue;
7946 : 38 : if (formal->sym)
7947 : 38 : gfc_add_interface_mapping (&mapping, formal->sym, NULL, arg->expr);
7948 : : }
7949 : :
7950 : 37 : gfc_init_se (&tse, NULL);
7951 : :
7952 : : /* Build the expression for the character length and convert it. */
7953 : 37 : gfc_apply_interface_mapping (&mapping, &tse, expr->ts.u.cl->length);
7954 : :
7955 : 37 : gfc_add_block_to_block (&se->pre, &tse.pre);
7956 : 37 : gfc_add_block_to_block (&se->post, &tse.post);
7957 : 37 : tse.expr = fold_convert (gfc_charlen_type_node, tse.expr);
7958 : 74 : tse.expr = fold_build2_loc (input_location, MAX_EXPR,
7959 : 37 : TREE_TYPE (tse.expr), tse.expr,
7960 : 37 : build_zero_cst (TREE_TYPE (tse.expr)));
7961 : 37 : expr->ts.u.cl->backend_decl = tse.expr;
7962 : 37 : gfc_free_interface_mapping (&mapping);
7963 : 37 : break;
7964 : :
7965 : 81 : default:
7966 : 81 : gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
7967 : 81 : break;
7968 : : }
7969 : : }
7970 : :
7971 : :
7972 : : /* Helper function to check dimensions. */
7973 : : static bool
7974 : 144 : transposed_dims (gfc_ss *ss)
7975 : : {
7976 : 144 : int n;
7977 : :
7978 : 167926 : for (n = 0; n < ss->dimen; n++)
7979 : 86583 : if (ss->dim[n] != n)
7980 : : return true;
7981 : : return false;
7982 : : }
7983 : :
7984 : :
7985 : : /* Convert the last ref of a scalar coarray from an AR_ELEMENT to an
7986 : : AR_FULL, suitable for the scalarizer. */
7987 : :
7988 : : static gfc_ss *
7989 : 1170 : walk_coarray (gfc_expr *e)
7990 : : {
7991 : 1170 : gfc_ss *ss;
7992 : :
7993 : 1170 : ss = gfc_walk_expr (e);
7994 : :
7995 : : /* Fix scalar coarray. */
7996 : 1170 : if (ss == gfc_ss_terminator)
7997 : : {
7998 : 251 : gfc_ref *ref;
7999 : :
8000 : 251 : ref = e->ref;
8001 : 368 : while (ref)
8002 : : {
8003 : 368 : if (ref->type == REF_ARRAY
8004 : 251 : && ref->u.ar.codimen > 0)
8005 : : break;
8006 : :
8007 : 117 : ref = ref->next;
8008 : : }
8009 : :
8010 : 251 : gcc_assert (ref != NULL);
8011 : 251 : if (ref->u.ar.type == AR_ELEMENT)
8012 : 237 : ref->u.ar.type = AR_SECTION;
8013 : 251 : ss = gfc_reverse_ss (gfc_walk_array_ref (ss, e, ref, false));
8014 : : }
8015 : :
8016 : 1170 : return ss;
8017 : : }
8018 : :
8019 : : gfc_array_spec *
8020 : 1774 : get_coarray_as (const gfc_expr *e)
8021 : : {
8022 : 1774 : gfc_array_spec *as;
8023 : 1774 : gfc_symbol *sym = e->symtree->n.sym;
8024 : 1774 : gfc_component *comp;
8025 : :
8026 : 1774 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.codimension)
8027 : 544 : as = CLASS_DATA (sym)->as;
8028 : 1230 : else if (sym->attr.codimension)
8029 : 1184 : as = sym->as;
8030 : : else
8031 : : as = nullptr;
8032 : :
8033 : 4192 : for (gfc_ref *ref = e->ref; ref; ref = ref->next)
8034 : : {
8035 : 2418 : switch (ref->type)
8036 : : {
8037 : 644 : case REF_COMPONENT:
8038 : 644 : comp = ref->u.c.component;
8039 : 644 : if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.codimension)
8040 : 18 : as = CLASS_DATA (comp)->as;
8041 : 626 : else if (comp->ts.type != BT_CLASS && comp->attr.codimension)
8042 : 590 : as = comp->as;
8043 : : break;
8044 : :
8045 : : case REF_ARRAY:
8046 : : case REF_SUBSTRING:
8047 : : case REF_INQUIRY:
8048 : : break;
8049 : : }
8050 : : }
8051 : :
8052 : 1774 : return as;
8053 : : }
8054 : :
8055 : : bool
8056 : 136858 : is_explicit_coarray (gfc_expr *expr)
8057 : : {
8058 : 136858 : if (!gfc_is_coarray (expr))
8059 : : return false;
8060 : :
8061 : 1774 : gfc_array_spec *cas = get_coarray_as (expr);
8062 : 1774 : return cas && cas->cotype == AS_EXPLICIT;
8063 : : }
8064 : :
8065 : : /* Convert an array for passing as an actual argument. Expressions and
8066 : : vector subscripts are evaluated and stored in a temporary, which is then
8067 : : passed. For whole arrays the descriptor is passed. For array sections
8068 : : a modified copy of the descriptor is passed, but using the original data.
8069 : :
8070 : : This function is also used for array pointer assignments, and there
8071 : : are three cases:
8072 : :
8073 : : - se->want_pointer && !se->direct_byref
8074 : : EXPR is an actual argument. On exit, se->expr contains a
8075 : : pointer to the array descriptor.
8076 : :
8077 : : - !se->want_pointer && !se->direct_byref
8078 : : EXPR is an actual argument to an intrinsic function or the
8079 : : left-hand side of a pointer assignment. On exit, se->expr
8080 : : contains the descriptor for EXPR.
8081 : :
8082 : : - !se->want_pointer && se->direct_byref
8083 : : EXPR is the right-hand side of a pointer assignment and
8084 : : se->expr is the descriptor for the previously-evaluated
8085 : : left-hand side. The function creates an assignment from
8086 : : EXPR to se->expr.
8087 : :
8088 : :
8089 : : The se->force_tmp flag disables the non-copying descriptor optimization
8090 : : that is used for transpose. It may be used in cases where there is an
8091 : : alias between the transpose argument and another argument in the same
8092 : : function call. */
8093 : :
8094 : : void
8095 : 152697 : gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr)
8096 : : {
8097 : 152697 : gfc_ss *ss;
8098 : 152697 : gfc_ss_type ss_type;
8099 : 152697 : gfc_ss_info *ss_info;
8100 : 152697 : gfc_loopinfo loop;
8101 : 152697 : gfc_array_info *info;
8102 : 152697 : int need_tmp;
8103 : 152697 : int n;
8104 : 152697 : tree tmp;
8105 : 152697 : tree desc;
8106 : 152697 : stmtblock_t block;
8107 : 152697 : tree start;
8108 : 152697 : int full;
8109 : 152697 : bool subref_array_target = false;
8110 : 152697 : bool deferred_array_component = false;
8111 : 152697 : bool substr = false;
8112 : 152697 : gfc_expr *arg, *ss_expr;
8113 : :
8114 : 152697 : if (se->want_coarray || expr->rank == 0)
8115 : 1170 : ss = walk_coarray (expr);
8116 : : else
8117 : 151527 : ss = gfc_walk_expr (expr);
8118 : :
8119 : 152697 : gcc_assert (ss != NULL);
8120 : 152697 : gcc_assert (ss != gfc_ss_terminator);
8121 : :
8122 : 152697 : ss_info = ss->info;
8123 : 152697 : ss_type = ss_info->type;
8124 : 152697 : ss_expr = ss_info->expr;
8125 : :
8126 : : /* Special case: TRANSPOSE which needs no temporary. */
8127 : 157881 : while (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym
8128 : 157615 : && (arg = gfc_get_noncopying_intrinsic_argument (expr)) != NULL)
8129 : : {
8130 : : /* This is a call to transpose which has already been handled by the
8131 : : scalarizer, so that we just need to get its argument's descriptor. */
8132 : 408 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
8133 : 408 : expr = expr->value.function.actual->expr;
8134 : : }
8135 : :
8136 : 152697 : if (!se->direct_byref)
8137 : 293734 : se->unlimited_polymorphic = UNLIMITED_POLY (expr);
8138 : :
8139 : : /* Special case things we know we can pass easily. */
8140 : 152697 : switch (expr->expr_type)
8141 : : {
8142 : 137032 : case EXPR_VARIABLE:
8143 : : /* If we have a linear array section, we can pass it directly.
8144 : : Otherwise we need to copy it into a temporary. */
8145 : :
8146 : 137032 : gcc_assert (ss_type == GFC_SS_SECTION);
8147 : 137032 : gcc_assert (ss_expr == expr);
8148 : 137032 : info = &ss_info->data.array;
8149 : :
8150 : : /* Get the descriptor for the array. */
8151 : 137032 : gfc_conv_ss_descriptor (&se->pre, ss, 0);
8152 : 137032 : desc = info->descriptor;
8153 : :
8154 : : /* The charlen backend decl for deferred character components cannot
8155 : : be used because it is fixed at zero. Instead, the hidden string
8156 : : length component is used. */
8157 : 137032 : if (expr->ts.type == BT_CHARACTER
8158 : 19910 : && expr->ts.deferred
8159 : 2770 : && TREE_CODE (desc) == COMPONENT_REF)
8160 : 137032 : deferred_array_component = true;
8161 : :
8162 : 137032 : substr = info->ref && info->ref->next
8163 : 137742 : && info->ref->next->type == REF_SUBSTRING;
8164 : :
8165 : 137032 : subref_array_target = (is_subref_array (expr)
8166 : 137032 : && (se->direct_byref
8167 : 2274 : || expr->ts.type == BT_CHARACTER));
8168 : 137032 : need_tmp = (gfc_ref_needs_temporary_p (expr->ref)
8169 : 137032 : && !subref_array_target);
8170 : :
8171 : 137032 : if (se->force_tmp)
8172 : : need_tmp = 1;
8173 : 136962 : else if (se->force_no_tmp)
8174 : : need_tmp = 0;
8175 : :
8176 : 130819 : if (need_tmp)
8177 : : full = 0;
8178 : 136858 : else if (is_explicit_coarray (expr))
8179 : : full = 0;
8180 : 136222 : else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
8181 : : {
8182 : : /* Create a new descriptor if the array doesn't have one. */
8183 : : full = 0;
8184 : : }
8185 : 87408 : else if (info->ref->u.ar.type == AR_FULL || se->descriptor_only)
8186 : : full = 1;
8187 : 7861 : else if (se->direct_byref)
8188 : : full = 0;
8189 : 7529 : else if (info->ref->u.ar.dimen == 0 && !info->ref->next)
8190 : : full = 1;
8191 : 7423 : else if (info->ref->u.ar.type == AR_SECTION && se->want_pointer)
8192 : : full = 0;
8193 : : else
8194 : 3552 : full = gfc_full_array_ref_p (info->ref, NULL);
8195 : :
8196 : 163098 : if (full && !transposed_dims (ss))
8197 : : {
8198 : 79836 : if (se->direct_byref && !se->byref_noassign)
8199 : : {
8200 : 1011 : struct lang_type *lhs_ls
8201 : 1011 : = TYPE_LANG_SPECIFIC (TREE_TYPE (se->expr)),
8202 : 1011 : *rhs_ls = TYPE_LANG_SPECIFIC (TREE_TYPE (desc));
8203 : : /* When only the array_kind differs, do a view_convert. */
8204 : 1369 : tmp = lhs_ls && rhs_ls && lhs_ls->rank == rhs_ls->rank
8205 : 1011 : && lhs_ls->akind != rhs_ls->akind
8206 : 1369 : ? build1 (VIEW_CONVERT_EXPR, TREE_TYPE (se->expr), desc)
8207 : : : desc;
8208 : : /* Copy the descriptor for pointer assignments. */
8209 : 1011 : gfc_add_modify (&se->pre, se->expr, tmp);
8210 : :
8211 : : /* Add any offsets from subreferences. */
8212 : 1011 : gfc_get_dataptr_offset (&se->pre, se->expr, desc, NULL_TREE,
8213 : : subref_array_target, expr);
8214 : :
8215 : : /* ....and set the span field. */
8216 : 1011 : if (ss_info->expr->ts.type == BT_CHARACTER)
8217 : 141 : tmp = gfc_conv_descriptor_span_get (desc);
8218 : : else
8219 : 870 : tmp = gfc_get_array_span (desc, expr);
8220 : 1011 : gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
8221 : 1011 : }
8222 : 78825 : else if (se->want_pointer)
8223 : : {
8224 : : /* We pass full arrays directly. This means that pointers and
8225 : : allocatable arrays should also work. */
8226 : 13457 : se->expr = gfc_build_addr_expr (NULL_TREE, desc);
8227 : : }
8228 : : else
8229 : : {
8230 : 65368 : se->expr = desc;
8231 : : }
8232 : :
8233 : 79836 : if (expr->ts.type == BT_CHARACTER && !deferred_array_component)
8234 : 8279 : se->string_length = gfc_get_expr_charlen (expr);
8235 : : /* The ss_info string length is returned set to the value of the
8236 : : hidden string length component. */
8237 : 71294 : else if (deferred_array_component)
8238 : 263 : se->string_length = ss_info->string_length;
8239 : :
8240 : 79836 : se->class_container = ss_info->class_container;
8241 : :
8242 : 79836 : gfc_free_ss_chain (ss);
8243 : 159798 : return;
8244 : : }
8245 : : break;
8246 : :
8247 : 4776 : case EXPR_FUNCTION:
8248 : : /* A transformational function return value will be a temporary
8249 : : array descriptor. We still need to go through the scalarizer
8250 : : to create the descriptor. Elemental functions are handled as
8251 : : arbitrary expressions, i.e. copy to a temporary. */
8252 : :
8253 : 4776 : if (se->direct_byref)
8254 : : {
8255 : 126 : gcc_assert (ss_type == GFC_SS_FUNCTION && ss_expr == expr);
8256 : :
8257 : : /* For pointer assignments pass the descriptor directly. */
8258 : 126 : if (se->ss == NULL)
8259 : 126 : se->ss = ss;
8260 : : else
8261 : 0 : gcc_assert (se->ss == ss);
8262 : :
8263 : 126 : if (!is_pointer_array (se->expr))
8264 : : {
8265 : 120 : tmp = gfc_get_element_type (TREE_TYPE (se->expr));
8266 : 120 : tmp = fold_convert (gfc_array_index_type,
8267 : : size_in_bytes (tmp));
8268 : 120 : gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
8269 : : }
8270 : :
8271 : 126 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
8272 : 126 : gfc_conv_expr (se, expr);
8273 : :
8274 : 126 : gfc_free_ss_chain (ss);
8275 : 126 : return;
8276 : : }
8277 : :
8278 : 4650 : if (ss_expr != expr || ss_type != GFC_SS_FUNCTION)
8279 : : {
8280 : 3268 : if (ss_expr != expr)
8281 : : /* Elemental function. */
8282 : 2543 : gcc_assert ((expr->value.function.esym != NULL
8283 : : && expr->value.function.esym->attr.elemental)
8284 : : || (expr->value.function.isym != NULL
8285 : : && expr->value.function.isym->elemental)
8286 : : || (gfc_expr_attr (expr).proc_pointer
8287 : : && gfc_expr_attr (expr).elemental)
8288 : : || gfc_inline_intrinsic_function_p (expr));
8289 : :
8290 : 3268 : need_tmp = 1;
8291 : 3268 : if (expr->ts.type == BT_CHARACTER
8292 : 35 : && expr->ts.u.cl->length
8293 : 29 : && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8294 : 13 : get_array_charlen (expr, se);
8295 : :
8296 : : info = NULL;
8297 : : }
8298 : : else
8299 : : {
8300 : : /* Transformational function. */
8301 : 1382 : info = &ss_info->data.array;
8302 : 1382 : need_tmp = 0;
8303 : : }
8304 : : break;
8305 : :
8306 : 10188 : case EXPR_ARRAY:
8307 : : /* Constant array constructors don't need a temporary. */
8308 : 10188 : if (ss_type == GFC_SS_CONSTRUCTOR
8309 : 10188 : && expr->ts.type != BT_CHARACTER
8310 : 19189 : && gfc_constant_array_constructor_p (expr->value.constructor))
8311 : : {
8312 : 6646 : need_tmp = 0;
8313 : 6646 : info = &ss_info->data.array;
8314 : : }
8315 : : else
8316 : : {
8317 : : need_tmp = 1;
8318 : : info = NULL;
8319 : : }
8320 : : break;
8321 : :
8322 : : default:
8323 : : /* Something complicated. Copy it into a temporary. */
8324 : : need_tmp = 1;
8325 : : info = NULL;
8326 : : break;
8327 : : }
8328 : :
8329 : : /* If we are creating a temporary, we don't need to bother about aliases
8330 : : anymore. */
8331 : 65224 : if (need_tmp)
8332 : 7685 : se->force_tmp = 0;
8333 : :
8334 : 72735 : gfc_init_loopinfo (&loop);
8335 : :
8336 : : /* Associate the SS with the loop. */
8337 : 72735 : gfc_add_ss_to_loop (&loop, ss);
8338 : :
8339 : : /* Tell the scalarizer not to bother creating loop variables, etc. */
8340 : 72735 : if (!need_tmp)
8341 : 65050 : loop.array_parameter = 1;
8342 : : else
8343 : : /* The right-hand side of a pointer assignment mustn't use a temporary. */
8344 : 7685 : gcc_assert (!se->direct_byref);
8345 : :
8346 : : /* Do we need bounds checking or not? */
8347 : 72735 : ss->no_bounds_check = expr->no_bounds_check;
8348 : :
8349 : : /* Setup the scalarizing loops and bounds. */
8350 : 72735 : gfc_conv_ss_startstride (&loop);
8351 : :
8352 : : /* Add bounds-checking for elemental dimensions. */
8353 : 72735 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !expr->no_bounds_check)
8354 : 6667 : array_bound_check_elemental (se, ss, expr);
8355 : :
8356 : 72735 : if (need_tmp)
8357 : : {
8358 : 7685 : if (expr->ts.type == BT_CHARACTER
8359 : 1398 : && (!expr->ts.u.cl->backend_decl || expr->expr_type == EXPR_ARRAY))
8360 : 1318 : get_array_charlen (expr, se);
8361 : :
8362 : : /* Tell the scalarizer to make a temporary. */
8363 : 7685 : loop.temp_ss = gfc_get_temp_ss (gfc_typenode_for_spec (&expr->ts),
8364 : 7685 : ((expr->ts.type == BT_CHARACTER)
8365 : 1398 : ? expr->ts.u.cl->backend_decl
8366 : : : NULL),
8367 : : loop.dimen);
8368 : :
8369 : 7685 : se->string_length = loop.temp_ss->info->string_length;
8370 : 7685 : gcc_assert (loop.temp_ss->dimen == loop.dimen);
8371 : 7685 : gfc_add_ss_to_loop (&loop, loop.temp_ss);
8372 : : }
8373 : :
8374 : 72735 : gfc_conv_loop_setup (&loop, & expr->where);
8375 : :
8376 : 72735 : if (need_tmp)
8377 : : {
8378 : : /* Copy into a temporary and pass that. We don't need to copy the data
8379 : : back because expressions and vector subscripts must be INTENT_IN. */
8380 : : /* TODO: Optimize passing function return values. */
8381 : 7685 : gfc_se lse;
8382 : 7685 : gfc_se rse;
8383 : 7685 : bool deep_copy;
8384 : :
8385 : : /* Start the copying loops. */
8386 : 7685 : gfc_mark_ss_chain_used (loop.temp_ss, 1);
8387 : 7685 : gfc_mark_ss_chain_used (ss, 1);
8388 : 7685 : gfc_start_scalarized_body (&loop, &block);
8389 : :
8390 : : /* Copy each data element. */
8391 : 7685 : gfc_init_se (&lse, NULL);
8392 : 7685 : gfc_copy_loopinfo_to_se (&lse, &loop);
8393 : 7685 : gfc_init_se (&rse, NULL);
8394 : 7685 : gfc_copy_loopinfo_to_se (&rse, &loop);
8395 : :
8396 : 7685 : lse.ss = loop.temp_ss;
8397 : 7685 : rse.ss = ss;
8398 : :
8399 : 7685 : gfc_conv_tmp_array_ref (&lse);
8400 : 7685 : if (expr->ts.type == BT_CHARACTER)
8401 : : {
8402 : 1398 : gfc_conv_expr (&rse, expr);
8403 : 1398 : if (POINTER_TYPE_P (TREE_TYPE (rse.expr)))
8404 : 1099 : rse.expr = build_fold_indirect_ref_loc (input_location,
8405 : : rse.expr);
8406 : : }
8407 : : else
8408 : 6287 : gfc_conv_expr_val (&rse, expr);
8409 : :
8410 : 7685 : gfc_add_block_to_block (&block, &rse.pre);
8411 : 7685 : gfc_add_block_to_block (&block, &lse.pre);
8412 : :
8413 : 7685 : lse.string_length = rse.string_length;
8414 : :
8415 : 15370 : deep_copy = !se->data_not_needed
8416 : 7685 : && (expr->expr_type == EXPR_VARIABLE
8417 : 7258 : || expr->expr_type == EXPR_ARRAY);
8418 : 7685 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
8419 : : deep_copy, false);
8420 : 7685 : gfc_add_expr_to_block (&block, tmp);
8421 : :
8422 : : /* Finish the copying loops. */
8423 : 7685 : gfc_trans_scalarizing_loops (&loop, &block);
8424 : :
8425 : 7685 : desc = loop.temp_ss->info->data.array.descriptor;
8426 : : }
8427 : 66432 : else if (expr->expr_type == EXPR_FUNCTION && !transposed_dims (ss))
8428 : : {
8429 : 1369 : desc = info->descriptor;
8430 : 1369 : se->string_length = ss_info->string_length;
8431 : : }
8432 : : else
8433 : : {
8434 : : /* We pass sections without copying to a temporary. Make a new
8435 : : descriptor and point it at the section we want. The loop variable
8436 : : limits will be the limits of the section.
8437 : : A function may decide to repack the array to speed up access, but
8438 : : we're not bothered about that here. */
8439 : 63681 : int dim, ndim, codim;
8440 : 63681 : tree parm;
8441 : 63681 : tree parmtype;
8442 : 63681 : tree dtype;
8443 : 63681 : tree stride;
8444 : 63681 : tree from;
8445 : 63681 : tree to;
8446 : 63681 : tree base;
8447 : 63681 : tree offset;
8448 : :
8449 : 63681 : ndim = info->ref ? info->ref->u.ar.dimen : ss->dimen;
8450 : :
8451 : 63681 : if (se->want_coarray)
8452 : : {
8453 : 529 : gfc_array_ref *ar = &info->ref->u.ar;
8454 : :
8455 : 529 : codim = expr->corank;
8456 : 1083 : for (n = 0; n < codim - 1; n++)
8457 : : {
8458 : : /* Make sure we are not lost somehow. */
8459 : 554 : gcc_assert (ar->dimen_type[n + ndim] == DIMEN_THIS_IMAGE);
8460 : :
8461 : : /* Make sure the call to gfc_conv_section_startstride won't
8462 : : generate unnecessary code to calculate stride. */
8463 : 554 : gcc_assert (ar->stride[n + ndim] == NULL);
8464 : :
8465 : 554 : gfc_conv_section_startstride (&loop.pre, ss, n + ndim);
8466 : 554 : loop.from[n + loop.dimen] = info->start[n + ndim];
8467 : 554 : loop.to[n + loop.dimen] = info->end[n + ndim];
8468 : : }
8469 : :
8470 : 529 : gcc_assert (n == codim - 1);
8471 : 529 : evaluate_bound (&loop.pre, info->start, ar->start,
8472 : : info->descriptor, n + ndim, true,
8473 : 529 : ar->as->type == AS_DEFERRED);
8474 : 529 : loop.from[n + loop.dimen] = info->start[n + ndim];
8475 : : }
8476 : : else
8477 : : codim = 0;
8478 : :
8479 : : /* Set the string_length for a character array. */
8480 : 63681 : if (expr->ts.type == BT_CHARACTER)
8481 : : {
8482 : 11351 : if (deferred_array_component && !substr)
8483 : 37 : se->string_length = ss_info->string_length;
8484 : : else
8485 : 11314 : se->string_length = gfc_get_expr_charlen (expr);
8486 : :
8487 : 11351 : if (VAR_P (se->string_length)
8488 : 978 : && expr->ts.u.cl->backend_decl == se->string_length)
8489 : 972 : tmp = ss_info->string_length;
8490 : : else
8491 : : tmp = se->string_length;
8492 : :
8493 : 11351 : if (expr->ts.deferred && expr->ts.u.cl->backend_decl
8494 : 205 : && VAR_P (expr->ts.u.cl->backend_decl))
8495 : 144 : gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl, tmp);
8496 : : else
8497 : 11207 : expr->ts.u.cl->backend_decl = tmp;
8498 : : }
8499 : :
8500 : : /* If we have an array section, are assigning or passing an array
8501 : : section argument make sure that the lower bound is 1. References
8502 : : to the full array should otherwise keep the original bounds. */
8503 : 63681 : if (!info->ref || info->ref->u.ar.type != AR_FULL)
8504 : 81883 : for (dim = 0; dim < loop.dimen; dim++)
8505 : 49948 : if (!integer_onep (loop.from[dim]))
8506 : : {
8507 : 26624 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
8508 : : gfc_array_index_type, gfc_index_one_node,
8509 : : loop.from[dim]);
8510 : 26624 : loop.to[dim] = fold_build2_loc (input_location, PLUS_EXPR,
8511 : : gfc_array_index_type,
8512 : : loop.to[dim], tmp);
8513 : 26624 : loop.from[dim] = gfc_index_one_node;
8514 : : }
8515 : :
8516 : 63681 : desc = info->descriptor;
8517 : 63681 : if (se->direct_byref && !se->byref_noassign)
8518 : : {
8519 : : /* For pointer assignments we fill in the destination. */
8520 : 2594 : parm = se->expr;
8521 : 2594 : parmtype = TREE_TYPE (parm);
8522 : : }
8523 : : else
8524 : : {
8525 : : /* Otherwise make a new one. */
8526 : 61087 : if (expr->ts.type == BT_CHARACTER)
8527 : 10723 : parmtype = gfc_typenode_for_spec (&expr->ts);
8528 : : else
8529 : 50364 : parmtype = gfc_get_element_type (TREE_TYPE (desc));
8530 : :
8531 : 61087 : parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, codim,
8532 : : loop.from, loop.to, 0,
8533 : : GFC_ARRAY_UNKNOWN, false);
8534 : 61087 : parm = gfc_create_var (parmtype, "parm");
8535 : :
8536 : : /* When expression is a class object, then add the class' handle to
8537 : : the parm_decl. */
8538 : 61087 : if (expr->ts.type == BT_CLASS && expr->expr_type == EXPR_VARIABLE)
8539 : : {
8540 : 1122 : gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
8541 : 1122 : gfc_se classse;
8542 : :
8543 : : /* class_expr can be NULL, when no _class ref is in expr.
8544 : : We must not fix this here with a gfc_fix_class_ref (). */
8545 : 1122 : if (class_expr)
8546 : : {
8547 : 1112 : gfc_init_se (&classse, NULL);
8548 : 1112 : gfc_conv_expr (&classse, class_expr);
8549 : 1112 : gfc_free_expr (class_expr);
8550 : :
8551 : 1112 : gcc_assert (classse.pre.head == NULL_TREE
8552 : : && classse.post.head == NULL_TREE);
8553 : 1112 : gfc_allocate_lang_decl (parm);
8554 : 1112 : GFC_DECL_SAVED_DESCRIPTOR (parm) = classse.expr;
8555 : : }
8556 : : }
8557 : : }
8558 : :
8559 : 63681 : if (expr->ts.type == BT_CHARACTER
8560 : 63681 : && VAR_P (TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)))))
8561 : : {
8562 : 0 : tree elem_len = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)));
8563 : 0 : gfc_add_modify (&loop.pre, elem_len,
8564 : 0 : fold_convert (TREE_TYPE (elem_len),
8565 : : gfc_get_array_span (desc, expr)));
8566 : : }
8567 : :
8568 : : /* Set the span field. */
8569 : 63681 : tmp = NULL_TREE;
8570 : 63681 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
8571 : 7601 : tmp = gfc_conv_descriptor_span_get (desc);
8572 : : else
8573 : 56080 : tmp = gfc_get_array_span (desc, expr);
8574 : 63681 : if (tmp)
8575 : 63601 : gfc_conv_descriptor_span_set (&loop.pre, parm, tmp);
8576 : :
8577 : : /* The following can be somewhat confusing. We have two
8578 : : descriptors, a new one and the original array.
8579 : : {parm, parmtype, dim} refer to the new one.
8580 : : {desc, type, n, loop} refer to the original, which maybe
8581 : : a descriptorless array.
8582 : : The bounds of the scalarization are the bounds of the section.
8583 : : We don't have to worry about numeric overflows when calculating
8584 : : the offsets because all elements are within the array data. */
8585 : :
8586 : : /* Set the dtype. */
8587 : 63681 : tmp = gfc_conv_descriptor_dtype (parm);
8588 : 63681 : if (se->unlimited_polymorphic)
8589 : 565 : dtype = gfc_get_dtype (TREE_TYPE (desc), &loop.dimen);
8590 : 63116 : else if (expr->ts.type == BT_ASSUMED)
8591 : : {
8592 : 127 : tree tmp2 = desc;
8593 : 127 : if (DECL_LANG_SPECIFIC (tmp2) && GFC_DECL_SAVED_DESCRIPTOR (tmp2))
8594 : 127 : tmp2 = GFC_DECL_SAVED_DESCRIPTOR (tmp2);
8595 : 127 : if (POINTER_TYPE_P (TREE_TYPE (tmp2)))
8596 : 127 : tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
8597 : 127 : dtype = gfc_conv_descriptor_dtype (tmp2);
8598 : : }
8599 : : else
8600 : 62989 : dtype = gfc_get_dtype (parmtype);
8601 : 63681 : gfc_add_modify (&loop.pre, tmp, dtype);
8602 : :
8603 : : /* The 1st element in the section. */
8604 : 63681 : base = gfc_index_zero_node;
8605 : 63681 : if (expr->ts.type == BT_CHARACTER && expr->rank == 0 && codim)
8606 : 6 : base = gfc_index_one_node;
8607 : :
8608 : : /* The offset from the 1st element in the section. */
8609 : : offset = gfc_index_zero_node;
8610 : :
8611 : 164527 : for (n = 0; n < ndim; n++)
8612 : : {
8613 : 100846 : stride = gfc_conv_array_stride (desc, n);
8614 : :
8615 : : /* Work out the 1st element in the section. */
8616 : 100846 : if (info->ref
8617 : 93874 : && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
8618 : : {
8619 : 1142 : gcc_assert (info->subscript[n]
8620 : : && info->subscript[n]->info->type == GFC_SS_SCALAR);
8621 : 1142 : start = info->subscript[n]->info->data.scalar.value;
8622 : : }
8623 : : else
8624 : : {
8625 : : /* Evaluate and remember the start of the section. */
8626 : 99704 : start = info->start[n];
8627 : 99704 : stride = gfc_evaluate_now (stride, &loop.pre);
8628 : : }
8629 : :
8630 : 100846 : tmp = gfc_conv_array_lbound (desc, n);
8631 : 100846 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
8632 : : start, tmp);
8633 : 100846 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
8634 : : tmp, stride);
8635 : 100846 : base = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
8636 : : base, tmp);
8637 : :
8638 : 100846 : if (info->ref
8639 : 93874 : && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
8640 : : {
8641 : : /* For elemental dimensions, we only need the 1st
8642 : : element in the section. */
8643 : 1142 : continue;
8644 : : }
8645 : :
8646 : : /* Vector subscripts need copying and are handled elsewhere. */
8647 : 99704 : if (info->ref)
8648 : 92732 : gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
8649 : :
8650 : : /* look for the corresponding scalarizer dimension: dim. */
8651 : 150799 : for (dim = 0; dim < ndim; dim++)
8652 : 150799 : if (ss->dim[dim] == n)
8653 : : break;
8654 : :
8655 : : /* loop exited early: the DIM being looked for has been found. */
8656 : 99704 : gcc_assert (dim < ndim);
8657 : :
8658 : : /* Set the new lower bound. */
8659 : 99704 : from = loop.from[dim];
8660 : 99704 : to = loop.to[dim];
8661 : :
8662 : 99704 : gfc_conv_descriptor_lbound_set (&loop.pre, parm,
8663 : : gfc_rank_cst[dim], from);
8664 : :
8665 : : /* Set the new upper bound. */
8666 : 99704 : gfc_conv_descriptor_ubound_set (&loop.pre, parm,
8667 : : gfc_rank_cst[dim], to);
8668 : :
8669 : : /* Multiply the stride by the section stride to get the
8670 : : total stride. */
8671 : 99704 : stride = fold_build2_loc (input_location, MULT_EXPR,
8672 : : gfc_array_index_type,
8673 : : stride, info->stride[n]);
8674 : :
8675 : 99704 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8676 : 99704 : TREE_TYPE (offset), stride, from);
8677 : 99704 : offset = fold_build2_loc (input_location, MINUS_EXPR,
8678 : 99704 : TREE_TYPE (offset), offset, tmp);
8679 : :
8680 : : /* Store the new stride. */
8681 : 99704 : gfc_conv_descriptor_stride_set (&loop.pre, parm,
8682 : : gfc_rank_cst[dim], stride);
8683 : : }
8684 : :
8685 : 64764 : for (n = loop.dimen; n < loop.dimen + codim; n++)
8686 : : {
8687 : 1083 : from = loop.from[n];
8688 : 1083 : to = loop.to[n];
8689 : 1083 : gfc_conv_descriptor_lbound_set (&loop.pre, parm,
8690 : : gfc_rank_cst[n], from);
8691 : 1083 : if (n < loop.dimen + codim - 1)
8692 : 554 : gfc_conv_descriptor_ubound_set (&loop.pre, parm,
8693 : : gfc_rank_cst[n], to);
8694 : : }
8695 : :
8696 : 63681 : if (se->data_not_needed)
8697 : 5986 : gfc_conv_descriptor_data_set (&loop.pre, parm,
8698 : : gfc_index_zero_node);
8699 : : else
8700 : : /* Point the data pointer at the 1st element in the section. */
8701 : 57695 : gfc_get_dataptr_offset (&loop.pre, parm, desc, base,
8702 : : subref_array_target, expr);
8703 : :
8704 : 63681 : gfc_conv_descriptor_offset_set (&loop.pre, parm, offset);
8705 : :
8706 : 63681 : if (flag_coarray == GFC_FCOARRAY_LIB && expr->corank)
8707 : : {
8708 : 215 : tmp = INDIRECT_REF_P (desc) ? TREE_OPERAND (desc, 0) : desc;
8709 : 215 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8710 : : {
8711 : 12 : tmp = gfc_conv_descriptor_token (tmp);
8712 : : }
8713 : 203 : else if (DECL_P (tmp) && DECL_LANG_SPECIFIC (tmp)
8714 : 245 : && GFC_DECL_TOKEN (tmp) != NULL_TREE)
8715 : 34 : tmp = GFC_DECL_TOKEN (tmp);
8716 : : else
8717 : : {
8718 : 169 : tmp = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (tmp));
8719 : : }
8720 : :
8721 : 215 : gfc_add_modify (&loop.pre, gfc_conv_descriptor_token (parm), tmp);
8722 : : }
8723 : : desc = parm;
8724 : : }
8725 : :
8726 : : /* For class arrays add the class tree into the saved descriptor to
8727 : : enable getting of _vptr and the like. */
8728 : 72735 : if (expr->expr_type == EXPR_VARIABLE && VAR_P (desc)
8729 : 56305 : && IS_CLASS_ARRAY (expr->symtree->n.sym))
8730 : : {
8731 : 1097 : gfc_allocate_lang_decl (desc);
8732 : 1097 : GFC_DECL_SAVED_DESCRIPTOR (desc) =
8733 : 1097 : DECL_LANG_SPECIFIC (expr->symtree->n.sym->backend_decl) ?
8734 : 1011 : GFC_DECL_SAVED_DESCRIPTOR (expr->symtree->n.sym->backend_decl)
8735 : : : expr->symtree->n.sym->backend_decl;
8736 : : }
8737 : 71638 : else if (expr->expr_type == EXPR_ARRAY && VAR_P (desc)
8738 : 10188 : && IS_CLASS_ARRAY (expr))
8739 : : {
8740 : 12 : tree vtype;
8741 : 12 : gfc_allocate_lang_decl (desc);
8742 : 12 : tmp = gfc_create_var (expr->ts.u.derived->backend_decl, "class");
8743 : 12 : GFC_DECL_SAVED_DESCRIPTOR (desc) = tmp;
8744 : 12 : vtype = gfc_class_vptr_get (tmp);
8745 : 12 : gfc_add_modify (&se->pre, vtype,
8746 : 12 : gfc_build_addr_expr (TREE_TYPE (vtype),
8747 : 12 : gfc_find_vtab (&expr->ts)->backend_decl));
8748 : : }
8749 : 72735 : if (!se->direct_byref || se->byref_noassign)
8750 : : {
8751 : : /* Get a pointer to the new descriptor. */
8752 : 70141 : if (se->want_pointer)
8753 : 40306 : se->expr = gfc_build_addr_expr (NULL_TREE, desc);
8754 : : else
8755 : 29835 : se->expr = desc;
8756 : : }
8757 : :
8758 : 72735 : gfc_add_block_to_block (&se->pre, &loop.pre);
8759 : 72735 : gfc_add_block_to_block (&se->post, &loop.post);
8760 : :
8761 : : /* Cleanup the scalarizer. */
8762 : 72735 : gfc_cleanup_loop (&loop);
8763 : : }
8764 : :
8765 : :
8766 : : /* Calculate the array size (number of elements); if dim != NULL_TREE,
8767 : : return size for that dim (dim=0..rank-1; only for GFC_DESCRIPTOR_TYPE_P).
8768 : : If !expr && descriptor array, the rank is taken from the descriptor. */
8769 : : tree
8770 : 14724 : gfc_tree_array_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree dim)
8771 : : {
8772 : 14724 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
8773 : : {
8774 : 34 : gcc_assert (dim == NULL_TREE);
8775 : 34 : return GFC_TYPE_ARRAY_SIZE (TREE_TYPE (desc));
8776 : : }
8777 : 14690 : tree size, tmp, rank = NULL_TREE, cond = NULL_TREE;
8778 : 14690 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
8779 : 14690 : enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (TREE_TYPE (desc));
8780 : 14690 : if (expr == NULL || expr->rank < 0)
8781 : 3071 : rank = fold_convert (signed_char_type_node,
8782 : : gfc_conv_descriptor_rank (desc));
8783 : : else
8784 : 11619 : rank = build_int_cst (signed_char_type_node, expr->rank);
8785 : :
8786 : 14690 : if (dim || (expr && expr->rank == 1))
8787 : : {
8788 : 4375 : if (!dim)
8789 : 4375 : dim = gfc_index_zero_node;
8790 : 13037 : tree ubound = gfc_conv_descriptor_ubound_get (desc, dim);
8791 : 13037 : tree lbound = gfc_conv_descriptor_lbound_get (desc, dim);
8792 : :
8793 : 13037 : size = fold_build2_loc (input_location, MINUS_EXPR,
8794 : : gfc_array_index_type, ubound, lbound);
8795 : 13037 : size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8796 : : size, gfc_index_one_node);
8797 : : /* if (!allocatable && !pointer && assumed rank)
8798 : : size = (idx == rank && ubound[rank-1] == -1 ? -1 : size;
8799 : : else
8800 : : size = max (0, size); */
8801 : 13037 : size = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
8802 : : size, gfc_index_zero_node);
8803 : 13037 : if (akind == GFC_ARRAY_ASSUMED_RANK_CONT
8804 : 13037 : || akind == GFC_ARRAY_ASSUMED_RANK)
8805 : : {
8806 : 2384 : tmp = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
8807 : : rank, build_int_cst (signed_char_type_node, 1));
8808 : 2384 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8809 : : fold_convert (signed_char_type_node, dim),
8810 : : tmp);
8811 : 2384 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8812 : : gfc_conv_descriptor_ubound_get (desc, dim),
8813 : : build_int_cst (gfc_array_index_type, -1));
8814 : 2384 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
8815 : : cond, tmp);
8816 : 2384 : tmp = build_int_cst (gfc_array_index_type, -1);
8817 : 2384 : size = build3_loc (input_location, COND_EXPR, gfc_array_index_type,
8818 : : cond, tmp, size);
8819 : : }
8820 : 13037 : return size;
8821 : : }
8822 : :
8823 : : /* size = 1. */
8824 : 1653 : size = gfc_create_var (gfc_array_index_type, "size");
8825 : 1653 : gfc_add_modify (block, size, build_int_cst (TREE_TYPE (size), 1));
8826 : 1653 : tree extent = gfc_create_var (gfc_array_index_type, "extent");
8827 : :
8828 : 1653 : stmtblock_t cond_block, loop_body;
8829 : 1653 : gfc_init_block (&cond_block);
8830 : 1653 : gfc_init_block (&loop_body);
8831 : :
8832 : : /* Loop: for (i = 0; i < rank; ++i). */
8833 : 1653 : tree idx = gfc_create_var (signed_char_type_node, "idx");
8834 : : /* Loop body. */
8835 : : /* #if (assumed-rank + !allocatable && !pointer)
8836 : : if (idx == rank - 1 && dim[idx].ubound == -1)
8837 : : extent = -1;
8838 : : else
8839 : : #endif
8840 : : extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1
8841 : : if (extent < 0)
8842 : : extent = 0
8843 : : size *= extent. */
8844 : 1653 : cond = NULL_TREE;
8845 : 1653 : if (akind == GFC_ARRAY_ASSUMED_RANK_CONT || akind == GFC_ARRAY_ASSUMED_RANK)
8846 : : {
8847 : 459 : tmp = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
8848 : : rank, build_int_cst (signed_char_type_node, 1));
8849 : 459 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8850 : : idx, tmp);
8851 : 459 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8852 : : gfc_conv_descriptor_ubound_get (desc, idx),
8853 : : build_int_cst (gfc_array_index_type, -1));
8854 : 459 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
8855 : : cond, tmp);
8856 : : }
8857 : 1653 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8858 : : gfc_conv_descriptor_ubound_get (desc, idx),
8859 : : gfc_conv_descriptor_lbound_get (desc, idx));
8860 : 1653 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8861 : : tmp, gfc_index_one_node);
8862 : 1653 : gfc_add_modify (&cond_block, extent, tmp);
8863 : 1653 : tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
8864 : : extent, gfc_index_zero_node);
8865 : 1653 : tmp = build3_v (COND_EXPR, tmp,
8866 : : fold_build2_loc (input_location, MODIFY_EXPR,
8867 : : gfc_array_index_type,
8868 : : extent, gfc_index_zero_node),
8869 : : build_empty_stmt (input_location));
8870 : 1653 : gfc_add_expr_to_block (&cond_block, tmp);
8871 : 1653 : tmp = gfc_finish_block (&cond_block);
8872 : 1653 : if (cond)
8873 : 459 : tmp = build3_v (COND_EXPR, cond,
8874 : : fold_build2_loc (input_location, MODIFY_EXPR,
8875 : : gfc_array_index_type, extent,
8876 : : build_int_cst (gfc_array_index_type, -1)),
8877 : : tmp);
8878 : 1653 : gfc_add_expr_to_block (&loop_body, tmp);
8879 : : /* size *= extent. */
8880 : 1653 : gfc_add_modify (&loop_body, size,
8881 : : fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8882 : : size, extent));
8883 : : /* Generate loop. */
8884 : 3306 : gfc_simple_for_loop (block, idx, build_int_cst (TREE_TYPE (idx), 0), rank, LT_EXPR,
8885 : 1653 : build_int_cst (TREE_TYPE (idx), 1),
8886 : : gfc_finish_block (&loop_body));
8887 : 1653 : return size;
8888 : : }
8889 : :
8890 : : /* Helper function for gfc_conv_array_parameter if array size needs to be
8891 : : computed. */
8892 : :
8893 : : static void
8894 : 102 : array_parameter_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree *size)
8895 : : {
8896 : 102 : tree elem;
8897 : 102 : *size = gfc_tree_array_size (block, desc, expr, NULL);
8898 : 102 : elem = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
8899 : 102 : *size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8900 : : *size, fold_convert (gfc_array_index_type, elem));
8901 : 102 : }
8902 : :
8903 : : /* Helper function - return true if the argument is a pointer. */
8904 : :
8905 : : static bool
8906 : 733 : is_pointer (gfc_expr *e)
8907 : : {
8908 : 733 : gfc_symbol *sym;
8909 : :
8910 : 733 : if (e->expr_type != EXPR_VARIABLE || e->symtree == NULL)
8911 : : return false;
8912 : :
8913 : 733 : sym = e->symtree->n.sym;
8914 : 733 : if (sym == NULL)
8915 : : return false;
8916 : :
8917 : 733 : return sym->attr.pointer || sym->attr.proc_pointer;
8918 : : }
8919 : :
8920 : : /* Convert an array for passing as an actual parameter. */
8921 : :
8922 : : void
8923 : 65184 : gfc_conv_array_parameter (gfc_se *se, gfc_expr *expr, bool g77,
8924 : : const gfc_symbol *fsym, const char *proc_name,
8925 : : tree *size, tree *lbshift, tree *packed)
8926 : : {
8927 : 65184 : tree ptr;
8928 : 65184 : tree desc;
8929 : 65184 : tree tmp = NULL_TREE;
8930 : 65184 : tree stmt;
8931 : 65184 : tree parent = DECL_CONTEXT (current_function_decl);
8932 : 65184 : tree ctree;
8933 : 65184 : tree pack_attr = NULL_TREE; /* Set when packing class arrays. */
8934 : 65184 : bool full_array_var;
8935 : 65184 : bool this_array_result;
8936 : 65184 : bool contiguous;
8937 : 65184 : bool no_pack;
8938 : 65184 : bool array_constructor;
8939 : 65184 : bool good_allocatable;
8940 : 65184 : bool ultimate_ptr_comp;
8941 : 65184 : bool ultimate_alloc_comp;
8942 : 65184 : bool readonly;
8943 : 65184 : gfc_symbol *sym;
8944 : 65184 : stmtblock_t block;
8945 : 65184 : gfc_ref *ref;
8946 : :
8947 : 65184 : ultimate_ptr_comp = false;
8948 : 65184 : ultimate_alloc_comp = false;
8949 : :
8950 : 65884 : for (ref = expr->ref; ref; ref = ref->next)
8951 : : {
8952 : 54150 : if (ref->next == NULL)
8953 : : break;
8954 : :
8955 : 700 : if (ref->type == REF_COMPONENT)
8956 : : {
8957 : 622 : ultimate_ptr_comp = ref->u.c.component->attr.pointer;
8958 : 622 : ultimate_alloc_comp = ref->u.c.component->attr.allocatable;
8959 : : }
8960 : : }
8961 : :
8962 : 65184 : full_array_var = false;
8963 : 65184 : contiguous = false;
8964 : :
8965 : 65184 : if (expr->expr_type == EXPR_VARIABLE && ref && !ultimate_ptr_comp)
8966 : 53358 : full_array_var = gfc_full_array_ref_p (ref, &contiguous);
8967 : :
8968 : 53358 : sym = full_array_var ? expr->symtree->n.sym : NULL;
8969 : :
8970 : : /* The symbol should have an array specification. */
8971 : 62382 : gcc_assert (!sym || sym->as || ref->u.ar.as);
8972 : :
8973 : 65184 : if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_CHARACTER)
8974 : : {
8975 : 678 : get_array_ctor_strlen (&se->pre, expr->value.constructor, &tmp);
8976 : 678 : expr->ts.u.cl->backend_decl = tmp;
8977 : 678 : se->string_length = tmp;
8978 : : }
8979 : :
8980 : : /* Is this the result of the enclosing procedure? */
8981 : 65184 : this_array_result = (full_array_var && sym->attr.flavor == FL_PROCEDURE);
8982 : 58 : if (this_array_result
8983 : 58 : && (sym->backend_decl != current_function_decl)
8984 : 0 : && (sym->backend_decl != parent))
8985 : 65184 : this_array_result = false;
8986 : :
8987 : : /* Passing an optional dummy argument as actual to an optional dummy? */
8988 : 65184 : bool pass_optional;
8989 : 65184 : pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
8990 : :
8991 : : /* Passing address of the array if it is not pointer or assumed-shape. */
8992 : 65184 : if (full_array_var && g77 && !this_array_result
8993 : 15321 : && sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
8994 : : {
8995 : 12280 : tmp = gfc_get_symbol_decl (sym);
8996 : :
8997 : 12280 : if (sym->ts.type == BT_CHARACTER)
8998 : 2741 : se->string_length = sym->ts.u.cl->backend_decl;
8999 : :
9000 : 12280 : if (!sym->attr.pointer
9001 : 11775 : && sym->as
9002 : 11775 : && sym->as->type != AS_ASSUMED_SHAPE
9003 : 11531 : && sym->as->type != AS_DEFERRED
9004 : 10129 : && sym->as->type != AS_ASSUMED_RANK
9005 : 10061 : && !sym->attr.allocatable)
9006 : : {
9007 : : /* Some variables are declared directly, others are declared as
9008 : : pointers and allocated on the heap. */
9009 : 9609 : if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
9010 : 2469 : se->expr = tmp;
9011 : : else
9012 : 7140 : se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
9013 : 9609 : if (size)
9014 : 34 : array_parameter_size (&se->pre, tmp, expr, size);
9015 : 16191 : return;
9016 : : }
9017 : :
9018 : 2671 : if (sym->attr.allocatable)
9019 : : {
9020 : 1734 : if (sym->attr.dummy || sym->attr.result)
9021 : : {
9022 : 1092 : gfc_conv_expr_descriptor (se, expr);
9023 : 1092 : tmp = se->expr;
9024 : : }
9025 : 1734 : if (size)
9026 : 6 : array_parameter_size (&se->pre, tmp, expr, size);
9027 : 1734 : se->expr = gfc_conv_array_data (tmp);
9028 : 1734 : if (pass_optional)
9029 : : {
9030 : 18 : tree cond = gfc_conv_expr_present (sym);
9031 : 36 : se->expr = build3_loc (input_location, COND_EXPR,
9032 : 18 : TREE_TYPE (se->expr), cond, se->expr,
9033 : 18 : fold_convert (TREE_TYPE (se->expr),
9034 : : null_pointer_node));
9035 : : }
9036 : 1734 : return;
9037 : : }
9038 : : }
9039 : :
9040 : : /* A convenient reduction in scope. */
9041 : 53841 : contiguous = g77 && !this_array_result && contiguous;
9042 : :
9043 : : /* There is no need to pack and unpack the array, if it is contiguous
9044 : : and not a deferred- or assumed-shape array, or if it is simply
9045 : : contiguous. */
9046 : 53841 : no_pack = false;
9047 : : // clang-format off
9048 : 53841 : if (sym)
9049 : : {
9050 : 39303 : symbol_attribute *attr = &(IS_CLASS_ARRAY (sym)
9051 : : ? CLASS_DATA (sym)->attr : sym->attr);
9052 : 39303 : gfc_array_spec *as = IS_CLASS_ARRAY (sym)
9053 : 39303 : ? CLASS_DATA (sym)->as : sym->as;
9054 : 39303 : no_pack = (as
9055 : 39037 : && !attr->pointer
9056 : 35818 : && as->type != AS_DEFERRED
9057 : 26307 : && as->type != AS_ASSUMED_RANK
9058 : 63013 : && as->type != AS_ASSUMED_SHAPE);
9059 : : }
9060 : 53841 : if (ref && ref->u.ar.as)
9061 : 42105 : no_pack = no_pack
9062 : 42105 : || (ref->u.ar.as->type != AS_DEFERRED
9063 : : && ref->u.ar.as->type != AS_ASSUMED_RANK
9064 : : && ref->u.ar.as->type != AS_ASSUMED_SHAPE);
9065 : 107682 : no_pack = contiguous
9066 : 53841 : && (no_pack || gfc_is_simply_contiguous (expr, false, true));
9067 : : // clang-format on
9068 : :
9069 : : /* If we have an EXPR_OP or a function returning an explicit-shaped
9070 : : or allocatable array, an array temporary will be generated which
9071 : : does not need to be packed / unpacked if passed to an
9072 : : explicit-shape dummy array. */
9073 : :
9074 : 53841 : if (g77)
9075 : : {
9076 : 5911 : if (expr->expr_type == EXPR_OP)
9077 : : no_pack = 1;
9078 : 5834 : else if (expr->expr_type == EXPR_FUNCTION && expr->value.function.esym)
9079 : : {
9080 : 41 : gfc_symbol *result = expr->value.function.esym->result;
9081 : 41 : if (result->attr.dimension
9082 : 41 : && (result->as->type == AS_EXPLICIT
9083 : 14 : || result->attr.allocatable
9084 : 7 : || result->attr.contiguous))
9085 : 112 : no_pack = 1;
9086 : : }
9087 : : }
9088 : :
9089 : : /* Array constructors are always contiguous and do not need packing. */
9090 : 53841 : array_constructor = g77 && !this_array_result && expr->expr_type == EXPR_ARRAY;
9091 : :
9092 : : /* Same is true of contiguous sections from allocatable variables. */
9093 : 107682 : good_allocatable = contiguous
9094 : 4095 : && expr->symtree
9095 : 57936 : && expr->symtree->n.sym->attr.allocatable;
9096 : :
9097 : : /* Or ultimate allocatable components. */
9098 : 53841 : ultimate_alloc_comp = contiguous && ultimate_alloc_comp;
9099 : :
9100 : 53841 : if (no_pack || array_constructor || good_allocatable || ultimate_alloc_comp)
9101 : : {
9102 : 4462 : gfc_conv_expr_descriptor (se, expr);
9103 : : /* Deallocate the allocatable components of structures that are
9104 : : not variable. */
9105 : 4462 : if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
9106 : 2956 : && expr->ts.u.derived->attr.alloc_comp
9107 : 1675 : && expr->expr_type != EXPR_VARIABLE)
9108 : : {
9109 : 2 : tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, se->expr, expr->rank);
9110 : :
9111 : : /* The components shall be deallocated before their containing entity. */
9112 : 2 : gfc_prepend_expr_to_block (&se->post, tmp);
9113 : : }
9114 : 4462 : if (expr->ts.type == BT_CHARACTER && expr->expr_type != EXPR_FUNCTION)
9115 : 279 : se->string_length = expr->ts.u.cl->backend_decl;
9116 : 4462 : if (size)
9117 : 32 : array_parameter_size (&se->pre, se->expr, expr, size);
9118 : 4462 : se->expr = gfc_conv_array_data (se->expr);
9119 : 4462 : return;
9120 : : }
9121 : :
9122 : 49379 : if (fsym && fsym->ts.type == BT_CLASS)
9123 : : {
9124 : 1211 : gcc_assert (se->expr);
9125 : : ctree = se->expr;
9126 : : }
9127 : : else
9128 : : ctree = NULL_TREE;
9129 : :
9130 : 49379 : if (this_array_result)
9131 : : {
9132 : : /* Result of the enclosing function. */
9133 : 58 : gfc_conv_expr_descriptor (se, expr);
9134 : 58 : if (size)
9135 : 0 : array_parameter_size (&se->pre, se->expr, expr, size);
9136 : 58 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
9137 : :
9138 : 18 : if (g77 && TREE_TYPE (TREE_TYPE (se->expr)) != NULL_TREE
9139 : 76 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
9140 : 18 : se->expr = gfc_conv_array_data (build_fold_indirect_ref_loc (input_location,
9141 : : se->expr));
9142 : :
9143 : 58 : return;
9144 : : }
9145 : : else
9146 : : {
9147 : : /* Every other type of array. */
9148 : 49321 : se->want_pointer = (ctree) ? 0 : 1;
9149 : 49321 : se->want_coarray = expr->corank;
9150 : 49321 : gfc_conv_expr_descriptor (se, expr);
9151 : :
9152 : 49321 : if (size)
9153 : 30 : array_parameter_size (&se->pre,
9154 : : build_fold_indirect_ref_loc (input_location,
9155 : : se->expr),
9156 : : expr, size);
9157 : 49321 : if (ctree)
9158 : : {
9159 : 1211 : stmtblock_t block;
9160 : :
9161 : 1211 : gfc_init_block (&block);
9162 : 1211 : if (lbshift && *lbshift)
9163 : : {
9164 : : /* Apply a shift of the lbound when supplied. */
9165 : 98 : for (int dim = 0; dim < expr->rank; ++dim)
9166 : 49 : gfc_conv_shift_descriptor_lbound (&block, se->expr, dim,
9167 : : *lbshift);
9168 : : }
9169 : 1211 : tmp = gfc_class_data_get (ctree);
9170 : 1211 : if (expr->rank > 1 && CLASS_DATA (fsym)->as->rank != expr->rank
9171 : 84 : && CLASS_DATA (fsym)->as->type == AS_EXPLICIT && !no_pack)
9172 : : {
9173 : 36 : tree arr = gfc_create_var (TREE_TYPE (tmp), "parm");
9174 : 36 : gfc_conv_descriptor_data_set (&block, arr,
9175 : : gfc_conv_descriptor_data_get (
9176 : : se->expr));
9177 : 36 : gfc_conv_descriptor_lbound_set (&block, arr, gfc_index_zero_node,
9178 : : gfc_index_zero_node);
9179 : 36 : gfc_conv_descriptor_ubound_set (
9180 : : &block, arr, gfc_index_zero_node,
9181 : : gfc_conv_descriptor_size (se->expr, expr->rank));
9182 : 36 : gfc_conv_descriptor_stride_set (
9183 : : &block, arr, gfc_index_zero_node,
9184 : : gfc_conv_descriptor_stride_get (se->expr, gfc_index_zero_node));
9185 : 36 : gfc_add_modify (&block, gfc_conv_descriptor_dtype (arr),
9186 : : gfc_conv_descriptor_dtype (se->expr));
9187 : 36 : gfc_add_modify (&block, gfc_conv_descriptor_rank (arr),
9188 : : build_int_cst (signed_char_type_node, 1));
9189 : 36 : gfc_conv_descriptor_span_set (&block, arr,
9190 : : gfc_conv_descriptor_span_get (arr));
9191 : 36 : gfc_conv_descriptor_offset_set (&block, arr, gfc_index_zero_node);
9192 : 36 : se->expr = arr;
9193 : : }
9194 : 1211 : gfc_class_array_data_assign (&block, tmp, se->expr, true);
9195 : :
9196 : : /* Handle optional. */
9197 : 1211 : if (fsym && fsym->attr.optional && sym && sym->attr.optional)
9198 : 348 : tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
9199 : : gfc_finish_block (&block),
9200 : : build_empty_stmt (input_location));
9201 : : else
9202 : 863 : tmp = gfc_finish_block (&block);
9203 : :
9204 : 1211 : gfc_add_expr_to_block (&se->pre, tmp);
9205 : : }
9206 : : }
9207 : :
9208 : : /* Deallocate the allocatable components of structures that are
9209 : : not variable, for descriptorless arguments.
9210 : : Arguments with a descriptor are handled in gfc_conv_procedure_call. */
9211 : 49321 : if (g77 && (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
9212 : 75 : && expr->ts.u.derived->attr.alloc_comp
9213 : 21 : && expr->expr_type != EXPR_VARIABLE)
9214 : : {
9215 : 0 : tmp = build_fold_indirect_ref_loc (input_location, se->expr);
9216 : 0 : tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
9217 : :
9218 : : /* The components shall be deallocated before their containing entity. */
9219 : 0 : gfc_prepend_expr_to_block (&se->post, tmp);
9220 : : }
9221 : :
9222 : 47890 : if (g77 || (fsym && fsym->attr.contiguous
9223 : 1527 : && !gfc_is_simply_contiguous (expr, false, true)))
9224 : : {
9225 : 1575 : tree origptr = NULL_TREE, packedptr = NULL_TREE;
9226 : :
9227 : 1575 : desc = se->expr;
9228 : :
9229 : : /* For contiguous arrays, save the original value of the descriptor. */
9230 : 1575 : if (!g77 && !ctree)
9231 : : {
9232 : 48 : origptr = gfc_create_var (pvoid_type_node, "origptr");
9233 : 48 : tmp = build_fold_indirect_ref_loc (input_location, desc);
9234 : 48 : tmp = gfc_conv_array_data (tmp);
9235 : 96 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
9236 : 48 : TREE_TYPE (origptr), origptr,
9237 : 48 : fold_convert (TREE_TYPE (origptr), tmp));
9238 : 48 : gfc_add_expr_to_block (&se->pre, tmp);
9239 : : }
9240 : :
9241 : : /* Repack the array. */
9242 : 1575 : if (warn_array_temporaries)
9243 : : {
9244 : 28 : if (fsym)
9245 : 18 : gfc_warning (OPT_Warray_temporaries,
9246 : : "Creating array temporary at %L for argument %qs",
9247 : 18 : &expr->where, fsym->name);
9248 : : else
9249 : 10 : gfc_warning (OPT_Warray_temporaries,
9250 : : "Creating array temporary at %L", &expr->where);
9251 : : }
9252 : :
9253 : : /* When optimizing, we can use gfc_conv_subref_array_arg for
9254 : : making the packing and unpacking operation visible to the
9255 : : optimizers. */
9256 : :
9257 : 1431 : if (g77 && flag_inline_arg_packing && expr->expr_type == EXPR_VARIABLE
9258 : 733 : && !is_pointer (expr) && ! gfc_has_dimen_vector_ref (expr)
9259 : 349 : && !(expr->symtree->n.sym->as
9260 : 320 : && expr->symtree->n.sym->as->type == AS_ASSUMED_RANK)
9261 : 1924 : && (fsym == NULL || fsym->ts.type != BT_ASSUMED))
9262 : : {
9263 : 328 : gfc_conv_subref_array_arg (se, expr, g77,
9264 : 137 : fsym ? fsym->attr.intent : INTENT_INOUT,
9265 : : false, fsym, proc_name, sym, true);
9266 : 328 : return;
9267 : : }
9268 : :
9269 : 1247 : if (ctree)
9270 : : {
9271 : 96 : packedptr
9272 : 96 : = gfc_build_addr_expr (NULL_TREE, gfc_create_var (TREE_TYPE (ctree),
9273 : : "packed"));
9274 : 96 : if (fsym)
9275 : : {
9276 : 96 : int pack_mask = 0;
9277 : :
9278 : : /* Set bit 0 to the mask, when this is an unlimited_poly
9279 : : class. */
9280 : 96 : if (CLASS_DATA (fsym)->ts.u.derived->attr.unlimited_polymorphic)
9281 : 36 : pack_mask = 1 << 0;
9282 : 96 : pack_attr = build_int_cst (integer_type_node, pack_mask);
9283 : : }
9284 : : else
9285 : 0 : pack_attr = integer_zero_node;
9286 : :
9287 : 96 : gfc_add_expr_to_block (
9288 : : &se->pre,
9289 : : build_call_expr_loc (input_location, gfor_fndecl_in_pack_class, 4,
9290 : : packedptr,
9291 : : gfc_build_addr_expr (NULL_TREE, ctree),
9292 : 96 : size_in_bytes (TREE_TYPE (ctree)), pack_attr));
9293 : 96 : ptr = gfc_conv_array_data (gfc_class_data_get (packedptr));
9294 : 96 : se->expr = packedptr;
9295 : 96 : if (packed)
9296 : 96 : *packed = packedptr;
9297 : : }
9298 : : else
9299 : : {
9300 : 1151 : ptr = build_call_expr_loc (input_location, gfor_fndecl_in_pack, 1,
9301 : : desc);
9302 : :
9303 : 1151 : if (fsym && fsym->attr.optional && sym && sym->attr.optional)
9304 : : {
9305 : 11 : tmp = gfc_conv_expr_present (sym);
9306 : 22 : ptr = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
9307 : 11 : tmp, fold_convert (TREE_TYPE (se->expr), ptr),
9308 : 11 : fold_convert (TREE_TYPE (se->expr),
9309 : : null_pointer_node));
9310 : : }
9311 : :
9312 : 1151 : ptr = gfc_evaluate_now (ptr, &se->pre);
9313 : : }
9314 : :
9315 : : /* Use the packed data for the actual argument, except for contiguous arrays,
9316 : : where the descriptor's data component is set. */
9317 : 1247 : if (g77)
9318 : 1103 : se->expr = ptr;
9319 : : else
9320 : : {
9321 : 144 : tmp = build_fold_indirect_ref_loc (input_location, desc);
9322 : :
9323 : 144 : gfc_ss * ss = gfc_walk_expr (expr);
9324 : 288 : if (!transposed_dims (ss))
9325 : : {
9326 : 138 : if (!ctree)
9327 : 48 : gfc_conv_descriptor_data_set (&se->pre, tmp, ptr);
9328 : : }
9329 : 6 : else if (!ctree)
9330 : : {
9331 : 0 : tree old_field, new_field;
9332 : :
9333 : : /* The original descriptor has transposed dims so we can't reuse
9334 : : it directly; we have to create a new one. */
9335 : 0 : tree old_desc = tmp;
9336 : 0 : tree new_desc = gfc_create_var (TREE_TYPE (old_desc), "arg_desc");
9337 : :
9338 : 0 : old_field = gfc_conv_descriptor_dtype (old_desc);
9339 : 0 : new_field = gfc_conv_descriptor_dtype (new_desc);
9340 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9341 : :
9342 : 0 : old_field = gfc_conv_descriptor_offset (old_desc);
9343 : 0 : new_field = gfc_conv_descriptor_offset (new_desc);
9344 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9345 : :
9346 : 0 : for (int i = 0; i < expr->rank; i++)
9347 : : {
9348 : 0 : old_field = gfc_conv_descriptor_dimension (old_desc,
9349 : 0 : gfc_rank_cst[get_array_ref_dim_for_loop_dim (ss, i)]);
9350 : 0 : new_field = gfc_conv_descriptor_dimension (new_desc,
9351 : : gfc_rank_cst[i]);
9352 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9353 : : }
9354 : :
9355 : 0 : if (flag_coarray == GFC_FCOARRAY_LIB
9356 : 0 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (old_desc))
9357 : 0 : && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (old_desc))
9358 : : == GFC_ARRAY_ALLOCATABLE)
9359 : : {
9360 : 0 : old_field = gfc_conv_descriptor_token (old_desc);
9361 : 0 : new_field = gfc_conv_descriptor_token (new_desc);
9362 : 0 : gfc_add_modify (&se->pre, new_field, old_field);
9363 : : }
9364 : :
9365 : 0 : gfc_conv_descriptor_data_set (&se->pre, new_desc, ptr);
9366 : 0 : se->expr = gfc_build_addr_expr (NULL_TREE, new_desc);
9367 : : }
9368 : 144 : gfc_free_ss (ss);
9369 : : }
9370 : :
9371 : 1247 : if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
9372 : : {
9373 : 8 : char * msg;
9374 : :
9375 : 8 : if (fsym && proc_name)
9376 : 8 : msg = xasprintf ("An array temporary was created for argument "
9377 : 8 : "'%s' of procedure '%s'", fsym->name, proc_name);
9378 : : else
9379 : 0 : msg = xasprintf ("An array temporary was created");
9380 : :
9381 : 8 : tmp = build_fold_indirect_ref_loc (input_location,
9382 : : desc);
9383 : 8 : tmp = gfc_conv_array_data (tmp);
9384 : 8 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9385 : 8 : fold_convert (TREE_TYPE (tmp), ptr), tmp);
9386 : :
9387 : 8 : if (pass_optional)
9388 : 6 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9389 : : logical_type_node,
9390 : : gfc_conv_expr_present (sym), tmp);
9391 : :
9392 : 8 : gfc_trans_runtime_check (false, true, tmp, &se->pre,
9393 : : &expr->where, msg);
9394 : 8 : free (msg);
9395 : : }
9396 : :
9397 : 1247 : gfc_start_block (&block);
9398 : :
9399 : : /* Copy the data back. If input expr is read-only, e.g. a PARAMETER
9400 : : array, copying back modified values is undefined behavior. */
9401 : 2494 : readonly = (expr->expr_type == EXPR_VARIABLE
9402 : 854 : && expr->symtree
9403 : 2101 : && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
9404 : :
9405 : 1247 : if ((fsym == NULL || fsym->attr.intent != INTENT_IN) && !readonly)
9406 : : {
9407 : 1114 : if (ctree)
9408 : : {
9409 : 66 : tmp = gfc_build_addr_expr (NULL_TREE, ctree);
9410 : 66 : tmp = build_call_expr_loc (input_location,
9411 : : gfor_fndecl_in_unpack_class, 4, tmp,
9412 : : packedptr,
9413 : 66 : size_in_bytes (TREE_TYPE (ctree)),
9414 : : pack_attr);
9415 : : }
9416 : : else
9417 : 1048 : tmp = build_call_expr_loc (input_location, gfor_fndecl_in_unpack, 2,
9418 : : desc, ptr);
9419 : 1114 : gfc_add_expr_to_block (&block, tmp);
9420 : : }
9421 : 133 : else if (ctree && fsym->attr.intent == INTENT_IN)
9422 : : {
9423 : : /* Need to free the memory for class arrays, that got packed. */
9424 : 30 : gfc_add_expr_to_block (&block, gfc_call_free (ptr));
9425 : : }
9426 : :
9427 : : /* Free the temporary. */
9428 : 1144 : if (!ctree)
9429 : 1151 : gfc_add_expr_to_block (&block, gfc_call_free (ptr));
9430 : :
9431 : 1247 : stmt = gfc_finish_block (&block);
9432 : :
9433 : 1247 : gfc_init_block (&block);
9434 : : /* Only if it was repacked. This code needs to be executed before the
9435 : : loop cleanup code. */
9436 : 1247 : tmp = (ctree) ? desc : build_fold_indirect_ref_loc (input_location, desc);
9437 : 1247 : tmp = gfc_conv_array_data (tmp);
9438 : 1247 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9439 : 1247 : fold_convert (TREE_TYPE (tmp), ptr), tmp);
9440 : :
9441 : 1247 : if (pass_optional)
9442 : 11 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9443 : : logical_type_node,
9444 : : gfc_conv_expr_present (sym), tmp);
9445 : :
9446 : 1247 : tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
9447 : :
9448 : 1247 : gfc_add_expr_to_block (&block, tmp);
9449 : 1247 : gfc_add_block_to_block (&block, &se->post);
9450 : :
9451 : 1247 : gfc_init_block (&se->post);
9452 : :
9453 : : /* Reset the descriptor pointer. */
9454 : 1247 : if (!g77 && !ctree)
9455 : : {
9456 : 48 : tmp = build_fold_indirect_ref_loc (input_location, desc);
9457 : 48 : gfc_conv_descriptor_data_set (&se->post, tmp, origptr);
9458 : : }
9459 : :
9460 : 1247 : gfc_add_block_to_block (&se->post, &block);
9461 : : }
9462 : : }
9463 : :
9464 : :
9465 : : /* This helper function calculates the size in words of a full array. */
9466 : :
9467 : : tree
9468 : 15508 : gfc_full_array_size (stmtblock_t *block, tree decl, int rank)
9469 : : {
9470 : 15508 : tree idx;
9471 : 15508 : tree nelems;
9472 : 15508 : tree tmp;
9473 : 15508 : if (rank < 0)
9474 : 0 : idx = gfc_conv_descriptor_rank (decl);
9475 : : else
9476 : 15508 : idx = gfc_rank_cst[rank - 1];
9477 : 15508 : nelems = gfc_conv_descriptor_ubound_get (decl, idx);
9478 : 15508 : tmp = gfc_conv_descriptor_lbound_get (decl, idx);
9479 : 15508 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9480 : : nelems, tmp);
9481 : 15508 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
9482 : : tmp, gfc_index_one_node);
9483 : 15508 : tmp = gfc_evaluate_now (tmp, block);
9484 : :
9485 : 15508 : nelems = gfc_conv_descriptor_stride_get (decl, idx);
9486 : 15508 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9487 : : nelems, tmp);
9488 : 15508 : return gfc_evaluate_now (tmp, block);
9489 : : }
9490 : :
9491 : :
9492 : : /* Allocate dest to the same size as src, and copy src -> dest.
9493 : : If no_malloc is set, only the copy is done. */
9494 : :
9495 : : static tree
9496 : 6688 : duplicate_allocatable (tree dest, tree src, tree type, int rank,
9497 : : bool no_malloc, bool no_memcpy, tree str_sz,
9498 : : tree add_when_allocated)
9499 : : {
9500 : 6688 : tree tmp;
9501 : 6688 : tree eltype;
9502 : 6688 : tree size;
9503 : 6688 : tree nelems;
9504 : 6688 : tree null_cond;
9505 : 6688 : tree null_data;
9506 : 6688 : stmtblock_t block;
9507 : :
9508 : : /* If the source is null, set the destination to null. Then,
9509 : : allocate memory to the destination. */
9510 : 6688 : gfc_init_block (&block);
9511 : :
9512 : 6688 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
9513 : : {
9514 : 1973 : gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
9515 : 1973 : null_data = gfc_finish_block (&block);
9516 : :
9517 : 1973 : gfc_init_block (&block);
9518 : 1973 : eltype = TREE_TYPE (type);
9519 : 1973 : if (str_sz != NULL_TREE)
9520 : : size = str_sz;
9521 : : else
9522 : 1679 : size = TYPE_SIZE_UNIT (eltype);
9523 : :
9524 : 1973 : if (!no_malloc)
9525 : : {
9526 : 1973 : tmp = gfc_call_malloc (&block, type, size);
9527 : 1973 : gfc_add_modify (&block, dest, fold_convert (type, tmp));
9528 : : }
9529 : :
9530 : 1973 : if (!no_memcpy)
9531 : : {
9532 : 1548 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9533 : 1548 : tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
9534 : : fold_convert (size_type_node, size));
9535 : 1548 : gfc_add_expr_to_block (&block, tmp);
9536 : : }
9537 : : }
9538 : : else
9539 : : {
9540 : 4715 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9541 : 4715 : null_data = gfc_finish_block (&block);
9542 : :
9543 : 4715 : gfc_init_block (&block);
9544 : 4715 : if (rank)
9545 : 4704 : nelems = gfc_full_array_size (&block, src, rank);
9546 : : else
9547 : 11 : nelems = gfc_index_one_node;
9548 : :
9549 : : /* If type is not the array type, then it is the element type. */
9550 : 4715 : if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
9551 : 4685 : eltype = gfc_get_element_type (type);
9552 : : else
9553 : : eltype = type;
9554 : :
9555 : 4715 : if (str_sz != NULL_TREE)
9556 : 43 : tmp = fold_convert (gfc_array_index_type, str_sz);
9557 : : else
9558 : 4672 : tmp = fold_convert (gfc_array_index_type,
9559 : : TYPE_SIZE_UNIT (eltype));
9560 : :
9561 : 4715 : tmp = gfc_evaluate_now (tmp, &block);
9562 : 4715 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9563 : : nelems, tmp);
9564 : 4715 : if (!no_malloc)
9565 : : {
9566 : 4664 : tmp = TREE_TYPE (gfc_conv_descriptor_data_get (src));
9567 : 4664 : tmp = gfc_call_malloc (&block, tmp, size);
9568 : 4664 : gfc_conv_descriptor_data_set (&block, dest, tmp);
9569 : : }
9570 : :
9571 : : /* We know the temporary and the value will be the same length,
9572 : : so can use memcpy. */
9573 : 4715 : if (!no_memcpy)
9574 : : {
9575 : 4285 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9576 : 4285 : tmp = build_call_expr_loc (input_location, tmp, 3,
9577 : : gfc_conv_descriptor_data_get (dest),
9578 : : gfc_conv_descriptor_data_get (src),
9579 : : fold_convert (size_type_node, size));
9580 : 4285 : gfc_add_expr_to_block (&block, tmp);
9581 : : }
9582 : : }
9583 : :
9584 : 6688 : gfc_add_expr_to_block (&block, add_when_allocated);
9585 : 6688 : tmp = gfc_finish_block (&block);
9586 : :
9587 : : /* Null the destination if the source is null; otherwise do
9588 : : the allocate and copy. */
9589 : 6688 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
9590 : : null_cond = src;
9591 : : else
9592 : 4715 : null_cond = gfc_conv_descriptor_data_get (src);
9593 : :
9594 : 6688 : null_cond = convert (pvoid_type_node, null_cond);
9595 : 6688 : null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9596 : : null_cond, null_pointer_node);
9597 : 6688 : return build3_v (COND_EXPR, null_cond, tmp, null_data);
9598 : : }
9599 : :
9600 : :
9601 : : /* Allocate dest to the same size as src, and copy data src -> dest. */
9602 : :
9603 : : tree
9604 : 5414 : gfc_duplicate_allocatable (tree dest, tree src, tree type, int rank,
9605 : : tree add_when_allocated)
9606 : : {
9607 : 5414 : return duplicate_allocatable (dest, src, type, rank, false, false,
9608 : 5414 : NULL_TREE, add_when_allocated);
9609 : : }
9610 : :
9611 : :
9612 : : /* Copy data src -> dest. */
9613 : :
9614 : : tree
9615 : 51 : gfc_copy_allocatable_data (tree dest, tree src, tree type, int rank)
9616 : : {
9617 : 51 : return duplicate_allocatable (dest, src, type, rank, true, false,
9618 : 51 : NULL_TREE, NULL_TREE);
9619 : : }
9620 : :
9621 : : /* Allocate dest to the same size as src, but don't copy anything. */
9622 : :
9623 : : tree
9624 : 855 : gfc_duplicate_allocatable_nocopy (tree dest, tree src, tree type, int rank)
9625 : : {
9626 : 855 : return duplicate_allocatable (dest, src, type, rank, false, true,
9627 : 855 : NULL_TREE, NULL_TREE);
9628 : : }
9629 : :
9630 : : static tree
9631 : 55 : duplicate_allocatable_coarray (tree dest, tree dest_tok, tree src, tree type,
9632 : : int rank, tree add_when_allocated)
9633 : : {
9634 : 55 : tree tmp;
9635 : 55 : tree size;
9636 : 55 : tree nelems;
9637 : 55 : tree null_cond;
9638 : 55 : tree null_data;
9639 : 55 : stmtblock_t block, globalblock;
9640 : :
9641 : : /* If the source is null, set the destination to null. Then,
9642 : : allocate memory to the destination. */
9643 : 55 : gfc_init_block (&block);
9644 : 55 : gfc_init_block (&globalblock);
9645 : :
9646 : 55 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
9647 : : {
9648 : 15 : gfc_se se;
9649 : 15 : symbol_attribute attr;
9650 : 15 : tree dummy_desc;
9651 : :
9652 : 15 : gfc_init_se (&se, NULL);
9653 : 15 : gfc_clear_attr (&attr);
9654 : 15 : attr.allocatable = 1;
9655 : 15 : dummy_desc = gfc_conv_scalar_to_descriptor (&se, dest, attr);
9656 : 15 : gfc_add_block_to_block (&globalblock, &se.pre);
9657 : 15 : size = TYPE_SIZE_UNIT (TREE_TYPE (type));
9658 : :
9659 : 15 : gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
9660 : 15 : gfc_allocate_using_caf_lib (&block, dummy_desc, size,
9661 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9662 : : NULL_TREE, NULL_TREE, NULL_TREE,
9663 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
9664 : 15 : gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
9665 : 15 : null_data = gfc_finish_block (&block);
9666 : :
9667 : 15 : gfc_init_block (&block);
9668 : :
9669 : 15 : gfc_allocate_using_caf_lib (&block, dummy_desc,
9670 : : fold_convert (size_type_node, size),
9671 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9672 : : NULL_TREE, NULL_TREE, NULL_TREE,
9673 : : GFC_CAF_COARRAY_ALLOC);
9674 : 15 : gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
9675 : :
9676 : 15 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9677 : 15 : tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
9678 : : fold_convert (size_type_node, size));
9679 : 15 : gfc_add_expr_to_block (&block, tmp);
9680 : : }
9681 : : else
9682 : : {
9683 : : /* Set the rank or unitialized memory access may be reported. */
9684 : 40 : tmp = gfc_conv_descriptor_rank (dest);
9685 : 40 : gfc_add_modify (&globalblock, tmp, build_int_cst (TREE_TYPE (tmp), rank));
9686 : :
9687 : 40 : if (rank)
9688 : 40 : nelems = gfc_full_array_size (&globalblock, src, rank);
9689 : : else
9690 : 0 : nelems = integer_one_node;
9691 : :
9692 : 40 : tmp = fold_convert (size_type_node,
9693 : : TYPE_SIZE_UNIT (gfc_get_element_type (type)));
9694 : 40 : size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
9695 : : fold_convert (size_type_node, nelems), tmp);
9696 : :
9697 : 40 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9698 : 40 : gfc_allocate_using_caf_lib (&block, dest, fold_convert (size_type_node,
9699 : : size),
9700 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9701 : : NULL_TREE, NULL_TREE, NULL_TREE,
9702 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
9703 : 40 : null_data = gfc_finish_block (&block);
9704 : :
9705 : 40 : gfc_init_block (&block);
9706 : 40 : gfc_allocate_using_caf_lib (&block, dest,
9707 : : fold_convert (size_type_node, size),
9708 : : gfc_build_addr_expr (NULL_TREE, dest_tok),
9709 : : NULL_TREE, NULL_TREE, NULL_TREE,
9710 : : GFC_CAF_COARRAY_ALLOC);
9711 : :
9712 : 40 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
9713 : 40 : tmp = build_call_expr_loc (input_location, tmp, 3,
9714 : : gfc_conv_descriptor_data_get (dest),
9715 : : gfc_conv_descriptor_data_get (src),
9716 : : fold_convert (size_type_node, size));
9717 : 40 : gfc_add_expr_to_block (&block, tmp);
9718 : : }
9719 : 55 : gfc_add_expr_to_block (&block, add_when_allocated);
9720 : 55 : tmp = gfc_finish_block (&block);
9721 : :
9722 : : /* Null the destination if the source is null; otherwise do
9723 : : the register and copy. */
9724 : 55 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
9725 : : null_cond = src;
9726 : : else
9727 : 40 : null_cond = gfc_conv_descriptor_data_get (src);
9728 : :
9729 : 55 : null_cond = convert (pvoid_type_node, null_cond);
9730 : 55 : null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9731 : : null_cond, null_pointer_node);
9732 : 55 : gfc_add_expr_to_block (&globalblock, build3_v (COND_EXPR, null_cond, tmp,
9733 : : null_data));
9734 : 55 : return gfc_finish_block (&globalblock);
9735 : : }
9736 : :
9737 : :
9738 : : /* Helper function to abstract whether coarray processing is enabled. */
9739 : :
9740 : : static bool
9741 : 73 : caf_enabled (int caf_mode)
9742 : : {
9743 : 73 : return (caf_mode & GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY)
9744 : 73 : == GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY;
9745 : : }
9746 : :
9747 : :
9748 : : /* Helper function to abstract whether coarray processing is enabled
9749 : : and we are in a derived type coarray. */
9750 : :
9751 : : static bool
9752 : 8050 : caf_in_coarray (int caf_mode)
9753 : : {
9754 : 8050 : static const int pat = GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
9755 : : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY;
9756 : 8050 : return (caf_mode & pat) == pat;
9757 : : }
9758 : :
9759 : :
9760 : : /* Helper function to abstract whether coarray is to deallocate only. */
9761 : :
9762 : : bool
9763 : 311 : gfc_caf_is_dealloc_only (int caf_mode)
9764 : : {
9765 : 311 : return (caf_mode & GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY)
9766 : 311 : == GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY;
9767 : : }
9768 : :
9769 : :
9770 : : /* Recursively traverse an object of derived type, generating code to
9771 : : deallocate, nullify or copy allocatable components. This is the work horse
9772 : : function for the functions named in this enum. */
9773 : :
9774 : : enum {DEALLOCATE_ALLOC_COMP = 1, NULLIFY_ALLOC_COMP,
9775 : : COPY_ALLOC_COMP, COPY_ONLY_ALLOC_COMP, REASSIGN_CAF_COMP,
9776 : : ALLOCATE_PDT_COMP, DEALLOCATE_PDT_COMP, CHECK_PDT_DUMMY,
9777 : : BCAST_ALLOC_COMP};
9778 : :
9779 : : static gfc_actual_arglist *pdt_param_list;
9780 : :
9781 : : static tree
9782 : 15936 : structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest,
9783 : : int rank, int purpose, int caf_mode,
9784 : : gfc_co_subroutines_args *args,
9785 : : bool no_finalization = false)
9786 : : {
9787 : 15936 : gfc_component *c;
9788 : 15936 : gfc_loopinfo loop;
9789 : 15936 : stmtblock_t fnblock;
9790 : 15936 : stmtblock_t loopbody;
9791 : 15936 : stmtblock_t tmpblock;
9792 : 15936 : tree decl_type;
9793 : 15936 : tree tmp;
9794 : 15936 : tree comp;
9795 : 15936 : tree dcmp;
9796 : 15936 : tree nelems;
9797 : 15936 : tree index;
9798 : 15936 : tree var;
9799 : 15936 : tree cdecl;
9800 : 15936 : tree ctype;
9801 : 15936 : tree vref, dref;
9802 : 15936 : tree null_cond = NULL_TREE;
9803 : 15936 : tree add_when_allocated;
9804 : 15936 : tree dealloc_fndecl;
9805 : 15936 : tree caf_token;
9806 : 15936 : gfc_symbol *vtab;
9807 : 15936 : int caf_dereg_mode;
9808 : 15936 : symbol_attribute *attr;
9809 : 15936 : bool deallocate_called;
9810 : 15936 : static hash_set<gfc_symbol *> seen_derived_types;
9811 : :
9812 : 15936 : gfc_init_block (&fnblock);
9813 : :
9814 : 15936 : decl_type = TREE_TYPE (decl);
9815 : :
9816 : 15936 : if ((POINTER_TYPE_P (decl_type))
9817 : : || (TREE_CODE (decl_type) == REFERENCE_TYPE && rank == 0))
9818 : : {
9819 : 1220 : decl = build_fold_indirect_ref_loc (input_location, decl);
9820 : : /* Deref dest in sync with decl, but only when it is not NULL. */
9821 : 1220 : if (dest)
9822 : 91 : dest = build_fold_indirect_ref_loc (input_location, dest);
9823 : :
9824 : : /* Update the decl_type because it got dereferenced. */
9825 : 1220 : decl_type = TREE_TYPE (decl);
9826 : : }
9827 : :
9828 : : /* If this is an array of derived types with allocatable components
9829 : : build a loop and recursively call this function. */
9830 : 15936 : if (TREE_CODE (decl_type) == ARRAY_TYPE
9831 : 15936 : || (GFC_DESCRIPTOR_TYPE_P (decl_type) && rank != 0))
9832 : : {
9833 : 2639 : tmp = gfc_conv_array_data (decl);
9834 : 2639 : var = build_fold_indirect_ref_loc (input_location, tmp);
9835 : :
9836 : : /* Get the number of elements - 1 and set the counter. */
9837 : 2639 : if (GFC_DESCRIPTOR_TYPE_P (decl_type))
9838 : : {
9839 : : /* Use the descriptor for an allocatable array. Since this
9840 : : is a full array reference, we only need the descriptor
9841 : : information from dimension = rank. */
9842 : 1841 : tmp = gfc_full_array_size (&fnblock, decl, rank);
9843 : 1841 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9844 : : gfc_array_index_type, tmp,
9845 : : gfc_index_one_node);
9846 : :
9847 : 1841 : null_cond = gfc_conv_descriptor_data_get (decl);
9848 : 1841 : null_cond = fold_build2_loc (input_location, NE_EXPR,
9849 : : logical_type_node, null_cond,
9850 : 1841 : build_int_cst (TREE_TYPE (null_cond), 0));
9851 : : }
9852 : : else
9853 : : {
9854 : : /* Otherwise use the TYPE_DOMAIN information. */
9855 : 798 : tmp = array_type_nelts_minus_one (decl_type);
9856 : 798 : tmp = fold_convert (gfc_array_index_type, tmp);
9857 : : }
9858 : :
9859 : : /* Remember that this is, in fact, the no. of elements - 1. */
9860 : 2639 : nelems = gfc_evaluate_now (tmp, &fnblock);
9861 : 2639 : index = gfc_create_var (gfc_array_index_type, "S");
9862 : :
9863 : : /* Build the body of the loop. */
9864 : 2639 : gfc_init_block (&loopbody);
9865 : :
9866 : 2639 : vref = gfc_build_array_ref (var, index, NULL);
9867 : :
9868 : 2639 : if (purpose == COPY_ALLOC_COMP || purpose == COPY_ONLY_ALLOC_COMP)
9869 : : {
9870 : 491 : tmp = build_fold_indirect_ref_loc (input_location,
9871 : : gfc_conv_array_data (dest));
9872 : 491 : dref = gfc_build_array_ref (tmp, index, NULL);
9873 : 491 : tmp = structure_alloc_comps (der_type, vref, dref, rank,
9874 : : COPY_ALLOC_COMP, caf_mode, args,
9875 : : no_finalization);
9876 : : }
9877 : : else
9878 : 2148 : tmp = structure_alloc_comps (der_type, vref, NULL_TREE, rank, purpose,
9879 : : caf_mode, args, no_finalization);
9880 : :
9881 : 2639 : gfc_add_expr_to_block (&loopbody, tmp);
9882 : :
9883 : : /* Build the loop and return. */
9884 : 2639 : gfc_init_loopinfo (&loop);
9885 : 2639 : loop.dimen = 1;
9886 : 2639 : loop.from[0] = gfc_index_zero_node;
9887 : 2639 : loop.loopvar[0] = index;
9888 : 2639 : loop.to[0] = nelems;
9889 : 2639 : gfc_trans_scalarizing_loops (&loop, &loopbody);
9890 : 2639 : gfc_add_block_to_block (&fnblock, &loop.pre);
9891 : :
9892 : 2639 : tmp = gfc_finish_block (&fnblock);
9893 : : /* When copying allocateable components, the above implements the
9894 : : deep copy. Nevertheless is a deep copy only allowed, when the current
9895 : : component is allocated, for which code will be generated in
9896 : : gfc_duplicate_allocatable (), where the deep copy code is just added
9897 : : into the if's body, by adding tmp (the deep copy code) as last
9898 : : argument to gfc_duplicate_allocatable (). */
9899 : 2639 : if (purpose == COPY_ALLOC_COMP && caf_mode == 0
9900 : 2639 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
9901 : 414 : tmp = gfc_duplicate_allocatable (dest, decl, decl_type, rank,
9902 : : tmp);
9903 : 2225 : else if (null_cond != NULL_TREE)
9904 : 1427 : tmp = build3_v (COND_EXPR, null_cond, tmp,
9905 : : build_empty_stmt (input_location));
9906 : :
9907 : 2639 : return tmp;
9908 : : }
9909 : :
9910 : 13297 : if (purpose == DEALLOCATE_ALLOC_COMP && der_type->attr.pdt_type)
9911 : : {
9912 : 35 : tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
9913 : : DEALLOCATE_PDT_COMP, 0, args,
9914 : : no_finalization);
9915 : 35 : gfc_add_expr_to_block (&fnblock, tmp);
9916 : : }
9917 : 13262 : else if (purpose == ALLOCATE_PDT_COMP && der_type->attr.alloc_comp)
9918 : : {
9919 : 14 : tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
9920 : : NULLIFY_ALLOC_COMP, 0, args,
9921 : : no_finalization);
9922 : 14 : gfc_add_expr_to_block (&fnblock, tmp);
9923 : : }
9924 : :
9925 : : /* Still having a descriptor array of rank == 0 here, indicates an
9926 : : allocatable coarrays. Dereference it correctly. */
9927 : 13297 : if (GFC_DESCRIPTOR_TYPE_P (decl_type))
9928 : : {
9929 : 4 : decl = build_fold_indirect_ref (gfc_conv_array_data (decl));
9930 : : }
9931 : : /* Otherwise, act on the components or recursively call self to
9932 : : act on a chain of components. */
9933 : 13297 : seen_derived_types.add (der_type);
9934 : 36890 : for (c = der_type->components; c; c = c->next)
9935 : : {
9936 : 23593 : bool cmp_has_alloc_comps = (c->ts.type == BT_DERIVED
9937 : 23593 : || c->ts.type == BT_CLASS)
9938 : 23593 : && c->ts.u.derived->attr.alloc_comp;
9939 : 23593 : bool same_type
9940 : : = (c->ts.type == BT_DERIVED
9941 : 4720 : && seen_derived_types.contains (c->ts.u.derived))
9942 : 27571 : || (c->ts.type == BT_CLASS
9943 : 2118 : && seen_derived_types.contains (CLASS_DATA (c)->ts.u.derived));
9944 : :
9945 : 47186 : bool is_pdt_type = c->ts.type == BT_DERIVED
9946 : 23593 : && c->ts.u.derived->attr.pdt_type;
9947 : :
9948 : 23593 : cdecl = c->backend_decl;
9949 : 23593 : ctype = TREE_TYPE (cdecl);
9950 : :
9951 : 23593 : switch (purpose)
9952 : : {
9953 : :
9954 : 3 : case BCAST_ALLOC_COMP:
9955 : :
9956 : 3 : tree ubound;
9957 : 3 : tree cdesc;
9958 : 3 : stmtblock_t derived_type_block;
9959 : :
9960 : 3 : gfc_init_block (&tmpblock);
9961 : :
9962 : 3 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
9963 : : decl, cdecl, NULL_TREE);
9964 : :
9965 : : /* Shortcut to get the attributes of the component. */
9966 : 3 : if (c->ts.type == BT_CLASS)
9967 : : {
9968 : 0 : attr = &CLASS_DATA (c)->attr;
9969 : 0 : if (attr->class_pointer)
9970 : 0 : continue;
9971 : : }
9972 : : else
9973 : : {
9974 : 3 : attr = &c->attr;
9975 : 3 : if (attr->pointer)
9976 : 0 : continue;
9977 : : }
9978 : :
9979 : : /* Do not broadcast a caf_token. These are local to the image. */
9980 : 3 : if (attr->caf_token)
9981 : 1 : continue;
9982 : :
9983 : 2 : add_when_allocated = NULL_TREE;
9984 : 2 : if (cmp_has_alloc_comps
9985 : 0 : && !c->attr.pointer && !c->attr.proc_pointer)
9986 : : {
9987 : 0 : if (c->ts.type == BT_CLASS)
9988 : : {
9989 : 0 : rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
9990 : 0 : add_when_allocated
9991 : 0 : = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
9992 : : comp, NULL_TREE, rank, purpose,
9993 : : caf_mode, args, no_finalization);
9994 : : }
9995 : : else
9996 : : {
9997 : 0 : rank = c->as ? c->as->rank : 0;
9998 : 0 : add_when_allocated = structure_alloc_comps (c->ts.u.derived,
9999 : : comp, NULL_TREE,
10000 : : rank, purpose,
10001 : : caf_mode, args,
10002 : : no_finalization);
10003 : : }
10004 : : }
10005 : :
10006 : 2 : gfc_init_block (&derived_type_block);
10007 : 2 : if (add_when_allocated)
10008 : 0 : gfc_add_expr_to_block (&derived_type_block, add_when_allocated);
10009 : 2 : tmp = gfc_finish_block (&derived_type_block);
10010 : 2 : gfc_add_expr_to_block (&tmpblock, tmp);
10011 : :
10012 : : /* Convert the component into a rank 1 descriptor type. */
10013 : 2 : if (attr->dimension)
10014 : : {
10015 : 0 : tmp = gfc_get_element_type (TREE_TYPE (comp));
10016 : 0 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
10017 : 0 : ubound = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (comp));
10018 : : else
10019 : 0 : ubound = gfc_full_array_size (&tmpblock, comp,
10020 : 0 : c->ts.type == BT_CLASS
10021 : 0 : ? CLASS_DATA (c)->as->rank
10022 : 0 : : c->as->rank);
10023 : : }
10024 : : else
10025 : : {
10026 : 2 : tmp = TREE_TYPE (comp);
10027 : 2 : ubound = build_int_cst (gfc_array_index_type, 1);
10028 : : }
10029 : :
10030 : : /* Treat strings like arrays. Or the other way around, do not
10031 : : * generate an additional array layer for scalar components. */
10032 : 2 : if (attr->dimension || c->ts.type == BT_CHARACTER)
10033 : : {
10034 : 0 : cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
10035 : : &ubound, 1,
10036 : : GFC_ARRAY_ALLOCATABLE, false);
10037 : :
10038 : 0 : cdesc = gfc_create_var (cdesc, "cdesc");
10039 : 0 : DECL_ARTIFICIAL (cdesc) = 1;
10040 : :
10041 : 0 : gfc_add_modify (&tmpblock, gfc_conv_descriptor_dtype (cdesc),
10042 : : gfc_get_dtype_rank_type (1, tmp));
10043 : 0 : gfc_conv_descriptor_lbound_set (&tmpblock, cdesc,
10044 : : gfc_index_zero_node,
10045 : : gfc_index_one_node);
10046 : 0 : gfc_conv_descriptor_stride_set (&tmpblock, cdesc,
10047 : : gfc_index_zero_node,
10048 : : gfc_index_one_node);
10049 : 0 : gfc_conv_descriptor_ubound_set (&tmpblock, cdesc,
10050 : : gfc_index_zero_node, ubound);
10051 : : }
10052 : : else
10053 : : /* Prevent warning. */
10054 : : cdesc = NULL_TREE;
10055 : :
10056 : 2 : if (attr->dimension)
10057 : : {
10058 : 0 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
10059 : 0 : comp = gfc_conv_descriptor_data_get (comp);
10060 : : else
10061 : 0 : comp = gfc_build_addr_expr (NULL_TREE, comp);
10062 : : }
10063 : : else
10064 : : {
10065 : 2 : gfc_se se;
10066 : :
10067 : 2 : gfc_init_se (&se, NULL);
10068 : :
10069 : 2 : comp = gfc_conv_scalar_to_descriptor (&se, comp,
10070 : 2 : c->ts.type == BT_CLASS
10071 : 2 : ? CLASS_DATA (c)->attr
10072 : : : c->attr);
10073 : 2 : if (c->ts.type == BT_CHARACTER)
10074 : 0 : comp = gfc_build_addr_expr (NULL_TREE, comp);
10075 : 2 : gfc_add_block_to_block (&tmpblock, &se.pre);
10076 : : }
10077 : :
10078 : 2 : if (attr->dimension || c->ts.type == BT_CHARACTER)
10079 : 0 : gfc_conv_descriptor_data_set (&tmpblock, cdesc, comp);
10080 : : else
10081 : 2 : cdesc = comp;
10082 : :
10083 : 2 : tree fndecl;
10084 : :
10085 : 2 : fndecl = build_call_expr_loc (input_location,
10086 : : gfor_fndecl_co_broadcast, 5,
10087 : : gfc_build_addr_expr (pvoid_type_node,cdesc),
10088 : : args->image_index,
10089 : : null_pointer_node, null_pointer_node,
10090 : : null_pointer_node);
10091 : :
10092 : 2 : gfc_add_expr_to_block (&tmpblock, fndecl);
10093 : 2 : gfc_add_block_to_block (&fnblock, &tmpblock);
10094 : :
10095 : 19792 : break;
10096 : :
10097 : 9747 : case DEALLOCATE_ALLOC_COMP:
10098 : :
10099 : 9747 : gfc_init_block (&tmpblock);
10100 : :
10101 : 9747 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10102 : : decl, cdecl, NULL_TREE);
10103 : :
10104 : : /* Shortcut to get the attributes of the component. */
10105 : 9747 : if (c->ts.type == BT_CLASS)
10106 : : {
10107 : 960 : attr = &CLASS_DATA (c)->attr;
10108 : 960 : if (attr->class_pointer || c->attr.proc_pointer)
10109 : 18 : continue;
10110 : : }
10111 : : else
10112 : : {
10113 : 8787 : attr = &c->attr;
10114 : 8787 : if (attr->pointer || attr->proc_pointer)
10115 : 130 : continue;
10116 : : }
10117 : :
10118 : 9599 : if (!no_finalization && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
10119 : 7434 : || (c->ts.type == BT_CLASS && !CLASS_DATA (c)->attr.class_pointer)))
10120 : : /* Call the finalizer, which will free the memory and nullify the
10121 : : pointer of an array. */
10122 : 2725 : deallocate_called = gfc_add_comp_finalizer_call (&tmpblock, comp, c,
10123 : 2725 : caf_enabled (caf_mode))
10124 : 2725 : && attr->dimension;
10125 : : else
10126 : : deallocate_called = false;
10127 : :
10128 : : /* Add the _class ref for classes. */
10129 : 9599 : if (c->ts.type == BT_CLASS && attr->allocatable)
10130 : 942 : comp = gfc_class_data_get (comp);
10131 : :
10132 : 9599 : add_when_allocated = NULL_TREE;
10133 : 9599 : if (cmp_has_alloc_comps
10134 : 1928 : && !c->attr.pointer && !c->attr.proc_pointer
10135 : : && !same_type
10136 : 1928 : && !deallocate_called)
10137 : : {
10138 : : /* Add checked deallocation of the components. This code is
10139 : : obviously added because the finalizer is not trusted to free
10140 : : all memory. */
10141 : 1264 : if (c->ts.type == BT_CLASS)
10142 : : {
10143 : 241 : rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
10144 : 241 : add_when_allocated
10145 : 241 : = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
10146 : : comp, NULL_TREE, rank, purpose,
10147 : : caf_mode, args, no_finalization);
10148 : : }
10149 : : else
10150 : : {
10151 : 1023 : rank = c->as ? c->as->rank : 0;
10152 : 1023 : add_when_allocated = structure_alloc_comps (c->ts.u.derived,
10153 : : comp, NULL_TREE,
10154 : : rank, purpose,
10155 : : caf_mode, args,
10156 : : no_finalization);
10157 : : }
10158 : : }
10159 : :
10160 : 6641 : if (attr->allocatable && !same_type
10161 : 15756 : && (!attr->codimension || caf_enabled (caf_mode)))
10162 : : {
10163 : : /* Handle all types of components besides components of the
10164 : : same_type as the current one, because those would create an
10165 : : endless loop. */
10166 : 45 : caf_dereg_mode = (caf_in_coarray (caf_mode)
10167 : 51 : && (attr->dimension || c->caf_token))
10168 : 6101 : || attr->codimension
10169 : 6222 : ? (gfc_caf_is_dealloc_only (caf_mode)
10170 : : ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
10171 : : : GFC_CAF_COARRAY_DEREGISTER)
10172 : : : GFC_CAF_COARRAY_NOCOARRAY;
10173 : :
10174 : 6151 : caf_token = NULL_TREE;
10175 : : /* Coarray components are handled directly by
10176 : : deallocate_with_status. */
10177 : 6151 : if (!attr->codimension
10178 : 6130 : && caf_dereg_mode != GFC_CAF_COARRAY_NOCOARRAY)
10179 : : {
10180 : 50 : if (c->caf_token)
10181 : 16 : caf_token
10182 : 16 : = fold_build3_loc (input_location, COMPONENT_REF,
10183 : 16 : TREE_TYPE (gfc_comp_caf_token (c)),
10184 : : decl, gfc_comp_caf_token (c),
10185 : : NULL_TREE);
10186 : 34 : else if (attr->dimension && !attr->proc_pointer)
10187 : 34 : caf_token = gfc_conv_descriptor_token (comp);
10188 : : }
10189 : :
10190 : 6151 : tmp = gfc_deallocate_with_status (comp, NULL_TREE, NULL_TREE,
10191 : : NULL_TREE, NULL_TREE, true,
10192 : : NULL, caf_dereg_mode, NULL_TREE,
10193 : : add_when_allocated, caf_token);
10194 : :
10195 : 6151 : gfc_add_expr_to_block (&tmpblock, tmp);
10196 : : }
10197 : 3448 : else if (attr->allocatable && !attr->codimension
10198 : 484 : && !deallocate_called)
10199 : : {
10200 : : /* Case of recursive allocatable derived types. */
10201 : 484 : tree is_allocated;
10202 : 484 : tree ubound;
10203 : 484 : tree cdesc;
10204 : 484 : stmtblock_t dealloc_block;
10205 : :
10206 : 484 : gfc_init_block (&dealloc_block);
10207 : 484 : if (add_when_allocated)
10208 : 0 : gfc_add_expr_to_block (&dealloc_block, add_when_allocated);
10209 : :
10210 : : /* Convert the component into a rank 1 descriptor type. */
10211 : 484 : if (attr->dimension)
10212 : : {
10213 : 52 : tmp = gfc_get_element_type (TREE_TYPE (comp));
10214 : 52 : ubound = gfc_full_array_size (&dealloc_block, comp,
10215 : 52 : c->ts.type == BT_CLASS
10216 : 0 : ? CLASS_DATA (c)->as->rank
10217 : 52 : : c->as->rank);
10218 : : }
10219 : : else
10220 : : {
10221 : 432 : tmp = TREE_TYPE (comp);
10222 : 432 : ubound = build_int_cst (gfc_array_index_type, 1);
10223 : : }
10224 : :
10225 : 484 : cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
10226 : : &ubound, 1,
10227 : : GFC_ARRAY_ALLOCATABLE, false);
10228 : :
10229 : 484 : cdesc = gfc_create_var (cdesc, "cdesc");
10230 : 484 : DECL_ARTIFICIAL (cdesc) = 1;
10231 : :
10232 : 484 : gfc_add_modify (&dealloc_block, gfc_conv_descriptor_dtype (cdesc),
10233 : : gfc_get_dtype_rank_type (1, tmp));
10234 : 484 : gfc_conv_descriptor_lbound_set (&dealloc_block, cdesc,
10235 : : gfc_index_zero_node,
10236 : : gfc_index_one_node);
10237 : 484 : gfc_conv_descriptor_stride_set (&dealloc_block, cdesc,
10238 : : gfc_index_zero_node,
10239 : : gfc_index_one_node);
10240 : 484 : gfc_conv_descriptor_ubound_set (&dealloc_block, cdesc,
10241 : : gfc_index_zero_node, ubound);
10242 : :
10243 : 484 : if (attr->dimension)
10244 : 52 : comp = gfc_conv_descriptor_data_get (comp);
10245 : :
10246 : 484 : gfc_conv_descriptor_data_set (&dealloc_block, cdesc, comp);
10247 : :
10248 : : /* Now call the deallocator. */
10249 : 484 : vtab = gfc_find_vtab (&c->ts);
10250 : 484 : if (vtab->backend_decl == NULL)
10251 : 28 : gfc_get_symbol_decl (vtab);
10252 : 484 : tmp = gfc_build_addr_expr (NULL_TREE, vtab->backend_decl);
10253 : 484 : dealloc_fndecl = gfc_vptr_deallocate_get (tmp);
10254 : 484 : dealloc_fndecl = build_fold_indirect_ref_loc (input_location,
10255 : : dealloc_fndecl);
10256 : 484 : tmp = build_int_cst (TREE_TYPE (comp), 0);
10257 : 484 : is_allocated = fold_build2_loc (input_location, NE_EXPR,
10258 : : logical_type_node, tmp,
10259 : : comp);
10260 : 484 : cdesc = gfc_build_addr_expr (NULL_TREE, cdesc);
10261 : :
10262 : 484 : tmp = build_call_expr_loc (input_location,
10263 : : dealloc_fndecl, 1,
10264 : : cdesc);
10265 : 484 : gfc_add_expr_to_block (&dealloc_block, tmp);
10266 : :
10267 : 484 : tmp = gfc_finish_block (&dealloc_block);
10268 : :
10269 : 484 : tmp = fold_build3_loc (input_location, COND_EXPR,
10270 : : void_type_node, is_allocated, tmp,
10271 : : build_empty_stmt (input_location));
10272 : :
10273 : 484 : gfc_add_expr_to_block (&tmpblock, tmp);
10274 : 484 : }
10275 : 2964 : else if (add_when_allocated)
10276 : 525 : gfc_add_expr_to_block (&tmpblock, add_when_allocated);
10277 : :
10278 : 942 : if (c->ts.type == BT_CLASS && attr->allocatable
10279 : 10541 : && (!attr->codimension || !caf_enabled (caf_mode)))
10280 : : {
10281 : : /* Finally, reset the vptr to the declared type vtable and, if
10282 : : necessary reset the _len field.
10283 : :
10284 : : First recover the reference to the component and obtain
10285 : : the vptr. */
10286 : 927 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10287 : : decl, cdecl, NULL_TREE);
10288 : 927 : tmp = gfc_class_vptr_get (comp);
10289 : :
10290 : 927 : if (UNLIMITED_POLY (c))
10291 : : {
10292 : : /* Both vptr and _len field should be nulled. */
10293 : 175 : gfc_add_modify (&tmpblock, tmp,
10294 : 175 : build_int_cst (TREE_TYPE (tmp), 0));
10295 : 175 : tmp = gfc_class_len_get (comp);
10296 : 175 : gfc_add_modify (&tmpblock, tmp,
10297 : 175 : build_int_cst (TREE_TYPE (tmp), 0));
10298 : : }
10299 : : else
10300 : : {
10301 : : /* Build the vtable address and set the vptr with it. */
10302 : 752 : gfc_reset_vptr (&tmpblock, nullptr, tmp, c->ts.u.derived);
10303 : : }
10304 : : }
10305 : :
10306 : : /* Now add the deallocation of this component. */
10307 : 9599 : gfc_add_block_to_block (&fnblock, &tmpblock);
10308 : 9599 : break;
10309 : :
10310 : 3904 : case NULLIFY_ALLOC_COMP:
10311 : : /* Nullify
10312 : : - allocatable components (regular or in class)
10313 : : - components that have allocatable components
10314 : : - pointer components when in a coarray.
10315 : : Skip everything else especially proc_pointers, which may come
10316 : : coupled with the regular pointer attribute. */
10317 : 5155 : if (c->attr.proc_pointer
10318 : 3904 : || !(c->attr.allocatable || (c->ts.type == BT_CLASS
10319 : 412 : && CLASS_DATA (c)->attr.allocatable)
10320 : 1569 : || (cmp_has_alloc_comps
10321 : 234 : && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
10322 : 18 : || (c->ts.type == BT_CLASS
10323 : 12 : && !CLASS_DATA (c)->attr.class_pointer)))
10324 : 1353 : || (caf_in_coarray (caf_mode) && c->attr.pointer)))
10325 : 1251 : continue;
10326 : :
10327 : : /* Process class components first, because they always have the
10328 : : pointer-attribute set which would be caught wrong else. */
10329 : 2653 : if (c->ts.type == BT_CLASS
10330 : 399 : && (CLASS_DATA (c)->attr.allocatable
10331 : 0 : || CLASS_DATA (c)->attr.class_pointer))
10332 : : {
10333 : 399 : tree class_ref;
10334 : :
10335 : : /* Allocatable CLASS components. */
10336 : 399 : class_ref = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10337 : : decl, cdecl, NULL_TREE);
10338 : :
10339 : 399 : comp = gfc_class_data_get (class_ref);
10340 : 399 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
10341 : 215 : gfc_conv_descriptor_data_set (&fnblock, comp,
10342 : : null_pointer_node);
10343 : : else
10344 : : {
10345 : 184 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
10346 : : void_type_node, comp,
10347 : 184 : build_int_cst (TREE_TYPE (comp), 0));
10348 : 184 : gfc_add_expr_to_block (&fnblock, tmp);
10349 : : }
10350 : :
10351 : : /* The dynamic type of a disassociated pointer or unallocated
10352 : : allocatable variable is its declared type. An unlimited
10353 : : polymorphic entity has no declared type. */
10354 : 399 : gfc_reset_vptr (&fnblock, nullptr, class_ref, c->ts.u.derived);
10355 : :
10356 : 399 : cmp_has_alloc_comps = false;
10357 : 399 : }
10358 : : /* Coarrays need the component to be nulled before the api-call
10359 : : is made. */
10360 : 2254 : else if (c->attr.pointer || c->attr.allocatable)
10361 : : {
10362 : 2038 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10363 : : decl, cdecl, NULL_TREE);
10364 : 2038 : if (c->attr.dimension || c->attr.codimension)
10365 : 1440 : gfc_conv_descriptor_data_set (&fnblock, comp,
10366 : : null_pointer_node);
10367 : : else
10368 : 598 : gfc_add_modify (&fnblock, comp,
10369 : 598 : build_int_cst (TREE_TYPE (comp), 0));
10370 : 2038 : if (gfc_deferred_strlen (c, &comp))
10371 : : {
10372 : 250 : comp = fold_build3_loc (input_location, COMPONENT_REF,
10373 : 250 : TREE_TYPE (comp),
10374 : : decl, comp, NULL_TREE);
10375 : 500 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
10376 : 250 : TREE_TYPE (comp), comp,
10377 : 250 : build_int_cst (TREE_TYPE (comp), 0));
10378 : 250 : gfc_add_expr_to_block (&fnblock, tmp);
10379 : : }
10380 : : cmp_has_alloc_comps = false;
10381 : : }
10382 : :
10383 : 2653 : if (flag_coarray == GFC_FCOARRAY_LIB && caf_in_coarray (caf_mode))
10384 : : {
10385 : : /* Register a component of a derived type coarray with the
10386 : : coarray library. Do not register ultimate component
10387 : : coarrays here. They are treated like regular coarrays and
10388 : : are either allocated on all images or on none. */
10389 : 126 : tree token;
10390 : :
10391 : 126 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10392 : : decl, cdecl, NULL_TREE);
10393 : 126 : if (c->attr.dimension)
10394 : : {
10395 : : /* Set the dtype, because caf_register needs it. */
10396 : 100 : gfc_add_modify (&fnblock, gfc_conv_descriptor_dtype (comp),
10397 : 100 : gfc_get_dtype (TREE_TYPE (comp)));
10398 : 100 : tmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10399 : : decl, cdecl, NULL_TREE);
10400 : 100 : token = gfc_conv_descriptor_token (tmp);
10401 : : }
10402 : : else
10403 : : {
10404 : 26 : gfc_se se;
10405 : :
10406 : 26 : gfc_init_se (&se, NULL);
10407 : 52 : token = fold_build3_loc (input_location, COMPONENT_REF,
10408 : : pvoid_type_node, decl,
10409 : 26 : gfc_comp_caf_token (c), NULL_TREE);
10410 : 26 : comp = gfc_conv_scalar_to_descriptor (&se, comp,
10411 : 26 : c->ts.type == BT_CLASS
10412 : 26 : ? CLASS_DATA (c)->attr
10413 : : : c->attr);
10414 : 26 : gfc_add_block_to_block (&fnblock, &se.pre);
10415 : : }
10416 : :
10417 : 126 : gfc_allocate_using_caf_lib (&fnblock, comp, size_zero_node,
10418 : : gfc_build_addr_expr (NULL_TREE,
10419 : : token),
10420 : : NULL_TREE, NULL_TREE, NULL_TREE,
10421 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
10422 : : }
10423 : :
10424 : 2653 : if (cmp_has_alloc_comps)
10425 : : {
10426 : 216 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10427 : : decl, cdecl, NULL_TREE);
10428 : 216 : rank = c->as ? c->as->rank : 0;
10429 : 216 : tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE,
10430 : : rank, purpose, caf_mode, args,
10431 : : no_finalization);
10432 : 216 : gfc_add_expr_to_block (&fnblock, tmp);
10433 : : }
10434 : : break;
10435 : :
10436 : 29 : case REASSIGN_CAF_COMP:
10437 : 29 : if (caf_enabled (caf_mode)
10438 : 29 : && (c->attr.codimension
10439 : 23 : || (c->ts.type == BT_CLASS
10440 : 2 : && (CLASS_DATA (c)->attr.coarray_comp
10441 : 2 : || caf_in_coarray (caf_mode)))
10442 : 21 : || (c->ts.type == BT_DERIVED
10443 : 7 : && (c->ts.u.derived->attr.coarray_comp
10444 : 6 : || caf_in_coarray (caf_mode))))
10445 : 44 : && !same_type)
10446 : : {
10447 : 13 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10448 : : decl, cdecl, NULL_TREE);
10449 : 13 : dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10450 : : dest, cdecl, NULL_TREE);
10451 : :
10452 : 13 : if (c->attr.codimension)
10453 : : {
10454 : 6 : if (c->ts.type == BT_CLASS)
10455 : : {
10456 : 0 : comp = gfc_class_data_get (comp);
10457 : 0 : dcmp = gfc_class_data_get (dcmp);
10458 : : }
10459 : 6 : gfc_conv_descriptor_data_set (&fnblock, dcmp,
10460 : : gfc_conv_descriptor_data_get (comp));
10461 : : }
10462 : : else
10463 : : {
10464 : 7 : tmp = structure_alloc_comps (c->ts.u.derived, comp, dcmp,
10465 : : rank, purpose, caf_mode
10466 : : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY,
10467 : : args, no_finalization);
10468 : 7 : gfc_add_expr_to_block (&fnblock, tmp);
10469 : : }
10470 : : }
10471 : : break;
10472 : :
10473 : 7767 : case COPY_ALLOC_COMP:
10474 : 7767 : if (c->attr.pointer || c->attr.proc_pointer)
10475 : 158 : continue;
10476 : :
10477 : : /* We need source and destination components. */
10478 : 7609 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype, decl,
10479 : : cdecl, NULL_TREE);
10480 : 7609 : dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype, dest,
10481 : : cdecl, NULL_TREE);
10482 : 7609 : dcmp = fold_convert (TREE_TYPE (comp), dcmp);
10483 : :
10484 : 7609 : if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
10485 : : {
10486 : 716 : tree ftn_tree;
10487 : 716 : tree size;
10488 : 716 : tree dst_data;
10489 : 716 : tree src_data;
10490 : 716 : tree null_data;
10491 : :
10492 : 716 : dst_data = gfc_class_data_get (dcmp);
10493 : 716 : src_data = gfc_class_data_get (comp);
10494 : 716 : size = fold_convert (size_type_node,
10495 : : gfc_class_vtab_size_get (comp));
10496 : :
10497 : 716 : if (CLASS_DATA (c)->attr.dimension)
10498 : : {
10499 : 704 : nelems = gfc_conv_descriptor_size (src_data,
10500 : 352 : CLASS_DATA (c)->as->rank);
10501 : 352 : size = fold_build2_loc (input_location, MULT_EXPR,
10502 : : size_type_node, size,
10503 : : fold_convert (size_type_node,
10504 : : nelems));
10505 : : }
10506 : : else
10507 : 364 : nelems = build_int_cst (size_type_node, 1);
10508 : :
10509 : 716 : if (CLASS_DATA (c)->attr.dimension
10510 : 364 : || CLASS_DATA (c)->attr.codimension)
10511 : : {
10512 : 360 : src_data = gfc_conv_descriptor_data_get (src_data);
10513 : 360 : dst_data = gfc_conv_descriptor_data_get (dst_data);
10514 : : }
10515 : :
10516 : 716 : gfc_init_block (&tmpblock);
10517 : :
10518 : 716 : gfc_add_modify (&tmpblock, gfc_class_vptr_get (dcmp),
10519 : : gfc_class_vptr_get (comp));
10520 : :
10521 : : /* Copy the unlimited '_len' field. If it is greater than zero
10522 : : (ie. a character(_len)), multiply it by size and use this
10523 : : for the malloc call. */
10524 : 716 : if (UNLIMITED_POLY (c))
10525 : : {
10526 : 135 : gfc_add_modify (&tmpblock, gfc_class_len_get (dcmp),
10527 : : gfc_class_len_get (comp));
10528 : 135 : size = gfc_resize_class_size_with_len (&tmpblock, comp, size);
10529 : : }
10530 : :
10531 : : /* Coarray component have to have the same allocation status and
10532 : : shape/type-parameter/effective-type on the LHS and RHS of an
10533 : : intrinsic assignment. Hence, we did not deallocated them - and
10534 : : do not allocate them here. */
10535 : 716 : if (!CLASS_DATA (c)->attr.codimension)
10536 : : {
10537 : 701 : ftn_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
10538 : 701 : tmp = build_call_expr_loc (input_location, ftn_tree, 1, size);
10539 : 701 : gfc_add_modify (&tmpblock, dst_data,
10540 : 701 : fold_convert (TREE_TYPE (dst_data), tmp));
10541 : : }
10542 : :
10543 : 1417 : tmp = gfc_copy_class_to_class (comp, dcmp, nelems,
10544 : 716 : UNLIMITED_POLY (c));
10545 : 716 : gfc_add_expr_to_block (&tmpblock, tmp);
10546 : 716 : tmp = gfc_finish_block (&tmpblock);
10547 : :
10548 : 716 : gfc_init_block (&tmpblock);
10549 : 716 : gfc_add_modify (&tmpblock, dst_data,
10550 : 716 : fold_convert (TREE_TYPE (dst_data),
10551 : : null_pointer_node));
10552 : 716 : null_data = gfc_finish_block (&tmpblock);
10553 : :
10554 : 716 : null_cond = fold_build2_loc (input_location, NE_EXPR,
10555 : : logical_type_node, src_data,
10556 : : null_pointer_node);
10557 : :
10558 : 716 : gfc_add_expr_to_block (&fnblock, build3_v (COND_EXPR, null_cond,
10559 : : tmp, null_data));
10560 : 716 : continue;
10561 : 716 : }
10562 : :
10563 : : /* To implement guarded deep copy, i.e., deep copy only allocatable
10564 : : components that are really allocated, the deep copy code has to
10565 : : be generated first and then added to the if-block in
10566 : : gfc_duplicate_allocatable (). */
10567 : 6893 : if (cmp_has_alloc_comps && !c->attr.proc_pointer && !same_type)
10568 : : {
10569 : 1126 : rank = c->as ? c->as->rank : 0;
10570 : 1126 : tmp = fold_convert (TREE_TYPE (dcmp), comp);
10571 : 1126 : gfc_add_modify (&fnblock, dcmp, tmp);
10572 : 1126 : add_when_allocated = structure_alloc_comps (c->ts.u.derived,
10573 : : comp, dcmp,
10574 : : rank, purpose,
10575 : : caf_mode, args,
10576 : : no_finalization);
10577 : : }
10578 : : else
10579 : : add_when_allocated = NULL_TREE;
10580 : :
10581 : 6893 : if (gfc_deferred_strlen (c, &tmp))
10582 : : {
10583 : 337 : tree len, size;
10584 : 337 : len = tmp;
10585 : 337 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
10586 : 337 : TREE_TYPE (len),
10587 : : decl, len, NULL_TREE);
10588 : 337 : len = fold_build3_loc (input_location, COMPONENT_REF,
10589 : 337 : TREE_TYPE (len),
10590 : : dest, len, NULL_TREE);
10591 : 337 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
10592 : 337 : TREE_TYPE (len), len, tmp);
10593 : 337 : gfc_add_expr_to_block (&fnblock, tmp);
10594 : 337 : size = size_of_string_in_bytes (c->ts.kind, len);
10595 : : /* This component cannot have allocatable components,
10596 : : therefore add_when_allocated of duplicate_allocatable ()
10597 : : is always NULL. */
10598 : 337 : rank = c->as ? c->as->rank : 0;
10599 : 337 : tmp = duplicate_allocatable (dcmp, comp, ctype, rank,
10600 : : false, false, size, NULL_TREE);
10601 : 337 : gfc_add_expr_to_block (&fnblock, tmp);
10602 : : }
10603 : 6556 : else if (c->attr.pdt_array)
10604 : : {
10605 : 31 : tmp = duplicate_allocatable (dcmp, comp, ctype,
10606 : 31 : c->as ? c->as->rank : 0,
10607 : : false, false, NULL_TREE, NULL_TREE);
10608 : 31 : gfc_add_expr_to_block (&fnblock, tmp);
10609 : : }
10610 : 3675 : else if (c->attr.allocatable && !c->attr.proc_pointer
10611 : 10200 : && (!(cmp_has_alloc_comps && c->as) || c->attr.codimension
10612 : 319 : || caf_in_coarray (caf_mode)))
10613 : : {
10614 : 3360 : rank = c->as ? c->as->rank : 0;
10615 : 3360 : if (c->attr.codimension)
10616 : 15 : tmp = gfc_copy_allocatable_data (dcmp, comp, ctype, rank);
10617 : 3345 : else if (flag_coarray == GFC_FCOARRAY_LIB
10618 : 3345 : && caf_in_coarray (caf_mode))
10619 : : {
10620 : 55 : tree dst_tok;
10621 : 55 : if (c->as)
10622 : 40 : dst_tok = gfc_conv_descriptor_token (dcmp);
10623 : : else
10624 : : {
10625 : 15 : dst_tok
10626 : 15 : = fold_build3_loc (input_location, COMPONENT_REF,
10627 : : pvoid_type_node, dest,
10628 : 15 : gfc_comp_caf_token (c), NULL_TREE);
10629 : : }
10630 : 55 : tmp
10631 : 55 : = duplicate_allocatable_coarray (dcmp, dst_tok, comp, ctype,
10632 : : rank, add_when_allocated);
10633 : : }
10634 : : else
10635 : 3290 : tmp = gfc_duplicate_allocatable (dcmp, comp, ctype, rank,
10636 : : add_when_allocated);
10637 : 3360 : gfc_add_expr_to_block (&fnblock, tmp);
10638 : : }
10639 : : else
10640 : 3165 : if (cmp_has_alloc_comps || is_pdt_type)
10641 : 1099 : gfc_add_expr_to_block (&fnblock, add_when_allocated);
10642 : :
10643 : : break;
10644 : :
10645 : 900 : case ALLOCATE_PDT_COMP:
10646 : :
10647 : 900 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10648 : : decl, cdecl, NULL_TREE);
10649 : :
10650 : : /* Set the PDT KIND and LEN fields. */
10651 : 900 : if (c->attr.pdt_kind || c->attr.pdt_len)
10652 : : {
10653 : 439 : gfc_se tse;
10654 : 439 : gfc_expr *c_expr = NULL;
10655 : 439 : gfc_actual_arglist *param = pdt_param_list;
10656 : 439 : gfc_init_se (&tse, NULL);
10657 : 1767 : for (; param; param = param->next)
10658 : 889 : if (param->name && !strcmp (c->name, param->name))
10659 : 415 : c_expr = param->expr;
10660 : :
10661 : 439 : if (!c_expr)
10662 : 24 : c_expr = c->initializer;
10663 : :
10664 : 24 : if (c_expr)
10665 : : {
10666 : 439 : gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
10667 : 439 : gfc_add_modify (&fnblock, comp, tse.expr);
10668 : : }
10669 : : }
10670 : :
10671 : 900 : if (c->attr.pdt_string)
10672 : : {
10673 : 72 : gfc_se tse;
10674 : 72 : gfc_init_se (&tse, NULL);
10675 : 72 : tree strlen = NULL_TREE;
10676 : 72 : gfc_expr *e = gfc_copy_expr (c->ts.u.cl->length);
10677 : : /* Convert the parameterized string length to its value. The
10678 : : string length is stored in a hidden field in the same way as
10679 : : deferred string lengths. */
10680 : 72 : gfc_insert_parameter_exprs (e, pdt_param_list);
10681 : 72 : if (gfc_deferred_strlen (c, &strlen) && strlen != NULL_TREE)
10682 : : {
10683 : 72 : gfc_conv_expr_type (&tse, e,
10684 : 72 : TREE_TYPE (strlen));
10685 : 72 : strlen = fold_build3_loc (input_location, COMPONENT_REF,
10686 : 72 : TREE_TYPE (strlen),
10687 : : decl, strlen, NULL_TREE);
10688 : 72 : gfc_add_modify (&fnblock, strlen, tse.expr);
10689 : 72 : c->ts.u.cl->backend_decl = strlen;
10690 : : }
10691 : 72 : gfc_free_expr (e);
10692 : :
10693 : : /* Scalar parameterized strings can be allocated now. */
10694 : 72 : if (!c->as)
10695 : : {
10696 : 72 : tmp = fold_convert (gfc_array_index_type, strlen);
10697 : 72 : tmp = size_of_string_in_bytes (c->ts.kind, tmp);
10698 : 72 : tmp = gfc_evaluate_now (tmp, &fnblock);
10699 : 72 : tmp = gfc_call_malloc (&fnblock, TREE_TYPE (comp), tmp);
10700 : 72 : gfc_add_modify (&fnblock, comp, tmp);
10701 : : }
10702 : : }
10703 : :
10704 : : /* Allocate parameterized arrays of parameterized derived types. */
10705 : 900 : if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
10706 : 735 : && !((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10707 : 62 : && (c->ts.u.derived && c->ts.u.derived->attr.pdt_type)))
10708 : 673 : continue;
10709 : :
10710 : 227 : if (c->ts.type == BT_CLASS)
10711 : 0 : comp = gfc_class_data_get (comp);
10712 : :
10713 : 227 : if (c->attr.pdt_array)
10714 : : {
10715 : 165 : gfc_se tse;
10716 : 165 : int i;
10717 : 165 : tree size = gfc_index_one_node;
10718 : 165 : tree offset = gfc_index_zero_node;
10719 : 165 : tree lower, upper;
10720 : 165 : gfc_expr *e;
10721 : :
10722 : : /* This chunk takes the expressions for 'lower' and 'upper'
10723 : : in the arrayspec and substitutes in the expressions for
10724 : : the parameters from 'pdt_param_list'. The descriptor
10725 : : fields can then be filled from the values so obtained. */
10726 : 165 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)));
10727 : 408 : for (i = 0; i < c->as->rank; i++)
10728 : : {
10729 : 243 : gfc_init_se (&tse, NULL);
10730 : 243 : e = gfc_copy_expr (c->as->lower[i]);
10731 : 243 : gfc_insert_parameter_exprs (e, pdt_param_list);
10732 : 243 : gfc_conv_expr_type (&tse, e, gfc_array_index_type);
10733 : 243 : gfc_free_expr (e);
10734 : 243 : lower = tse.expr;
10735 : 243 : gfc_conv_descriptor_lbound_set (&fnblock, comp,
10736 : : gfc_rank_cst[i],
10737 : : lower);
10738 : 243 : e = gfc_copy_expr (c->as->upper[i]);
10739 : 243 : gfc_insert_parameter_exprs (e, pdt_param_list);
10740 : 243 : gfc_conv_expr_type (&tse, e, gfc_array_index_type);
10741 : 243 : gfc_free_expr (e);
10742 : 243 : upper = tse.expr;
10743 : 243 : gfc_conv_descriptor_ubound_set (&fnblock, comp,
10744 : : gfc_rank_cst[i],
10745 : : upper);
10746 : 243 : gfc_conv_descriptor_stride_set (&fnblock, comp,
10747 : : gfc_rank_cst[i],
10748 : : size);
10749 : 243 : size = gfc_evaluate_now (size, &fnblock);
10750 : 243 : offset = fold_build2_loc (input_location,
10751 : : MINUS_EXPR,
10752 : : gfc_array_index_type,
10753 : : offset, size);
10754 : 243 : offset = gfc_evaluate_now (offset, &fnblock);
10755 : 243 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
10756 : : gfc_array_index_type,
10757 : : upper, lower);
10758 : 243 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
10759 : : gfc_array_index_type,
10760 : : tmp, gfc_index_one_node);
10761 : 243 : size = fold_build2_loc (input_location, MULT_EXPR,
10762 : : gfc_array_index_type, size, tmp);
10763 : : }
10764 : 165 : gfc_conv_descriptor_offset_set (&fnblock, comp, offset);
10765 : 165 : if (c->ts.type == BT_CLASS)
10766 : : {
10767 : 0 : tmp = gfc_get_vptr_from_expr (comp);
10768 : 0 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
10769 : 0 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
10770 : 0 : tmp = gfc_vptr_size_get (tmp);
10771 : : }
10772 : : else
10773 : 165 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (ctype));
10774 : 165 : tmp = fold_convert (gfc_array_index_type, tmp);
10775 : 165 : size = fold_build2_loc (input_location, MULT_EXPR,
10776 : : gfc_array_index_type, size, tmp);
10777 : 165 : size = gfc_evaluate_now (size, &fnblock);
10778 : 165 : tmp = gfc_call_malloc (&fnblock, NULL, size);
10779 : 165 : gfc_conv_descriptor_data_set (&fnblock, comp, tmp);
10780 : 165 : tmp = gfc_conv_descriptor_dtype (comp);
10781 : 165 : gfc_add_modify (&fnblock, tmp, gfc_get_dtype (ctype));
10782 : :
10783 : 165 : if (c->initializer && c->initializer->rank)
10784 : : {
10785 : 48 : gfc_init_se (&tse, NULL);
10786 : 48 : e = gfc_copy_expr (c->initializer);
10787 : 48 : gfc_insert_parameter_exprs (e, pdt_param_list);
10788 : 48 : gfc_conv_expr_descriptor (&tse, e);
10789 : 48 : gfc_add_block_to_block (&fnblock, &tse.pre);
10790 : 48 : gfc_free_expr (e);
10791 : 48 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
10792 : 48 : tmp = build_call_expr_loc (input_location, tmp, 3,
10793 : : gfc_conv_descriptor_data_get (comp),
10794 : : gfc_conv_descriptor_data_get (tse.expr),
10795 : : fold_convert (size_type_node, size));
10796 : 48 : gfc_add_expr_to_block (&fnblock, tmp);
10797 : 48 : gfc_add_block_to_block (&fnblock, &tse.post);
10798 : : }
10799 : : }
10800 : :
10801 : : /* Recurse in to PDT components. */
10802 : 227 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10803 : 74 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
10804 : 74 : && !(c->attr.pointer || c->attr.allocatable))
10805 : : {
10806 : 48 : bool is_deferred = false;
10807 : 48 : gfc_actual_arglist *tail = c->param_list;
10808 : :
10809 : 126 : for (; tail; tail = tail->next)
10810 : 78 : if (!tail->expr)
10811 : 24 : is_deferred = true;
10812 : :
10813 : 48 : tail = is_deferred ? pdt_param_list : c->param_list;
10814 : 48 : tmp = gfc_allocate_pdt_comp (c->ts.u.derived, comp,
10815 : 48 : c->as ? c->as->rank : 0,
10816 : : tail);
10817 : 48 : gfc_add_expr_to_block (&fnblock, tmp);
10818 : : }
10819 : :
10820 : : break;
10821 : :
10822 : 979 : case DEALLOCATE_PDT_COMP:
10823 : : /* Deallocate array or parameterized string length components
10824 : : of parameterized derived types. */
10825 : 979 : if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
10826 : 797 : && !c->attr.pdt_string
10827 : 731 : && !((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10828 : 91 : && (c->ts.u.derived && c->ts.u.derived->attr.pdt_type)))
10829 : 640 : continue;
10830 : :
10831 : 339 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10832 : : decl, cdecl, NULL_TREE);
10833 : 339 : if (c->ts.type == BT_CLASS)
10834 : 0 : comp = gfc_class_data_get (comp);
10835 : :
10836 : : /* Recurse in to PDT components. */
10837 : 339 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10838 : 97 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
10839 : 97 : && (!c->attr.pointer && !c->attr.allocatable))
10840 : : {
10841 : 42 : tmp = gfc_deallocate_pdt_comp (c->ts.u.derived, comp,
10842 : 42 : c->as ? c->as->rank : 0);
10843 : 42 : gfc_add_expr_to_block (&fnblock, tmp);
10844 : : }
10845 : :
10846 : 339 : if (c->attr.pdt_array || c->attr.pdt_string)
10847 : : {
10848 : 248 : tmp = comp;
10849 : 248 : if (c->attr.pdt_array)
10850 : 182 : tmp = gfc_conv_descriptor_data_get (comp);
10851 : 248 : null_cond = fold_build2_loc (input_location, NE_EXPR,
10852 : : logical_type_node, tmp,
10853 : 248 : build_int_cst (TREE_TYPE (tmp), 0));
10854 : 248 : if (flag_openmp_allocators)
10855 : : {
10856 : 0 : tree cd, t;
10857 : 0 : if (c->attr.pdt_array)
10858 : 0 : cd = fold_build2_loc (input_location, EQ_EXPR,
10859 : : boolean_type_node,
10860 : : gfc_conv_descriptor_version (comp),
10861 : : build_int_cst (integer_type_node, 1));
10862 : : else
10863 : 0 : cd = gfc_omp_call_is_alloc (tmp);
10864 : 0 : t = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
10865 : 0 : t = build_call_expr_loc (input_location, t, 1, tmp);
10866 : :
10867 : 0 : stmtblock_t tblock;
10868 : 0 : gfc_init_block (&tblock);
10869 : 0 : gfc_add_expr_to_block (&tblock, t);
10870 : 0 : if (c->attr.pdt_array)
10871 : 0 : gfc_add_modify (&tblock, gfc_conv_descriptor_version (comp),
10872 : : integer_zero_node);
10873 : 0 : tmp = build3_loc (input_location, COND_EXPR, void_type_node,
10874 : : cd, gfc_finish_block (&tblock),
10875 : : gfc_call_free (tmp));
10876 : : }
10877 : : else
10878 : 248 : tmp = gfc_call_free (tmp);
10879 : 248 : tmp = build3_v (COND_EXPR, null_cond, tmp,
10880 : : build_empty_stmt (input_location));
10881 : 248 : gfc_add_expr_to_block (&fnblock, tmp);
10882 : :
10883 : 248 : if (c->attr.pdt_array)
10884 : 182 : gfc_conv_descriptor_data_set (&fnblock, comp, null_pointer_node);
10885 : : else
10886 : : {
10887 : 66 : tmp = fold_convert (TREE_TYPE (comp), null_pointer_node);
10888 : 66 : gfc_add_modify (&fnblock, comp, tmp);
10889 : : }
10890 : : }
10891 : :
10892 : : break;
10893 : :
10894 : 264 : case CHECK_PDT_DUMMY:
10895 : :
10896 : 264 : comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
10897 : : decl, cdecl, NULL_TREE);
10898 : 264 : if (c->ts.type == BT_CLASS)
10899 : 0 : comp = gfc_class_data_get (comp);
10900 : :
10901 : : /* Recurse in to PDT components. */
10902 : 264 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10903 : 0 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_type)
10904 : : {
10905 : 0 : tmp = gfc_check_pdt_dummy (c->ts.u.derived, comp,
10906 : 0 : c->as ? c->as->rank : 0,
10907 : : pdt_param_list);
10908 : 0 : gfc_add_expr_to_block (&fnblock, tmp);
10909 : : }
10910 : :
10911 : 264 : if (!c->attr.pdt_len)
10912 : 216 : continue;
10913 : : else
10914 : : {
10915 : 48 : gfc_se tse;
10916 : 48 : gfc_expr *c_expr = NULL;
10917 : 48 : gfc_actual_arglist *param = pdt_param_list;
10918 : :
10919 : 48 : gfc_init_se (&tse, NULL);
10920 : 186 : for (; param; param = param->next)
10921 : 90 : if (!strcmp (c->name, param->name)
10922 : 48 : && param->spec_type == SPEC_EXPLICIT)
10923 : 30 : c_expr = param->expr;
10924 : :
10925 : 48 : if (c_expr)
10926 : : {
10927 : 30 : tree error, cond, cname;
10928 : 30 : gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
10929 : 30 : cond = fold_build2_loc (input_location, NE_EXPR,
10930 : : logical_type_node,
10931 : : comp, tse.expr);
10932 : 30 : cname = gfc_build_cstring_const (c->name);
10933 : 30 : cname = gfc_build_addr_expr (pchar_type_node, cname);
10934 : 30 : error = gfc_trans_runtime_error (true, NULL,
10935 : : "The value of the PDT LEN "
10936 : : "parameter '%s' does not "
10937 : : "agree with that in the "
10938 : : "dummy declaration",
10939 : : cname);
10940 : 30 : tmp = fold_build3_loc (input_location, COND_EXPR,
10941 : : void_type_node, cond, error,
10942 : : build_empty_stmt (input_location));
10943 : 30 : gfc_add_expr_to_block (&fnblock, tmp);
10944 : : }
10945 : : }
10946 : 48 : break;
10947 : :
10948 : 0 : default:
10949 : 0 : gcc_unreachable ();
10950 : 3803 : break;
10951 : : }
10952 : : }
10953 : 13297 : seen_derived_types.remove (der_type);
10954 : :
10955 : 13297 : return gfc_finish_block (&fnblock);
10956 : : }
10957 : :
10958 : : /* Recursively traverse an object of derived type, generating code to
10959 : : nullify allocatable components. */
10960 : :
10961 : : tree
10962 : 2247 : gfc_nullify_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
10963 : : int caf_mode)
10964 : : {
10965 : 2247 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10966 : : NULLIFY_ALLOC_COMP,
10967 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
10968 : 2247 : NULL);
10969 : : }
10970 : :
10971 : :
10972 : : /* Recursively traverse an object of derived type, generating code to
10973 : : deallocate allocatable components. */
10974 : :
10975 : : tree
10976 : 2453 : gfc_deallocate_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
10977 : : int caf_mode)
10978 : : {
10979 : 2453 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
10980 : : DEALLOCATE_ALLOC_COMP,
10981 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
10982 : 2453 : NULL);
10983 : : }
10984 : :
10985 : : tree
10986 : 1 : gfc_bcast_alloc_comp (gfc_symbol *derived, gfc_expr *expr, int rank,
10987 : : tree image_index, tree stat, tree errmsg,
10988 : : tree errmsg_len)
10989 : : {
10990 : 1 : tree tmp, array;
10991 : 1 : gfc_se argse;
10992 : 1 : stmtblock_t block, post_block;
10993 : 1 : gfc_co_subroutines_args args;
10994 : :
10995 : 1 : args.image_index = image_index;
10996 : 1 : args.stat = stat;
10997 : 1 : args.errmsg = errmsg;
10998 : 1 : args.errmsg_len = errmsg_len;
10999 : :
11000 : 1 : if (rank == 0)
11001 : : {
11002 : 1 : gfc_start_block (&block);
11003 : 1 : gfc_init_block (&post_block);
11004 : 1 : gfc_init_se (&argse, NULL);
11005 : 1 : gfc_conv_expr (&argse, expr);
11006 : 1 : gfc_add_block_to_block (&block, &argse.pre);
11007 : 1 : gfc_add_block_to_block (&post_block, &argse.post);
11008 : 1 : array = argse.expr;
11009 : : }
11010 : : else
11011 : : {
11012 : 0 : gfc_init_se (&argse, NULL);
11013 : 0 : argse.want_pointer = 1;
11014 : 0 : gfc_conv_expr_descriptor (&argse, expr);
11015 : 0 : array = argse.expr;
11016 : : }
11017 : :
11018 : 1 : tmp = structure_alloc_comps (derived, array, NULL_TREE, rank,
11019 : : BCAST_ALLOC_COMP,
11020 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
11021 : : &args);
11022 : 1 : return tmp;
11023 : : }
11024 : :
11025 : : /* Recursively traverse an object of derived type, generating code to
11026 : : deallocate allocatable components. But do not deallocate coarrays.
11027 : : To be used for intrinsic assignment, which may not change the allocation
11028 : : status of coarrays. */
11029 : :
11030 : : tree
11031 : 1826 : gfc_deallocate_alloc_comp_no_caf (gfc_symbol * der_type, tree decl, int rank,
11032 : : bool no_finalization)
11033 : : {
11034 : 1826 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11035 : : DEALLOCATE_ALLOC_COMP, 0, NULL,
11036 : 1826 : no_finalization);
11037 : : }
11038 : :
11039 : :
11040 : : tree
11041 : 4 : gfc_reassign_alloc_comp_caf (gfc_symbol *der_type, tree decl, tree dest)
11042 : : {
11043 : 4 : return structure_alloc_comps (der_type, decl, dest, 0, REASSIGN_CAF_COMP,
11044 : : GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
11045 : 4 : NULL);
11046 : : }
11047 : :
11048 : :
11049 : : /* Recursively traverse an object of derived type, generating code to
11050 : : copy it and its allocatable components. */
11051 : :
11052 : : tree
11053 : 3472 : gfc_copy_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank,
11054 : : int caf_mode)
11055 : : {
11056 : 3472 : return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
11057 : 3472 : caf_mode, NULL);
11058 : : }
11059 : :
11060 : :
11061 : : /* Recursively traverse an object of derived type, generating code to
11062 : : copy it and its allocatable components, while suppressing any
11063 : : finalization that might occur. This is used in the finalization of
11064 : : function results. */
11065 : :
11066 : : tree
11067 : 37 : gfc_copy_alloc_comp_no_fini (gfc_symbol * der_type, tree decl, tree dest,
11068 : : int rank, int caf_mode)
11069 : : {
11070 : 37 : return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
11071 : 37 : caf_mode, NULL, true);
11072 : : }
11073 : :
11074 : :
11075 : : /* Recursively traverse an object of derived type, generating code to
11076 : : copy only its allocatable components. */
11077 : :
11078 : : tree
11079 : 0 : gfc_copy_only_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
11080 : : {
11081 : 0 : return structure_alloc_comps (der_type, decl, dest, rank,
11082 : 0 : COPY_ONLY_ALLOC_COMP, 0, NULL);
11083 : : }
11084 : :
11085 : :
11086 : : /* Recursively traverse an object of parameterized derived type, generating
11087 : : code to allocate parameterized components. */
11088 : :
11089 : : tree
11090 : 280 : gfc_allocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank,
11091 : : gfc_actual_arglist *param_list)
11092 : : {
11093 : 280 : tree res;
11094 : 280 : gfc_actual_arglist *old_param_list = pdt_param_list;
11095 : 280 : pdt_param_list = param_list;
11096 : 280 : res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11097 : : ALLOCATE_PDT_COMP, 0, NULL);
11098 : 280 : pdt_param_list = old_param_list;
11099 : 280 : return res;
11100 : : }
11101 : :
11102 : : /* Recursively traverse an object of parameterized derived type, generating
11103 : : code to deallocate parameterized components. */
11104 : :
11105 : : tree
11106 : 267 : gfc_deallocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank)
11107 : : {
11108 : 267 : return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11109 : 267 : DEALLOCATE_PDT_COMP, 0, NULL);
11110 : : }
11111 : :
11112 : :
11113 : : /* Recursively traverse a dummy of parameterized derived type to check the
11114 : : values of LEN parameters. */
11115 : :
11116 : : tree
11117 : 48 : gfc_check_pdt_dummy (gfc_symbol * der_type, tree decl, int rank,
11118 : : gfc_actual_arglist *param_list)
11119 : : {
11120 : 48 : tree res;
11121 : 48 : gfc_actual_arglist *old_param_list = pdt_param_list;
11122 : 48 : pdt_param_list = param_list;
11123 : 48 : res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
11124 : : CHECK_PDT_DUMMY, 0, NULL);
11125 : 48 : pdt_param_list = old_param_list;
11126 : 48 : return res;
11127 : : }
11128 : :
11129 : :
11130 : : /* Returns the value of LBOUND for an expression. This could be broken out
11131 : : from gfc_conv_intrinsic_bound but this seemed to be simpler. This is
11132 : : called by gfc_alloc_allocatable_for_assignment. */
11133 : : static tree
11134 : 947 : get_std_lbound (gfc_expr *expr, tree desc, int dim, bool assumed_size)
11135 : : {
11136 : 947 : tree lbound;
11137 : 947 : tree ubound;
11138 : 947 : tree stride;
11139 : 947 : tree cond, cond1, cond3, cond4;
11140 : 947 : tree tmp;
11141 : 947 : gfc_ref *ref;
11142 : :
11143 : 947 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
11144 : : {
11145 : 462 : tmp = gfc_rank_cst[dim];
11146 : 462 : lbound = gfc_conv_descriptor_lbound_get (desc, tmp);
11147 : 462 : ubound = gfc_conv_descriptor_ubound_get (desc, tmp);
11148 : 462 : stride = gfc_conv_descriptor_stride_get (desc, tmp);
11149 : 462 : cond1 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
11150 : : ubound, lbound);
11151 : 462 : cond3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
11152 : : stride, gfc_index_zero_node);
11153 : 462 : cond3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
11154 : : logical_type_node, cond3, cond1);
11155 : 462 : cond4 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
11156 : : stride, gfc_index_zero_node);
11157 : 462 : if (assumed_size)
11158 : 0 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11159 : : tmp, build_int_cst (gfc_array_index_type,
11160 : 0 : expr->rank - 1));
11161 : : else
11162 : 462 : cond = logical_false_node;
11163 : :
11164 : 462 : cond1 = fold_build2_loc (input_location, TRUTH_OR_EXPR,
11165 : : logical_type_node, cond3, cond4);
11166 : 462 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
11167 : : logical_type_node, cond, cond1);
11168 : :
11169 : 462 : return fold_build3_loc (input_location, COND_EXPR,
11170 : : gfc_array_index_type, cond,
11171 : 462 : lbound, gfc_index_one_node);
11172 : : }
11173 : :
11174 : 485 : if (expr->expr_type == EXPR_FUNCTION)
11175 : : {
11176 : : /* A conversion function, so use the argument. */
11177 : 7 : gcc_assert (expr->value.function.isym
11178 : : && expr->value.function.isym->conversion);
11179 : 7 : expr = expr->value.function.actual->expr;
11180 : : }
11181 : :
11182 : 485 : if (expr->expr_type == EXPR_VARIABLE)
11183 : : {
11184 : 485 : tmp = TREE_TYPE (expr->symtree->n.sym->backend_decl);
11185 : 1283 : for (ref = expr->ref; ref; ref = ref->next)
11186 : : {
11187 : 798 : if (ref->type == REF_COMPONENT
11188 : 264 : && ref->u.c.component->as
11189 : 215 : && ref->next
11190 : 215 : && ref->next->u.ar.type == AR_FULL)
11191 : 173 : tmp = TREE_TYPE (ref->u.c.component->backend_decl);
11192 : : }
11193 : 485 : return GFC_TYPE_ARRAY_LBOUND(tmp, dim);
11194 : : }
11195 : :
11196 : 0 : return gfc_index_one_node;
11197 : : }
11198 : :
11199 : :
11200 : : /* Returns true if an expression represents an lhs that can be reallocated
11201 : : on assignment. */
11202 : :
11203 : : bool
11204 : 627997 : gfc_is_reallocatable_lhs (gfc_expr *expr)
11205 : : {
11206 : 627997 : gfc_ref * ref;
11207 : 627997 : gfc_symbol *sym;
11208 : :
11209 : 627997 : if (!expr->ref)
11210 : : return false;
11211 : :
11212 : 195817 : sym = expr->symtree->n.sym;
11213 : :
11214 : 195817 : if (sym->attr.associate_var && !expr->ref)
11215 : : return false;
11216 : :
11217 : : /* An allocatable class variable with no reference. */
11218 : 195817 : if (sym->ts.type == BT_CLASS
11219 : 7673 : && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
11220 : 7505 : && CLASS_DATA (sym)->attr.allocatable
11221 : : && expr->ref
11222 : 4849 : && ((expr->ref->type == REF_ARRAY && expr->ref->u.ar.type == AR_FULL
11223 : 0 : && expr->ref->next == NULL)
11224 : 4849 : || (expr->ref->type == REF_COMPONENT
11225 : 4849 : && strcmp (expr->ref->u.c.component->name, "_data") == 0
11226 : 4029 : && (expr->ref->next == NULL
11227 : 3250 : || (expr->ref->next->type == REF_ARRAY
11228 : 3250 : && expr->ref->next->u.ar.type == AR_FULL
11229 : 2676 : && expr->ref->next->next == NULL)))))
11230 : : return true;
11231 : :
11232 : : /* An allocatable variable. */
11233 : 192557 : if (sym->attr.allocatable
11234 : 42599 : && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
11235 : : && expr->ref
11236 : 42599 : && expr->ref->type == REF_ARRAY
11237 : 40745 : && expr->ref->u.ar.type == AR_FULL)
11238 : : return true;
11239 : :
11240 : : /* All that can be left are allocatable components. */
11241 : 167310 : if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
11242 : : return false;
11243 : :
11244 : : /* Find a component ref followed by an array reference. */
11245 : 84494 : for (ref = expr->ref; ref; ref = ref->next)
11246 : 58943 : if (ref->next
11247 : 33392 : && ref->type == REF_COMPONENT
11248 : 20168 : && ref->next->type == REF_ARRAY
11249 : 15016 : && !ref->next->next)
11250 : : break;
11251 : :
11252 : 37201 : if (!ref)
11253 : : return false;
11254 : :
11255 : : /* Return true if valid reallocatable lhs. */
11256 : 11650 : if (ref->u.c.component->attr.allocatable
11257 : 5143 : && ref->next->u.ar.type == AR_FULL)
11258 : 3701 : return true;
11259 : :
11260 : : return false;
11261 : : }
11262 : :
11263 : :
11264 : : static tree
11265 : 56 : concat_str_length (gfc_expr* expr)
11266 : : {
11267 : 56 : tree type;
11268 : 56 : tree len1;
11269 : 56 : tree len2;
11270 : 56 : gfc_se se;
11271 : :
11272 : 56 : type = gfc_typenode_for_spec (&expr->value.op.op1->ts);
11273 : 56 : len1 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
11274 : 56 : if (len1 == NULL_TREE)
11275 : : {
11276 : 56 : if (expr->value.op.op1->expr_type == EXPR_OP)
11277 : 31 : len1 = concat_str_length (expr->value.op.op1);
11278 : 25 : else if (expr->value.op.op1->expr_type == EXPR_CONSTANT)
11279 : 25 : len1 = build_int_cst (gfc_charlen_type_node,
11280 : 25 : expr->value.op.op1->value.character.length);
11281 : 0 : else if (expr->value.op.op1->ts.u.cl->length)
11282 : : {
11283 : 0 : gfc_init_se (&se, NULL);
11284 : 0 : gfc_conv_expr (&se, expr->value.op.op1->ts.u.cl->length);
11285 : 0 : len1 = se.expr;
11286 : : }
11287 : : else
11288 : : {
11289 : : /* Last resort! */
11290 : 0 : gfc_init_se (&se, NULL);
11291 : 0 : se.want_pointer = 1;
11292 : 0 : se.descriptor_only = 1;
11293 : 0 : gfc_conv_expr (&se, expr->value.op.op1);
11294 : 0 : len1 = se.string_length;
11295 : : }
11296 : : }
11297 : :
11298 : 56 : type = gfc_typenode_for_spec (&expr->value.op.op2->ts);
11299 : 56 : len2 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
11300 : 56 : if (len2 == NULL_TREE)
11301 : : {
11302 : 31 : if (expr->value.op.op2->expr_type == EXPR_OP)
11303 : 0 : len2 = concat_str_length (expr->value.op.op2);
11304 : 31 : else if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
11305 : 25 : len2 = build_int_cst (gfc_charlen_type_node,
11306 : 25 : expr->value.op.op2->value.character.length);
11307 : 6 : else if (expr->value.op.op2->ts.u.cl->length)
11308 : : {
11309 : 6 : gfc_init_se (&se, NULL);
11310 : 6 : gfc_conv_expr (&se, expr->value.op.op2->ts.u.cl->length);
11311 : 6 : len2 = se.expr;
11312 : : }
11313 : : else
11314 : : {
11315 : : /* Last resort! */
11316 : 0 : gfc_init_se (&se, NULL);
11317 : 0 : se.want_pointer = 1;
11318 : 0 : se.descriptor_only = 1;
11319 : 0 : gfc_conv_expr (&se, expr->value.op.op2);
11320 : 0 : len2 = se.string_length;
11321 : : }
11322 : : }
11323 : :
11324 : 56 : gcc_assert(len1 && len2);
11325 : 56 : len1 = fold_convert (gfc_charlen_type_node, len1);
11326 : 56 : len2 = fold_convert (gfc_charlen_type_node, len2);
11327 : :
11328 : 56 : return fold_build2_loc (input_location, PLUS_EXPR,
11329 : 56 : gfc_charlen_type_node, len1, len2);
11330 : : }
11331 : :
11332 : :
11333 : : /* Allocate the lhs of an assignment to an allocatable array, otherwise
11334 : : reallocate it. */
11335 : :
11336 : : tree
11337 : 5796 : gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop,
11338 : : gfc_expr *expr1,
11339 : : gfc_expr *expr2)
11340 : : {
11341 : 5796 : stmtblock_t realloc_block;
11342 : 5796 : stmtblock_t alloc_block;
11343 : 5796 : stmtblock_t fblock;
11344 : 5796 : stmtblock_t loop_pre_block;
11345 : 5796 : gfc_ref *ref;
11346 : 5796 : gfc_ss *rss;
11347 : 5796 : gfc_ss *lss;
11348 : 5796 : gfc_array_info *linfo;
11349 : 5796 : tree realloc_expr;
11350 : 5796 : tree alloc_expr;
11351 : 5796 : tree size1;
11352 : 5796 : tree size2;
11353 : 5796 : tree elemsize1;
11354 : 5796 : tree elemsize2;
11355 : 5796 : tree array1;
11356 : 5796 : tree cond_null;
11357 : 5796 : tree cond;
11358 : 5796 : tree tmp;
11359 : 5796 : tree tmp2;
11360 : 5796 : tree lbound;
11361 : 5796 : tree ubound;
11362 : 5796 : tree desc;
11363 : 5796 : tree old_desc;
11364 : 5796 : tree desc2;
11365 : 5796 : tree offset;
11366 : 5796 : tree jump_label1;
11367 : 5796 : tree jump_label2;
11368 : 5796 : tree lbd;
11369 : 5796 : tree class_expr2 = NULL_TREE;
11370 : 5796 : int n;
11371 : 5796 : int dim;
11372 : 5796 : gfc_array_spec * as;
11373 : 5796 : bool coarray = (flag_coarray == GFC_FCOARRAY_LIB
11374 : 5796 : && gfc_caf_attr (expr1, true).codimension);
11375 : 5796 : tree token;
11376 : 5796 : gfc_se caf_se;
11377 : :
11378 : : /* x = f(...) with x allocatable. In this case, expr1 is the rhs.
11379 : : Find the lhs expression in the loop chain and set expr1 and
11380 : : expr2 accordingly. */
11381 : 5796 : if (expr1->expr_type == EXPR_FUNCTION && expr2 == NULL)
11382 : : {
11383 : 166 : expr2 = expr1;
11384 : : /* Find the ss for the lhs. */
11385 : 166 : lss = loop->ss;
11386 : 332 : for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
11387 : 332 : if (lss->info->expr && lss->info->expr->expr_type == EXPR_VARIABLE)
11388 : : break;
11389 : 166 : if (lss == gfc_ss_terminator)
11390 : : return NULL_TREE;
11391 : 166 : expr1 = lss->info->expr;
11392 : : }
11393 : :
11394 : : /* Bail out if this is not a valid allocate on assignment. */
11395 : 5796 : if (!gfc_is_reallocatable_lhs (expr1)
11396 : 5796 : || (expr2 && !expr2->rank))
11397 : : return NULL_TREE;
11398 : :
11399 : : /* Find the ss for the lhs. */
11400 : 5796 : lss = loop->ss;
11401 : 14714 : for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
11402 : 14714 : if (lss->info->expr == expr1)
11403 : : break;
11404 : :
11405 : 5796 : if (lss == gfc_ss_terminator)
11406 : : return NULL_TREE;
11407 : :
11408 : 5796 : linfo = &lss->info->data.array;
11409 : :
11410 : : /* Find an ss for the rhs. For operator expressions, we see the
11411 : : ss's for the operands. Any one of these will do. */
11412 : 5796 : rss = loop->ss;
11413 : 6279 : for (; rss && rss != gfc_ss_terminator; rss = rss->loop_chain)
11414 : 6279 : if (rss->info->expr != expr1 && rss != loop->temp_ss)
11415 : : break;
11416 : :
11417 : 5796 : if (expr2 && rss == gfc_ss_terminator)
11418 : : return NULL_TREE;
11419 : :
11420 : : /* Ensure that the string length from the current scope is used. */
11421 : 5796 : if (expr2->ts.type == BT_CHARACTER
11422 : 879 : && expr2->expr_type == EXPR_FUNCTION
11423 : 130 : && !expr2->value.function.isym)
11424 : 21 : expr2->ts.u.cl->backend_decl = rss->info->string_length;
11425 : :
11426 : 5796 : gfc_start_block (&fblock);
11427 : :
11428 : : /* Since the lhs is allocatable, this must be a descriptor type.
11429 : : Get the data and array size. */
11430 : 5796 : desc = linfo->descriptor;
11431 : 5796 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
11432 : 5796 : array1 = gfc_conv_descriptor_data_get (desc);
11433 : :
11434 : 5796 : if (expr2)
11435 : 5796 : desc2 = rss->info->data.array.descriptor;
11436 : : else
11437 : : desc2 = NULL_TREE;
11438 : :
11439 : : /* Get the old lhs element size for deferred character and class expr1. */
11440 : 5796 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11441 : : {
11442 : 579 : if (expr1->ts.u.cl->backend_decl
11443 : 579 : && VAR_P (expr1->ts.u.cl->backend_decl))
11444 : : elemsize1 = expr1->ts.u.cl->backend_decl;
11445 : : else
11446 : 64 : elemsize1 = lss->info->string_length;
11447 : 579 : tree unit_size = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
11448 : 1158 : elemsize1 = fold_build2_loc (input_location, MULT_EXPR,
11449 : 579 : TREE_TYPE (elemsize1), elemsize1,
11450 : 579 : fold_convert (TREE_TYPE (elemsize1), unit_size));
11451 : :
11452 : 579 : }
11453 : 5217 : else if (expr1->ts.type == BT_CLASS)
11454 : : {
11455 : : /* Unfortunately, the lhs vptr is set too early in many cases.
11456 : : Play it safe by using the descriptor element length. */
11457 : 549 : tmp = gfc_conv_descriptor_elem_len (desc);
11458 : 549 : elemsize1 = fold_convert (gfc_array_index_type, tmp);
11459 : : }
11460 : : else
11461 : : elemsize1 = NULL_TREE;
11462 : 1128 : if (elemsize1 != NULL_TREE)
11463 : 1128 : elemsize1 = gfc_evaluate_now (elemsize1, &fblock);
11464 : :
11465 : : /* Get the new lhs size in bytes. */
11466 : 5796 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11467 : : {
11468 : 579 : if (expr2->ts.deferred)
11469 : : {
11470 : 183 : if (expr2->ts.u.cl->backend_decl
11471 : 183 : && VAR_P (expr2->ts.u.cl->backend_decl))
11472 : : tmp = expr2->ts.u.cl->backend_decl;
11473 : : else
11474 : 0 : tmp = rss->info->string_length;
11475 : : }
11476 : : else
11477 : : {
11478 : 396 : tmp = expr2->ts.u.cl->backend_decl;
11479 : 396 : if (!tmp && expr2->expr_type == EXPR_OP
11480 : 25 : && expr2->value.op.op == INTRINSIC_CONCAT)
11481 : : {
11482 : 25 : tmp = concat_str_length (expr2);
11483 : 25 : expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
11484 : : }
11485 : 12 : else if (!tmp && expr2->ts.u.cl->length)
11486 : : {
11487 : 12 : gfc_se tmpse;
11488 : 12 : gfc_init_se (&tmpse, NULL);
11489 : 12 : gfc_conv_expr_type (&tmpse, expr2->ts.u.cl->length,
11490 : : gfc_charlen_type_node);
11491 : 12 : tmp = tmpse.expr;
11492 : 12 : expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
11493 : : }
11494 : 396 : tmp = fold_convert (TREE_TYPE (expr1->ts.u.cl->backend_decl), tmp);
11495 : : }
11496 : :
11497 : 579 : if (expr1->ts.u.cl->backend_decl
11498 : 579 : && VAR_P (expr1->ts.u.cl->backend_decl))
11499 : 515 : gfc_add_modify (&fblock, expr1->ts.u.cl->backend_decl, tmp);
11500 : : else
11501 : 64 : gfc_add_modify (&fblock, lss->info->string_length, tmp);
11502 : :
11503 : 579 : if (expr1->ts.kind > 1)
11504 : 12 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11505 : 6 : TREE_TYPE (tmp),
11506 : 6 : tmp, build_int_cst (TREE_TYPE (tmp),
11507 : 6 : expr1->ts.kind));
11508 : : }
11509 : 5217 : else if (expr1->ts.type == BT_CHARACTER && expr1->ts.u.cl->backend_decl)
11510 : : {
11511 : 251 : tmp = TYPE_SIZE_UNIT (TREE_TYPE (gfc_typenode_for_spec (&expr1->ts)));
11512 : 251 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
11513 : : fold_convert (gfc_array_index_type, tmp),
11514 : 251 : expr1->ts.u.cl->backend_decl);
11515 : : }
11516 : 4966 : else if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
11517 : 110 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
11518 : 4856 : else if (expr1->ts.type == BT_CLASS && expr2->ts.type == BT_CLASS)
11519 : : {
11520 : 268 : tmp = expr2->rank ? gfc_get_class_from_expr (desc2) : NULL_TREE;
11521 : 268 : if (tmp == NULL_TREE && expr2->expr_type == EXPR_VARIABLE)
11522 : 24 : tmp = class_expr2 = gfc_get_class_from_gfc_expr (expr2);
11523 : :
11524 : 31 : if (tmp != NULL_TREE)
11525 : 261 : tmp = gfc_class_vtab_size_get (tmp);
11526 : : else
11527 : 7 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
11528 : : }
11529 : : else
11530 : 4588 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
11531 : 5796 : elemsize2 = fold_convert (gfc_array_index_type, tmp);
11532 : 5796 : elemsize2 = gfc_evaluate_now (elemsize2, &fblock);
11533 : :
11534 : : /* 7.4.1.3 "If variable is an allocated allocatable variable, it is
11535 : : deallocated if expr is an array of different shape or any of the
11536 : : corresponding length type parameter values of variable and expr
11537 : : differ." This assures F95 compatibility. */
11538 : 5796 : jump_label1 = gfc_build_label_decl (NULL_TREE);
11539 : 5796 : jump_label2 = gfc_build_label_decl (NULL_TREE);
11540 : :
11541 : : /* Allocate if data is NULL. */
11542 : 5796 : cond_null = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11543 : 5796 : array1, build_int_cst (TREE_TYPE (array1), 0));
11544 : 5796 : cond_null= gfc_evaluate_now (cond_null, &fblock);
11545 : :
11546 : : /* If the data is null, set the descriptor bounds and offset. This suppresses
11547 : : the maybe used uninitialized warning and forces the use of malloc because
11548 : : the size is zero in all dimensions. Note that this block is only executed
11549 : : if the lhs is unallocated and is only applied once in any namespace.
11550 : : Component references are not subject to the warnings. */
11551 : 10574 : for (ref = expr1->ref; ref; ref = ref->next)
11552 : 5991 : if (ref->type == REF_COMPONENT)
11553 : : break;
11554 : :
11555 : 5796 : if (!expr1->symtree->n.sym->allocated_in_scope && !ref)
11556 : : {
11557 : 2402 : gfc_start_block (&loop_pre_block);
11558 : 8225 : for (n = 0; n < expr1->rank; n++)
11559 : : {
11560 : 3421 : gfc_conv_descriptor_lbound_set (&loop_pre_block, desc,
11561 : : gfc_rank_cst[n],
11562 : : gfc_index_one_node);
11563 : 3421 : gfc_conv_descriptor_ubound_set (&loop_pre_block, desc,
11564 : : gfc_rank_cst[n],
11565 : : gfc_index_zero_node);
11566 : 3421 : gfc_conv_descriptor_stride_set (&loop_pre_block, desc,
11567 : : gfc_rank_cst[n],
11568 : : gfc_index_zero_node);
11569 : : }
11570 : :
11571 : 2402 : tmp = gfc_conv_descriptor_offset (desc);
11572 : 2402 : gfc_add_modify (&loop_pre_block, tmp, gfc_index_zero_node);
11573 : :
11574 : 2402 : tmp = fold_build2_loc (input_location, EQ_EXPR,
11575 : : logical_type_node, array1,
11576 : 2402 : build_int_cst (TREE_TYPE (array1), 0));
11577 : 2402 : tmp = build3_v (COND_EXPR, tmp,
11578 : : gfc_finish_block (&loop_pre_block),
11579 : : build_empty_stmt (input_location));
11580 : 2402 : gfc_prepend_expr_to_block (&loop->pre, tmp);
11581 : :
11582 : 2402 : expr1->symtree->n.sym->allocated_in_scope = 1;
11583 : : }
11584 : :
11585 : 5796 : tmp = build3_v (COND_EXPR, cond_null,
11586 : : build1_v (GOTO_EXPR, jump_label1),
11587 : : build_empty_stmt (input_location));
11588 : 5796 : gfc_add_expr_to_block (&fblock, tmp);
11589 : :
11590 : : /* Get arrayspec if expr is a full array. */
11591 : 5796 : if (expr2 && expr2->expr_type == EXPR_FUNCTION
11592 : 2712 : && expr2->value.function.isym
11593 : 2266 : && expr2->value.function.isym->conversion)
11594 : : {
11595 : : /* For conversion functions, take the arg. */
11596 : 243 : gfc_expr *arg = expr2->value.function.actual->expr;
11597 : 243 : as = gfc_get_full_arrayspec_from_expr (arg);
11598 : 243 : }
11599 : : else if (expr2)
11600 : 5553 : as = gfc_get_full_arrayspec_from_expr (expr2);
11601 : : else
11602 : : as = NULL;
11603 : :
11604 : : /* If the lhs shape is not the same as the rhs jump to setting the
11605 : : bounds and doing the reallocation....... */
11606 : 14631 : for (n = 0; n < expr1->rank; n++)
11607 : : {
11608 : : /* Check the shape. */
11609 : 8835 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11610 : 8835 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
11611 : 8835 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11612 : : gfc_array_index_type,
11613 : : loop->to[n], loop->from[n]);
11614 : 8835 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11615 : : gfc_array_index_type,
11616 : : tmp, lbound);
11617 : 8835 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11618 : : gfc_array_index_type,
11619 : : tmp, ubound);
11620 : 8835 : cond = fold_build2_loc (input_location, NE_EXPR,
11621 : : logical_type_node,
11622 : : tmp, gfc_index_zero_node);
11623 : 8835 : tmp = build3_v (COND_EXPR, cond,
11624 : : build1_v (GOTO_EXPR, jump_label1),
11625 : : build_empty_stmt (input_location));
11626 : 8835 : gfc_add_expr_to_block (&fblock, tmp);
11627 : : }
11628 : :
11629 : : /* ...else if the element lengths are not the same also go to
11630 : : setting the bounds and doing the reallocation.... */
11631 : 5796 : if (elemsize1 != NULL_TREE)
11632 : : {
11633 : 1128 : cond = fold_build2_loc (input_location, NE_EXPR,
11634 : : logical_type_node,
11635 : : elemsize1, elemsize2);
11636 : 1128 : tmp = build3_v (COND_EXPR, cond,
11637 : : build1_v (GOTO_EXPR, jump_label1),
11638 : : build_empty_stmt (input_location));
11639 : 1128 : gfc_add_expr_to_block (&fblock, tmp);
11640 : : }
11641 : :
11642 : : /* ....else jump past the (re)alloc code. */
11643 : 5796 : tmp = build1_v (GOTO_EXPR, jump_label2);
11644 : 5796 : gfc_add_expr_to_block (&fblock, tmp);
11645 : :
11646 : : /* Add the label to start automatic (re)allocation. */
11647 : 5796 : tmp = build1_v (LABEL_EXPR, jump_label1);
11648 : 5796 : gfc_add_expr_to_block (&fblock, tmp);
11649 : :
11650 : : /* Get the rhs size and fix it. */
11651 : 5796 : size2 = gfc_index_one_node;
11652 : 14631 : for (n = 0; n < expr2->rank; n++)
11653 : : {
11654 : 8835 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11655 : : gfc_array_index_type,
11656 : : loop->to[n], loop->from[n]);
11657 : 8835 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11658 : : gfc_array_index_type,
11659 : : tmp, gfc_index_one_node);
11660 : 8835 : size2 = fold_build2_loc (input_location, MULT_EXPR,
11661 : : gfc_array_index_type,
11662 : : tmp, size2);
11663 : : }
11664 : 5796 : size2 = gfc_evaluate_now (size2, &fblock);
11665 : :
11666 : : /* Deallocation of allocatable components will have to occur on
11667 : : reallocation. Fix the old descriptor now. */
11668 : 5796 : if ((expr1->ts.type == BT_DERIVED)
11669 : 276 : && expr1->ts.u.derived->attr.alloc_comp)
11670 : 103 : old_desc = gfc_evaluate_now (desc, &fblock);
11671 : : else
11672 : : old_desc = NULL_TREE;
11673 : :
11674 : : /* Now modify the lhs descriptor and the associated scalarizer
11675 : : variables. F2003 7.4.1.3: "If variable is or becomes an
11676 : : unallocated allocatable variable, then it is allocated with each
11677 : : deferred type parameter equal to the corresponding type parameters
11678 : : of expr , with the shape of expr , and with each lower bound equal
11679 : : to the corresponding element of LBOUND(expr)."
11680 : : Reuse size1 to keep a dimension-by-dimension track of the
11681 : : stride of the new array. */
11682 : 5796 : size1 = gfc_index_one_node;
11683 : 5796 : offset = gfc_index_zero_node;
11684 : :
11685 : 14631 : for (n = 0; n < expr2->rank; n++)
11686 : : {
11687 : 8835 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11688 : : gfc_array_index_type,
11689 : : loop->to[n], loop->from[n]);
11690 : 8835 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11691 : : gfc_array_index_type,
11692 : : tmp, gfc_index_one_node);
11693 : :
11694 : 8835 : lbound = gfc_index_one_node;
11695 : 8835 : ubound = tmp;
11696 : :
11697 : 8835 : if (as)
11698 : : {
11699 : 1894 : lbd = get_std_lbound (expr2, desc2, n,
11700 : 947 : as->type == AS_ASSUMED_SIZE);
11701 : 947 : ubound = fold_build2_loc (input_location,
11702 : : MINUS_EXPR,
11703 : : gfc_array_index_type,
11704 : : ubound, lbound);
11705 : 947 : ubound = fold_build2_loc (input_location,
11706 : : PLUS_EXPR,
11707 : : gfc_array_index_type,
11708 : : ubound, lbd);
11709 : 947 : lbound = lbd;
11710 : : }
11711 : :
11712 : 8835 : gfc_conv_descriptor_lbound_set (&fblock, desc,
11713 : : gfc_rank_cst[n],
11714 : : lbound);
11715 : 8835 : gfc_conv_descriptor_ubound_set (&fblock, desc,
11716 : : gfc_rank_cst[n],
11717 : : ubound);
11718 : 8835 : gfc_conv_descriptor_stride_set (&fblock, desc,
11719 : : gfc_rank_cst[n],
11720 : : size1);
11721 : 8835 : lbound = gfc_conv_descriptor_lbound_get (desc,
11722 : : gfc_rank_cst[n]);
11723 : 8835 : tmp2 = fold_build2_loc (input_location, MULT_EXPR,
11724 : : gfc_array_index_type,
11725 : : lbound, size1);
11726 : 8835 : offset = fold_build2_loc (input_location, MINUS_EXPR,
11727 : : gfc_array_index_type,
11728 : : offset, tmp2);
11729 : 8835 : size1 = fold_build2_loc (input_location, MULT_EXPR,
11730 : : gfc_array_index_type,
11731 : : tmp, size1);
11732 : : }
11733 : :
11734 : : /* Set the lhs descriptor and scalarizer offsets. For rank > 1,
11735 : : the array offset is saved and the info.offset is used for a
11736 : : running offset. Use the saved_offset instead. */
11737 : 5796 : tmp = gfc_conv_descriptor_offset (desc);
11738 : 5796 : gfc_add_modify (&fblock, tmp, offset);
11739 : 5796 : if (linfo->saved_offset
11740 : 5796 : && VAR_P (linfo->saved_offset))
11741 : 5796 : gfc_add_modify (&fblock, linfo->saved_offset, tmp);
11742 : :
11743 : : /* Now set the deltas for the lhs. */
11744 : 14631 : for (n = 0; n < expr1->rank; n++)
11745 : : {
11746 : 8835 : tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11747 : 8835 : dim = lss->dim[n];
11748 : 8835 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11749 : : gfc_array_index_type, tmp,
11750 : : loop->from[dim]);
11751 : 8835 : if (linfo->delta[dim] && VAR_P (linfo->delta[dim]))
11752 : 8497 : gfc_add_modify (&fblock, linfo->delta[dim], tmp);
11753 : : }
11754 : :
11755 : : /* Take into account _len of unlimited polymorphic entities, so that span
11756 : : for array descriptors and allocation sizes are computed correctly. */
11757 : 5796 : if (UNLIMITED_POLY (expr2))
11758 : : {
11759 : 92 : tree len = gfc_class_len_get (TREE_OPERAND (desc2, 0));
11760 : 92 : len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
11761 : : fold_convert (size_type_node, len),
11762 : : size_one_node);
11763 : 92 : elemsize2 = fold_build2_loc (input_location, MULT_EXPR,
11764 : : gfc_array_index_type, elemsize2,
11765 : : fold_convert (gfc_array_index_type, len));
11766 : : }
11767 : :
11768 : 5796 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
11769 : 5796 : gfc_conv_descriptor_span_set (&fblock, desc, elemsize2);
11770 : :
11771 : 5796 : size2 = fold_build2_loc (input_location, MULT_EXPR,
11772 : : gfc_array_index_type,
11773 : : elemsize2, size2);
11774 : 5796 : size2 = fold_convert (size_type_node, size2);
11775 : 5796 : size2 = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
11776 : : size2, size_one_node);
11777 : 5796 : size2 = gfc_evaluate_now (size2, &fblock);
11778 : :
11779 : : /* For deferred character length, the 'size' field of the dtype might
11780 : : have changed so set the dtype. */
11781 : 5796 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
11782 : 5796 : && expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11783 : : {
11784 : 579 : tree type;
11785 : 579 : tmp = gfc_conv_descriptor_dtype (desc);
11786 : 579 : if (expr2->ts.u.cl->backend_decl)
11787 : 579 : type = gfc_typenode_for_spec (&expr2->ts);
11788 : : else
11789 : 0 : type = gfc_typenode_for_spec (&expr1->ts);
11790 : :
11791 : 579 : gfc_add_modify (&fblock, tmp,
11792 : : gfc_get_dtype_rank_type (expr1->rank,type));
11793 : : }
11794 : 5217 : else if (expr1->ts.type == BT_CLASS)
11795 : : {
11796 : 549 : tree type;
11797 : 549 : tmp = gfc_conv_descriptor_dtype (desc);
11798 : :
11799 : 549 : if (expr2->ts.type != BT_CLASS)
11800 : 281 : type = gfc_typenode_for_spec (&expr2->ts);
11801 : : else
11802 : 268 : type = gfc_get_character_type_len (1, elemsize2);
11803 : :
11804 : 549 : gfc_add_modify (&fblock, tmp,
11805 : : gfc_get_dtype_rank_type (expr2->rank,type));
11806 : : /* Set the _len field as well... */
11807 : 549 : if (UNLIMITED_POLY (expr1))
11808 : : {
11809 : 202 : tmp = gfc_class_len_get (TREE_OPERAND (desc, 0));
11810 : 202 : if (expr2->ts.type == BT_CHARACTER)
11811 : 49 : gfc_add_modify (&fblock, tmp,
11812 : 49 : fold_convert (TREE_TYPE (tmp),
11813 : : TYPE_SIZE_UNIT (type)));
11814 : 153 : else if (UNLIMITED_POLY (expr2))
11815 : 92 : gfc_add_modify (&fblock, tmp,
11816 : 92 : gfc_class_len_get (TREE_OPERAND (desc2, 0)));
11817 : : else
11818 : 61 : gfc_add_modify (&fblock, tmp,
11819 : 61 : build_int_cst (TREE_TYPE (tmp), 0));
11820 : : }
11821 : : /* ...and the vptr. */
11822 : 549 : tmp = gfc_class_vptr_get (TREE_OPERAND (desc, 0));
11823 : 549 : if (expr2->ts.type == BT_CLASS && !VAR_P (desc2)
11824 : 261 : && TREE_CODE (desc2) == COMPONENT_REF)
11825 : : {
11826 : 237 : tmp2 = gfc_get_class_from_expr (desc2);
11827 : 237 : tmp2 = gfc_class_vptr_get (tmp2);
11828 : : }
11829 : 312 : else if (expr2->ts.type == BT_CLASS && class_expr2 != NULL_TREE)
11830 : 24 : tmp2 = gfc_class_vptr_get (class_expr2);
11831 : : else
11832 : : {
11833 : 288 : tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
11834 : 288 : tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
11835 : : }
11836 : :
11837 : 549 : gfc_add_modify (&fblock, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
11838 : : }
11839 : 4668 : else if (coarray && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
11840 : : {
11841 : 38 : gfc_add_modify (&fblock, gfc_conv_descriptor_dtype (desc),
11842 : 38 : gfc_get_dtype (TREE_TYPE (desc)));
11843 : : }
11844 : :
11845 : : /* Realloc expression. Note that the scalarizer uses desc.data
11846 : : in the array reference - (*desc.data)[<element>]. */
11847 : 5796 : gfc_init_block (&realloc_block);
11848 : 5796 : gfc_init_se (&caf_se, NULL);
11849 : :
11850 : 5796 : if (coarray)
11851 : : {
11852 : 38 : token = gfc_get_ultimate_alloc_ptr_comps_caf_token (&caf_se, expr1);
11853 : 38 : if (token == NULL_TREE)
11854 : : {
11855 : 8 : tmp = gfc_get_tree_for_caf_expr (expr1);
11856 : 8 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
11857 : 6 : tmp = build_fold_indirect_ref (tmp);
11858 : 8 : gfc_get_caf_token_offset (&caf_se, &token, NULL, tmp, NULL_TREE,
11859 : : expr1);
11860 : 8 : token = gfc_build_addr_expr (NULL_TREE, token);
11861 : : }
11862 : :
11863 : 38 : gfc_add_block_to_block (&realloc_block, &caf_se.pre);
11864 : : }
11865 : 5796 : if ((expr1->ts.type == BT_DERIVED)
11866 : 276 : && expr1->ts.u.derived->attr.alloc_comp)
11867 : : {
11868 : 103 : tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, old_desc,
11869 : : expr1->rank, true);
11870 : 103 : gfc_add_expr_to_block (&realloc_block, tmp);
11871 : : }
11872 : :
11873 : 5796 : if (!coarray)
11874 : : {
11875 : 5758 : tmp = build_call_expr_loc (input_location,
11876 : : builtin_decl_explicit (BUILT_IN_REALLOC), 2,
11877 : : fold_convert (pvoid_type_node, array1),
11878 : : size2);
11879 : 5758 : if (flag_openmp_allocators)
11880 : : {
11881 : 2 : tree cond, omp_tmp;
11882 : 2 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
11883 : : gfc_conv_descriptor_version (desc),
11884 : : build_int_cst (integer_type_node, 1));
11885 : 2 : omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
11886 : 2 : omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
11887 : : fold_convert (pvoid_type_node, array1), size2,
11888 : : build_zero_cst (ptr_type_node),
11889 : : build_zero_cst (ptr_type_node));
11890 : 2 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
11891 : : omp_tmp, tmp);
11892 : : }
11893 : :
11894 : 5758 : gfc_conv_descriptor_data_set (&realloc_block, desc, tmp);
11895 : : }
11896 : : else
11897 : : {
11898 : 38 : tmp = build_call_expr_loc (input_location,
11899 : : gfor_fndecl_caf_deregister, 5, token,
11900 : : build_int_cst (integer_type_node,
11901 : : GFC_CAF_COARRAY_DEALLOCATE_ONLY),
11902 : : null_pointer_node, null_pointer_node,
11903 : : integer_zero_node);
11904 : 38 : gfc_add_expr_to_block (&realloc_block, tmp);
11905 : 38 : tmp = build_call_expr_loc (input_location,
11906 : : gfor_fndecl_caf_register,
11907 : : 7, size2,
11908 : : build_int_cst (integer_type_node,
11909 : : GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY),
11910 : : token, gfc_build_addr_expr (NULL_TREE, desc),
11911 : : null_pointer_node, null_pointer_node,
11912 : : integer_zero_node);
11913 : 38 : gfc_add_expr_to_block (&realloc_block, tmp);
11914 : : }
11915 : :
11916 : 5796 : if ((expr1->ts.type == BT_DERIVED)
11917 : 276 : && expr1->ts.u.derived->attr.alloc_comp)
11918 : : {
11919 : 103 : tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
11920 : : expr1->rank);
11921 : 103 : gfc_add_expr_to_block (&realloc_block, tmp);
11922 : : }
11923 : :
11924 : 5796 : gfc_add_block_to_block (&realloc_block, &caf_se.post);
11925 : 5796 : realloc_expr = gfc_finish_block (&realloc_block);
11926 : :
11927 : : /* Malloc expression. */
11928 : 5796 : gfc_init_block (&alloc_block);
11929 : 5796 : if (!coarray)
11930 : : {
11931 : 5758 : tmp = build_call_expr_loc (input_location,
11932 : : builtin_decl_explicit (BUILT_IN_MALLOC),
11933 : : 1, size2);
11934 : 5758 : gfc_conv_descriptor_data_set (&alloc_block,
11935 : : desc, tmp);
11936 : : }
11937 : : else
11938 : : {
11939 : 38 : tmp = build_call_expr_loc (input_location,
11940 : : gfor_fndecl_caf_register,
11941 : : 7, size2,
11942 : : build_int_cst (integer_type_node,
11943 : : GFC_CAF_COARRAY_ALLOC),
11944 : : token, gfc_build_addr_expr (NULL_TREE, desc),
11945 : : null_pointer_node, null_pointer_node,
11946 : : integer_zero_node);
11947 : 38 : gfc_add_expr_to_block (&alloc_block, tmp);
11948 : : }
11949 : :
11950 : :
11951 : : /* We already set the dtype in the case of deferred character
11952 : : length arrays and class lvalues. */
11953 : 5796 : if (!(GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
11954 : 5796 : && ((expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
11955 : 5217 : || coarray))
11956 : 10975 : && expr1->ts.type != BT_CLASS)
11957 : : {
11958 : 4630 : tmp = gfc_conv_descriptor_dtype (desc);
11959 : 4630 : gfc_add_modify (&alloc_block, tmp, gfc_get_dtype (TREE_TYPE (desc)));
11960 : : }
11961 : :
11962 : 5796 : if ((expr1->ts.type == BT_DERIVED)
11963 : 276 : && expr1->ts.u.derived->attr.alloc_comp)
11964 : : {
11965 : 103 : tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
11966 : : expr1->rank);
11967 : 103 : gfc_add_expr_to_block (&alloc_block, tmp);
11968 : : }
11969 : 5796 : alloc_expr = gfc_finish_block (&alloc_block);
11970 : :
11971 : : /* Malloc if not allocated; realloc otherwise. */
11972 : 5796 : tmp = build3_v (COND_EXPR, cond_null, alloc_expr, realloc_expr);
11973 : 5796 : gfc_add_expr_to_block (&fblock, tmp);
11974 : :
11975 : : /* Make sure that the scalarizer data pointer is updated. */
11976 : 5796 : if (linfo->data && VAR_P (linfo->data))
11977 : : {
11978 : 5247 : tmp = gfc_conv_descriptor_data_get (desc);
11979 : 5247 : gfc_add_modify (&fblock, linfo->data, tmp);
11980 : : }
11981 : :
11982 : : /* Add the label for same shape lhs and rhs. */
11983 : 5796 : tmp = build1_v (LABEL_EXPR, jump_label2);
11984 : 5796 : gfc_add_expr_to_block (&fblock, tmp);
11985 : :
11986 : 5796 : return gfc_finish_block (&fblock);
11987 : : }
11988 : :
11989 : :
11990 : : /* Initialize class descriptor's TKR information. */
11991 : :
11992 : : void
11993 : 2795 : gfc_trans_class_array (gfc_symbol * sym, gfc_wrapped_block * block)
11994 : : {
11995 : 2795 : tree type, etype;
11996 : 2795 : tree tmp;
11997 : 2795 : tree descriptor;
11998 : 2795 : stmtblock_t init;
11999 : 2795 : int rank;
12000 : :
12001 : : /* Make sure the frontend gets these right. */
12002 : 2795 : gcc_assert (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
12003 : : && (CLASS_DATA (sym)->attr.class_pointer
12004 : : || CLASS_DATA (sym)->attr.allocatable));
12005 : :
12006 : 2795 : gcc_assert (VAR_P (sym->backend_decl)
12007 : : || TREE_CODE (sym->backend_decl) == PARM_DECL);
12008 : :
12009 : 2795 : if (sym->attr.dummy)
12010 : 1402 : return;
12011 : :
12012 : 2795 : descriptor = gfc_class_data_get (sym->backend_decl);
12013 : 2795 : type = TREE_TYPE (descriptor);
12014 : :
12015 : 2795 : if (type == NULL || !GFC_DESCRIPTOR_TYPE_P (type))
12016 : : return;
12017 : :
12018 : 1393 : location_t loc = input_location;
12019 : 1393 : input_location = gfc_get_location (&sym->declared_at);
12020 : 1393 : gfc_init_block (&init);
12021 : :
12022 : 1393 : rank = CLASS_DATA (sym)->as ? (CLASS_DATA (sym)->as->rank) : (0);
12023 : 1393 : gcc_assert (rank>=0);
12024 : 1393 : tmp = gfc_conv_descriptor_dtype (descriptor);
12025 : 1393 : etype = gfc_get_element_type (type);
12026 : 1393 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp), tmp,
12027 : : gfc_get_dtype_rank_type (rank, etype));
12028 : 1393 : gfc_add_expr_to_block (&init, tmp);
12029 : :
12030 : 1393 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
12031 : 1393 : input_location = loc;
12032 : : }
12033 : :
12034 : :
12035 : : /* NULLIFY an allocatable/pointer array on function entry, free it on exit.
12036 : : Do likewise, recursively if necessary, with the allocatable components of
12037 : : derived types. This function is also called for assumed-rank arrays, which
12038 : : are always dummy arguments. */
12039 : :
12040 : : void
12041 : 16654 : gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
12042 : : {
12043 : 16654 : tree type;
12044 : 16654 : tree tmp;
12045 : 16654 : tree descriptor;
12046 : 16654 : stmtblock_t init;
12047 : 16654 : stmtblock_t cleanup;
12048 : 16654 : int rank;
12049 : 16654 : bool sym_has_alloc_comp, has_finalizer;
12050 : :
12051 : 33308 : sym_has_alloc_comp = (sym->ts.type == BT_DERIVED
12052 : 10549 : || sym->ts.type == BT_CLASS)
12053 : 16654 : && sym->ts.u.derived->attr.alloc_comp;
12054 : 16654 : has_finalizer = gfc_may_be_finalized (sym->ts);
12055 : :
12056 : : /* Make sure the frontend gets these right. */
12057 : 16654 : gcc_assert (sym->attr.pointer || sym->attr.allocatable || sym_has_alloc_comp
12058 : : || has_finalizer
12059 : : || (sym->as->type == AS_ASSUMED_RANK && sym->attr.dummy));
12060 : :
12061 : 16654 : location_t loc = input_location;
12062 : 16654 : input_location = gfc_get_location (&sym->declared_at);
12063 : 16654 : gfc_init_block (&init);
12064 : :
12065 : 16654 : gcc_assert (VAR_P (sym->backend_decl)
12066 : : || TREE_CODE (sym->backend_decl) == PARM_DECL);
12067 : :
12068 : 16654 : if (sym->ts.type == BT_CHARACTER
12069 : 1277 : && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
12070 : : {
12071 : 748 : if (sym->ts.deferred && !sym->ts.u.cl->length && !sym->attr.dummy)
12072 : : {
12073 : 556 : tree len_expr = sym->ts.u.cl->backend_decl;
12074 : 556 : tree init_val = build_zero_cst (TREE_TYPE (len_expr));
12075 : 556 : if (VAR_P (len_expr)
12076 : 556 : && sym->attr.save
12077 : 581 : && !DECL_INITIAL (len_expr))
12078 : 25 : DECL_INITIAL (len_expr) = init_val;
12079 : : else
12080 : 531 : gfc_add_modify (&init, len_expr, init_val);
12081 : : }
12082 : 748 : gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
12083 : 748 : gfc_trans_vla_type_sizes (sym, &init);
12084 : :
12085 : : /* Presence check of optional deferred-length character dummy. */
12086 : 748 : if (sym->ts.deferred && sym->attr.dummy && sym->attr.optional)
12087 : : {
12088 : 43 : tmp = gfc_finish_block (&init);
12089 : 43 : tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
12090 : : tmp, build_empty_stmt (input_location));
12091 : 43 : gfc_add_expr_to_block (&init, tmp);
12092 : : }
12093 : : }
12094 : :
12095 : : /* Dummy, use associated and result variables don't need anything special. */
12096 : 16654 : if (sym->attr.dummy || sym->attr.use_assoc || sym->attr.result)
12097 : : {
12098 : 705 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
12099 : 705 : input_location = loc;
12100 : 925 : return;
12101 : : }
12102 : :
12103 : 15949 : descriptor = sym->backend_decl;
12104 : :
12105 : : /* Although static, derived types with default initializers and
12106 : : allocatable components must not be nulled wholesale; instead they
12107 : : are treated component by component. */
12108 : 15949 : if (TREE_STATIC (descriptor) && !sym_has_alloc_comp && !has_finalizer)
12109 : : {
12110 : : /* SAVEd variables are not freed on exit. */
12111 : 220 : gfc_trans_static_array_pointer (sym);
12112 : :
12113 : 220 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
12114 : 220 : input_location = loc;
12115 : 220 : return;
12116 : : }
12117 : :
12118 : : /* Get the descriptor type. */
12119 : 15729 : type = TREE_TYPE (sym->backend_decl);
12120 : :
12121 : 15729 : if ((sym_has_alloc_comp || (has_finalizer && sym->ts.type != BT_CLASS))
12122 : 4727 : && !(sym->attr.pointer || sym->attr.allocatable))
12123 : : {
12124 : 2562 : if (!sym->attr.save
12125 : 2202 : && !(TREE_STATIC (sym->backend_decl) && sym->attr.is_main_program))
12126 : : {
12127 : 2202 : if (sym->value == NULL
12128 : 2202 : || !gfc_has_default_initializer (sym->ts.u.derived))
12129 : : {
12130 : 1888 : rank = sym->as ? sym->as->rank : 0;
12131 : 1888 : tmp = gfc_nullify_alloc_comp (sym->ts.u.derived,
12132 : : descriptor, rank);
12133 : 1888 : gfc_add_expr_to_block (&init, tmp);
12134 : : }
12135 : : else
12136 : 314 : gfc_init_default_dt (sym, &init, false);
12137 : : }
12138 : : }
12139 : 13167 : else if (!GFC_DESCRIPTOR_TYPE_P (type))
12140 : : {
12141 : : /* If the backend_decl is not a descriptor, we must have a pointer
12142 : : to one. */
12143 : 1686 : descriptor = build_fold_indirect_ref_loc (input_location,
12144 : : sym->backend_decl);
12145 : 1686 : type = TREE_TYPE (descriptor);
12146 : : }
12147 : :
12148 : : /* NULLIFY the data pointer for non-saved allocatables, or for non-saved
12149 : : pointers when -fcheck=pointer is specified. */
12150 : 27210 : if (GFC_DESCRIPTOR_TYPE_P (type) && !sym->attr.save
12151 : 27197 : && (sym->attr.allocatable
12152 : 3228 : || (sym->attr.pointer && (gfc_option.rtcheck & GFC_RTCHECK_POINTER))))
12153 : : {
12154 : 8283 : gfc_conv_descriptor_data_set (&init, descriptor, null_pointer_node);
12155 : 8283 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
12156 : : {
12157 : : /* Declare the variable static so its array descriptor stays present
12158 : : after leaving the scope. It may still be accessed through another
12159 : : image. This may happen, for example, with the caf_mpi
12160 : : implementation. */
12161 : 104 : TREE_STATIC (descriptor) = 1;
12162 : 104 : tmp = gfc_conv_descriptor_token (descriptor);
12163 : 104 : gfc_add_modify (&init, tmp, fold_convert (TREE_TYPE (tmp),
12164 : : null_pointer_node));
12165 : : }
12166 : : }
12167 : :
12168 : : /* Set initial TKR for pointers and allocatables */
12169 : 15729 : if (GFC_DESCRIPTOR_TYPE_P (type)
12170 : 15729 : && (sym->attr.pointer || sym->attr.allocatable))
12171 : : {
12172 : 11481 : tree etype;
12173 : :
12174 : 11481 : gcc_assert (sym->as && sym->as->rank>=0);
12175 : 11481 : tmp = gfc_conv_descriptor_dtype (descriptor);
12176 : 11481 : etype = gfc_get_element_type (type);
12177 : 11481 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
12178 : 11481 : TREE_TYPE (tmp), tmp,
12179 : 11481 : gfc_get_dtype_rank_type (sym->as->rank, etype));
12180 : 11481 : gfc_add_expr_to_block (&init, tmp);
12181 : : }
12182 : 15729 : input_location = loc;
12183 : 15729 : gfc_init_block (&cleanup);
12184 : :
12185 : : /* Allocatable arrays need to be freed when they go out of scope.
12186 : : The allocatable components of pointers must not be touched. */
12187 : 15729 : if (!sym->attr.allocatable && has_finalizer && sym->ts.type != BT_CLASS
12188 : 486 : && !sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
12189 : 236 : && !sym->ns->proc_name->attr.is_main_program)
12190 : : {
12191 : 198 : gfc_expr *e;
12192 : 198 : sym->attr.referenced = 1;
12193 : 198 : e = gfc_lval_expr_from_sym (sym);
12194 : 198 : gfc_add_finalizer_call (&cleanup, e);
12195 : 198 : gfc_free_expr (e);
12196 : 198 : }
12197 : 15531 : else if ((!sym->attr.allocatable || !has_finalizer)
12198 : 15420 : && sym_has_alloc_comp && !(sym->attr.function || sym->attr.result)
12199 : 4273 : && !sym->attr.pointer && !sym->attr.save
12200 : 2201 : && !(sym->attr.artificial && sym->name[0] == '_')
12201 : 2146 : && !sym->ns->proc_name->attr.is_main_program)
12202 : : {
12203 : 568 : int rank;
12204 : 568 : rank = sym->as ? sym->as->rank : 0;
12205 : 568 : tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank,
12206 : 568 : (sym->attr.codimension
12207 : 2 : && flag_coarray == GFC_FCOARRAY_LIB)
12208 : : ? GFC_STRUCTURE_CAF_MODE_IN_COARRAY
12209 : : : 0);
12210 : 568 : gfc_add_expr_to_block (&cleanup, tmp);
12211 : : }
12212 : :
12213 : 15729 : if (sym->attr.allocatable && (sym->attr.dimension || sym->attr.codimension)
12214 : 8247 : && !sym->attr.save && !sym->attr.result
12215 : 8240 : && !sym->ns->proc_name->attr.is_main_program)
12216 : : {
12217 : 4440 : gfc_expr *e;
12218 : 4440 : e = has_finalizer ? gfc_lval_expr_from_sym (sym) : NULL;
12219 : 8880 : tmp = gfc_deallocate_with_status (sym->backend_decl, NULL_TREE, NULL_TREE,
12220 : : NULL_TREE, NULL_TREE, true, e,
12221 : 4440 : sym->attr.codimension
12222 : : ? GFC_CAF_COARRAY_DEREGISTER
12223 : : : GFC_CAF_COARRAY_NOCOARRAY,
12224 : : NULL_TREE, gfc_finish_block (&cleanup));
12225 : 4440 : if (e)
12226 : 32 : gfc_free_expr (e);
12227 : 4440 : gfc_init_block (&cleanup);
12228 : 4440 : gfc_add_expr_to_block (&cleanup, tmp);
12229 : : }
12230 : :
12231 : 15729 : gfc_add_init_cleanup (block, gfc_finish_block (&init),
12232 : : gfc_finish_block (&cleanup));
12233 : : }
12234 : :
12235 : : /************ Expression Walking Functions ******************/
12236 : :
12237 : : /* Walk a variable reference.
12238 : :
12239 : : Possible extension - multiple component subscripts.
12240 : : x(:,:) = foo%a(:)%b(:)
12241 : : Transforms to
12242 : : forall (i=..., j=...)
12243 : : x(i,j) = foo%a(j)%b(i)
12244 : : end forall
12245 : : This adds a fair amount of complexity because you need to deal with more
12246 : : than one ref. Maybe handle in a similar manner to vector subscripts.
12247 : : Maybe not worth the effort. */
12248 : :
12249 : :
12250 : : static gfc_ss *
12251 : 658747 : gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
12252 : : {
12253 : 658747 : gfc_ref *ref;
12254 : :
12255 : 658747 : gfc_fix_class_refs (expr);
12256 : :
12257 : 765653 : for (ref = expr->ref; ref; ref = ref->next)
12258 : 422922 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
12259 : : break;
12260 : :
12261 : 658747 : return gfc_walk_array_ref (ss, expr, ref);
12262 : : }
12263 : :
12264 : : gfc_ss *
12265 : 658998 : gfc_walk_array_ref (gfc_ss *ss, gfc_expr *expr, gfc_ref *ref, bool array_only)
12266 : : {
12267 : 658998 : gfc_array_ref *ar;
12268 : 658998 : gfc_ss *newss;
12269 : 658998 : int n;
12270 : :
12271 : 982960 : for (; ref; ref = ref->next)
12272 : : {
12273 : 323962 : if (ref->type == REF_SUBSTRING)
12274 : : {
12275 : 1302 : ss = gfc_get_scalar_ss (ss, ref->u.ss.start);
12276 : 1302 : if (ref->u.ss.end)
12277 : 1276 : ss = gfc_get_scalar_ss (ss, ref->u.ss.end);
12278 : : }
12279 : :
12280 : : /* We're only interested in array sections from now on. */
12281 : 323962 : if (ref->type != REF_ARRAY
12282 : 316525 : || (array_only && ref->u.ar.as && ref->u.ar.as->rank == 0))
12283 : 7534 : continue;
12284 : :
12285 : 316428 : ar = &ref->u.ar;
12286 : :
12287 : 316428 : switch (ar->type)
12288 : : {
12289 : 188 : case AR_ELEMENT:
12290 : 423 : for (n = ar->dimen - 1; n >= 0; n--)
12291 : 235 : ss = gfc_get_scalar_ss (ss, ar->start[n]);
12292 : : break;
12293 : :
12294 : 261832 : case AR_FULL:
12295 : : /* Assumed shape arrays from interface mapping need this fix. */
12296 : 261832 : if (!ar->as && expr->symtree->n.sym->as)
12297 : : {
12298 : 6 : ar->as = gfc_get_array_spec();
12299 : 6 : *ar->as = *expr->symtree->n.sym->as;
12300 : : }
12301 : 261832 : newss = gfc_get_array_ss (ss, expr, ar->as->rank, GFC_SS_SECTION);
12302 : 261832 : newss->info->data.array.ref = ref;
12303 : :
12304 : : /* Make sure array is the same as array(:,:), this way
12305 : : we don't need to special case all the time. */
12306 : 261832 : ar->dimen = ar->as->rank;
12307 : 609318 : for (n = 0; n < ar->dimen; n++)
12308 : : {
12309 : 347486 : ar->dimen_type[n] = DIMEN_RANGE;
12310 : :
12311 : 347486 : gcc_assert (ar->start[n] == NULL);
12312 : 347486 : gcc_assert (ar->end[n] == NULL);
12313 : 347486 : gcc_assert (ar->stride[n] == NULL);
12314 : : }
12315 : : ss = newss;
12316 : : break;
12317 : :
12318 : 54408 : case AR_SECTION:
12319 : 54408 : newss = gfc_get_array_ss (ss, expr, 0, GFC_SS_SECTION);
12320 : 54408 : newss->info->data.array.ref = ref;
12321 : :
12322 : : /* We add SS chains for all the subscripts in the section. */
12323 : 140406 : for (n = 0; n < ar->dimen; n++)
12324 : : {
12325 : 85998 : gfc_ss *indexss;
12326 : :
12327 : 85998 : switch (ar->dimen_type[n])
12328 : : {
12329 : 6116 : case DIMEN_ELEMENT:
12330 : : /* Add SS for elemental (scalar) subscripts. */
12331 : 6116 : gcc_assert (ar->start[n]);
12332 : 6116 : indexss = gfc_get_scalar_ss (gfc_ss_terminator, ar->start[n]);
12333 : 6116 : indexss->loop_chain = gfc_ss_terminator;
12334 : 6116 : newss->info->data.array.subscript[n] = indexss;
12335 : 6116 : break;
12336 : :
12337 : 79076 : case DIMEN_RANGE:
12338 : : /* We don't add anything for sections, just remember this
12339 : : dimension for later. */
12340 : 79076 : newss->dim[newss->dimen] = n;
12341 : 79076 : newss->dimen++;
12342 : 79076 : break;
12343 : :
12344 : 806 : case DIMEN_VECTOR:
12345 : : /* Create a GFC_SS_VECTOR index in which we can store
12346 : : the vector's descriptor. */
12347 : 806 : indexss = gfc_get_array_ss (gfc_ss_terminator, ar->start[n],
12348 : : 1, GFC_SS_VECTOR);
12349 : 806 : indexss->loop_chain = gfc_ss_terminator;
12350 : 806 : newss->info->data.array.subscript[n] = indexss;
12351 : 806 : newss->dim[newss->dimen] = n;
12352 : 806 : newss->dimen++;
12353 : 806 : break;
12354 : :
12355 : 0 : default:
12356 : : /* We should know what sort of section it is by now. */
12357 : 0 : gcc_unreachable ();
12358 : : }
12359 : : }
12360 : : /* We should have at least one non-elemental dimension,
12361 : : unless we are creating a descriptor for a (scalar) coarray. */
12362 : 54408 : gcc_assert (newss->dimen > 0
12363 : : || newss->info->data.array.ref->u.ar.as->corank > 0);
12364 : : ss = newss;
12365 : : break;
12366 : :
12367 : 0 : default:
12368 : : /* We should know what sort of section it is by now. */
12369 : 0 : gcc_unreachable ();
12370 : : }
12371 : :
12372 : : }
12373 : 658998 : return ss;
12374 : : }
12375 : :
12376 : :
12377 : : /* Walk an expression operator. If only one operand of a binary expression is
12378 : : scalar, we must also add the scalar term to the SS chain. */
12379 : :
12380 : : static gfc_ss *
12381 : 54351 : gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
12382 : : {
12383 : 54351 : gfc_ss *head;
12384 : 54351 : gfc_ss *head2;
12385 : :
12386 : 54351 : head = gfc_walk_subexpr (ss, expr->value.op.op1);
12387 : 54351 : if (expr->value.op.op2 == NULL)
12388 : : head2 = head;
12389 : : else
12390 : 52407 : head2 = gfc_walk_subexpr (head, expr->value.op.op2);
12391 : :
12392 : : /* All operands are scalar. Pass back and let the caller deal with it. */
12393 : 54351 : if (head2 == ss)
12394 : : return head2;
12395 : :
12396 : : /* All operands require scalarization. */
12397 : 49908 : if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
12398 : : return head2;
12399 : :
12400 : : /* One of the operands needs scalarization, the other is scalar.
12401 : : Create a gfc_ss for the scalar expression. */
12402 : 18681 : if (head == ss)
12403 : : {
12404 : : /* First operand is scalar. We build the chain in reverse order, so
12405 : : add the scalar SS after the second operand. */
12406 : : head = head2;
12407 : 2242 : while (head && head->next != ss)
12408 : : head = head->next;
12409 : : /* Check we haven't somehow broken the chain. */
12410 : 1999 : gcc_assert (head);
12411 : 1999 : head->next = gfc_get_scalar_ss (ss, expr->value.op.op1);
12412 : : }
12413 : : else /* head2 == head */
12414 : : {
12415 : 16682 : gcc_assert (head2 == head);
12416 : : /* Second operand is scalar. */
12417 : 16682 : head2 = gfc_get_scalar_ss (head2, expr->value.op.op2);
12418 : : }
12419 : :
12420 : : return head2;
12421 : : }
12422 : :
12423 : :
12424 : : /* Reverse a SS chain. */
12425 : :
12426 : : gfc_ss *
12427 : 831359 : gfc_reverse_ss (gfc_ss * ss)
12428 : : {
12429 : 831359 : gfc_ss *next;
12430 : 831359 : gfc_ss *head;
12431 : :
12432 : 831359 : gcc_assert (ss != NULL);
12433 : :
12434 : : head = gfc_ss_terminator;
12435 : 1253886 : while (ss != gfc_ss_terminator)
12436 : : {
12437 : 422527 : next = ss->next;
12438 : : /* Check we didn't somehow break the chain. */
12439 : 422527 : gcc_assert (next != NULL);
12440 : 422527 : ss->next = head;
12441 : 422527 : head = ss;
12442 : 422527 : ss = next;
12443 : : }
12444 : :
12445 : 831359 : return (head);
12446 : : }
12447 : :
12448 : :
12449 : : /* Given an expression referring to a procedure, return the symbol of its
12450 : : interface. We can't get the procedure symbol directly as we have to handle
12451 : : the case of (deferred) type-bound procedures. */
12452 : :
12453 : : gfc_symbol *
12454 : 160 : gfc_get_proc_ifc_for_expr (gfc_expr *procedure_ref)
12455 : : {
12456 : 160 : gfc_symbol *sym;
12457 : 160 : gfc_ref *ref;
12458 : :
12459 : 160 : if (procedure_ref == NULL)
12460 : : return NULL;
12461 : :
12462 : : /* Normal procedure case. */
12463 : 160 : if (procedure_ref->expr_type == EXPR_FUNCTION
12464 : 160 : && procedure_ref->value.function.esym)
12465 : : sym = procedure_ref->value.function.esym;
12466 : : else
12467 : 24 : sym = procedure_ref->symtree->n.sym;
12468 : :
12469 : : /* Typebound procedure case. */
12470 : 208 : for (ref = procedure_ref->ref; ref; ref = ref->next)
12471 : : {
12472 : 48 : if (ref->type == REF_COMPONENT
12473 : 48 : && ref->u.c.component->attr.proc_pointer)
12474 : 24 : sym = ref->u.c.component->ts.interface;
12475 : : else
12476 : : sym = NULL;
12477 : : }
12478 : :
12479 : : return sym;
12480 : : }
12481 : :
12482 : :
12483 : : /* Given an expression referring to an intrinsic function call,
12484 : : return the intrinsic symbol. */
12485 : :
12486 : : gfc_intrinsic_sym *
12487 : 7765 : gfc_get_intrinsic_for_expr (gfc_expr *call)
12488 : : {
12489 : 7765 : if (call == NULL)
12490 : : return NULL;
12491 : :
12492 : : /* Normal procedure case. */
12493 : 2341 : if (call->expr_type == EXPR_FUNCTION)
12494 : 2235 : return call->value.function.isym;
12495 : : else
12496 : : return NULL;
12497 : : }
12498 : :
12499 : :
12500 : : /* Indicates whether an argument to an intrinsic function should be used in
12501 : : scalarization. It is usually the case, except for some intrinsics
12502 : : requiring the value to be constant, and using the value at compile time only.
12503 : : As the value is not used at runtime in those cases, we don’t produce code
12504 : : for it, and it should not be visible to the scalarizer.
12505 : : FUNCTION is the intrinsic function being called, ACTUAL_ARG is the actual
12506 : : argument being examined in that call, and ARG_NUM the index number
12507 : : of ACTUAL_ARG in the list of arguments.
12508 : : The intrinsic procedure’s dummy argument associated with ACTUAL_ARG is
12509 : : identified using the name in ACTUAL_ARG if it is present (that is: if it’s
12510 : : a keyword argument), otherwise using ARG_NUM. */
12511 : :
12512 : : static bool
12513 : 37859 : arg_evaluated_for_scalarization (gfc_intrinsic_sym *function,
12514 : : gfc_dummy_arg *dummy_arg)
12515 : : {
12516 : 37859 : if (function != NULL && dummy_arg != NULL)
12517 : : {
12518 : 12277 : switch (function->id)
12519 : : {
12520 : 241 : case GFC_ISYM_INDEX:
12521 : 241 : case GFC_ISYM_LEN_TRIM:
12522 : 241 : case GFC_ISYM_MASKL:
12523 : 241 : case GFC_ISYM_MASKR:
12524 : 241 : case GFC_ISYM_SCAN:
12525 : 241 : case GFC_ISYM_VERIFY:
12526 : 241 : if (strcmp ("kind", gfc_dummy_arg_get_name (*dummy_arg)) == 0)
12527 : : return false;
12528 : : /* Fallthrough. */
12529 : :
12530 : : default:
12531 : : break;
12532 : : }
12533 : : }
12534 : :
12535 : : return true;
12536 : : }
12537 : :
12538 : :
12539 : : /* Walk the arguments of an elemental function.
12540 : : PROC_EXPR is used to check whether an argument is permitted to be absent. If
12541 : : it is NULL, we don't do the check and the argument is assumed to be present.
12542 : : */
12543 : :
12544 : : gfc_ss *
12545 : 27133 : gfc_walk_elemental_function_args (gfc_ss * ss, gfc_actual_arglist *arg,
12546 : : gfc_intrinsic_sym *intrinsic_sym,
12547 : : gfc_ss_type type)
12548 : : {
12549 : 27133 : int scalar;
12550 : 27133 : gfc_ss *head;
12551 : 27133 : gfc_ss *tail;
12552 : 27133 : gfc_ss *newss;
12553 : :
12554 : 27133 : head = gfc_ss_terminator;
12555 : 27133 : tail = NULL;
12556 : :
12557 : 27133 : scalar = 1;
12558 : 66448 : for (; arg; arg = arg->next)
12559 : : {
12560 : 39315 : gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
12561 : 40804 : if (!arg->expr
12562 : 38009 : || arg->expr->expr_type == EXPR_NULL
12563 : 77174 : || !arg_evaluated_for_scalarization (intrinsic_sym, dummy_arg))
12564 : 1489 : continue;
12565 : :
12566 : 37826 : newss = gfc_walk_subexpr (head, arg->expr);
12567 : 37826 : if (newss == head)
12568 : : {
12569 : : /* Scalar argument. */
12570 : 18342 : gcc_assert (type == GFC_SS_SCALAR || type == GFC_SS_REFERENCE);
12571 : 18342 : newss = gfc_get_scalar_ss (head, arg->expr);
12572 : 18342 : newss->info->type = type;
12573 : 18342 : if (dummy_arg)
12574 : 15368 : newss->info->data.scalar.dummy_arg = dummy_arg;
12575 : : }
12576 : : else
12577 : : scalar = 0;
12578 : :
12579 : 34852 : if (dummy_arg != NULL
12580 : 25718 : && gfc_dummy_arg_is_optional (*dummy_arg)
12581 : 2492 : && arg->expr->expr_type == EXPR_VARIABLE
12582 : 36556 : && (gfc_expr_attr (arg->expr).optional
12583 : 1223 : || gfc_expr_attr (arg->expr).allocatable
12584 : 37773 : || gfc_expr_attr (arg->expr).pointer))
12585 : 1005 : newss->info->can_be_null_ref = true;
12586 : :
12587 : 37826 : head = newss;
12588 : 37826 : if (!tail)
12589 : : {
12590 : : tail = head;
12591 : 33704 : while (tail->next != gfc_ss_terminator)
12592 : : tail = tail->next;
12593 : : }
12594 : : }
12595 : :
12596 : 27133 : if (scalar)
12597 : : {
12598 : : /* If all the arguments are scalar we don't need the argument SS. */
12599 : 10169 : gfc_free_ss_chain (head);
12600 : : /* Pass it back. */
12601 : 10169 : return ss;
12602 : : }
12603 : :
12604 : : /* Add it onto the existing chain. */
12605 : 16964 : tail->next = ss;
12606 : 16964 : return head;
12607 : : }
12608 : :
12609 : :
12610 : : /* Walk a function call. Scalar functions are passed back, and taken out of
12611 : : scalarization loops. For elemental functions we walk their arguments.
12612 : : The result of functions returning arrays is stored in a temporary outside
12613 : : the loop, so that the function is only called once. Hence we do not need
12614 : : to walk their arguments. */
12615 : :
12616 : : static gfc_ss *
12617 : 63383 : gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
12618 : : {
12619 : 63383 : gfc_intrinsic_sym *isym;
12620 : 63383 : gfc_symbol *sym;
12621 : 63383 : gfc_component *comp = NULL;
12622 : :
12623 : 63383 : isym = expr->value.function.isym;
12624 : :
12625 : : /* Handle intrinsic functions separately. */
12626 : 63383 : if (isym)
12627 : 55780 : return gfc_walk_intrinsic_function (ss, expr, isym);
12628 : :
12629 : 7603 : sym = expr->value.function.esym;
12630 : 7603 : if (!sym)
12631 : 546 : sym = expr->symtree->n.sym;
12632 : :
12633 : 7603 : if (gfc_is_class_array_function (expr))
12634 : 228 : return gfc_get_array_ss (ss, expr,
12635 : 228 : CLASS_DATA (expr->value.function.esym->result)->as->rank,
12636 : 228 : GFC_SS_FUNCTION);
12637 : :
12638 : : /* A function that returns arrays. */
12639 : 7375 : comp = gfc_get_proc_ptr_comp (expr);
12640 : 6977 : if ((!comp && gfc_return_by_reference (sym) && sym->result->attr.dimension)
12641 : 7375 : || (comp && comp->attr.dimension))
12642 : 2606 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
12643 : :
12644 : : /* Walk the parameters of an elemental function. For now we always pass
12645 : : by reference. */
12646 : 4769 : if (sym->attr.elemental || (comp && comp->attr.elemental))
12647 : : {
12648 : 2199 : gfc_ss *old_ss = ss;
12649 : :
12650 : 2199 : ss = gfc_walk_elemental_function_args (old_ss,
12651 : : expr->value.function.actual,
12652 : : gfc_get_intrinsic_for_expr (expr),
12653 : : GFC_SS_REFERENCE);
12654 : 2199 : if (ss != old_ss
12655 : 1163 : && (comp
12656 : 1102 : || sym->attr.proc_pointer
12657 : 1102 : || sym->attr.if_source != IFSRC_DECL
12658 : 987 : || sym->attr.array_outer_dependency))
12659 : 224 : ss->info->array_outer_dependency = 1;
12660 : : }
12661 : :
12662 : : /* Scalar functions are OK as these are evaluated outside the scalarization
12663 : : loop. Pass back and let the caller deal with it. */
12664 : : return ss;
12665 : : }
12666 : :
12667 : :
12668 : : /* An array temporary is constructed for array constructors. */
12669 : :
12670 : : static gfc_ss *
12671 : 48965 : gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
12672 : : {
12673 : 0 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_CONSTRUCTOR);
12674 : : }
12675 : :
12676 : :
12677 : : /* Walk an expression. Add walked expressions to the head of the SS chain.
12678 : : A wholly scalar expression will not be added. */
12679 : :
12680 : : gfc_ss *
12681 : 980397 : gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
12682 : : {
12683 : 980397 : gfc_ss *head;
12684 : :
12685 : 980397 : switch (expr->expr_type)
12686 : : {
12687 : 658747 : case EXPR_VARIABLE:
12688 : 658747 : head = gfc_walk_variable_expr (ss, expr);
12689 : 658747 : return head;
12690 : :
12691 : 54351 : case EXPR_OP:
12692 : 54351 : head = gfc_walk_op_expr (ss, expr);
12693 : 54351 : return head;
12694 : :
12695 : 63383 : case EXPR_FUNCTION:
12696 : 63383 : head = gfc_walk_function_expr (ss, expr);
12697 : 63383 : return head;
12698 : :
12699 : : case EXPR_CONSTANT:
12700 : : case EXPR_NULL:
12701 : : case EXPR_STRUCTURE:
12702 : : /* Pass back and let the caller deal with it. */
12703 : : break;
12704 : :
12705 : 48965 : case EXPR_ARRAY:
12706 : 48965 : head = gfc_walk_array_constructor (ss, expr);
12707 : 48965 : return head;
12708 : :
12709 : : case EXPR_SUBSTRING:
12710 : : /* Pass back and let the caller deal with it. */
12711 : : break;
12712 : :
12713 : 0 : default:
12714 : 0 : gfc_internal_error ("bad expression type during walk (%d)",
12715 : : expr->expr_type);
12716 : : }
12717 : : return ss;
12718 : : }
12719 : :
12720 : :
12721 : : /* Entry point for expression walking.
12722 : : A return value equal to the passed chain means this is
12723 : : a scalar expression. It is up to the caller to take whatever action is
12724 : : necessary to translate these. */
12725 : :
12726 : : gfc_ss *
12727 : 828898 : gfc_walk_expr (gfc_expr * expr)
12728 : : {
12729 : 828898 : gfc_ss *res;
12730 : :
12731 : 828898 : res = gfc_walk_subexpr (gfc_ss_terminator, expr);
12732 : 828898 : return gfc_reverse_ss (res);
12733 : : }
|