Bug 1499574 - Part 2: Assert that pn_arity is redundant. r=jwalden
authorJason Orendorff <jorendorff@mozilla.com>
Tue, 23 Oct 2018 20:32:05 +0000
changeset 490998 9d7dcb18cd7296847201c8cf8fcfa2674ebe2450
parent 490997 bc839874f8d76fdac6ebd4162c3d5f70316d85dc
child 490999 81f3d1e42395241f97ff12aa489ce7ee6153b5c5
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersjwalden
bugs1499574
milestone65.0a1
Bug 1499574 - Part 2: Assert that pn_arity is redundant. r=jwalden Depends on D8935 Differential Revision: https://phabricator.services.mozilla.com/D8936
js/src/frontend/ParseNode.cpp
js/src/frontend/ParseNode.h
--- a/js/src/frontend/ParseNode.cpp
+++ b/js/src/frontend/ParseNode.cpp
@@ -99,16 +99,22 @@ ParseNode::appendOrCreateList(ParseNodeK
     }
 
     list->append(right);
     return list;
 }
 
 #ifdef DEBUG
 
+const ParseNodeArity js::frontend::ParseNodeKindArity[] = {
+#define ARITY(_name, arity) arity,
+    FOR_EACH_PARSE_NODE_KIND(ARITY)
+#undef ARITY
+};
+
 static const char * const parseNodeNames[] = {
 #define STRINGIFY(name, _arity) #name,
     FOR_EACH_PARSE_NODE_KIND(STRINGIFY)
 #undef STRINGIFY
 };
 
 void
 frontend::DumpParseTree(ParseNode* pn, GenericPrinter& out, int indent)
--- a/js/src/frontend/ParseNode.h
+++ b/js/src/frontend/ParseNode.h
@@ -570,16 +570,21 @@ enum ParseNodeArity
     macro(UnaryNode, UnaryNodeType, asUnary) \
     macro(ThisLiteral, ThisLiteralType, asThisLiteral)
 
 #define DECLARE_CLASS(typeName, longTypeName, asMethodName) \
 class typeName;
 FOR_EACH_PARSENODE_SUBCLASS(DECLARE_CLASS)
 #undef DECLARE_CLASS
 
+#ifdef DEBUG
+// ParseNodeKindArity[size_t(pnk)] is the arity of a ParseNode of kind pnk.
+extern const ParseNodeArity ParseNodeKindArity[];
+#endif
+
 class ParseNode
 {
     ParseNodeKind pn_type;   /* ParseNodeKind::PNK_* type */
     // pn_op and pn_arity are not declared as the correct enum types
     // due to difficulties with MS bitfield layout rules and a GCC
     // bug.  See https://bugzilla.mozilla.org/show_bug.cgi?id=1383157#c4 for
     // details.
     uint8_t pn_op;      /* see JSOp enum and jsopcode.tbl */
@@ -598,49 +603,55 @@ class ParseNode
         pn_op(op),
         pn_arity(arity),
         pn_parens(false),
         pn_rhs_anon_fun(false),
         pn_pos(0, 0),
         pn_next(nullptr)
     {
         MOZ_ASSERT(kind < ParseNodeKind::Limit);
+        MOZ_ASSERT(hasExpectedArity());
         memset(&pn_u, 0, sizeof pn_u);
     }
 
     ParseNode(ParseNodeKind kind, JSOp op, ParseNodeArity arity, const TokenPos& pos)
       : pn_type(kind),
         pn_op(op),
         pn_arity(arity),
         pn_parens(false),
         pn_rhs_anon_fun(false),
         pn_pos(pos),
         pn_next(nullptr)
     {
         MOZ_ASSERT(kind < ParseNodeKind::Limit);
+        MOZ_ASSERT(hasExpectedArity());
         memset(&pn_u, 0, sizeof pn_u);
     }
 
     JSOp getOp() const                     { return JSOp(pn_op); }
     void setOp(JSOp op)                    { pn_op = op; }
     bool isOp(JSOp op) const               { return getOp() == op; }
 
     ParseNodeKind getKind() const {
         MOZ_ASSERT(pn_type < ParseNodeKind::Limit);
+        MOZ_ASSERT(hasExpectedArity());
         return pn_type;
     }
     void setKind(ParseNodeKind kind) {
         MOZ_ASSERT(kind < ParseNodeKind::Limit);
         pn_type = kind;
     }
     bool isKind(ParseNodeKind kind) const  { return getKind() == kind; }
 
     ParseNodeArity getArity() const        { return ParseNodeArity(pn_arity); }
+#ifdef DEBUG
+    bool hasExpectedArity() const          { return isArity(ParseNodeKindArity[size_t(pn_type)]); }
+#endif
     bool isArity(ParseNodeArity a) const   { return getArity() == a; }
-    void setArity(ParseNodeArity a)        { pn_arity = a; }
+    void setArity(ParseNodeArity a)        { pn_arity = a; MOZ_ASSERT(hasExpectedArity()); }
 
     bool isBinaryOperation() const {
         ParseNodeKind kind = getKind();
         return ParseNodeKind::BinOpFirst <= kind && kind <= ParseNodeKind::BinOpLast;
     }
     inline bool isName(PropertyName* name) const;
 
     /* Boolean attributes. */