2007-02-02 17:17:13 +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.ipa.callgraph;
|
|
|
|
|
2007-05-14 21:31:03 +00:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import com.ibm.wala.cast.ipa.callgraph.AstCallGraph.ScriptFakeRoot;
|
|
|
|
import com.ibm.wala.classLoader.CallSiteReference;
|
|
|
|
import com.ibm.wala.classLoader.IClass;
|
|
|
|
import com.ibm.wala.classLoader.IMethod;
|
|
|
|
import com.ibm.wala.ipa.callgraph.Entrypoint;
|
2007-07-06 22:09:29 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.impl.AbstractRootMethod;
|
2007-06-01 03:31:35 +00:00
|
|
|
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
2007-05-14 21:31:03 +00:00
|
|
|
import com.ibm.wala.ssa.SSAAbstractInvokeInstruction;
|
|
|
|
import com.ibm.wala.types.TypeReference;
|
2007-07-11 21:07:32 +00:00
|
|
|
import com.ibm.wala.util.collections.HashSetFactory;
|
2007-02-02 17:17:13 +00:00
|
|
|
|
2007-05-15 17:44:59 +00:00
|
|
|
public abstract class ScriptEntryPoints implements Iterable<Entrypoint> {
|
2007-02-02 17:17:13 +00:00
|
|
|
|
2016-06-11 01:21:08 +00:00
|
|
|
protected final IClassHierarchy cha;
|
2007-04-26 16:04:16 +00:00
|
|
|
|
2016-06-11 01:21:08 +00:00
|
|
|
protected final IClass scriptType;
|
2007-02-02 17:17:13 +00:00
|
|
|
|
2016-06-11 01:21:08 +00:00
|
|
|
public class ScriptEntryPoint extends Entrypoint {
|
|
|
|
public ScriptEntryPoint(IMethod scriptCodeBody) {
|
2007-04-26 16:04:16 +00:00
|
|
|
super(scriptCodeBody);
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
2008-01-16 16:32:33 +00:00
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2008-01-16 16:32:33 +00:00
|
|
|
public CallSiteReference makeSite(int programCounter) {
|
|
|
|
return makeScriptSite(getMethod(), programCounter);
|
|
|
|
}
|
2007-02-02 17:17:13 +00:00
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-02-02 17:17:13 +00:00
|
|
|
public TypeReference[] getParameterTypes(int i) {
|
2009-04-30 13:16:52 +00:00
|
|
|
assert i == 0;
|
2007-12-10 04:26:21 +00:00
|
|
|
if (getMethod().isStatic()) {
|
|
|
|
return new TypeReference[0];
|
|
|
|
} else {
|
|
|
|
return new TypeReference[] { getMethod().getDeclaringClass().getReference() };
|
|
|
|
}
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
2007-04-26 16:04:16 +00:00
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-02-02 17:17:13 +00:00
|
|
|
public int getNumberOfParameters() {
|
2007-12-10 04:26:21 +00:00
|
|
|
return getMethod().isStatic()? 0: 1;
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 16:32:33 +00:00
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-07-06 22:09:29 +00:00
|
|
|
public SSAAbstractInvokeInstruction addCall(AbstractRootMethod m){
|
2007-02-02 17:17:13 +00:00
|
|
|
CallSiteReference site = makeSite(0);
|
|
|
|
|
|
|
|
if (site == null) {
|
2007-04-26 16:04:16 +00:00
|
|
|
return null;
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2007-12-10 04:26:21 +00:00
|
|
|
int functionVn = getMethod().isStatic()? -1: makeArgument(m, 0);
|
2012-01-27 20:15:33 +00:00
|
|
|
int paramVns[] = new int[Math.min(0, getNumberOfParameters() - 1)];
|
2007-02-02 17:17:13 +00:00
|
|
|
for (int j = 0; j < paramVns.length; j++) {
|
2007-07-06 22:09:29 +00:00
|
|
|
paramVns[j] = makeArgument(m, j + 1);
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2007-04-26 16:04:16 +00:00
|
|
|
return ((ScriptFakeRoot) m).addDirectCall(functionVn, paramVns, site);
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-01 03:31:35 +00:00
|
|
|
public ScriptEntryPoints(IClassHierarchy cha, IClass scriptType) {
|
2007-02-02 17:17:13 +00:00
|
|
|
this.cha = cha;
|
|
|
|
this.scriptType = scriptType;
|
|
|
|
}
|
|
|
|
|
2008-01-16 16:32:33 +00:00
|
|
|
protected abstract CallSiteReference makeScriptSite(IMethod m, int pc);
|
|
|
|
|
2009-04-22 16:37:47 +00:00
|
|
|
protected boolean keep(IMethod method) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-05-14 21:31:03 +00:00
|
|
|
public Iterator<Entrypoint> iterator() {
|
2007-07-11 21:07:32 +00:00
|
|
|
Set<Entrypoint> ES = HashSetFactory.make();
|
2007-02-14 16:16:23 +00:00
|
|
|
Iterator<IClass> classes = scriptType.getClassLoader().iterateAllClasses();
|
2007-04-26 16:04:16 +00:00
|
|
|
while (classes.hasNext()) {
|
2007-02-14 16:16:23 +00:00
|
|
|
IClass cls = classes.next();
|
2008-02-28 17:44:04 +00:00
|
|
|
if (cha.isSubclassOf(cls, scriptType) && !cls.isAbstract()) {
|
2009-04-22 16:37:47 +00:00
|
|
|
for (IMethod method : cls.getDeclaredMethods()) {
|
|
|
|
if (keep(method)) {
|
|
|
|
ES.add(new ScriptEntryPoint(method));
|
|
|
|
}
|
2007-04-26 16:04:16 +00:00
|
|
|
}
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-26 16:04:16 +00:00
|
|
|
|
2007-02-02 17:17:13 +00:00
|
|
|
return ES.iterator();
|
|
|
|
}
|
2007-04-26 16:04:16 +00:00
|
|
|
|
2013-08-05 18:35:41 +00:00
|
|
|
public Entrypoint make(String scriptName) {
|
|
|
|
IClass cls = cha.lookupClass(TypeReference.findOrCreate(scriptType.getClassLoader().getReference(), scriptName));
|
2014-01-03 15:07:11 +00:00
|
|
|
assert cls != null && cha.isSubclassOf(cls, scriptType) && !cls.isAbstract() : String.valueOf(cls) + " for " + scriptName;
|
2013-08-05 18:35:41 +00:00
|
|
|
for (IMethod method : cls.getDeclaredMethods()) {
|
|
|
|
if (keep(method)) {
|
|
|
|
return new ScriptEntryPoint(method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert false;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|