diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/type/BoundedGenericTypeVar.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/type/BoundedGenericTypeVar.html
index 77026755d..b378dc3bf 100644
--- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/type/BoundedGenericTypeVar.html
+++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/type/BoundedGenericTypeVar.html
@@ -109,7 +109,7 @@ var activeTableTab = "activeTableTab";
de.dhbwstuttgart.syntaxtree.type.ObjectType
-- de.dhbwstuttgart.syntaxtree.type.GenericTypeVar
+- de.dhbwstuttgart.syntaxtree.type.GenericRefType
-
diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/type/class-use/GenericTypeVar.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/type/class-use/GenericTypeVar.html
index e079210dc..7f094d035 100644
--- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/type/class-use/GenericTypeVar.html
+++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/type/class-use/GenericTypeVar.html
@@ -3,7 +3,7 @@
-Uses of Class de.dhbwstuttgart.syntaxtree.type.GenericTypeVar
+Uses of Class de.dhbwstuttgart.syntaxtree.type.GenericRefType
@@ -70,7 +70,7 @@
diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericsRegistry.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericsRegistry.java
new file mode 100644
index 000000000..8684ae1c7
--- /dev/null
+++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericsRegistry.java
@@ -0,0 +1,7 @@
+package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
+
+import java.util.HashSet;
+
+public class GenericsRegistry extends HashSet {
+
+}
diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java
index 5d91d3728..90b21d0b5 100644
--- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java
+++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java
@@ -25,20 +25,25 @@ public class StatementGenerator {
private JavaClassRegistry reg;
private Set localVars = new HashSet<>();
+ private GenericsRegistry generics;
- public StatementGenerator(JavaClassRegistry reg){
+ public StatementGenerator(JavaClassRegistry reg, GenericsRegistry generics){
this.reg = reg;
+ this.generics = generics;
}
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext) {
Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader();
String name = header.methodDeclarator().Identifier().getText();
-
+ GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart());
+ if(methodDeclarationContext.methodHeader().typeParameters() != null){
+ gtvDeclarations = TypeGenerator.convert(methodDeclarationContext.methodHeader().typeParameters(), reg, generics);
+ }
RefTypeOrTPHOrWildcardOrGeneric retType;
if(header.result() != null){
if(header.result().unannType() != null){
- retType = TypeGenerator.convert(header.result().unannType(), reg);
+ retType = TypeGenerator.convert(header.result().unannType(), reg, generics);
}
else retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart());
}else{
@@ -54,10 +59,6 @@ public class StatementGenerator {
}else{
block = this.convert(methodDeclarationContext.methodBody().block());
}
- GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart());
- if(methodDeclarationContext.methodHeader().typeParameters() != null){
- gtvDeclarations = TypeGenerator.convert(methodDeclarationContext.methodHeader().typeParameters());
- }
return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, methodDeclarationContext.getStart());
}
@@ -79,7 +80,7 @@ public class StatementGenerator {
String paramName = SyntaxTreeGenerator.convert(fp.variableDeclaratorId());
RefTypeOrTPHOrWildcardOrGeneric type;
if(fp.unannType() != null){
- type = TypeGenerator.convert(fp.unannType(), reg);
+ type = TypeGenerator.convert(fp.unannType(), reg, generics);
}else{
type = TypePlaceholder.fresh(fp.getStart());
}
@@ -284,7 +285,7 @@ public class StatementGenerator {
receiver = new This(offset);
}else if(receiver == null){ //Handelt es sich um keinen Statischen Klassennamen:
String part = expression.substring(0,expression.length() - (1 + parts[parts.length-1].length()));
- receiver = generateFieldVarOrClassname(part, offset);
+ receiver = generateLocalOrFieldVarOrClassName(part, offset);
}
return new FieldVar(receiver, parts[parts.length-1], TypePlaceholder.fresh(offset) ,offset);
}
@@ -421,7 +422,7 @@ public class StatementGenerator {
if(declaration.unannType()==null){
type = TypePlaceholder.fresh(declaration.getStart());
}else{
- type = TypeGenerator.convert(declaration.unannType(), reg);
+ type = TypeGenerator.convert(declaration.unannType(), reg, generics);
}
for(Java8Parser.VariableDeclaratorContext varDecl : declaration.variableDeclaratorList().variableDeclarator()){
TerminalNode name = varDecl.variableDeclaratorId().Identifier();
@@ -753,9 +754,13 @@ public class StatementGenerator {
receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart());
}else if(methodInvocationContext.expressionName()!=null){
receiver = convert(methodInvocationContext.expressionName());
- }else if(methodInvocationContext.toString().startsWith("super")){
+ }else if(methodInvocationContext.toString().startsWith("super")) {
receiver = new Super(methodInvocationContext.getStart());
- }else throw new NotImplementedException();
+ }else if(methodInvocationContext.methodName() != null){
+ receiver = new This(methodInvocationContext.methodName().getStart());
+ }else {
+ throw new NotImplementedException();
+ }
ArgumentList argumentList = convert(methodInvocationContext.argumentList());
MethodCall ret = new MethodCall(new Receiver(receiver), name, argumentList, methodInvocationContext.getStart());
diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java
index 0f197f07f..a11760adb 100644
--- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java
+++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java
@@ -2,6 +2,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.InvalidClassNameException;
+import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.PackageCrawler;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*;
@@ -23,7 +24,8 @@ public class SyntaxTreeGenerator{
private JavaClassRegistry reg;
private String pkgName = "";
List imports = new ArrayList();
-
+ private GenericsRegistry generics = new GenericsRegistry();
+
public SyntaxTreeGenerator(JavaClassRegistry reg){
this.reg = reg;
}
@@ -49,10 +51,10 @@ public class SyntaxTreeGenerator{
else{
nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
}
- //Die Generic TypeParameter Definitionen ebenfalls an die JavaClassName-Registry anfügen:
+ //Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen:
if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){
for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){
- this.reg.add(tp.Identifier().toString());
+ //this.reg.add(tp.Identifier().toString());
}
}
this.reg.add(nameString);
@@ -197,10 +199,11 @@ public class SyntaxTreeGenerator{
}
}
JavaClassName name = convert(ctx.Identifier());
+ GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), reg, generics);
Block class_block = null;
List fielddecl = convertFields(ctx.classBody());
List methods = convertMethods(ctx.classBody());
- GenericDeclarationList genericClassParameters = null;
+
Token offset = ctx.getStart();
RefType superClass ;
if(ctx.superclass() != null){
@@ -225,7 +228,7 @@ public class SyntaxTreeGenerator{
if(classMemberDeclarationContext.fieldDeclaration() != null){
//Do nothing!
}else if(classMemberDeclarationContext.methodDeclaration()!= null){
- StatementGenerator stmtGen = new StatementGenerator(reg);
+ StatementGenerator stmtGen = new StatementGenerator(reg, generics);
ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration()));
}
}
@@ -262,7 +265,7 @@ public class SyntaxTreeGenerator{
for(Java8Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()){
modifiers+=(convert(fieldModifierContext));
}
- RefTypeOrTPHOrWildcardOrGeneric fieldType = TypeGenerator.convert(fieldDeclarationContext.unannType(), reg);
+ RefTypeOrTPHOrWildcardOrGeneric fieldType = TypeGenerator.convert(fieldDeclarationContext.unannType(), reg, generics);
for(Java8Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){
String fieldName = convert(varCtx.variableDeclaratorId());
if(varCtx.variableInitializer() != null){
diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java
index c380625e2..679110e17 100644
--- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java
+++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java
@@ -1,22 +1,39 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
+import de.dhbwstuttgart.exceptions.TypeinferenceException;
+import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
+import de.dhbwstuttgart.syntaxtree.GTVDeclarationContext;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
+import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
+import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
+import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
+import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
+import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
+import java.util.ArrayList;
+import java.util.List;
+
public class TypeGenerator {
- public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg) {
+ public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
String name = unannClassOrInterfaceTypeContext.getText();
-
+ if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein:
+ if(generics.contains(name)){
+ return new GenericRefType(new GenericTypeName(name), unannClassOrInterfaceTypeContext.getStart());
+ }else{
+ throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",unannClassOrInterfaceTypeContext.getStart());
+ }
+ }
return new RefType(reg.getName(name), unannClassOrInterfaceTypeContext.getStart());
}
- public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg) {
+ public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) {
if(unannTypeContext.unannPrimitiveType()!=null){
throw new NotImplementedException();
}else
@@ -27,11 +44,38 @@ public class TypeGenerator {
JavaClassName name = reg.getName(unannTypeContext.unannReferenceType().unannTypeVariable().Identifier().toString());
return new RefType(name, unannTypeContext.getStart());
}
- return TypeGenerator.convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType(), reg);
+ return TypeGenerator.convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType(), reg, genericsRegistry);
}
- public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext) {
- throw new NotImplementedException();
+ public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext, JavaClassRegistry reg, GenericsRegistry generics) {
+ if(typeParametersContext == null){
+ return new GenericDeclarationList(new ArrayList<>(), new NullToken());
+ }
+ Token endOffset = typeParametersContext.getStop();
+ List typeVars = new ArrayList<>();
+ for(Java8Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList().typeParameter()){
+ typeVars.add(convert(typeParameter, reg, generics));
+ endOffset = typeParameter.getStop();
+ }
+ return new GenericDeclarationList(typeVars, endOffset);
}
+ public static GenericTypeVar convert(Java8Parser.TypeParameterContext typeParameter, JavaClassRegistry reg, GenericsRegistry generics) {
+ String name = typeParameter.Identifier().getText();
+ List bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics);
+
+ GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop());
+ generics.add(name);
+ return ret;
+ }
+
+ public static List convert(Java8Parser.TypeBoundContext typeBoundContext, JavaClassRegistry reg, GenericsRegistry generics) {
+ if(typeBoundContext == null){
+ List ret = new ArrayList<>();
+ ret.add(new ASTFactory(reg).createObjectClass().getType());
+ return ret;
+ }
+
+ throw new de.dhbwstuttgart.exceptions.NotImplementedException();
+ }
}
diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java
index 257ea6f53..a69a55e37 100755
--- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java
+++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java
@@ -69,7 +69,7 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
public ConstraintSet getConstraints(TypeInferenceInformation info) {
ConstraintSet ret = new ConstraintSet();
for(Method m : this.getMethods()){
- ret.addAll(m.getConstraints(info));
+ ret.addAll(m.getConstraints(info, this));
}
return ret;
}
diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java
index 20919a91e..6c0fd84ba 100644
--- a/src/de/dhbwstuttgart/syntaxtree/Field.java
+++ b/src/de/dhbwstuttgart/syntaxtree/Field.java
@@ -17,7 +17,7 @@ public class Field extends GTVDeclarationContext implements Generic {
this.type = type;
}
- public String getName(){
+ public String getName(){
return this.name;
}
diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java
index 8cea9e41c..e117dba54 100755
--- a/src/de/dhbwstuttgart/syntaxtree/Method.java
+++ b/src/de/dhbwstuttgart/syntaxtree/Method.java
@@ -33,9 +33,9 @@ public class Method extends Field implements IItemWithOffset
this.block = block;
}
- public ConstraintSet getConstraints(TypeInferenceInformation info) {
+ public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) {
ConstraintSet ret = new ConstraintSet();
- ret.addAll(block.getConstraints(new TypeInferenceBlockInformation(info.getAvailableClasses(), this)));
+ ret.addAll(block.getConstraints(new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this)));
return ret;
}
diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java
index 431bd641d..1345edb6d 100644
--- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java
+++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java
@@ -8,7 +8,7 @@ import java.util.List;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
-import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
+import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typecheck.GenericTypeName;
@@ -19,7 +19,6 @@ import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import org.antlr.v4.runtime.Token;
-import sun.reflect.generics.reflectiveObjects.NotImplementedException;
/**
* Anmerkung:
@@ -140,8 +139,7 @@ public class ASTFactory {
}else{
if(type instanceof TypeVariable){
//GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()"
- return new GenericTypeVar(new GenericTypeName(type.getTypeName(),
- new GTVDeclarationContext(new NullToken()) {}), new NullToken());
+ return new GenericRefType(new GenericTypeName(type.getTypeName()), new NullToken());
}
List params = new ArrayList<>();
if(type instanceof ParameterizedType){
diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java
index 3f4a38912..a76d347fa 100644
--- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java
+++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java
@@ -41,13 +41,17 @@ public class UnifyTypeFactory {
return new UnifyPair(tl, tr, PairOperator.SMALLERDOT);
}
+ public static UnifyPair generateEqualDotPair(UnifyType tl, UnifyType tr){
+ return new UnifyPair(tl, tr, PairOperator.EQUALSDOT);
+ }
+
/**
* Convert from
* ASTType -> UnifyType
*/
public static UnifyType convert(RefTypeOrTPHOrWildcardOrGeneric t){
- if(t instanceof GenericTypeVar){
- return UnifyTypeFactory.convert((GenericTypeVar)t);
+ if(t instanceof GenericRefType){
+ return UnifyTypeFactory.convert((GenericRefType)t);
}else
if(t instanceof FunN){
return UnifyTypeFactory.convert((FunN)t);
@@ -94,7 +98,7 @@ public class UnifyTypeFactory {
return new PlaceholderType(tph.getName());
}
- public static UnifyType convert(GenericTypeVar t){
+ public static UnifyType convert(GenericRefType t){
return new PlaceholderType(TypePlaceholder.fresh(t.getOffset()).getName());
}
@@ -112,10 +116,16 @@ public class UnifyTypeFactory {
}
public static UnifyPair convert(Pair p) {
- if(!p.OperatorSmaller())throw new NotImplementedException();
- UnifyPair ret = generateSmallerDotPair(UnifyTypeFactory.convert(p.TA1)
- , UnifyTypeFactory.convert(p.TA2));
- return ret;
+ if(p.OperatorSmaller()) {
+ UnifyPair ret = generateSmallerDotPair(UnifyTypeFactory.convert(p.TA1)
+ , UnifyTypeFactory.convert(p.TA2));
+ return ret;
+ }else if(p.OperatorEqual()){
+ UnifyPair ret = generateEqualDotPair(UnifyTypeFactory.convert(p.TA1)
+ , UnifyTypeFactory.convert(p.TA2));
+ return ret;
+ }else throw new NotImplementedException();
+
}
/**
diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java
index aa5de22ee..0eac0866c 100644
--- a/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java
+++ b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java
@@ -1,21 +1,43 @@
package de.dhbwstuttgart.syntaxtree.statement;
+import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
+import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
+import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
+import de.dhbwstuttgart.typeinference.constraints.Pair;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
+import java.util.HashSet;
+import java.util.Set;
+
public class FieldVar extends Expression {
+
+ private String fieldVarName;
+ public Expression receiver;
+
public FieldVar(Expression receiver, String fieldVarName, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(type, offset);
+ this.fieldVarName = fieldVarName;
+ this.receiver = receiver;
}
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
- //for(FieldAssumption fieldAssumption : info.getFields(fieldVarName)){
-
- //}
- throw new NotImplementedException();
+ ConstraintSet ret = new ConstraintSet();
+ Set oderConstraints = new HashSet<>();
+ ret.addAll(receiver.getConstraints(info));
+ for(FieldAssumption fieldAssumption : info.getFields(fieldVarName)){
+ Constraint constraint = new Constraint();
+ constraint.add(new Pair(receiver.getType(),fieldAssumption.getReceiverType()));
+ constraint.add(new Pair(this.getType(),fieldAssumption.getType()));
+ oderConstraints.add(constraint);
+ }
+ if(oderConstraints.size() == 0)
+ throw new TypeinferenceException("Kein Feld "+fieldVarName+ " gefunden", getOffset());
+ ret.addOderConstraint(oderConstraints);
+ return ret;
}
}
diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java
index 9ff4a075a..d1d3fba6f 100644
--- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java
+++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java
@@ -1,5 +1,6 @@
package de.dhbwstuttgart.syntaxtree.statement;
+import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
@@ -24,6 +25,7 @@ public class LocalVar extends Statement{
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
+ //throw new NotImplementedException();
return new ConstraintSet();
}
}
diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java
index 9da971243..96d0a48b0 100755
--- a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java
+++ b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java
@@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
+import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
@@ -31,6 +32,10 @@ public class MethodCall extends Statement
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = receiver.getConstraints(info);
+ ret.addAll(receiver.getConstraints(info));
+ for(int i = 0;i methodConstraints = new HashSet<>();
for(MethodAssumption m : info.getMethods(name, arglist)){
diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/This.java b/src/de/dhbwstuttgart/syntaxtree/statement/This.java
index ffe3b3f7c..b290c9312 100755
--- a/src/de/dhbwstuttgart/syntaxtree/statement/This.java
+++ b/src/de/dhbwstuttgart/syntaxtree/statement/This.java
@@ -1,22 +1,30 @@
package de.dhbwstuttgart.syntaxtree.statement;
+import de.dhbwstuttgart.syntaxtree.operator.Operator;
+import de.dhbwstuttgart.syntaxtree.type.RefType;
+import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
+import de.dhbwstuttgart.typeinference.constraints.Pair;
+import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
+import org.antlr.v4.runtime.atn.SemanticContext;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class This extends Expression
{
public This(Token offset)
{
- super(null,null);
+ super(TypePlaceholder.fresh(offset),offset);
}
public ArgumentList arglist;
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
- throw new NotImplementedException();
+ ConstraintSet ret = new ConstraintSet();
+ ret.addUndConstraint(new Pair( this.getType(), info.getCurrentClass().getType(), PairOperator.EQUALSDOT));
+ return ret;
}
}
diff --git a/src/de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.java b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java
similarity index 69%
rename from src/de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.java
rename to src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java
index 8a871676e..746dac848 100755
--- a/src/de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.java
+++ b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java
@@ -4,10 +4,10 @@ import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
import org.antlr.v4.runtime.Token;
-public class GenericTypeVar extends RefTypeOrTPHOrWildcardOrGeneric
+public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
{
private GenericTypeName name;
- public GenericTypeVar(GenericTypeName name, Token offset)
+ public GenericRefType(GenericTypeName name, Token offset)
{
super(offset);
this.name = name;
diff --git a/src/de/dhbwstuttgart/typecheck/GenericTypeName.java b/src/de/dhbwstuttgart/typecheck/GenericTypeName.java
index 188bb9cb9..4191bdd52 100644
--- a/src/de/dhbwstuttgart/typecheck/GenericTypeName.java
+++ b/src/de/dhbwstuttgart/typecheck/GenericTypeName.java
@@ -3,7 +3,7 @@ package de.dhbwstuttgart.typecheck;
import de.dhbwstuttgart.syntaxtree.GTVDeclarationContext;
public class GenericTypeName extends JavaClassName {
- public GenericTypeName(String name, GTVDeclarationContext declarationContext) {
+ public GenericTypeName(String name) {
super(name);
}
}
diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/FieldAssumption.java b/src/de/dhbwstuttgart/typeinference/assumptions/FieldAssumption.java
new file mode 100644
index 000000000..d0a278a5e
--- /dev/null
+++ b/src/de/dhbwstuttgart/typeinference/assumptions/FieldAssumption.java
@@ -0,0 +1,22 @@
+package de.dhbwstuttgart.typeinference.assumptions;
+
+import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
+
+public class FieldAssumption {
+ private RefTypeOrTPHOrWildcardOrGeneric receiverType;
+ private RefTypeOrTPHOrWildcardOrGeneric type;
+
+ public FieldAssumption(RefTypeOrTPHOrWildcardOrGeneric receiverType,
+ RefTypeOrTPHOrWildcardOrGeneric type){
+ this.type = type;
+ this.receiverType = receiverType;
+ }
+
+ public RefTypeOrTPHOrWildcardOrGeneric getReceiverType() {
+ return receiverType;
+ }
+
+ public RefTypeOrTPHOrWildcardOrGeneric getType() {
+ return type;
+ }
+}
diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java
index 8844b2656..c878a85ec 100644
--- a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java
+++ b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java
@@ -10,9 +10,9 @@ import java.util.stream.Collectors;
public class MethodAssumption {
private RefType receiver;
private RefTypeOrTPHOrWildcardOrGeneric retType;
- ParameterList params;
+ List params;
- public MethodAssumption(RefType receiver, RefTypeOrTPHOrWildcardOrGeneric retType, ParameterList params){
+ public MethodAssumption(RefType receiver, RefTypeOrTPHOrWildcardOrGeneric retType, List params){
this.receiver = receiver;
this.retType = retType;
this.params = params;
@@ -27,7 +27,6 @@ public class MethodAssumption {
}
public List getArgTypes() {
- return params.getFormalparalist().stream().
- map(formalParameter -> formalParameter.getType()).collect(Collectors.toList());
+ return params;
}
}
diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java
index 55d992e4d..e31d2ec27 100644
--- a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java
+++ b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java
@@ -1,15 +1,22 @@
package de.dhbwstuttgart.typeinference.assumptions;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
+import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import java.util.Set;
public class TypeInferenceBlockInformation extends TypeInferenceInformation {
private Method methodContext;
+ private ClassOrInterface currentClass;
- public TypeInferenceBlockInformation(Set availableClasses, Method methodContext) {
+ public TypeInferenceBlockInformation(Set availableClasses, ClassOrInterface currentClass, Method methodContext) {
super(availableClasses);
this.methodContext = methodContext;
+ this.currentClass = currentClass;
+ }
+
+ public ClassOrInterface getCurrentClass() {
+ return currentClass;
}
}
diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java
index 44637d301..ec3ecb26a 100644
--- a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java
+++ b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java
@@ -1,8 +1,15 @@
package de.dhbwstuttgart.typeinference.assumptions;
+import de.dhbwstuttgart.exceptions.NotImplementedException;
+import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
+import de.dhbwstuttgart.syntaxtree.Field;
+import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
+import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
+import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
+import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import java.util.ArrayList;
import java.util.List;
@@ -25,13 +32,39 @@ public class TypeInferenceInformation {
classes = availableClasses;
}
+ private RefTypeOrTPHOrWildcardOrGeneric checkGTV(RefTypeOrTPHOrWildcardOrGeneric type){
+ if(type instanceof GenericRefType){
+ return TypePlaceholder.fresh(new NullToken());
+ }else{
+ return type;
+ }
+ }
+
public List getMethods(String name, ArgumentList arglist) {
List ret = new ArrayList<>();
for(ClassOrInterface cl : classes){
for(Method m : cl.getMethods()){
if(m.getName().equals(name) &&
m.getParameterList().getFormalparalist().size() == arglist.getArguments().size()){
- ret.add(new MethodAssumption(cl.getType(), m.getType(), m.getParameterList()));
+ RefTypeOrTPHOrWildcardOrGeneric retType = checkGTV(m.getType());
+
+ List params = new ArrayList<>();
+ for(FormalParameter fp : m.getParameterList().getFormalparalist()){
+ params.add(checkGTV(fp.getType()));
+ }
+ ret.add(new MethodAssumption(cl.getType(), retType, params));
+ }
+ }
+ }
+ return ret;
+ }
+
+ public List getFields(String name){
+ List ret = new ArrayList<>();
+ for(ClassOrInterface cl : classes){
+ for(Field m : cl.getFieldDecl()){
+ if(m.getName().equals(name)){
+ ret.add(new FieldAssumption(cl.getType(), checkGTV(m.getType())));
}
}
}
@@ -41,4 +74,5 @@ public class TypeInferenceInformation {
public Set getAvailableClasses() {
return classes;
}
+
}
diff --git a/test/javFiles/Generics.jav b/test/javFiles/Generics.jav
new file mode 100644
index 000000000..703ba6c29
--- /dev/null
+++ b/test/javFiles/Generics.jav
@@ -0,0 +1,6 @@
+
+class Generics {
+ A mt1(A a, B b){
+ return mt1(a, a);
+ }
+}
diff --git a/test/javFiles/Methods.jav b/test/javFiles/Methods.jav
index 9216b2f34..9142ab5d2 100644
--- a/test/javFiles/Methods.jav
+++ b/test/javFiles/Methods.jav
@@ -9,3 +9,9 @@ class Methods {
mt3(a) {return a.add(); }
}
+class Test {
+ String f;
+ add(){}
+ add(b){}
+ sub(b){}
+}
\ No newline at end of file
diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java
index 996e7588f..8760e3217 100644
--- a/test/typeinference/JavaTXCompilerTest.java
+++ b/test/typeinference/JavaTXCompilerTest.java
@@ -16,7 +16,8 @@ public class JavaTXCompilerTest {
@Test
public void test() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler();
- compiler.parse(new File(rootDirectory+"MethodsEasy.jav"));
+ //compiler.parse(new File(rootDirectory+"Methods.jav"));
+ compiler.parse(new File(rootDirectory+"Generics.jav"));
compiler.typeInference();
}
}
\ No newline at end of file