JavaPatternMatching/src/typinferenz/SingleConstraint.java

77 lines
3.1 KiB
Java
Raw Normal View History

2013-10-18 11:33:46 +00:00
package typinferenz;
import java.util.Vector;
2014-04-15 12:56:20 +00:00
import typinferenz.exceptions.DebugException;
import typinferenz.exceptions.TypeinferenceException;
import mycompiler.mytype.GenericTypeVar;
2013-10-18 11:33:46 +00:00
import mycompiler.mytype.Pair;
import mycompiler.mytype.RefType;
import mycompiler.mytype.Type;
import mycompiler.mytype.TypePlaceholder;
import mycompiler.mytypereconstruction.CTriple;
import mycompiler.mytypereconstruction.set.CTripleSet;
/**
* Beschreibung von Herrn Plümicke:
* "The set of constraints consists of constraints of the form θ R θ' , where θ and
* θ' are Java types and R (R { < , <? , = }) is a subtyping condition."
*
* @author AI10023 - Andreas Stadelmeier
*
* Die Klasse stellt ein OderConstraint-Set dar, welches nur aus einem Constraint besteht.
*
*/
public class SingleConstraint extends UndConstraint{
private Pair constraintPair; //entspricht θ condition θ'
//private R condition; //entspricht der condition (R)
public SingleConstraint(Type p1, Type p2){
//super(p1,p2);
Pair constraintPair = new Pair(p1,p2);//super.getConstraintPairs().firstElement();
this.addConstraint(constraintPair);
}
public Pair getPair(){
return constraintPair;
}
@Override //Methode überschreiben, damit immer nur ein Vector mit nur einem Element zurückgeliefert wird.
public Vector<Pair> getConstraintPairs(){
Vector<Pair> ret = new Vector<Pair>();
ret.add(constraintPair);
return ret;
}
public void addConstraint(Pair toAdd){
2014-04-15 12:56:20 +00:00
if(constraintPair != null)throw new DebugException("Ein Constraint darf nur aus einem ConstraintPair bestehen. Das hinzufügen von "+ toAdd + " ist nicht möglich.");
2013-10-18 11:33:46 +00:00
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();
2014-06-18 09:30:14 +00:00
if((p1 instanceof GenericTypeVar))p1 = ((GenericTypeVar)p1).getTypePlaceHolder();//throw new DebugException("GenericTypeVar sind in den Constraints nicht erlaubt");//
if((p2 instanceof GenericTypeVar))p2 = ((GenericTypeVar)p2).getTypePlaceHolder();//throw new DebugException("GenericTypeVar sind in den Constraints nicht erlaubt");//
2013-10-18 11:33:46 +00:00
// 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 instanceof GenericTypeVar))p1 = new RefType(p1);
if(!(p2 instanceof RefType) && !(p2 instanceof TypePlaceholder) && !(p2 instanceof GenericTypeVar))p2 = new RefType(p2);
2013-10-18 11:33:46 +00:00
//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:
// throw new TypinferenzException("Ein Constraint darf nur aus TypePlaceholdern und Reftypes bestehen");
//}
constraintPair = new Pair(p1,p2);
}
@Override
public String toString(){
return ""+constraintPair.TA1.toString()+" < "+constraintPair.TA2.toString();
}
}