Use backend interface for basic types
authorEvan Shaw <edsrzf@gmail.com>
Mon, 25 Apr 2011 16:39:24 +0000 (16:39 +0000)
committerIan Lance Taylor <ian@gcc.gnu.org>
Mon, 25 Apr 2011 16:39:24 +0000 (16:39 +0000)
* go-gcc.c (class Gcc_tree): Make get_tree const.
(Gcc_backend::void_type): Implement.
(Gcc_backend::bool_type): Implement.
(Gcc_backend::integer_type): Implement.
(Gcc_backend::float_type): Implement.
(Gcc_backend::complex_type): New function.
(Gcc_backend::pointer_type): New function.
(Gcc_backend::make_type): New function.
(type_to_tree): New function.

From-SVN: r172931

gcc/go/ChangeLog
gcc/go/go-gcc.cc
gcc/go/gofrontend/backend.h
gcc/go/gofrontend/types.cc

index aa713dbb0a8be5dbb3016cfa5dd6ab1db16bad06..ad7ec733fe8ba4b34a237ef7c5efff60ff0d98c8 100644 (file)
@@ -1,3 +1,15 @@
+2011-04-25  Evan Shaw  <edsrzf@gmail.com>
+
+       * go-gcc.c (class Gcc_tree): Make get_tree const.
+       (Gcc_backend::void_type): Implement.
+       (Gcc_backend::bool_type): Implement.
+       (Gcc_backend::integer_type): Implement.
+       (Gcc_backend::float_type): Implement.
+       (Gcc_backend::complex_type): New function.
+       (Gcc_backend::pointer_type): New function.
+       (Gcc_backend::make_type): New function.
+       (type_to_tree): New function.
+
 2011-04-21  Ian Lance Taylor  <iant@google.com>
 
        * go-system.h (go_assert, go_unreachable): Define.
index 7b7923d06f95fc5f7656363d6a91050809d0e91b..96375220638272c9007063ccf869d3ec525725a6 100644 (file)
@@ -52,7 +52,7 @@ class Gcc_tree
   { }
 
   tree
-  get_tree()
+  get_tree() const
   { return this->t_; }
 
  private:
@@ -133,24 +133,28 @@ class Gcc_backend : public Backend
 
   Btype*
   void_type()
-  { gcc_unreachable(); }
+  { return this->make_type(void_type_node); }
 
   Btype*
   bool_type()
-  { gcc_unreachable(); }
+  { return this->make_type(boolean_type_node); }
 
   Btype*
-  integer_type(bool /* is_unsigned */, int /* bits */)
-  { gcc_unreachable(); }
+  integer_type(bool, int);
 
   Btype*
-  float_type(int /* bits */)
-  { gcc_unreachable(); }
+  float_type(int);
+
+  Btype*
+  complex_type(int);
 
   Btype*
   string_type()
   { gcc_unreachable(); }
 
+  Btype*
+  pointer_type(const Btype*);
+
   Btype*
   function_type(const Function_type*, Btype* /* receiver */,
                const Btypes* /* parameters */,
@@ -283,6 +287,11 @@ class Gcc_backend : public Backend
   Bstatement*
   make_statement(tree t)
   { return new Bstatement(t); }
+
+  // Make a Btype from a tree.
+  Btype*
+  make_type(tree t)
+  { return new Btype(t); }
 };
 
 // A helper function.
@@ -293,6 +302,97 @@ get_identifier_from_string(const std::string& str)
   return get_identifier_with_length(str.data(), str.length());
 }
 
+// Get an unnamed integer type.
+
+Btype*
+Gcc_backend::integer_type(bool is_unsigned, int bits)
+{
+  tree type;
+  if (is_unsigned)
+    {
+      if (bits == INT_TYPE_SIZE)
+        type = unsigned_type_node;
+      else if (bits == CHAR_TYPE_SIZE)
+        type = unsigned_char_type_node;
+      else if (bits == SHORT_TYPE_SIZE)
+        type = short_unsigned_type_node;
+      else if (bits == LONG_TYPE_SIZE)
+        type = long_unsigned_type_node;
+      else if (bits == LONG_LONG_TYPE_SIZE)
+        type = long_long_unsigned_type_node;
+      else
+        type = make_unsigned_type(bits);
+    }
+  else
+    {
+      if (bits == INT_TYPE_SIZE)
+        type = integer_type_node;
+      else if (bits == CHAR_TYPE_SIZE)
+        type = signed_char_type_node;
+      else if (bits == SHORT_TYPE_SIZE)
+        type = short_integer_type_node;
+      else if (bits == LONG_TYPE_SIZE)
+        type = long_integer_type_node;
+      else if (bits == LONG_LONG_TYPE_SIZE)
+        type = long_long_integer_type_node;
+      else
+        type = make_signed_type(bits);
+    }
+  return this->make_type(type);
+}
+
+// Get an unnamed float type.
+
+Btype*
+Gcc_backend::float_type(int bits)
+{
+  tree type;
+  if (bits == FLOAT_TYPE_SIZE)
+    type = float_type_node;
+  else if (bits == DOUBLE_TYPE_SIZE)
+    type = double_type_node;
+  else if (bits == LONG_DOUBLE_TYPE_SIZE)
+    type = long_double_type_node;
+  else
+    {
+      type = make_node(REAL_TYPE);
+      TYPE_PRECISION(type) = bits;
+      layout_type(type);
+    }
+  return this->make_type(type);
+}
+
+// Get an unnamed complex type.
+
+Btype*
+Gcc_backend::complex_type(int bits)
+{
+  tree type;
+  if (bits == FLOAT_TYPE_SIZE * 2)
+    type = complex_float_type_node;
+  else if (bits == DOUBLE_TYPE_SIZE * 2)
+    type = complex_double_type_node;
+  else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
+    type = complex_long_double_type_node;
+  else
+    {
+      type = make_node(REAL_TYPE);
+      TYPE_PRECISION(type) = bits / 2;
+      layout_type(type);
+      type = build_complex_type(type);
+    }
+  return this->make_type(type);
+}
+
+// Get a pointer type.
+
+Btype*
+Gcc_backend::pointer_type(const Btype* to_type)
+{
+  tree type = build_pointer_type(to_type->get_tree());
+  return this->make_type(type);
+}
+
 // An expression as a statement.
 
 Bstatement*
@@ -866,6 +966,12 @@ tree_to_block(tree t)
   return new Bblock(t);
 }
 
+tree
+type_to_tree(Btype* bt)
+{
+  return bt->get_tree();
+}
+
 tree
 expr_to_tree(Bexpression* be)
 {
index 4377332ed09807fa25e9dbcabdafcb1f9b6a458e..2d7300c72242ee8a10d73b2a351c5ca3b4f062fe 100644 (file)
@@ -73,10 +73,18 @@ class Backend
   virtual Btype*
   float_type(int bits) = 0;
 
+  // Get an unnamed complex type with the given number of bits.
+  virtual Btype*
+  complex_type(int bits) = 0;
+
   // Get the unnamed string type.
   virtual Btype*
   string_type() = 0;
 
+  // Get a pointer type.
+  virtual Btype*
+  pointer_type(const Btype* to_type) = 0;
+
   // Get a function type.  The receiver, parameter, and results are
   // generated from the types in the Function_type.  The Function_type
   // is provided so that the names are available.
@@ -299,6 +307,7 @@ extern Bexpression* tree_to_expr(tree);
 extern Bstatement* tree_to_stat(tree);
 extern Bfunction* tree_to_function(tree);
 extern Bblock* tree_to_block(tree);
+extern tree type_to_tree(Btype*);
 extern tree expr_to_tree(Bexpression*);
 extern tree stat_to_tree(Bstatement*);
 extern tree block_to_tree(Bblock*);
index 86d65c1a0480f72a5e4fee788d3df2d475b2dfb3..f8d705b374a223ccf5819f80ddbafa1eb976ebf4 100644 (file)
@@ -31,6 +31,7 @@ extern "C"
 #include "statements.h"
 #include "export.h"
 #include "import.h"
+#include "backend.h"
 #include "types.h"
 
 // Class Type.
@@ -1622,8 +1623,11 @@ class Void_type : public Type
 
  protected:
   tree
-  do_get_tree(Gogo*)
-  { return void_type_node; }
+  do_get_tree(Gogo* gogo)
+  {
+    Btype* btype = gogo->backend()->void_type();
+    return type_to_tree(btype);
+  }
 
   tree
   do_get_init_tree(Gogo*, tree, bool)
@@ -1660,8 +1664,11 @@ class Boolean_type : public Type
 
  protected:
   tree
-  do_get_tree(Gogo*)
-  { return boolean_type_node; }
+  do_get_tree(Gogo* gogo)
+  {
+    Btype* btype = gogo->backend()->bool_type();
+    return type_to_tree(btype);
+  }
 
   tree
   do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
@@ -1806,36 +1813,10 @@ Integer_type::do_get_tree(Gogo*)
       return error_mark_node;
     }
 
-  if (this->is_unsigned_)
-    {
-      if (this->bits_ == INT_TYPE_SIZE)
-       return unsigned_type_node;
-      else if (this->bits_ == CHAR_TYPE_SIZE)
-       return unsigned_char_type_node;
-      else if (this->bits_ == SHORT_TYPE_SIZE)
-       return short_unsigned_type_node;
-      else if (this->bits_ == LONG_TYPE_SIZE)
-       return long_unsigned_type_node;
-      else if (this->bits_ == LONG_LONG_TYPE_SIZE)
-       return long_long_unsigned_type_node;
-      else
-       return make_unsigned_type(this->bits_);
-    }
-  else
-    {
-      if (this->bits_ == INT_TYPE_SIZE)
-       return integer_type_node;
-      else if (this->bits_ == CHAR_TYPE_SIZE)
-       return signed_char_type_node;
-      else if (this->bits_ == SHORT_TYPE_SIZE)
-       return short_integer_type_node;
-      else if (this->bits_ == LONG_TYPE_SIZE)
-       return long_integer_type_node;
-      else if (this->bits_ == LONG_LONG_TYPE_SIZE)
-       return long_long_integer_type_node;
-      else
-       return make_signed_type(this->bits_);
-    }
+  // FIXME: GOGO can be NULL when called from go_type_for_size, so call
+  // go_get_backend() instead of gogo->backend().
+  Btype* btype = go_get_backend()->integer_type(this->is_unsigned_, this->bits_);
+  return type_to_tree(btype);
 }
 
 tree
@@ -1968,19 +1949,8 @@ Float_type::do_hash_for_method(Gogo*) const
 tree
 Float_type::type_tree() const
 {
-  if (this->bits_ == FLOAT_TYPE_SIZE)
-    return float_type_node;
-  else if (this->bits_ == DOUBLE_TYPE_SIZE)
-    return double_type_node;
-  else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE)
-    return long_double_type_node;
-  else
-    {
-      tree ret = make_node(REAL_TYPE);
-      TYPE_PRECISION(ret) = this->bits_;
-      layout_type(ret);
-      return ret;
-    }
+  Btype* btype = go_get_backend()->float_type(this->bits_);
+  return type_to_tree(btype);
 }
 
 // Get a tree.
@@ -2124,19 +2094,8 @@ Complex_type::do_hash_for_method(Gogo*) const
 tree
 Complex_type::type_tree() const
 {
-  if (this->bits_ == FLOAT_TYPE_SIZE * 2)
-    return complex_float_type_node;
-  else if (this->bits_ == DOUBLE_TYPE_SIZE * 2)
-    return complex_double_type_node;
-  else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE * 2)
-    return complex_long_double_type_node;
-  else
-    {
-      tree ret = make_node(REAL_TYPE);
-      TYPE_PRECISION(ret) = this->bits_ / 2;
-      layout_type(ret);
-      return build_complex_type(ret);
-    }
+  Btype* btype = go_get_backend()->complex_type(this->bits_);
+  return type_to_tree(btype);
 }
 
 // Get a tree.
@@ -3136,7 +3095,9 @@ Pointer_type::do_hash_for_method(Gogo* gogo) const
 tree
 Pointer_type::do_get_tree(Gogo* gogo)
 {
-  return build_pointer_type(this->to_type_->get_tree(gogo));
+  Btype* to_btype = tree_to_type(this->to_type_->get_tree(gogo));
+  Btype* btype = gogo->backend()->pointer_type(to_btype);
+  return type_to_tree(btype);
 }
 
 // Initialize a pointer type.