Bug 1296186 part 1 - Make borrowed type just alias of corresponding pointer type. r?bholley draft
authorXidorn Quan <xidorn+moz@upsuper.org>
Fri, 19 Aug 2016 16:07:05 +1000
changeset 403741 883334b9697464dac74f7ac9265759c16ef6c4b0
parent 403740 0351a55fff75591b50627590d47dbf9bd258d0b7
child 403742 fcfd577eaa5bba328c0d3b62fd84ec0c36cb6848
push id27001
push userxquan@mozilla.com
push dateMon, 22 Aug 2016 05:23:16 +0000
reviewersbholley
bugs1296186
milestone51.0a1
Bug 1296186 part 1 - Make borrowed type just alias of corresponding pointer type. r?bholley Compilers would complain about using user-defined type as return type of extern "C" functions. A struct is considered a user-defined type if there is any non-trivial constructor. Having a type without non-trivial constructor for borrowed type would be very inconvenient. Actually the currently borrowed type doesn't seem to provide any additional safety / benefit over a plain pointer. MozReview-Commit-ID: ncxmCEWCkv
layout/style/ServoBindings.cpp
layout/style/ServoBindings.h
--- a/layout/style/ServoBindings.cpp
+++ b/layout/style/ServoBindings.cpp
@@ -21,26 +21,28 @@
 #include "nsStyleStruct.h"
 #include "nsStyleUtil.h"
 #include "nsTArray.h"
 
 #include "mozilla/EventStates.h"
 #include "mozilla/ServoElementSnapshot.h"
 #include "mozilla/dom/Element.h"
 
-#define IMPL_STRONG_REF_TYPE(name, T)           \
-  already_AddRefed<T> name::Consume() {         \
-    RefPtr<T> result = dont_AddRef(mPtr);       \
-    mPtr = nullptr;                             \
-    return result.forget();                     \
-  };
+#define IMPL_STRONG_REF_TYPE_FOR(type_) \
+  already_AddRefed<type_>               \
+  type_##Strong::Consume() {            \
+    RefPtr<type_> result;               \
+    result.swap(mPtr);                  \
+    return result.forget();             \
+  }
 
+IMPL_STRONG_REF_TYPE_FOR(ServoComputedValues)
+IMPL_STRONG_REF_TYPE_FOR(RawServoStyleSheet)
 
-IMPL_STRONG_REF_TYPE(ServoComputedValuesStrong, ServoComputedValues);
-IMPL_STRONG_REF_TYPE(RawServoStyleSheetStrong, RawServoStyleSheet);
+#undef IMPL_STRONG_REF_TYPE_FOR
 
 uint32_t
 Gecko_ChildrenCount(RawGeckoNode* aNode)
 {
   return aNode->GetChildCount();
 }
 
 bool
--- a/layout/style/ServoBindings.h
+++ b/layout/style/ServoBindings.h
@@ -11,34 +11,16 @@
 #include "mozilla/css/SheetParsingMode.h"
 #include "nsChangeHint.h"
 #include "nsColor.h"
 #include "nsProxyRelease.h"
 #include "nsStyleCoord.h"
 #include "nsStyleStruct.h"
 #include "stdint.h"
 
-#define DECL_STRONG_REF_TYPE(name, T)                   \
-  struct MOZ_MUST_USE_TYPE name {                         \
-    T* mPtr;                                              \
-    already_AddRefed<T> Consume();                        \
-  }
-
-#define DECL_BORROWED_REF_TYPE(name, T)                 \
-  struct name {                                           \
-    T* mPtr;                                              \
-    MOZ_IMPLICIT                                          \
-    name(T* x): mPtr(x) {};                               \
-    MOZ_IMPLICIT                                          \
-    name(const RefPtr<T>& aPtr) : mPtr(aPtr.get()) {};    \
-    operator T*() const & {                               \
-      return mPtr;                                        \
-    }                                                     \
-  }
-
 /*
  * API for Servo to access Gecko data structures. This file must compile as valid
  * C code in order for the binding generator to parse it.
  *
  * Functions beginning with Gecko_ are implemented in Gecko and invoked from Servo.
  * Functions beginning with Servo_ are implemented in Servo and invoked from Gecko.
  */
 
@@ -57,31 +39,40 @@ using mozilla::FontFamilyList;
 using mozilla::FontFamilyType;
 using mozilla::dom::Element;
 using mozilla::ServoElementSnapshot;
 typedef mozilla::dom::Element RawGeckoElement;
 class nsIDocument;
 typedef nsIDocument RawGeckoDocument;
 struct ServoNodeData;
 struct ServoComputedValues;
-DECL_STRONG_REF_TYPE(ServoComputedValuesStrong, ServoComputedValues);
-DECL_BORROWED_REF_TYPE(ServoComputedValuesBorrowed, ServoComputedValues);
 struct RawServoStyleSheet;
-DECL_STRONG_REF_TYPE(RawServoStyleSheetStrong, RawServoStyleSheet);
-DECL_BORROWED_REF_TYPE(RawServoStyleSheetBorrowed, RawServoStyleSheet);
 struct RawServoStyleSet;
 class nsHTMLCSSStyleSheet;
 struct nsStyleList;
 struct nsStyleImage;
 struct nsStyleGradientStop;
 class nsStyleGradient;
 class nsStyleCoord;
 struct nsStyleDisplay;
 struct ServoDeclarationBlock;
 
+#define DECL_REF_TYPE_FOR(type_)          \
+  typedef type_* type_##Borrowed;         \
+  struct MOZ_MUST_USE_TYPE type_##Strong  \
+  {                                       \
+    type_* mPtr;                          \
+    already_AddRefed<type_> Consume();    \
+  };
+
+DECL_REF_TYPE_FOR(ServoComputedValues)
+DECL_REF_TYPE_FOR(RawServoStyleSheet)
+
+#undef DECL_REF_TYPE_FOR
+
 #define NS_DECL_THREADSAFE_FFI_REFCOUNTING(class_, name_)                     \
   void Gecko_AddRef##name_##ArbitraryThread(class_* aPtr);                    \
   void Gecko_Release##name_##ArbitraryThread(class_* aPtr);
 #define NS_IMPL_THREADSAFE_FFI_REFCOUNTING(class_, name_)                     \
   static_assert(class_::HasThreadSafeRefCnt::value,                           \
                 "NS_DECL_THREADSAFE_FFI_REFCOUNTING can only be used with "   \
                 "classes that have thread-safe refcounting");                 \
   void Gecko_AddRef##name_##ArbitraryThread(class_* aPtr)                     \