Add the placeholder mapping

This commit is contained in:
Till Schnell 2021-03-27 18:01:28 +01:00
parent 9d2c85d686
commit ad7e7ec42b
4 changed files with 101 additions and 53 deletions

View File

@ -0,0 +1,55 @@
package de.dhbwstuttgart.inferWildcards;
import java.util.HashSet;
import java.util.Set;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
public final class ConstraintsGenerationUtils
{
private ConstraintsGenerationUtils () {
throw new AssertionError("No ConstraintsGenerationUtils instance for you");
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static ConstraintSet generateConstraints (JavaClassName name, TypePlaceholder tph) {
ConstraintSet constraintSet = new ConstraintSet<>();
Set<Constraint<?>> oderConstraints = new HashSet<>();
constraintSet.addOderConstraint(oderConstraints);
Constraint<?> c = new Constraint<>();
oderConstraints.add(c);
// single type
RefType refType = new RefType(name, new NullToken());
ConstraintsGenerationUtils.addToConstraint(c, tph, refType);
// extends type
ExtendsWildcardType extendsWildcardType = new ExtendsWildcardType(refType, new NullToken());
ConstraintsGenerationUtils.addToConstraint(c, tph, extendsWildcardType);
// super type
SuperWildcardType superWildcardType = new SuperWildcardType(refType, new NullToken());
ConstraintsGenerationUtils.addToConstraint(c, tph, superWildcardType);
return constraintSet;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void addToConstraint (Constraint c, TypePlaceholder tph, RefTypeOrTPHOrWildcardOrGeneric type) {
Pair pair = new Pair(tph, type, PairOperator.EQUALSDOT);
c.add(pair);
}
}

View File

@ -1,6 +1,8 @@
package de.dhbwstuttgart.inferWildcards; package de.dhbwstuttgart.inferWildcards;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import com.sun.source.tree.IdentifierTree; import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.ParameterizedTypeTree; import com.sun.source.tree.ParameterizedTypeTree;
@ -10,6 +12,9 @@ import com.sun.source.tree.TypeParameterTree;
import com.sun.source.tree.VariableTree; import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreeScanner; import com.sun.source.util.TreeScanner;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
public class InferWildcardsVisitor public class InferWildcardsVisitor
@ -18,10 +23,18 @@ public class InferWildcardsVisitor
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private final ConstraintSet constraintsSet; private final ConstraintSet constraintsSet;
private final Map<TypePlaceholder, Tree> placeholderMapping;
public InferWildcardsVisitor () { public InferWildcardsVisitor () {
super(); super();
this.constraintsSet = new ConstraintSet<>(); this.constraintsSet = new ConstraintSet<>();
this.placeholderMapping = new HashMap<>();
}
private TypePlaceholder generateTypePlaceholder (Tree t) {
TypePlaceholder tph = TypePlaceholder.fresh(new NullToken());
placeholderMapping.put(tph, t);
return tph;
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@ -31,8 +44,16 @@ public class InferWildcardsVisitor
List<? extends Tree> typeArguments = node.getTypeArguments(); List<? extends Tree> typeArguments = node.getTypeArguments();
typeArguments.forEach(t -> { typeArguments.forEach(t -> {
ConstraintSet s = t.accept(new TypeParamVisitor(), null); // generate Type Placeholder
constraintsSet.addAll(s); TypePlaceholder tph = generateTypePlaceholder(t);
// Search for the type parameter
List<JavaClassName> foundClassNames = t.accept(new TypeParamVisitor(), null);
foundClassNames.forEach(name -> {
ConstraintSet constraints = ConstraintsGenerationUtils.generateConstraints(name, tph);
constraintsSet.addAll(constraints);
});
}); });
return super.visitParameterizedType(node, p); return super.visitParameterizedType(node, p);
@ -71,4 +92,8 @@ public class InferWildcardsVisitor
public ConstraintSet getConstraintSet() { public ConstraintSet getConstraintSet() {
return constraintsSet; return constraintsSet;
} }
public Map<TypePlaceholder, Tree> getPlaceholderMapping () {
return placeholderMapping;
}
} }

View File

@ -1,71 +1,30 @@
package de.dhbwstuttgart.inferWildcards; package de.dhbwstuttgart.inferWildcards;
import java.util.HashSet; import java.util.ArrayList;
import java.util.Set; import java.util.List;
import com.sun.source.tree.IdentifierTree; import com.sun.source.tree.IdentifierTree;
import com.sun.source.util.TreeScanner; import com.sun.source.util.TreeScanner;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
@SuppressWarnings("rawtypes")
public class TypeParamVisitor public class TypeParamVisitor
extends TreeScanner<ConstraintSet, Void> extends TreeScanner<List<JavaClassName>, Void>
{ {
public TypeParamVisitor () { public TypeParamVisitor () {
} }
@SuppressWarnings("unchecked")
private static ConstraintSet generateConstraints (IdentifierTree type) {
ConstraintSet constraintSet = new ConstraintSet<>();
Set<Constraint<?>> oderConstraints = new HashSet<>();
constraintSet.addOderConstraint(oderConstraints);
Constraint<?> c = new Constraint<>(); @Override
oderConstraints.add(c); public List<JavaClassName> visitIdentifier (IdentifierTree node, Void p) {
ArrayList<JavaClassName> list = new ArrayList<>();
// generate Type Placeholder list.add(new JavaClassName(node.getName().toString()));
TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); return list;
// single type
RefType refType = new RefType(new JavaClassName(type.getName().toString()), new NullToken());
TypeParamVisitor.addToConstraint(c, tph, refType);
// extends type
ExtendsWildcardType extendsWildcardType = new ExtendsWildcardType(refType, new NullToken());
TypeParamVisitor.addToConstraint(c, tph, extendsWildcardType);
// super type
SuperWildcardType superWildcardType = new SuperWildcardType(refType, new NullToken());
TypeParamVisitor.addToConstraint(c, tph, superWildcardType);
return constraintSet;
}
@SuppressWarnings({ "unchecked" })
private static void addToConstraint (Constraint c, TypePlaceholder tph, RefTypeOrTPHOrWildcardOrGeneric type) {
Pair pair = new Pair(tph, type, PairOperator.EQUALSDOT);
c.add(pair);
} }
@Override @Override
public ConstraintSet visitIdentifier (IdentifierTree node, Void p) { public List<JavaClassName> reduce (List<JavaClassName> r1, List<JavaClassName> r2) {
return TypeParamVisitor.generateConstraints(node);
}
@Override
public ConstraintSet reduce (ConstraintSet r1, ConstraintSet r2) {
r1.addAll(r2); r1.addAll(r2);
return r1; return r1;
} }

View File

@ -3,6 +3,7 @@ package inferWildcards;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map;
import javax.tools.JavaCompiler; import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
@ -13,9 +14,11 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.JavacTask; import com.sun.source.util.JavacTask;
import de.dhbwstuttgart.inferWildcards.InferWildcardsVisitor; import de.dhbwstuttgart.inferWildcards.InferWildcardsVisitor;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
public class TestInferWildcards public class TestInferWildcards
@ -48,9 +51,15 @@ public class TestInferWildcards
InferWildcardsVisitor inferWildcardsVisitor = new InferWildcardsVisitor(); InferWildcardsVisitor inferWildcardsVisitor = new InferWildcardsVisitor();
t.accept(inferWildcardsVisitor, null); t.accept(inferWildcardsVisitor, null);
// Generated Constraints
ConstraintSet constraintSet = inferWildcardsVisitor.getConstraintSet(); ConstraintSet constraintSet = inferWildcardsVisitor.getConstraintSet();
System.out.println("Constraints: " + constraintSet); System.out.println("Constraints: " + constraintSet);
}
// Generated TPH
Map<TypePlaceholder, Tree> placeholderMapping = inferWildcardsVisitor.getPlaceholderMapping();
System.out.println("Placeholder: " + placeholderMapping.toString());
}
} }
} }