GenericVarAssumption eingeführt

This commit is contained in:
JanUlrich 2014-06-18 09:06:08 +02:00
parent 4a67a4a58e
commit 5979835783
13 changed files with 190 additions and 8 deletions

View File

@ -16,7 +16,7 @@ import mycompiler.mytype.Pair;
import mycompiler.mytype.Type;
import mycompiler.mytype.TypePlaceholder;
public abstract class SyntaxTreeNode {
public abstract class SyntaxTreeNode{
protected SyntaxTreeNode parent;

View File

@ -546,6 +546,14 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
public ConstraintsSet TYPE(TypeAssumptions ass) {
ConstraintsSet ret = new ConstraintsSet();
TypeAssumptions localAss = new TypeAssumptions();
localAss.add(ass); //Die globalen Assumptions anhängen
//Generische Parameterdeklarationen den Assumptions anfügen:
for(GenericTypeVar gtv : this.genericMethodParameters){
ret.add(gtv.TYPE(localAss));
}
//TypeCheck, falls es sich um einen RefType handelt:
if(this.returntype!=null && (this.returntype instanceof RefType)&&
!(this.returntype instanceof mycompiler.mytype.Void)){//Sonderfall der Methode: Ihr Typ darf Void definiert werden.
@ -554,9 +562,6 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
if(replaceType == null)throw new TypeinferenceException("Der Typ "+this.getType().getName()+" ist nicht korrekt",this);
this.returntype = replaceType;
}
ConstraintsSet ret = new ConstraintsSet();
TypeAssumptions localAss = new TypeAssumptions();
localAss.add(ass); //Die globalen Assumptions anhängen
//Die Parameter zu den Assumptions hinzufügen:
if(this.parameterlist!=null)for(FormalParameter param : this.parameterlist){

View File

@ -14,12 +14,18 @@ import java.util.Vector;
import mycompiler.mytypereconstruction.replacementlistener.CReplaceTypeEvent;
import mycompiler.mytypereconstruction.replacementlistener.ITypeReplacementListener;
import typinferenz.ConstraintsSet;
import typinferenz.JavaCodeResult;
import typinferenz.ResultSet;
import typinferenz.SingleConstraint;
import typinferenz.TypeInsertPoint;
import typinferenz.TypeInsertable;
import typinferenz.assumptions.TypeAssumptions;
// ino.class.GenericTypeVar.26505.description type=javadoc
@ -36,6 +42,7 @@ public class GenericTypeVar extends Type
{
Type genericTypeVar;
Vector<Type> extendVars = new Vector<Type>();
private Pair genericConstraint;
/**
* Eine Registry f<EFBFBD>r alle Generic-Instanzen, die vor der Bytecode-Generierung durch
* Ihre Superklasse ersetzt werden m<EFBFBD>ssen. Siehe "Type Erasure" in Sun Spezifikation.
@ -54,6 +61,9 @@ public class GenericTypeVar extends Type
super(offset);
genericTypeVar = genericTypeVarExtendsVar.TA1;
if(genericTypeVarExtendsVar.TA2!=null)this.extendVars.add(genericTypeVarExtendsVar.TA2);
else{
this.genericConstraint = genericTypeVarExtendsVar;
}
this.name = genericTypeVar.toString();
}
@ -182,6 +192,13 @@ public class GenericTypeVar extends Type
return this.name;
}
public ConstraintsSet TYPE(TypeAssumptions ass){
ConstraintsSet ret = new ConstraintsSet();
if(this.genericConstraint != null)ret.add(new SingleConstraint(this.genericConstraint.TA1, this.genericConstraint.TA2));
ass.addGenericVarAssumption(this);
return ret;
}
}
// ino.end

View File

@ -17,6 +17,7 @@ import mycompiler.mytype.TypePlaceholder;
* Diese müssen gemeinsam eingesetzt werden.
* Das TypeInsertSet löst zudem Abhängigkeiten auf. Wird eine Generische Variable eingesetzt,
* müssen alle mit ihr in Verbindung stehenden Typen ebenfalls eingesetzt werden.
* TODO: Es müssen eigentlich nur die generischen Variablendeklarationen eingesetzt werden.
* @author janulrich
*
*/
@ -30,8 +31,6 @@ public class TypeInsertSet {
/**
* Fügt einen TypeInsertPoint dem TypeInsertSet hinzu.
* Dabei werden alle involvierten TPHs berechnet und zurückgeliefert.
* Die von diesen TPHs abhängigen Typen müssen anschließend ebenfalls dem TypeInsertSet angefügt werden.
* @param typeInsertPoint
* @return
*/
@ -46,6 +45,26 @@ public class TypeInsertSet {
if(!this.genericTypeInsertPoints.contains(typeInsertPoint))this.genericTypeInsertPoints.add(typeInsertPoint);
}
/**
* Fügt nur einen einzelnen TypeInsertPoint in fileContent ein.
* @param tip
* @param fileContent
* @return
*/
public String insertType(TypeInsertPoint tip, String fileContent){
TypeInsertSet tis = new TypeInsertSet();
int additionalOffset = 0;
String ret = fileContent;
JavaCodeResult unresolvedTPHs = tip.insertType(ret, additionalOffset);
for(TypePlaceholder tph : unresolvedTPHs.getUnresolvedTPH()){
GenericTypeInsertPoint genericTIP = new GenericTypeInsertPoint((TypeInsertable)tip.getGenericTypeVarInsertNode(),tph,tip.getResultSet());
tis.add(genericTIP);
}
tis.add(tip);
return tis.insertAllTypes(fileContent);
}
/**
* Fügt alle Typen dieses TypeInsertSets in den übergebenen Quellcode ein
* TODO: Beim Einsetzen eines Typs alle Abhängigkeiten auflösen. Benutze Generische Variablen müssen an allen Punkte eingesetzt werden

View File

@ -9,6 +9,12 @@ public class Assumption {
private TypeInsertable typable;
/**
* Wird dieser Konstruktor benutzt müssen alle Methoden der Klasse Assumption überschrieben werden.
*/
protected Assumption(){
}
public Assumption(TypeInsertable ass){
this.typable = ass;
}

View File

@ -0,0 +1,24 @@
package typinferenz.assumptions;
import mycompiler.mystatement.LocalVarDecl;
import mycompiler.mytype.GenericTypeVar;
import mycompiler.mytype.RefType;
import mycompiler.mytype.Type;
public class GenericVarAssumption extends Assumption{
private GenericTypeVar genericVar;
public GenericVarAssumption(GenericTypeVar genericVar){
super();
this.genericVar = genericVar;
}
public Type getAssumedType() {
return new RefType(genericVar.getTypePlaceHolder().getName(), -1);
}
public String getIdentifier(){
return genericVar.getTypePlaceHolder().getName();
}
}

View File

@ -52,6 +52,7 @@ public class TypeAssumptions {
private Vector<FieldAssumption> fieldAssumptions = new Vector<FieldAssumption>();
private Vector<LocalVarAssumption> localVarAssumptions = new Vector<LocalVarAssumption>();
private Vector<ParameterAssumption> parameterAssumptions = new Vector<ParameterAssumption>();
private Vector<GenericVarAssumption> genericVarAssumptions = new Vector<GenericVarAssumption>();
private Vector<ClassAssumption> classAssumptions = new Vector<ClassAssumption>();
/**
@ -165,7 +166,11 @@ public class TypeAssumptions {
for(LocalVarAssumption ass : this.localVarAssumptions){
if(ass.getIdentifier().equals(variableName))return ass.getAssumedType();
}
//Ebenso wie die Generischen Variablen:
for(GenericVarAssumption ass : this.genericVarAssumptions){
}
//und zuletzt die Felder der Klasse in dessen Namensraum sich dieses AssumptionSet befindet.
if(inScope!=null){
for(FieldAssumption ass : this.getAssumptionsFor(inScope.getName())){
@ -218,6 +223,7 @@ public class TypeAssumptions {
for(Assumption f : this.fieldAssumptions)ret.add(f);
for(Assumption f : this.parameterAssumptions)ret.add(f);
for(Assumption f : this.constructorAssumptions)ret.add(f);
for(Assumption f : this.genericVarAssumptions)ret.add(f);
return ret;
}
@ -233,6 +239,7 @@ public class TypeAssumptions {
if(ass instanceof LocalVarAssumption)if(!this.localVarAssumptions.contains(ass))this.localVarAssumptions.add((LocalVarAssumption)ass);//this.localVarAssumptions.add((LocalVarAssumption)ass);//
if(ass instanceof ParameterAssumption)if(!this.parameterAssumptions.contains(ass))this.parameterAssumptions.add((ParameterAssumption)ass);//this.parameterAssumptions.add((ParameterAssumption)ass);
if(ass instanceof FieldAssumption)if(!this.fieldAssumptions.contains(ass))this.fieldAssumptions.add((FieldAssumption)ass);//this.fieldAssumptions.add((FieldAssumption)ass);
if(ass instanceof GenericVarAssumption)if(!this.genericVarAssumptions.contains(ass))this.genericVarAssumptions.add((GenericVarAssumption)ass);
}
@ -273,6 +280,7 @@ public class TypeAssumptions {
ret += "FieldVar Assumptions:\n" + this.fieldAssumptions.toString() + "\n";
ret += "LocalVar Assumptions:\n" + this.localVarAssumptions.toString() + "\n";
ret += "Parameter Assumptions:\n" + this.parameterAssumptions.toString() + "\n";
ret += "Generic Var Assumptions:\n" + this.genericVarAssumptions.toString() + "\n";
ret += "Konstruktor Assumptions:\n" + this.constructorAssumptions.toString() + "\n";
ret += "Class Assumptions:\n" + this.classAssumptions.toString() + "\n";
//return assumptions.toString();
@ -287,7 +295,6 @@ public class TypeAssumptions {
* @return null, falls der Typ nicht vorhanden ist.
*/
public Type getTypeFor(RefType t){
//TODO: Die Parameterliste noch kontrollieren: (hier könnte es Constraints geben: "? extends String")
//Alle bekannten Klassen nach diesem Typ durchsuchen:
String typName = t.getName();
String[] names = typName.split("[.]");
@ -308,6 +315,11 @@ public class TypeAssumptions {
return ret;
}
}
//Auch die generischen Variablen durchsuchen:
for(GenericVarAssumption ass : this.genericVarAssumptions){
if(ass.getIdentifier().equals(t.getName()))return ass.getAssumedType();
}
return null;
}
@ -335,6 +347,11 @@ public class TypeAssumptions {
return null;
}
public void addGenericVarAssumption(
GenericTypeVar genericTypeVar) {
this.genericVarAssumptions.add(new GenericVarAssumption(genericTypeVar));
}
/**
* Prüft einen Typ auf das vorhandensein in den BasicAssumptions.
* Dabei werden alle Konstruktoren nach diesem Typ durchsucht. Denn jede Klasse hat einen Konstruktor und der muss in den TypeAssumptions vorhanden sein.

View File

@ -31,6 +31,7 @@ public class GeneralParserTest{
filenames.add("FieldInitializationTest.jav");
filenames.add("ImportTest.jav");
filenames.add("BoundedParameter.jav");
filenames.add("GenericFieldVarTest.jav");
MyCompilerAPI compiler = MyCompiler.getAPI();
try{
for(String filename : filenames)

View File

@ -0,0 +1,3 @@
class Test{
<A> A var;
}

View File

@ -0,0 +1,60 @@
package plugindevelopment;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Vector;
import org.junit.Test;
import junit.framework.TestCase;
import mycompiler.MyCompiler;
import mycompiler.MyCompilerAPI;
import mycompiler.myparser.JavaParser.yyException;
import mycompiler.mytypereconstruction.TypeinferenceResultSet;
import typinferenz.TypeInsertPoint;
import typinferenz.TypeInsertSet;
public class InsertSingleTypeTest {
private static final String testFile = "SingleTypeInsertTest.jav";
@Test
public void test1(){
TypeinferenceResultSet res = test(testFile);
TypeInsertPoint p = res.getTypeInsertionPoints().points.firstElement();
try {
System.out.println(res.getTypeInsertionPoints().insertType(p, this.getFileContent(rootDirectory + testFile)));
} catch (IOException e) {
TestCase.fail();
e.printStackTrace();
}
}
static final String rootDirectory = System.getProperty("user.dir")+"/test/plugindevelopment/";
public static TypeinferenceResultSet test(String sourceFileToInfere){
String inferedSource = "";
MyCompilerAPI compiler = MyCompiler.getAPI();
try {
compiler.parse(new File(rootDirectory + sourceFileToInfere));
Vector<TypeinferenceResultSet> results = compiler.typeReconstruction();
TestCase.assertTrue("Es darf nicht mehr als eine Lösungsmöglichkeit geben und nicht "+results.size(), results.size()==1);
return results.firstElement();
} catch (IOException | yyException e) {
e.printStackTrace();
TestCase.fail();
return null;
}
}
//Source: https://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
//PS: benötigt Java 7
public static String getFileContent(String path)throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(encoded)).toString();
}
}

View File

@ -0,0 +1,3 @@
class Test{
var;
}

View File

@ -0,0 +1,9 @@
class Test{
m;
<CT> CT methode(){
return m;
}
}

View File

@ -0,0 +1,18 @@
package plugindevelopment.TypeInsertTests;
import java.util.Vector;
import org.junit.Test;
public class LambdaTest10 {
private static final String TEST_FILE = "LambdaTest10.jav";
@Test
public void run(){
Vector<String> mustContain = new Vector<String>();
//mustContain.add("A a");
MultipleTypesInsertTester.test(this.TEST_FILE, mustContain);
}
}