avoid storing a pointer to the Rhino AST in the object returned from walkEntity()

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@4141 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
msridhar1 2011-04-22 19:55:26 +00:00
parent a7f99801b6
commit f347c93f61
1 changed files with 124 additions and 107 deletions

View File

@ -633,39 +633,27 @@ public class RhinoToAstTranslator {
return siblings;
}
private CAstEntity walkEntity(final ScriptOrFnNode n, WalkContext context) {
final FunctionContext child = (n instanceof FunctionNode) ? new FunctionContext(context, n) : new ScriptContext(context, n,
n.getSourceName());
CAstNode[] stmts = gatherChildren(n, child);
// add variable / constant / function declarations, if any
if (!child.nameDecls.isEmpty()) {
// new first statement will be a block declaring all names.
CAstNode[] newStmts = new CAstNode[stmts.length + 1];
newStmts[0] = Ast.makeNode(CAstNode.BLOCK_STMT, child.nameDecls.toArray(new CAstNode[child.nameDecls.size()]));
System.arraycopy(stmts, 0, newStmts, 1, stmts.length);
stmts = newStmts;
}
final CAstNode ast = Ast.makeNode(CAstNode.BLOCK_STMT, stmts);
final CAstControlFlowMap map = child.cfg();
final CAstSourcePositionMap pos = child.pos();
// not sure if we need this copy --MS
final Map<CAstNode, Collection<CAstEntity>> subs = HashMapFactory.make(child.getScopedEntities());
// TODO don't store pointer to n
return new CAstEntity() {
/**
* Used to represent a script or function in the CAst; see walkEntity().
*
*/
private class ScriptOrFnEntity implements CAstEntity {
private final String[] arguments;
private final String name;
// constructor of inner class
{
private final int kind;
private final Map<CAstNode, Collection<CAstEntity>> subs;
private final CAstNode ast;
private final CAstControlFlowMap map;
private final CAstSourcePositionMap pos;
ScriptOrFnEntity(ScriptOrFnNode n, Map<CAstNode, Collection<CAstEntity>> subs, CAstNode ast, CAstControlFlowMap map,
CAstSourcePositionMap pos) {
if (n instanceof FunctionNode) {
String x = ((FunctionNode) n).getFunctionName();
if (x == null || "".equals(x)) {
@ -690,6 +678,11 @@ public class RhinoToAstTranslator {
} else {
arguments = new String[0];
}
kind = (n instanceof FunctionNode) ? CAstEntity.FUNCTION_ENTITY : CAstEntity.SCRIPT_ENTITY;
this.subs = subs;
this.ast = ast;
this.map = map;
this.pos = pos;
}
public String toString() {
@ -706,10 +699,7 @@ public class RhinoToAstTranslator {
}
public int getKind() {
if (n instanceof FunctionNode)
return CAstEntity.FUNCTION_ENTITY;
else
return CAstEntity.SCRIPT_ENTITY;
return kind;
}
public String[] getArgumentNames() {
@ -764,7 +754,34 @@ public class RhinoToAstTranslator {
Assertions.UNREACHABLE("JuliansUnnamedCAstEntity$2.getType()");
return null;
}
};
}
private CAstEntity walkEntity(final ScriptOrFnNode n, WalkContext context) {
final FunctionContext child = (n instanceof FunctionNode) ? new FunctionContext(context, n) : new ScriptContext(context, n,
n.getSourceName());
CAstNode[] stmts = gatherChildren(n, child);
// add variable / constant / function declarations, if any
if (!child.nameDecls.isEmpty()) {
// new first statement will be a block declaring all names.
CAstNode[] newStmts = new CAstNode[stmts.length + 1];
newStmts[0] = Ast.makeNode(CAstNode.BLOCK_STMT, child.nameDecls.toArray(new CAstNode[child.nameDecls.size()]));
System.arraycopy(stmts, 0, newStmts, 1, stmts.length);
stmts = newStmts;
}
final CAstNode ast = Ast.makeNode(CAstNode.BLOCK_STMT, stmts);
final CAstControlFlowMap map = child.cfg();
final CAstSourcePositionMap pos = child.pos();
// not sure if we need this copy --MS
final Map<CAstNode, Collection<CAstEntity>> subs = HashMapFactory.make(child.getScopedEntities());
return new ScriptOrFnEntity(n, subs, ast, map, pos);
}
private CAstNode[] gatherSiblings(Node n, WalkContext context) {