Line data Source code
1 : /* Copyright (C) 2002-2025 Free Software Foundation, Inc.
2 :
3 : This file is part of GCC.
4 :
5 : GCC is free software; you can redistribute it and/or modify it under
6 : the terms of the GNU General Public License as published by the Free
7 : Software Foundation; either version 3, or (at your option) any later
8 : version.
9 :
10 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : for more details.
14 :
15 : You should have received a copy of the GNU General Public License
16 : along with GCC; see the file COPYING3. If not see
17 : <http://www.gnu.org/licenses/>. */
18 :
19 :
20 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "tree.h"
24 : #include "fold-const.h"
25 : #include "gfortran.h"
26 : #include "trans.h"
27 : #include "trans-const.h"
28 : #include "trans-types.h"
29 : #include "trans-array.h"
30 : #include "trans-descriptor.h"
31 :
32 :
33 : /* Array descriptor low level access routines.
34 : ******************************************************************************/
35 :
36 : /* Build expressions to access the members of an array descriptor.
37 : It's surprisingly easy to mess up here, so never access
38 : an array descriptor by "brute force", always use these
39 : functions. This also avoids problems if we change the format
40 : of an array descriptor.
41 :
42 : To understand these magic numbers, look at the comments
43 : before gfc_build_array_type() in trans-types.cc.
44 :
45 : The code within these defines should be the only code which knows the format
46 : of an array descriptor.
47 :
48 : Any code just needing to read obtain the bounds of an array should use
49 : gfc_conv_array_* rather than the following functions as these will return
50 : know constant values, and work with arrays which do not have descriptors.
51 :
52 : Don't forget to #undef these! */
53 :
54 : #define DATA_FIELD 0
55 : #define OFFSET_FIELD 1
56 : #define DTYPE_FIELD 2
57 : #define SPAN_FIELD 3
58 : #define DIMENSION_FIELD 4
59 : #define CAF_TOKEN_FIELD 5
60 :
61 : #define STRIDE_SUBFIELD 0
62 : #define LBOUND_SUBFIELD 1
63 : #define UBOUND_SUBFIELD 2
64 :
65 : #define GFC_DTYPE_ELEM_LEN 0
66 : #define GFC_DTYPE_VERSION 1
67 : #define GFC_DTYPE_RANK 2
68 : #define GFC_DTYPE_TYPE 3
69 : #define GFC_DTYPE_ATTRIBUTE 4
70 :
71 :
72 : /* Get FIELD_IDX'th field in struct TYPE. */
73 :
74 : static tree
75 2183618 : get_type_field (tree type, unsigned field_idx)
76 : {
77 2183618 : tree field = gfc_advance_chain (TYPE_FIELDS (type), field_idx);
78 2183618 : gcc_assert (field != NULL_TREE);
79 :
80 2183618 : return field;
81 : }
82 :
83 :
84 : /* Return a reference to the FIELD_IDX-th field of the descriptor DESC. */
85 :
86 : static tree
87 2183390 : gfc_get_descriptor_field (tree desc, unsigned field_idx)
88 : {
89 2183390 : tree type = TREE_TYPE (desc);
90 2183390 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
91 :
92 2183390 : tree field = get_type_field (type, field_idx);
93 :
94 2183390 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
95 2183390 : desc, field, NULL_TREE);
96 : }
97 :
98 :
99 : /* Return a reference to the data field of the array descriptor DESC. */
100 :
101 : static tree
102 484845 : conv_descriptor_data (tree desc)
103 : {
104 0 : return gfc_get_descriptor_field (desc, DATA_FIELD);
105 : }
106 :
107 : /* This provides READ-ONLY access to the data field. The field itself
108 : doesn't have the proper type. */
109 :
110 : tree
111 309610 : gfc_conv_descriptor_data_get (tree desc)
112 : {
113 309610 : tree type = TREE_TYPE (desc);
114 309610 : if (TREE_CODE (type) == REFERENCE_TYPE)
115 0 : gcc_unreachable ();
116 :
117 309610 : tree data = conv_descriptor_data (desc);
118 309610 : return fold_convert (GFC_TYPE_ARRAY_DATAPTR_TYPE (type), data);
119 : }
120 :
121 : /* This provides WRITE access to the data field. */
122 :
123 : void
124 175235 : gfc_conv_descriptor_data_set (stmtblock_t *block, tree desc, tree value)
125 : {
126 175235 : tree data = conv_descriptor_data (desc);
127 175235 : gfc_add_modify (block, data, fold_convert (TREE_TYPE (data), value));
128 175235 : }
129 :
130 :
131 : /* Return a reference to the offset field of the array descriptor DESC. */
132 :
133 : static tree
134 223344 : conv_descriptor_offset (tree desc)
135 : {
136 223344 : tree field = gfc_get_descriptor_field (desc, OFFSET_FIELD);
137 223344 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
138 223344 : return field;
139 : }
140 :
141 : /* Return the offset value of the array descriptor DESC. */
142 :
143 : tree
144 81920 : gfc_conv_descriptor_offset_get (tree desc)
145 : {
146 81920 : return conv_descriptor_offset (desc);
147 : }
148 :
149 : /* Add code to BLOCK assigning VALUE to the offset field of the array descriptor
150 : DESC. */
151 :
152 : void
153 141424 : gfc_conv_descriptor_offset_set (stmtblock_t *block, tree desc, tree value)
154 : {
155 141424 : tree t = conv_descriptor_offset (desc);
156 141424 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
157 141424 : }
158 :
159 :
160 : /* Return a reference to the dtype field of the array descriptor DESC. */
161 :
162 : static tree
163 194287 : conv_descriptor_dtype (tree desc)
164 : {
165 194287 : tree field = gfc_get_descriptor_field (desc, DTYPE_FIELD);
166 194287 : gcc_assert (TREE_TYPE (field) == get_dtype_type_node ());
167 194287 : return field;
168 : }
169 :
170 : /* Return the dtype value of the array descriptor DESC. */
171 :
172 : tree
173 3472 : gfc_conv_descriptor_dtype_get (tree desc)
174 : {
175 3472 : return conv_descriptor_dtype (desc);
176 : }
177 :
178 : /* Add code to BLOCK assigning VALUE to the dtype field of the array descriptor
179 : DESC. */
180 :
181 : void
182 153124 : gfc_conv_descriptor_dtype_set (stmtblock_t *block, tree desc, tree value)
183 : {
184 153124 : location_t loc = input_location;
185 153124 : tree t = conv_descriptor_dtype (desc);
186 153124 : gfc_add_modify_loc (loc, block, t,
187 153124 : fold_convert_loc (loc, TREE_TYPE (t), value));
188 153124 : }
189 :
190 :
191 : /* Return a reference to the span field of the array descriptor DESC. */
192 :
193 : static tree
194 172597 : conv_descriptor_span (tree desc)
195 : {
196 172597 : tree field = gfc_get_descriptor_field (desc, SPAN_FIELD);
197 172597 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
198 172597 : return field;
199 : }
200 :
201 : /* Return the span value of the array descriptor DESC. */
202 :
203 : tree
204 39318 : gfc_conv_descriptor_span_get (tree desc)
205 : {
206 39318 : return conv_descriptor_span (desc);
207 : }
208 :
209 : /* Add code to BLOCK assigning VALUE to the span field of the array descriptor
210 : DESC. */
211 :
212 : void
213 133279 : gfc_conv_descriptor_span_set (stmtblock_t *block, tree desc, tree value)
214 : {
215 133279 : tree t = conv_descriptor_span (desc);
216 133279 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
217 133279 : }
218 :
219 :
220 : /* Return a reference to the rank field of the array descriptor DESC. */
221 :
222 : static tree
223 25434 : conv_descriptor_rank (tree desc)
224 : {
225 25434 : tree tmp;
226 25434 : tree dtype;
227 :
228 25434 : dtype = conv_descriptor_dtype (desc);
229 25434 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_RANK);
230 25434 : gcc_assert (tmp != NULL_TREE
231 : && TREE_TYPE (tmp) == gfc_array_dim_rank_type);
232 25434 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
233 25434 : dtype, tmp, NULL_TREE);
234 : }
235 :
236 : /* Return the rank value of the array descriptor DESC. */
237 :
238 : tree
239 24538 : gfc_conv_descriptor_rank_get (tree desc)
240 : {
241 24538 : return conv_descriptor_rank (desc);
242 : }
243 :
244 : /* Add code to BLOCK assigning VALUE to the rank field of the array descriptor
245 : DESC. */
246 :
247 : void
248 896 : gfc_conv_descriptor_rank_set (stmtblock_t *block, tree desc, tree value)
249 : {
250 896 : location_t loc = input_location;
251 896 : tree t = conv_descriptor_rank (desc);
252 896 : gfc_add_modify_loc (loc, block, t,
253 896 : fold_convert_loc (loc, TREE_TYPE (t), value));
254 896 : }
255 :
256 : /* Add code to BLOCK assigning VALUE to the rank field of the array descriptor
257 : DESC. */
258 :
259 : void
260 309 : gfc_conv_descriptor_rank_set (stmtblock_t *block, tree desc, int value)
261 : {
262 309 : gfc_conv_descriptor_rank_set (block, desc, gfc_rank_cst[value]);
263 309 : }
264 :
265 :
266 : /* Return a reference to the format version field of the array descriptor
267 : DESC. */
268 :
269 : static tree
270 131 : conv_descriptor_version (tree desc)
271 : {
272 131 : tree tmp;
273 131 : tree dtype;
274 :
275 131 : dtype = conv_descriptor_dtype (desc);
276 131 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_VERSION);
277 131 : gcc_assert (tmp != NULL_TREE
278 : && TREE_TYPE (tmp) == integer_type_node);
279 131 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
280 131 : dtype, tmp, NULL_TREE);
281 : }
282 :
283 : /* Return the format version value of the array descriptor DESC. */
284 :
285 : tree
286 50 : gfc_conv_descriptor_version_get (tree desc)
287 : {
288 50 : return conv_descriptor_version (desc);
289 : }
290 :
291 : /* Add code to BLOCK assigning VALUE to the format version field of the array
292 : descriptor DESC. */
293 :
294 : void
295 81 : gfc_conv_descriptor_version_set (stmtblock_t *block, tree desc, tree value)
296 : {
297 81 : location_t loc = input_location;
298 81 : tree t = conv_descriptor_version (desc);
299 81 : gfc_add_modify_loc (loc, block, t,
300 81 : fold_convert_loc (loc, TREE_TYPE (t), value));
301 81 : }
302 :
303 :
304 : /* Return a reference to the element length field of the array descriptor
305 : DESC. */
306 :
307 : static tree
308 11939 : conv_descriptor_elem_len (tree desc)
309 : {
310 11939 : tree tmp;
311 11939 : tree dtype;
312 :
313 11939 : dtype = conv_descriptor_dtype (desc);
314 11939 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
315 : GFC_DTYPE_ELEM_LEN);
316 11939 : gcc_assert (tmp != NULL_TREE
317 : && TREE_TYPE (tmp) == size_type_node);
318 11939 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
319 11939 : dtype, tmp, NULL_TREE);
320 : }
321 :
322 : /* Return the element length value of the array descriptor DESC. */
323 :
324 : tree
325 11057 : gfc_conv_descriptor_elem_len_get (tree desc)
326 : {
327 11057 : return conv_descriptor_elem_len (desc);
328 : }
329 :
330 : /* Add code to BLOCK assigning VALUE to the element length field of the array
331 : descriptor DESC. */
332 :
333 : void
334 882 : gfc_conv_descriptor_elem_len_set (stmtblock_t *block, tree desc, tree value)
335 : {
336 882 : location_t loc = input_location;
337 882 : tree t = conv_descriptor_elem_len (desc);
338 882 : gfc_add_modify_loc (loc, block, t,
339 882 : fold_convert_loc (loc, TREE_TYPE (t), value));
340 882 : }
341 :
342 :
343 : /* Return a reference to the type discriminator field of the array descriptor
344 : DESC. */
345 :
346 : static tree
347 187 : conv_descriptor_type (tree desc)
348 : {
349 187 : tree tmp;
350 187 : tree dtype;
351 :
352 187 : dtype = conv_descriptor_dtype (desc);
353 187 : tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_TYPE);
354 187 : gcc_assert (tmp!= NULL_TREE
355 : && TREE_TYPE (tmp) == signed_char_type_node);
356 187 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
357 187 : dtype, tmp, NULL_TREE);
358 : }
359 :
360 : /* Return the type discriminator value of the array descriptor DESC. */
361 :
362 : tree
363 54 : gfc_conv_descriptor_type_get (tree desc)
364 : {
365 54 : return conv_descriptor_type (desc);
366 : }
367 :
368 : /* Add code to BLOCK assigning VALUE to the type discriminator field of the
369 : array descriptor DESC. */
370 :
371 : void
372 133 : gfc_conv_descriptor_type_set (stmtblock_t *block, tree desc, tree value)
373 : {
374 133 : location_t loc = input_location;
375 133 : tree t = conv_descriptor_type (desc);
376 133 : gfc_add_modify_loc (loc, block, t,
377 133 : fold_convert_loc (loc, TREE_TYPE (t), value));
378 133 : }
379 :
380 : /* Add code to BLOCK assigning VALUE to the type discriminator field of the
381 : array descriptor DESC. */
382 :
383 : void
384 114 : gfc_conv_descriptor_type_set (stmtblock_t *block, tree desc, int value)
385 : {
386 114 : tree type = TREE_TYPE (desc);
387 114 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
388 :
389 114 : tree dtype = get_type_field (type, DTYPE_FIELD);
390 114 : tree field = get_type_field (TREE_TYPE (dtype), GFC_DTYPE_TYPE);
391 114 : tree type_value = build_int_cst (TREE_TYPE (field), value);
392 :
393 114 : gfc_conv_descriptor_type_set (block, desc, type_value);
394 114 : }
395 :
396 : /* Return a statement assigning VALUE to the type discriminator field of the
397 : array descriptor DESC. */
398 :
399 : tree
400 19 : gfc_conv_descriptor_type_set (tree desc, tree value)
401 : {
402 19 : stmtblock_t block;
403 :
404 19 : gfc_init_block (&block);
405 19 : gfc_conv_descriptor_type_set (&block, desc, value);
406 19 : return gfc_finish_block (&block);
407 : }
408 :
409 : /* Return a statement assigning VALUE to the type discriminator field of the
410 : array descriptor DESC. */
411 :
412 : tree
413 114 : gfc_conv_descriptor_type_set (tree desc, int value)
414 : {
415 114 : stmtblock_t block;
416 :
417 114 : gfc_init_block (&block);
418 114 : gfc_conv_descriptor_type_set (&block, desc, value);
419 114 : return gfc_finish_block (&block);
420 : }
421 :
422 :
423 : /* Return a reference to the array of dimension descriptors of the array
424 : descriptor DESC. */
425 :
426 : tree
427 1105653 : gfc_get_descriptor_dimension (tree desc)
428 : {
429 1105653 : tree field = gfc_get_descriptor_field (desc, DIMENSION_FIELD);
430 1105653 : gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
431 : && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == RECORD_TYPE);
432 1105653 : return field;
433 : }
434 :
435 :
436 : /* Return a reference to the dimension descriptor for the (zero-based) dimension
437 : DIM of the array descriptor DESC. */
438 :
439 : static tree
440 1101313 : conv_descriptor_dimension (tree desc, tree dim)
441 : {
442 1101313 : tree tmp;
443 :
444 1101313 : tmp = gfc_get_descriptor_dimension (desc);
445 :
446 1101313 : return gfc_build_array_ref (tmp, dim, NULL_TREE, true);
447 : }
448 :
449 :
450 : /* Return a reference to the coarray token field of the array descriptor
451 : DESC. */
452 :
453 : tree
454 2664 : gfc_conv_descriptor_token (tree desc)
455 : {
456 2664 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
457 2664 : tree field = gfc_get_descriptor_field (desc, CAF_TOKEN_FIELD);
458 : /* Should be a restricted pointer - except in the finalization wrapper. */
459 2664 : gcc_assert (TREE_TYPE (field) == prvoid_type_node
460 : || TREE_TYPE (field) == pvoid_type_node);
461 2664 : return field;
462 : }
463 :
464 : /* Add code to BLOCK assigning VALUE to the coarray token field of the array
465 : descriptor DESC. */
466 :
467 : void
468 731 : gfc_conv_descriptor_token_set (stmtblock_t *block, tree desc, tree value)
469 : {
470 731 : location_t loc = input_location;
471 731 : tree t = gfc_conv_descriptor_token (desc);
472 731 : gfc_add_modify_loc (loc, block, t,
473 731 : fold_convert_loc (loc, TREE_TYPE (t), value));
474 731 : }
475 :
476 :
477 : /* Return a reference to the FIELD_IDX'th subfield of the dimension descriptor
478 : of the (zero-based) dimension DIM of the array descriptor DESC. */
479 :
480 : static tree
481 1101313 : gfc_conv_descriptor_subfield (tree desc, tree dim, unsigned field_idx)
482 : {
483 1101313 : tree tmp = conv_descriptor_dimension (desc, dim);
484 1101313 : tree field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (tmp)), field_idx);
485 1101313 : gcc_assert (field != NULL_TREE);
486 :
487 1101313 : return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
488 1101313 : tmp, field, NULL_TREE);
489 : }
490 :
491 :
492 : /* Return a reference to the stride field of the (zero-based) dimension DIM of
493 : the array descriptor DESC. */
494 :
495 : static tree
496 295182 : conv_descriptor_stride (tree desc, tree dim)
497 : {
498 295182 : tree field = gfc_conv_descriptor_subfield (desc, dim, STRIDE_SUBFIELD);
499 295182 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
500 295182 : return field;
501 : }
502 :
503 : /* Return the stride value for the (zero-based) dimension DIM of the array
504 : descriptor DESC. */
505 :
506 : tree
507 179356 : gfc_conv_descriptor_stride_get (tree desc, tree dim)
508 : {
509 179356 : tree type = TREE_TYPE (desc);
510 179356 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
511 179356 : if (integer_zerop (dim)
512 179356 : && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
513 47680 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT
514 46587 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT
515 46431 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
516 46281 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT
517 46281 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT))
518 75380 : return gfc_index_one_node;
519 :
520 103976 : return conv_descriptor_stride (desc, dim);
521 : }
522 :
523 : /* Add code to BLOCK assigning VALUE to the stride field of the (zero-based)
524 : dimension DIM of the array descriptor DESC. */
525 :
526 : void
527 191206 : gfc_conv_descriptor_stride_set (stmtblock_t *block, tree desc,
528 : tree dim, tree value)
529 : {
530 191206 : tree t = conv_descriptor_stride (desc, dim);
531 191206 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
532 191206 : }
533 :
534 :
535 : /* Return a reference to the lower bound field of the (zero-based) dimension DIM
536 : of the array descriptor DESC. */
537 :
538 : static tree
539 417506 : conv_descriptor_lbound (tree desc, tree dim)
540 : {
541 417506 : tree field = gfc_conv_descriptor_subfield (desc, dim, LBOUND_SUBFIELD);
542 417506 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
543 417506 : return field;
544 : }
545 :
546 : /* Return the lower bound value for the (zero-based) dimension DIM of the array
547 : descriptor DESC. */
548 :
549 : tree
550 221022 : gfc_conv_descriptor_lbound_get (tree desc, tree dim)
551 : {
552 221022 : return conv_descriptor_lbound (desc, dim);
553 : }
554 :
555 : /* Add code to BLOCK assigning VALUE to the lower bound field of the
556 : (zero-based) dimension DIM of the array descriptor DESC. */
557 :
558 : void
559 196484 : gfc_conv_descriptor_lbound_set (stmtblock_t *block, tree desc,
560 : tree dim, tree value)
561 : {
562 196484 : tree t = conv_descriptor_lbound (desc, dim);
563 196484 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
564 196484 : }
565 :
566 :
567 : /* Return a reference to the upper bound field of the (zero-based) dimension DIM
568 : of the array descriptor DESC. */
569 :
570 : static tree
571 388625 : conv_descriptor_ubound (tree desc, tree dim)
572 : {
573 388625 : tree field = gfc_conv_descriptor_subfield (desc, dim, UBOUND_SUBFIELD);
574 388625 : gcc_assert (TREE_TYPE (field) == gfc_array_index_type);
575 388625 : return field;
576 : }
577 :
578 : /* Return the upper bound value for the (zero-based) dimension DIM of the array
579 : descriptor DESC. */
580 :
581 : tree
582 192515 : gfc_conv_descriptor_ubound_get (tree desc, tree dim)
583 : {
584 192515 : return conv_descriptor_ubound (desc, dim);
585 : }
586 :
587 : /* Add code to BLOCK assigning VALUE to the upper bound field of the
588 : (zero-based) dimension DIM of the array descriptor DESC. */
589 :
590 : void
591 196110 : gfc_conv_descriptor_ubound_set (stmtblock_t *block, tree desc,
592 : tree dim, tree value)
593 : {
594 196110 : tree t = conv_descriptor_ubound (desc, dim);
595 196110 : gfc_add_modify (block, t, fold_convert (TREE_TYPE (t), value));
596 196110 : }
597 :
598 :
599 : /* Obtain offsets for trans-types.cc(gfc_get_array_descr_info). */
600 :
601 : void
602 296565 : gfc_get_descriptor_offsets_for_info (const_tree desc_type, tree *data_off,
603 : tree *rank_off, tree *span_off,
604 : tree *dim_off, tree *dim_size,
605 : tree *stride_suboff, tree *lower_suboff,
606 : tree *upper_suboff)
607 : {
608 296565 : tree field;
609 296565 : tree type;
610 :
611 296565 : type = TYPE_MAIN_VARIANT (desc_type);
612 296565 : tree fields = TYPE_FIELDS (type);
613 296565 : field = gfc_advance_chain (fields, DATA_FIELD);
614 296565 : *data_off = byte_position (field);
615 296565 : field = gfc_advance_chain (fields, DTYPE_FIELD);
616 296565 : tree dtype_off = byte_position (field);
617 296565 : type = TREE_TYPE (field);
618 296565 : field = gfc_advance_chain (TYPE_FIELDS (type), GFC_DTYPE_RANK);
619 296565 : tree rank_suboff = byte_position (field);
620 296565 : *rank_off = fold_build2 (PLUS_EXPR, TREE_TYPE (dtype_off), dtype_off,
621 : rank_suboff);
622 296565 : field = gfc_advance_chain (fields, SPAN_FIELD);
623 296565 : *span_off = byte_position (field);
624 296565 : field = gfc_advance_chain (fields, DIMENSION_FIELD);
625 296565 : *dim_off = byte_position (field);
626 296565 : type = TREE_TYPE (TREE_TYPE (field));
627 296565 : *dim_size = TYPE_SIZE_UNIT (type);
628 296565 : field = gfc_advance_chain (TYPE_FIELDS (type), STRIDE_SUBFIELD);
629 296565 : *stride_suboff = byte_position (field);
630 296565 : field = gfc_advance_chain (TYPE_FIELDS (type), LBOUND_SUBFIELD);
631 296565 : *lower_suboff = byte_position (field);
632 296565 : field = gfc_advance_chain (TYPE_FIELDS (type), UBOUND_SUBFIELD);
633 296565 : *upper_suboff = byte_position (field);
634 296565 : }
635 :
636 :
637 : /* Array descriptor higher level routines.
638 : ******************************************************************************/
639 :
640 : /* Return a constructor for a descriptor dtype with the caracteristics given by
641 : the arguments. */
642 :
643 : tree
644 154555 : gfc_build_dtype_constructor (tree size, int type, int rank)
645 : {
646 154555 : tree field;
647 154555 : vec<constructor_elt, va_gc> *v = NULL;
648 :
649 154555 : tree dtype_type_node = get_dtype_type_node ();
650 : /* Create initialization for the element length field using the SIZE provided
651 : as input. In the scalar unlimited polymorphic case, SIZE is null and the
652 : initialization is omitted. Otherwise, the value provided as SIZE and used
653 : here is that of the declared type. In the polymorphic case (either
654 : unlimited or not), the field is expected to be explicitly set at a later
655 : time with the actual runtime value, which may be different from the
656 : initial value set here. */
657 154555 : if (size)
658 : {
659 154509 : STRIP_NOPS (size);
660 154509 : size = fold_convert (size_type_node, size);
661 154509 : field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
662 : GFC_DTYPE_ELEM_LEN);
663 154509 : CONSTRUCTOR_APPEND_ELT (v, field,
664 : fold_convert (TREE_TYPE (field), size));
665 : }
666 :
667 154555 : field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
668 : GFC_DTYPE_VERSION);
669 154555 : CONSTRUCTOR_APPEND_ELT (v, field,
670 : build_zero_cst (TREE_TYPE (field)));
671 :
672 154555 : field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
673 : GFC_DTYPE_RANK);
674 154555 : if (rank >= 0)
675 153968 : CONSTRUCTOR_APPEND_ELT (v, field,
676 : build_int_cst (TREE_TYPE (field), rank));
677 :
678 154555 : field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
679 : GFC_DTYPE_TYPE);
680 154555 : CONSTRUCTOR_APPEND_ELT (v, field,
681 : build_int_cst (TREE_TYPE (field), type));
682 :
683 154555 : return build_constructor (dtype_type_node, v);
684 : }
685 :
686 :
687 : /* Build a null array descriptor constructor. */
688 :
689 : tree
690 1154 : gfc_build_null_descriptor (tree type)
691 : {
692 1154 : tree field;
693 1154 : tree tmp;
694 :
695 1154 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
696 1154 : gcc_assert (DATA_FIELD == 0);
697 1154 : field = TYPE_FIELDS (type);
698 :
699 : /* Set a NULL data pointer. */
700 1154 : tmp = build_constructor_single (type, field, null_pointer_node);
701 1154 : TREE_CONSTANT (tmp) = 1;
702 : /* All other fields are ignored. */
703 :
704 1154 : return tmp;
705 : }
706 :
707 :
708 : /* Cleanup those #defines. */
709 :
710 : #undef DATA_FIELD
711 : #undef OFFSET_FIELD
712 : #undef DTYPE_FIELD
713 : #undef SPAN_FIELD
714 : #undef DIMENSION_FIELD
715 : #undef CAF_TOKEN_FIELD
716 :
717 : #undef STRIDE_SUBFIELD
718 : #undef LBOUND_SUBFIELD
719 : #undef UBOUND_SUBFIELD
720 :
721 : #undef GFC_DTYPE_ELEM_LEN
722 : #undef GFC_DTYPE_VERSION
723 : #undef GFC_DTYPE_RANK
724 : #undef GFC_DTYPE_TYPE
725 : #undef GFC_DTYPE_ATTRIBUTE
726 :
727 :
728 : /* Add code to BLOCK implementing the pointer assigment from NULL() to the
729 : pointer represented by the array descriptor DESCR. */
730 :
731 : void
732 692 : gfc_nullify_descriptor (stmtblock_t *block, tree descr)
733 : {
734 692 : gfc_conv_descriptor_data_set (block, descr, null_pointer_node);
735 692 : }
736 :
737 :
738 : /* Add code to BLOCK default-initializing array function result descriptor
739 : DESCR. This is used for the initialization of polymorphic allocatable
740 : function results. */
741 :
742 : void
743 34 : gfc_init_result_descriptor (stmtblock_t *block, tree descr)
744 : {
745 34 : gfc_conv_descriptor_data_set (block, descr, null_pointer_node);
746 34 : }
747 :
748 :
749 : /* Add code to BLOCK initializing array descriptor DESCR so that it represents
750 : an absent actual argument associated with an optional dummy. */
751 :
752 : void
753 696 : gfc_init_absent_descriptor (stmtblock_t *block, tree descr)
754 : {
755 696 : gfc_conv_descriptor_data_set (block, descr, null_pointer_node);
756 696 : }
757 :
758 :
759 : /* Add code to BLOCK initializing the array descriptor DESCR corresponding to
760 : the array variable SYM. This is only used for variables needing a default
761 : initialization of their descriptor. Typically allocatable (array) variables,
762 : that have an initial status of unallocated, are among them; they need their
763 : data pointer set to nullptr. */
764 :
765 : void
766 12319 : gfc_init_descriptor_variable (stmtblock_t *block, gfc_symbol *sym, tree descr)
767 : {
768 : /* NULLIFY the data pointer for non-saved allocatables, or for non-saved
769 : pointers when -fcheck=pointer is specified. */
770 12319 : if (!sym->attr.save
771 12306 : && (sym->attr.allocatable
772 3318 : || (sym->attr.pointer && (gfc_option.rtcheck & GFC_RTCHECK_POINTER))))
773 : {
774 9031 : gfc_conv_descriptor_data_set (block, descr, null_pointer_node);
775 9031 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
776 271 : gfc_conv_descriptor_token_set (block, descr, null_pointer_node);
777 : }
778 :
779 12319 : gcc_assert (sym->as && sym->as->rank>=0);
780 12319 : tree etype = gfc_get_element_type (TREE_TYPE (descr));
781 12319 : gfc_conv_descriptor_dtype_set (block, descr,
782 12319 : gfc_get_dtype_rank_type (sym->as->rank,
783 : etype));
784 12319 : }
785 :
786 :
787 : /* Create a fresh array descriptor copied from SOURCE_DESCR, with a cleared data
788 : pointer and a possibly different dtype value. Set the dtype field to DTYPE
789 : if different from NULL_TREE; otherwise set it with a default value built
790 : using SOURCE_DESCR's type. Add the copying code and any other initialization
791 : to BLOCK and return the descriptor declaration.
792 :
793 : The descriptor created by this function is used to pass to intrinsic
794 : functions from the library, when the result is assigned to a reallocatable
795 : variable. The left hand side variable descriptor is not passed directly to
796 : the library, and the unallocated descriptor this function creates is passed
797 : instead. Allocation happens in the library; deallocation of the left hand
798 : side variable data, if any, and correct bounds mapping happen outside the
799 : library, after the function returns. */
800 :
801 : tree
802 2143 : gfc_create_unallocated_library_result_descriptor (stmtblock_t *block,
803 : tree source_descr, tree dtype)
804 : {
805 : /* Unallocated, the descriptor does not have a dtype. */
806 2143 : if (dtype == NULL_TREE)
807 2130 : dtype = gfc_get_dtype (TREE_TYPE (source_descr));
808 :
809 2143 : gfc_conv_descriptor_dtype_set (block, source_descr, dtype);
810 :
811 2143 : tree res_desc = gfc_evaluate_now (source_descr, block);
812 2143 : gfc_conv_descriptor_data_set (block, res_desc, null_pointer_node);
813 :
814 2143 : return res_desc;
815 : }
816 :
817 :
818 : /* Create a new descriptor to represent a null actual argument of type TS and
819 : rank RANK passed to a dummy argument having attributes ATTR. Add
820 : initialization code to BLOCK and return the descriptor declaration. */
821 :
822 : tree
823 282 : gfc_create_null_actual_descriptor (stmtblock_t *block, gfc_typespec *ts,
824 : symbol_attribute attr, int rank)
825 : {
826 282 : tree etype = gfc_typenode_for_spec (ts);
827 :
828 282 : enum gfc_array_kind akind;
829 :
830 282 : if (attr.pointer)
831 : akind = GFC_ARRAY_POINTER_CONT;
832 114 : else if (attr.allocatable)
833 : akind = GFC_ARRAY_ALLOCATABLE;
834 : else
835 0 : akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
836 :
837 282 : tree lower[GFC_MAX_DIMENSIONS];
838 282 : tree upper[GFC_MAX_DIMENSIONS];
839 282 : memset (&lower, 0, rank * sizeof (lower[0]));
840 282 : memset (&upper, 0, rank * sizeof (upper[0]));
841 :
842 450 : tree type = gfc_get_array_type_bounds (etype, rank, 0, lower, upper, 1,
843 : akind, !(attr.pointer || attr.target));
844 282 : tree desc = gfc_create_var (type, "desc");
845 282 : DECL_ARTIFICIAL (desc) = 1;
846 :
847 282 : gfc_conv_descriptor_dtype_set (block, desc,
848 : gfc_get_dtype_rank_type (rank, etype));
849 282 : gfc_conv_descriptor_data_set (block, desc, null_pointer_node);
850 282 : gfc_conv_descriptor_span_set (block, desc,
851 : gfc_conv_descriptor_elem_len_get (desc));
852 :
853 282 : return desc;
854 : }
855 :
856 :
857 : /* Add code to BLOCK initializing the zero-rank array descriptor DESCR. Guess
858 : the dtype field initialization values using TYPE. If COND_PRESENCE is set,
859 : make the value assigned to the data field either SCALAR or nullptr depending
860 : on COND_PRESENCE; otherwise SCALAR unconditionally. */
861 :
862 : static void
863 7813 : set_descriptor_from_scalar (stmtblock_t *block, tree descr, tree type,
864 : tree scalar, tree cond_presence)
865 : {
866 7813 : gfc_conv_descriptor_dtype_set (block, descr,
867 : gfc_get_dtype_rank_type (0, type));
868 :
869 7813 : if (cond_presence)
870 192 : scalar = build3_loc (input_location, COND_EXPR,
871 96 : TREE_TYPE (scalar),
872 : cond_presence, scalar,
873 96 : fold_convert (TREE_TYPE (scalar),
874 : null_pointer_node));
875 7813 : gfc_conv_descriptor_data_set (block, descr, scalar);
876 7813 : }
877 :
878 :
879 : /* Add code to BLOCK initializing the zero-rank array descriptor DESCR, so that
880 : it represents the same data as the pointer-typed middle-end expression
881 : SCALAR. If COND_PRESENCE is set, make the value assigned to the data field
882 : either SCALAR or nullptr depending on COND_PRESENCE; otherwise SCALAR
883 : unconditionally. This is used to implement the argument association between
884 : the actual argument SCALAR and an assumed-rank dummy argument. */
885 :
886 : void
887 335 : gfc_set_descriptor_from_scalar (stmtblock_t *block, tree descr,
888 : tree scalar, tree cond_presence)
889 : {
890 335 : tree scalar_type = TREE_TYPE (scalar);
891 335 : tree etype = POINTER_TYPE_P (scalar_type)
892 335 : ? TREE_TYPE (scalar_type)
893 335 : : scalar_type;
894 335 : set_descriptor_from_scalar (block, descr, etype, scalar, cond_presence);
895 335 : gfc_copy_coarray_desc_part (block, descr, scalar);
896 335 : }
897 :
898 :
899 : /* Add code to BLOCK initializing the zero-rank array descriptor DESCR, so that
900 : it represents the same data as the scalar reference SCALAR. This is used to
901 : implement the argument association between the actual argument SCALAR and an
902 : assumed-rank dummy argument. */
903 :
904 : void
905 7061 : gfc_set_descriptor_from_scalar (stmtblock_t *block, tree descr, tree scalar)
906 : {
907 7061 : tree etype = TREE_TYPE (scalar);
908 7061 : if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
909 1070 : scalar = gfc_build_addr_expr (NULL_TREE, scalar);
910 7061 : set_descriptor_from_scalar (block, descr, etype, scalar, NULL_TREE);
911 7061 : gfc_conv_descriptor_span_set (block, descr,
912 : gfc_conv_descriptor_elem_len_get (descr));
913 7061 : }
914 :
915 :
916 : /* Return true if EXPR is a polymorphic reference; otherwise return false. If
917 : CLASS_REF isn't nullptr, set its target to a reference to the class container
918 : in the true case, and leave it unmodified in the false case. If VAL_PTR
919 : isn't nullptr, set it to a pointer to the actual data wrapped by the class
920 : container similarly. */
921 :
922 : static bool
923 417 : is_polymorphic_ref (tree expr, tree *class_ref = nullptr,
924 : tree *val_ptr = nullptr)
925 : {
926 417 : tree valptr_candidate = NULL_TREE;
927 417 : tree class_pointer = NULL_TREE;
928 417 : tree class_container = NULL_TREE;
929 :
930 417 : tree e = expr;
931 1217 : while (true)
932 : {
933 817 : STRIP_NOPS (e);
934 817 : if (TREE_CODE (e) == ADDR_EXPR)
935 : ;
936 903 : else if (POINTER_TYPE_P (TREE_TYPE (e))
937 521 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (e))))
938 : {
939 : class_pointer = e;
940 : break;
941 : }
942 443 : else if (GFC_CLASS_TYPE_P (TREE_TYPE (e)))
943 : {
944 : class_container = e;
945 : break;
946 : }
947 104 : else if (TREE_CODE (e) == COMPONENT_REF)
948 : {
949 86 : tree base_obj = TREE_OPERAND (e, 0);
950 86 : tree field_decl = TREE_OPERAND (e, 1);
951 86 : tree field_name = DECL_NAME (field_decl);
952 86 : if (!((GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (base_obj))
953 43 : && strcmp (IDENTIFIER_POINTER (field_name), "data") == 0)
954 43 : || (GFC_CLASS_TYPE_P (TREE_TYPE (base_obj))
955 43 : && strcmp (IDENTIFIER_POINTER (field_name), "_data") == 0)))
956 : return false;
957 : }
958 : else if (!(TREE_CODE (e) == INDIRECT_REF
959 : || TREE_CODE (e) == ARRAY_REF
960 : || TREE_CODE (e) == POINTER_PLUS_EXPR))
961 : return false;
962 :
963 400 : if (valptr_candidate == nullptr
964 339 : && TREE_CODE (e) != INDIRECT_REF
965 739 : && !(GFC_CLASS_TYPE_P (TREE_TYPE (e))
966 339 : || (POINTER_TYPE_P (TREE_TYPE (e))
967 339 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (e))))))
968 : valptr_candidate = e;
969 :
970 400 : e = TREE_OPERAND (e, 0);
971 400 : }
972 :
973 417 : gcc_assert (class_container != nullptr || class_pointer != nullptr);
974 :
975 417 : bool valptr_from_class_container = false;
976 417 : if (val_ptr != nullptr)
977 : {
978 417 : if (valptr_candidate != nullptr)
979 : {
980 43 : if (POINTER_TYPE_P (TREE_TYPE (valptr_candidate)))
981 43 : *val_ptr = valptr_candidate;
982 : else
983 0 : *val_ptr = gfc_build_addr_expr (NULL_TREE, valptr_candidate);
984 : }
985 : else
986 : valptr_from_class_container = true;
987 : }
988 :
989 417 : if (class_container == nullptr
990 78 : && (class_ref != nullptr
991 78 : || valptr_from_class_container))
992 78 : class_container = build_fold_indirect_ref_loc (input_location,
993 : class_pointer);
994 417 : if (class_ref != nullptr)
995 417 : *class_ref = class_container;
996 :
997 417 : if (valptr_from_class_container)
998 374 : *val_ptr = gfc_class_data_get (class_container);
999 :
1000 : return true;
1001 : }
1002 :
1003 :
1004 : /* Add code to BLOCK initializing the zero-rank array descriptor DESCR, so that
1005 : it represents the same data as the class descriptor reference SCALAR. This
1006 : is used to implement the argument association between the actual argument
1007 : SCALAR and an assumed-rank dummy argument. */
1008 :
1009 : void
1010 417 : gfc_set_descriptor_from_scalar_class (stmtblock_t *block, tree descr,
1011 : tree scalar)
1012 : {
1013 417 : tree class_ref = nullptr;
1014 417 : tree val_ptr = nullptr;
1015 417 : bool is_class = is_polymorphic_ref (scalar, &class_ref, &val_ptr);
1016 417 : gcc_assert (is_class);
1017 :
1018 417 : tree tmp = val_ptr;
1019 417 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1020 0 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1021 :
1022 417 : set_descriptor_from_scalar (block, descr, TREE_TYPE (class_ref), tmp,
1023 : NULL_TREE);
1024 :
1025 : /* Set the elem_len field... */
1026 417 : tree vptr = gfc_class_vptr_get (class_ref);
1027 417 : stmtblock_t b;
1028 417 : gfc_init_block (&b);
1029 417 : tree len = gfc_vptr_size_get (vptr);
1030 417 : len = gfc_resize_class_size_with_len (nullptr, class_ref, len);
1031 417 : gfc_conv_descriptor_elem_len_set (&b, descr, len);
1032 417 : tree len_init = gfc_finish_block (&b);
1033 : /* ... if the vptr is non-NULL. */
1034 417 : tree vptr_non_null = fold_build2_loc (input_location, NE_EXPR,
1035 : boolean_type_node, vptr,
1036 417 : build_zero_cst (TREE_TYPE (vptr)));
1037 417 : len_init = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1038 : vptr_non_null, len_init,
1039 : build_empty_stmt (input_location));
1040 417 : gfc_add_expr_to_block (block, len_init);
1041 417 : }
1042 :
1043 :
1044 : /* For an array descriptor, get the total number of elements. This is just
1045 : the product of the extents along from_dim to to_dim. */
1046 :
1047 : static tree
1048 2363 : gfc_conv_descriptor_size_1 (tree desc, int from_dim, int to_dim)
1049 : {
1050 2363 : tree res;
1051 2363 : int dim;
1052 :
1053 2363 : res = gfc_index_one_node;
1054 :
1055 5652 : for (dim = from_dim; dim < to_dim; ++dim)
1056 : {
1057 3289 : tree lbound;
1058 3289 : tree ubound;
1059 3289 : tree extent;
1060 :
1061 3289 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
1062 3289 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
1063 :
1064 3289 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1065 3289 : res = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1066 : res, extent);
1067 : }
1068 :
1069 2363 : return res;
1070 : }
1071 :
1072 :
1073 : /* Full size of an array. */
1074 :
1075 : tree
1076 2283 : gfc_conv_descriptor_size (tree desc, int rank)
1077 : {
1078 2283 : return gfc_conv_descriptor_size_1 (desc, 0, rank);
1079 : }
1080 :
1081 :
1082 : /* Size of a coarray for all dimensions but the last. */
1083 :
1084 : tree
1085 80 : gfc_conv_descriptor_cosize (tree desc, int rank, int corank)
1086 : {
1087 80 : return gfc_conv_descriptor_size_1 (desc, rank, rank + corank - 1);
1088 : }
1089 :
1090 :
1091 : /* Modify a descriptor such that the lbound of a given dimension is the value
1092 : specified. This also updates ubound and offset accordingly. */
1093 :
1094 : void
1095 1141 : gfc_conv_shift_descriptor_lbound (stmtblock_t* block, tree desc,
1096 : int dim, tree new_lbound)
1097 : {
1098 1141 : tree offs, ubound, lbound, stride;
1099 1141 : tree diff, offs_diff;
1100 :
1101 1141 : new_lbound = fold_convert (gfc_array_index_type, new_lbound);
1102 :
1103 1141 : offs = gfc_conv_descriptor_offset_get (desc);
1104 1141 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
1105 1141 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
1106 1141 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[dim]);
1107 :
1108 : /* Get difference (new - old) by which to shift stuff. */
1109 1141 : diff = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1110 : new_lbound, lbound);
1111 :
1112 : /* Shift ubound and offset accordingly. This has to be done before
1113 : updating the lbound, as they depend on the lbound expression! */
1114 1141 : ubound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1115 : ubound, diff);
1116 1141 : gfc_conv_descriptor_ubound_set (block, desc, gfc_rank_cst[dim], ubound);
1117 1141 : offs_diff = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1118 : diff, stride);
1119 1141 : offs = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1120 : offs, offs_diff);
1121 1141 : gfc_conv_descriptor_offset_set (block, desc, offs);
1122 :
1123 : /* Finally set lbound to value we want. */
1124 1141 : gfc_conv_descriptor_lbound_set (block, desc, gfc_rank_cst[dim], new_lbound);
1125 1141 : }
1126 :
1127 :
1128 : /* Add code to BLOCK copying the cobounds from array descriptor SRC to array
1129 : descriptor DEST. The values are picked from SRC's type if they are
1130 : set there. Otherwise the runtime values are used with references to SRC's
1131 : fields. The type of DEST is not enriched with the cobounds; only
1132 : assignments setting DEST's fields at runtime are generated. If SRC is not a
1133 : coarray, no code is generated. */
1134 :
1135 : void
1136 2349 : gfc_copy_coarray_desc_part (stmtblock_t *block, tree dest, tree src)
1137 : {
1138 2349 : tree src_type = TREE_TYPE (src);
1139 2349 : if (TYPE_LANG_SPECIFIC (src_type) && TYPE_LANG_SPECIFIC (src_type)->corank)
1140 : {
1141 135 : struct lang_type *lang_specific = TYPE_LANG_SPECIFIC (src_type);
1142 270 : for (int c = 0; c < lang_specific->corank; ++c)
1143 : {
1144 135 : int dim = lang_specific->rank + c;
1145 135 : tree codim = gfc_rank_cst[dim];
1146 :
1147 135 : if (lang_specific->lbound[dim])
1148 54 : gfc_conv_descriptor_lbound_set (block, dest, codim,
1149 : lang_specific->lbound[dim]);
1150 : else
1151 81 : gfc_conv_descriptor_lbound_set (
1152 : block, dest, codim, gfc_conv_descriptor_lbound_get (src, codim));
1153 135 : if (dim + 1 < lang_specific->corank)
1154 : {
1155 0 : if (lang_specific->ubound[dim])
1156 0 : gfc_conv_descriptor_ubound_set (block, dest, codim,
1157 : lang_specific->ubound[dim]);
1158 : else
1159 0 : gfc_conv_descriptor_ubound_set (
1160 : block, dest, codim,
1161 : gfc_conv_descriptor_ubound_get (src, codim));
1162 : }
1163 : }
1164 : }
1165 2349 : }
1166 :
1167 :
1168 : void
1169 1778 : gfc_copy_descriptor (stmtblock_t *block, tree dst, tree src, int rank)
1170 : {
1171 1778 : int n;
1172 1778 : tree dim;
1173 1778 : tree tmp;
1174 1778 : tree tmp2;
1175 1778 : tree size;
1176 1778 : tree offset;
1177 :
1178 1778 : offset = gfc_index_zero_node;
1179 :
1180 : /* Use memcpy to copy the descriptor. The size is the minimum of
1181 : the sizes of 'src' and 'dst'. This avoids a non-trivial conversion. */
1182 1778 : tmp = TYPE_SIZE_UNIT (TREE_TYPE (src));
1183 1778 : tmp2 = TYPE_SIZE_UNIT (TREE_TYPE (dst));
1184 1778 : size = fold_build2_loc (input_location, MIN_EXPR,
1185 1778 : TREE_TYPE (tmp), tmp, tmp2);
1186 1778 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
1187 1778 : tmp = build_call_expr_loc (input_location, tmp, 3,
1188 : gfc_build_addr_expr (NULL_TREE, dst),
1189 : gfc_build_addr_expr (NULL_TREE, src),
1190 : fold_convert (size_type_node, size));
1191 1778 : gfc_add_expr_to_block (block, tmp);
1192 :
1193 : /* Set the offset correctly. */
1194 8792 : for (n = 0; n < rank; n++)
1195 : {
1196 5236 : dim = gfc_rank_cst[n];
1197 5236 : tmp = gfc_conv_descriptor_lbound_get (src, dim);
1198 5236 : tmp2 = gfc_conv_descriptor_stride_get (src, dim);
1199 5236 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
1200 : tmp, tmp2);
1201 5236 : offset = fold_build2_loc (input_location, MINUS_EXPR,
1202 5236 : TREE_TYPE (offset), offset, tmp);
1203 5236 : offset = gfc_evaluate_now (offset, block);
1204 : }
1205 :
1206 1778 : gfc_conv_descriptor_offset_set (block, dst, offset);
1207 1778 : }
1208 :
1209 :
1210 : /* Extend the data in array DESC by EXTRA elements. */
1211 :
1212 : void
1213 1068 : gfc_grow_array (stmtblock_t * pblock, tree desc, tree extra)
1214 : {
1215 1068 : tree arg0, arg1;
1216 1068 : tree tmp;
1217 1068 : tree size;
1218 1068 : tree ubound;
1219 :
1220 1068 : if (integer_zerop (extra))
1221 : return;
1222 :
1223 1038 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[0]);
1224 :
1225 : /* Add EXTRA to the upper bound. */
1226 1038 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1227 : ubound, extra);
1228 1038 : gfc_conv_descriptor_ubound_set (pblock, desc, gfc_rank_cst[0], tmp);
1229 :
1230 : /* Get the value of the current data pointer. */
1231 1038 : arg0 = gfc_conv_descriptor_data_get (desc);
1232 :
1233 : /* Calculate the new array size. */
1234 1038 : size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
1235 1038 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1236 : ubound, gfc_index_one_node);
1237 1038 : arg1 = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
1238 : fold_convert (size_type_node, tmp),
1239 : fold_convert (size_type_node, size));
1240 :
1241 : /* Call the realloc() function. */
1242 1038 : tmp = gfc_call_realloc (pblock, arg0, arg1);
1243 1038 : gfc_conv_descriptor_data_set (pblock, desc, tmp);
1244 : }
|