diff --git a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java index f7f5e711..54246610 100644 --- a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java +++ b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java @@ -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 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 usedTypes){ - Type t = resolveTPH(toTPH, usedTypes); + public org.apache.commons.bcel6.generic.Type getNearestUsedType(Type t, Menge 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 resolveTPH(TypePlaceholder typePlaceholder) { return resolveTPH(typePlaceholder, null); } - public Type resolveTPH(TypePlaceholder typePlaceholder, Menge toOneOfTheseTypes) { + public Menge resolveTPH(TypePlaceholder typePlaceholder, Menge toOneOfTheseTypes) { return tiResult.getTypeOfPlaceholder(typePlaceholder, toOneOfTheseTypes); } diff --git a/src/de/dhbwstuttgart/core/MyCompiler.java b/src/de/dhbwstuttgart/core/MyCompiler.java index 60ea5bb2..1f39fb97 100755 --- a/src/de/dhbwstuttgart/core/MyCompiler.java +++ b/src/de/dhbwstuttgart/core/MyCompiler.java @@ -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 generateBytecode(Menge m_AbstractSyntaxTree, TypeinferenceResultSet typeinferenceResult) { + public Menge generateBytecode(Menge m_AbstractSyntaxTree, TypeinferenceResults typeinferenceResults) { //SourceFile parsedFile = this.m_AbstractSyntaxTree.firstElement(); //Class parsedClass = parsedFile.KlassenVektor.firstElement(); Menge ret = new Menge<>(); for(SourceFile sf : m_AbstractSyntaxTree){ - ret.addAll(sf.generateBytecode(typeinferenceResult)); + ret.addAll(sf.generateBytecode(typeinferenceResults)); } return ret; } diff --git a/src/de/dhbwstuttgart/core/MyCompilerAPI.java b/src/de/dhbwstuttgart/core/MyCompilerAPI.java index f0eac42f..fabf2499 100755 --- a/src/de/dhbwstuttgart/core/MyCompilerAPI.java +++ b/src/de/dhbwstuttgart/core/MyCompilerAPI.java @@ -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 generateBytecode(Menge m_AbstractSyntaxTree, TypeinferenceResultSet typeinferenceResult); + public Menge generateBytecode(Menge m_AbstractSyntaxTree, TypeinferenceResults typeinferenceResults); } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 87abe745..0332ce13 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -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); diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index b7185c82..06fdd8f6 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -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 generateBytecode(TypeinferenceResultSet rs) { + public Menge generateBytecode(TypeinferenceResults results) { Menge ret = new Menge<>(); for(Class cl : this.KlassenVektor){ - ret.add(cl.genByteCode(rs)); + ret.add(cl.genByteCode(results)); } return ret; } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 5b877197..26b1806e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -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+";"; diff --git a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index 4b48e9c5..1f9d2621 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -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 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 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 diff --git a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java index 5dbf463c..913d9d6b 100755 --- a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java +++ b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java @@ -112,15 +112,6 @@ public class TypeinferenceResultSet this.ownerOfResultSet.addTypeInsertPoints(ret,this.unifiedConstraints); return ret; - } - - /** - * Startet die Bytecodegenerierung dieser Lösung. - * Dabei wird die codegen-Methode der inferierten Klasse mit diesem ResultSet aufgerufen. - */ - public ByteCodeResult codegen(){ - return this.ownerOfResultSet.genByteCode(this); - } - + } } // ino.end diff --git a/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java b/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java new file mode 100644 index 00000000..7cce5b67 --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java @@ -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 typeReconstruction) { + // TODO Auto-generated constructor stub + } + + public Menge getTypeOfPlaceholder(TypePlaceholder typePlaceholder, Menge toOneOfTheseTypes) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/test/bytecode/SingleClassTester.java b/test/bytecode/SingleClassTester.java index 46693780..7b336bfb 100644 --- a/test/bytecode/SingleClassTester.java +++ b/test/bytecode/SingleClassTester.java @@ -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 sourceFiles = new Menge<>(); sourceFiles.add(sf); - Menge bytecode = compiler.generateBytecode(sourceFiles, compiler.typeReconstruction(sourceFiles).firstElement()); + TypeinferenceResults results = new TypeinferenceResults(compiler.typeReconstruction(sourceFiles)); + + Menge bytecode = compiler.generateBytecode(sourceFiles, results); //System.out.println(bytecode); ByteCodeResult result = bytecode.firstElement(); diff --git a/test/bytecode/types/AutoOverloading.jav b/test/bytecode/types/AutoOverloading.jav new file mode 100644 index 00000000..d4b74752 --- /dev/null +++ b/test/bytecode/types/AutoOverloading.jav @@ -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(),"hallo"); + methode(new Vector(), 1); + } +} \ No newline at end of file diff --git a/test/bytecode/types/AutoOverloadingTest.java b/test/bytecode/types/AutoOverloadingTest.java new file mode 100644 index 00000000..a25df619 --- /dev/null +++ b/test/bytecode/types/AutoOverloadingTest.java @@ -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); + } +} diff --git a/test/bytecode/types/OverloadingAddition.jav b/test/bytecode/types/OverloadingAddition.jav new file mode 100644 index 00000000..c7196c15 --- /dev/null +++ b/test/bytecode/types/OverloadingAddition.jav @@ -0,0 +1,11 @@ +class OverloadingAddition{ + + methode(a){ + return a+a; + } + + methode(){ + methode(1); + methode(1.1); + } +} \ No newline at end of file diff --git a/test/bytecode/types/OverloadingAdditionTest.java b/test/bytecode/types/OverloadingAdditionTest.java new file mode 100644 index 00000000..0830a656 --- /dev/null +++ b/test/bytecode/types/OverloadingAdditionTest.java @@ -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); + } +}