From 70a3951d05a543b1be1c75bc6dbeab64c9a683bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Emma=20S=C3=B6derberg?= <emma.m.soderberg@gmail.com>
Date: Fri, 29 May 2020 10:01:35 +0200
Subject: [PATCH] Update incremental handler generation to include handlers for
 equations overloading syn lazy attributes. Tested in
 incremental/param/super_01.

---
 src/jastadd/ast/JragCodeGen.jrag            |  1 +
 src/jastadd/incremental/IncrementalDDG.jadd | 70 ++++++++++++---------
 src/template/incremental/DDGNode.tt         | 15 +++++
 src/template/incremental/DDGNodeCopy.tt     | 20 ++++++
 4 files changed, 77 insertions(+), 29 deletions(-)

diff --git a/src/jastadd/ast/JragCodeGen.jrag b/src/jastadd/ast/JragCodeGen.jrag
index 1a64f31a..8fb102e3 100644
--- a/src/jastadd/ast/JragCodeGen.jrag
+++ b/src/jastadd/ast/JragCodeGen.jrag
@@ -661,6 +661,7 @@ aspect JragCodeGen {
   }
 
   syn boolean AttrDecl.isParameterized() = getNumParameter() != 0;
+  syn boolean SynEq.isParameterized() = decl().isParameterized();
 
   /**
    * Generate code to test if two attribute values differ based on the type of the attribute.
diff --git a/src/jastadd/incremental/IncrementalDDG.jadd b/src/jastadd/incremental/IncrementalDDG.jadd
index 432b1ec2..6146df8c 100644
--- a/src/jastadd/incremental/IncrementalDDG.jadd
+++ b/src/jastadd/incremental/IncrementalDDG.jadd
@@ -82,26 +82,46 @@ aspect IncrementalDDG {
       if (hasRewriteAttribute()) {
         tt.expand("ASTDecl.createRewriteAttributeHandler", out);
       }
-      // Collect attributes.
-      ArrayList<AttrDecl> decls = new ArrayList<AttrDecl>();
-      for (AttrDecl attr : synDecls()) {
-        if (attr.isMemoized()) {
-          decls.add(attr);
-        }
+      // Attribute declaration handlers.
+      for (AttrDecl attr : gatherIncrementalHandlersForAttrDecls()) {
+        attr.templateContext().expand("AttrDecl.createAttributeHandler", out);
       }
-      for (AttrDecl attr : getInhDeclList()) {
-        if (attr.isMemoized()) {
-          decls.add(attr);
-        }
+      // Attribute equation handlers.
+     for (SynEq eq : gatherIncrementalHandlersForSynEqs()) {
+        eq.templateContext().expand("SynEq.createEquationHandler", out);
       }
-      for (CollDecl decl : collDecls()) {
-        decls.add(decl);
+   }
+  }
+
+  public ArrayList<AttrDecl> ASTDecl.gatherIncrementalHandlersForAttrDecls() {
+    // Collect lazy attribute declarations.
+    ArrayList<AttrDecl> decls = new ArrayList<AttrDecl>();
+    for (AttrDecl attr : synDecls()) {
+      if (attr.isMemoized()) {
+        decls.add(attr);
       }
-      // Attribute code.
-      for (AttrDecl attr : decls) {
-        attr.templateContext().expand("AttrDecl.createAttributeHandler", out);
+    }
+    for (AttrDecl attr : getInhDeclList()) {
+      if (attr.isMemoized()) {
+        decls.add(attr);
       }
     }
+    for (CollDecl decl : collDecls()) {
+      decls.add(decl);
+    }
+    return decls;
+  }
+
+  public ArrayList<SynEq> ASTDecl.gatherIncrementalHandlersForSynEqs() {
+    // Collect eq for lazy syn attributes where eq is defined on a subtype.
+    ArrayList<SynEq> eqs = new ArrayList<SynEq>();
+    for (SynEq eq : getSynEqList()) {
+      AttrDecl decl = eq.decl();
+      if (decl.isMemoized() && !decl.hostClass().equals(eq.hostClass())) {
+        eqs.add(eq);
+      }
+    }
+    return eqs;
   }
 
   /**
@@ -182,22 +202,14 @@ aspect IncrementalDDG {
   public String ASTDecl.emitCopyAttributeHandlersString() {
     StringBuilder res = new StringBuilder();
     if (config().incrementalLevelParam() || config().incrementalLevelAttr()) {
-      // Collect attributes.
-      ArrayList<AttrDecl> decls = new ArrayList<AttrDecl>();
-      for (AttrDecl attr : synDecls()) {
-        if (attr != null && attr.isMemoized()) {
-          decls.add(attr);
-        }
-      }
-      for (AttrDecl attr : getInhDeclList()) {
-        if (attr != null && attr.isMemoized()) {
-          decls.add(attr);
-        }
-      }
-      // Attribute code.
-      for (AttrDecl attr : decls) {
+      // Copy attribute declaration handlers.
+      for (AttrDecl attr : gatherIncrementalHandlersForAttrDecls()) {
         res.append(attr.templateContext().expand("AttrDecl.copyAttributeHandler"));
       }
+      // Copy syn attribute equation handlers.
+      for (SynEq eq : gatherIncrementalHandlersForSynEqs()) {
+        res.append(eq.templateContext().expand("SynEq.copyAttributeHandler"));
+      }
     }
     return res.toString();
   }
diff --git a/src/template/incremental/DDGNode.tt b/src/template/incremental/DDGNode.tt
index 5c634fa8..2c082858 100644
--- a/src/template/incremental/DDGNode.tt
+++ b/src/template/incremental/DDGNode.tt
@@ -477,6 +477,21 @@ $if (IncrementalLevelAttr)
 $endif
 ]]
 
+# Create DDG node for lazy attribute equations.
+SynEq.createEquationHandler = [[
+$if (IncrementalLevelParam)
+$if (#isParameterized)
+  protected java.util.Map #hostClass.#(signature)_handler = new java.util.HashMap(4);
+$else
+  protected $DDGNodeName #hostClass.#(signature)_handler;
+$endif
+$endif
+$if (IncrementalLevelAttr)
+  protected $DDGNodeName #hostClass.#(signature)_handler;
+$endif
+]]
+
+
 # Initialize DDG node for children
 ASTDecl.incrementalInitChildHandlers = [[
 $if (IncrementalLevelParam)
diff --git a/src/template/incremental/DDGNodeCopy.tt b/src/template/incremental/DDGNodeCopy.tt
index 40abb2a6..2ad0e14b 100644
--- a/src/template/incremental/DDGNodeCopy.tt
+++ b/src/template/incremental/DDGNodeCopy.tt
@@ -105,3 +105,23 @@ $if (IncrementalLevelAttr)
     }
 $endif
 ]]
+
+# Copy attribute equation DDG node
+SynEq.copyAttributeHandler = [[
+$if (IncrementalLevelParam)
+  $if (#isParameterized)
+    if (#(signature)_handler != null) {
+      copy.#(signature)_handler = new java.util.HashMap(4);
+    }
+  $else
+    if (#(signature)_handler != null) {
+      copy.#(signature)_handler = $DDGNodeName.createAttrHandler(#(signature)_handler, copy);
+    }
+  $endif
+$endif
+$if (IncrementalLevelAttr)
+    if (#(signature)_handler != null) {
+      copy.#(signature)_handler = new $DDGNodeName(#(signature)_handler, copy);
+    }
+$endif
+]]
-- 
GitLab