Refactor TeaVM core for new diagnostics API

This commit is contained in:
Alexey Andreev 2014-12-11 18:54:15 +04:00
parent 0945c27f16
commit 47f5943e8b
11 changed files with 159 additions and 110 deletions

View File

@ -89,10 +89,13 @@ public class DefaultCallGraphNode implements CallGraphNode {
return safeFieldAccessSites;
}
public void addFieldAccess(FieldReference field, InstructionLocation location) {
public boolean addFieldAccess(FieldReference field, InstructionLocation location) {
DefaultFieldAccessSite site = new DefaultFieldAccessSite(location, this, field);
if (fieldAccessSites.add(site)) {
graph.addFieldAccess(site);
return true;
} else {
return false;
}
}
@ -104,10 +107,13 @@ public class DefaultCallGraphNode implements CallGraphNode {
return safeClassAccessSites;
}
public void addClassAccess(String className, InstructionLocation location) {
public boolean addClassAccess(String className, InstructionLocation location) {
DefaultClassAccessSite site = new DefaultClassAccessSite(location, this, className);
if (classAccessSites.add(site)) {
graph.addClassAccess(site);
return true;
} else {
return false;
}
}
}

View File

@ -15,14 +15,34 @@
*/
package org.teavm.dependency;
import java.io.IOException;
import java.util.*;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.teavm.callgraph.CallGraph;
import org.teavm.callgraph.DefaultCallGraph;
import org.teavm.common.*;
import org.teavm.callgraph.DefaultCallGraphNode;
import org.teavm.common.CachedMapper;
import org.teavm.common.CachedMapper.KeyListener;
import org.teavm.common.Mapper;
import org.teavm.common.ServiceRepository;
import org.teavm.diagnostics.Diagnostics;
import org.teavm.model.*;
import org.teavm.model.AnnotationReader;
import org.teavm.model.CallLocation;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassReader;
import org.teavm.model.ClassReaderSource;
import org.teavm.model.FieldReader;
import org.teavm.model.FieldReference;
import org.teavm.model.InstructionLocation;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.MethodReader;
import org.teavm.model.MethodReference;
import org.teavm.model.ValueType;
import org.teavm.model.util.ModelUtils;
/**
@ -42,9 +62,6 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
private List<DependencyListener> listeners = new ArrayList<>();
private ServiceRepository services;
private Queue<Runnable> tasks = new ArrayDeque<>();
Set<MethodDependency> missingMethods = new HashSet<>();
Set<ClassDependency> missingClasses = new HashSet<>();
Set<FieldDependency> missingFields = new HashSet<>();
List<DependencyType> types = new ArrayList<>();
Map<String, DependencyType> typeMap = new HashMap<>();
private DependencyCheckerInterruptor interruptor;
@ -216,20 +233,38 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
@Override
public ClassDependency linkClass(String className, CallLocation callLocation) {
return classCache.map(className);
ClassDependency dep = classCache.map(className);
if (callLocation != null && callLocation.getMethod() != null) {
DefaultCallGraphNode callGraphNode = callGraph.getNode(callLocation.getMethod());
addClassAccess(callGraphNode, className, callLocation.getSourceLocation());
}
return dep;
}
private ClassDependency createClassDependency(String className, CallLocation callLocation) {
private void addClassAccess(DefaultCallGraphNode node, String className, InstructionLocation loc) {
if (!node.addClassAccess(className, loc)) {
return;
}
ClassReader cls = classSource.get(className);
ClassDependency dependency = new ClassDependency(this, className, stack, cls);
if (dependency.isMissing()) {
missingClasses.add(dependency);
} else {
if (cls != null) {
if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) {
addClassAccess(node, cls.getParent(), loc);
}
for (String iface : cls.getInterfaces()) {
addClassAccess(node, iface, loc);
}
}
}
private ClassDependency createClassDependency(String className) {
ClassReader cls = classSource.get(className);
ClassDependency dependency = new ClassDependency(this, className, cls);
if (!dependency.isMissing()) {
if (cls.getParent() != null && !cls.getParent().equals(className)) {
linkClass(cls.getParent(), stack);
linkClass(cls.getParent(), null);
}
for (String ifaceName : cls.getInterfaces()) {
linkClass(ifaceName, stack);
linkClass(ifaceName, null);
}
}
return dependency;
@ -241,9 +276,8 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
throw new IllegalArgumentException();
}
callGraph.getNode(methodRef);
if (callLocation != null) {
callGraph.addNode(caller);
callGraph.getNode(caller).addCallSite(methodRef, location);
if (callLocation != null && callLocation.getMethod() != null) {
callGraph.getNode(callLocation.getMethod()).addCallSite(methodRef, callLocation.getSourceLocation());
}
return methodCache.map(methodRef);
}
@ -306,10 +340,7 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
return null;
}
private MethodDependency createMethodDep(MethodReference methodRef, MethodReader method, DependencyStack stack) {
if (stack == null) {
stack = DependencyStack.ROOT;
}
private MethodDependency createMethodDep(MethodReference methodRef, MethodReader method) {
ValueType[] arguments = methodRef.getParameterTypes();
int paramCount = arguments.length + 1;
int varCount = Math.max(paramCount, method != null && method.getProgram() != null ?
@ -335,16 +366,14 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
thrown.setTag(methodRef + ":THROWN");
}
final MethodDependency dep = new MethodDependency(this, parameterNodes, paramCount, resultNode, thrown,
stack, method, methodRef);
method, methodRef);
if (method != null) {
final DependencyStack initClassStack = stack;
tasks.add(new Runnable() {
@Override public void run() {
linkClass(dep.getMethod().getOwnerName(), dep.getStack()).initClass(initClassStack);
CallLocation caller = new CallLocation(dep.getMethod().getReference());
linkClass(dep.getMethod().getOwnerName(), caller).initClass(caller);
}
});
} else {
missingMethods.add(dep);
}
return dep;
}
@ -374,9 +403,19 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
}
@Override
public FieldDependency linkField(FieldReference fieldRef, MethodReference caller, InstructionLocation location) {
fieldStacks.put(fieldRef, stack);
return fieldCache.map(fieldRef);
public FieldDependency linkField(final FieldReference fieldRef, final CallLocation location) {
if (location != null) {
callGraph.getNode(location.getMethod()).addFieldAccess(fieldRef, location.getSourceLocation());
}
FieldDependency dep = fieldCache.map(fieldRef);
if (!dep.isMissing()) {
tasks.add(new Runnable() {
@Override public void run() {
linkClass(fieldRef.getClassName(), location).initClass(location);
}
});
}
return dep;
}
@Override
@ -389,19 +428,16 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
return classCache.getKnown(className);
}
private FieldDependency createFieldNode(final FieldReference fieldRef, FieldReader field,
final DependencyStack stack) {
private FieldDependency createFieldNode(final FieldReference fieldRef, FieldReader field) {
DependencyNode node = new DependencyNode(this);
if (shouldLog) {
node.setTag(fieldRef.getClassName() + "#" + fieldRef.getFieldName());
}
FieldDependency dep = new FieldDependency(node, stack, field, fieldRef);
if (dep.isMissing()) {
missingFields.add(dep);
} else {
FieldDependency dep = new FieldDependency(node, field, fieldRef);
if (!dep.isMissing()) {
tasks.add(new Runnable() {
@Override public void run() {
linkClass(fieldRef.getClassName(), stack).initClass(stack);
linkClass(fieldRef.getClassName(), null).initClass(null);
}
});
}

View File

@ -63,7 +63,7 @@ class DependencyGraphBuilder {
}
for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) {
if (tryCatch.getExceptionType() != null) {
dependencyChecker.linkClass(tryCatch.getExceptionType(), caller.getMethod(), null);
dependencyChecker.linkClass(tryCatch.getExceptionType(), new CallLocation(caller.getMethod()));
}
}
}
@ -151,7 +151,7 @@ class DependencyGraphBuilder {
return;
}
MethodReference methodRef = new MethodReference(className, methodDesc);
MethodDependency methodDep = checker.linkMethod(methodRef, caller.getMethod(), location);
MethodDependency methodDep = checker.linkMethod(methodRef, new CallLocation(caller.getMethod(), location));
if (!methodDep.isMissing() && knownMethods.add(methodRef)) {
methodDep.use();
DependencyNode[] targetParams = methodDep.getVariables();
@ -204,7 +204,7 @@ class DependencyGraphBuilder {
}
if (cst instanceof ValueType.Object) {
final String className = ((ValueType.Object)cst).getClassName();
dependencyChecker.linkClass(className, caller.getMethod(), currentLocation);
dependencyChecker.linkClass(className, new CallLocation(caller.getMethod(), currentLocation));
}
}
@ -232,7 +232,7 @@ class DependencyGraphBuilder {
public void stringConstant(VariableReader receiver, String cst) {
nodes[receiver.getIndex()].propagate(dependencyChecker.getType("java.lang.String"));
MethodDependency method = dependencyChecker.linkMethod(new MethodReference(String.class,
"<init>", char[].class, void.class), caller.getMethod(), currentLocation);
"<init>", char[].class, void.class), new CallLocation(caller.getMethod(), currentLocation));
method.use();
}
@ -320,7 +320,7 @@ class DependencyGraphBuilder {
nodes[receiver.getIndex()].propagate(dependencyChecker.getType("[" + itemType));
String className = extractClassName(itemType);
if (className != null) {
dependencyChecker.linkClass(className, caller.getMethod(), currentLocation);
dependencyChecker.linkClass(className, new CallLocation(caller.getMethod(), currentLocation));
}
}
@ -342,7 +342,7 @@ class DependencyGraphBuilder {
nodes[receiver.getIndex()].propagate(dependencyChecker.getType(sb.toString()));
String className = extractClassName(itemType);
if (className != null) {
dependencyChecker.linkClass(className, caller.getMethod(), currentLocation);
dependencyChecker.linkClass(className, new CallLocation(caller.getMethod(), currentLocation));
}
}
@ -354,7 +354,8 @@ class DependencyGraphBuilder {
@Override
public void getField(VariableReader receiver, VariableReader instance, FieldReference field,
ValueType fieldType) {
FieldDependency fieldDep = dependencyChecker.linkField(field, caller.getMethod(), currentLocation);
FieldDependency fieldDep = dependencyChecker.linkField(field,
new CallLocation(caller.getMethod(), currentLocation));
DependencyNode receiverNode = nodes[receiver.getIndex()];
fieldDep.getValue().connect(receiverNode);
initClass(field.getClassName());
@ -362,7 +363,8 @@ class DependencyGraphBuilder {
@Override
public void putField(VariableReader instance, FieldReference field, VariableReader value) {
FieldDependency fieldDep = dependencyChecker.linkField(field, caller.getMethod(), currentLocation);
FieldDependency fieldDep = dependencyChecker.linkField(field,
new CallLocation(caller.getMethod(), currentLocation));
DependencyNode valueNode = nodes[value.getIndex()];
valueNode.connect(fieldDep.getValue());
initClass(field.getClassName());
@ -424,7 +426,8 @@ class DependencyGraphBuilder {
private void invokeSpecial(VariableReader receiver, VariableReader instance, MethodReference method,
List<? extends VariableReader> arguments) {
MethodDependency methodDep = dependencyChecker.linkMethod(method, caller.getMethod(), currentLocation);
MethodDependency methodDep = dependencyChecker.linkMethod(method,
new CallLocation(caller.getMethod(), currentLocation));
if (methodDep.isMissing()) {
return;
}
@ -445,7 +448,8 @@ class DependencyGraphBuilder {
private void invokeVirtual(VariableReader receiver, VariableReader instance, MethodReference method,
List<? extends VariableReader> arguments) {
MethodDependency methodDep = dependencyChecker.linkMethod(method, caller.getMethod(), currentLocation);
MethodDependency methodDep = dependencyChecker.linkMethod(method,
new CallLocation(caller.getMethod(), currentLocation));
if (methodDep.isMissing()) {
return;
}
@ -466,14 +470,14 @@ class DependencyGraphBuilder {
public void isInstance(VariableReader receiver, VariableReader value, final ValueType type) {
String className = extractClassName(type);
if (className != null) {
dependencyChecker.linkClass(className, caller.getMethod(), currentLocation);
dependencyChecker.linkClass(className, new CallLocation(caller.getMethod(), currentLocation));
}
}
@Override
public void initClass(final String className) {
dependencyChecker.linkClass(className, caller.getMethod(), currentLocation)
.initClass(caller.getMethod(), currentLocation);
CallLocation callLocation = new CallLocation(caller.getMethod(), currentLocation);
dependencyChecker.linkClass(className, callLocation).initClass(callLocation);
}
@Override
@ -482,7 +486,7 @@ class DependencyGraphBuilder {
DependencyNode receiverNode = nodes[receiver.getIndex()];
valueNode.connect(receiverNode);
dependencyChecker.linkMethod(new MethodReference(NullPointerException.class, "<init>", void.class),
caller.getMethod(), currentLocation).use();
new CallLocation(caller.getMethod(), currentLocation)).use();
currentExceptionConsumer.consume(dependencyChecker.getType("java.lang.NullPointerException"));
}
};

View File

@ -24,13 +24,11 @@ import org.teavm.model.FieldReference;
*/
public class FieldDependency implements FieldDependencyInfo {
private DependencyNode value;
private DependencyStack stack;
private FieldReader field;
private FieldReference reference;
FieldDependency(DependencyNode value, DependencyStack stack, FieldReader field, FieldReference reference) {
FieldDependency(DependencyNode value, FieldReader field, FieldReference reference) {
this.value = value;
this.stack = stack;
this.field = field;
this.reference = reference;
}
@ -40,11 +38,6 @@ public class FieldDependency implements FieldDependencyInfo {
return value;
}
@Override
public DependencyStack getStack() {
return stack;
}
public FieldReader getField() {
return field;
}

View File

@ -29,20 +29,17 @@ public class MethodDependency implements MethodDependencyInfo {
private int parameterCount;
private DependencyNode resultNode;
private DependencyNode thrown;
private DependencyStack stack;
private MethodReader method;
private MethodReference reference;
private boolean used;
MethodDependency(DependencyChecker dependencyChecker, DependencyNode[] variableNodes, int parameterCount,
DependencyNode resultNode, DependencyNode thrown, DependencyStack stack, MethodReader method,
MethodReference reference) {
DependencyNode resultNode, DependencyNode thrown, MethodReader method, MethodReference reference) {
this.dependencyChecker = dependencyChecker;
this.variableNodes = Arrays.copyOf(variableNodes, variableNodes.length);
this.parameterCount = parameterCount;
this.thrown = thrown;
this.resultNode = resultNode;
this.stack = stack;
this.method = method;
this.reference = reference;
}
@ -81,11 +78,6 @@ public class MethodDependency implements MethodDependencyInfo {
return thrown;
}
@Override
public DependencyStack getStack() {
return stack;
}
@Override
public MethodReference getReference() {
return reference;

View File

@ -16,6 +16,7 @@
package org.teavm.diagnostics;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.teavm.model.CallLocation;
@ -23,9 +24,11 @@ import org.teavm.model.CallLocation;
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class AccumulationDiagnostics implements Diagnostics {
public class AccumulationDiagnostics implements Diagnostics, ProblemProvider {
private List<Problem> problems = new ArrayList<>();
private List<Problem> readonlyProblems = Collections.unmodifiableList(problems);
private List<Problem> severeProblems = new ArrayList<>();
private List<Problem> readonlySevereProblems = Collections.unmodifiableList(severeProblems);
@Override
public void error(CallLocation location, String error, Object... params) {
@ -40,11 +43,13 @@ public class AccumulationDiagnostics implements Diagnostics {
problems.add(problem);
}
@Override
public List<Problem> getProblems() {
return problems;
return readonlyProblems;
}
@Override
public List<Problem> getSevereProblems() {
return severeProblems;
return readonlySevereProblems;
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.diagnostics;
import java.util.List;
/**
*
* @author Alexey Andreev
*/
public interface ProblemProvider {
List<Problem> getProblems();
List<Problem> getSevereProblems();
}

View File

@ -25,7 +25,14 @@ public class CallLocation {
private MethodReference method;
private InstructionLocation sourceLocation;
public CallLocation(MethodReference method) {
this(method, null);
}
public CallLocation(MethodReference method, InstructionLocation sourceLocation) {
if (method == null) {
throw new IllegalArgumentException("Method must not be null");
}
this.method = method;
this.sourceLocation = sourceLocation;
}

View File

@ -366,21 +366,11 @@ public class TeaVMTestTool {
vm.exportType("TestClass", cons.getClassName());
vm.setDebugEmitter(debugInfoBuilder);
vm.build(innerWriter, new DirectoryBuildTarget(outputDir));
if (!vm.hasMissingItems()) {
innerWriter.append("\n");
innerWriter.append("\nJUnitClient.run();");
if (sourceMapsGenerated) {
String sourceMapsFileName = targetName.substring(targetName.lastIndexOf('/') + 1) + ".map";
innerWriter.append("\n//# sourceMappingURL=").append(sourceMapsFileName);
}
} else {
innerWriter.append("JUnitClient.reportError(\n");
StringBuilder sb = new StringBuilder();
vm.showMissingItems(sb);
escapeStringLiteral(sb.toString(), innerWriter);
innerWriter.append(");");
log.warning("Error building test " + methodRef);
log.warning(sb.toString());
innerWriter.append("\n");
innerWriter.append("\nJUnitClient.run();");
if (sourceMapsGenerated) {
String sourceMapsFileName = targetName.substring(targetName.lastIndexOf('/') + 1) + ".map";
innerWriter.append("\n//# sourceMappingURL=").append(sourceMapsFileName);
}
}
if (sourceMapsGenerated) {
@ -402,20 +392,6 @@ public class TeaVMTestTool {
}
}
private void escapeStringLiteral(String text, Writer writer) throws IOException {
int index = 0;
while (true) {
int next = text.indexOf('\n', index);
if (next < 0) {
break;
}
escapeString(text.substring(index, next + 1), writer);
writer.append(" +\n");
index = next + 1;
}
escapeString(text.substring(index), writer);
}
private void escapeString(String string, Writer writer) throws IOException {
writer.append('\"');
for (int i = 0; i < string.length(); ++i) {

View File

@ -24,6 +24,7 @@ import org.teavm.cache.DiskRegularMethodNodeCache;
import org.teavm.cache.FileSymbolTable;
import org.teavm.debugging.information.DebugInformation;
import org.teavm.debugging.information.DebugInformationBuilder;
import org.teavm.diagnostics.ProblemProvider;
import org.teavm.javascript.RenderingContext;
import org.teavm.model.*;
import org.teavm.parsing.ClasspathClassHolderSource;
@ -199,6 +200,10 @@ public class TeaVMTool {
return cancelled;
}
public ProblemProvider getProblemProvider() {
return vm != null ? vm.getProblemProvider() : null;
}
public Collection<String> getClasses() {
return vm != null ? vm.getClasses() : Collections.<String>emptyList();
}
@ -289,10 +294,6 @@ public class TeaVMTool {
cancelled = true;
return;
}
if (vm.hasMissingItems()) {
log.info("Missing items found");
return;
}
log.info("JavaScript file successfully built");
if (debugInformationGenerated) {
DebugInformation debugInfo = debugEmitter.getDebugInformation();
@ -344,10 +345,6 @@ public class TeaVMTool {
}
}
public void checkForMissingItems() {
vm.checkForViolations();
}
private void copySourceFiles() {
if (vm.getWrittenClasses() == null) {
return;

View File

@ -23,6 +23,7 @@ import org.teavm.debugging.information.DebugInformationEmitter;
import org.teavm.debugging.information.SourceLocation;
import org.teavm.dependency.*;
import org.teavm.diagnostics.AccumulationDiagnostics;
import org.teavm.diagnostics.ProblemProvider;
import org.teavm.javascript.*;
import org.teavm.javascript.ast.ClassNode;
import org.teavm.javascript.ni.Generator;
@ -214,6 +215,10 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
return cancelled;
}
public ProblemProvider getProblemProvider() {
return diagnostics;
}
/**
* <p>Adds an entry point. TeaVM guarantees, that all methods that are required by the entry point
* will be available at run-time in browser. Also you need to specify for each parameter of entry point