template.ast.Attributes.tt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jastadd Show documentation
Show all versions of jastadd Show documentation
A metacompilation framework for Java using attribute grammars.
# Copyright (c) 2013-2016, 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.
AttrDecl.returnStmt [[
$if(#isAttrNTA)
#(getType) node = (#boxedType) this.getChild(#(signature)ChildPosition());
$include(AttrDecl.incHookAttrCompEnd)
return node;
$else
$if(!#isCircular)
$include(AttrDecl.incHookAttrCompEnd)
$endif
return #(signature)_value;
$endif
]]
AttrDecl.cacheDeclarations [[
$if(!#isParameterized)
/** @apilevel internal */
$if(#simpleCacheCheck)
protected boolean #(signature)_computed = false;
$else
protected $StateClass.Cycle #(signature)_computed = null;
$endif
/** @apilevel internal */
protected #getType #(signature)_value;
$if(#isCircular)
/** @apilevel internal */
protected boolean #(signature)_initialized = false;
$endif
$else
$if(#declaredNTA)
/** @apilevel internal */
protected $ASTNode #(signature)_proxy;
$endif
$if(LazyMaps)
/** @apilevel internal */
protected $DefaultMapType #(signature)_values;
$else
/** @apilevel internal */
protected $DefaultMapType #(signature)_values = $CreateDefaultMap;
$endif
$if(!#simpleCacheCheck)
/** @apilevel internal */
protected $DefaultMapType #(signature)_computed;
$endif
$endif
]]
# Method headers for attribute declarations.
AttrDecl.synDecl = AttrDecl.inhDecl [[
#docComment
#annotations
$include(AttrDecl.generatedAnnotations)
public #getType #name(#parametersDecl);
]]
AttrDecl.abstractSynDecl [[
#docComment
#annotations
$include(AttrDecl.generatedAnnotations)
public abstract #getType #name(#parametersDecl);
]]
AttrDecl.emitInlineComputeWithTry [[
#docComment
#annotations
$include(AttrDecl.generatedAnnotations)
public #getType #name($ParamDecl) {
#parameterStructure
#initLazyMaps
$include(AttrDecl.cacheCheck)
$include(AttrDecl.enterLazyAttribute)
#lazyState
$include(AttrDecl.checkVisited)
$include(AttrDecl.setVisited)
$include(AttrDecl.traceComputeBegin)
try $ComputeBody
finally {
$include(AttrDecl.leaveLazyAttribute)
#higherOrderAttributeCode
$include(AttrDecl.clearVisited)
$include(AttrDecl.traceComputeEndInline)
}
}
]]
AttrDecl.emitInlineComputeWithoutTry [[
#docComment
#annotations
$include(AttrDecl.generatedAnnotations)
public #getType #name($ParamDecl) {
#parameterStructure
#initLazyMaps
#lazyState
$include(AttrDecl.cacheCheck)
$include(AttrDecl.checkVisited)
$include(AttrDecl.setVisited)
$include(AttrDecl.traceComputeBegin)
$ComputeBody
}
]]
SynEq.emitComputeMethod [[
/** @apilevel internal */
$if(#hasComputeBlock)
private #getType #(name)_compute(#parametersDecl) #computeCode
$else
private #getType #(name)_compute(#parametersDecl) {
return #computeCode;
}
$endif
]]
AttrDecl.visitedException [[
$if(TraceVisitCheck)
System.out.println("Circular definition of attribute #hostClassName.#signatureJavaStyle.");
$else
throw new RuntimeException("Circular definition of attribute #hostClassName.#signatureJavaStyle.");
$endif
]]
AttrDecl.visitedDeclaration [[
$if(LegacyRewrite)
$if(#isParameterized)
/** @apilevel internal */
protected $DefaultMapType #(signature)_visited$if(!LazyMaps) = $CreateDefaultMap$endif;
$else
/** @apilevel internal */
protected int #(signature)_visited = -1;
$endif
$else
$if(#isParameterized)
/** @apilevel internal */
protected $DefaultSetType #(signature)_visited$if(!LazyMaps) = $CreateDefaultSet$endif;
$else
/** @apilevel internal */
protected boolean #(signature)_visited = false;
$endif
$endif
]]
AttrDecl.checkVisited [[
$if(VisitCheckEnabled)
$if(LegacyRewrite)
$if(#isParameterized)
if (Integer.valueOf(state().boundariesCrossed).equals(#(signature)_visited.get(_parameters))) {
$include(AttrDecl.visitedException)
}
$else
if (#(signature)_visited == state().boundariesCrossed) {
$include(AttrDecl.visitedException)
}
$endif
$else
$if(#isParameterized)
if (#(signature)_visited.contains(_parameters)) {
$include(AttrDecl.visitedException)
}
$else
if (#(signature)_visited) {
$include(AttrDecl.visitedException)
}
$endif
$endif
$endif
]]
AttrDecl.setVisited [[
$if(VisitCheckEnabled)
$if(LegacyRewrite)
$if(#isParameterized)
#(signature)_visited.put(_parameters, Integer.valueOf(state().boundariesCrossed));
$else
#(signature)_visited = state().boundariesCrossed;
$endif
$else
$if(#isParameterized)
#(signature)_visited.add(_parameters);
$else
#(signature)_visited = true;
$endif
$endif
$endif
]]
AttrDecl.clearVisited [[
$if(VisitCheckEnabled)
$if(LegacyRewrite)
$if(#isParameterized)
#(signature)_visited.remove(_parameters);
$else
#(signature)_visited = -1;
$endif
$else
$if(#isParameterized)
#(signature)_visited.remove(_parameters);
$else
#(signature)_visited = false;
$endif
$endif
$endif
]]
SynDecl.higherOrderAttributeCode:norewrite [[
$if(#isParameterized)
if (#(signature)_proxy == null) {
#(signature)_proxy = new $ASTNode();
$include(AttrDecl.incHookCreateNtaList)
#(signature)_proxy.setParent(this);
}
if (#(signature)_value != null) {
#(signature)_value.setParent(#(signature)_proxy);
}
$else
#(signature)_value.setParent(this);
$endif
]]
SynDecl.higherOrderAttributeCode:rewritesEnabled [[
$if(#isParameterized)
if (#(signature)_proxy == null) {
#(signature)_proxy = new $ASTNode();
$include(AttrDecl.incHookCreateNtaList)
$if(LegacyRewrite)
#(signature)_proxy.is$$Final = true;
$endif
#(signature)_proxy.setParent(this);
}
if (#(signature)_value != null) {
$if(LegacyRewrite)
#(signature)_proxy.addChild(#(signature)_value);
// Proxy child access is used to trigger rewrite of NTA value.
#(signature)_value = (#boxedType) #(signature)_proxy.getChild(#(signature)_proxy.numChildren - 1);
#(signature)_value.is$$Final = true;
$else
#(signature)_value.setParent(#(signature)_proxy);
if (#(signature)_value.mayHaveRewrite()) {
#(signature)_value = (#boxedType) #(signature)_value.rewrittenNode();
#(signature)_value.setParent(#(signature)_proxy);
}
$endif
}
$else
#(signature)_value.setParent(this);
$if(LegacyRewrite)
#(signature)_value.is$$Final = true;
$endif
$endif
]]
AttrDecl.emitEquation [[
$DocComment
#annotations
$include(AttrDecl.generatedAnnotations)
public #getType #name($ParamDecl) {
#parameterStructure
#initLazyMaps
#lazyState
$include(AttrDecl.cacheCheck)
$include(AttrDecl.incHookAttrCompStart)
$include(AttrDecl.checkVisited)
$include(AttrDecl.setVisited)
$include(AttrDecl.cacheInit)
$include(AttrDecl.enterLazyAttribute)
$include(AttrDecl.traceComputeBegin)
#computeLhs = $ComputeRhs;
$if(EmptyContainerSingletons)
$if(#isOptNTA)
if(#(signature)_value.numChildren() <= 0) {
#(signature)_value = Opt.EMPTY;
}
$endif
$if(#isListNTA)
if(#(signature)_value.numChildren() <= 0) {
#(signature)_value = List.EMPTY;
}
$endif
$endif
$include(AttrDecl.traceComputeEnd)
#higherOrderAttributeCode
$include(AttrDecl.cacheUpdate)
$include(AttrDecl.leaveLazyAttribute)
$include(AttrDecl.clearVisited)
$include(AttrDecl.returnStmt)
}
]]
AttrDecl.cacheCheck [[
$if(#isMemoized)
$include(AttrDecl.incHookAttrRead)
$if(!#isParameterized)
$if(#simpleCacheCheck)
if (#(signature)_computed) {
$else
if (#(signature)_computed == $StateClass.NON_CYCLE || #(signature)_computed == state().cycle()) {
$endif
$include(AttrDecl.traceCacheRead)
$if(#isAttrNTA)
return (#boxedType) getChild(#(signature)ChildPosition());
$else
return #(signature)_value;
$endif
}
$else
$if(#simpleCacheCheck)
if (#(signature)_values.containsKey(_parameters)) {
$else
if (#(signature)_values.containsKey(_parameters)
&& #(signature)_computed.containsKey(_parameters)
&& (#(signature)_computed.get(_parameters) == $StateClass.NON_CYCLE || #(signature)_computed.get(_parameters) == state().cycle())) {
$endif
$include(AttrDecl.traceCacheRead)
$if(#isAttrNTA)
return (#boxedType) getChild(#(signature)ChildPosition()));
$else
return (#boxedType) #(signature)_values.get(_parameters);
$endif
}
$endif
$endif
]]
# Update the cache value for this attribute if caching is enabled.
AttrDecl.cacheUpdate [[
$if(#isMemoized)
$if(LegacyRewrite)
if (#cacheStoreCondition) {
$endif
$if(#simpleCacheCheck)
$if(#isParameterized)
$include(AttrDecl.incHookAttrCompBeforeStore)
#(signature)_values.put(_parameters, #(signature)_value);
$include(AttrDecl.traceCacheStore)
$else
$include(AttrDecl.incHookAttrCompBeforeStore)
#(signature)_computed = true;
$include(AttrDecl.traceCacheStore)
$endif
$else
if (state().inCircle()) {
$if(#isParameterized)
$include(AttrDecl.incHookAttrCompBeforeStore)
#(signature)_values.put(_parameters, #(signature)_value);
#(signature)_computed.put(_parameters, state().cycle());
$include(AttrDecl.traceCacheStore)
$else
$include(AttrDecl.incHookAttrCompBeforeStore)
#(signature)_computed = state().cycle();
$include(AttrDecl.traceCacheStore)
$endif
} else {
$if(#isParameterized)
$include(AttrDecl.incHookAttrCompBeforeStore)
#(signature)_values.put(_parameters, #(signature)_value);
#(signature)_computed.put(_parameters, $StateClass.NON_CYCLE);
$include(AttrDecl.traceCacheStore)
$else
$include(AttrDecl.incHookAttrCompBeforeStore)
#(signature)_computed = $StateClass.NON_CYCLE;
$include(AttrDecl.traceCacheStore)
$endif
}
$endif
$if(LegacyRewrite)
} else {
$include(AttrDecl.traceCacheAbort)
$include(AttrDecl.incHookAttrCompAfterStore)
}
$endif
$endif
]]
AttrDecl.cacheInit [[
$if(LegacyRewrite)
$if(#isMemoized)
$include(AttrDecl.cacheInitRewrite)
$endif
$endif
]]
AttrDecl.cacheInitRewrite [[
$if(RewriteEnabled)
int _boundaries = state.boundariesCrossed;
boolean isFinal = this.is$$Final();
$endif
]]
AttrDecl.componentCheckDeclaration [[
/** @apilevel internal */
protected $StateClass.CircleState #(signature)_circle = null;
]]
AttrDecl.enterLazyAttribute [[
$if(ComponentCheck)
state().assertSameCircle(#(signature)_circle, "#hostClassName.#signatureJavaStyle");
#(signature)_circle = state().currentCircle();
state().enterLazyAttribute();
$else
$if(#isMemoized)
$if(#simpleCacheCheck)
state().enterLazyAttribute();
$endif
$endif
$endif
]]
AttrDecl.leaveLazyAttribute [[
$if(ComponentCheck)
state().leaveLazyAttribute();
$else
$if(#isMemoized)
$if(#simpleCacheCheck)
state().leaveLazyAttribute();
$endif
$endif
$endif
]]