Vorbereitung für "Mehrere Lösungen in einer Classfile"

This commit is contained in:
Enrico Schrödter 2015-11-06 16:42:22 +01:00
parent 947d145cf1
commit 87e49a692a
14 changed files with 145 additions and 34 deletions

View File

@ -20,13 +20,14 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
public class ClassGenerator extends ClassGen{
private DHBWConstantPoolGen cp;
private DHBWInstructionFactory factory;
private TypeinferenceResultSet tiResult;
private TypeinferenceResults tiResult;
private int lambdaMethodeNr = 0;
private Type superClass;
@ -34,9 +35,9 @@ public class ClassGenerator extends ClassGen{
private Map<String, ClassGenerator> extraClasses = new HashMap<>();
public ClassGenerator(String name, Type superClass, String string, short accessflags, String[] strings, TypeinferenceResultSet resultSet) {
public ClassGenerator(String name, Type superClass, String string, short accessflags, String[] strings, TypeinferenceResults typeinferenceResults) {
super(name,superClass.get_Name(),string,accessflags,strings, new DHBWConstantPoolGen());
this.tiResult = resultSet;
this.tiResult = typeinferenceResults;
this.superClass = superClass;
cp = (DHBWConstantPoolGen) super.getConstantPool();
@ -54,8 +55,7 @@ public class ClassGenerator extends ClassGen{
* @param toTPH
* @return Es gilt dann "toTPH extends Type"
*/
public org.apache.commons.bcel6.generic.Type getNearestUsedType(TypePlaceholder toTPH, Menge<TypePlaceholder> usedTypes){
Type t = resolveTPH(toTPH, usedTypes);
public org.apache.commons.bcel6.generic.Type getNearestUsedType(Type t, Menge<TypePlaceholder> usedTypes){
if(t == null){
return this.getInstructionFactory().createObjectType();
}else if(t instanceof TypePlaceholder){ //Es muss sich in diesem Fall um einen TPH handeln:
@ -69,11 +69,11 @@ public class ClassGenerator extends ClassGen{
return this.getNearestUsedType(toTPH, null);
}
public Type resolveTPH(TypePlaceholder typePlaceholder) {
public Menge<Type> resolveTPH(TypePlaceholder typePlaceholder) {
return resolveTPH(typePlaceholder, null);
}
public Type resolveTPH(TypePlaceholder typePlaceholder, Menge<TypePlaceholder> toOneOfTheseTypes) {
public Menge<Type> resolveTPH(TypePlaceholder typePlaceholder, Menge<TypePlaceholder> toOneOfTheseTypes) {
return tiResult.getTypeOfPlaceholder(typePlaceholder, toOneOfTheseTypes);
}

View File

@ -39,6 +39,7 @@ import de.dhbwstuttgart.typeinference.FunVoidNInterface;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
import de.dhbwstuttgart.typeinference.exceptions.ParserError;
@ -317,12 +318,12 @@ public class MyCompiler implements MyCompilerAPI{
}
@Override
public Menge<ByteCodeResult> generateBytecode(Menge<SourceFile> m_AbstractSyntaxTree, TypeinferenceResultSet typeinferenceResult) {
public Menge<ByteCodeResult> generateBytecode(Menge<SourceFile> m_AbstractSyntaxTree, TypeinferenceResults typeinferenceResults) {
//SourceFile parsedFile = this.m_AbstractSyntaxTree.firstElement();
//Class parsedClass = parsedFile.KlassenVektor.firstElement();
Menge<ByteCodeResult> ret = new Menge<>();
for(SourceFile sf : m_AbstractSyntaxTree){
ret.addAll(sf.generateBytecode(typeinferenceResult));
ret.addAll(sf.generateBytecode(typeinferenceResults));
}
return ret;
}

View File

@ -15,6 +15,7 @@ import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
import de.dhbwstuttgart.typeinference.exceptions.ParserError;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
@ -127,6 +128,6 @@ public interface MyCompilerAPI
* Dafür müssen die Schritte Parsen und typeReconstruction ausgeführt werden.
* @return
*/
public Menge<ByteCodeResult> generateBytecode(Menge<SourceFile> m_AbstractSyntaxTree, TypeinferenceResultSet typeinferenceResult);
public Menge<ByteCodeResult> generateBytecode(Menge<SourceFile> m_AbstractSyntaxTree, TypeinferenceResults typeinferenceResults);
}
// ino.end

View File

@ -78,7 +78,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
* @param resultSet - Fehlende Typen im Syntaxbaum werden nach diesem ResultSet aufgelöst
* @return
*/
public ByteCodeResult genByteCode(TypeinferenceResultSet resultSet) {
public ByteCodeResult genByteCode(TypeinferenceResults typeinferenceResults) {
InstructionFactory _factory;
DHBWConstantPoolGen _cp;
ClassGenerator _cg;
@ -90,7 +90,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
if(pkgName != null)throw new NotImplementedException();
short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public
_cg = new ClassGenerator(name, this.getSuperClass(), name + ".java", constants , new String[] { }, resultSet); //letzter Parameter sind implementierte Interfaces
_cg = new ClassGenerator(name, this.getSuperClass(), name + ".java", constants , new String[] { }, typeinferenceResults); //letzter Parameter sind implementierte Interfaces
_cp = _cg.getConstantPool();
_factory = new DHBWInstructionFactory(_cg, _cp);

View File

@ -40,6 +40,7 @@ import de.dhbwstuttgart.typeinference.FunNMethod;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.assumptions.ClassAssumption;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
@ -1836,10 +1837,10 @@ public class SourceFile
* Bisher wird nur der Bytecode der Klassen generiert. Nicht der Interfaces.
* @return
*/
public Menge<ByteCodeResult> generateBytecode(TypeinferenceResultSet rs) {
public Menge<ByteCodeResult> generateBytecode(TypeinferenceResults results) {
Menge<ByteCodeResult> ret = new Menge<>();
for(Class cl : this.KlassenVektor){
ret.add(cl.genByteCode(rs));
ret.add(cl.genByteCode(results));
}
return ret;
}

View File

@ -26,6 +26,7 @@ import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeInsertable;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.unify.CSubstitutionGenVar;
@ -838,7 +839,7 @@ public class RefType extends ObjectType implements IMatchable
if(!combinedType.equals(getName().toString())){
Class generatedClass = ASTFactory.createClass(getCombinedType(cg), getGenericClassType(), null, null, new SourceFile());
cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())).getByteCode());
cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResults()).getByteCode());
}
return "L"+combinedType+";";

View File

@ -437,21 +437,21 @@ public class TypePlaceholder extends ObjectType
@Override
public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) {
Type resolvedType = cg.resolveTPH(this);
if(resolvedType instanceof TypePlaceholder){
Menge<Type> resolvedType = cg.resolveTPH(this);
if(resolvedType.firstElement() instanceof TypePlaceholder){
return DHBWInstructionFactory.createObjectType();
}
return resolvedType.getBytecodeType(cg);
return resolvedType.firstElement().getBytecodeType(cg);
}
@Override
public String getBytecodeSignature(ClassGenerator cg) {
Type resolvedType = cg.resolveTPH(this);
if(resolvedType instanceof TypePlaceholder){
cg.addUsedTPH((TypePlaceholder)resolvedType);
return new TypePlaceholderType((TypePlaceholder)resolvedType).getSignature();
Menge<Type> resolvedType = cg.resolveTPH(this);
if(resolvedType.firstElement() instanceof TypePlaceholder){
cg.addUsedTPH((TypePlaceholder)resolvedType.firstElement());
return new TypePlaceholderType((TypePlaceholder)resolvedType.firstElement()).getSignature();
}
return resolvedType.getBytecodeSignature(cg);
return resolvedType.firstElement().getBytecodeSignature(cg);
}
@Override

View File

@ -112,15 +112,6 @@ public class TypeinferenceResultSet
this.ownerOfResultSet.addTypeInsertPoints(ret,this.unifiedConstraints);
return ret;
}
/**
* Startet die Bytecodegenerierung dieser sung.
* Dabei wird die codegen-Methode der inferierten Klasse mit diesem ResultSet aufgerufen.
*/
public ByteCodeResult codegen(){
return this.ownerOfResultSet.genByteCode(this);
}
}
}
// ino.end

View File

@ -0,0 +1,21 @@
package de.dhbwstuttgart.typeinference;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class TypeinferenceResults {
public TypeinferenceResults() {
// TODO Auto-generated constructor stub
}
public TypeinferenceResults(Menge<TypeinferenceResultSet> typeReconstruction) {
// TODO Auto-generated constructor stub
}
public Menge<Type> getTypeOfPlaceholder(TypePlaceholder typePlaceholder, Menge<TypePlaceholder> toOneOfTheseTypes) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -20,6 +20,7 @@ import de.dhbwstuttgart.parser.JavaParser.yyException;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
public class SingleClassTester {
@ -32,7 +33,9 @@ public class SingleClassTester {
Menge<SourceFile> sourceFiles = new Menge<>();
sourceFiles.add(sf);
Menge<ByteCodeResult> bytecode = compiler.generateBytecode(sourceFiles, compiler.typeReconstruction(sourceFiles).firstElement());
TypeinferenceResults results = new TypeinferenceResults(compiler.typeReconstruction(sourceFiles));
Menge<ByteCodeResult> bytecode = compiler.generateBytecode(sourceFiles, results);
//System.out.println(bytecode);
ByteCodeResult result = bytecode.firstElement();

View File

@ -0,0 +1,17 @@
import java.util.Vector;
class AutoOverloading{
methode(v, x){
v.add(x);
}
methode(v, x){
v.add(x);
}
methode(){
methode(new Vector<String>(),"hallo");
methode(new Vector<Integer>(), 1);
}
}

View File

@ -0,0 +1,32 @@
package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.Test;
import bytecode.BytecodeTest;
public class AutoOverloadingTest extends BytecodeTest{
@Override
protected void init() {
testName = "AutoOverloading";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
}
@Test
public void testConstruct() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
assertTrue(true);
}
}

View File

@ -0,0 +1,11 @@
class OverloadingAddition{
methode(a){
return a+a;
}
methode(){
methode(1);
methode(1.1);
}
}

View File

@ -0,0 +1,32 @@
package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.Test;
import bytecode.BytecodeTest;
public class OverloadingAdditionTest extends BytecodeTest{
@Override
protected void init() {
testName = "OverloadingAddition";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
}
@Test
public void testConstruct() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
assertTrue(true);
}
}