diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index e5f7f379..6b114e74 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -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 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 unifyCons = UnifyTypeFactory.convert(cons); - /* + TypeUnify unify = new TypeUnify(); for(List> xCons : unifyCons.cartesianProduct()){ - IFiniteClosure finiteClosure; - unify.unify(Arrays.asList(, finiteClosure); + Set xConsSet = new HashSet<>(); + for(Constraint constraint : xCons){ + xConsSet.addAll(constraint); + } + Set> result = unify.unify(xConsSet, finiteClosure); + System.out.println(result); } - */ + } public void parse(File sourceFile) throws IOException, ClassNotFoundException { diff --git a/src/de/dhbwstuttgart/parser/JavaTXParser.java b/src/de/dhbwstuttgart/parser/JavaTXParser.java index 4fa172cf..2acd90ae 100644 --- a/src/de/dhbwstuttgart/parser/JavaTXParser.java +++ b/src/de/dhbwstuttgart/parser/JavaTXParser.java @@ -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; } } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 8c3102e5..0f197f07 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -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 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 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 convertMethods(Java8Parser.ClassBodyContext classBodyContext) { List ret = new ArrayList<>(); for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 5355872d..431bd641 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -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 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(), 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 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(), 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 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 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 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){ diff --git a/src/de/dhbwstuttgart/typeinference/constraints/Constraint.java b/src/de/dhbwstuttgart/typeinference/constraints/Constraint.java index 71f95ef7..476f18dd 100644 --- a/src/de/dhbwstuttgart/typeinference/constraints/Constraint.java +++ b/src/de/dhbwstuttgart/typeinference/constraints/Constraint.java @@ -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;