OSDN Git Service

2011-07-17 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / trans-const.c
index e4da3f0..5fbe765 100644 (file)
@@ -1,6 +1,6 @@
 /* Translation of constants
-   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
-   Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+   Free Software Foundation, Inc.
    Contributed by Paul Brook
 
 This file is part of GCC.
@@ -25,9 +25,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tree.h"
-#include "ggc.h"
-#include "toplev.h"
-#include "real.h"
+#include "realmpfr.h"
+#include "diagnostic-core.h"   /* For fatal_error.  */
 #include "double-int.h"
 #include "gfortran.h"
 #include "trans.h"
@@ -76,11 +75,11 @@ gfc_build_string_const (int length, const char *s)
   tree len;
 
   str = build_string (length, s);
-  len = build_int_cst (NULL_TREE, length);
+  len = size_int (length);
   TREE_TYPE (str) =
     build_array_type (gfc_character1_type_node,
                      build_range_type (gfc_charlen_type_node,
-                                       integer_one_node, len));
+                                       size_one_node, len));
   return str;
 }
 
@@ -99,17 +98,17 @@ gfc_build_wide_string_const (int kind, int length, const gfc_char_t *string)
   i = gfc_validate_kind (BT_CHARACTER, kind, false);
   size = length * gfc_character_kinds[i].bit_size / 8;
 
-  s = gfc_getmem (size);
+  s = XCNEWVAR (char, size);
   gfc_encode_character (kind, length, string, (unsigned char *) s, size);
 
   str = build_string (size, s);
-  gfc_free (s);
+  free (s);
 
-  len = build_int_cst (NULL_TREE, length);
+  len = size_int (length);
   TREE_TYPE (str) =
     build_array_type (gfc_get_char_type (kind),
                      build_range_type (gfc_charlen_type_node,
-                                       integer_one_node, len));
+                                       size_one_node, len));
   return str;
 }
 
@@ -165,7 +164,7 @@ gfc_conv_string_init (tree length, gfc_expr * expr)
   str = gfc_build_wide_string_const (expr->ts.kind, len, s);
 
   if (free_s)
-    gfc_free (s);
+    free (s);
 
   return str;
 }
@@ -176,7 +175,7 @@ gfc_conv_string_init (tree length, gfc_expr * expr)
 void
 gfc_conv_const_charlen (gfc_charlen * cl)
 {
-  if (cl->backend_decl)
+  if (!cl || cl->backend_decl)
     return;
 
   if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
@@ -218,7 +217,7 @@ gfc_conv_tree_to_mpz (mpz_t i, tree source)
 /* Converts a real constant into backend form.  */
 
 tree
-gfc_conv_mpfr_to_tree (mpfr_t f, int kind)
+gfc_conv_mpfr_to_tree (mpfr_t f, int kind, int is_snan)
 {
   tree type;
   int n;
@@ -228,10 +227,34 @@ gfc_conv_mpfr_to_tree (mpfr_t f, int kind)
   gcc_assert (gfc_real_kinds[n].radix == 2);
 
   type = gfc_get_real_type (kind);
-  real_from_mpfr (&real, f, type, GFC_RND_MODE);
+  if (mpfr_nan_p (f) && is_snan)
+     real_from_string (&real, "SNaN");
+  else
+    real_from_mpfr (&real, f, type, GFC_RND_MODE);
+
   return build_real (type, real);
 }
 
+/* Returns a real constant that is +Infinity if the target
+   supports infinities for this floating-point mode, and
+   +HUGE_VAL otherwise (the largest representable number).  */
+
+tree
+gfc_build_inf_or_huge (tree type, int kind)
+{
+  if (HONOR_INFINITIES (TYPE_MODE (type)))
+    {
+      REAL_VALUE_TYPE real;
+      real_inf (&real);
+      return build_real (type, real);
+    }
+  else
+    {
+      int k = gfc_validate_kind (BT_REAL, kind, false);
+      return gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge, kind, 0);
+    }
+}
+
 /* Converts a backend tree into a real constant.  */
 
 void
@@ -263,44 +286,50 @@ gfc_conv_constant_to_tree (gfc_expr * expr)
     {
     case BT_INTEGER:
       if (expr->representation.string)
-       return fold_build1 (VIEW_CONVERT_EXPR,
-                           gfc_get_int_type (expr->ts.kind),
-                           gfc_build_string_const (expr->representation.length,
-                                                   expr->representation.string));
+       return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
+                        gfc_get_int_type (expr->ts.kind),
+                        gfc_build_string_const (expr->representation.length,
+                                                expr->representation.string));
       else
        return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
 
     case BT_REAL:
       if (expr->representation.string)
-       return fold_build1 (VIEW_CONVERT_EXPR,
-                           gfc_get_real_type (expr->ts.kind),
-                           gfc_build_string_const (expr->representation.length,
-                                                   expr->representation.string));
+       return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
+                        gfc_get_real_type (expr->ts.kind),
+                        gfc_build_string_const (expr->representation.length,
+                                                expr->representation.string));
       else
-       return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind);
+       return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind, expr->is_snan);
 
     case BT_LOGICAL:
       if (expr->representation.string)
-       return fold_build1 (VIEW_CONVERT_EXPR,
-                           gfc_get_logical_type (expr->ts.kind),
-                           gfc_build_string_const (expr->representation.length,
-                                                   expr->representation.string));
+       {
+         tree tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
+                       gfc_get_int_type (expr->ts.kind),
+                       gfc_build_string_const (expr->representation.length,
+                                               expr->representation.string));
+         if (!integer_zerop (tmp) && !integer_onep (tmp))
+           gfc_warning ("Assigning value other than 0 or 1 to LOGICAL"
+                        " has undefined result at %L", &expr->where);
+         return fold_convert (gfc_get_logical_type (expr->ts.kind), tmp);
+       }
       else
        return build_int_cst (gfc_get_logical_type (expr->ts.kind),
-                           expr->value.logical);
+                             expr->value.logical);
 
     case BT_COMPLEX:
       if (expr->representation.string)
-       return fold_build1 (VIEW_CONVERT_EXPR,
-                           gfc_get_complex_type (expr->ts.kind),
-                           gfc_build_string_const (expr->representation.length,
-                                                   expr->representation.string));
+       return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
+                        gfc_get_complex_type (expr->ts.kind),
+                        gfc_build_string_const (expr->representation.length,
+                                                expr->representation.string));
       else
        {
-         tree real = gfc_conv_mpfr_to_tree (expr->value.complex.r,
-                                         expr->ts.kind);
-         tree imag = gfc_conv_mpfr_to_tree (expr->value.complex.i,
-                                         expr->ts.kind);
+         tree real = gfc_conv_mpfr_to_tree (mpc_realref (expr->value.complex),
+                                         expr->ts.kind, expr->is_snan);
+         tree imag = gfc_conv_mpfr_to_tree (mpc_imagref (expr->value.complex),
+                                         expr->ts.kind, expr->is_snan);
 
          return build_complex (gfc_typenode_for_spec (&expr->ts),
                                real, imag);
@@ -330,20 +359,26 @@ void
 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
 {
   /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR.  If
-     so, they expr_type will not yet be an EXPR_CONSTANT.  We need to make
+     so, the expr_type will not yet be an EXPR_CONSTANT.  We need to make
      it so here.  */
-  if (expr->ts.type == BT_DERIVED && expr->ts.derived
-      && expr->ts.derived->attr.is_iso_c)
+  if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
+      && expr->ts.u.derived->attr.is_iso_c)
     {
       if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR 
           || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
         {
           /* Create a new EXPR_CONSTANT expression for our local uses.  */
-          expr = gfc_int_expr (0);
+          expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
         }
     }
 
-  gcc_assert (expr->expr_type == EXPR_CONSTANT);
+  if (expr->expr_type != EXPR_CONSTANT)
+    {
+      gfc_expr *e = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
+      gfc_error ("non-constant initialization expression at %L", &expr->where);
+      se->expr = gfc_conv_constant_to_tree (e);
+      return;
+    }
 
   if (se->ss != NULL)
     {