Node::reverse*() cleanup

porten at froglogic.com porten at froglogic.com
Wed Oct 29 01:56:32 CET 2003


Hi,

I got around rewriting the Node list code to do the reversal during
construction. Patch attached. Add to that the one I posted last time. Then
there's the first change done several months ago. Should be trivial to
change the remaining occurrence yourself.

I run several tests but I can't exclude problems with e.g. lacking null
pointer checks. Review carefully. Linked list madness.

Harri.
-------------- next part --------------
? diff.txt
? reverse.diff
Index: ChangeLog
===================================================================
RCS file: /home/kde/kdelibs/kjs/ChangeLog,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -3 -p -r1.17 -r1.18
--- ChangeLog	27 Oct 2003 00:59:07 -0000	1.17
+++ ChangeLog	29 Oct 2003 00:46:40 -0000	1.18
@@ -1,3 +1,7 @@
+2003-10-29  Harri Porten  <porten at kde.org>
+
+	* nodes.*: got rid of remaining reverse*() functions
+
 2003-10-26  Harri Porten  <porten at kde.org>
 
 	* date_object.cpp (call): respect optional arguments in set*
Index: grammar.y
===================================================================
RCS file: /home/kde/kdelibs/kjs/grammar.y,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -3 -p -r1.53 -r1.54
--- grammar.y	26 Oct 2003 23:17:23 -0000	1.53
+++ grammar.y	29 Oct 2003 00:46:40 -0000	1.54
@@ -592,7 +592,7 @@ FunctionDeclaration:
 ;
 
 FunctionDeclarationInternal:
-    FUNCTION IDENT '(' ')' FunctionBody    { $$ = new FuncDeclNode(*$2, 0L, $5); DBG($$, at 1, at 4);
+    FUNCTION IDENT '(' ')' FunctionBody    { $$ = new FuncDeclNode(*$2, $5); DBG($$, at 1, at 4);
                                              delete $2; }
   | FUNCTION IDENT '(' FormalParameterList ')' FunctionBody
                                    { $$ = new FuncDeclNode(*$2, $4, $6); DBG($$, at 1, at 5);
Index: nodes.cpp
===================================================================
RCS file: /home/kde/kdelibs/kjs/nodes.cpp,v
retrieving revision 1.161
retrieving revision 1.163
diff -u -3 -p -r1.161 -r1.163
--- nodes.cpp	27 Oct 2003 22:16:31 -0000	1.161
+++ nodes.cpp	29 Oct 2003 00:46:40 -0000	1.163
@@ -459,18 +459,6 @@ Value ElementNode::evaluate(ExecState *e
 
 // ----------------------------- ArrayNode ------------------------------------
 
-void ArrayNode::reverseElementList()
-{
-  ElementNode *head = 0;
-  ElementNode *next;
-  for (ElementNode *n = element; n; n = next) {
-    next = n->list;
-    n->list = head;
-    head = n;
-  }
-  element = head;
-}
-
 void ArrayNode::ref()
 {
   Node::ref();
@@ -1623,13 +1611,16 @@ Value CommaNode::evaluate(ExecState *exe
 
 // ----------------------------- StatListNode ---------------------------------
 
-StatListNode::StatListNode(StatementNode *s) : statement(s), list(0L)
+StatListNode::StatListNode(StatementNode *s)
+  : statement(s), list(this)
 {
   setLoc(s->firstLine(),s->lastLine(),s->code());
 }
 
-StatListNode::StatListNode(StatListNode *l, StatementNode *s) : statement(s), list(l)
+StatListNode::StatListNode(StatListNode *l, StatementNode *s)
+  : statement(s), list(l->list)
 {
+  l->list = this;
   setLoc(l->firstLine(),s->lastLine(),l->code());
 }
 
@@ -1819,18 +1810,6 @@ void VarDeclListNode::processVarDecls(Ex
 
 // ----------------------------- VarStatementNode -----------------------------
 
-void VarStatementNode::reverseList()
-{
-  VarDeclListNode *head = 0;
-  VarDeclListNode *next;
-  for (VarDeclListNode *n = list; n; n = next) {
-    next = n->list;
-    n->list = head;
-    head = n;
-  }
-  list = head;
-}
-
 void VarStatementNode::ref()
 {
   StatementNode::ref();
@@ -1863,23 +1842,15 @@ void VarStatementNode::processVarDecls(E
 
 // ----------------------------- BlockNode ------------------------------------
 
-BlockNode::BlockNode(SourceElementsNode *s) : source(s)
+BlockNode::BlockNode(SourceElementsNode *s)
 {
-  if (s)
+  if (s) {
+    source = s->elements;
+    s->elements = 0;
     setLoc(s->firstLine(), s->lastLine(), s->code());
-  reverseList();
-}
-
-void BlockNode::reverseList()
-{
-  SourceElementsNode *head = 0;
-  SourceElementsNode *next;
-  for (SourceElementsNode *n = source; n; n = next) {
-    next = n->elements;
-    n->elements = head;
-    head = n;
+  } else {
+    source = 0;
   }
-  source = head;
 }
 
 void BlockNode::ref()
@@ -2114,18 +2085,6 @@ void WhileNode::processVarDecls(ExecStat
 
 // ----------------------------- ForNode --------------------------------------
 
-VarDeclListNode *ForNode::reverseList(VarDeclListNode *list)
-{
-  VarDeclListNode *head = 0;
-  VarDeclListNode *next;
-  for (VarDeclListNode *n = list; n; n = next) {
-    next = n->list;
-    n->list = head;
-    head = n;
-  }
-  return head;
-}
-
 void ForNode::ref()
 {
   StatementNode::ref();
@@ -2414,18 +2373,6 @@ void WithNode::processVarDecls(ExecState
 
 // ----------------------------- CaseClauseNode -------------------------------
 
-void CaseClauseNode::reverseList()
-{
-  StatListNode *head = 0;
-  StatListNode *next;
-  for (StatListNode *n = list; n; n = next) {
-    next = n->list;
-    n->list = head;
-    head = n;
-  }
-  list = head;
-}
-
 void CaseClauseNode::ref()
 {
   Node::ref();
@@ -2509,24 +2456,22 @@ void ClauseListNode::processVarDecls(Exe
 
 // ----------------------------- CaseBlockNode --------------------------------
 
-void CaseBlockNode::reverseLists()
+CaseBlockNode::CaseBlockNode(ClauseListNode *l1, CaseClauseNode *d,
+                             ClauseListNode *l2)
 {
-  ClauseListNode *head = 0;
-  ClauseListNode *next;
-  for (ClauseListNode *n = list1; n; n = next) {
-    next = n->nx;
-    n->nx = head;
-    head = n;
+  def = d;
+  if (l1) {
+    list1 = l1->nx;
+    l1->nx = 0;
+  } else {
+    list1 = 0;
   }
-  list1 = head;
-
-  head = 0;
-  for (ClauseListNode *n = list2; n; n = next) {
-    next = n->nx;
-    n->nx = head;
-    head = n;
+  if (l2) {
+    list2 = l2->nx;
+    l2->nx = 0;
+  } else {
+    list2 = 0;
   }
-  list2 = head;
 }
 
 void CaseBlockNode::ref()
@@ -2939,18 +2884,6 @@ Completion FunctionBodyNode::execute(Exe
 
 // ----------------------------- FuncDeclNode ---------------------------------
 
-void FuncDeclNode::reverseParameterList()
-{
-  ParameterNode *head = 0;
-  ParameterNode *next;
-  for (ParameterNode *n = param; n; n = next) {
-    next = n->next;
-    n->next = head;
-    head = n;
-  }
-  param = head;
-}
-
 void FuncDeclNode::ref()
 {
   StatementNode::ref();
@@ -3011,18 +2944,6 @@ void FuncDeclNode::processFuncDecl(ExecS
 
 // ----------------------------- FuncExprNode ---------------------------------
 
-void FuncExprNode::reverseParameterList()
-{
-  ParameterNode *head = 0;
-  ParameterNode *next;
-  for (ParameterNode *n = param; n; n = next) {
-    next = n->next;
-    n->next = head;
-    head = n;
-  }
-  param = head;
-}
-
 void FuncExprNode::ref()
 {
   Node::ref();
@@ -3062,13 +2983,14 @@ Value FuncExprNode::evaluate(ExecState *
 SourceElementsNode::SourceElementsNode(StatementNode *s1)
 {
   element = s1;
-  elements = 0L;
+  elements = this;
   setLoc(s1->firstLine(),s1->lastLine(),s1->code());
 }
 
 SourceElementsNode::SourceElementsNode(SourceElementsNode *s1, StatementNode *s2)
 {
-  elements = s1;
+  elements = s1->elements;
+  s1->elements = this;
   element = s2;
   setLoc(s1->firstLine(),s2->lastLine(),s1->code());
 }
Index: nodes.h
===================================================================
RCS file: /home/kde/kdelibs/kjs/nodes.h,v
retrieving revision 1.79
retrieving revision 1.83
diff -u -3 -p -r1.79 -r1.83
--- nodes.h	26 Oct 2003 23:17:24 -0000	1.79
+++ nodes.h	29 Oct 2003 00:50:45 -0000	1.83
@@ -241,9 +241,10 @@ namespace KJS {
 
   class ElementNode : public Node {
   public:
-    ElementNode(int e, Node *n) : list(0L), elision(e), node(n) { }
+    // list is circular during construction. cracked in ArrayNode ctor
+    ElementNode(int e, Node *n) : list(this), elision(e), node(n) { }
     ElementNode(ElementNode *l, int e, Node *n)
-      : list(l), elision(e), node(n) { }
+      : list(l->list), elision(e), node(n) { l->list = this; }
     virtual void ref();
     virtual bool deref();
     virtual Value evaluate(ExecState *exec) const;
@@ -259,15 +260,14 @@ namespace KJS {
   public:
     ArrayNode(int e) : element(0L), elision(e), opt(true) { }
     ArrayNode(ElementNode *ele)
-      : element(ele), elision(0), opt(false) { reverseElementList(); }
+      : element(ele->list), elision(0), opt(false) { ele->list = 0; }
     ArrayNode(int eli, ElementNode *ele)
-      : element(ele), elision(eli), opt(true) { reverseElementList(); }
+      : element(ele->list), elision(eli), opt(true) { ele->list = 0; }
     virtual void ref();
     virtual bool deref();
     virtual Value evaluate(ExecState *exec) const;
     virtual void streamTo(SourceStream &s) const;
   private:
-    void reverseElementList();
     ElementNode *element;
     int elision;
     bool opt;
@@ -342,6 +342,7 @@ namespace KJS {
 
   class ArgumentListNode : public Node {
   public:
+    // list is circular during construction. cracked in ArgumentsNode ctor
     ArgumentListNode(Node *e) : list(this), expr(e) {}
     ArgumentListNode(ArgumentListNode *l, Node *e)
       : list(l->list), expr(e) { l->list = this; }
@@ -366,7 +367,6 @@ namespace KJS {
     List evaluateList(ExecState *exec) const;
     virtual void streamTo(SourceStream &s) const;
   private:
-    void reverseList();
     ArgumentListNode *list;
   };
 
@@ -647,6 +647,7 @@ namespace KJS {
 
   class StatListNode : public StatementNode {
   public:
+    // list is circular during construction. cracked in CaseClauseNode ctor
     StatListNode(StatementNode *s);
     StatListNode(StatListNode *l, StatementNode *s);
     virtual void ref();
@@ -686,8 +687,10 @@ namespace KJS {
 
   class VarDeclListNode : public Node {
   public:
-    VarDeclListNode(VarDeclNode *v) : list(0L), var(v) {}
-    VarDeclListNode(VarDeclListNode *l, VarDeclNode *v) : list(l), var(v) {}
+    // list is circular until cracked in VarStatementNode/ForNode ctor
+    VarDeclListNode(VarDeclNode *v) : list(this), var(v) {}
+    VarDeclListNode(VarDeclListNode *l, VarDeclNode *v)
+      : list(l->list), var(v) { l->list = this; }
     virtual void ref();
     virtual bool deref();
     virtual Value evaluate(ExecState *exec) const;
@@ -702,14 +705,13 @@ namespace KJS {
 
   class VarStatementNode : public StatementNode {
   public:
-    VarStatementNode(VarDeclListNode *l) : list(l) { reverseList(); }
+    VarStatementNode(VarDeclListNode *l) : list(l->list) { l->list = 0; }
     virtual void ref();
     virtual bool deref();
     virtual Completion execute(ExecState *exec);
     virtual void processVarDecls(ExecState *exec);
     virtual void streamTo(SourceStream &s) const;
   private:
-    void reverseList();
     VarDeclListNode *list;
   };
 
@@ -722,7 +724,6 @@ namespace KJS {
     virtual void processVarDecls(ExecState *exec);
     virtual void streamTo(SourceStream &s) const;
   protected:
-    void reverseList();
     SourceElementsNode *source;
   };
 
@@ -789,14 +790,13 @@ namespace KJS {
     ForNode(Node *e1, Node *e2, Node *e3, StatementNode *s) :
       expr1(e1), expr2(e2), expr3(e3), statement(s) {}
     ForNode(VarDeclListNode *e1, Node *e2, Node *e3, StatementNode *s) :
-      expr1(reverseList(e1)), expr2(e2), expr3(e3), statement(s) {}
+      expr1(e1->list), expr2(e2), expr3(e3), statement(s) { e1->list = 0; }
     virtual void ref();
     virtual bool deref();
     virtual Completion execute(ExecState *exec);
     virtual void processVarDecls(ExecState *exec);
     virtual void streamTo(SourceStream &s) const;
   private:
-    static VarDeclListNode *reverseList(VarDeclListNode *);
     Node *expr1, *expr2, *expr3;
     StatementNode *statement;
   };
@@ -864,7 +864,8 @@ namespace KJS {
 
   class CaseClauseNode: public Node {
   public:
-    CaseClauseNode(Node *e, StatListNode *l) : expr(e), list(l) { reverseList(); }
+    CaseClauseNode(Node *e, StatListNode *l)
+      : expr(e) { if (l) { list = l; l->list = 0; } else { list = 0; } }
     virtual void ref();
     virtual bool deref();
     virtual Value evaluate(ExecState *exec) const;
@@ -872,15 +873,16 @@ namespace KJS {
     virtual void processVarDecls(ExecState *exec);
     virtual void streamTo(SourceStream &s) const;
   private:
-    void reverseList();
     Node *expr;
     StatListNode *list;
   };
 
   class ClauseListNode : public Node {
   public:
-    ClauseListNode(CaseClauseNode *c) : cl(c), nx(0L) { }
-    ClauseListNode(ClauseListNode *n, CaseClauseNode *c) : cl(c), nx(n) { }
+    // list is circular during construction. cracked in CaseBlockNode ctor
+    ClauseListNode(CaseClauseNode *c) : cl(c), nx(this) { }
+    ClauseListNode(ClauseListNode *n, CaseClauseNode *c)
+      : cl(c), nx(n->nx) { n->nx = this; }
     virtual void ref();
     virtual bool deref();
     virtual Value evaluate(ExecState *exec) const;
@@ -896,8 +898,7 @@ namespace KJS {
 
   class CaseBlockNode: public Node {
   public:
-    CaseBlockNode(ClauseListNode *l1, CaseClauseNode *d, ClauseListNode *l2)
-      : list1(l1), def(d), list2(l2) { reverseLists(); }
+    CaseBlockNode(ClauseListNode *l1, CaseClauseNode *d, ClauseListNode *l2);
     virtual void ref();
     virtual bool deref();
     virtual Value evaluate(ExecState *exec) const;
@@ -905,7 +906,6 @@ namespace KJS {
     virtual void processVarDecls(ExecState *exec);
     virtual void streamTo(SourceStream &s) const;
   private:
-    void reverseLists();
     ClauseListNode *list1;
     CaseClauseNode *def;
     ClauseListNode *list2;
@@ -991,8 +991,10 @@ namespace KJS {
 
   class ParameterNode : public Node {
   public:
-    ParameterNode(const Identifier &i) : id(i), next(0L) { }
-    ParameterNode(ParameterNode *list, const Identifier &i) : id(i), next(list) { }
+    // list is circular during construction. cracked in FuncDecl/ExprNode ctor.
+    ParameterNode(const Identifier &i) : id(i), next(this) { }
+    ParameterNode(ParameterNode *list, const Identifier &i)
+      : id(i), next(list->next) { list->next = this; }
     virtual void ref();
     virtual bool deref();
     virtual Value evaluate(ExecState *exec) const;
@@ -1019,8 +1021,10 @@ namespace KJS {
 
   class FuncDeclNode : public StatementNode {
   public:
+    FuncDeclNode(const Identifier &i, FunctionBodyNode *b)
+      : ident(i), param(0), body(b) { }
     FuncDeclNode(const Identifier &i, ParameterNode *p, FunctionBodyNode *b)
-      : ident(i), param(p), body(b) { reverseParameterList(); }
+      : ident(i), param(p->next), body(b) { p->next = 0; }
     virtual void ref();
     virtual bool deref();
     Completion execute(ExecState* /*exec*/)
@@ -1028,7 +1032,6 @@ namespace KJS {
     void processFuncDecl(ExecState *exec);
     virtual void streamTo(SourceStream &s) const;
   private:
-    void reverseParameterList();
     Identifier ident;
     ParameterNode *param;
     FunctionBodyNode *body;
@@ -1037,13 +1040,12 @@ namespace KJS {
   class FuncExprNode : public Node {
   public:
     FuncExprNode(ParameterNode *p, FunctionBodyNode *b)
-	: param(p), body(b) { reverseParameterList(); }
+	: param(p->next), body(b) { p->next = 0; }
     virtual void ref();
     virtual bool deref();
     virtual Value evaluate(ExecState *exec) const;
     virtual void streamTo(SourceStream &s) const;
   private:
-    void reverseParameterList();
     ParameterNode *param;
     FunctionBodyNode *body;
   };
@@ -1051,6 +1053,7 @@ namespace KJS {
   // A linked list of source element nodes
   class SourceElementsNode : public StatementNode {
   public:
+    // list is circular until cracked in BlockNode/FunctionBodyNode ctor
     SourceElementsNode(StatementNode *s1);
     SourceElementsNode(SourceElementsNode *s1, StatementNode *s2);
     virtual void ref();


More information about the Khtml-devel mailing list