2007-04-19 13:48:01 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 IBM Corporation.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* IBM Corporation - initial API and implementation
|
|
|
|
*****************************************************************************/
|
|
|
|
package com.ibm.wala.cast.tree.impl;
|
|
|
|
|
|
|
|
import com.ibm.wala.cast.tree.*;
|
|
|
|
import com.ibm.wala.util.collections.*;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public abstract class CAstRewriter<RewriteContext> {
|
|
|
|
|
|
|
|
protected final CAst Ast;
|
|
|
|
|
|
|
|
protected final boolean recursive;
|
|
|
|
|
|
|
|
protected final RewriteContext rootContext;
|
|
|
|
|
2007-06-25 20:08:56 +00:00
|
|
|
public CAstRewriter(CAst Ast, boolean recursive, RewriteContext rootContext)
|
|
|
|
{
|
2007-04-19 13:48:01 +00:00
|
|
|
this.Ast = Ast;
|
|
|
|
this.recursive = recursive;
|
|
|
|
this.rootContext = rootContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface Rewrite {
|
|
|
|
|
|
|
|
CAstNode newRoot();
|
|
|
|
|
|
|
|
CAstControlFlowMap newCfg();
|
|
|
|
|
|
|
|
CAstSourcePositionMap newPos();
|
|
|
|
|
|
|
|
CAstNodeTypeMap newTypes();
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
Map<CAstNode,Collection<CAstEntity>> newChildren();
|
2007-04-19 13:48:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
protected abstract CAstNode copyNodes(CAstNode root, RewriteContext context, Map<CAstNode, CAstNode> nodeMap);
|
2007-04-19 13:48:01 +00:00
|
|
|
|
2007-06-25 20:08:56 +00:00
|
|
|
protected CAstNode flowOutTo(Map<CAstNode, CAstNode> nodeMap,
|
|
|
|
CAstNode oldSource,
|
|
|
|
Object label,
|
|
|
|
CAstNode oldTarget,
|
|
|
|
CAstControlFlowMap orig,
|
|
|
|
CAstSourcePositionMap src)
|
|
|
|
{
|
|
|
|
return oldTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
private CAstControlFlowMap copyFlow(Map<CAstNode, CAstNode> nodeMap, CAstControlFlowMap orig, CAstSourcePositionMap src) {
|
|
|
|
Set mappedOutsideNodes = new HashSet(1);
|
2007-04-19 13:48:01 +00:00
|
|
|
Collection<CAstNode> oldSources = orig.getMappedNodes();
|
2007-06-25 20:08:56 +00:00
|
|
|
CAstControlFlowRecorder newMap = new CAstControlFlowRecorder(src);
|
2007-04-24 14:10:57 +00:00
|
|
|
for (Iterator<CAstNode> NS = nodeMap.keySet().iterator(); NS.hasNext();) {
|
2007-04-19 13:48:01 +00:00
|
|
|
CAstNode old = NS.next();
|
|
|
|
CAstNode newNode = nodeMap.get(old);
|
|
|
|
newMap.map(newNode, newNode);
|
|
|
|
if (oldSources.contains(old)) {
|
2007-04-24 14:10:57 +00:00
|
|
|
if (orig.getTarget(old, null) != null) {
|
|
|
|
CAstNode oldTarget = orig.getTarget(old, null);
|
|
|
|
if (nodeMap.containsKey(oldTarget)) {
|
|
|
|
newMap.add(newNode, nodeMap.get(oldTarget), null);
|
|
|
|
} else {
|
2007-06-25 20:08:56 +00:00
|
|
|
CAstNode tgt = flowOutTo(nodeMap, old, null, oldTarget, orig, src);
|
|
|
|
newMap.add(newNode, tgt, null);
|
|
|
|
if (tgt != CAstControlFlowMap.EXCEPTION_TO_EXIT
|
|
|
|
&&
|
|
|
|
!mappedOutsideNodes.contains(tgt))
|
|
|
|
{
|
|
|
|
mappedOutsideNodes.add(tgt);
|
|
|
|
newMap.map(tgt, tgt);
|
|
|
|
}
|
2007-04-24 14:10:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Iterator LS = orig.getTargetLabels(old).iterator(); LS.hasNext();) {
|
|
|
|
Object label = LS.next();
|
|
|
|
CAstNode oldTarget = orig.getTarget(old, label);
|
|
|
|
if (nodeMap.containsKey(oldTarget)) {
|
|
|
|
newMap.add(newNode, nodeMap.get(oldTarget), label);
|
|
|
|
} else {
|
2007-06-25 20:08:56 +00:00
|
|
|
CAstNode tgt = flowOutTo(nodeMap, old, null, oldTarget, orig, src);
|
|
|
|
newMap.add(newNode, tgt, label);
|
|
|
|
if (tgt != CAstControlFlowMap.EXCEPTION_TO_EXIT
|
|
|
|
&&
|
|
|
|
!mappedOutsideNodes.contains(tgt))
|
|
|
|
{
|
|
|
|
mappedOutsideNodes.add(tgt);
|
|
|
|
newMap.map(tgt, tgt);
|
|
|
|
}
|
2007-04-24 14:10:57 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newMap;
|
|
|
|
}
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
private CAstSourcePositionMap copySource(Map<CAstNode, CAstNode> nodeMap, CAstSourcePositionMap orig) {
|
2007-04-19 13:48:01 +00:00
|
|
|
if (orig == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
CAstSourcePositionRecorder newMap = new CAstSourcePositionRecorder();
|
2007-04-24 14:10:57 +00:00
|
|
|
for (Iterator<CAstNode> NS = nodeMap.keySet().iterator(); NS.hasNext();) {
|
|
|
|
CAstNode old = NS.next();
|
|
|
|
CAstNode newNode = nodeMap.get(old);
|
|
|
|
|
|
|
|
if (orig.getPosition(old) != null) {
|
|
|
|
newMap.setPosition(newNode, orig.getPosition(old));
|
|
|
|
}
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private CAstNodeTypeMap copyTypes(Map nodeMap, CAstNodeTypeMap orig) {
|
|
|
|
if (orig != null) {
|
|
|
|
CAstNodeTypeMapRecorder newMap = new CAstNodeTypeMapRecorder();
|
2007-04-24 14:10:57 +00:00
|
|
|
for (Iterator NS = nodeMap.entrySet().iterator(); NS.hasNext();) {
|
|
|
|
Map.Entry entry = (Map.Entry) NS.next();
|
|
|
|
CAstNode oldNode = (CAstNode) entry.getKey();
|
|
|
|
CAstNode newNode = (CAstNode) entry.getValue();
|
|
|
|
|
|
|
|
if (orig.getNodeType(oldNode) != null) {
|
|
|
|
newMap.add(newNode, orig.getNodeType(oldNode));
|
|
|
|
}
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
2007-04-24 14:10:57 +00:00
|
|
|
|
2007-04-19 13:48:01 +00:00
|
|
|
return newMap;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
private Map<CAstNode,Collection<CAstEntity>> copyChildren(Map nodeMap, Map<CAstNode,Collection<CAstEntity>> children) {
|
|
|
|
final Map<CAstNode,Collection<CAstEntity>> newChildren = new LinkedHashMap<CAstNode,Collection<CAstEntity>>();
|
2007-04-19 13:48:01 +00:00
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
for (Iterator NS = nodeMap.entrySet().iterator(); NS.hasNext();) {
|
|
|
|
Map.Entry entry = (Map.Entry) NS.next();
|
2007-04-19 13:48:01 +00:00
|
|
|
CAstNode oldNode = (CAstNode) entry.getKey();
|
|
|
|
CAstNode newNode = (CAstNode) entry.getValue();
|
2007-04-24 14:10:57 +00:00
|
|
|
|
2007-04-19 13:48:01 +00:00
|
|
|
if (children.containsKey(oldNode)) {
|
2007-04-24 14:10:57 +00:00
|
|
|
Set<CAstEntity> newEntities = new LinkedHashSet<CAstEntity>();
|
|
|
|
newChildren.put(newNode, newEntities);
|
|
|
|
for (Iterator oldEntities = ((Collection) children.get(oldNode)).iterator(); oldEntities.hasNext();) {
|
|
|
|
CAstEntity oldE = (CAstEntity) oldEntities.next();
|
|
|
|
newEntities.add(recursive ? rewrite(oldE) : oldE);
|
|
|
|
}
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-24 14:10:57 +00:00
|
|
|
|
|
|
|
for (Iterator<Map.Entry<CAstNode,Collection<CAstEntity>>> keys = children.entrySet().iterator(); keys.hasNext();) {
|
|
|
|
Map.Entry<CAstNode,Collection<CAstEntity>> entry = keys.next();
|
|
|
|
CAstNode key = entry.getKey();
|
|
|
|
if (!(key instanceof CAstNode)) {
|
|
|
|
Set<CAstEntity> newEntities = new LinkedHashSet<CAstEntity>();
|
|
|
|
newChildren.put(key, newEntities);
|
|
|
|
for (Iterator oldEntities = ((Collection) entry.getValue()).iterator(); oldEntities.hasNext();) {
|
|
|
|
CAstEntity oldE = (CAstEntity) oldEntities.next();
|
|
|
|
newEntities.add(recursive ? rewrite(oldE) : oldE);
|
|
|
|
}
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newChildren;
|
|
|
|
}
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
public Rewrite rewrite(CAstNode root, final CAstControlFlowMap cfg, final CAstSourcePositionMap pos, final CAstNodeTypeMap types,
|
|
|
|
final Map<CAstNode,Collection<CAstEntity>> children) {
|
2007-04-19 13:48:01 +00:00
|
|
|
final Map<CAstNode, CAstNode> nodes = new HashMap<CAstNode, CAstNode>();
|
|
|
|
final CAstNode newRoot = copyNodes(root, rootContext, nodes);
|
|
|
|
return new Rewrite() {
|
|
|
|
private CAstControlFlowMap theCfg = null;
|
2007-04-24 14:10:57 +00:00
|
|
|
|
2007-04-19 13:48:01 +00:00
|
|
|
private CAstSourcePositionMap theSource = null;
|
2007-04-24 14:10:57 +00:00
|
|
|
|
2007-04-19 13:48:01 +00:00
|
|
|
private CAstNodeTypeMap theTypes = null;
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
private Map<CAstNode,Collection<CAstEntity>> theChildren = null;
|
2007-04-19 13:48:01 +00:00
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
public CAstNode newRoot() {
|
|
|
|
return newRoot;
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
public CAstControlFlowMap newCfg() {
|
|
|
|
if (theCfg == null)
|
2007-06-25 20:08:56 +00:00
|
|
|
theCfg = copyFlow(nodes, cfg, newPos());
|
2007-04-24 14:10:57 +00:00
|
|
|
return theCfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CAstSourcePositionMap newPos() {
|
|
|
|
if (theSource == null)
|
|
|
|
theSource = copySource(nodes, pos);
|
|
|
|
return theSource;
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public CAstNodeTypeMap newTypes() {
|
2007-04-24 14:10:57 +00:00
|
|
|
if (theTypes == null)
|
|
|
|
theTypes = copyTypes(nodes, types);
|
|
|
|
return theTypes;
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
public Map<CAstNode,Collection<CAstEntity>> newChildren() {
|
|
|
|
if (theChildren == null)
|
|
|
|
theChildren = copyChildren(nodes, children);
|
|
|
|
return theChildren;
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public CAstEntity rewrite(final CAstEntity root) {
|
|
|
|
if (root.getAST() != null) {
|
2007-04-24 14:10:57 +00:00
|
|
|
final Rewrite rewrite = rewrite(root.getAST(), root.getControlFlow(), root.getSourceMap(), root.getNodeTypeMap(), root
|
|
|
|
.getAllScopedEntities());
|
2007-04-19 13:48:01 +00:00
|
|
|
|
|
|
|
return new DelegatingEntity(root) {
|
2007-04-24 14:10:57 +00:00
|
|
|
public String toString() {
|
|
|
|
return root.toString() + " (clone)";
|
|
|
|
}
|
|
|
|
|
|
|
|
public Iterator getScopedEntities(CAstNode construct) {
|
|
|
|
Map newChildren = getAllScopedEntities();
|
|
|
|
if (newChildren.containsKey(construct)) {
|
|
|
|
return ((Collection) newChildren.get(construct)).iterator();
|
|
|
|
} else {
|
|
|
|
return EmptyIterator.instance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<CAstNode, Collection<CAstEntity>> getAllScopedEntities() {
|
|
|
|
return rewrite.newChildren();
|
|
|
|
}
|
|
|
|
|
|
|
|
public CAstNode getAST() {
|
|
|
|
return rewrite.newRoot();
|
|
|
|
}
|
|
|
|
|
|
|
|
public CAstNodeTypeMap getNodeTypeMap() {
|
|
|
|
return rewrite.newTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
public CAstSourcePositionMap getSourceMap() {
|
|
|
|
return rewrite.newPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
public CAstControlFlowMap getControlFlow() {
|
|
|
|
return rewrite.newCfg();
|
|
|
|
}
|
2007-04-19 13:48:01 +00:00
|
|
|
};
|
2007-04-24 14:10:57 +00:00
|
|
|
|
2007-04-19 13:48:01 +00:00
|
|
|
} else if (recursive) {
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
Map<CAstNode, Collection<CAstEntity>> children = root.getAllScopedEntities();
|
|
|
|
final Map<CAstNode,Collection<CAstEntity>> newChildren = new LinkedHashMap<CAstNode,Collection<CAstEntity>>();
|
|
|
|
for (Iterator<Map.Entry<CAstNode,Collection<CAstEntity>>> keys = children.entrySet().iterator(); keys.hasNext();) {
|
|
|
|
Map.Entry<CAstNode,Collection<CAstEntity>> entry = keys.next();
|
|
|
|
CAstNode key = entry.getKey();
|
|
|
|
Set<CAstEntity> newValues = new LinkedHashSet<CAstEntity>();
|
|
|
|
newChildren.put(key, newValues);
|
|
|
|
for (Iterator es = ((Collection) entry.getValue()).iterator(); es.hasNext();) {
|
|
|
|
newValues.add(rewrite((CAstEntity) es.next()));
|
|
|
|
}
|
2007-04-19 13:48:01 +00:00
|
|
|
}
|
2007-04-24 14:10:57 +00:00
|
|
|
|
2007-04-19 13:48:01 +00:00
|
|
|
return new DelegatingEntity(root) {
|
2007-04-24 14:10:57 +00:00
|
|
|
public String toString() {
|
|
|
|
return root.toString() + " (clone)";
|
|
|
|
}
|
|
|
|
|
|
|
|
public Iterator getScopedEntities(CAstNode construct) {
|
|
|
|
if (newChildren.containsKey(construct)) {
|
|
|
|
return ((Collection) newChildren.get(construct)).iterator();
|
|
|
|
} else {
|
|
|
|
return EmptyIterator.instance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<CAstNode,Collection<CAstEntity>> getAllScopedEntities() {
|
|
|
|
return newChildren;
|
|
|
|
}
|
|
|
|
};
|
2007-04-19 13:48:01 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-24 14:10:57 +00:00
|
|
|
}
|