From ffae18635f019d19a1f08fb47cdc1b78eedbc61c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Emma=20S=C3=B6derberg?= <emma.m.soderberg@gmail.com>
Date: Sat, 12 May 2018 22:52:46 +0200
Subject: [PATCH] Updates the incremental/param code generation.

- Adjusting to work with CNTAs.
- Removes use of init_children and children_computed.
- Some issues remaining but incremental/param tests now compile.

Tested successfully with jastadd-test and default tests that should pass.
---
 src/jastadd/incremental/IncrementalDDG.jadd   |  4 +++
 .../incremental/IncrementalNotification.jadd  |  9 +++---
 src/template/ast/CopyNode.tt                  |  2 --
 src/template/ast/NodeConstructor.tt           |  1 -
 src/template/incremental/DDGNode.tt           |  5 ++++
 src/template/incremental/Debug.tt             |  9 ------
 src/template/incremental/Notification.tt      | 28 -------------------
 src/template/incremental/Rewrites.tt          | 10 ++++---
 src/template/incremental/State.tt             | 12 --------
 src/template/incremental/Tracking.tt          |  1 +
 10 files changed, 21 insertions(+), 60 deletions(-)

diff --git a/src/jastadd/incremental/IncrementalDDG.jadd b/src/jastadd/incremental/IncrementalDDG.jadd
index 33f76578..1470b385 100644
--- a/src/jastadd/incremental/IncrementalDDG.jadd
+++ b/src/jastadd/incremental/IncrementalDDG.jadd
@@ -60,6 +60,10 @@ aspect IncrementalDDG {
       if (name().equals(config().astNodeType())) {
         tt.expand("ASTDecl.createASTHandlers", out);
       }
+      // Rewrite attribute.
+      if (hasRewriteAttribute()) {
+        tt.expand("ASTDecl.createRewriteAttributeHandler", out);
+      }
       // Collect attributes.
       ArrayList list = new ArrayList();
       for (AttrDecl attr : synDecls()) {
diff --git a/src/jastadd/incremental/IncrementalNotification.jadd b/src/jastadd/incremental/IncrementalNotification.jadd
index 33829769..7bbd28c4 100644
--- a/src/jastadd/incremental/IncrementalNotification.jadd
+++ b/src/jastadd/incremental/IncrementalNotification.jadd
@@ -32,10 +32,11 @@
 
     TemplateContext tt = templateContext();
 
-    tt.expand("ASTNode.incFlushRewritesLocateEnclosingRewriteMethod", out);
-    tt.expand("ASTNode.incResetRewritesMethod", out);
-    tt.expand("ASTNode.incRestoreEnclosingRewriteMethod", out);
-    tt.expand("ASTNode.incRestoreInitialForIndexMethod", out);
+//    tt.expand("ASTNode.incFlushRewritesLocateEnclosingRewriteMethod", out);
+//    tt.expand("ASTNode.incResetRewritesMethod", out);
+//    tt.expand("ASTNode.incRestoreEnclosingRewriteMethod", out);
+//    tt.expand("ASTNode.incRestoreInitialForIndexMethod", out);
+
     tt.expand("ASTNode.incLocateInitialCopyMethod", out);
 
     tt.bind("AttrAffectedChecks", emitAttrAffectedChecksString());
diff --git a/src/template/ast/CopyNode.tt b/src/template/ast/CopyNode.tt
index 27bd3392..10b631df 100644
--- a/src/template/ast/CopyNode.tt
+++ b/src/template/ast/CopyNode.tt
@@ -42,8 +42,6 @@ $if(IncrementalEnabled)
       for (int i = 0; node.children != null && i < node.children.length; i++) {
         node.children[i] = null;
       }
-      node.init_children = null;
-      node.children_computed = null;
       inc_copyHandlers(node);
 $endif
 $if(Concurrent)
diff --git a/src/template/ast/NodeConstructor.tt b/src/template/ast/NodeConstructor.tt
index 521d0021..ba43b91a 100644
--- a/src/template/ast/NodeConstructor.tt
+++ b/src/template/ast/NodeConstructor.tt
@@ -79,7 +79,6 @@ OptDecl.constructor = [[
 ListDecl.constructor = [[
   public $List.$List(T... initialChildren) {
     children = new $ASTNode[initialChildren.length];
-    #genIncrementalInitChildHandlers
     for (int i = 0; i < children.length; ++i) {
       addChild(initialChildren[i]);
     }
diff --git a/src/template/incremental/DDGNode.tt b/src/template/incremental/DDGNode.tt
index cf721c45..8a473f14 100644
--- a/src/template/incremental/DDGNode.tt
+++ b/src/template/incremental/DDGNode.tt
@@ -499,6 +499,11 @@ ASTDecl.createNodeHandler = [[
   protected $DDGNodeName #name.handler = new $DDGNodeName(this);
 ]]
 
+# Create DDG node for rewrite attribute.
+ASTDecl.createRewriteAttributeHandler = [[
+  protected $DDGNodeName #name.rewrittenNode_handler;
+]]
+
 # Create DDG node for attribute
 ASTDecl.createAttributeHandler = [[
 $if (IncrementalLevelParam)
diff --git a/src/template/incremental/Debug.tt b/src/template/incremental/Debug.tt
index ef8b0ad5..573f1082 100644
--- a/src/template/incremental/Debug.tt
+++ b/src/template/incremental/Debug.tt
@@ -117,15 +117,6 @@ $endif
 # Generate code for method dumping cached values
 ASTDecl.dumpCachedValuesMethod = [[
 public void #name.dumpCachedValues() {
-$if (#isASTNodeDecl)
-  // Rewritten children
-  for (int k = 0; children_computed != null && k < children_computed.length; k++) {
-    if (children_computed[k]) {
-      System.out.println(children[k].relativeNodeID() + " rewritten, initial=" +
-        (init_children[k] != null ? init_children[k].relativeNodeID() : null));
-    }
-  }
-$endif
   $DumpAttributeValues
   // TODO: dump collection values
 $if (!#isASTNodeDecl)
diff --git a/src/template/incremental/Notification.tt b/src/template/incremental/Notification.tt
index d8528d09..55a12673 100644
--- a/src/template/incremental/Notification.tt
+++ b/src/template/incremental/Notification.tt
@@ -181,34 +181,6 @@ $if(IncrementalChangeFlush)
 
 // flush children
 $if(IncrementalLevelParam)
-$if(#isASTNodeDecl)
-if (attrID.equals("getChild")) {
-  int i = (Integer)_parameters;
-  if (children_computed != null && i < children_computed.length && children_computed[i]) {
-    // Make sure this condition is false before calling notify.
-    // There may be a circular dependency resulting in a double flush otherwise.
-    // A double flush will cause null to be moved in as the initial child.
-    children_computed[i] = false;
-    children[i].inc_resetRewrites();
-    // outer rewrites
-    if (init_children[i] != null) {
-      // set up change
-      $ASTNode oldChild = children[i];
-      $ASTNode newChild = init_children[i];
-      // make change
-      oldChild.inc_flush_subtree(getChild_handler[i]);
-      children[i] = newChild;
-      init_children[i] = null;
-      // set parent and notify
-      newChild.setParent(this);
-      getChild_handler[i].notifyDependencies();
-    } else {
-      // inner rewrites
-      inc_restoreEnclosingRewrite();
-    }
-  }
-}
-$endif
 $endif
 
 $if(IncrementalLevelAttr)
diff --git a/src/template/incremental/Rewrites.tt b/src/template/incremental/Rewrites.tt
index d60370b2..24152c05 100644
--- a/src/template/incremental/Rewrites.tt
+++ b/src/template/incremental/Rewrites.tt
@@ -212,6 +212,8 @@ $if(IncrementalEnabled)
 $if(#isASTNodeDecl)
   /** @apilevel internal */
   protected $ASTNode $ASTNode.inc_locateInitialCopy() {
+    //TODO(emso): fix this method to work with CNTAs.
+    if (true) return null;
     // locate enclosing rewrite
     $ASTNode child = this;
     $ASTNode parent = getParent();
@@ -219,9 +221,9 @@ $if(#isASTNodeDecl)
     while (parent != null) {
       int index = child.childIndex;
       indexList.addFirst(Integer.valueOf(index));
-      if (parent.init_children != null && index >= 0 && index < parent.init_children.length && parent.init_children[index] != null) {
-        break;
-      }
+      //if (parent.init_children != null && index >= 0 && index < parent.init_children.length && parent.init_children[index] != null) {
+      //  break;
+      //}
       child = parent;
       parent = child.getParent();
     }
@@ -235,7 +237,7 @@ $if(#isASTNodeDecl)
       int index = ((Integer)itr.next()).intValue();
       if (first) {
         first = false;
-        child = parent.init_children[index];
+           //child = parent.init_children[index];
         parent = child;
       } else if (index < parent.getNumChildNoTransform()) {
         child = parent.getChildNoTransform(index);
diff --git a/src/template/incremental/State.tt b/src/template/incremental/State.tt
index aa359045..1cf8fce4 100644
--- a/src/template/incremental/State.tt
+++ b/src/template/incremental/State.tt
@@ -277,9 +277,6 @@ $if(#isASTNodeDecl)
     if (child != null) {
       child.inc_throwAway();
     }
-    if (init_children != null && i < init_children.length && init_children[i] != null) {
-      init_children[i].inc_throwAway();
-    }
   }
   $endif
   $if(IncrementalLevelAttr)
@@ -291,9 +288,6 @@ $if(#isASTNodeDecl)
     if (child != null) {
       child.inc_throwAway();
     }
-    if (init_children != null && i < init_children.length && init_children[i] != null) {
-      init_children[i].inc_throwAway();
-    }
   }
   $endif
   $if(IncrementalLevelNode)
@@ -303,9 +297,6 @@ $if(#isASTNodeDecl)
     if (child != null) {
       child.inc_throwAway();
     }
-    if (init_children != null && i < init_children.length && init_children[i] != null) {
-      init_children[i].inc_throwAway();
-    }
   }
   $endif
   $if(IncrementalLevelRegion)
@@ -317,9 +308,6 @@ $if(#isASTNodeDecl)
     if (child != null) {
       child.inc_throwAway();
     }
-    if (init_children != null && i < init_children.length && init_children[i] != null) {
-      init_children[i].inc_throwAway();
-    }
   }
   $endif
 $else
diff --git a/src/template/incremental/Tracking.tt b/src/template/incremental/Tracking.tt
index 9bd43b7c..a9446267 100644
--- a/src/template/incremental/Tracking.tt
+++ b/src/template/incremental/Tracking.tt
@@ -119,6 +119,7 @@ $endif
 $endif
 ]]
 
+
 ASTNode.incHookGetChild1 = [[
 $if(IncrementalEnabled)
 
-- 
GitLab