All Downloads are FREE. Search and download functionalities are using the official Maven repository.

template.incremental.State.tt Maven / Gradle / Ivy

There is a newer version: 2.3.6
Show newest version
# Copyright (c) 2013, The JastAdd Team
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Lund University nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

State.incHook = [[
$if(IncrementalEnabled)

$if(IncrementalLevelRegion)
  public int IN_COMPUTATION = 0;
$endif

  protected java.util.Stack handlerAttrStack = new java.util.Stack();

//protected java.util.Stack handlerRewriteStack = new java.util.Stack();

  public void addHandlerDepTo($ASTNode$$DepGraphNode handler) {
    if (!IN_ATTR_STORE_EVAL || handler == null) {
//  if (!IN_ATTR_STORE_EVAL && !IN_REWRITE_EVAL || handler == null) {
      return;
    }
$if(IncrementalTrack)
    $ASTNode$$DepGraphNode.trackedReads++;
$endif
    java.util.Stack handlerStack = handlerAttrStack;
//  if (IN_REWRITE_EVAL)
//    handlerStack = handlerRewriteStack;
    if (!handlerStack.isEmpty()) {
      //throw new Error("Handler stack is empty at addition of dependency!");
      $ASTNode$$DepGraphNode top = ($ASTNode$$DepGraphNode) handlerStack.peek();
      handler.addDependant(top);

$if(IncrementalPropLimit)
      if (!handler.cacheInDependent)
        top.noCacheRead = false;
$endif
    }
  }

  public boolean IN_ATTR_STORE_EVAL = false;

  public void enterAttrStoreEval($ASTNode$$DepGraphNode handler) {
$if(IncrementalTrack)
    $ASTNode$$DepGraphNode.trackedComputes++;
$endif
//  if (!IN_REWRITE_EVAL) {
    IN_ATTR_STORE_EVAL = true;
    //System.out.println("attr eval stack enter: " + handler.fAttrID);
    pushHandler(handlerAttrStack, handler);
//  }
  }

  public void exitAttrStoreEval($ASTNode$$DepGraphNode handler) {
//  if (!IN_REWRITE_EVAL) {
    popHandler(handlerAttrStack, handler);
    //System.out.println("attr eval stack exit: " + handler.fAttrID);
    IN_ATTR_STORE_EVAL = !handlerAttrStack.isEmpty();
//  }
  }

//public boolean IN_REWRITE_EVAL = false;

  public void enterRewriteEval($ASTNode$$DepGraphNode handler) {
    enterAttrStoreEval(handler);
//  if (grammar().incrementalTrack) {
//    $ASTNode$$DepGraphNode.trackedComp++;
//  }
//  IN_REWRITE_EVAL = true;
//  pushHandler(handlerRewriteStack, handler);
  }

  public void exitRewriteEval($ASTNode$$DepGraphNode handler) {
    exitAttrStoreEval(handler);
//  if (IN_REWRITE_EVAL) {
//    popHandler(handlerRewriteStack, handler);
//    IN_REWRITE_EVAL = !handlerRewriteStack.isEmpty();
//  }
  }

  public boolean IN_CONSTRUCTION = false;

  private int inc_constructionCount = 0;

  public void enterConstruction() {
    IN_CONSTRUCTION = true;
    inc_constructionCount++;
  }

  public void exitConstruction() {
    inc_constructionCount--;
    if (inc_constructionCount == 0)
    IN_CONSTRUCTION = false;
  }

  protected void pushHandler(java.util.Stack stack, $ASTNode$$DepGraphNode handler) {
$if(IncrementalPropLimit)
    // assume that evaluation will be clean from cached values
    handler.noCacheRead = true;
$endif
    stack.push(handler);
  }

  protected $ASTNode$$DepGraphNode popHandler(java.util.Stack stack, $ASTNode$$DepGraphNode handler) {
    if (stack.isEmpty())
      throw new Error("Handler stack is empty at exit!");
    $ASTNode$$DepGraphNode h = ($ASTNode$$DepGraphNode)stack.pop();
//  Skip this for now, deal with transitive dependencies later
//  if (grammar().incrementalPropLimit) {
//    h.setCacheInDependent(h.noCacheRead);
//    if (!stack.isEmpty()) {
//      $ASTNode$$DepGraphNode top = ($ASTNode$$DepGraphNode) stack.peek();
//      top.noCacheRead &= h.noCacheRead;
//    }
//  }
    if (h != handler)
      throw new Error("Top of handler stack does not match at pop!");
//    throw new Error("Top of handler stack does not match at pop! [" + handler.fAttrID + "]");
    return h;
  }

$endif
]]

State.incHookConstructionStart = [[
$if(IncrementalEnabled)
state().enterConstruction();
$endif
]]

State.incHookConstructionEnd = [[
$if(IncrementalEnabled)
state().exitConstruction();
$endif
]]

ASTDecl.incChangeStateMethod = [[
/** @apilevel internal */
public void #name.inc_changeState(int newState) {
$if(#isASTNodeDecl)
  inc_state = newState;

  $if(IncrementalLevelParam)
  getParent_handler.changeState(newState);
  numChildren_handler.changeState(newState);
  for (int i = 0; children != null && i < children.length; i++) {
    $ASTNode child = children[i];
    if (getChild_handler[i] != null) {
      getChild_handler[i].changeState(newState);
    }
    if (child != null) {
      child.inc_changeState(newState);
    }
  }
  $endif

  $if(IncrementalLevelAttr)
  getParent_handler.changeState(newState);
  numChildren_handler.changeState(newState);
  getChild_handler.changeState(newState);
  for (int i = 0; children != null && i < children.length; i++) {
    $ASTNode child = children[i];
    if (child != null) {
      child.inc_changeState(newState);
    }
  }
  $endif

  $if(IncrementalLevelNode)
  handler.changeState(newState);
  for (int i = 0; children != null && i < children.length; i++) {
    $ASTNode child = children[i];
    if (child != null) {
      child.inc_changeState(newState);
    }
  }
  $endif

  $if(IncrementalLevelRegion)
  if (isRegionRoot()) {
    handler().changeState(newState);
  }
  for (int i = 0; children != null && i < children.length; i++) {
    $ASTNode child = children[i];
    if (child != null) {
      child.inc_changeState(newState);
    }
  }
  $endif

$else
  super.inc_changeState(newState);
$endif
  $ChangeStateTokens
  $ChangeStateAttributes
}
]]

ASTDecl.changeStateTokenHandler = [[
$if(IncrementalLevelParam)
if (get$(Id)_handler != null) {
  get$(Id)_handler.changeState(newState);
}
$endif
$if(IncrementalLevelAttr)
if (get$(Id)_handler != null) {
  get$(Id)_handler.changeState(newState);
}
$endif
]]

ASTDecl.changeStateAttributeHandler = [[
$if(IncrementalLevelParam)
$if(IsParameterized)
for (java.util.Iterator itr = $(AttrSign)_handler.values().iterator(); itr.hasNext();) {
  $DDGNodeName handler = ($DDGNodeName)itr.next();
  handler.changeState(newState);
}
$else
if ($(AttrSign)_handler != null) {
  $(AttrSign)_handler.changeState(newState);
}
$endif
$endif
$if(IncrementalLevelAttr)
if ($(AttrSign)_handler != null) {
  $(AttrSign)_handler.changeState(newState);
}
$endif
$if(ChangeStateValue)
$if(IsParameterized)
if ($(AttrSign)_list != null) {
  $(AttrSign)_list.inc_changeState(newState);
}
$else
if ($(AttrSign)_computed && ($(AttrSign)_value instanceof $ASTNode)) {
  $(AttrSign)_value.inc_changeState(newState);
}
$endif
$endif
]]

ASTDecl.incThrowAwayMethod = [[
/** @apilevel internal */
public void #name.inc_throwAway() {
  inc_state = inc_GARBAGE;
$if(#isASTNodeDecl)
  $if(IncrementalLevelParam)
  getParent_handler.throwAway();
  numChildren_handler.throwAway();
  for (int i = 0; children != null && i < children.length; i++) {
    $ASTNode child = children[i];
    if (getChild_handler[i] != null) {
      getChild_handler[i].throwAway();
    }
    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)
  getParent_handler.throwAway();
  numChildren_handler.throwAway();
  getChild_handler.throwAway();
  for (int i = 0; children != null && i < children.length; i++) {
    $ASTNode child = children[i];
    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)
  handler.throwAway();
  for (int i = 0; children != null && i < children.length; i++) {
    $ASTNode child = children[i];
    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)
  if (isRegionRoot()) {
    handler().throwAway();
  }
  for (int i = 0; children != null && i < children.length; i++) {
    $ASTNode child = children[i];
    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
  super.inc_throwAway();
$endif
  $ThrowAwayTokens
  $ThrowAwayAttributes
}
]]

ASTDecl.throwAwayTokenHandler = [[
$if(IncrementalLevelParam)
if (get$(Id)_handler != null) {
  get$(Id)_handler.throwAway();
}
$endif
$if(IncrementalLevelAttr)
if (get$(Id)_handler != null) {
  get$(Id)_handler.throwAway();
}
$endif
]]

ASTDecl.throwAwayAttributeHandler = [[
$if(IncrementalLevelParam)
$if(IsParameterized)
for (java.util.Iterator itr = $(AttrSign)_handler.values().iterator(); itr.hasNext();) {
  $DDGNodeName handler = ($DDGNodeName)itr.next();
  handler.throwAway();
}
$else
if ($(AttrSign)_handler != null) {
  $(AttrSign)_handler.throwAway();
}
$endif
$endif
$if(IncrementalLevelAttr)
if ($(AttrSign)_handler != null) {
  $(AttrSign)_handler.throwAway();
}
$endif
$if(ThrowAwayValue)
$if(IsParameterized)
if ($(AttrSign)_list != null) {
  $(AttrSign)_list.inc_throwAway();
}
$else
if ($(AttrSign)_computed && ($(AttrSign)_value instanceof $ASTNode)) {
  $(AttrSign)_value.inc_throwAway();
}
$endif
$endif
]]

ASTDecl.incStateFields = [[
$if(#isASTNodeDecl)
public static final int $ASTNode.inc_CREATED = 0;
public static final int $ASTNode.inc_CLONED = 1;
public static final int $ASTNode.inc_LIVE = 2;
public static final int $ASTNode.inc_GARBAGE = 3;
public int $ASTNode.inc_state = inc_CREATED;
$endif
]]




© 2015 - 2024 Weber Informatics LLC | Privacy Policy