OSDN Git Service

PR c/18946
[pf3gnuchains/gcc-fork.git] / gcc / fortran / trans-io.c
1 /* IO Code translation/library interface
2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Paul Brook
4
5 This file is part of GCC.
6
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 2, or (at your option) any later
10 version.
11
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
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "tree-gimple.h"
28 #include "ggc.h"
29 #include "toplev.h"
30 #include "real.h"
31 #include "gfortran.h"
32 #include "trans.h"
33 #include "trans-stmt.h"
34 #include "trans-array.h"
35 #include "trans-types.h"
36 #include "trans-const.h"
37
38
39 /* Members of the ioparm structure.  */
40
41 static GTY(()) tree ioparm_unit;
42 static GTY(()) tree ioparm_err;
43 static GTY(()) tree ioparm_end;
44 static GTY(()) tree ioparm_eor;
45 static GTY(()) tree ioparm_list_format;
46 static GTY(()) tree ioparm_library_return;
47 static GTY(()) tree ioparm_iostat;
48 static GTY(()) tree ioparm_exist;
49 static GTY(()) tree ioparm_opened;
50 static GTY(()) tree ioparm_number;
51 static GTY(()) tree ioparm_named;
52 static GTY(()) tree ioparm_rec;
53 static GTY(()) tree ioparm_nextrec;
54 static GTY(()) tree ioparm_size;
55 static GTY(()) tree ioparm_recl_in;
56 static GTY(()) tree ioparm_recl_out;
57 static GTY(()) tree ioparm_iolength;
58 static GTY(()) tree ioparm_file;
59 static GTY(()) tree ioparm_file_len;
60 static GTY(()) tree ioparm_status;
61 static GTY(()) tree ioparm_status_len;
62 static GTY(()) tree ioparm_access;
63 static GTY(()) tree ioparm_access_len;
64 static GTY(()) tree ioparm_form;
65 static GTY(()) tree ioparm_form_len;
66 static GTY(()) tree ioparm_blank;
67 static GTY(()) tree ioparm_blank_len;
68 static GTY(()) tree ioparm_position;
69 static GTY(()) tree ioparm_position_len;
70 static GTY(()) tree ioparm_action;
71 static GTY(()) tree ioparm_action_len;
72 static GTY(()) tree ioparm_delim;
73 static GTY(()) tree ioparm_delim_len;
74 static GTY(()) tree ioparm_pad;
75 static GTY(()) tree ioparm_pad_len;
76 static GTY(()) tree ioparm_format;
77 static GTY(()) tree ioparm_format_len;
78 static GTY(()) tree ioparm_advance;
79 static GTY(()) tree ioparm_advance_len;
80 static GTY(()) tree ioparm_name;
81 static GTY(()) tree ioparm_name_len;
82 static GTY(()) tree ioparm_internal_unit;
83 static GTY(()) tree ioparm_internal_unit_len;
84 static GTY(()) tree ioparm_sequential;
85 static GTY(()) tree ioparm_sequential_len;
86 static GTY(()) tree ioparm_direct;
87 static GTY(()) tree ioparm_direct_len;
88 static GTY(()) tree ioparm_formatted;
89 static GTY(()) tree ioparm_formatted_len;
90 static GTY(()) tree ioparm_unformatted;
91 static GTY(()) tree ioparm_unformatted_len;
92 static GTY(()) tree ioparm_read;
93 static GTY(()) tree ioparm_read_len;
94 static GTY(()) tree ioparm_write;
95 static GTY(()) tree ioparm_write_len;
96 static GTY(()) tree ioparm_readwrite;
97 static GTY(()) tree ioparm_readwrite_len;
98 static GTY(()) tree ioparm_namelist_name;
99 static GTY(()) tree ioparm_namelist_name_len;
100 static GTY(()) tree ioparm_namelist_read_mode;
101
102 /* The global I/O variables */
103
104 static GTY(()) tree ioparm_var;
105 static GTY(()) tree locus_file;
106 static GTY(()) tree locus_line;
107
108
109 /* Library I/O subroutines */
110
111 static GTY(()) tree iocall_read;
112 static GTY(()) tree iocall_read_done;
113 static GTY(()) tree iocall_write;
114 static GTY(()) tree iocall_write_done;
115 static GTY(()) tree iocall_x_integer;
116 static GTY(()) tree iocall_x_logical;
117 static GTY(()) tree iocall_x_character;
118 static GTY(()) tree iocall_x_real;
119 static GTY(()) tree iocall_x_complex;
120 static GTY(()) tree iocall_open;
121 static GTY(()) tree iocall_close;
122 static GTY(()) tree iocall_inquire;
123 static GTY(()) tree iocall_iolength;
124 static GTY(()) tree iocall_iolength_done;
125 static GTY(()) tree iocall_rewind;
126 static GTY(()) tree iocall_backspace;
127 static GTY(()) tree iocall_endfile;
128 static GTY(()) tree iocall_set_nml_val_int;
129 static GTY(()) tree iocall_set_nml_val_float;
130 static GTY(()) tree iocall_set_nml_val_char;
131 static GTY(()) tree iocall_set_nml_val_complex;
132 static GTY(()) tree iocall_set_nml_val_log;
133
134 /* Variable for keeping track of what the last data transfer statement
135    was.  Used for deciding which subroutine to call when the data
136    transfer is complete.  */
137 static enum { READ, WRITE, IOLENGTH } last_dt;
138
139 #define ADD_FIELD(name, type)                                           \
140   ioparm_ ## name = gfc_add_field_to_struct                             \
141         (&(TYPE_FIELDS (ioparm_type)), ioparm_type,                     \
142          get_identifier (stringize(name)), type)
143
144 #define ADD_STRING(name) \
145   ioparm_ ## name = gfc_add_field_to_struct                             \
146         (&(TYPE_FIELDS (ioparm_type)), ioparm_type,                     \
147          get_identifier (stringize(name)), pchar_type_node);            \
148   ioparm_ ## name ## _len = gfc_add_field_to_struct                     \
149         (&(TYPE_FIELDS (ioparm_type)), ioparm_type,                     \
150          get_identifier (stringize(name) "_len"), gfc_charlen_type_node)
151
152
153 /* Create function decls for IO library functions.  */
154
155 void
156 gfc_build_io_library_fndecls (void)
157 {
158   tree gfc_int4_type_node;
159   tree gfc_pint4_type_node;
160   tree ioparm_type;
161
162   gfc_int4_type_node = gfc_get_int_type (4);
163   gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
164
165   /* Build the st_parameter structure.  Information associated with I/O
166      calls are transferred here.  This must match the one defined in the
167      library exactly.  */
168
169   ioparm_type = make_node (RECORD_TYPE);
170   TYPE_NAME (ioparm_type) = get_identifier ("_gfc_ioparm");
171
172   ADD_FIELD (unit, gfc_int4_type_node);
173   ADD_FIELD (err, gfc_int4_type_node);
174   ADD_FIELD (end, gfc_int4_type_node);
175   ADD_FIELD (eor, gfc_int4_type_node);
176   ADD_FIELD (list_format, gfc_int4_type_node);
177   ADD_FIELD (library_return, gfc_int4_type_node);
178
179   ADD_FIELD (iostat, gfc_pint4_type_node);
180   ADD_FIELD (exist, gfc_pint4_type_node);
181   ADD_FIELD (opened, gfc_pint4_type_node);
182   ADD_FIELD (number, gfc_pint4_type_node);
183   ADD_FIELD (named, gfc_pint4_type_node);
184   ADD_FIELD (rec, gfc_int4_type_node);
185   ADD_FIELD (nextrec, gfc_pint4_type_node);
186   ADD_FIELD (size, gfc_pint4_type_node);
187
188   ADD_FIELD (recl_in, gfc_int4_type_node);
189   ADD_FIELD (recl_out, gfc_pint4_type_node);
190
191   ADD_FIELD (iolength, gfc_pint4_type_node);
192
193   ADD_STRING (file);
194   ADD_STRING (status);
195
196   ADD_STRING (access);
197   ADD_STRING (form);
198   ADD_STRING (blank);
199   ADD_STRING (position);
200   ADD_STRING (action);
201   ADD_STRING (delim);
202   ADD_STRING (pad);
203   ADD_STRING (format);
204   ADD_STRING (advance);
205   ADD_STRING (name);
206   ADD_STRING (internal_unit);
207   ADD_STRING (sequential);
208
209   ADD_STRING (direct);
210   ADD_STRING (formatted);
211   ADD_STRING (unformatted);
212   ADD_STRING (read);
213   ADD_STRING (write);
214   ADD_STRING (readwrite);
215
216   ADD_STRING (namelist_name);
217   ADD_FIELD (namelist_read_mode, gfc_int4_type_node);
218
219   gfc_finish_type (ioparm_type);
220
221   ioparm_var = build_decl (VAR_DECL, get_identifier (PREFIX("ioparm")),
222                            ioparm_type);
223   DECL_EXTERNAL (ioparm_var) = 1;
224   TREE_PUBLIC (ioparm_var) = 1;
225
226   locus_line = build_decl (VAR_DECL, get_identifier (PREFIX("line")),
227                            gfc_int4_type_node);
228   DECL_EXTERNAL (locus_line) = 1;
229   TREE_PUBLIC (locus_line) = 1;
230
231   locus_file = build_decl (VAR_DECL, get_identifier (PREFIX("filename")),
232                            pchar_type_node);
233   DECL_EXTERNAL (locus_file) = 1;
234   TREE_PUBLIC (locus_file) = 1;
235
236   /* Define the transfer functions.  */
237
238   iocall_x_integer =
239     gfc_build_library_function_decl (get_identifier
240                                      (PREFIX("transfer_integer")),
241                                      void_type_node, 2, pvoid_type_node,
242                                      gfc_int4_type_node);
243
244   iocall_x_logical =
245     gfc_build_library_function_decl (get_identifier
246                                      (PREFIX("transfer_logical")),
247                                      void_type_node, 2, pvoid_type_node,
248                                      gfc_int4_type_node);
249
250   iocall_x_character =
251     gfc_build_library_function_decl (get_identifier
252                                      (PREFIX("transfer_character")),
253                                      void_type_node, 2, pvoid_type_node,
254                                      gfc_int4_type_node);
255
256   iocall_x_real =
257     gfc_build_library_function_decl (get_identifier (PREFIX("transfer_real")),
258                                      void_type_node, 2,
259                                      pvoid_type_node, gfc_int4_type_node);
260
261   iocall_x_complex =
262     gfc_build_library_function_decl (get_identifier
263                                      (PREFIX("transfer_complex")),
264                                      void_type_node, 2, pvoid_type_node,
265                                      gfc_int4_type_node);
266
267   /* Library entry points */
268
269   iocall_read =
270     gfc_build_library_function_decl (get_identifier (PREFIX("st_read")),
271                                      void_type_node, 0);
272
273   iocall_write =
274     gfc_build_library_function_decl (get_identifier (PREFIX("st_write")),
275                                      void_type_node, 0);
276   iocall_open =
277     gfc_build_library_function_decl (get_identifier (PREFIX("st_open")),
278                                      void_type_node, 0);
279
280   iocall_close =
281     gfc_build_library_function_decl (get_identifier (PREFIX("st_close")),
282                                      void_type_node, 0);
283
284   iocall_inquire =
285     gfc_build_library_function_decl (get_identifier (PREFIX("st_inquire")),
286                                      gfc_int4_type_node, 0);
287
288   iocall_iolength =
289     gfc_build_library_function_decl(get_identifier (PREFIX("st_iolength")),
290                                     void_type_node, 0);
291
292   iocall_rewind =
293     gfc_build_library_function_decl (get_identifier (PREFIX("st_rewind")),
294                                      gfc_int4_type_node, 0);
295
296   iocall_backspace =
297     gfc_build_library_function_decl (get_identifier (PREFIX("st_backspace")),
298                                      gfc_int4_type_node, 0);
299
300   iocall_endfile =
301     gfc_build_library_function_decl (get_identifier (PREFIX("st_endfile")),
302                                      gfc_int4_type_node, 0);
303   /* Library helpers */
304
305   iocall_read_done =
306     gfc_build_library_function_decl (get_identifier (PREFIX("st_read_done")),
307                                      gfc_int4_type_node, 0);
308
309   iocall_write_done =
310     gfc_build_library_function_decl (get_identifier (PREFIX("st_write_done")),
311                                      gfc_int4_type_node, 0);
312
313   iocall_iolength_done =
314     gfc_build_library_function_decl (get_identifier (PREFIX("st_iolength_done")),
315                                      gfc_int4_type_node, 0);
316
317   iocall_set_nml_val_int =
318     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_int")),
319                                      void_type_node, 4,
320                                      pvoid_type_node, pvoid_type_node,
321                                      gfc_int4_type_node,gfc_int4_type_node);
322
323   iocall_set_nml_val_float =
324     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_float")),
325                                      void_type_node, 4,
326                                      pvoid_type_node, pvoid_type_node,
327                                      gfc_int4_type_node,gfc_int4_type_node);
328   iocall_set_nml_val_char =
329     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_char")),
330                                      void_type_node, 5,
331                                      pvoid_type_node, pvoid_type_node,
332                                      gfc_int4_type_node, gfc_int4_type_node, 
333                                      gfc_charlen_type_node);
334   iocall_set_nml_val_complex =
335     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_complex")),
336                                      void_type_node, 4,
337                                      pvoid_type_node, pvoid_type_node,
338                                      gfc_int4_type_node,gfc_int4_type_node);
339   iocall_set_nml_val_log =
340     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_log")),
341                                      void_type_node, 4,
342                                      pvoid_type_node, pvoid_type_node,
343                                      gfc_int4_type_node,gfc_int4_type_node);
344
345 }
346
347
348 /* Generate code to store an non-string I/O parameter into the
349    ioparm structure.  This is a pass by value.  */
350
351 static void
352 set_parameter_value (stmtblock_t * block, tree var, gfc_expr * e)
353 {
354   gfc_se se;
355   tree tmp;
356
357   gfc_init_se (&se, NULL);
358   gfc_conv_expr_type (&se, e, TREE_TYPE (var));
359   gfc_add_block_to_block (block, &se.pre);
360
361   tmp = build3 (COMPONENT_REF, TREE_TYPE (var), ioparm_var, var, NULL_TREE);
362   gfc_add_modify_expr (block, tmp, se.expr);
363 }
364
365
366 /* Generate code to store an non-string I/O parameter into the
367    ioparm structure.  This is pass by reference.  */
368
369 static void
370 set_parameter_ref (stmtblock_t * block, tree var, gfc_expr * e)
371 {
372   gfc_se se;
373   tree tmp;
374
375   gfc_init_se (&se, NULL);
376   se.want_pointer = 1;
377
378   gfc_conv_expr_type (&se, e, TREE_TYPE (var));
379   gfc_add_block_to_block (block, &se.pre);
380
381   tmp = build3 (COMPONENT_REF, TREE_TYPE (var), ioparm_var, var, NULL_TREE);
382   gfc_add_modify_expr (block, tmp, se.expr);
383 }
384
385
386 /* Generate code to store a string and its length into the
387    ioparm structure.  */
388
389 static void
390 set_string (stmtblock_t * block, stmtblock_t * postblock, tree var,
391             tree var_len, gfc_expr * e)
392 {
393   gfc_se se;
394   tree tmp;
395   tree msg;
396   tree io;
397   tree len;
398
399   gfc_init_se (&se, NULL);
400   gfc_conv_expr (&se, e);
401
402   io = build3 (COMPONENT_REF, TREE_TYPE (var), ioparm_var, var, NULL_TREE);
403   len = build3 (COMPONENT_REF, TREE_TYPE (var_len), ioparm_var, var_len,
404                 NULL_TREE);
405
406   /* Integer variable assigned a format label.  */
407   if (e->ts.type == BT_INTEGER && e->symtree->n.sym->attr.assign == 1)
408     {
409       msg =
410         gfc_build_cstring_const ("Assigned label is not a format label");
411       tmp = GFC_DECL_STRING_LEN (se.expr);
412       tmp = build2 (LE_EXPR, boolean_type_node,
413                     tmp, convert (TREE_TYPE (tmp), integer_minus_one_node));
414       gfc_trans_runtime_check (tmp, msg, &se.pre);
415       gfc_add_modify_expr (&se.pre, io, GFC_DECL_ASSIGN_ADDR (se.expr));
416       gfc_add_modify_expr (&se.pre, len, GFC_DECL_STRING_LEN (se.expr));
417     }
418   else
419     {
420       gfc_conv_string_parameter (&se);
421       gfc_add_modify_expr (&se.pre, io, fold_convert (TREE_TYPE (io), se.expr));
422       gfc_add_modify_expr (&se.pre, len, se.string_length);
423     }
424
425   gfc_add_block_to_block (block, &se.pre);
426   gfc_add_block_to_block (postblock, &se.post);
427
428 }
429
430
431 /* Set a member of the ioparm structure to one.  */
432 static void
433 set_flag (stmtblock_t *block, tree var)
434 {
435   tree tmp, type = TREE_TYPE (var);
436
437   tmp = build3 (COMPONENT_REF, type, ioparm_var, var, NULL_TREE);
438   gfc_add_modify_expr (block, tmp, convert (type, integer_one_node));
439 }
440
441
442 /* Add a case to a IO-result switch.  */
443
444 static void
445 add_case (int label_value, gfc_st_label * label, stmtblock_t * body)
446 {
447   tree tmp, value;
448
449   if (label == NULL)
450     return;                     /* No label, no case */
451
452   value = build_int_cst (NULL_TREE, label_value);
453
454   /* Make a backend label for this case.  */
455   tmp = gfc_build_label_decl (NULL_TREE);
456
457   /* And the case itself.  */
458   tmp = build3_v (CASE_LABEL_EXPR, value, NULL_TREE, tmp);
459   gfc_add_expr_to_block (body, tmp);
460
461   /* Jump to the label.  */
462   tmp = build1_v (GOTO_EXPR, gfc_get_label_decl (label));
463   gfc_add_expr_to_block (body, tmp);
464 }
465
466
467 /* Generate a switch statement that branches to the correct I/O
468    result label.  The last statement of an I/O call stores the
469    result into a variable because there is often cleanup that
470    must be done before the switch, so a temporary would have to
471    be created anyway.  */
472
473 static void
474 io_result (stmtblock_t * block, gfc_st_label * err_label,
475            gfc_st_label * end_label, gfc_st_label * eor_label)
476 {
477   stmtblock_t body;
478   tree tmp, rc;
479
480   /* If no labels are specified, ignore the result instead
481      of building an empty switch.  */
482   if (err_label == NULL
483       && end_label == NULL
484       && eor_label == NULL)
485     return;
486
487   /* Build a switch statement.  */
488   gfc_start_block (&body);
489
490   /* The label values here must be the same as the values
491      in the library_return enum in the runtime library */
492   add_case (1, err_label, &body);
493   add_case (2, end_label, &body);
494   add_case (3, eor_label, &body);
495
496   tmp = gfc_finish_block (&body);
497
498   rc = build3 (COMPONENT_REF, TREE_TYPE (ioparm_library_return), ioparm_var,
499                ioparm_library_return, NULL_TREE);
500
501   tmp = build3_v (SWITCH_EXPR, rc, tmp, NULL_TREE);
502
503   gfc_add_expr_to_block (block, tmp);
504 }
505
506
507 /* Store the current file and line number to variables so that if a
508    library call goes awry, we can tell the user where the problem is.  */
509
510 static void
511 set_error_locus (stmtblock_t * block, locus * where)
512 {
513   gfc_file *f;
514   tree tmp;
515   int line;
516
517   f = where->lb->file;
518   tmp = gfc_build_cstring_const (f->filename);
519
520   tmp = gfc_build_addr_expr (pchar_type_node, tmp);
521   gfc_add_modify_expr (block, locus_file, tmp);
522
523 #ifdef USE_MAPPED_LOCATION
524   line = LOCATION_LINE (where->lb->location);
525 #else
526   line = where->lb->linenum;
527 #endif
528   gfc_add_modify_expr (block, locus_line, build_int_cst (NULL_TREE, line));
529 }
530
531
532 /* Translate an OPEN statement.  */
533
534 tree
535 gfc_trans_open (gfc_code * code)
536 {
537   stmtblock_t block, post_block;
538   gfc_open *p;
539   tree tmp;
540
541   gfc_init_block (&block);
542   gfc_init_block (&post_block);
543
544   set_error_locus (&block, &code->loc);
545   p = code->ext.open;
546
547   if (p->unit)
548     set_parameter_value (&block, ioparm_unit, p->unit);
549
550   if (p->file)
551     set_string (&block, &post_block, ioparm_file, ioparm_file_len, p->file);
552
553   if (p->status)
554     set_string (&block, &post_block, ioparm_status,
555                 ioparm_status_len, p->status);
556
557   if (p->access)
558     set_string (&block, &post_block, ioparm_access,
559                 ioparm_access_len, p->access);
560
561   if (p->form)
562     set_string (&block, &post_block, ioparm_form, ioparm_form_len, p->form);
563
564   if (p->recl)
565     set_parameter_value (&block, ioparm_recl_in, p->recl);
566
567   if (p->blank)
568     set_string (&block, &post_block, ioparm_blank, ioparm_blank_len,
569                 p->blank);
570
571   if (p->position)
572     set_string (&block, &post_block, ioparm_position,
573                 ioparm_position_len, p->position);
574
575   if (p->action)
576     set_string (&block, &post_block, ioparm_action,
577                 ioparm_action_len, p->action);
578
579   if (p->delim)
580     set_string (&block, &post_block, ioparm_delim, ioparm_delim_len,
581                 p->delim);
582
583   if (p->pad)
584     set_string (&block, &post_block, ioparm_pad, ioparm_pad_len, p->pad);
585
586   if (p->iostat)
587     set_parameter_ref (&block, ioparm_iostat, p->iostat);
588
589   if (p->err)
590     set_flag (&block, ioparm_err);
591
592   tmp = gfc_build_function_call (iocall_open, NULL_TREE);
593   gfc_add_expr_to_block (&block, tmp);
594
595   gfc_add_block_to_block (&block, &post_block);
596
597   io_result (&block, p->err, NULL, NULL);
598
599   return gfc_finish_block (&block);
600 }
601
602
603 /* Translate a CLOSE statement.  */
604
605 tree
606 gfc_trans_close (gfc_code * code)
607 {
608   stmtblock_t block, post_block;
609   gfc_close *p;
610   tree tmp;
611
612   gfc_init_block (&block);
613   gfc_init_block (&post_block);
614
615   set_error_locus (&block, &code->loc);
616   p = code->ext.close;
617
618   if (p->unit)
619     set_parameter_value (&block, ioparm_unit, p->unit);
620
621   if (p->status)
622     set_string (&block, &post_block, ioparm_status,
623                 ioparm_status_len, p->status);
624
625   if (p->iostat)
626     set_parameter_ref (&block, ioparm_iostat, p->iostat);
627
628   if (p->err)
629     set_flag (&block, ioparm_err);
630
631   tmp = gfc_build_function_call (iocall_close, NULL_TREE);
632   gfc_add_expr_to_block (&block, tmp);
633
634   gfc_add_block_to_block (&block, &post_block);
635
636   io_result (&block, p->err, NULL, NULL);
637
638   return gfc_finish_block (&block);
639 }
640
641
642 /* Common subroutine for building a file positioning statement.  */
643
644 static tree
645 build_filepos (tree function, gfc_code * code)
646 {
647   stmtblock_t block;
648   gfc_filepos *p;
649   tree tmp;
650
651   p = code->ext.filepos;
652
653   gfc_init_block (&block);
654
655   set_error_locus (&block, &code->loc);
656
657   if (p->unit)
658     set_parameter_value (&block, ioparm_unit, p->unit);
659
660   if (p->iostat)
661     set_parameter_ref (&block, ioparm_iostat, p->iostat);
662
663   if (p->err)
664     set_flag (&block, ioparm_err);
665
666   tmp = gfc_build_function_call (function, NULL);
667   gfc_add_expr_to_block (&block, tmp);
668
669   io_result (&block, p->err, NULL, NULL);
670
671   return gfc_finish_block (&block);
672 }
673
674
675 /* Translate a BACKSPACE statement.  */
676
677 tree
678 gfc_trans_backspace (gfc_code * code)
679 {
680
681   return build_filepos (iocall_backspace, code);
682 }
683
684
685 /* Translate an ENDFILE statement.  */
686
687 tree
688 gfc_trans_endfile (gfc_code * code)
689 {
690
691   return build_filepos (iocall_endfile, code);
692 }
693
694
695 /* Translate a REWIND statement.  */
696
697 tree
698 gfc_trans_rewind (gfc_code * code)
699 {
700
701   return build_filepos (iocall_rewind, code);
702 }
703
704
705 /* Translate the non-IOLENGTH form of an INQUIRE statement.  */
706
707 tree
708 gfc_trans_inquire (gfc_code * code)
709 {
710   stmtblock_t block, post_block;
711   gfc_inquire *p;
712   tree tmp;
713
714   gfc_init_block (&block);
715   gfc_init_block (&post_block);
716
717   set_error_locus (&block, &code->loc);
718   p = code->ext.inquire;
719
720   if (p->unit)
721     set_parameter_value (&block, ioparm_unit, p->unit);
722
723   if (p->file)
724     set_string (&block, &post_block, ioparm_file, ioparm_file_len, p->file);
725
726   if (p->iostat)
727     set_parameter_ref (&block, ioparm_iostat, p->iostat);
728
729   if (p->exist)
730     set_parameter_ref (&block, ioparm_exist, p->exist);
731
732   if (p->opened)
733     set_parameter_ref (&block, ioparm_opened, p->opened);
734
735   if (p->number)
736     set_parameter_ref (&block, ioparm_number, p->number);
737
738   if (p->named)
739     set_parameter_ref (&block, ioparm_named, p->named);
740
741   if (p->name)
742     set_string (&block, &post_block, ioparm_name, ioparm_name_len, p->name);
743
744   if (p->access)
745     set_string (&block, &post_block, ioparm_access,
746                 ioparm_access_len, p->access);
747
748   if (p->sequential)
749     set_string (&block, &post_block, ioparm_sequential,
750                 ioparm_sequential_len, p->sequential);
751
752   if (p->direct)
753     set_string (&block, &post_block, ioparm_direct,
754                 ioparm_direct_len, p->direct);
755
756   if (p->form)
757     set_string (&block, &post_block, ioparm_form, ioparm_form_len, p->form);
758
759   if (p->formatted)
760     set_string (&block, &post_block, ioparm_formatted,
761                 ioparm_formatted_len, p->formatted);
762
763   if (p->unformatted)
764     set_string (&block, &post_block, ioparm_unformatted,
765                 ioparm_unformatted_len, p->unformatted);
766
767   if (p->recl)
768     set_parameter_ref (&block, ioparm_recl_out, p->recl);
769
770   if (p->nextrec)
771     set_parameter_ref (&block, ioparm_nextrec, p->nextrec);
772
773   if (p->blank)
774     set_string (&block, &post_block, ioparm_blank, ioparm_blank_len,
775                 p->blank);
776
777   if (p->position)
778     set_string (&block, &post_block, ioparm_position,
779                 ioparm_position_len, p->position);
780
781   if (p->action)
782     set_string (&block, &post_block, ioparm_action,
783                 ioparm_action_len, p->action);
784
785   if (p->read)
786     set_string (&block, &post_block, ioparm_read, ioparm_read_len, p->read);
787
788   if (p->write)
789     set_string (&block, &post_block, ioparm_write,
790                 ioparm_write_len, p->write);
791
792   if (p->readwrite)
793     set_string (&block, &post_block, ioparm_readwrite,
794                 ioparm_readwrite_len, p->readwrite);
795
796   if (p->delim)
797     set_string (&block, &post_block, ioparm_delim, ioparm_delim_len,
798                 p->delim);
799
800   if (p->pad)
801     set_string (&block, &post_block, ioparm_pad, ioparm_pad_len,
802                 p->pad); 
803
804   if (p->err)
805     set_flag (&block, ioparm_err);
806
807   tmp = gfc_build_function_call (iocall_inquire, NULL);
808   gfc_add_expr_to_block (&block, tmp);
809
810   gfc_add_block_to_block (&block, &post_block);
811
812   io_result (&block, p->err, NULL, NULL);
813
814   return gfc_finish_block (&block);
815 }
816
817
818 static gfc_expr *
819 gfc_new_nml_name_expr (char * name)
820 {
821    gfc_expr * nml_name;
822    nml_name = gfc_get_expr();
823    nml_name->ref = NULL;
824    nml_name->expr_type = EXPR_CONSTANT;
825    nml_name->ts.kind = gfc_default_character_kind;
826    nml_name->ts.type = BT_CHARACTER;
827    nml_name->value.character.length = strlen(name);
828    nml_name->value.character.string = name;
829
830    return nml_name;
831 }
832
833 static gfc_expr *
834 get_new_var_expr(gfc_symbol * sym)
835 {
836   gfc_expr * nml_var;
837
838   nml_var = gfc_get_expr();
839   nml_var->expr_type = EXPR_VARIABLE;
840   nml_var->ts = sym->ts;
841   if (sym->as)
842     nml_var->rank = sym->as->rank;
843   nml_var->symtree = (gfc_symtree *)gfc_getmem (sizeof (gfc_symtree));
844   nml_var->symtree->n.sym = sym;
845   nml_var->where = sym->declared_at;
846   sym->attr.referenced = 1;
847
848   return nml_var;
849 }
850
851 /* For a scalar variable STRING whose address is ADDR_EXPR, generate a
852    call to iocall_set_nml_val.  For derived type variable, recursively
853    generate calls to iocall_set_nml_val for each leaf field. The leafs
854    have no names -- their STRING field is null, and are interpreted by
855    the run-time library as having only the value, as in the example:
856
857    &foo bzz=1,2,3,4,5/
858
859    Note that the first output field appears after the name of the
860    variable, not of the field name.  This causes a little complication
861    documented below.  */
862
863 static void
864 transfer_namelist_element (stmtblock_t * block, gfc_typespec * ts, tree addr_expr, 
865                            tree string, tree string_length)
866 {
867   tree tmp, args, arg2;
868   tree expr;
869
870   gcc_assert (POINTER_TYPE_P (TREE_TYPE (addr_expr)));
871
872   if (ts->type == BT_DERIVED)
873     {
874       gfc_component *c;
875       expr = gfc_build_indirect_ref (addr_expr);
876
877       for (c = ts->derived->components; c; c = c->next)
878         {
879           tree field = c->backend_decl;
880           gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
881           tmp = build3 (COMPONENT_REF, TREE_TYPE (field), 
882                         expr, field, NULL_TREE);
883
884           if (c->dimension)
885             gfc_todo_error ("NAMELIST IO of array in derived type");
886           if (!c->pointer)
887             tmp = gfc_build_addr_expr (NULL, tmp);
888           transfer_namelist_element (block, &c->ts, tmp, string, string_length);
889
890           /* The first output field bears the name of the topmost
891              derived type variable.  All other fields are anonymous
892              and appear with nulls in their string and string_length
893              fields.  After the first use, we set string and
894              string_length to null.  */
895           string = null_pointer_node;
896           string_length = integer_zero_node;
897         }
898
899       return;
900     }
901
902   args = gfc_chainon_list (NULL_TREE, addr_expr);
903   args = gfc_chainon_list (args, string);
904   args = gfc_chainon_list (args, string_length);
905   arg2 = build_int_cst (gfc_array_index_type, ts->kind);
906   args = gfc_chainon_list (args,arg2);
907
908   switch (ts->type)
909     {
910     case BT_INTEGER:
911       tmp = gfc_build_function_call (iocall_set_nml_val_int, args);
912       break;
913
914     case BT_CHARACTER:
915       expr = gfc_build_indirect_ref (addr_expr);
916       gcc_assert (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE);
917       args = gfc_chainon_list (args,
918                                TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (expr))));
919       tmp = gfc_build_function_call (iocall_set_nml_val_char, args);
920       break;
921
922     case BT_REAL:
923       tmp = gfc_build_function_call (iocall_set_nml_val_float, args);
924       break;
925
926     case BT_LOGICAL:
927       tmp = gfc_build_function_call (iocall_set_nml_val_log, args);
928       break;
929
930     case BT_COMPLEX:
931       tmp = gfc_build_function_call (iocall_set_nml_val_complex, args);
932       break;
933
934     default :
935       internal_error ("Bad namelist IO basetype (%d)", ts->type);
936     }
937
938   gfc_add_expr_to_block (block, tmp);
939 }
940
941 /* Create a data transfer statement.  Not all of the fields are valid
942    for both reading and writing, but improper use has been filtered
943    out by now.  */
944
945 static tree
946 build_dt (tree * function, gfc_code * code)
947 {
948   stmtblock_t block, post_block;
949   gfc_dt *dt;
950   tree tmp;
951   gfc_expr *nmlname, *nmlvar;
952   gfc_namelist *nml;
953   gfc_se se,se2;
954
955   gfc_init_block (&block);
956   gfc_init_block (&post_block);
957
958   set_error_locus (&block, &code->loc);
959   dt = code->ext.dt;
960
961   gcc_assert (dt != NULL);
962
963   if (dt->io_unit)
964     {
965       if (dt->io_unit->ts.type == BT_CHARACTER)
966         {
967           set_string (&block, &post_block, ioparm_internal_unit,
968                       ioparm_internal_unit_len, dt->io_unit);
969         }
970       else
971         set_parameter_value (&block, ioparm_unit, dt->io_unit);
972     }
973
974   if (dt->rec)
975     set_parameter_value (&block, ioparm_rec, dt->rec);
976
977   if (dt->advance)
978     set_string (&block, &post_block, ioparm_advance, ioparm_advance_len,
979                 dt->advance);
980
981   if (dt->format_expr)
982     set_string (&block, &post_block, ioparm_format, ioparm_format_len,
983                 dt->format_expr);
984
985   if (dt->format_label)
986     {
987       if (dt->format_label == &format_asterisk)
988         set_flag (&block, ioparm_list_format);
989       else
990         set_string (&block, &post_block, ioparm_format,
991                     ioparm_format_len, dt->format_label->format);
992     }
993
994   if (dt->iostat)
995     set_parameter_ref (&block, ioparm_iostat, dt->iostat);
996
997   if (dt->size)
998     set_parameter_ref (&block, ioparm_size, dt->size);
999
1000   if (dt->err)
1001     set_flag (&block, ioparm_err);
1002
1003   if (dt->eor)
1004     set_flag(&block, ioparm_eor);
1005
1006   if (dt->end)
1007     set_flag(&block, ioparm_end);
1008
1009   if (dt->namelist)
1010     {
1011        if (dt->format_expr || dt->format_label)
1012           fatal_error("A format cannot be specified with a namelist");
1013
1014        nmlname = gfc_new_nml_name_expr(dt->namelist->name);
1015
1016        set_string (&block, &post_block, ioparm_namelist_name,
1017                 ioparm_namelist_name_len, nmlname);
1018
1019        if (last_dt == READ)
1020           set_flag (&block, ioparm_namelist_read_mode);
1021
1022         for (nml = dt->namelist->namelist; nml; nml = nml->next)
1023           {
1024             gfc_init_se (&se, NULL);
1025             gfc_init_se (&se2, NULL);
1026             nmlvar = get_new_var_expr (nml->sym);
1027             nmlname = gfc_new_nml_name_expr (nml->sym->name);
1028             gfc_conv_expr_reference (&se2, nmlname);
1029             gfc_conv_expr_reference (&se, nmlvar);
1030             gfc_evaluate_now (se.expr, &se.pre); 
1031
1032             transfer_namelist_element (&block, &nml->sym->ts, se.expr,
1033                                        se2.expr, se2.string_length);
1034           }
1035     }
1036
1037   tmp = gfc_build_function_call (*function, NULL_TREE);
1038   gfc_add_expr_to_block (&block, tmp);
1039
1040   gfc_add_block_to_block (&block, &post_block);
1041
1042   return gfc_finish_block (&block);
1043 }
1044
1045
1046 /* Translate the IOLENGTH form of an INQUIRE statement.  We treat
1047    this as a third sort of data transfer statement, except that
1048    lengths are summed instead of actually transferring any data.  */
1049
1050 tree
1051 gfc_trans_iolength (gfc_code * code)
1052 {
1053   stmtblock_t block;
1054   gfc_inquire *inq;
1055   tree dt;
1056
1057   gfc_init_block (&block);
1058
1059   set_error_locus (&block, &code->loc);
1060
1061   inq = code->ext.inquire;
1062
1063   /* First check that preconditions are met.  */
1064   gcc_assert (inq != NULL);
1065   gcc_assert (inq->iolength != NULL);
1066
1067   /* Connect to the iolength variable.  */
1068   if (inq->iolength)
1069     set_parameter_ref (&block, ioparm_iolength, inq->iolength);
1070
1071   /* Actual logic.  */
1072   last_dt = IOLENGTH;
1073   dt = build_dt(&iocall_iolength, code);
1074
1075   gfc_add_expr_to_block (&block, dt);
1076
1077   return gfc_finish_block (&block);
1078 }
1079
1080
1081 /* Translate a READ statement.  */
1082
1083 tree
1084 gfc_trans_read (gfc_code * code)
1085 {
1086
1087   last_dt = READ;
1088   return build_dt (&iocall_read, code);
1089 }
1090
1091
1092 /* Translate a WRITE statement */
1093
1094 tree
1095 gfc_trans_write (gfc_code * code)
1096 {
1097
1098   last_dt = WRITE;
1099   return build_dt (&iocall_write, code);
1100 }
1101
1102
1103 /* Finish a data transfer statement.  */
1104
1105 tree
1106 gfc_trans_dt_end (gfc_code * code)
1107 {
1108   tree function, tmp;
1109   stmtblock_t block;
1110
1111   gfc_init_block (&block);
1112
1113   switch (last_dt)
1114     {
1115     case READ:
1116       function = iocall_read_done;
1117       break;
1118
1119     case WRITE:
1120       function = iocall_write_done;
1121       break;
1122
1123     case IOLENGTH:
1124       function = iocall_iolength_done;
1125       break;
1126
1127     default:
1128       gcc_unreachable ();
1129     }
1130
1131   tmp = gfc_build_function_call (function, NULL);
1132   gfc_add_expr_to_block (&block, tmp);
1133
1134   if (last_dt != IOLENGTH)
1135     {
1136       gcc_assert (code->ext.dt != NULL);
1137       io_result (&block, code->ext.dt->err,
1138                  code->ext.dt->end, code->ext.dt->eor);
1139     }
1140
1141   return gfc_finish_block (&block);
1142 }
1143
1144 static void
1145 transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr);
1146
1147 /* Given an array field in a derived type variable, generate the code
1148    for the loop that iterates over array elements, and the code that
1149    accesses those array elements.  Use transfer_expr to generate code
1150    for transferring that element.  Because elements may also be
1151    derived types, transfer_expr and transfer_array_component are mutually
1152    recursive.  */
1153
1154 static tree
1155 transfer_array_component (tree expr, gfc_component * cm)
1156 {
1157   tree tmp;
1158   stmtblock_t body;
1159   stmtblock_t block;
1160   gfc_loopinfo loop;
1161   int n;
1162   gfc_ss *ss;
1163   gfc_se se;
1164
1165   gfc_start_block (&block);
1166   gfc_init_se (&se, NULL);
1167
1168   /* Create and initialize Scalarization Status.  Unlike in
1169      gfc_trans_transfer, we can't simply use gfc_walk_expr to take
1170      care of this task, because we don't have a gfc_expr at hand.
1171      Build one manually, as in gfc_trans_subarray_assign.  */
1172
1173   ss = gfc_get_ss ();
1174   ss->type = GFC_SS_COMPONENT;
1175   ss->expr = NULL;
1176   ss->shape = gfc_get_shape (cm->as->rank);
1177   ss->next = gfc_ss_terminator;
1178   ss->data.info.dimen = cm->as->rank;
1179   ss->data.info.descriptor = expr;
1180   ss->data.info.data = gfc_conv_array_data (expr);
1181   ss->data.info.offset = gfc_conv_array_offset (expr);
1182   for (n = 0; n < cm->as->rank; n++)
1183     {
1184       ss->data.info.dim[n] = n;
1185       ss->data.info.start[n] = gfc_conv_array_lbound (expr, n);
1186       ss->data.info.stride[n] = gfc_index_one_node;
1187
1188       mpz_init (ss->shape[n]);
1189       mpz_sub (ss->shape[n], cm->as->upper[n]->value.integer,
1190                cm->as->lower[n]->value.integer);
1191       mpz_add_ui (ss->shape[n], ss->shape[n], 1);
1192     }
1193
1194   /* Once we got ss, we use scalarizer to create the loop.  */
1195
1196   gfc_init_loopinfo (&loop);
1197   gfc_add_ss_to_loop (&loop, ss);
1198   gfc_conv_ss_startstride (&loop);
1199   gfc_conv_loop_setup (&loop);
1200   gfc_mark_ss_chain_used (ss, 1);
1201   gfc_start_scalarized_body (&loop, &body);
1202
1203   gfc_copy_loopinfo_to_se (&se, &loop);
1204   se.ss = ss;
1205
1206   /* gfc_conv_tmp_array_ref assumes that se.expr contains the array.  */
1207   se.expr = expr;
1208   gfc_conv_tmp_array_ref (&se);
1209
1210   /* Now se.expr contains an element of the array.  Take the address and pass
1211      it to the IO routines.  */
1212   tmp = gfc_build_addr_expr (NULL, se.expr);
1213   transfer_expr (&se, &cm->ts, tmp);
1214
1215   /* We are done now with the loop body.  Wrap up the scalarizer and
1216      return.  */
1217
1218   gfc_add_block_to_block (&body, &se.pre);
1219   gfc_add_block_to_block (&body, &se.post);
1220
1221   gfc_trans_scalarizing_loops (&loop, &body);
1222
1223   gfc_add_block_to_block (&block, &loop.pre);
1224   gfc_add_block_to_block (&block, &loop.post);
1225
1226   for (n = 0; n < cm->as->rank; n++)
1227     mpz_clear (ss->shape[n]);
1228   gfc_free (ss->shape);
1229
1230   gfc_cleanup_loop (&loop);
1231
1232   return gfc_finish_block (&block);
1233 }
1234
1235 /* Generate the call for a scalar transfer node.  */
1236
1237 static void
1238 transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr)
1239 {
1240   tree args, tmp, function, arg2, field, expr;
1241   gfc_component *c;
1242   int kind;
1243
1244   kind = ts->kind;
1245   function = NULL;
1246   arg2 = NULL;
1247
1248   switch (ts->type)
1249     {
1250     case BT_INTEGER:
1251       arg2 = build_int_cst (NULL_TREE, kind);
1252       function = iocall_x_integer;
1253       break;
1254
1255     case BT_REAL:
1256       arg2 = build_int_cst (NULL_TREE, kind);
1257       function = iocall_x_real;
1258       break;
1259
1260     case BT_COMPLEX:
1261       arg2 = build_int_cst (NULL_TREE, kind);
1262       function = iocall_x_complex;
1263       break;
1264
1265     case BT_LOGICAL:
1266       arg2 = build_int_cst (NULL_TREE, kind);
1267       function = iocall_x_logical;
1268       break;
1269
1270     case BT_CHARACTER:
1271       if (se->string_length)
1272         arg2 = se->string_length;
1273       else
1274         {
1275           tmp = gfc_build_indirect_ref (addr_expr);
1276           gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
1277           arg2 = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tmp)));
1278         }
1279       function = iocall_x_character;
1280       break;
1281
1282     case BT_DERIVED:
1283       /* Recurse into the elements of the derived type.  */
1284       expr = gfc_evaluate_now (addr_expr, &se->pre);
1285       expr = gfc_build_indirect_ref (expr);
1286
1287       for (c = ts->derived->components; c; c = c->next)
1288         {
1289           field = c->backend_decl;
1290           gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
1291
1292           tmp = build3 (COMPONENT_REF, TREE_TYPE (field), expr, field,
1293                         NULL_TREE);
1294
1295           if (c->dimension)
1296             {
1297               tmp = transfer_array_component (tmp, c);
1298               gfc_add_expr_to_block (&se->pre, tmp);
1299             }
1300           else
1301             {
1302               if (!c->pointer)
1303                 tmp = gfc_build_addr_expr (NULL, tmp);
1304               transfer_expr (se, &c->ts, tmp);
1305             }
1306         }
1307       return;
1308
1309     default:
1310       internal_error ("Bad IO basetype (%d)", ts->type);
1311     }
1312
1313   args = gfc_chainon_list (NULL_TREE, addr_expr);
1314   args = gfc_chainon_list (args, arg2);
1315
1316   tmp = gfc_build_function_call (function, args);
1317   gfc_add_expr_to_block (&se->pre, tmp);
1318   gfc_add_block_to_block (&se->pre, &se->post);
1319
1320 }
1321
1322
1323 /* gfc_trans_transfer()-- Translate a TRANSFER code node */
1324
1325 tree
1326 gfc_trans_transfer (gfc_code * code)
1327 {
1328   stmtblock_t block, body;
1329   gfc_loopinfo loop;
1330   gfc_expr *expr;
1331   gfc_ss *ss;
1332   gfc_se se;
1333   tree tmp;
1334
1335   gfc_start_block (&block);
1336
1337   expr = code->expr;
1338   ss = gfc_walk_expr (expr);
1339
1340   gfc_init_se (&se, NULL);
1341
1342   if (ss == gfc_ss_terminator)
1343     gfc_init_block (&body);
1344   else
1345     {
1346       /* Initialize the scalarizer.  */
1347       gfc_init_loopinfo (&loop);
1348       gfc_add_ss_to_loop (&loop, ss);
1349
1350       /* Initialize the loop.  */
1351       gfc_conv_ss_startstride (&loop);
1352       gfc_conv_loop_setup (&loop);
1353
1354       /* The main loop body.  */
1355       gfc_mark_ss_chain_used (ss, 1);
1356       gfc_start_scalarized_body (&loop, &body);
1357
1358       gfc_copy_loopinfo_to_se (&se, &loop);
1359       se.ss = ss;
1360     }
1361
1362   gfc_conv_expr_reference (&se, expr);
1363
1364   transfer_expr (&se, &expr->ts, se.expr);
1365
1366   gfc_add_block_to_block (&body, &se.pre);
1367   gfc_add_block_to_block (&body, &se.post);
1368
1369   if (se.ss == NULL)
1370     tmp = gfc_finish_block (&body);
1371   else
1372     {
1373       gcc_assert (se.ss == gfc_ss_terminator);
1374       gfc_trans_scalarizing_loops (&loop, &body);
1375
1376       gfc_add_block_to_block (&loop.pre, &loop.post);
1377       tmp = gfc_finish_block (&loop.pre);
1378       gfc_cleanup_loop (&loop);
1379     }
1380
1381   gfc_add_expr_to_block (&block, tmp);
1382
1383   return gfc_finish_block (&block);
1384 }
1385
1386 #include "gt-fortran-trans-io.h"
1387