Bug 1596445 - Define nsINode::AsContent in nsINode.h. r=bzbarsky
authorEmilio Cobos Álvarez <emilio@crisal.io>
Fri, 15 Nov 2019 12:49:10 +0000
changeset 502181 a3cf1224284f9177fe2d8a86daab3fd40aa4578b
parent 502180 ce76d83dde22c957c75a8f5ab8161170fa762923
child 502182 52a8e787a8642a2c5ad6d8cb8ffee4770f742758
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1596445
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1596445 - Define nsINode::AsContent in nsINode.h. r=bzbarsky We were already relying on reinterpret_cast so it seems ok and will make the code in following patches a bit nicer. Differential Revision: https://phabricator.services.mozilla.com/D53028
dom/base/nsIContent.h
dom/base/nsINode.h
--- a/dom/base/nsIContent.h
+++ b/dom/base/nsIContent.h
@@ -64,16 +64,17 @@ class nsIContent : public nsINode {
 
 #ifdef MOZILLA_INTERNAL_API
   // If you're using the external API, the only thing you can know about
   // nsIContent is that it exists with an IID
 
   explicit nsIContent(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
       : nsINode(std::move(aNodeInfo)) {
     MOZ_ASSERT(mNodeInfo);
+    MOZ_ASSERT(static_cast<nsINode*>(this) == reinterpret_cast<nsINode*>(this));
     SetNodeIsContent();
   }
 #endif  // MOZILLA_INTERNAL_API
 
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICONTENT_IID)
 
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_CLASS(nsIContent)
@@ -867,18 +868,9 @@ class nsIContent : public nsINode {
 
   // accessibility.tabfocus_applies_to_xul pref - if it is set to true,
   // the tabfocus bit field applies to xul elements.
   static bool sTabFocusModelAppliesToXUL;
 };
 
 NS_DEFINE_STATIC_IID_ACCESSOR(nsIContent, NS_ICONTENT_IID)
 
-inline nsIContent* nsINode::AsContent() {
-  MOZ_ASSERT(IsContent());
-  return static_cast<nsIContent*>(this);
-}
-
-inline const nsIContent* nsINode::AsContent() const {
-  return const_cast<nsINode*>(this)->AsContent();
-}
-
 #endif /* nsIContent_h___ */
--- a/dom/base/nsINode.h
+++ b/dom/base/nsINode.h
@@ -498,20 +498,28 @@ class nsINode : public mozilla::dom::Eve
    * Return this node as an Element.  Should only be used for nodes
    * for which IsElement() is true.  This is defined inline in Element.h.
    */
   inline mozilla::dom::Element* AsElement();
   inline const mozilla::dom::Element* AsElement() const;
 
   /**
    * Return this node as nsIContent.  Should only be used for nodes for which
-   * IsContent() is true.  This is defined inline in nsIContent.h.
+   * IsContent() is true.
+   *
+   * The assertion in nsIContent's constructor makes this safe.
    */
-  inline nsIContent* AsContent();
-  inline const nsIContent* AsContent() const;
+  nsIContent* AsContent() {
+    MOZ_ASSERT(IsContent());
+    return reinterpret_cast<nsIContent*>(this);
+  }
+  const nsIContent* AsContent() const {
+    MOZ_ASSERT(IsContent());
+    return reinterpret_cast<const nsIContent*>(this);
+  }
 
   /*
    * Return whether the node is a Text node (which might be an actual
    * textnode, or might be a CDATA section).
    */
   bool IsText() const {
     uint32_t nodeType = NodeType();
     return nodeType == TEXT_NODE || nodeType == CDATA_SECTION_NODE;
@@ -899,17 +907,17 @@ class nsINode : public mozilla::dom::Eve
   nsIContentSecurityPolicy* GetCsp() const;
 
   /**
    * Get the parent nsIContent for this node.
    * @return the parent, or null if no parent or the parent is not an nsIContent
    */
   nsIContent* GetParent() const {
     return MOZ_LIKELY(GetBoolFlag(ParentIsContent))
-               ? reinterpret_cast<nsIContent*>(mParent)
+               ? mParent->AsContent()
                : nullptr;
   }
 
   /**
    * Get the parent nsINode for this node. This can be either an nsIContent, a
    * Document or an Attr.
    * @return the parent node
    */