GTV2TPH eingeführt. Beim Anfügen eines SingleConstraints werden GenericTypeVars nun durch Typeplaceholder ausgetauscht.

This commit is contained in:
JanUlrich 2014-03-28 18:36:08 +01:00
parent b432c74ec4
commit 8e7776dc4c
19 changed files with 9576 additions and 9080 deletions

View File

@ -323,7 +323,7 @@ compilationunit : typedeclarations
{
$$=$1;
}
/* |importdeclarations typedeclarations
|importdeclarations typedeclarations
{
$2.addImports($1);
$$=$2;
@ -346,7 +346,7 @@ compilationunit : typedeclarations
this.testPair.add(new Pair($1,$2));
$$=$3;
}
*/
packagedeclaration : PACKAGE name ';' ;
{
// SCJU: Package

View File

@ -5,6 +5,7 @@ package mycompiler;
// ino.module.SourceFile.8722.import
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
@ -690,6 +691,27 @@ public class SourceFile
}
}
//Alle Generischen Typvariablen in TPH umwandeln:
HashMap<GenericTypeVar,TypePlaceholder> gtv2tph = new HashMap<GenericTypeVar,TypePlaceholder>();
for(Pair pair : constraints){
if(pair.TA1 instanceof GenericTypeVar){
TypePlaceholder tph = gtv2tph.get(pair.TA1);
if(tph == null){
tph = TypePlaceholder.fresh();
gtv2tph.put((GenericTypeVar)pair.TA1, tph);
}
pair.TA1 = tph;
}
if(pair.TA2 instanceof GenericTypeVar){
TypePlaceholder tph = gtv2tph.get(pair.TA2);
if(tph == null){
tph = TypePlaceholder.fresh();
gtv2tph.put((GenericTypeVar)pair.TA2, tph);
}
pair.TA2 = tph;
}
}
//Erst die Unifizierung erstellen:
Vector<Pair> constraintsClone = (Vector<Pair>)constraints.clone();
Vector<Vector<Pair>> unifyResult = Unify.unify(constraintsClone, finiteClosure);

View File

@ -1250,6 +1250,10 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface
* @return
*/
public RefType getType() {
Vector<Type> parameter = new Vector<Type>();
for(Type param : this.get_ParaList()){
parameter.add(((GenericTypeVar)param).getTypePlaceHolder());//(TypePlaceholder.fresh()); //Hier ist kein ReplacementListener notwendig. Der Typ soll nie eingesetzt werden. Der TPH wird nur gebraucht, damit das Unifizieren funktioniert.
}
return new RefType(this.getName(), this.get_ParaList(), 0);
}

View File

@ -705,7 +705,9 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
public boolean equals(Object obj){
if(!(obj instanceof Method))return false;
Method equals = (Method) obj;
if(!this.returntype.equals(equals.returntype))return false;
if((this.returntype!=null && equals.returntype==null))return false;
if((this.returntype==null && equals.returntype!=null))return false;
if(this.returntype!=null && equals.returntype!=null)if(!this.returntype.equals(equals.returntype))return false;
if(!this.parameterlist.equals(equals.parameterlist))return false;
return super.equals(obj);
}

File diff suppressed because it is too large Load Diff

View File

@ -323,7 +323,7 @@ compilationunit : typedeclarations
{
$$=$1;
}
/* |importdeclarations typedeclarations
|importdeclarations typedeclarations
{
$2.addImports($1);
$$=$2;
@ -346,7 +346,7 @@ compilationunit : typedeclarations
this.testPair.add(new Pair($1,$2));
$$=$3;
}
*/
packagedeclaration : PACKAGE name ';' ;
{
// SCJU: Package

View File

@ -36,10 +36,16 @@ import org.apache.log4j.Logger;
import typinferenz.ConstraintsSet;
import typinferenz.FreshTypeVariable;
import typinferenz.JavaCodeResult;
import typinferenz.OderConstraint;
import typinferenz.ResultSet;
import typinferenz.UndConstraint;
import typinferenz.assumptions.FieldAssumption;
import typinferenz.assumptions.TypeAssumptions;
@ -103,19 +109,25 @@ public class InstVar extends Expr
super(offset,variableLength);
Iterator namen = ui.get_Name().iterator();
LocalOrFieldVar innerLOFV = new LocalOrFieldVar((String)namen.next(),getOffset());
innerLOFV.setType(TypePlaceholder.fresh(this));
//innerLOFV.setType(TypePlaceholder.fresh(this));
InstVar INSTVA = new InstVar(innerLOFV, (String)namen.next(),offset);
INSTVA.setType(TypePlaceholder.fresh(this));
//INSTVA.setType(TypePlaceholder.fresh(this));
while(namen.hasNext()) {
INSTVA = new InstVar(INSTVA, (String)namen.next(),offset);
INSTVA.setType(TypePlaceholder.fresh(this));
//INSTVA.setType(TypePlaceholder.fresh(this));
}
expr = INSTVA.expr;
usedid = INSTVA.usedid;
}
// ino.end
// ino.method.sc_check.25417.definition
@Override
public void parserPostProcessing(SyntaxTreeNode parent) {
super.parserPostProcessing(parent);
if(this.getType()==null)this.set_Type(TypePlaceholder.fresh(this));
}
// ino.method.sc_check.25417.definition
public void sc_check(Vector<Class> classname, Hashtable ch, Hashtable<String, String> bh, boolean ext, Hashtable parach, Hashtable<String, Hashtable> parabh)
// ino.end
// ino.method.sc_check.25417.body
@ -152,7 +164,7 @@ public class InstVar extends Expr
// ino.end
// ino.method.get_Name.25420.body
{
return null;
return this.usedid.get_Name_1Element();
}
// ino.end
@ -267,14 +279,27 @@ public class InstVar extends Expr
@Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet();
if(this.expr == null){
this.expr = new This(0, 0);
}
ret.add(expr.TYPEExpr(assumptions));
this.setType(TypePlaceholder.fresh(this));
return null;
OderConstraint oderConstraint = new OderConstraint();
for(FieldAssumption fa : assumptions.getFieldVars(this.get_Name())){
UndConstraint undConstraint = new UndConstraint();
undConstraint.addConstraint(fa.getAssumedType(),this.getType());
undConstraint.addConstraint(this.expr.getType(),fa.getParentClass().getType());
oderConstraint.addConstraint(undConstraint);
}
ret.add(oderConstraint);
return ret;
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet) {
return new JavaCodeResult().attach(this.expr.printJavaCode(resultSet)).attach("."+this.usedid.get_Name_1Element());
JavaCodeResult ret = new JavaCodeResult();
if(this.expr != null)ret.attach(this.expr.printJavaCode(resultSet)).attach(".");
return ret.attach(this.usedid.get_Name_1Element());
}
@Override

View File

@ -33,6 +33,7 @@ public class GenericTypeVar extends Type
// ino.method.GenericTypeVar.26509.defdescription type=line
// private Hashtable<String, Vector<GenericTypeVar>> m_TypeErasureList;
// ino.end
private TypePlaceholder tph;
/**
*
@ -134,6 +135,11 @@ public class GenericTypeVar extends Type
return new JavaCodeResult(this.name);
}
public TypePlaceholder getTypePlaceHolder() {
if(this.tph == null)this.tph = TypePlaceholder.fresh();
return this.tph;
}
}
// ino.end

View File

@ -87,7 +87,7 @@ public class RefType extends Type implements IMatchable
{
super(offset);
this.setName(fullyQualifiedName);
if(parameter != null && parameter.size()>0)this.parameter = parameter;
if(parameter != null && parameter.size()>0)this.set_ParaList(parameter);
}
// ino.end
@ -99,7 +99,7 @@ public class RefType extends Type implements IMatchable
super(offset);
// otth: Copy-Konstruktor
this.setName(R.getTypeName());
this.parameter = R.get_ParaList();
this.set_ParaList(R.get_ParaList());
}
// ino.end
@ -195,7 +195,20 @@ public class RefType extends Type implements IMatchable
//*/
}
// ino.end
/**
* Wandelt die Parameter des RefTypes in TPHs um, sofern es sich um Generische Variablen handelt.
* @return
*/
public void GTV2TPH(){
Vector<Type> paralist = new Vector<Type>();
if(this.get_ParaList()!=null)for(Type t : this.get_ParaList()){
if(t instanceof GenericTypeVar)paralist.add(((GenericTypeVar)t).getTypePlaceHolder());
else paralist.add(t);
}
this.set_ParaList(paralist);
}
// ino.method.setName.26655.definition
public void setName( String name )
// ino.end
@ -241,14 +254,25 @@ public class RefType extends Type implements IMatchable
}
// ino.end
/**
* Fügt eine Parameterliste an und tauscht zuvor alle GenerictTypeVars durch TPH aus.
* In einem RefType dürfen keine GTVs enthalten sein.
* @param v
*/
// ino.method.set_ParaList.26661.definition
public void set_ParaList(Vector<Type> v)
// ino.end
// ino.method.set_ParaList.26661.body
{
this.parameter = v;
parserlog.debug("T->Type.java->set_ParaList->parameter: " + parameter);
parserlog.debug("T->Type.java->get_Type: " + getName());
/*
Vector<Type> paralist = new Vector<Type>();
if(v!=null)for(Type t : v){
if(t instanceof GenericTypeVar)paralist.add(((GenericTypeVar)t).getTypePlaceHolder());
else paralist.add(t);
}*/
this.parameter = v;//paralist;
parserlog.debug("T->Type.java->set_ParaList->parameter: " + parameter);
parserlog.debug("T->Type.java->get_Type: " + getName());
}
// ino.end

View File

@ -24,8 +24,10 @@ public class FunNInterface extends Class{
super("Fun"+N);
Vector<Type> paralist = new Vector<Type>();
paralist.add(new GenericTypeVar("R",0));
//paralist.add(TypePlaceholder.fresh());
for(int i = 1; i<=N;i++){
paralist.add(new GenericTypeVar("T"+i,0));
//paralist.add(TypePlaceholder.fresh());
}
this.set_ParaList(paralist);
}

View File

@ -3,6 +3,7 @@ package typinferenz;
import java.util.Vector;
import typinferenz.exceptions.TypinferenzException;
import mycompiler.mytype.GenericTypeVar;
import mycompiler.mytype.Pair;
import mycompiler.mytype.RefType;
import mycompiler.mytype.Type;
@ -47,10 +48,20 @@ public class SingleConstraint extends UndConstraint{
Type p1 = toAdd.TA1;
Type p2 = toAdd.TA2;
if(p1==null || p2 == null)throw new NullPointerException();
//Hier werden die GTVs zu TPH gewandelt.
if(p1 instanceof RefType)((RefType)p1).GTV2TPH();
if(p2 instanceof RefType)((RefType)p2).GTV2TPH();
if((p1 instanceof GenericTypeVar))
p1 = ((GenericTypeVar)p1).getTypePlaceHolder();
if((p2 instanceof GenericTypeVar))
p2 = ((GenericTypeVar)p2).getTypePlaceHolder();
// BaseTypes werden in RefTypes umgewandelt. Constraints dürfen nur RefTypes oder TypePlaceholder enthalten, da sonst der Unify-Algorithmus nicht funktioniert.
if(!(p1 instanceof RefType) && !(p1 instanceof TypePlaceholder))p1 = new RefType(p1);
if(!(p2 instanceof RefType) && !(p2 instanceof TypePlaceholder))p2 = new RefType(p2);
if(!(p1 instanceof RefType) && !(p1 instanceof TypePlaceholder) && !(p1 instanceof GenericTypeVar))p1 = new RefType(p1);
if(!(p2 instanceof RefType) && !(p2 instanceof TypePlaceholder) && !(p2 instanceof GenericTypeVar))p2 = new RefType(p2);
//if(!(TypePlaceholder.class.isInstance(p1)) || !(RefType.class.isInstance(p1)) || !(TypePlaceholder.class.isInstance(p2)) || !(RefType.class.isInstance(p2)))
//{//Wenn die beiden übergebenen Typen weder RefTypes noch TypePlaceholder sind:

View File

@ -31,7 +31,8 @@ public class MethodAssumption extends FieldAssumption {
* @return
*/
public Type getParameterType(int i) {
return this.method.getParameterList().getParameterAt(i).getType();
Type ret = this.method.getParameterList().getParameterAt(i).getType();
return ret;
}
public Type getParentClassType() {

View File

@ -93,15 +93,19 @@ public class TypeAssumptions {
* @param withName
* @return
*/
@Deprecated
public Vector<FieldAssumption> getFieldVars2(String withName){
//TODO: Implementieren
return new Vector<FieldAssumption>();
public Vector<FieldAssumption> getFieldVars(String withName){
Vector<FieldAssumption> ret = new Vector<FieldAssumption>();
for(FieldAssumption fa : this.fieldAssumptions){
if(fa.getIdentifier().equals(withName))ret.add(fa);
}
return ret;
}
@Deprecated
public Type getTypeOfFieldVar2(String withName, Class inClass){
//TODO: Implementieren
return null;
public Type getTypeOfFieldVar(String withName, Class inClass){
for(FieldAssumption fa : this.getFieldVars(withName)){
if(fa.getParentClass().equals(inClass))return fa.getAssumedType();
}
throw new TypinferenzException("Das Feld "+withName+" ist in der Klasse "+inClass.getName()+" nicht vorhanden.");
}
/**

View File

@ -29,6 +29,7 @@ public class GeneralParserTest{
public void run(){
Vector<String> filenames = new Vector<String>();
filenames.add("FieldInitializationTest.jav");
filenames.add("ImportTest.jav");
MyCompilerAPI compiler = MyCompiler.getAPI();
try{
for(String filename : filenames)

View File

@ -0,0 +1,4 @@
import java.util.*;
class ImportTest{
}

View File

@ -0,0 +1,11 @@
class GTVTest<GTV1>{
GTVTest2<String> var;
methode(){
return var.var2;
}
}
class GTVTest2<GTV2>{
GTV2 var2;
}

View File

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

View File

@ -11,7 +11,7 @@ public class LambdaTest1 {
@Test
public void run(){
Vector<String> mustContain = new Vector<String>();
mustContain.add("Fun0<Fun1<String, Fun2<LambdaTest, String>>> op");
mustContain.add("Fun0<Fun1<String, Fun2<G, LambdaTest, String>>> op");
MultipleTypesInsertTester.test(this.TEST_FILE, mustContain);
}

File diff suppressed because it is too large Load Diff