OSDN Git Service

2008-01-22 Paul Thomas <pault@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / trans-io.c
index ed6cf9a..1e12415 100644 (file)
@@ -1,12 +1,13 @@
 /* IO Code translation/library interface
-   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
+   Foundation, Inc.
    Contributed by Paul Brook
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 2, or (at your option) any later
+Software Foundation; either version 3, or (at your option) any later
 version.
 
 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -15,9 +16,8 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to the Free
-Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
-02110-1301, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 
 #include "config.h"
@@ -35,7 +35,6 @@ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
 #include "trans-types.h"
 #include "trans-const.h"
 
-
 /* Members of the ioparm structure.  */
 
 enum ioparam_type
@@ -52,7 +51,9 @@ enum ioparam_type
 enum iofield_type
 {
   IOPARM_type_int4,
+  IOPARM_type_intio,
   IOPARM_type_pint4,
+  IOPARM_type_pintio,
   IOPARM_type_pchar,
   IOPARM_type_parray,
   IOPARM_type_pad,
@@ -160,7 +161,7 @@ gfc_build_st_parameter (enum ioparam_type ptype, tree *types)
   gcc_assert (len <= sizeof (name) - sizeof ("__st_parameter_"));
   memcpy (name, "__st_parameter_", sizeof ("__st_parameter_"));
   memcpy (name + sizeof ("__st_parameter_") - 1, st_parameter[ptype].name,
-         len);
+         len + 1);
   TYPE_NAME (t) = get_identifier (name);
 
   for (type = 0, p = st_parameter_field; type < IOPARM_field_num; type++, p++)
@@ -168,7 +169,9 @@ gfc_build_st_parameter (enum ioparam_type ptype, tree *types)
       switch (p->type)
        {
        case IOPARM_type_int4:
+       case IOPARM_type_intio:
        case IOPARM_type_pint4:
+       case IOPARM_type_pintio:
        case IOPARM_type_parray:
        case IOPARM_type_pchar:
        case IOPARM_type_pad:
@@ -208,26 +211,92 @@ gfc_build_st_parameter (enum ioparam_type ptype, tree *types)
   st_parameter[ptype].type = t;
 }
 
+
+/* Build code to test an error condition and call generate_error if needed.
+   Note: This builds calls to generate_error in the runtime library function.
+   The function generate_error is dependent on certain parameters in the
+   st_parameter_common flags to be set. (See libgfortran/runtime/error.c)
+   Therefore, the code to set these flags must be generated before
+   this function is used.  */
+
+void
+gfc_trans_io_runtime_check (tree cond, tree var, int error_code,
+                        const char * msgid, stmtblock_t * pblock)
+{
+  stmtblock_t block;
+  tree body;
+  tree tmp;
+  tree arg1, arg2, arg3;
+  char *message;
+
+  if (integer_zerop (cond))
+    return;
+
+  /* The code to generate the error.  */
+  gfc_start_block (&block);
+  
+  arg1 = build_fold_addr_expr (var);
+  
+  arg2 = build_int_cst (integer_type_node, error_code),
+  
+  asprintf (&message, "%s", _(msgid));
+  arg3 = gfc_build_addr_expr (pchar_type_node, gfc_build_cstring_const(message));
+  gfc_free(message);
+  
+  tmp = build_call_expr (gfor_fndecl_generate_error, 3, arg1, arg2, arg3);
+
+  gfc_add_expr_to_block (&block, tmp);
+
+  body = gfc_finish_block (&block);
+
+  if (integer_onep (cond))
+    {
+      gfc_add_expr_to_block (pblock, body);
+    }
+  else
+    {
+      /* Tell the compiler that this isn't likely.  */
+      cond = fold_convert (long_integer_type_node, cond);
+      tmp = build_int_cst (long_integer_type_node, 0);
+      cond = build_call_expr (built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
+      cond = fold_convert (boolean_type_node, cond);
+
+      tmp = build3_v (COND_EXPR, cond, body, build_empty_stmt ());
+      gfc_add_expr_to_block (pblock, tmp);
+    }
+}
+
+
 /* Create function decls for IO library functions.  */
 
 void
 gfc_build_io_library_fndecls (void)
 {
   tree types[IOPARM_type_num], pad_idx, gfc_int4_type_node;
+  tree gfc_intio_type_node;
   tree parm_type, dt_parm_type;
-  tree gfc_c_int_type_node;
   HOST_WIDE_INT pad_size;
   enum ioparam_type ptype;
 
   types[IOPARM_type_int4] = gfc_int4_type_node = gfc_get_int_type (4);
+  types[IOPARM_type_intio] = gfc_intio_type_node
+                           = gfc_get_int_type (gfc_intio_kind);
   types[IOPARM_type_pint4] = build_pointer_type (gfc_int4_type_node);
+  types[IOPARM_type_pintio]
+                           = build_pointer_type (gfc_intio_type_node);
   types[IOPARM_type_parray] = pchar_type_node;
   types[IOPARM_type_pchar] = pchar_type_node;
   pad_size = 16 * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (pchar_type_node));
   pad_size += 32 * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (integer_type_node));
   pad_idx = build_index_type (build_int_cst (NULL_TREE, pad_size));
   types[IOPARM_type_pad] = build_array_type (char_type_node, pad_idx);
-  gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
+
+  /* pad actually contains pointers and integers so it needs to have an
+     alignment that is at least as large as the needed alignment for those
+     types.  See the st_parameter_dt structure in libgfortran/io/io.h for
+     what really goes into this space.  */
+  TYPE_ALIGN (types[IOPARM_type_pad]) = MAX (TYPE_ALIGN (pchar_type_node),
+                    TYPE_ALIGN (gfc_get_int_type (gfc_max_integer_kind)));
 
   for (ptype = IOPARM_ptype_common; ptype < IOPARM_ptype_num; ptype++)
     gfc_build_st_parameter (ptype, types);
@@ -269,7 +338,7 @@ gfc_build_io_library_fndecls (void)
     gfc_build_library_function_decl (get_identifier
                                     (PREFIX("transfer_array")),
                                     void_type_node, 4, dt_parm_type,
-                                    pvoid_type_node, gfc_c_int_type_node,
+                                    pvoid_type_node, integer_type_node,
                                     gfc_charlen_type_node);
 
   /* Library entry points */
@@ -344,8 +413,8 @@ gfc_build_io_library_fndecls (void)
   iocall[IOCALL_SET_NML_VAL_DIM] =
     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_dim")),
                                     void_type_node, 5, dt_parm_type,
-                                    gfc_int4_type_node, gfc_int4_type_node,
-                                    gfc_int4_type_node, gfc_int4_type_node);
+                                    gfc_int4_type_node, gfc_array_index_type,
+                                    gfc_array_index_type, gfc_array_index_type);
 }
 
 
@@ -379,16 +448,46 @@ set_parameter_value (stmtblock_t *block, tree var, enum iofield type,
   gfc_se se;
   tree tmp;
   gfc_st_parameter_field *p = &st_parameter_field[type];
+  tree dest_type = TREE_TYPE (p->field);
 
   gfc_init_se (&se, NULL);
-  gfc_conv_expr_type (&se, e, TREE_TYPE (p->field));
+  gfc_conv_expr_val (&se, e);
+
+  /* If we're storing a UNIT number, we need to check it first.  */
+  if (type == IOPARM_common_unit && e->ts.kind != 4)
+    {
+      tree cond, max;
+      int i;
+
+      /* Don't evaluate the UNIT number multiple times.  */
+      se.expr = gfc_evaluate_now (se.expr, &se.pre);
+
+      /* UNIT numbers should be nonnegative.  */
+      cond = fold_build2 (LT_EXPR, boolean_type_node, se.expr,
+                         build_int_cst (TREE_TYPE (se.expr),0));
+      gfc_trans_io_runtime_check (cond, var, LIBERROR_BAD_UNIT,
+                              "Negative unit number in I/O statement",
+                              &se.pre);
+    
+      /* UNIT numbers should be less than the max.  */
+      i = gfc_validate_kind (BT_INTEGER, 4, false);
+      max = gfc_conv_mpz_to_tree (gfc_integer_kinds[i].huge, 4);
+      cond = fold_build2 (GT_EXPR, boolean_type_node, se.expr,
+                         fold_convert (TREE_TYPE (se.expr), max));
+      gfc_trans_io_runtime_check (cond, var, LIBERROR_BAD_UNIT,
+                              "Unit number in I/O statement too large",
+                              &se.pre);
+
+    }
+
+  se.expr = convert (dest_type, se.expr);
   gfc_add_block_to_block (block, &se.pre);
 
   if (p->param_type == IOPARM_ptype_common)
     var = build3 (COMPONENT_REF, st_parameter[IOPARM_ptype_common].type,
                  var, TYPE_FIELDS (TREE_TYPE (var)), NULL_TREE);
-  tmp = build3 (COMPONENT_REF, TREE_TYPE (p->field), var, p->field,
-               NULL_TREE);
+
+  tmp = build3 (COMPONENT_REF, dest_type, var, p->field, NULL_TREE);
   gfc_add_modify_expr (block, tmp, se.expr);
   return p->mask;
 }
@@ -413,20 +512,34 @@ set_parameter_ref (stmtblock_t *block, stmtblock_t *postblock,
 
   if (TYPE_MODE (TREE_TYPE (se.expr))
       == TYPE_MODE (TREE_TYPE (TREE_TYPE (p->field))))
-    addr = convert (TREE_TYPE (p->field),
-                   build_fold_addr_expr (se.expr));
+    {
+      addr = convert (TREE_TYPE (p->field), build_fold_addr_expr (se.expr));
+
+      /* If this is for the iostat variable initialize the
+        user variable to LIBERROR_OK which is zero.  */
+      if (type == IOPARM_common_iostat)
+       gfc_add_modify_expr (block, se.expr,
+                            build_int_cst (TREE_TYPE (se.expr), LIBERROR_OK));
+    }
   else
     {
       /* The type used by the library has different size
-        from the type of the variable supplied by the user.
-        Need to use a temporary.  */
-      tree tmpvar
-       = gfc_create_var (TREE_TYPE (TREE_TYPE (p->field)),
-                         st_parameter_field[type].name);
+       from the type of the variable supplied by the user.
+       Need to use a temporary.  */
+      tree tmpvar = gfc_create_var (TREE_TYPE (TREE_TYPE (p->field)),
+                                   st_parameter_field[type].name);
+
+      /* If this is for the iostat variable, initialize the
+        user variable to LIBERROR_OK which is zero.  */
+      if (type == IOPARM_common_iostat)
+       gfc_add_modify_expr (block, tmpvar,
+                            build_int_cst (TREE_TYPE (tmpvar), LIBERROR_OK));
+
       addr = build_fold_addr_expr (tmpvar);
+       /* After the I/O operation, we set the variable from the temporary.  */
       tmp = convert (TREE_TYPE (se.expr), tmpvar);
       gfc_add_modify_expr (postblock, se.expr, tmp);
-    }
+     }
 
   if (p->param_type == IOPARM_ptype_common)
     var = build3 (COMPONENT_REF, st_parameter[IOPARM_ptype_common].type,
@@ -495,7 +608,8 @@ gfc_convert_array_to_string (gfc_se * se, gfc_expr * e)
     }
 
   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
-  size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
+  size = fold_build2 (MULT_EXPR, gfc_array_index_type, size,
+                     fold_convert (gfc_array_index_type, tmp));
 
   se->string_length = fold_convert (gfc_charlen_type_node, size);
 }
@@ -510,7 +624,6 @@ set_string (stmtblock_t * block, stmtblock_t * postblock, tree var,
 {
   gfc_se se;
   tree tmp;
-  tree msg;
   tree io;
   tree len;
   gfc_st_parameter_field *p = &st_parameter_field[type];
@@ -528,13 +641,20 @@ set_string (stmtblock_t * block, stmtblock_t * postblock, tree var,
   /* Integer variable assigned a format label.  */
   if (e->ts.type == BT_INTEGER && e->symtree->n.sym->attr.assign == 1)
     {
+      char * msg;
+      tree cond;
+
       gfc_conv_label_variable (&se, e);
-      msg =
-        gfc_build_cstring_const ("Assigned label is not a format label");
       tmp = GFC_DECL_STRING_LEN (se.expr);
-      tmp = build2 (LE_EXPR, boolean_type_node,
-                   tmp, convert (TREE_TYPE (tmp), integer_minus_one_node));
-      gfc_trans_runtime_check (tmp, msg, &se.pre);
+      cond = fold_build2 (LT_EXPR, boolean_type_node,
+                         tmp, build_int_cst (TREE_TYPE (tmp), 0));
+
+      asprintf(&msg, "Label assigned to variable '%s' (%%ld) is not a format "
+              "label", e->symtree->name);
+      gfc_trans_runtime_check (cond, &se.pre, &e->where, msg,
+                              fold_convert (long_integer_type_node, tmp));
+      gfc_free (msg);
+
       gfc_add_modify_expr (&se.pre, io,
                 fold_convert (TREE_TYPE (io), GFC_DECL_ASSIGN_ADDR (se.expr)));
       gfc_add_modify_expr (&se.pre, len, GFC_DECL_STRING_LEN (se.expr));
@@ -565,7 +685,8 @@ set_string (stmtblock_t * block, stmtblock_t * postblock, tree var,
    for an internal unit.  */
 
 static unsigned int
-set_internal_unit (stmtblock_t * block, tree var, gfc_expr * e)
+set_internal_unit (stmtblock_t * block, stmtblock_t * post_block,
+                  tree var, gfc_expr * e)
 {
   gfc_se se;
   tree io;
@@ -595,18 +716,31 @@ set_internal_unit (stmtblock_t * block, tree var, gfc_expr * e)
       gfc_conv_expr (&se, e);
       gfc_conv_string_parameter (&se);
       tmp = se.expr;
-      se.expr = fold_convert (pchar_type_node, integer_zero_node);
+      se.expr = build_int_cst (pchar_type_node, 0);
     }
 
   /* Character array.  */
-  else if (e->symtree && (e->symtree->n.sym->as->rank > 0))
+  else if (e->rank > 0)
     {
       se.ss = gfc_walk_expr (e);
 
-      /* Return the data pointer and rank from the descriptor.  */
-      gfc_conv_expr_descriptor (&se, e, se.ss);
-      tmp = gfc_conv_descriptor_data_get (se.expr);
-      se.expr = gfc_build_addr_expr (pchar_type_node, se.expr);
+      if (is_subref_array (e))
+       {
+         /* Use a temporary for components of arrays of derived types
+            or substring array references.  */
+         gfc_conv_subref_array_arg (&se, e, 0,
+               last_dt == READ ? INTENT_IN : INTENT_OUT);
+         tmp = build_fold_indirect_ref (se.expr);
+         se.expr = gfc_build_addr_expr (pchar_type_node, tmp);
+         tmp = gfc_conv_descriptor_data_get (tmp);
+       }
+      else
+       {
+         /* Return the data pointer and rank from the descriptor.  */
+         gfc_conv_expr_descriptor (&se, e, se.ss);
+         tmp = gfc_conv_descriptor_data_get (se.expr);
+         se.expr = gfc_build_addr_expr (pchar_type_node, se.expr);
+       }
     }
   else
     gcc_unreachable ();
@@ -614,10 +748,12 @@ set_internal_unit (stmtblock_t * block, tree var, gfc_expr * e)
   /* The cast is needed for character substrings and the descriptor
      data.  */
   gfc_add_modify_expr (&se.pre, io, fold_convert (TREE_TYPE (io), tmp));
-  gfc_add_modify_expr (&se.pre, len, se.string_length);
+  gfc_add_modify_expr (&se.pre, len,
+                      fold_convert (TREE_TYPE (len), se.string_length));
   gfc_add_modify_expr (&se.pre, desc, se.expr);
 
   gfc_add_block_to_block (block, &se.pre);
+  gfc_add_block_to_block (post_block, &se.post);
   return mask;
 }
 
@@ -739,10 +875,16 @@ gfc_trans_open (gfc_code * code)
   set_error_locus (&block, var, &code->loc);
   p = code->ext.open;
 
-  if (p->unit)
-    set_parameter_value (&block, var, IOPARM_common_unit, p->unit);
-  else
-    set_parameter_const (&block, var, IOPARM_common_unit, 0);
+  if (p->iomsg)
+    mask |= set_string (&block, &post_block, var, IOPARM_common_iomsg,
+                       p->iomsg);
+
+  if (p->iostat)
+    mask |= set_parameter_ref (&block, &post_block, var, IOPARM_common_iostat,
+                              p->iostat);
+
+  if (p->err)
+    mask |= IOPARM_common_err;
 
   if (p->file)
     mask |= set_string (&block, &post_block, var, IOPARM_open_file, p->file);
@@ -780,26 +922,19 @@ gfc_trans_open (gfc_code * code)
   if (p->pad)
     mask |= set_string (&block, &post_block, var, IOPARM_open_pad, p->pad);
 
-  if (p->iomsg)
-    mask |= set_string (&block, &post_block, var, IOPARM_common_iomsg,
-                       p->iomsg);
-
-  if (p->iostat)
-    mask |= set_parameter_ref (&block, &post_block, var, IOPARM_common_iostat,
-                              p->iostat);
-
-  if (p->err)
-    mask |= IOPARM_common_err;
-
   if (p->convert)
     mask |= set_string (&block, &post_block, var, IOPARM_open_convert,
                        p->convert);
 
   set_parameter_const (&block, var, IOPARM_common_flags, mask);
 
+  if (p->unit)
+    set_parameter_value (&block, var, IOPARM_common_unit, p->unit);
+  else
+    set_parameter_const (&block, var, IOPARM_common_unit, 0);
+
   tmp = build_fold_addr_expr (var);
-  tmp = gfc_chainon_list (NULL_TREE, tmp);
-  tmp = build_function_call_expr (iocall[IOCALL_OPEN], tmp);
+  tmp = build_call_expr (iocall[IOCALL_OPEN], 1, tmp);
   gfc_add_expr_to_block (&block, tmp);
 
   gfc_add_block_to_block (&block, &post_block);
@@ -828,15 +963,6 @@ gfc_trans_close (gfc_code * code)
   set_error_locus (&block, var, &code->loc);
   p = code->ext.close;
 
-  if (p->unit)
-    set_parameter_value (&block, var, IOPARM_common_unit, p->unit);
-  else
-    set_parameter_const (&block, var, IOPARM_common_unit, 0);
-
-  if (p->status)
-    mask |= set_string (&block, &post_block, var, IOPARM_close_status,
-                       p->status);
-
   if (p->iomsg)
     mask |= set_string (&block, &post_block, var, IOPARM_common_iomsg,
                        p->iomsg);
@@ -848,11 +974,19 @@ gfc_trans_close (gfc_code * code)
   if (p->err)
     mask |= IOPARM_common_err;
 
+  if (p->status)
+    mask |= set_string (&block, &post_block, var, IOPARM_close_status,
+                       p->status);
+
   set_parameter_const (&block, var, IOPARM_common_flags, mask);
 
+  if (p->unit)
+    set_parameter_value (&block, var, IOPARM_common_unit, p->unit);
+  else
+    set_parameter_const (&block, var, IOPARM_common_unit, 0);
+
   tmp = build_fold_addr_expr (var);
-  tmp = gfc_chainon_list (NULL_TREE, tmp);
-  tmp = build_function_call_expr (iocall[IOCALL_CLOSE], tmp);
+  tmp = build_call_expr (iocall[IOCALL_CLOSE], 1, tmp);
   gfc_add_expr_to_block (&block, tmp);
 
   gfc_add_block_to_block (&block, &post_block);
@@ -883,11 +1017,6 @@ build_filepos (tree function, gfc_code * code)
 
   set_error_locus (&block, var, &code->loc);
 
-  if (p->unit)
-    set_parameter_value (&block, var, IOPARM_common_unit, p->unit);
-  else
-    set_parameter_const (&block, var, IOPARM_common_unit, 0);
-
   if (p->iomsg)
     mask |= set_string (&block, &post_block, var, IOPARM_common_iomsg,
                        p->iomsg);
@@ -901,9 +1030,13 @@ build_filepos (tree function, gfc_code * code)
 
   set_parameter_const (&block, var, IOPARM_common_flags, mask);
 
+  if (p->unit)
+    set_parameter_value (&block, var, IOPARM_common_unit, p->unit);
+  else
+    set_parameter_const (&block, var, IOPARM_common_unit, 0);
+
   tmp = build_fold_addr_expr (var);
-  tmp = gfc_chainon_list (NULL_TREE, tmp);
-  tmp = build_function_call_expr (function, tmp);
+  tmp = build_call_expr (function, 1, tmp);
   gfc_add_expr_to_block (&block, tmp);
 
   gfc_add_block_to_block (&block, &post_block);
@@ -950,6 +1083,33 @@ gfc_trans_flush (gfc_code * code)
 }
 
 
+/* Create a dummy iostat variable to catch any error due to bad unit.  */
+
+static gfc_expr *
+create_dummy_iostat (void)
+{
+  gfc_symtree *st;
+  gfc_expr *e;
+
+  gfc_get_ha_sym_tree ("@iostat", &st);
+  st->n.sym->ts.type = BT_INTEGER;
+  st->n.sym->ts.kind = gfc_default_integer_kind;
+  gfc_set_sym_referenced (st->n.sym);
+  gfc_commit_symbol (st->n.sym);
+  st->n.sym->backend_decl
+       = gfc_create_var (gfc_get_int_type (st->n.sym->ts.kind),
+                         st->n.sym->name);
+
+  e = gfc_get_expr ();
+  e->expr_type = EXPR_VARIABLE;
+  e->symtree = st;
+  e->ts.type = BT_INTEGER;
+  e->ts.kind = st->n.sym->ts.kind;
+
+  return e;
+}
+
+
 /* Translate the non-IOLENGTH form of an INQUIRE statement.  */
 
 tree
@@ -969,19 +1129,6 @@ gfc_trans_inquire (gfc_code * code)
   set_error_locus (&block, var, &code->loc);
   p = code->ext.inquire;
 
-  /* Sanity check.  */
-  if (p->unit && p->file)
-    gfc_error ("INQUIRE statement at %L cannot contain both FILE and UNIT specifiers.", &code->loc);
-
-  if (p->unit)
-    set_parameter_value (&block, var, IOPARM_common_unit, p->unit);
-  else
-    set_parameter_const (&block, var, IOPARM_common_unit, 0);
-
-  if (p->file)
-    mask |= set_string (&block, &post_block, var, IOPARM_inquire_file,
-                       p->file);
-
   if (p->iomsg)
     mask |= set_string (&block, &post_block, var, IOPARM_common_iomsg,
                        p->iomsg);
@@ -990,9 +1137,29 @@ gfc_trans_inquire (gfc_code * code)
     mask |= set_parameter_ref (&block, &post_block, var, IOPARM_common_iostat,
                               p->iostat);
 
+  if (p->err)
+    mask |= IOPARM_common_err;
+
+  /* Sanity check.  */
+  if (p->unit && p->file)
+    gfc_error ("INQUIRE statement at %L cannot contain both FILE and UNIT specifiers", &code->loc);
+
+  if (p->file)
+    mask |= set_string (&block, &post_block, var, IOPARM_inquire_file,
+                       p->file);
+
   if (p->exist)
-    mask |= set_parameter_ref (&block, &post_block, var, IOPARM_inquire_exist,
-                              p->exist);
+    {
+      mask |= set_parameter_ref (&block, &post_block, var, IOPARM_inquire_exist,
+                                p->exist);
+    
+      if (p->unit && !p->iostat)
+       {
+         p->iostat = create_dummy_iostat ();
+         mask |= set_parameter_ref (&block, &post_block, var,
+                                    IOPARM_common_iostat, p->iostat);
+       }
+    }
 
   if (p->opened)
     mask |= set_parameter_ref (&block, &post_block, var, IOPARM_inquire_opened,
@@ -1074,18 +1241,23 @@ gfc_trans_inquire (gfc_code * code)
     mask |= set_string (&block, &post_block, var, IOPARM_inquire_pad,
                        p->pad);
 
-  if (p->err)
-    mask |= IOPARM_common_err;
-
   if (p->convert)
     mask |= set_string (&block, &post_block, var, IOPARM_inquire_convert,
                        p->convert);
 
+  if (p->strm_pos)
+    mask |= set_parameter_ref (&block, &post_block, var,
+                              IOPARM_inquire_strm_pos_out, p->strm_pos);
+
   set_parameter_const (&block, var, IOPARM_common_flags, mask);
 
+  if (p->unit)
+    set_parameter_value (&block, var, IOPARM_common_unit, p->unit);
+  else
+    set_parameter_const (&block, var, IOPARM_common_unit, 0);
+
   tmp = build_fold_addr_expr (var);
-  tmp = gfc_chainon_list (NULL_TREE, tmp);
-  tmp = build_function_call_expr (iocall[IOCALL_INQUIRE], tmp);
+  tmp = build_call_expr (iocall[IOCALL_INQUIRE], 1, tmp);
   gfc_add_expr_to_block (&block, tmp);
 
   gfc_add_block_to_block (&block, &post_block);
@@ -1113,7 +1285,7 @@ gfc_new_nml_name_expr (const char * name)
 }
 
 /* nml_full_name builds up the fully qualified name of a
-   derived type component. */
+   derived type component.  */
 
 static char*
 nml_full_name (const char* var_name, const char* cmp_name)
@@ -1133,7 +1305,7 @@ nml_full_name (const char* var_name, const char* cmp_name)
    gfc_symbol or gfc_component backend_decl's. An offset is
    provided so that the address of an element of an array of
    derived types is returned. This is used in the runtime to
-   determine that span of the derived type. */
+   determine that span of the derived type.  */
 
 static tree
 nml_get_addr_expr (gfc_symbol * sym, gfc_component * c,
@@ -1149,6 +1321,13 @@ nml_get_addr_expr (gfc_symbol * sym, gfc_component * c,
     {
       sym->attr.referenced = 1;
       decl = gfc_get_symbol_decl (sym);
+
+      /* If this is the enclosing function declaration, use
+        the fake result instead.  */
+      if (decl == current_function_decl)
+       decl = gfc_get_fake_result_decl (sym, 0);
+      else if (decl == DECL_CONTEXT (current_function_decl))
+       decl =  gfc_get_fake_result_decl (sym, 1);
     }
   else
     decl = c->backend_decl;
@@ -1187,7 +1366,7 @@ nml_get_addr_expr (gfc_symbol * sym, gfc_component * c,
      a RECORD_TYPE.  */
 
   if (array_flagged)
-    tmp = gfc_build_array_ref (tmp, gfc_index_zero_node);
+    tmp = gfc_build_array_ref (tmp, gfc_index_zero_node, NULL);
 
   /* Now build the address expression.  */
 
@@ -1207,8 +1386,6 @@ nml_get_addr_expr (gfc_symbol * sym, gfc_component * c,
    call to iocall[IOCALL_SET_NML_VAL].  For derived type variable, recursively
    generate calls to iocall[IOCALL_SET_NML_VAL] for each component.  */
 
-#define NML_FIRST_ARG(a) args = gfc_chainon_list (NULL_TREE, a)
-#define NML_ADD_ARG(a) args = gfc_chainon_list (args, a)
 #define IARG(i) build_int_cst (gfc_array_index_type, i)
 
 static void
@@ -1222,7 +1399,6 @@ transfer_namelist_element (stmtblock_t * block, const char * var_name,
   tree dt = NULL;
   tree string;
   tree tmp;
-  tree args;
   tree dtype;
   tree dt_parm_addr;
   int n_dim; 
@@ -1288,18 +1464,14 @@ transfer_namelist_element (stmtblock_t * block, const char * var_name,
      (address, name, type, kind or string_length, dtype)  */
 
   dt_parm_addr = build_fold_addr_expr (dt_parm);
-  NML_FIRST_ARG (dt_parm_addr);
-  NML_ADD_ARG (addr_expr);
-  NML_ADD_ARG (string);
-  NML_ADD_ARG (IARG (ts->kind));
 
   if (ts->type == BT_CHARACTER)
-    NML_ADD_ARG (ts->cl->backend_decl);
+    tmp = ts->cl->backend_decl;
   else
-    NML_ADD_ARG (convert (gfc_charlen_type_node, integer_zero_node));
-
-  NML_ADD_ARG (dtype);
-  tmp = build_function_call_expr (iocall[IOCALL_SET_NML_VAL], args);
+    tmp = build_int_cst (gfc_charlen_type_node, 0);
+  tmp = build_call_expr (iocall[IOCALL_SET_NML_VAL], 6,
+                        dt_parm_addr, addr_expr, string,
+                        IARG (ts->kind), tmp, dtype);
   gfc_add_expr_to_block (block, tmp);
 
   /* If the object is an array, transfer rank times:
@@ -1307,12 +1479,12 @@ transfer_namelist_element (stmtblock_t * block, const char * var_name,
 
   for ( n_dim = 0 ; n_dim < rank ; n_dim++ )
     {
-      NML_FIRST_ARG (dt_parm_addr);
-      NML_ADD_ARG (IARG (n_dim));
-      NML_ADD_ARG (GFC_TYPE_ARRAY_STRIDE (dt, n_dim));
-      NML_ADD_ARG (GFC_TYPE_ARRAY_LBOUND (dt, n_dim));
-      NML_ADD_ARG (GFC_TYPE_ARRAY_UBOUND (dt, n_dim));
-      tmp = build_function_call_expr (iocall[IOCALL_SET_NML_VAL_DIM], args);
+      tmp = build_call_expr (iocall[IOCALL_SET_NML_VAL_DIM], 5,
+                            dt_parm_addr,
+                            IARG (n_dim),
+                            GFC_TYPE_ARRAY_STRIDE (dt, n_dim),
+                            GFC_TYPE_ARRAY_LBOUND (dt, n_dim),
+                            GFC_TYPE_ARRAY_UBOUND (dt, n_dim));
       gfc_add_expr_to_block (block, tmp);
     }
 
@@ -1336,8 +1508,6 @@ transfer_namelist_element (stmtblock_t * block, const char * var_name,
 }
 
 #undef IARG
-#undef NML_ADD_ARG
-#undef NML_FIRST_ARG
 
 /* Create a data transfer statement.  Not all of the fields are valid
    for both reading and writing, but improper use has been filtered
@@ -1346,7 +1516,7 @@ transfer_namelist_element (stmtblock_t * block, const char * var_name,
 static tree
 build_dt (tree function, gfc_code * code)
 {
-  stmtblock_t block, post_block, post_end_block;
+  stmtblock_t block, post_block, post_end_block, post_iu_block;
   gfc_dt *dt;
   tree tmp, var;
   gfc_expr *nmlname;
@@ -1356,6 +1526,7 @@ build_dt (tree function, gfc_code * code)
   gfc_start_block (&block);
   gfc_init_block (&post_block);
   gfc_init_block (&post_end_block);
+  gfc_init_block (&post_iu_block);
 
   var = gfc_create_var (st_parameter[IOPARM_ptype_dt].type, "dt_parm");
 
@@ -1386,17 +1557,33 @@ build_dt (tree function, gfc_code * code)
     {
       if (dt->io_unit->ts.type == BT_CHARACTER)
        {
-         mask |= set_internal_unit (&block, var, dt->io_unit);
+         mask |= set_internal_unit (&block, &post_iu_block,
+                                    var, dt->io_unit);
          set_parameter_const (&block, var, IOPARM_common_unit, 0);
        }
-      else
-       set_parameter_value (&block, var, IOPARM_common_unit, dt->io_unit);
     }
   else
     set_parameter_const (&block, var, IOPARM_common_unit, 0);
 
   if (dt)
     {
+      if (dt->iomsg)
+       mask |= set_string (&block, &post_block, var, IOPARM_common_iomsg,
+                           dt->iomsg);
+
+      if (dt->iostat)
+       mask |= set_parameter_ref (&block, &post_end_block, var,
+                                  IOPARM_common_iostat, dt->iostat);
+
+      if (dt->err)
+       mask |= IOPARM_common_err;
+
+      if (dt->eor)
+       mask |= IOPARM_common_eor;
+
+      if (dt->end)
+       mask |= IOPARM_common_end;
+
       if (dt->rec)
        mask |= set_parameter_value (&block, var, IOPARM_dt_rec, dt->rec);
 
@@ -1405,7 +1592,7 @@ build_dt (tree function, gfc_code * code)
                            dt->advance);
 
       if (dt->format_expr)
-       mask |= set_string (&block, &post_block, var, IOPARM_dt_format,
+       mask |= set_string (&block, &post_end_block, var, IOPARM_dt_format,
                            dt->format_expr);
 
       if (dt->format_label)
@@ -1417,27 +1604,10 @@ build_dt (tree function, gfc_code * code)
                                dt->format_label->format);
        }
 
-      if (dt->iomsg)
-       mask |= set_string (&block, &post_block, var, IOPARM_common_iomsg,
-                           dt->iomsg);
-
-      if (dt->iostat)
-       mask |= set_parameter_ref (&block, &post_end_block, var,
-                                  IOPARM_common_iostat, dt->iostat);
-
       if (dt->size)
        mask |= set_parameter_ref (&block, &post_end_block, var,
                                   IOPARM_dt_size, dt->size);
 
-      if (dt->err)
-       mask |= IOPARM_common_err;
-
-      if (dt->eor)
-       mask |= IOPARM_common_eor;
-
-      if (dt->end)
-       mask |= IOPARM_common_end;
-
       if (dt->namelist)
        {
          if (dt->format_expr || dt->format_label)
@@ -1461,13 +1631,15 @@ build_dt (tree function, gfc_code * code)
        }
       else
        set_parameter_const (&block, var, IOPARM_common_flags, mask);
+
+      if (dt->io_unit && dt->io_unit->ts.type == BT_INTEGER)
+       set_parameter_value (&block, var, IOPARM_common_unit, dt->io_unit);
     }
   else
     set_parameter_const (&block, var, IOPARM_common_flags, mask);
 
   tmp = build_fold_addr_expr (var);
-  tmp = gfc_chainon_list (NULL_TREE, tmp);
-  tmp = build_function_call_expr (function, tmp);
+  tmp = build_call_expr (function, 1, tmp);
   gfc_add_expr_to_block (&block, tmp);
 
   gfc_add_block_to_block (&block, &post_block);
@@ -1477,6 +1649,8 @@ build_dt (tree function, gfc_code * code)
 
   gfc_add_expr_to_block (&block, gfc_trans_code (code->block->next));
 
+  gfc_add_block_to_block (&block, &post_iu_block);
+
   dt_parm = NULL;
   dt_post_end_block = NULL;
 
@@ -1545,8 +1719,7 @@ gfc_trans_dt_end (gfc_code * code)
     }
 
   tmp = build_fold_addr_expr (dt_parm);
-  tmp = gfc_chainon_list (NULL_TREE, tmp);
-  tmp = build_function_call_expr (function, tmp);
+  tmp = build_call_expr (function, 1, tmp);
   gfc_add_expr_to_block (&block, tmp);
   gfc_add_block_to_block (&block, dt_post_end_block);
   gfc_init_block (dt_post_end_block);
@@ -1562,7 +1735,7 @@ gfc_trans_dt_end (gfc_code * code)
 }
 
 static void
-transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr);
+transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr, gfc_code * code);
 
 /* Given an array field in a derived type variable, generate the code
    for the loop that iterates over array elements, and the code that
@@ -1630,7 +1803,7 @@ transfer_array_component (tree expr, gfc_component * cm)
   /* Now se.expr contains an element of the array.  Take the address and pass
      it to the IO routines.  */
   tmp = build_fold_addr_expr (se.expr);
-  transfer_expr (&se, &cm->ts, tmp);
+  transfer_expr (&se, &cm->ts, tmp, NULL);
 
   /* We are done now with the loop body.  Wrap up the scalarizer and
      return.  */
@@ -1655,12 +1828,37 @@ transfer_array_component (tree expr, gfc_component * cm)
 /* Generate the call for a scalar transfer node.  */
 
 static void
-transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr)
+transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr, gfc_code * code)
 {
-  tree args, tmp, function, arg2, field, expr;
+  tree tmp, function, arg2, field, expr;
   gfc_component *c;
   int kind;
 
+  /* It is possible to get a C_NULL_PTR or C_NULL_FUNPTR expression here if
+     the user says something like: print *, 'c_null_ptr: ', c_null_ptr
+     We need to translate the expression to a constant if it's either
+     C_NULL_PTR or C_NULL_FUNPTR.  We could also get a user variable of
+     type C_PTR or C_FUNPTR, in which case the ts->type may no longer be
+     BT_DERIVED (could have been changed by gfc_conv_expr).  */
+  if ((ts->type == BT_DERIVED && ts->is_iso_c == 1 && ts->derived != NULL)
+      || (ts->derived != NULL && ts->derived->ts.is_iso_c == 1))
+    {
+      /* C_PTR and C_FUNPTR have private components which means they can not
+         be printed.  However, if -std=gnu and not -pedantic, allow
+         the component to be printed to help debugging.  */
+      if (gfc_notification_std (GFC_STD_GNU) != SILENT)
+       {
+         gfc_error_now ("Derived type '%s' at %L has PRIVATE components",
+                        ts->derived->name, code != NULL ? &(code->loc) : 
+                        &gfc_current_locus);
+         return;
+       }
+
+      ts->type = ts->derived->ts.type;
+      ts->kind = ts->derived->ts.kind;
+      ts->f90_type = ts->derived->ts.f90_type;
+    }
+  
   kind = ts->kind;
   function = NULL;
   arg2 = NULL;
@@ -1688,6 +1886,7 @@ transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr)
       break;
 
     case BT_CHARACTER:
+    case BT_HOLLERITH:
       if (se->string_length)
        arg2 = se->string_length;
       else
@@ -1721,7 +1920,7 @@ transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr)
             {
               if (!c->pointer)
                 tmp = build_fold_addr_expr (tmp);
-              transfer_expr (se, &c->ts, tmp);
+              transfer_expr (se, &c->ts, tmp, code);
             }
        }
       return;
@@ -1731,11 +1930,7 @@ transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr)
     }
 
   tmp = build_fold_addr_expr (dt_parm);
-  args = gfc_chainon_list (NULL_TREE, tmp);
-  args = gfc_chainon_list (args, addr_expr);
-  args = gfc_chainon_list (args, arg2);
-
-  tmp = build_function_call_expr (function, args);
+  tmp = build_call_expr (function, 3, tmp, addr_expr, arg2);
   gfc_add_expr_to_block (&se->pre, tmp);
   gfc_add_block_to_block (&se->pre, &se->post);
 
@@ -1748,21 +1943,18 @@ transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr)
 static void
 transfer_array_desc (gfc_se * se, gfc_typespec * ts, tree addr_expr)
 {
-  tree args, tmp, charlen_arg, kind_arg;
+  tree tmp, charlen_arg, kind_arg;
 
   if (ts->type == BT_CHARACTER)
     charlen_arg = se->string_length;
   else
-    charlen_arg = build_int_cstu (NULL_TREE, 0);
+    charlen_arg = build_int_cst (NULL_TREE, 0);
 
   kind_arg = build_int_cst (NULL_TREE, ts->kind);
 
   tmp = build_fold_addr_expr (dt_parm);
-  args = gfc_chainon_list (NULL_TREE, tmp);
-  args = gfc_chainon_list (args, addr_expr);
-  args = gfc_chainon_list (args, kind_arg);
-  args = gfc_chainon_list (args, charlen_arg);
-  tmp = build_function_call_expr (iocall[IOCALL_X_ARRAY], args);
+  tmp = build_call_expr (iocall[IOCALL_X_ARRAY], 4,
+                        tmp, addr_expr, kind_arg, charlen_arg);
   gfc_add_expr_to_block (&se->pre, tmp);
   gfc_add_block_to_block (&se->pre, &se->post);
 }
@@ -1780,6 +1972,7 @@ gfc_trans_transfer (gfc_code * code)
   gfc_ss *ss;
   gfc_se se;
   tree tmp;
+  int n;
 
   gfc_start_block (&block);
   gfc_init_block (&body);
@@ -1794,7 +1987,7 @@ gfc_trans_transfer (gfc_code * code)
     {
       /* Transfer a scalar value.  */
       gfc_conv_expr_reference (&se, expr);
-      transfer_expr (&se, &expr->ts, se.expr);
+      transfer_expr (&se, &expr->ts, se.expr, code);
     }
   else
     {
@@ -1808,11 +2001,32 @@ gfc_trans_transfer (gfc_code * code)
          gcc_assert (ref->type == REF_ARRAY);
        }
 
-      if (expr->ts.type != BT_DERIVED && ref && ref->next == NULL)
+      if (expr->ts.type != BT_DERIVED
+           && ref && ref->next == NULL
+           && !is_subref_array (expr))
        {
-         /* Get the descriptor.  */
-         gfc_conv_expr_descriptor (&se, expr, ss);
-         tmp = build_fold_addr_expr (se.expr);
+         bool seen_vector = false;
+
+         if (ref && ref->u.ar.type == AR_SECTION)
+           {
+             for (n = 0; n < ref->u.ar.dimen; n++)
+               if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
+                 seen_vector = true;
+           }
+
+         if (seen_vector && last_dt == READ)
+           {
+             /* Create a temp, read to that and copy it back.  */
+             gfc_conv_subref_array_arg (&se, expr, 0, INTENT_OUT);
+             tmp =  se.expr;
+           }
+         else
+           {
+             /* Get the descriptor.  */
+             gfc_conv_expr_descriptor (&se, expr, ss);
+             tmp = build_fold_addr_expr (se.expr);
+           }
+
          transfer_array_desc (&se, &expr->ts, tmp);
          goto finish_block_label;
        }
@@ -1833,7 +2047,7 @@ gfc_trans_transfer (gfc_code * code)
       se.ss = ss;
 
       gfc_conv_expr_reference (&se, expr);
-      transfer_expr (&se, &expr->ts, se.expr);
+      transfer_expr (&se, &expr->ts, se.expr, code);
     }
 
  finish_block_label: