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;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.unify.TypeUnify;
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.UnifyType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.*;
public class JavaTXCompiler {
@ -22,17 +23,26 @@ public class JavaTXCompiler {
public void typeInference(){
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){
cons.addAll(sf.getConstraints(sf.getTypeInferenceInformation(sourceFiles)));
}
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
/*
TypeUnify unify = new TypeUnify();
for(List<Constraint<UnifyPair>> xCons : unifyCons.cartesianProduct()){
IFiniteClosure finiteClosure;
unify.unify(Arrays.asList(, finiteClosure);
Set<UnifyPair> xConsSet = new HashSet<>();
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 {

View File

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

View File

@ -1,10 +1,13 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.InvalidClassNameException;
import de.dhbwstuttgart.parser.PackageCrawler;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typecheck.*;
@ -199,12 +202,21 @@ public class SyntaxTreeGenerator{
List<Method> methods = convertMethods(ctx.classBody());
GenericDeclarationList genericClassParameters = null;
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;
List<RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = null;
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) {
List<Method> ret = new ArrayList<>();
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.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
@ -62,13 +63,28 @@ public class ASTFactory {
return new ClassOrInterface(modifier, name, felder, methoden, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset);
}
private Method createConstructor(Constructor constructor, Class jreClass) {
throw new NotImplementedException();
private de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) {
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){
String name = jreMethod.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(jreMethod.getReturnType());
RefTypeOrTPHOrWildcardOrGeneric returnType;
returnType = createType(jreMethod.getReturnType());
Parameter[] jreParams = jreMethod.getParameters();
List<FormalParameter> params = new ArrayList<>();
for(Parameter jreParam : jreParams){
@ -77,14 +93,14 @@ public class ASTFactory {
}
ParameterList parameterList = new ParameterList(params, 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();
int modifier = jreMethod.getModifiers();
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<>();
for(TypeVariable jreTV : typeParameters){
de.dhbwstuttgart.syntaxtree.GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName());
@ -105,19 +121,37 @@ public class ASTFactory {
*/
public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type){
if(type instanceof TypeVariable){
//GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()"
return new GenericTypeVar(new GenericTypeName(type.getTypeName(),
new GTVDeclarationContext(new NullToken()) {}), new NullToken());
}
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
if(type instanceof ParameterizedType){
for(Type t : ((ParameterizedType)type).getActualTypeArguments()){
params.add(createType(t));
}
}
RefType ret = new RefType(this.names.getName(type.getTypeName()), params, new NullToken());
return ret;
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){
//GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()"
return new GenericTypeVar(new GenericTypeName(type.getTypeName(),
new GTVDeclarationContext(new NullToken()) {}), new NullToken());
}
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
if(type instanceof ParameterizedType){
for(Type t : ((ParameterizedType)type).getActualTypeArguments()){
params.add(createType(t));
}
}
RefType ret = new RefType(this.names.getName(type.getTypeName()), params, new NullToken());
return ret;
}
}
public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String name){

View File

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