Unify angefügt

This commit is contained in:
JanUlrich 2017-03-17 15:48:06 +01:00
parent c8beb9579e
commit d0eb091666
5 changed files with 88 additions and 28 deletions

View File

@ -1,20 +1,21 @@
package de.dhbwstuttgart.core; package de.dhbwstuttgart.core;
import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.unify.TypeUnify; import de.dhbwstuttgart.typeinference.unify.TypeUnify;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
public class JavaTXCompiler { public class JavaTXCompiler {
@ -22,17 +23,26 @@ public class JavaTXCompiler {
public void typeInference(){ public void typeInference(){
ConstraintSet cons = new ConstraintSet(); ConstraintSet cons = new ConstraintSet();
List<ClassOrInterface> allClasses = new ArrayList<>();
for(SourceFile sf : sourceFiles){
allClasses.addAll(sf.getClasses());
}
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
for(SourceFile sf : sourceFiles){ for(SourceFile sf : sourceFiles){
cons.addAll(sf.getConstraints(sf.getTypeInferenceInformation(sourceFiles))); cons.addAll(sf.getConstraints(sf.getTypeInferenceInformation(sourceFiles)));
} }
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons); ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
/*
TypeUnify unify = new TypeUnify(); TypeUnify unify = new TypeUnify();
for(List<Constraint<UnifyPair>> xCons : unifyCons.cartesianProduct()){ for(List<Constraint<UnifyPair>> xCons : unifyCons.cartesianProduct()){
IFiniteClosure finiteClosure; Set<UnifyPair> xConsSet = new HashSet<>();
unify.unify(Arrays.asList(, finiteClosure); for(Constraint<UnifyPair> constraint : xCons){
xConsSet.addAll(constraint);
} }
*/ Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
System.out.println(result);
}
} }
public void parse(File sourceFile) throws IOException, ClassNotFoundException { public void parse(File sourceFile) throws IOException, ClassNotFoundException {

View File

@ -31,7 +31,8 @@ public class JavaTXParser {
for(Class cl : PackageCrawler.getClassesInPackage("java.lang")){ for(Class cl : PackageCrawler.getClassesInPackage("java.lang")){
ret.add(cl.getName()); ret.add(cl.getName());
} }
//TODO: Wieso muss man das händisch anhängen?
ret.add("java.lang.Object");
return ret; return ret;
} }
} }

View File

@ -1,10 +1,13 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator; package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.InvalidClassNameException; import de.dhbwstuttgart.parser.InvalidClassNameException;
import de.dhbwstuttgart.parser.PackageCrawler; import de.dhbwstuttgart.parser.PackageCrawler;
import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typecheck.*; import de.dhbwstuttgart.typecheck.*;
@ -199,12 +202,21 @@ public class SyntaxTreeGenerator{
List<Method> methods = convertMethods(ctx.classBody()); List<Method> methods = convertMethods(ctx.classBody());
GenericDeclarationList genericClassParameters = null; GenericDeclarationList genericClassParameters = null;
Token offset = ctx.getStart(); Token offset = ctx.getStart();
RefTypeOrTPHOrWildcardOrGeneric superClass = null; RefType superClass ;
if(ctx.superclass() != null){
superClass = convert(ctx.superclass());
}else{
superClass = new ASTFactory(reg).createObjectClass().getType();
}
Boolean isInterface = false; Boolean isInterface = false;
List<RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = null; List<RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = null;
return new ClassOrInterface(modifiers, name, fielddecl, methods, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); return new ClassOrInterface(modifiers, name, fielddecl, methods, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);
} }
private RefType convert(Java8Parser.SuperclassContext superclass) {
throw new NotImplementedException();
}
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext) { private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext) {
List<Method> ret = new ArrayList<>(); List<Method> ret = new ArrayList<>();
for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){

View File

@ -10,6 +10,7 @@ import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typecheck.GenericTypeName; import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName; import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry; import de.dhbwstuttgart.typecheck.JavaClassRegistry;
@ -62,13 +63,28 @@ public class ASTFactory {
return new ClassOrInterface(modifier, name, felder, methoden, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset); return new ClassOrInterface(modifier, name, felder, methoden, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset);
} }
private Method createConstructor(Constructor constructor, Class jreClass) { private de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) {
throw new NotImplementedException(); String name = constructor.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass);
Parameter[] jreParams = constructor.getParameters();
List<FormalParameter> params = new ArrayList<>();
for(Parameter jreParam : jreParams){
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType());
params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken()));
}
ParameterList parameterList = new ParameterList(params, new NullToken());
Block block = new Block(new ArrayList<Statement>(), new NullToken());
GenericDeclarationList gtvDeclarations = createGenerics(constructor.getTypeParameters(), inClass, constructor.getName());
Token offset = new NullToken();
int modifier = constructor.getModifiers();
return new de.dhbwstuttgart.syntaxtree.Constructor(name,returnType, modifier, parameterList, block, gtvDeclarations, offset);
} }
public Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){ public Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){
String name = jreMethod.getName(); String name = jreMethod.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(jreMethod.getReturnType()); RefTypeOrTPHOrWildcardOrGeneric returnType;
returnType = createType(jreMethod.getReturnType());
Parameter[] jreParams = jreMethod.getParameters(); Parameter[] jreParams = jreMethod.getParameters();
List<FormalParameter> params = new ArrayList<>(); List<FormalParameter> params = new ArrayList<>();
for(Parameter jreParam : jreParams){ for(Parameter jreParam : jreParams){
@ -77,14 +93,14 @@ public class ASTFactory {
} }
ParameterList parameterList = new ParameterList(params, new NullToken()); ParameterList parameterList = new ParameterList(params, new NullToken());
Block block = new Block(new ArrayList<Statement>(), new NullToken()); Block block = new Block(new ArrayList<Statement>(), new NullToken());
GenericDeclarationList gtvDeclarations = createGenerics(jreMethod.getTypeParameters(), inClass, jreMethod); GenericDeclarationList gtvDeclarations = createGenerics(jreMethod.getTypeParameters(), inClass, jreMethod.getName());
Token offset = new NullToken(); Token offset = new NullToken();
int modifier = jreMethod.getModifiers(); int modifier = jreMethod.getModifiers();
return new Method(name,returnType, modifier, parameterList, block, gtvDeclarations, offset); return new Method(name,returnType, modifier, parameterList, block, gtvDeclarations, offset);
} }
public GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, java.lang.reflect.Method contextM){ public GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){
List<de.dhbwstuttgart.syntaxtree.GenericTypeVar> gtvs = new ArrayList<>(); List<de.dhbwstuttgart.syntaxtree.GenericTypeVar> gtvs = new ArrayList<>();
for(TypeVariable jreTV : typeParameters){ for(TypeVariable jreTV : typeParameters){
de.dhbwstuttgart.syntaxtree.GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName()); de.dhbwstuttgart.syntaxtree.GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName());
@ -105,6 +121,23 @@ public class ASTFactory {
*/ */
public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type){ public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type){
if(type.getTypeName().equals("void")){
return new Void(new NullToken());
}else if(type.getTypeName().equals("int")){
return new RefType(this.names.getName("java.lang.Integer"), new ArrayList<>(), new NullToken());
}else if(type.getTypeName().equals("byte")){
return new RefType(this.names.getName("java.lang.Byte"), new ArrayList<>(), new NullToken());
}else if(type.getTypeName().equals("boolean")){
return new RefType(this.names.getName("java.lang.Boolean"), new ArrayList<>(), new NullToken());
}else if(type.getTypeName().equals("char")){
return new RefType(this.names.getName("java.lang.Char"), new ArrayList<>(), new NullToken());
}else if(type.getTypeName().equals("short")){
return new RefType(this.names.getName("java.lang.Short"), new ArrayList<>(), new NullToken());
}else if(type.getTypeName().equals("double")){
return new RefType(this.names.getName("java.lang.Double"), new ArrayList<>(), new NullToken());
}else if(type.getTypeName().equals("long")){
return new RefType(this.names.getName("java.lang.Long"), new ArrayList<>(), new NullToken());
}else{
if(type instanceof TypeVariable){ if(type instanceof TypeVariable){
//GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()" //GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()"
return new GenericTypeVar(new GenericTypeName(type.getTypeName(), return new GenericTypeVar(new GenericTypeName(type.getTypeName(),
@ -119,6 +152,7 @@ public class ASTFactory {
RefType ret = new RefType(this.names.getName(type.getTypeName()), params, new NullToken()); RefType ret = new RefType(this.names.getName(type.getTypeName()), params, new NullToken());
return ret; return ret;
} }
}
public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String name){ public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String name){
List<RefType> genericBounds = new ArrayList<>(); List<RefType> genericBounds = new ArrayList<>();

View File

@ -1,5 +1,8 @@
package de.dhbwstuttgart.typeinference.constraints; package de.dhbwstuttgart.typeinference.constraints;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;