Generics angefügt

This commit is contained in:
JanUlrich 2017-03-22 16:05:59 +01:00
parent d0eb091666
commit 47f1f9ad54
25 changed files with 240 additions and 61 deletions

View File

@ -109,7 +109,7 @@ var activeTableTab = "activeTableTab";
<li><a href="../../../../de/dhbwstuttgart/syntaxtree/type/ObjectType.html" title="class in de.dhbwstuttgart.syntaxtree.type">de.dhbwstuttgart.syntaxtree.type.ObjectType</a></li>
<li>
<ul class="inheritance">
<li><a href="../../../../de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.html" title="class in de.dhbwstuttgart.syntaxtree.type">de.dhbwstuttgart.syntaxtree.type.GenericTypeVar</a></li>
<li><a href="../../../../de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.html" title="class in de.dhbwstuttgart.syntaxtree.type">de.dhbwstuttgart.syntaxtree.type.GenericRefType</a></li>
<li>
<ul class="inheritance">
<li>de.dhbwstuttgart.syntaxtree.GenericTypeVar</li>
@ -174,7 +174,7 @@ extends <a href="../../../../de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.htm
</tr>
</table>
<ul class="blockList">
<li class="blockList"><a name="fields.inherited.from.class.de.dhbwstuttgart.syntaxtree.type.GenericTypeVar">
<li class="blockList"><a name="fields.inherited.from.class.de.dhbwstuttgart.syntaxtree.type.GenericRefType">
<!-- -->
</a>
<h3>Fields inherited from class&nbsp;de.dhbwstuttgart.syntaxtree.type.<a href="../../../../de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.html" title="class in de.dhbwstuttgart.syntaxtree.type">GenericTypeVar</a></h3>
@ -265,7 +265,7 @@ extends <a href="../../../../de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.htm
</tr>
</table>
<ul class="blockList">
<li class="blockList"><a name="methods.inherited.from.class.de.dhbwstuttgart.syntaxtree.type.GenericTypeVar">
<li class="blockList"><a name="methods.inherited.from.class.de.dhbwstuttgart.syntaxtree.type.GenericRefType">
<!-- -->
</a>
<h3>Methods inherited from class&nbsp;de.dhbwstuttgart.syntaxtree.type.<a href="../../../../de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.html" title="class in de.dhbwstuttgart.syntaxtree.type">GenericTypeVar</a></h3>

View File

@ -109,7 +109,7 @@ var activeTableTab = "activeTableTab";
<li><a href="../../../../de/dhbwstuttgart/syntaxtree/type/ObjectType.html" title="class in de.dhbwstuttgart.syntaxtree.type">de.dhbwstuttgart.syntaxtree.type.ObjectType</a></li>
<li>
<ul class="inheritance">
<li>de.dhbwstuttgart.syntaxtree.type.GenericTypeVar</li>
<li>de.dhbwstuttgart.syntaxtree.type.GenericRefType</li>
</ul>
</li>
</ul>

View File

@ -3,7 +3,7 @@
<html lang="de">
<head>
<!-- Generated by javadoc (1.8.0_05) on Tue May 12 19:56:24 CEST 2015 -->
<title>Uses of Class de.dhbwstuttgart.syntaxtree.type.GenericTypeVar</title>
<title>Uses of Class de.dhbwstuttgart.syntaxtree.type.GenericRefType</title>
<meta name="date" content="2015-05-12">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
<script type="text/javascript" src="../../../../../script.js"></script>
@ -70,7 +70,7 @@
</a></div>
<!-- ========= END OF TOP NAVBAR ========= -->
<div class="header">
<h2 title="Uses of Class de.dhbwstuttgart.syntaxtree.type.GenericTypeVar" class="title">Uses of Class<br>de.dhbwstuttgart.syntaxtree.type.GenericTypeVar</h2>
<h2 title="Uses of Class de.dhbwstuttgart.syntaxtree.type.GenericRefType" class="title">Uses of Class<br>de.dhbwstuttgart.syntaxtree.type.GenericTypeVar</h2>
</div>
<div class="classUseContainer">
<ul class="blockList">

View File

@ -0,0 +1,7 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import java.util.HashSet;
public class GenericsRegistry extends HashSet<String> {
}

View File

@ -25,20 +25,25 @@ public class StatementGenerator {
private JavaClassRegistry reg;
private Set<String> 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());

View File

@ -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<JavaClassName> 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<Field> fielddecl = convertFields(ctx.classBody());
List<Method> 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){

View File

@ -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<GenericTypeVar> 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<RefTypeOrTPHOrWildcardOrGeneric> 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<RefTypeOrTPHOrWildcardOrGeneric> convert(Java8Parser.TypeBoundContext typeBoundContext, JavaClassRegistry reg, GenericsRegistry generics) {
if(typeBoundContext == null){
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
ret.add(new ASTFactory(reg).createObjectClass().getType());
return ret;
}
throw new de.dhbwstuttgart.exceptions.NotImplementedException();
}
}

View File

@ -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;
}

View File

@ -17,7 +17,7 @@ public class Field extends GTVDeclarationContext implements Generic {
this.type = type;
}
public String getName(){
public String getName(){
return this.name;
}

View File

@ -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;
}

View File

@ -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<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
if(type instanceof ParameterizedType){

View File

@ -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();
}
/**

View File

@ -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<Constraint> 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;
}
}

View File

@ -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();
}
}

View File

@ -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<arglist.getArguments().size();i++){
ret.addAll(arglist.getArguments().get(i).getConstraints(info));
}
//Overloading:
Set<Constraint> methodConstraints = new HashSet<>();
for(MethodAssumption m : info.getMethods(name, arglist)){

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -10,9 +10,9 @@ import java.util.stream.Collectors;
public class MethodAssumption {
private RefType receiver;
private RefTypeOrTPHOrWildcardOrGeneric retType;
ParameterList params;
List<RefTypeOrTPHOrWildcardOrGeneric> params;
public MethodAssumption(RefType receiver, RefTypeOrTPHOrWildcardOrGeneric retType, ParameterList params){
public MethodAssumption(RefType receiver, RefTypeOrTPHOrWildcardOrGeneric retType, List<RefTypeOrTPHOrWildcardOrGeneric> params){
this.receiver = receiver;
this.retType = retType;
this.params = params;
@ -27,7 +27,6 @@ public class MethodAssumption {
}
public List<RefTypeOrTPHOrWildcardOrGeneric> getArgTypes() {
return params.getFormalparalist().stream().
map(formalParameter -> formalParameter.getType()).collect(Collectors.toList());
return params;
}
}

View File

@ -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<ClassOrInterface> availableClasses, Method methodContext) {
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses, ClassOrInterface currentClass, Method methodContext) {
super(availableClasses);
this.methodContext = methodContext;
this.currentClass = currentClass;
}
public ClassOrInterface getCurrentClass() {
return currentClass;
}
}

View File

@ -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<MethodAssumption> getMethods(String name, ArgumentList arglist) {
List<MethodAssumption> 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<RefTypeOrTPHOrWildcardOrGeneric> 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<FieldAssumption> getFields(String name){
List<FieldAssumption> 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<ClassOrInterface> getAvailableClasses() {
return classes;
}
}

View File

@ -0,0 +1,6 @@
class Generics<B> {
<A> A mt1(A a, B b){
return mt1(a, a);
}
}

View File

@ -9,3 +9,9 @@ class Methods {
mt3(a) {return a.add(); }
}
class Test {
String f;
add(){}
add(b){}
sub(b){}
}

View File

@ -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();
}
}