1 /* IO Code translation/library interface
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "stringpool.h"
29 #include "fold-const.h"
30 #include "stor-layout.h"
31 #include "trans-stmt.h"
32 #include "trans-array.h"
33 #include "trans-types.h"
34 #include "trans-const.h"
36 /* Members of the ioparm structure. */
65 typedef struct GTY(()) gfc_st_parameter_field
{
68 enum ioparam_type param_type
;
69 enum iofield_type type
;
73 gfc_st_parameter_field
;
75 typedef struct GTY(()) gfc_st_parameter
{
83 #define IOPARM(param_type, name, mask, type) IOPARM_##param_type##_##name,
89 static GTY(()) gfc_st_parameter st_parameter
[] =
100 static GTY(()) gfc_st_parameter_field st_parameter_field
[] =
102 #define IOPARM(param_type, name, mask, type) \
103 { #name, mask, IOPARM_ptype_##param_type, IOPARM_type_##type, NULL, NULL },
104 #include "ioparm.def"
106 { NULL
, 0, (enum ioparam_type
) 0, (enum iofield_type
) 0, NULL
, NULL
}
109 /* Library I/O subroutines */
118 IOCALL_X_INTEGER_WRITE
,
120 IOCALL_X_LOGICAL_WRITE
,
122 IOCALL_X_CHARACTER_WRITE
,
123 IOCALL_X_CHARACTER_WIDE
,
124 IOCALL_X_CHARACTER_WIDE_WRITE
,
128 IOCALL_X_COMPLEX_WRITE
,
130 IOCALL_X_REAL128_WRITE
,
132 IOCALL_X_COMPLEX128_WRITE
,
134 IOCALL_X_ARRAY_WRITE
,
140 IOCALL_IOLENGTH_DONE
,
146 IOCALL_SET_NML_DTIO_VAL
,
147 IOCALL_SET_NML_VAL_DIM
,
152 static GTY(()) tree iocall
[IOCALL_NUM
];
154 /* Variable for keeping track of what the last data transfer statement
155 was. Used for deciding which subroutine to call when the data
156 transfer is complete. */
157 static enum { READ
, WRITE
, IOLENGTH
} last_dt
;
159 /* The data transfer parameter block that should be shared by all
160 data transfer calls belonging to the same read/write/iolength. */
161 static GTY(()) tree dt_parm
;
162 static stmtblock_t
*dt_post_end_block
;
165 gfc_build_st_parameter (enum ioparam_type ptype
, tree
*types
)
168 gfc_st_parameter_field
*p
;
171 tree t
= make_node (RECORD_TYPE
);
174 len
= strlen (st_parameter
[ptype
].name
);
175 gcc_assert (len
<= sizeof (name
) - sizeof ("__st_parameter_"));
176 memcpy (name
, "__st_parameter_", sizeof ("__st_parameter_"));
177 memcpy (name
+ sizeof ("__st_parameter_") - 1, st_parameter
[ptype
].name
,
179 TYPE_NAME (t
) = get_identifier (name
);
181 for (type
= 0, p
= st_parameter_field
; type
< IOPARM_field_num
; type
++, p
++)
182 if (p
->param_type
== ptype
)
185 case IOPARM_type_int4
:
186 case IOPARM_type_intio
:
187 case IOPARM_type_pint4
:
188 case IOPARM_type_pintio
:
189 case IOPARM_type_parray
:
190 case IOPARM_type_pchar
:
191 case IOPARM_type_pad
:
192 p
->field
= gfc_add_field_to_struct (t
, get_identifier (p
->name
),
193 types
[p
->type
], &chain
);
195 case IOPARM_type_char1
:
196 p
->field
= gfc_add_field_to_struct (t
, get_identifier (p
->name
),
197 pchar_type_node
, &chain
);
199 case IOPARM_type_char2
:
200 len
= strlen (p
->name
);
201 gcc_assert (len
<= sizeof (name
) - sizeof ("_len"));
202 memcpy (name
, p
->name
, len
);
203 memcpy (name
+ len
, "_len", sizeof ("_len"));
204 p
->field_len
= gfc_add_field_to_struct (t
, get_identifier (name
),
205 gfc_charlen_type_node
,
207 if (p
->type
== IOPARM_type_char2
)
208 p
->field
= gfc_add_field_to_struct (t
, get_identifier (p
->name
),
209 pchar_type_node
, &chain
);
211 case IOPARM_type_common
:
213 = gfc_add_field_to_struct (t
,
214 get_identifier (p
->name
),
215 st_parameter
[IOPARM_ptype_common
].type
,
218 case IOPARM_type_num
:
223 st_parameter
[ptype
].type
= t
;
227 /* Build code to test an error condition and call generate_error if needed.
228 Note: This builds calls to generate_error in the runtime library function.
229 The function generate_error is dependent on certain parameters in the
230 st_parameter_common flags to be set. (See libgfortran/runtime/error.c)
231 Therefore, the code to set these flags must be generated before
232 this function is used. */
235 gfc_trans_io_runtime_check (bool has_iostat
, tree cond
, tree var
,
236 int error_code
, const char * msgid
,
237 stmtblock_t
* pblock
)
242 tree arg1
, arg2
, arg3
;
245 if (integer_zerop (cond
))
248 /* The code to generate the error. */
249 gfc_start_block (&block
);
252 gfc_add_expr_to_block (&block
, build_predict_expr (PRED_FORTRAN_FAIL_IO
,
255 gfc_add_expr_to_block (&block
, build_predict_expr (PRED_NORETURN
,
258 arg1
= gfc_build_addr_expr (NULL_TREE
, var
);
260 arg2
= build_int_cst (integer_type_node
, error_code
),
262 message
= xasprintf ("%s", _(msgid
));
263 arg3
= gfc_build_addr_expr (pchar_type_node
,
264 gfc_build_localized_cstring_const (message
));
267 tmp
= build_call_expr_loc (input_location
,
268 gfor_fndecl_generate_error
, 3, arg1
, arg2
, arg3
);
270 gfc_add_expr_to_block (&block
, tmp
);
272 body
= gfc_finish_block (&block
);
274 if (integer_onep (cond
))
276 gfc_add_expr_to_block (pblock
, body
);
280 tmp
= build3_v (COND_EXPR
, cond
, body
, build_empty_stmt (input_location
));
281 gfc_add_expr_to_block (pblock
, tmp
);
286 /* Create function decls for IO library functions. */
289 gfc_build_io_library_fndecls (void)
291 tree types
[IOPARM_type_num
], pad_idx
, gfc_int4_type_node
;
292 tree gfc_intio_type_node
;
293 tree parm_type
, dt_parm_type
;
294 HOST_WIDE_INT pad_size
;
297 types
[IOPARM_type_int4
] = gfc_int4_type_node
= gfc_get_int_type (4);
298 types
[IOPARM_type_intio
] = gfc_intio_type_node
299 = gfc_get_int_type (gfc_intio_kind
);
300 types
[IOPARM_type_pint4
] = build_pointer_type (gfc_int4_type_node
);
301 types
[IOPARM_type_pintio
]
302 = build_pointer_type (gfc_intio_type_node
);
303 types
[IOPARM_type_parray
] = pchar_type_node
;
304 types
[IOPARM_type_pchar
] = pchar_type_node
;
305 pad_size
= 16 * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (pchar_type_node
));
306 pad_size
+= 32 * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (integer_type_node
));
307 pad_idx
= build_index_type (size_int (pad_size
- 1));
308 types
[IOPARM_type_pad
] = build_array_type (char_type_node
, pad_idx
);
310 /* pad actually contains pointers and integers so it needs to have an
311 alignment that is at least as large as the needed alignment for those
312 types. See the st_parameter_dt structure in libgfortran/io/io.h for
313 what really goes into this space. */
314 SET_TYPE_ALIGN (types
[IOPARM_type_pad
], MAX (TYPE_ALIGN (pchar_type_node
),
315 TYPE_ALIGN (gfc_get_int_type (gfc_intio_kind
))));
317 for (ptype
= IOPARM_ptype_common
; ptype
< IOPARM_ptype_num
; ptype
++)
318 gfc_build_st_parameter ((enum ioparam_type
) ptype
, types
);
320 /* Define the transfer functions. */
322 dt_parm_type
= build_pointer_type (st_parameter
[IOPARM_ptype_dt
].type
);
324 iocall
[IOCALL_X_INTEGER
] = gfc_build_library_function_decl_with_spec (
325 get_identifier (PREFIX("transfer_integer")), ".wW",
326 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
328 iocall
[IOCALL_X_INTEGER_WRITE
] = gfc_build_library_function_decl_with_spec (
329 get_identifier (PREFIX("transfer_integer_write")), ".wR",
330 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
332 iocall
[IOCALL_X_LOGICAL
] = gfc_build_library_function_decl_with_spec (
333 get_identifier (PREFIX("transfer_logical")), ".wW",
334 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
336 iocall
[IOCALL_X_LOGICAL_WRITE
] = gfc_build_library_function_decl_with_spec (
337 get_identifier (PREFIX("transfer_logical_write")), ".wR",
338 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
340 iocall
[IOCALL_X_CHARACTER
] = gfc_build_library_function_decl_with_spec (
341 get_identifier (PREFIX("transfer_character")), ".wW",
342 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
344 iocall
[IOCALL_X_CHARACTER_WRITE
] = gfc_build_library_function_decl_with_spec (
345 get_identifier (PREFIX("transfer_character_write")), ".wR",
346 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
348 iocall
[IOCALL_X_CHARACTER_WIDE
] = gfc_build_library_function_decl_with_spec (
349 get_identifier (PREFIX("transfer_character_wide")), ".wW",
350 void_type_node
, 4, dt_parm_type
, pvoid_type_node
,
351 gfc_charlen_type_node
, gfc_int4_type_node
);
353 iocall
[IOCALL_X_CHARACTER_WIDE_WRITE
] =
354 gfc_build_library_function_decl_with_spec (
355 get_identifier (PREFIX("transfer_character_wide_write")), ".wR",
356 void_type_node
, 4, dt_parm_type
, pvoid_type_node
,
357 gfc_charlen_type_node
, gfc_int4_type_node
);
359 iocall
[IOCALL_X_REAL
] = gfc_build_library_function_decl_with_spec (
360 get_identifier (PREFIX("transfer_real")), ".wW",
361 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
363 iocall
[IOCALL_X_REAL_WRITE
] = gfc_build_library_function_decl_with_spec (
364 get_identifier (PREFIX("transfer_real_write")), ".wR",
365 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
367 iocall
[IOCALL_X_COMPLEX
] = gfc_build_library_function_decl_with_spec (
368 get_identifier (PREFIX("transfer_complex")), ".wW",
369 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
371 iocall
[IOCALL_X_COMPLEX_WRITE
] = gfc_build_library_function_decl_with_spec (
372 get_identifier (PREFIX("transfer_complex_write")), ".wR",
373 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
375 /* Version for __float128. */
376 iocall
[IOCALL_X_REAL128
] = gfc_build_library_function_decl_with_spec (
377 get_identifier (PREFIX("transfer_real128")), ".wW",
378 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
380 iocall
[IOCALL_X_REAL128_WRITE
] = gfc_build_library_function_decl_with_spec (
381 get_identifier (PREFIX("transfer_real128_write")), ".wR",
382 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
384 iocall
[IOCALL_X_COMPLEX128
] = gfc_build_library_function_decl_with_spec (
385 get_identifier (PREFIX("transfer_complex128")), ".wW",
386 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
388 iocall
[IOCALL_X_COMPLEX128_WRITE
] = gfc_build_library_function_decl_with_spec (
389 get_identifier (PREFIX("transfer_complex128_write")), ".wR",
390 void_type_node
, 3, dt_parm_type
, pvoid_type_node
, gfc_int4_type_node
);
392 iocall
[IOCALL_X_ARRAY
] = gfc_build_library_function_decl_with_spec (
393 get_identifier (PREFIX("transfer_array")), ".ww",
394 void_type_node
, 4, dt_parm_type
, pvoid_type_node
,
395 integer_type_node
, gfc_charlen_type_node
);
397 iocall
[IOCALL_X_ARRAY_WRITE
] = gfc_build_library_function_decl_with_spec (
398 get_identifier (PREFIX("transfer_array_write")), ".wr",
399 void_type_node
, 4, dt_parm_type
, pvoid_type_node
,
400 integer_type_node
, gfc_charlen_type_node
);
402 iocall
[IOCALL_X_DERIVED
] = gfc_build_library_function_decl_with_spec (
403 get_identifier (PREFIX("transfer_derived")), ".wrR",
404 void_type_node
, 2, dt_parm_type
, pvoid_type_node
, pchar_type_node
);
406 /* Library entry points */
408 iocall
[IOCALL_READ
] = gfc_build_library_function_decl_with_spec (
409 get_identifier (PREFIX("st_read")), ".w",
410 void_type_node
, 1, dt_parm_type
);
412 iocall
[IOCALL_WRITE
] = gfc_build_library_function_decl_with_spec (
413 get_identifier (PREFIX("st_write")), ".w",
414 void_type_node
, 1, dt_parm_type
);
416 parm_type
= build_pointer_type (st_parameter
[IOPARM_ptype_open
].type
);
417 iocall
[IOCALL_OPEN
] = gfc_build_library_function_decl_with_spec (
418 get_identifier (PREFIX("st_open")), ".w",
419 void_type_node
, 1, parm_type
);
421 parm_type
= build_pointer_type (st_parameter
[IOPARM_ptype_close
].type
);
422 iocall
[IOCALL_CLOSE
] = gfc_build_library_function_decl_with_spec (
423 get_identifier (PREFIX("st_close")), ".w",
424 void_type_node
, 1, parm_type
);
426 parm_type
= build_pointer_type (st_parameter
[IOPARM_ptype_inquire
].type
);
427 iocall
[IOCALL_INQUIRE
] = gfc_build_library_function_decl_with_spec (
428 get_identifier (PREFIX("st_inquire")), ".w",
429 void_type_node
, 1, parm_type
);
431 iocall
[IOCALL_IOLENGTH
] = gfc_build_library_function_decl_with_spec(
432 get_identifier (PREFIX("st_iolength")), ".w",
433 void_type_node
, 1, dt_parm_type
);
435 /* TODO: Change when asynchronous I/O is implemented. */
436 parm_type
= build_pointer_type (st_parameter
[IOPARM_ptype_wait
].type
);
437 iocall
[IOCALL_WAIT
] = gfc_build_library_function_decl_with_spec (
438 get_identifier (PREFIX("st_wait")), ".X",
439 void_type_node
, 1, parm_type
);
441 parm_type
= build_pointer_type (st_parameter
[IOPARM_ptype_filepos
].type
);
442 iocall
[IOCALL_REWIND
] = gfc_build_library_function_decl_with_spec (
443 get_identifier (PREFIX("st_rewind")), ".w",
444 void_type_node
, 1, parm_type
);
446 iocall
[IOCALL_BACKSPACE
] = gfc_build_library_function_decl_with_spec (
447 get_identifier (PREFIX("st_backspace")), ".w",
448 void_type_node
, 1, parm_type
);
450 iocall
[IOCALL_ENDFILE
] = gfc_build_library_function_decl_with_spec (
451 get_identifier (PREFIX("st_endfile")), ".w",
452 void_type_node
, 1, parm_type
);
454 iocall
[IOCALL_FLUSH
] = gfc_build_library_function_decl_with_spec (
455 get_identifier (PREFIX("st_flush")), ".w",
456 void_type_node
, 1, parm_type
);
458 /* Library helpers */
460 iocall
[IOCALL_READ_DONE
] = gfc_build_library_function_decl_with_spec (
461 get_identifier (PREFIX("st_read_done")), ".w",
462 void_type_node
, 1, dt_parm_type
);
464 iocall
[IOCALL_WRITE_DONE
] = gfc_build_library_function_decl_with_spec (
465 get_identifier (PREFIX("st_write_done")), ".w",
466 void_type_node
, 1, dt_parm_type
);
468 iocall
[IOCALL_IOLENGTH_DONE
] = gfc_build_library_function_decl_with_spec (
469 get_identifier (PREFIX("st_iolength_done")), ".w",
470 void_type_node
, 1, dt_parm_type
);
472 iocall
[IOCALL_SET_NML_VAL
] = gfc_build_library_function_decl_with_spec (
473 get_identifier (PREFIX("st_set_nml_var")), ".w.R",
474 void_type_node
, 6, dt_parm_type
, pvoid_type_node
, pvoid_type_node
,
475 gfc_int4_type_node
, gfc_charlen_type_node
, gfc_int4_type_node
);
477 iocall
[IOCALL_SET_NML_DTIO_VAL
] = gfc_build_library_function_decl_with_spec (
478 get_identifier (PREFIX("st_set_nml_dtio_var")), ".w.R",
479 void_type_node
, 8, dt_parm_type
, pvoid_type_node
, pvoid_type_node
,
480 gfc_int4_type_node
, gfc_charlen_type_node
, gfc_int4_type_node
,
481 pvoid_type_node
, pvoid_type_node
);
483 iocall
[IOCALL_SET_NML_VAL_DIM
] = gfc_build_library_function_decl_with_spec (
484 get_identifier (PREFIX("st_set_nml_var_dim")), ".w",
485 void_type_node
, 5, dt_parm_type
, gfc_int4_type_node
,
486 gfc_array_index_type
, gfc_array_index_type
, gfc_array_index_type
);
491 set_parameter_tree (stmtblock_t
*block
, tree var
, enum iofield type
, tree value
)
494 gfc_st_parameter_field
*p
= &st_parameter_field
[type
];
496 if (p
->param_type
== IOPARM_ptype_common
)
497 var
= fold_build3_loc (input_location
, COMPONENT_REF
,
498 st_parameter
[IOPARM_ptype_common
].type
,
499 var
, TYPE_FIELDS (TREE_TYPE (var
)), NULL_TREE
);
500 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (p
->field
),
501 var
, p
->field
, NULL_TREE
);
502 gfc_add_modify (block
, tmp
, value
);
506 /* Generate code to store an integer constant into the
507 st_parameter_XXX structure. */
510 set_parameter_const (stmtblock_t
*block
, tree var
, enum iofield type
,
513 gfc_st_parameter_field
*p
= &st_parameter_field
[type
];
515 set_parameter_tree (block
, var
, type
,
516 build_int_cst (TREE_TYPE (p
->field
), val
));
521 /* Generate code to store a non-string I/O parameter into the
522 st_parameter_XXX structure. This is a pass by value. */
525 set_parameter_value (stmtblock_t
*block
, tree var
, enum iofield type
,
530 gfc_st_parameter_field
*p
= &st_parameter_field
[type
];
531 tree dest_type
= TREE_TYPE (p
->field
);
533 gfc_init_se (&se
, NULL
);
534 gfc_conv_expr_val (&se
, e
);
536 se
.expr
= convert (dest_type
, se
.expr
);
537 gfc_add_block_to_block (block
, &se
.pre
);
539 if (p
->param_type
== IOPARM_ptype_common
)
540 var
= fold_build3_loc (input_location
, COMPONENT_REF
,
541 st_parameter
[IOPARM_ptype_common
].type
,
542 var
, TYPE_FIELDS (TREE_TYPE (var
)), NULL_TREE
);
544 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
, dest_type
, var
,
545 p
->field
, NULL_TREE
);
546 gfc_add_modify (block
, tmp
, se
.expr
);
551 /* Similar to set_parameter_value except generate runtime
555 set_parameter_value_chk (stmtblock_t
*block
, bool has_iostat
, tree var
,
556 enum iofield type
, gfc_expr
*e
)
560 gfc_st_parameter_field
*p
= &st_parameter_field
[type
];
561 tree dest_type
= TREE_TYPE (p
->field
);
563 gfc_init_se (&se
, NULL
);
564 gfc_conv_expr_val (&se
, e
);
566 /* If we're storing a UNIT number, we need to check it first. */
567 if (type
== IOPARM_common_unit
&& e
->ts
.kind
> 4)
572 /* Don't evaluate the UNIT number multiple times. */
573 se
.expr
= gfc_evaluate_now (se
.expr
, &se
.pre
);
575 /* UNIT numbers should be greater than the min. */
576 i
= gfc_validate_kind (BT_INTEGER
, 4, false);
577 val
= gfc_conv_mpz_to_tree (gfc_integer_kinds
[i
].pedantic_min_int
, 4);
578 cond
= fold_build2_loc (input_location
, LT_EXPR
, boolean_type_node
,
580 fold_convert (TREE_TYPE (se
.expr
), val
));
581 gfc_trans_io_runtime_check (has_iostat
, cond
, var
, LIBERROR_BAD_UNIT
,
582 "Unit number in I/O statement too small",
585 /* UNIT numbers should be less than the max. */
586 val
= gfc_conv_mpz_to_tree (gfc_integer_kinds
[i
].huge
, 4);
587 cond
= fold_build2_loc (input_location
, GT_EXPR
, boolean_type_node
,
589 fold_convert (TREE_TYPE (se
.expr
), val
));
590 gfc_trans_io_runtime_check (has_iostat
, cond
, var
, LIBERROR_BAD_UNIT
,
591 "Unit number in I/O statement too large",
595 se
.expr
= convert (dest_type
, se
.expr
);
596 gfc_add_block_to_block (block
, &se
.pre
);
598 if (p
->param_type
== IOPARM_ptype_common
)
599 var
= fold_build3_loc (input_location
, COMPONENT_REF
,
600 st_parameter
[IOPARM_ptype_common
].type
,
601 var
, TYPE_FIELDS (TREE_TYPE (var
)), NULL_TREE
);
603 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
, dest_type
, var
,
604 p
->field
, NULL_TREE
);
605 gfc_add_modify (block
, tmp
, se
.expr
);
610 /* Build code to check the unit range if KIND=8 is used. Similar to
611 set_parameter_value_chk but we do not generate error calls for
612 inquire statements. */
615 set_parameter_value_inquire (stmtblock_t
*block
, tree var
,
616 enum iofield type
, gfc_expr
*e
)
619 gfc_st_parameter_field
*p
= &st_parameter_field
[type
];
620 tree dest_type
= TREE_TYPE (p
->field
);
622 gfc_init_se (&se
, NULL
);
623 gfc_conv_expr_val (&se
, e
);
625 /* If we're inquiring on a UNIT number, we need to check to make
626 sure it exists for larger than kind = 4. */
627 if (type
== IOPARM_common_unit
&& e
->ts
.kind
> 4)
629 stmtblock_t newblock
;
630 tree cond1
, cond2
, cond3
, val
, body
;
633 /* Don't evaluate the UNIT number multiple times. */
634 se
.expr
= gfc_evaluate_now (se
.expr
, &se
.pre
);
636 /* UNIT numbers should be greater than zero. */
637 i
= gfc_validate_kind (BT_INTEGER
, 4, false);
638 cond1
= build2_loc (input_location
, LT_EXPR
, boolean_type_node
,
640 fold_convert (TREE_TYPE (se
.expr
),
642 /* UNIT numbers should be less than the max. */
643 val
= gfc_conv_mpz_to_tree (gfc_integer_kinds
[i
].huge
, 4);
644 cond2
= build2_loc (input_location
, GT_EXPR
, boolean_type_node
,
646 fold_convert (TREE_TYPE (se
.expr
), val
));
647 cond3
= build2_loc (input_location
, TRUTH_OR_EXPR
,
648 boolean_type_node
, cond1
, cond2
);
650 gfc_start_block (&newblock
);
652 /* The unit number GFC_INVALID_UNIT is reserved. No units can
653 ever have this value. It is used here to signal to the
654 runtime library that the inquire unit number is outside the
655 allowable range and so cannot exist. It is needed when
656 -fdefault-integer-8 is used. */
657 set_parameter_const (&newblock
, var
, IOPARM_common_unit
,
660 body
= gfc_finish_block (&newblock
);
662 cond3
= gfc_unlikely (cond3
, PRED_FORTRAN_FAIL_IO
);
663 var
= build3_v (COND_EXPR
, cond3
, body
, build_empty_stmt (input_location
));
664 gfc_add_expr_to_block (&se
.pre
, var
);
667 se
.expr
= convert (dest_type
, se
.expr
);
668 gfc_add_block_to_block (block
, &se
.pre
);
674 /* Generate code to store a non-string I/O parameter into the
675 st_parameter_XXX structure. This is pass by reference. */
678 set_parameter_ref (stmtblock_t
*block
, stmtblock_t
*postblock
,
679 tree var
, enum iofield type
, gfc_expr
*e
)
683 gfc_st_parameter_field
*p
= &st_parameter_field
[type
];
685 gcc_assert (e
->ts
.type
== BT_INTEGER
|| e
->ts
.type
== BT_LOGICAL
);
686 gfc_init_se (&se
, NULL
);
687 gfc_conv_expr_lhs (&se
, e
);
689 gfc_add_block_to_block (block
, &se
.pre
);
691 if (TYPE_MODE (TREE_TYPE (se
.expr
))
692 == TYPE_MODE (TREE_TYPE (TREE_TYPE (p
->field
))))
694 addr
= convert (TREE_TYPE (p
->field
), gfc_build_addr_expr (NULL_TREE
, se
.expr
));
696 /* If this is for the iostat variable initialize the
697 user variable to LIBERROR_OK which is zero. */
698 if (type
== IOPARM_common_iostat
)
699 gfc_add_modify (block
, se
.expr
,
700 build_int_cst (TREE_TYPE (se
.expr
), LIBERROR_OK
));
704 /* The type used by the library has different size
705 from the type of the variable supplied by the user.
706 Need to use a temporary. */
707 tree tmpvar
= gfc_create_var (TREE_TYPE (TREE_TYPE (p
->field
)),
708 st_parameter_field
[type
].name
);
710 /* If this is for the iostat variable, initialize the
711 user variable to LIBERROR_OK which is zero. */
712 if (type
== IOPARM_common_iostat
)
713 gfc_add_modify (block
, tmpvar
,
714 build_int_cst (TREE_TYPE (tmpvar
), LIBERROR_OK
));
716 addr
= gfc_build_addr_expr (NULL_TREE
, tmpvar
);
717 /* After the I/O operation, we set the variable from the temporary. */
718 tmp
= convert (TREE_TYPE (se
.expr
), tmpvar
);
719 gfc_add_modify (postblock
, se
.expr
, tmp
);
722 set_parameter_tree (block
, var
, type
, addr
);
726 /* Given an array expr, find its address and length to get a string. If the
727 array is full, the string's address is the address of array's first element
728 and the length is the size of the whole array. If it is an element, the
729 string's address is the element's address and the length is the rest size of
733 gfc_convert_array_to_string (gfc_se
* se
, gfc_expr
* e
)
739 tree type
, array
, tmp
;
743 /* If it is an element, we need its address and size of the rest. */
744 gcc_assert (e
->expr_type
== EXPR_VARIABLE
);
745 gcc_assert (e
->ref
->u
.ar
.type
== AR_ELEMENT
);
746 sym
= e
->symtree
->n
.sym
;
747 rank
= sym
->as
->rank
- 1;
748 gfc_conv_expr (se
, e
);
750 array
= sym
->backend_decl
;
751 type
= TREE_TYPE (array
);
753 if (GFC_ARRAY_TYPE_P (type
))
754 size
= GFC_TYPE_ARRAY_SIZE (type
);
757 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type
));
758 size
= gfc_conv_array_stride (array
, rank
);
759 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
760 gfc_array_index_type
,
761 gfc_conv_array_ubound (array
, rank
),
762 gfc_conv_array_lbound (array
, rank
));
763 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
764 gfc_array_index_type
, tmp
,
766 size
= fold_build2_loc (input_location
, MULT_EXPR
,
767 gfc_array_index_type
, tmp
, size
);
771 size
= fold_build2_loc (input_location
, MINUS_EXPR
,
772 gfc_array_index_type
, size
,
773 TREE_OPERAND (se
->expr
, 1));
774 se
->expr
= gfc_build_addr_expr (NULL_TREE
, se
->expr
);
775 tmp
= TYPE_SIZE_UNIT (gfc_get_element_type (type
));
776 size
= fold_build2_loc (input_location
, MULT_EXPR
,
777 gfc_array_index_type
, size
,
778 fold_convert (gfc_array_index_type
, tmp
));
779 se
->string_length
= fold_convert (gfc_charlen_type_node
, size
);
783 gfc_conv_array_parameter (se
, e
, true, NULL
, NULL
, &size
);
784 se
->string_length
= fold_convert (gfc_charlen_type_node
, size
);
788 /* Generate code to store a string and its length into the
789 st_parameter_XXX structure. */
792 set_string (stmtblock_t
* block
, stmtblock_t
* postblock
, tree var
,
793 enum iofield type
, gfc_expr
* e
)
799 gfc_st_parameter_field
*p
= &st_parameter_field
[type
];
801 gfc_init_se (&se
, NULL
);
803 if (p
->param_type
== IOPARM_ptype_common
)
804 var
= fold_build3_loc (input_location
, COMPONENT_REF
,
805 st_parameter
[IOPARM_ptype_common
].type
,
806 var
, TYPE_FIELDS (TREE_TYPE (var
)), NULL_TREE
);
807 io
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (p
->field
),
808 var
, p
->field
, NULL_TREE
);
809 len
= fold_build3_loc (input_location
, COMPONENT_REF
,
810 TREE_TYPE (p
->field_len
),
811 var
, p
->field_len
, NULL_TREE
);
813 /* Integer variable assigned a format label. */
814 if (e
->ts
.type
== BT_INTEGER
816 && e
->symtree
->n
.sym
->attr
.assign
== 1)
821 gfc_conv_label_variable (&se
, e
);
822 tmp
= GFC_DECL_STRING_LEN (se
.expr
);
823 cond
= fold_build2_loc (input_location
, LT_EXPR
, boolean_type_node
,
824 tmp
, build_int_cst (TREE_TYPE (tmp
), 0));
826 msg
= xasprintf ("Label assigned to variable '%s' (%%ld) is not a format "
827 "label", e
->symtree
->name
);
828 gfc_trans_runtime_check (true, false, cond
, &se
.pre
, &e
->where
, msg
,
829 fold_convert (long_integer_type_node
, tmp
));
832 gfc_add_modify (&se
.pre
, io
,
833 fold_convert (TREE_TYPE (io
), GFC_DECL_ASSIGN_ADDR (se
.expr
)));
834 gfc_add_modify (&se
.pre
, len
, GFC_DECL_STRING_LEN (se
.expr
));
838 /* General character. */
839 if (e
->ts
.type
== BT_CHARACTER
&& e
->rank
== 0)
840 gfc_conv_expr (&se
, e
);
841 /* Array assigned Hollerith constant or character array. */
842 else if (e
->rank
> 0 || (e
->symtree
&& e
->symtree
->n
.sym
->as
->rank
> 0))
843 gfc_convert_array_to_string (&se
, e
);
847 gfc_conv_string_parameter (&se
);
848 gfc_add_modify (&se
.pre
, io
, fold_convert (TREE_TYPE (io
), se
.expr
));
849 gfc_add_modify (&se
.pre
, len
, se
.string_length
);
852 gfc_add_block_to_block (block
, &se
.pre
);
853 gfc_add_block_to_block (postblock
, &se
.post
);
858 /* Generate code to store the character (array) and the character length
859 for an internal unit. */
862 set_internal_unit (stmtblock_t
* block
, stmtblock_t
* post_block
,
863 tree var
, gfc_expr
* e
)
870 gfc_st_parameter_field
*p
;
873 gfc_init_se (&se
, NULL
);
875 p
= &st_parameter_field
[IOPARM_dt_internal_unit
];
877 io
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (p
->field
),
878 var
, p
->field
, NULL_TREE
);
879 len
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (p
->field_len
),
880 var
, p
->field_len
, NULL_TREE
);
881 p
= &st_parameter_field
[IOPARM_dt_internal_unit_desc
];
882 desc
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (p
->field
),
883 var
, p
->field
, NULL_TREE
);
885 gcc_assert (e
->ts
.type
== BT_CHARACTER
);
887 /* Character scalars. */
890 gfc_conv_expr (&se
, e
);
891 gfc_conv_string_parameter (&se
);
893 se
.expr
= build_int_cst (pchar_type_node
, 0);
896 /* Character array. */
897 else if (e
->rank
> 0)
899 if (is_subref_array (e
))
901 /* Use a temporary for components of arrays of derived types
902 or substring array references. */
903 gfc_conv_subref_array_arg (&se
, e
, 0,
904 last_dt
== READ
? INTENT_IN
: INTENT_OUT
, false);
905 tmp
= build_fold_indirect_ref_loc (input_location
,
907 se
.expr
= gfc_build_addr_expr (pchar_type_node
, tmp
);
908 tmp
= gfc_conv_descriptor_data_get (tmp
);
912 /* Return the data pointer and rank from the descriptor. */
913 gfc_conv_expr_descriptor (&se
, e
);
914 tmp
= gfc_conv_descriptor_data_get (se
.expr
);
915 se
.expr
= gfc_build_addr_expr (pchar_type_node
, se
.expr
);
921 /* The cast is needed for character substrings and the descriptor
923 gfc_add_modify (&se
.pre
, io
, fold_convert (TREE_TYPE (io
), tmp
));
924 gfc_add_modify (&se
.pre
, len
,
925 fold_convert (TREE_TYPE (len
), se
.string_length
));
926 gfc_add_modify (&se
.pre
, desc
, se
.expr
);
928 gfc_add_block_to_block (block
, &se
.pre
);
929 gfc_add_block_to_block (post_block
, &se
.post
);
933 /* Add a case to a IO-result switch. */
936 add_case (int label_value
, gfc_st_label
* label
, stmtblock_t
* body
)
941 return; /* No label, no case */
943 value
= build_int_cst (integer_type_node
, label_value
);
945 /* Make a backend label for this case. */
946 tmp
= gfc_build_label_decl (NULL_TREE
);
948 /* And the case itself. */
949 tmp
= build_case_label (value
, NULL_TREE
, tmp
);
950 gfc_add_expr_to_block (body
, tmp
);
952 /* Jump to the label. */
953 tmp
= build1_v (GOTO_EXPR
, gfc_get_label_decl (label
));
954 gfc_add_expr_to_block (body
, tmp
);
958 /* Generate a switch statement that branches to the correct I/O
959 result label. The last statement of an I/O call stores the
960 result into a variable because there is often cleanup that
961 must be done before the switch, so a temporary would have to
962 be created anyway. */
965 io_result (stmtblock_t
* block
, tree var
, gfc_st_label
* err_label
,
966 gfc_st_label
* end_label
, gfc_st_label
* eor_label
)
970 gfc_st_parameter_field
*p
= &st_parameter_field
[IOPARM_common_flags
];
972 /* If no labels are specified, ignore the result instead
973 of building an empty switch. */
974 if (err_label
== NULL
976 && eor_label
== NULL
)
979 /* Build a switch statement. */
980 gfc_start_block (&body
);
982 /* The label values here must be the same as the values
983 in the library_return enum in the runtime library */
984 add_case (1, err_label
, &body
);
985 add_case (2, end_label
, &body
);
986 add_case (3, eor_label
, &body
);
988 tmp
= gfc_finish_block (&body
);
990 var
= fold_build3_loc (input_location
, COMPONENT_REF
,
991 st_parameter
[IOPARM_ptype_common
].type
,
992 var
, TYPE_FIELDS (TREE_TYPE (var
)), NULL_TREE
);
993 rc
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (p
->field
),
994 var
, p
->field
, NULL_TREE
);
995 rc
= fold_build2_loc (input_location
, BIT_AND_EXPR
, TREE_TYPE (rc
),
996 rc
, build_int_cst (TREE_TYPE (rc
),
997 IOPARM_common_libreturn_mask
));
999 tmp
= fold_build3_loc (input_location
, SWITCH_EXPR
, NULL_TREE
,
1000 rc
, tmp
, NULL_TREE
);
1002 gfc_add_expr_to_block (block
, tmp
);
1006 /* Store the current file and line number to variables so that if a
1007 library call goes awry, we can tell the user where the problem is. */
1010 set_error_locus (stmtblock_t
* block
, tree var
, locus
* where
)
1013 tree str
, locus_file
;
1015 gfc_st_parameter_field
*p
= &st_parameter_field
[IOPARM_common_filename
];
1017 locus_file
= fold_build3_loc (input_location
, COMPONENT_REF
,
1018 st_parameter
[IOPARM_ptype_common
].type
,
1019 var
, TYPE_FIELDS (TREE_TYPE (var
)), NULL_TREE
);
1020 locus_file
= fold_build3_loc (input_location
, COMPONENT_REF
,
1021 TREE_TYPE (p
->field
), locus_file
,
1022 p
->field
, NULL_TREE
);
1023 f
= where
->lb
->file
;
1024 str
= gfc_build_cstring_const (f
->filename
);
1026 str
= gfc_build_addr_expr (pchar_type_node
, str
);
1027 gfc_add_modify (block
, locus_file
, str
);
1029 line
= LOCATION_LINE (where
->lb
->location
);
1030 set_parameter_const (block
, var
, IOPARM_common_line
, line
);
1034 /* Translate an OPEN statement. */
1037 gfc_trans_open (gfc_code
* code
)
1039 stmtblock_t block
, post_block
;
1042 unsigned int mask
= 0;
1044 gfc_start_block (&block
);
1045 gfc_init_block (&post_block
);
1047 var
= gfc_create_var (st_parameter
[IOPARM_ptype_open
].type
, "open_parm");
1049 set_error_locus (&block
, var
, &code
->loc
);
1053 mask
|= set_string (&block
, &post_block
, var
, IOPARM_common_iomsg
,
1057 mask
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_common_iostat
,
1061 mask
|= IOPARM_common_err
;
1064 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_file
, p
->file
);
1067 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_status
,
1071 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_access
,
1075 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_form
, p
->form
);
1078 mask
|= set_parameter_value (&block
, var
, IOPARM_open_recl_in
,
1082 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_blank
,
1086 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_position
,
1090 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_action
,
1094 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_delim
,
1098 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_pad
, p
->pad
);
1101 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_decimal
,
1105 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_encoding
,
1109 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_round
, p
->round
);
1112 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_sign
, p
->sign
);
1114 if (p
->asynchronous
)
1115 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_asynchronous
,
1119 mask
|= set_string (&block
, &post_block
, var
, IOPARM_open_convert
,
1123 mask
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_open_newunit
,
1126 set_parameter_const (&block
, var
, IOPARM_common_flags
, mask
);
1129 set_parameter_value_chk (&block
, p
->iostat
, var
, IOPARM_common_unit
, p
->unit
);
1131 set_parameter_const (&block
, var
, IOPARM_common_unit
, 0);
1133 tmp
= gfc_build_addr_expr (NULL_TREE
, var
);
1134 tmp
= build_call_expr_loc (input_location
,
1135 iocall
[IOCALL_OPEN
], 1, tmp
);
1136 gfc_add_expr_to_block (&block
, tmp
);
1138 gfc_add_block_to_block (&block
, &post_block
);
1140 io_result (&block
, var
, p
->err
, NULL
, NULL
);
1142 return gfc_finish_block (&block
);
1146 /* Translate a CLOSE statement. */
1149 gfc_trans_close (gfc_code
* code
)
1151 stmtblock_t block
, post_block
;
1154 unsigned int mask
= 0;
1156 gfc_start_block (&block
);
1157 gfc_init_block (&post_block
);
1159 var
= gfc_create_var (st_parameter
[IOPARM_ptype_close
].type
, "close_parm");
1161 set_error_locus (&block
, var
, &code
->loc
);
1162 p
= code
->ext
.close
;
1165 mask
|= set_string (&block
, &post_block
, var
, IOPARM_common_iomsg
,
1169 mask
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_common_iostat
,
1173 mask
|= IOPARM_common_err
;
1176 mask
|= set_string (&block
, &post_block
, var
, IOPARM_close_status
,
1179 set_parameter_const (&block
, var
, IOPARM_common_flags
, mask
);
1182 set_parameter_value_chk (&block
, p
->iostat
, var
, IOPARM_common_unit
, p
->unit
);
1184 set_parameter_const (&block
, var
, IOPARM_common_unit
, 0);
1186 tmp
= gfc_build_addr_expr (NULL_TREE
, var
);
1187 tmp
= build_call_expr_loc (input_location
,
1188 iocall
[IOCALL_CLOSE
], 1, tmp
);
1189 gfc_add_expr_to_block (&block
, tmp
);
1191 gfc_add_block_to_block (&block
, &post_block
);
1193 io_result (&block
, var
, p
->err
, NULL
, NULL
);
1195 return gfc_finish_block (&block
);
1199 /* Common subroutine for building a file positioning statement. */
1202 build_filepos (tree function
, gfc_code
* code
)
1204 stmtblock_t block
, post_block
;
1207 unsigned int mask
= 0;
1209 p
= code
->ext
.filepos
;
1211 gfc_start_block (&block
);
1212 gfc_init_block (&post_block
);
1214 var
= gfc_create_var (st_parameter
[IOPARM_ptype_filepos
].type
,
1217 set_error_locus (&block
, var
, &code
->loc
);
1220 mask
|= set_string (&block
, &post_block
, var
, IOPARM_common_iomsg
,
1224 mask
|= set_parameter_ref (&block
, &post_block
, var
,
1225 IOPARM_common_iostat
, p
->iostat
);
1228 mask
|= IOPARM_common_err
;
1230 set_parameter_const (&block
, var
, IOPARM_common_flags
, mask
);
1233 set_parameter_value_chk (&block
, p
->iostat
, var
, IOPARM_common_unit
,
1236 set_parameter_const (&block
, var
, IOPARM_common_unit
, 0);
1238 tmp
= gfc_build_addr_expr (NULL_TREE
, var
);
1239 tmp
= build_call_expr_loc (input_location
,
1241 gfc_add_expr_to_block (&block
, tmp
);
1243 gfc_add_block_to_block (&block
, &post_block
);
1245 io_result (&block
, var
, p
->err
, NULL
, NULL
);
1247 return gfc_finish_block (&block
);
1251 /* Translate a BACKSPACE statement. */
1254 gfc_trans_backspace (gfc_code
* code
)
1256 return build_filepos (iocall
[IOCALL_BACKSPACE
], code
);
1260 /* Translate an ENDFILE statement. */
1263 gfc_trans_endfile (gfc_code
* code
)
1265 return build_filepos (iocall
[IOCALL_ENDFILE
], code
);
1269 /* Translate a REWIND statement. */
1272 gfc_trans_rewind (gfc_code
* code
)
1274 return build_filepos (iocall
[IOCALL_REWIND
], code
);
1278 /* Translate a FLUSH statement. */
1281 gfc_trans_flush (gfc_code
* code
)
1283 return build_filepos (iocall
[IOCALL_FLUSH
], code
);
1287 /* Translate the non-IOLENGTH form of an INQUIRE statement. */
1290 gfc_trans_inquire (gfc_code
* code
)
1292 stmtblock_t block
, post_block
;
1295 unsigned int mask
= 0, mask2
= 0;
1297 gfc_start_block (&block
);
1298 gfc_init_block (&post_block
);
1300 var
= gfc_create_var (st_parameter
[IOPARM_ptype_inquire
].type
,
1303 set_error_locus (&block
, var
, &code
->loc
);
1304 p
= code
->ext
.inquire
;
1307 mask
|= set_string (&block
, &post_block
, var
, IOPARM_common_iomsg
,
1311 mask
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_common_iostat
,
1315 mask
|= IOPARM_common_err
;
1318 if (p
->unit
&& p
->file
)
1319 gfc_error ("INQUIRE statement at %L cannot contain both FILE and UNIT specifiers", &code
->loc
);
1322 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_file
,
1326 mask
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_inquire_exist
,
1330 mask
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_inquire_opened
,
1334 mask
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_inquire_number
,
1338 mask
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_inquire_named
,
1342 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_name
,
1346 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_access
,
1350 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_sequential
,
1354 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_direct
,
1358 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_form
,
1362 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_formatted
,
1366 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_unformatted
,
1370 mask
|= set_parameter_ref (&block
, &post_block
, var
,
1371 IOPARM_inquire_recl_out
, p
->recl
);
1374 mask
|= set_parameter_ref (&block
, &post_block
, var
,
1375 IOPARM_inquire_nextrec
, p
->nextrec
);
1378 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_blank
,
1382 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_delim
,
1386 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_position
,
1390 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_action
,
1394 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_read
,
1398 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_write
,
1402 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_readwrite
,
1406 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_pad
,
1410 mask
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_convert
,
1414 mask
|= set_parameter_ref (&block
, &post_block
, var
,
1415 IOPARM_inquire_strm_pos_out
, p
->strm_pos
);
1417 /* The second series of flags. */
1418 if (p
->asynchronous
)
1419 mask2
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_asynchronous
,
1423 mask2
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_decimal
,
1427 mask2
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_encoding
,
1431 mask2
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_round
,
1435 mask2
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_sign
,
1439 mask2
|= set_parameter_ref (&block
, &post_block
, var
,
1440 IOPARM_inquire_pending
, p
->pending
);
1443 mask2
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_inquire_size
,
1447 mask2
|= set_parameter_ref (&block
, &post_block
,var
, IOPARM_inquire_id
,
1450 mask2
|= set_string (&block
, &post_block
, var
, IOPARM_inquire_iqstream
,
1454 mask
|= set_parameter_const (&block
, var
, IOPARM_inquire_flags2
, mask2
);
1456 set_parameter_const (&block
, var
, IOPARM_common_flags
, mask
);
1460 set_parameter_value (&block
, var
, IOPARM_common_unit
, p
->unit
);
1461 set_parameter_value_inquire (&block
, var
, IOPARM_common_unit
, p
->unit
);
1464 set_parameter_const (&block
, var
, IOPARM_common_unit
, 0);
1466 tmp
= gfc_build_addr_expr (NULL_TREE
, var
);
1467 tmp
= build_call_expr_loc (input_location
,
1468 iocall
[IOCALL_INQUIRE
], 1, tmp
);
1469 gfc_add_expr_to_block (&block
, tmp
);
1471 gfc_add_block_to_block (&block
, &post_block
);
1473 io_result (&block
, var
, p
->err
, NULL
, NULL
);
1475 return gfc_finish_block (&block
);
1480 gfc_trans_wait (gfc_code
* code
)
1482 stmtblock_t block
, post_block
;
1485 unsigned int mask
= 0;
1487 gfc_start_block (&block
);
1488 gfc_init_block (&post_block
);
1490 var
= gfc_create_var (st_parameter
[IOPARM_ptype_wait
].type
,
1493 set_error_locus (&block
, var
, &code
->loc
);
1496 /* Set parameters here. */
1498 mask
|= set_string (&block
, &post_block
, var
, IOPARM_common_iomsg
,
1502 mask
|= set_parameter_ref (&block
, &post_block
, var
, IOPARM_common_iostat
,
1506 mask
|= IOPARM_common_err
;
1509 mask
|= set_parameter_value (&block
, var
, IOPARM_wait_id
, p
->id
);
1511 set_parameter_const (&block
, var
, IOPARM_common_flags
, mask
);
1514 set_parameter_value_chk (&block
, p
->iostat
, var
, IOPARM_common_unit
, p
->unit
);
1516 tmp
= gfc_build_addr_expr (NULL_TREE
, var
);
1517 tmp
= build_call_expr_loc (input_location
,
1518 iocall
[IOCALL_WAIT
], 1, tmp
);
1519 gfc_add_expr_to_block (&block
, tmp
);
1521 gfc_add_block_to_block (&block
, &post_block
);
1523 io_result (&block
, var
, p
->err
, NULL
, NULL
);
1525 return gfc_finish_block (&block
);
1530 /* nml_full_name builds up the fully qualified name of a
1531 derived type component. '+' is used to denote a type extension. */
1534 nml_full_name (const char* var_name
, const char* cmp_name
, bool parent
)
1536 int full_name_length
;
1539 full_name_length
= strlen (var_name
) + strlen (cmp_name
) + 1;
1540 full_name
= XCNEWVEC (char, full_name_length
+ 1);
1541 strcpy (full_name
, var_name
);
1542 full_name
= strcat (full_name
, parent
? "+" : "%");
1543 full_name
= strcat (full_name
, cmp_name
);
1548 /* nml_get_addr_expr builds an address expression from the
1549 gfc_symbol or gfc_component backend_decl's. An offset is
1550 provided so that the address of an element of an array of
1551 derived types is returned. This is used in the runtime to
1552 determine that span of the derived type. */
1555 nml_get_addr_expr (gfc_symbol
* sym
, gfc_component
* c
,
1558 tree decl
= NULL_TREE
;
1563 sym
->attr
.referenced
= 1;
1564 decl
= gfc_get_symbol_decl (sym
);
1566 /* If this is the enclosing function declaration, use
1567 the fake result instead. */
1568 if (decl
== current_function_decl
)
1569 decl
= gfc_get_fake_result_decl (sym
, 0);
1570 else if (decl
== DECL_CONTEXT (current_function_decl
))
1571 decl
= gfc_get_fake_result_decl (sym
, 1);
1574 decl
= c
->backend_decl
;
1576 gcc_assert (decl
&& ((TREE_CODE (decl
) == FIELD_DECL
1577 || TREE_CODE (decl
) == VAR_DECL
1578 || TREE_CODE (decl
) == PARM_DECL
)
1579 || TREE_CODE (decl
) == COMPONENT_REF
));
1583 /* Build indirect reference, if dummy argument. */
1585 if (POINTER_TYPE_P (TREE_TYPE(tmp
)))
1586 tmp
= build_fold_indirect_ref_loc (input_location
, tmp
);
1588 /* Treat the component of a derived type, using base_addr for
1589 the derived type. */
1591 if (TREE_CODE (decl
) == FIELD_DECL
)
1592 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
, TREE_TYPE (tmp
),
1593 base_addr
, tmp
, NULL_TREE
);
1595 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp
)))
1596 tmp
= gfc_conv_array_data (tmp
);
1599 if (!POINTER_TYPE_P (TREE_TYPE (tmp
)))
1600 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
1602 if (TREE_CODE (TREE_TYPE (tmp
)) == ARRAY_TYPE
)
1603 tmp
= gfc_build_array_ref (tmp
, gfc_index_zero_node
, NULL
);
1605 if (!POINTER_TYPE_P (TREE_TYPE (tmp
)))
1606 tmp
= build_fold_indirect_ref_loc (input_location
,
1610 gcc_assert (tmp
&& POINTER_TYPE_P (TREE_TYPE (tmp
)));
1616 /* For an object VAR_NAME whose base address is BASE_ADDR, generate a
1617 call to iocall[IOCALL_SET_NML_VAL]. For derived type variable, recursively
1618 generate calls to iocall[IOCALL_SET_NML_VAL] for each component. */
1620 #define IARG(i) build_int_cst (gfc_array_index_type, i)
1623 transfer_namelist_element (stmtblock_t
* block
, const char * var_name
,
1624 gfc_symbol
* sym
, gfc_component
* c
,
1627 gfc_typespec
* ts
= NULL
;
1628 gfc_array_spec
* as
= NULL
;
1629 tree addr_expr
= NULL
;
1635 tree decl
= NULL_TREE
;
1636 tree gfc_int4_type_node
= gfc_get_int_type (4);
1637 tree dtio_proc
= null_pointer_node
;
1638 tree vtable
= null_pointer_node
;
1643 gcc_assert (sym
|| c
);
1645 /* Build the namelist object name. */
1647 string
= gfc_build_cstring_const (var_name
);
1648 string
= gfc_build_addr_expr (pchar_type_node
, string
);
1650 /* Build ts, as and data address using symbol or component. */
1652 ts
= (sym
) ? &sym
->ts
: &c
->ts
;
1653 as
= (sym
) ? sym
->as
: c
->as
;
1655 addr_expr
= nml_get_addr_expr (sym
, c
, base_addr
);
1662 decl
= (sym
) ? sym
->backend_decl
: c
->backend_decl
;
1663 if (sym
&& sym
->attr
.dummy
)
1664 decl
= build_fold_indirect_ref_loc (input_location
, decl
);
1665 dt
= TREE_TYPE (decl
);
1666 dtype
= gfc_get_dtype (dt
);
1671 dtype
= IARG (itype
<< GFC_DTYPE_TYPE_SHIFT
);
1674 /* Build up the arguments for the transfer call.
1675 The call for the scalar part transfers:
1676 (address, name, type, kind or string_length, dtype) */
1678 dt_parm_addr
= gfc_build_addr_expr (NULL_TREE
, dt_parm
);
1680 /* Check if the derived type has a specific DTIO for the mode.
1681 Note that although namelist io is forbidden to have a format
1682 list, the specific subroutine is of the formatted kind. */
1683 if (ts
->type
== BT_DERIVED
)
1685 gfc_symbol
*dtio_sub
= NULL
;
1687 dtio_sub
= gfc_find_specific_dtio_proc (ts
->u
.derived
,
1690 if (dtio_sub
!= NULL
)
1692 dtio_proc
= gfc_get_symbol_decl (dtio_sub
);
1693 dtio_proc
= gfc_build_addr_expr (NULL
, dtio_proc
);
1694 vtab
= gfc_find_derived_vtab (ts
->u
.derived
);
1695 vtable
= vtab
->backend_decl
;
1696 if (vtable
== NULL_TREE
)
1697 vtable
= gfc_get_symbol_decl (vtab
);
1698 vtable
= gfc_build_addr_expr (pvoid_type_node
, vtable
);
1702 if (ts
->type
== BT_CHARACTER
)
1703 tmp
= ts
->u
.cl
->backend_decl
;
1705 tmp
= build_int_cst (gfc_charlen_type_node
, 0);
1707 if (dtio_proc
== NULL_TREE
)
1708 tmp
= build_call_expr_loc (input_location
,
1709 iocall
[IOCALL_SET_NML_VAL
], 6,
1710 dt_parm_addr
, addr_expr
, string
,
1711 build_int_cst (gfc_int4_type_node
, ts
->kind
),
1714 tmp
= build_call_expr_loc (input_location
,
1715 iocall
[IOCALL_SET_NML_DTIO_VAL
], 8,
1716 dt_parm_addr
, addr_expr
, string
,
1717 build_int_cst (gfc_int4_type_node
, ts
->kind
),
1718 tmp
, dtype
, dtio_proc
, vtable
);
1719 gfc_add_expr_to_block (block
, tmp
);
1721 /* If the object is an array, transfer rank times:
1722 (null pointer, name, stride, lbound, ubound) */
1724 for ( n_dim
= 0 ; n_dim
< rank
; n_dim
++ )
1726 tmp
= build_call_expr_loc (input_location
,
1727 iocall
[IOCALL_SET_NML_VAL_DIM
], 5,
1729 build_int_cst (gfc_int4_type_node
, n_dim
),
1730 gfc_conv_array_stride (decl
, n_dim
),
1731 gfc_conv_array_lbound (decl
, n_dim
),
1732 gfc_conv_array_ubound (decl
, n_dim
));
1733 gfc_add_expr_to_block (block
, tmp
);
1736 if (gfc_bt_struct (ts
->type
) && ts
->u
.derived
->components
1737 && dtio_proc
== null_pointer_node
)
1741 /* Provide the RECORD_TYPE to build component references. */
1743 tree expr
= build_fold_indirect_ref_loc (input_location
,
1746 for (cmp
= ts
->u
.derived
->components
; cmp
; cmp
= cmp
->next
)
1748 char *full_name
= nml_full_name (var_name
, cmp
->name
,
1749 ts
->u
.derived
->attr
.extension
);
1750 transfer_namelist_element (block
,
1760 /* Create a data transfer statement. Not all of the fields are valid
1761 for both reading and writing, but improper use has been filtered
1765 build_dt (tree function
, gfc_code
* code
)
1767 stmtblock_t block
, post_block
, post_end_block
, post_iu_block
;
1772 unsigned int mask
= 0;
1774 gfc_start_block (&block
);
1775 gfc_init_block (&post_block
);
1776 gfc_init_block (&post_end_block
);
1777 gfc_init_block (&post_iu_block
);
1779 var
= gfc_create_var (st_parameter
[IOPARM_ptype_dt
].type
, "dt_parm");
1781 set_error_locus (&block
, var
, &code
->loc
);
1783 if (last_dt
== IOLENGTH
)
1787 inq
= code
->ext
.inquire
;
1789 /* First check that preconditions are met. */
1790 gcc_assert (inq
!= NULL
);
1791 gcc_assert (inq
->iolength
!= NULL
);
1793 /* Connect to the iolength variable. */
1794 mask
|= set_parameter_ref (&block
, &post_end_block
, var
,
1795 IOPARM_dt_iolength
, inq
->iolength
);
1801 gcc_assert (dt
!= NULL
);
1804 if (dt
&& dt
->io_unit
)
1806 if (dt
->io_unit
->ts
.type
== BT_CHARACTER
)
1808 mask
|= set_internal_unit (&block
, &post_iu_block
,
1810 set_parameter_const (&block
, var
, IOPARM_common_unit
,
1811 dt
->io_unit
->ts
.kind
== 1 ? 0 : -1);
1815 set_parameter_const (&block
, var
, IOPARM_common_unit
, 0);
1820 mask
|= set_string (&block
, &post_block
, var
, IOPARM_common_iomsg
,
1824 mask
|= set_parameter_ref (&block
, &post_end_block
, var
,
1825 IOPARM_common_iostat
, dt
->iostat
);
1828 mask
|= IOPARM_common_err
;
1831 mask
|= IOPARM_common_eor
;
1834 mask
|= IOPARM_common_end
;
1837 mask
|= set_parameter_ref (&block
, &post_end_block
, var
,
1838 IOPARM_dt_id
, dt
->id
);
1841 mask
|= set_parameter_value (&block
, var
, IOPARM_dt_pos
, dt
->pos
);
1843 if (dt
->asynchronous
)
1844 mask
|= set_string (&block
, &post_block
, var
,
1845 IOPARM_dt_asynchronous
, dt
->asynchronous
);
1848 mask
|= set_string (&block
, &post_block
, var
, IOPARM_dt_blank
,
1852 mask
|= set_string (&block
, &post_block
, var
, IOPARM_dt_decimal
,
1856 mask
|= set_string (&block
, &post_block
, var
, IOPARM_dt_delim
,
1860 mask
|= set_string (&block
, &post_block
, var
, IOPARM_dt_pad
,
1864 mask
|= set_string (&block
, &post_block
, var
, IOPARM_dt_round
,
1868 mask
|= set_string (&block
, &post_block
, var
, IOPARM_dt_sign
,
1872 mask
|= set_parameter_value (&block
, var
, IOPARM_dt_rec
, dt
->rec
);
1875 mask
|= set_string (&block
, &post_block
, var
, IOPARM_dt_advance
,
1878 if (dt
->format_expr
)
1879 mask
|= set_string (&block
, &post_end_block
, var
, IOPARM_dt_format
,
1882 if (dt
->format_label
)
1884 if (dt
->format_label
== &format_asterisk
)
1885 mask
|= IOPARM_dt_list_format
;
1887 mask
|= set_string (&block
, &post_block
, var
, IOPARM_dt_format
,
1888 dt
->format_label
->format
);
1892 mask
|= set_parameter_ref (&block
, &post_end_block
, var
,
1893 IOPARM_dt_size
, dt
->size
);
1897 if (dt
->format_expr
|| dt
->format_label
)
1898 gfc_internal_error ("build_dt: format with namelist");
1900 nmlname
= gfc_get_character_expr (gfc_default_character_kind
, NULL
,
1902 strlen (dt
->namelist
->name
));
1904 mask
|= set_string (&block
, &post_block
, var
, IOPARM_dt_namelist_name
,
1907 gfc_free_expr (nmlname
);
1909 if (last_dt
== READ
)
1910 mask
|= IOPARM_dt_namelist_read_mode
;
1912 set_parameter_const (&block
, var
, IOPARM_common_flags
, mask
);
1916 for (nml
= dt
->namelist
->namelist
; nml
; nml
= nml
->next
)
1917 transfer_namelist_element (&block
, nml
->sym
->name
, nml
->sym
,
1921 set_parameter_const (&block
, var
, IOPARM_common_flags
, mask
);
1923 if (dt
->io_unit
&& dt
->io_unit
->ts
.type
== BT_INTEGER
)
1924 set_parameter_value_chk (&block
, dt
->iostat
, var
,
1925 IOPARM_common_unit
, dt
->io_unit
);
1928 set_parameter_const (&block
, var
, IOPARM_common_flags
, mask
);
1930 tmp
= gfc_build_addr_expr (NULL_TREE
, var
);
1931 tmp
= build_call_expr_loc (UNKNOWN_LOCATION
,
1933 gfc_add_expr_to_block (&block
, tmp
);
1935 gfc_add_block_to_block (&block
, &post_block
);
1938 dt_post_end_block
= &post_end_block
;
1940 /* Set implied do loop exit condition. */
1941 if (last_dt
== READ
|| last_dt
== WRITE
)
1943 gfc_st_parameter_field
*p
= &st_parameter_field
[IOPARM_common_flags
];
1945 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
1946 st_parameter
[IOPARM_ptype_common
].type
,
1947 dt_parm
, TYPE_FIELDS (TREE_TYPE (dt_parm
)),
1949 tmp
= fold_build3_loc (input_location
, COMPONENT_REF
,
1950 TREE_TYPE (p
->field
), tmp
, p
->field
, NULL_TREE
);
1951 tmp
= fold_build2_loc (input_location
, BIT_AND_EXPR
, TREE_TYPE (tmp
),
1952 tmp
, build_int_cst (TREE_TYPE (tmp
),
1953 IOPARM_common_libreturn_mask
));
1958 gfc_add_expr_to_block (&block
, gfc_trans_code_cond (code
->block
->next
, tmp
));
1960 gfc_add_block_to_block (&block
, &post_iu_block
);
1963 dt_post_end_block
= NULL
;
1965 return gfc_finish_block (&block
);
1969 /* Translate the IOLENGTH form of an INQUIRE statement. We treat
1970 this as a third sort of data transfer statement, except that
1971 lengths are summed instead of actually transferring any data. */
1974 gfc_trans_iolength (gfc_code
* code
)
1977 return build_dt (iocall
[IOCALL_IOLENGTH
], code
);
1981 /* Translate a READ statement. */
1984 gfc_trans_read (gfc_code
* code
)
1987 return build_dt (iocall
[IOCALL_READ
], code
);
1991 /* Translate a WRITE statement */
1994 gfc_trans_write (gfc_code
* code
)
1997 return build_dt (iocall
[IOCALL_WRITE
], code
);
2001 /* Finish a data transfer statement. */
2004 gfc_trans_dt_end (gfc_code
* code
)
2009 gfc_init_block (&block
);
2014 function
= iocall
[IOCALL_READ_DONE
];
2018 function
= iocall
[IOCALL_WRITE_DONE
];
2022 function
= iocall
[IOCALL_IOLENGTH_DONE
];
2029 tmp
= gfc_build_addr_expr (NULL_TREE
, dt_parm
);
2030 tmp
= build_call_expr_loc (input_location
,
2032 gfc_add_expr_to_block (&block
, tmp
);
2033 gfc_add_block_to_block (&block
, dt_post_end_block
);
2034 gfc_init_block (dt_post_end_block
);
2036 if (last_dt
!= IOLENGTH
)
2038 gcc_assert (code
->ext
.dt
!= NULL
);
2039 io_result (&block
, dt_parm
, code
->ext
.dt
->err
,
2040 code
->ext
.dt
->end
, code
->ext
.dt
->eor
);
2043 return gfc_finish_block (&block
);
2047 transfer_expr (gfc_se
* se
, gfc_typespec
* ts
, tree addr_expr
,
2048 gfc_code
* code
, tree vptr
);
2050 /* Given an array field in a derived type variable, generate the code
2051 for the loop that iterates over array elements, and the code that
2052 accesses those array elements. Use transfer_expr to generate code
2053 for transferring that element. Because elements may also be
2054 derived types, transfer_expr and transfer_array_component are mutually
2058 transfer_array_component (tree expr
, gfc_component
* cm
, locus
* where
)
2067 gfc_array_info
*ss_array
;
2069 gfc_start_block (&block
);
2070 gfc_init_se (&se
, NULL
);
2072 /* Create and initialize Scalarization Status. Unlike in
2073 gfc_trans_transfer, we can't simply use gfc_walk_expr to take
2074 care of this task, because we don't have a gfc_expr at hand.
2075 Build one manually, as in gfc_trans_subarray_assign. */
2077 ss
= gfc_get_array_ss (gfc_ss_terminator
, NULL
, cm
->as
->rank
,
2079 ss_array
= &ss
->info
->data
.array
;
2080 ss_array
->shape
= gfc_get_shape (cm
->as
->rank
);
2081 ss_array
->descriptor
= expr
;
2082 ss_array
->data
= gfc_conv_array_data (expr
);
2083 ss_array
->offset
= gfc_conv_array_offset (expr
);
2084 for (n
= 0; n
< cm
->as
->rank
; n
++)
2086 ss_array
->start
[n
] = gfc_conv_array_lbound (expr
, n
);
2087 ss_array
->stride
[n
] = gfc_index_one_node
;
2089 mpz_init (ss_array
->shape
[n
]);
2090 mpz_sub (ss_array
->shape
[n
], cm
->as
->upper
[n
]->value
.integer
,
2091 cm
->as
->lower
[n
]->value
.integer
);
2092 mpz_add_ui (ss_array
->shape
[n
], ss_array
->shape
[n
], 1);
2095 /* Once we got ss, we use scalarizer to create the loop. */
2097 gfc_init_loopinfo (&loop
);
2098 gfc_add_ss_to_loop (&loop
, ss
);
2099 gfc_conv_ss_startstride (&loop
);
2100 gfc_conv_loop_setup (&loop
, where
);
2101 gfc_mark_ss_chain_used (ss
, 1);
2102 gfc_start_scalarized_body (&loop
, &body
);
2104 gfc_copy_loopinfo_to_se (&se
, &loop
);
2107 /* gfc_conv_tmp_array_ref assumes that se.expr contains the array. */
2109 gfc_conv_tmp_array_ref (&se
);
2111 /* Now se.expr contains an element of the array. Take the address and pass
2112 it to the IO routines. */
2113 tmp
= gfc_build_addr_expr (NULL_TREE
, se
.expr
);
2114 transfer_expr (&se
, &cm
->ts
, tmp
, NULL
, NULL_TREE
);
2116 /* We are done now with the loop body. Wrap up the scalarizer and
2119 gfc_add_block_to_block (&body
, &se
.pre
);
2120 gfc_add_block_to_block (&body
, &se
.post
);
2122 gfc_trans_scalarizing_loops (&loop
, &body
);
2124 gfc_add_block_to_block (&block
, &loop
.pre
);
2125 gfc_add_block_to_block (&block
, &loop
.post
);
2127 gcc_assert (ss_array
->shape
!= NULL
);
2128 gfc_free_shape (&ss_array
->shape
, cm
->as
->rank
);
2129 gfc_cleanup_loop (&loop
);
2131 return gfc_finish_block (&block
);
2135 /* Helper function for transfer_expr that looks for the DTIO procedure
2136 either as a typebound binding or in a generic interface. If present,
2137 the address expression of the procedure is returned. It is assumed
2138 that the procedure interface has been checked during resolution. */
2141 get_dtio_proc (gfc_typespec
* ts
, gfc_code
* code
, gfc_symbol
**dtio_sub
)
2143 gfc_symbol
*derived
;
2144 bool formatted
= false;
2145 gfc_dt
*dt
= code
->ext
.dt
;
2147 if (dt
&& dt
->format_expr
)
2150 fmt
= gfc_widechar_to_char (dt
->format_expr
->value
.character
.string
,
2152 if (strtok (fmt
, "DT") != NULL
)
2155 else if (dt
&& dt
->format_label
== &format_asterisk
)
2157 /* List directed io must call the formatted DTIO procedure. */
2161 if (ts
->type
== BT_DERIVED
)
2162 derived
= ts
->u
.derived
;
2164 derived
= ts
->u
.derived
->components
->ts
.u
.derived
;
2166 *dtio_sub
= gfc_find_specific_dtio_proc (derived
, last_dt
== WRITE
,
2170 return gfc_build_addr_expr (NULL
, gfc_get_symbol_decl (*dtio_sub
));
2176 /* Generate the call for a scalar transfer node. */
2179 transfer_expr (gfc_se
* se
, gfc_typespec
* ts
, tree addr_expr
,
2180 gfc_code
* code
, tree vptr
)
2182 tree tmp
, function
, arg2
, arg3
, field
, expr
;
2186 /* It is possible to get a C_NULL_PTR or C_NULL_FUNPTR expression here if
2187 the user says something like: print *, 'c_null_ptr: ', c_null_ptr
2188 We need to translate the expression to a constant if it's either
2189 C_NULL_PTR or C_NULL_FUNPTR. We could also get a user variable of
2190 type C_PTR or C_FUNPTR, in which case the ts->type may no longer be
2191 BT_DERIVED (could have been changed by gfc_conv_expr). */
2192 if ((ts
->type
== BT_DERIVED
|| ts
->type
== BT_INTEGER
)
2193 && ts
->u
.derived
!= NULL
2194 && (ts
->is_iso_c
== 1 || ts
->u
.derived
->ts
.is_iso_c
== 1))
2196 ts
->type
= BT_INTEGER
;
2197 ts
->kind
= gfc_index_integer_kind
;
2208 arg2
= build_int_cst (integer_type_node
, kind
);
2209 if (last_dt
== READ
)
2210 function
= iocall
[IOCALL_X_INTEGER
];
2212 function
= iocall
[IOCALL_X_INTEGER_WRITE
];
2217 arg2
= build_int_cst (integer_type_node
, kind
);
2218 if (last_dt
== READ
)
2220 if (gfc_real16_is_float128
&& ts
->kind
== 16)
2221 function
= iocall
[IOCALL_X_REAL128
];
2223 function
= iocall
[IOCALL_X_REAL
];
2227 if (gfc_real16_is_float128
&& ts
->kind
== 16)
2228 function
= iocall
[IOCALL_X_REAL128_WRITE
];
2230 function
= iocall
[IOCALL_X_REAL_WRITE
];
2236 arg2
= build_int_cst (integer_type_node
, kind
);
2237 if (last_dt
== READ
)
2239 if (gfc_real16_is_float128
&& ts
->kind
== 16)
2240 function
= iocall
[IOCALL_X_COMPLEX128
];
2242 function
= iocall
[IOCALL_X_COMPLEX
];
2246 if (gfc_real16_is_float128
&& ts
->kind
== 16)
2247 function
= iocall
[IOCALL_X_COMPLEX128_WRITE
];
2249 function
= iocall
[IOCALL_X_COMPLEX_WRITE
];
2255 arg2
= build_int_cst (integer_type_node
, kind
);
2256 if (last_dt
== READ
)
2257 function
= iocall
[IOCALL_X_LOGICAL
];
2259 function
= iocall
[IOCALL_X_LOGICAL_WRITE
];
2266 if (se
->string_length
)
2267 arg2
= se
->string_length
;
2270 tmp
= build_fold_indirect_ref_loc (input_location
,
2272 gcc_assert (TREE_CODE (TREE_TYPE (tmp
)) == ARRAY_TYPE
);
2273 arg2
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tmp
)));
2274 arg2
= fold_convert (gfc_charlen_type_node
, arg2
);
2276 arg3
= build_int_cst (integer_type_node
, kind
);
2277 if (last_dt
== READ
)
2278 function
= iocall
[IOCALL_X_CHARACTER_WIDE
];
2280 function
= iocall
[IOCALL_X_CHARACTER_WIDE_WRITE
];
2282 tmp
= gfc_build_addr_expr (NULL_TREE
, dt_parm
);
2283 tmp
= build_call_expr_loc (input_location
,
2284 function
, 4, tmp
, addr_expr
, arg2
, arg3
);
2285 gfc_add_expr_to_block (&se
->pre
, tmp
);
2286 gfc_add_block_to_block (&se
->pre
, &se
->post
);
2291 if (se
->string_length
)
2292 arg2
= se
->string_length
;
2295 tmp
= build_fold_indirect_ref_loc (input_location
,
2297 gcc_assert (TREE_CODE (TREE_TYPE (tmp
)) == ARRAY_TYPE
);
2298 arg2
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tmp
)));
2300 if (last_dt
== READ
)
2301 function
= iocall
[IOCALL_X_CHARACTER
];
2303 function
= iocall
[IOCALL_X_CHARACTER_WRITE
];
2309 if (ts
->u
.derived
->components
== NULL
)
2311 if (ts
->type
== BT_DERIVED
|| ts
->type
== BT_CLASS
)
2313 gfc_symbol
*derived
;
2314 gfc_symbol
*dtio_sub
= NULL
;
2315 /* Test for a specific DTIO subroutine. */
2316 if (ts
->type
== BT_DERIVED
)
2317 derived
= ts
->u
.derived
;
2319 derived
= ts
->u
.derived
->components
->ts
.u
.derived
;
2321 if (derived
->attr
.has_dtio_procs
)
2322 arg2
= get_dtio_proc (ts
, code
, &dtio_sub
);
2324 if (dtio_sub
!= NULL
)
2327 decl
= build_fold_indirect_ref_loc (input_location
,
2329 /* Remember that the first dummy of the DTIO subroutines
2330 is CLASS(derived) for extensible derived types, so the
2331 conversion must be done here for derived type and for
2332 scalarized CLASS array element io-list objects. */
2333 if ((ts
->type
== BT_DERIVED
2334 && !(ts
->u
.derived
->attr
.sequence
2335 || ts
->u
.derived
->attr
.is_bind_c
))
2336 || (ts
->type
== BT_CLASS
2337 && !GFC_CLASS_TYPE_P (TREE_TYPE (decl
))))
2338 gfc_conv_derived_to_class (se
, code
->expr1
,
2339 dtio_sub
->formal
->sym
->ts
,
2340 vptr
, false, false);
2341 addr_expr
= se
->expr
;
2342 function
= iocall
[IOCALL_X_DERIVED
];
2345 else if (ts
->type
== BT_DERIVED
)
2347 /* Recurse into the elements of the derived type. */
2348 expr
= gfc_evaluate_now (addr_expr
, &se
->pre
);
2349 expr
= build_fold_indirect_ref_loc (input_location
,
2352 /* Make sure that the derived type has been built. An external
2353 function, if only referenced in an io statement, requires this
2354 check (see PR58771). */
2355 if (ts
->u
.derived
->backend_decl
== NULL_TREE
)
2356 (void) gfc_typenode_for_spec (ts
);
2358 for (c
= ts
->u
.derived
->components
; c
; c
= c
->next
)
2360 field
= c
->backend_decl
;
2361 gcc_assert (field
&& TREE_CODE (field
) == FIELD_DECL
);
2363 tmp
= fold_build3_loc (UNKNOWN_LOCATION
,
2364 COMPONENT_REF
, TREE_TYPE (field
),
2365 expr
, field
, NULL_TREE
);
2367 if (c
->attr
.dimension
)
2369 tmp
= transfer_array_component (tmp
, c
, & code
->loc
);
2370 gfc_add_expr_to_block (&se
->pre
, tmp
);
2374 if (!c
->attr
.pointer
)
2375 tmp
= gfc_build_addr_expr (NULL_TREE
, tmp
);
2376 transfer_expr (se
, &c
->ts
, tmp
, code
, NULL_TREE
);
2381 /* If a CLASS object gets through to here, fall through and ICE. */
2384 gfc_internal_error ("Bad IO basetype (%d)", ts
->type
);
2387 tmp
= gfc_build_addr_expr (NULL_TREE
, dt_parm
);
2388 tmp
= build_call_expr_loc (input_location
,
2389 function
, 3, tmp
, addr_expr
, arg2
);
2390 gfc_add_expr_to_block (&se
->pre
, tmp
);
2391 gfc_add_block_to_block (&se
->pre
, &se
->post
);
2396 /* Generate a call to pass an array descriptor to the IO library. The
2397 array should be of one of the intrinsic types. */
2400 transfer_array_desc (gfc_se
* se
, gfc_typespec
* ts
, tree addr_expr
)
2402 tree tmp
, charlen_arg
, kind_arg
, io_call
;
2404 if (ts
->type
== BT_CHARACTER
)
2405 charlen_arg
= se
->string_length
;
2407 charlen_arg
= build_int_cst (gfc_charlen_type_node
, 0);
2409 kind_arg
= build_int_cst (integer_type_node
, ts
->kind
);
2411 tmp
= gfc_build_addr_expr (NULL_TREE
, dt_parm
);
2412 if (last_dt
== READ
)
2413 io_call
= iocall
[IOCALL_X_ARRAY
];
2415 io_call
= iocall
[IOCALL_X_ARRAY_WRITE
];
2417 tmp
= build_call_expr_loc (UNKNOWN_LOCATION
,
2419 tmp
, addr_expr
, kind_arg
, charlen_arg
);
2420 gfc_add_expr_to_block (&se
->pre
, tmp
);
2421 gfc_add_block_to_block (&se
->pre
, &se
->post
);
2425 /* gfc_trans_transfer()-- Translate a TRANSFER code node */
2428 gfc_trans_transfer (gfc_code
* code
)
2430 stmtblock_t block
, body
;
2440 gfc_start_block (&block
);
2441 gfc_init_block (&body
);
2445 gfc_init_se (&se
, NULL
);
2447 if (expr
->rank
== 0)
2449 /* Transfer a scalar value. */
2450 if (expr
->ts
.type
== BT_CLASS
)
2452 se
.want_pointer
= 1;
2453 gfc_conv_expr (&se
, expr
);
2454 vptr
= gfc_get_vptr_from_expr (se
.expr
);
2459 gfc_conv_expr_reference (&se
, expr
);
2461 transfer_expr (&se
, &expr
->ts
, se
.expr
, code
, vptr
);
2465 /* Transfer an array. If it is an array of an intrinsic
2466 type, pass the descriptor to the library. Otherwise
2467 scalarize the transfer. */
2468 if (expr
->ref
&& !gfc_is_proc_ptr_comp (expr
))
2470 for (ref
= expr
->ref
; ref
&& ref
->type
!= REF_ARRAY
;
2472 gcc_assert (ref
&& ref
->type
== REF_ARRAY
);
2475 if (!(gfc_bt_struct (expr
->ts
.type
)
2476 || expr
->ts
.type
== BT_CLASS
)
2477 && ref
&& ref
->next
== NULL
2478 && !is_subref_array (expr
))
2480 bool seen_vector
= false;
2482 if (ref
&& ref
->u
.ar
.type
== AR_SECTION
)
2484 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
2485 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
)
2492 if (seen_vector
&& last_dt
== READ
)
2494 /* Create a temp, read to that and copy it back. */
2495 gfc_conv_subref_array_arg (&se
, expr
, 0, INTENT_OUT
, false);
2500 /* Get the descriptor. */
2501 gfc_conv_expr_descriptor (&se
, expr
);
2502 tmp
= gfc_build_addr_expr (NULL_TREE
, se
.expr
);
2505 transfer_array_desc (&se
, &expr
->ts
, tmp
);
2506 goto finish_block_label
;
2509 /* Initialize the scalarizer. */
2510 ss
= gfc_walk_expr (expr
);
2511 gfc_init_loopinfo (&loop
);
2512 gfc_add_ss_to_loop (&loop
, ss
);
2514 /* Initialize the loop. */
2515 gfc_conv_ss_startstride (&loop
);
2516 gfc_conv_loop_setup (&loop
, &code
->expr1
->where
);
2518 /* The main loop body. */
2519 gfc_mark_ss_chain_used (ss
, 1);
2520 gfc_start_scalarized_body (&loop
, &body
);
2522 gfc_copy_loopinfo_to_se (&se
, &loop
);
2524 gfc_conv_expr_reference (&se
, expr
);
2525 if (expr
->ts
.type
== BT_CLASS
)
2526 vptr
= gfc_get_vptr_from_expr (ss
->info
->data
.array
.descriptor
);
2529 transfer_expr (&se
, &expr
->ts
, se
.expr
, code
, vptr
);
2534 gfc_add_block_to_block (&body
, &se
.pre
);
2535 gfc_add_block_to_block (&body
, &se
.post
);
2538 tmp
= gfc_finish_block (&body
);
2541 gcc_assert (expr
->rank
!= 0);
2542 gcc_assert (se
.ss
== gfc_ss_terminator
);
2543 gfc_trans_scalarizing_loops (&loop
, &body
);
2545 gfc_add_block_to_block (&loop
.pre
, &loop
.post
);
2546 tmp
= gfc_finish_block (&loop
.pre
);
2547 gfc_cleanup_loop (&loop
);
2550 gfc_add_expr_to_block (&block
, tmp
);
2552 return gfc_finish_block (&block
);
2555 #include "gt-fortran-trans-io.h"