This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] rvalue reference implementation for C++0x


On 4/25/07, Russell Yanofsky <russ@yanofsky.org> wrote:
This is an update of a patch first posted two months ago:

http://gcc.gnu.org/ml/gcc-patches/2007-02/msg01760.html

There was some discussion about it which ended here:

http://gcc.gnu.org/ml/gcc-patches/2007-03/msg01283.html

The updated patch implements rvalue references entirely in the C++ front
end instead of modifying middle end code.

Thanks, Russell!


+/* Return a reference type node referring to TO_TYPE.  If RVAL is
+   true, return an rvalue reference type, otherwise return an lvalue
+   reference type.  If a type node exists, reuse it, otherwise create
+   a new one.  */
+tree
+cp_build_reference_type (tree to_type, bool rval)
+{
+  tree lvalue_ref, t;
+  lvalue_ref = build_reference_type (to_type);
+  if (!rval)
+    return lvalue_ref;
+
+  /* This code to create rvalue reference types is based on and tied
+     to the code creating lvalue reference types in the middle-end
+     functions build_reference_type_for_mode and build_reference_type.
+
+     It works by putting the rvalue reference type nodes after the
+     lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
+     they will effectively be ignored by the middle end.  */
+
+  for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
+    if (TYPE_REF_IS_RVALUE (t))
+      return t;
+
+  t = make_node (REFERENCE_TYPE);
+
+  TREE_TYPE (t) = to_type;
+  TYPE_MODE (t) = ptr_mode;
+  TYPE_REF_CAN_ALIAS_ALL (t) = false;
+  TYPE_REF_IS_RVALUE (t) = true;
+  TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
+  TYPE_NEXT_REF_TO (lvalue_ref) = t;
+
+  if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
+    SET_TYPE_STRUCTURAL_EQUALITY (t);
+  else if (TYPE_CANONICAL (to_type) != to_type)
+    TYPE_CANONICAL (t)
+      = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
+
+  layout_type (t);
+
+  return t;
+
+}

Yes, this is definitely the right approach to the rvalue reference type... and you didn't have to touch the middle-end at all. Great!

Russell, when I applied this patch to the cxx0x-branch, I ran into
three failing test cases that I'd like you to take a look at. They
behave differently under -std=c++0x than under -std=c++98, and I'm not
sure if they're exploiting C++98-specific behavior or whether they are
exposing a bug in the rvalue references implementation. The test cases
are:

 g++.dg/init/copy7.C
 g++.dg/overload/arg1.C
 g++.dg/overload/arg4.C

Would you take a look at these?

- Doug


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]