Merge mit ParserÄnderungen

This commit is contained in:
JanUlrich 2017-04-06 12:14:11 +02:00
commit 8453aaf7fd
33 changed files with 278 additions and 96 deletions

View File

@ -39,6 +39,8 @@ public class JavaTXCompiler {
for(Constraint<UnifyPair> constraint : xCons){
xConsSet.addAll(constraint);
}
System.out.println(xConsSet);
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
System.out.println(result);
}

View File

@ -0,0 +1,14 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.typecheck.JavaClassName;
public class GenericContext {
public final String parentMethod;
public final JavaClassName parentClass;
public GenericContext(JavaClassName parentClass, String parentMethod) {
if(parentMethod == null)parentMethod = "";
this.parentClass = parentClass;
this.parentMethod = parentMethod;
}
}

View File

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

View File

@ -10,6 +10,7 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.*;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
@ -33,12 +34,12 @@ public class StatementGenerator {
}
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext) {
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass) {
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);
gtvDeclarations = TypeGenerator.convert(methodDeclarationContext.methodHeader().typeParameters(), parentClass, name,reg, generics);
}
RefTypeOrTPHOrWildcardOrGeneric retType;
if(header.result() != null){
@ -128,7 +129,7 @@ public class StatementGenerator {
if(stmt.block() != null){
return convert(stmt.block());
}else if(stmt.emptyStatement() != null){
return new EmptyStmt();
return new EmptyStmt(stmt.getStart());
}else if(stmt.expressionStatement() != null){
return convert(stmt.expressionStatement());
}else if(stmt.assertStatement() != null){
@ -159,7 +160,7 @@ public class StatementGenerator {
Statement stmt = convert(statementContext);
statements.add(stmt);
}
statements = SyntacticSugar.addTrailingReturn(statements);
return new Block(statements, block.getStart());
}
@ -228,6 +229,8 @@ public class StatementGenerator {
receiver = convert(methodInvocationContext.primary());
}else if(methodInvocationContext.toString().startsWith("super")){
receiver = new Super(methodInvocationContext.getStart());
}else if(methodInvocationContext.methodName() != null){
receiver = new This(methodInvocationContext.getStart());
}else throw new NotImplementedException();
ArgumentList argumentList = convert(methodInvocationContext.argumentList());
@ -631,7 +634,15 @@ public class StatementGenerator {
}
private Expression convert(Java8Parser.CastExpressionContext expressionContext) {
throw new NotImplementedException();
Expression expr = null;
if(expressionContext.unaryExpression() != null){
throw new NotImplementedException();
}else if(expressionContext.unaryExpressionNotPlusMinus() != null){
expr = convert(expressionContext.unaryExpressionNotPlusMinus());
}else if(expressionContext.lambdaExpression() != null){
expr = convert(expressionContext.lambdaExpression());
}
return new CastExpr(TypeGenerator.convert(expressionContext.referenceType(), reg, generics),expr, expressionContext.getStart());
}
private Expression convert(Java8Parser.PostfixExpressionContext expression) {

View File

@ -0,0 +1,34 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.statement.*;
import java.util.List;
public class SyntacticSugar {
public static List<Statement> addTrailingReturn(List<Statement> statements){
if(statements.size()!=0) {
Statement lastStmt = statements.get(statements.size() - 1);
if (lastStmt instanceof Return) return statements;
if (lastStmt instanceof WhileStmt) {
if (hasReturn(((WhileStmt) lastStmt).loop_block)) return statements;
} else if (lastStmt instanceof IfStmt) {
if (hasReturn(((IfStmt) lastStmt).then_block)
&& hasReturn(((IfStmt) lastStmt).else_block)) return statements;
} else if (lastStmt instanceof ForStmt) {
if (hasReturn(((ForStmt) lastStmt).body_Loop_block)) return statements;
} else {
}
}
statements.add(new ReturnVoid(new NullToken()));
return statements;
}
private static boolean hasReturn(Block block){
for(Statement s : block.getStatements())
if(s instanceof Return)return true;
return false;
}
}

View File

@ -200,10 +200,9 @@ public class SyntaxTreeGenerator{
}
}
JavaClassName name = reg.getName(ctx.Identifier().getText());
GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), reg, generics);
Block class_block = null;
GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics);
List<Field> fielddecl = convertFields(ctx.classBody());
List<Method> methods = convertMethods(ctx.classBody());
List<Method> methods = convertMethods(ctx.classBody(), name);
Token offset = ctx.getStart();
RefType superClass ;
@ -221,7 +220,7 @@ public class SyntaxTreeGenerator{
throw new NotImplementedException();
}
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext) {
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext, JavaClassName parentClass) {
List<Method> ret = new ArrayList<>();
for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){
if(classMember.classMemberDeclaration() != null){
@ -230,7 +229,7 @@ public class SyntaxTreeGenerator{
//Do nothing!
}else if(classMemberDeclarationContext.methodDeclaration()!= null){
StatementGenerator stmtGen = new StatementGenerator(reg, generics);
ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration()));
ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration(), parentClass));
}
}
}

View File

@ -24,14 +24,7 @@ public class TypeGenerator {
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());
return convertTypeName(name,unannClassOrInterfaceTypeContext.getStart(), reg, generics);
}
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) {
@ -48,25 +41,25 @@ public class TypeGenerator {
return TypeGenerator.convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType(), reg, genericsRegistry);
}
public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext, JavaClassRegistry reg, GenericsRegistry generics) {
public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext, JavaClassName parentClass, String parentMethod, 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));
typeVars.add(convert(typeParameter, parentClass, parentMethod, reg, generics));
endOffset = typeParameter.getStop();
}
return new GenericDeclarationList(typeVars, endOffset);
}
public static GenericTypeVar convert(Java8Parser.TypeParameterContext typeParameter, JavaClassRegistry reg, GenericsRegistry generics) {
public static GenericTypeVar convert(Java8Parser.TypeParameterContext typeParameter, JavaClassName parentClass, String parentMethod, 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);
GenericTypeVar ret = new GenericTypeVar(new GenericTypeName(new GenericContext(parentClass, parentMethod), name), bounds, typeParameter.getStart(), typeParameter.getStop());
generics.put(name, new GenericContext(parentClass, parentMethod));
return ret;
}
@ -79,4 +72,19 @@ public class TypeGenerator {
throw new de.dhbwstuttgart.exceptions.NotImplementedException();
}
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.ReferenceTypeContext referenceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
return convertTypeName(referenceTypeContext.getText(), referenceTypeContext.getStart(), reg, generics);
}
private static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(String name, Token offset, JavaClassRegistry reg, GenericsRegistry generics){
if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein:
if(generics.keySet().contains(name)){
return new GenericRefType(new GenericTypeName(generics.get(name),name), offset);
}else{
throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset);
}
}
return new RefType(reg.getName(name), offset);
}
}

View File

@ -85,4 +85,8 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
public RefTypeOrTPHOrWildcardOrGeneric getSuperClass() {
return superClass;
}
public Iterable<? extends GenericTypeVar> getGenerics() {
return this.genericClassParameters;
}
}

View File

@ -24,6 +24,6 @@ public class GenericDeclarationList extends SyntaxTreeNode implements Iterable<G
@Override
public Iterator<GenericTypeVar> iterator() {
return null;
return gtvs.iterator();
}
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typecheck.GenericTypeName;
import org.antlr.v4.runtime.Token;
import java.util.ArrayList;
@ -23,9 +24,9 @@ public class GenericTypeVar extends SyntaxTreeNode
*/
List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds=new ArrayList<RefTypeOrTPHOrWildcardOrGeneric>();
private Token endOffset;
private String name;
private GenericTypeName name;
public GenericTypeVar(String s, List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds, Token offset, Token endOffset)
public GenericTypeVar(GenericTypeName s, List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds, Token offset, Token endOffset)
{
super(offset);
name = s;
@ -47,4 +48,7 @@ public class GenericTypeVar extends SyntaxTreeNode
return "BoGTV " + this.name;
}
public GenericTypeName getName() {
return name;
}
}

View File

@ -5,8 +5,11 @@ import java.util.ArrayList;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.core.IItemWithOffset;
@ -25,17 +28,24 @@ public class Method extends Field implements IItemWithOffset
private Block block;
private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken());
private ExceptionList exceptionlist;
private GenericDeclarationList generics;
public Method(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block block,
GenericDeclarationList gtvDeclarations, Token offset) {
super(name, returnType, modifiers, offset);
this.parameterlist = parameterList;
this.block = block;
this.generics = gtvDeclarations;
}
public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) {
ConstraintSet ret = new ConstraintSet();
ret.addAll(block.getConstraints(new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this)));
TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this);
ret.addAll(block.getConstraints(blockInfo));
/*
ret.addUndConstraint(
ConstraintsFactory.createPair(block.getType(), this.getType(), blockInfo));
*/
return ret;
}
@ -43,11 +53,13 @@ public class Method extends Field implements IItemWithOffset
return parameterlist;
}
//erweitert Sebastian Ritt
public Block get_Block() {
return block;
}
public Iterable<? extends GenericTypeVar> getGenerics() {
return generics;
}
}

View File

@ -6,10 +6,10 @@ import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
@ -17,7 +17,6 @@ import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import org.antlr.v4.runtime.Token;
/**
@ -48,13 +47,13 @@ public class ASTFactory {
java.lang.Class superjreClass = jreClass.getSuperclass();
RefType superClass;
if(superjreClass != null){
superClass = (RefType) createType(superjreClass);
superClass = (RefType) createType(superjreClass, name, "");
}else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!)
superClass = (RefType) createType(java.lang.Object.class);
superClass = (RefType) createType(java.lang.Object.class, name, "");
}
List<RefType> implementedInterfaces = new ArrayList<>();
for(java.lang.Class jreInterface : jreClass.getInterfaces()){
implementedInterfaces.add((RefType) createType(jreInterface));
implementedInterfaces.add((RefType) createType(jreInterface, name, ""));
}
GenericDeclarationList genericDeclarationList = createGenerics(jreClass.getTypeParameters(), jreClass, null);
@ -64,11 +63,11 @@ public class ASTFactory {
private de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) {
String name = constructor.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass);
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, names.getName(inClass.getName()), name);
Parameter[] jreParams = constructor.getParameters();
List<FormalParameter> params = new ArrayList<>();
for(Parameter jreParam : jreParams){
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType());
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),names.getName(inClass.getName()), name);
params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken()));
}
ParameterList parameterList = new ParameterList(params, new NullToken());
@ -83,11 +82,11 @@ public class ASTFactory {
public Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){
String name = jreMethod.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType;
returnType = createType(jreMethod.getReturnType());
returnType = createType(jreMethod.getReturnType(),names.getName(inClass.getName()), name);
Parameter[] jreParams = jreMethod.getParameters();
List<FormalParameter> params = new ArrayList<>();
for(Parameter jreParam : jreParams){
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType());
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),names.getName(inClass.getName()), name);
params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken()));
}
ParameterList parameterList = new ParameterList(params, new NullToken());
@ -99,14 +98,14 @@ public class ASTFactory {
return new Method(name,returnType, modifier, parameterList, block, gtvDeclarations, offset);
}
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());
gtvs.add(gtv);
}
return new GenericDeclarationList(gtvs,new NullToken());
}
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(), context, methodName);
gtvs.add(gtv);
}
return new GenericDeclarationList(gtvs,new NullToken());
}
/*
public RefType createType(java.lang.Class jreClass){
@ -119,7 +118,7 @@ public class ASTFactory {
}
*/
public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type){
public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){
if(type.getTypeName().equals("void")){
return new Void(new NullToken());
}else if(type.getTypeName().equals("int")){
@ -139,12 +138,14 @@ public class ASTFactory {
}else{
if(type instanceof TypeVariable){
//GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()"
return new GenericRefType(new GenericTypeName(type.getTypeName()), new NullToken());
return new GenericRefType(
new GenericTypeName(new GenericContext(parentClass, parentMethod),type.getTypeName()),
new NullToken());
}
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
if(type instanceof ParameterizedType){
for(Type t : ((ParameterizedType)type).getActualTypeArguments()){
params.add(createType(t));
params.add(createType(t, parentClass, parentMethod));
}
}
RefType ret = new RefType(this.names.getName(type.getTypeName()), params, new NullToken());
@ -152,15 +153,18 @@ public class ASTFactory {
}
}
public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String name){
public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String jreTVName, Class context, String parentMethod){
JavaClassName parentClass = names.getName(context.getName());
List<RefType> genericBounds = new ArrayList<>();
java.lang.reflect.Type[] bounds = jreTypeVar.getBounds();
if(bounds.length > 0){
for(java.lang.reflect.Type bound : bounds){
genericBounds.add((RefType) createType(bound));
genericBounds.add((RefType) createType(bound, parentClass, parentMethod));
}
}
return new de.dhbwstuttgart.syntaxtree.GenericTypeVar(name, genericBounds, new NullToken(), new NullToken());
return new de.dhbwstuttgart.syntaxtree.GenericTypeVar(
new GenericTypeName(new GenericContext(parentClass, parentMethod), jreTVName)
, genericBounds, new NullToken(), new NullToken());
}
public ClassOrInterface createObjectClass() {
@ -212,4 +216,4 @@ public class ASTFactory {
return createObjectClass().getType();
}
*/
}
}

View File

@ -3,8 +3,8 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
@ -23,7 +23,8 @@ public class Assign extends Statement
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = lefSide.getConstraints(info);
ret.addAll(rightSide.getConstraints(info));
ret.addUndConstraint(new Pair(rightSide.getType(), lefSide.getType()));
ret.addUndConstraint(ConstraintsFactory.createPair(
rightSide.getType(), lefSide.getType(), PairOperator.SMALLERDOT, info));
return ret;
}
}

View File

@ -31,8 +31,6 @@ public class Block extends Statement
}
return ret;
}
}

View File

@ -3,14 +3,16 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class CastExpr extends Expression
{
public CastExpr(RefTypeOrTPHOrWildcardOrGeneric castType, Expression expr, int offset)
public CastExpr(RefTypeOrTPHOrWildcardOrGeneric castType, Expression expr, Token offset)
{
super(null, null);
super(castType, offset);
this.expr = expr;
}
public Expression expr;

View File

@ -1,15 +1,17 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
public class EmptyStmt extends Statement
{
public EmptyStmt()
public EmptyStmt(Token offset)
{
super(null,null);
super(new Void(offset),offset);
}

View File

@ -6,9 +6,9 @@ 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 de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.HashSet;
import java.util.Set;
@ -31,8 +31,10 @@ public class FieldVar extends Expression {
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()));
constraint.add(ConstraintsFactory.createPair(
receiver.getType(),fieldAssumption.getReceiverType(), info));
constraint.add(ConstraintsFactory.createPair(
this.getType(),fieldAssumption.getType(), info));
oderConstraints.add(constraint);
}
if(oderConstraints.size() == 0)

View File

@ -15,7 +15,7 @@ public class ForStmt extends Statement
private Expression head_Initializer;
private Expression head_Condition;
private Expression head_Loop_expr;
private Statement body_Loop_block;
public Block body_Loop_block;
public ForStmt(int offset, int variableLength)
{

View File

@ -14,8 +14,8 @@ public class IfStmt extends Statement
public boolean hamaDebug = true; //hama: Debug Ausgaben von mir ein- bzw. ausschalten
public Expression expr;
public Statement then_block;
public Statement else_block;
public Block then_block;
public Block else_block;
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {

View File

@ -4,8 +4,9 @@ import de.dhbwstuttgart.exceptions.TypeinferenceException;
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 de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
@ -56,11 +57,11 @@ public class MethodCall extends Statement
Set<Constraint> methodConstraints = new HashSet<>();
for(MethodAssumption m : info.getMethods(name, arglist)){
Constraint methodConstraint = new Constraint();
methodConstraint.add(new Pair(receiver.getType(), m.getReceiverType()));
methodConstraint.add(new Pair(m.getReturnType(), this.getType()));
methodConstraint.add(ConstraintsFactory.createPair(receiver.getType(), m.getReceiverType(), PairOperator.SMALLERDOT, info));
methodConstraint.add(ConstraintsFactory.createPair(m.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info));
for(int i = 0;i<arglist.getArguments().size();i++){
methodConstraint.add(new Pair(m.getArgTypes().get(i),
arglist.getArguments().get(i).getType()));
methodConstraint.add(ConstraintsFactory.createPair(m.getArgTypes().get(i),
arglist.getArguments().get(i).getType(), PairOperator.SMALLERDOT, info));
}
methodConstraints.add(methodConstraint);
}

View File

@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import org.antlr.v4.runtime.Token;
@ -16,10 +17,12 @@ public class Return extends Statement
this.retexpr = retExpr;
}
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
return retexpr.getConstraints(info);
ConstraintSet ret = retexpr.getConstraints(info);
ret.addUndConstraint(ConstraintsFactory.createPair(
this.getType(),info.getCurrentMethod().getType(), info));
return ret;
}
// @Sebastian

View File

@ -0,0 +1,9 @@
package de.dhbwstuttgart.syntaxtree.statement;
import org.antlr.v4.runtime.Token;
public class ReturnVoid extends Return{
public ReturnVoid(Token offset) {
super(new EmptyStmt(offset), offset);
}
}

View File

@ -6,6 +6,7 @@ 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.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
@ -24,7 +25,7 @@ public class This extends Expression
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = new ConstraintSet();
ret.addUndConstraint(new Pair( this.getType(), info.getCurrentClass().getType(), PairOperator.EQUALSDOT));
ret.addUndConstraint(ConstraintsFactory.createPair( this.getType(), info.getCurrentClass().getType(), PairOperator.EQUALSDOT, info));
return ret;
}
}

View File

@ -14,7 +14,7 @@ public class WhileStmt extends Statement
}
public Expression expr;
public Statement loop_block;
public Block loop_block;
/**
* <br/>Author: Martin Pl�micke

View File

@ -12,5 +12,10 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
super(offset);
this.name = name;
}
public GenericTypeName getName(){
return name;
}
}

View File

@ -32,6 +32,11 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
this.name = (fullyQualifiedName);
}
@Override
public String toString(){
return this.name.toString();
}
@Override
public int hashCode() {
int hash = 0;

View File

@ -1,16 +1,10 @@
package de.dhbwstuttgart.syntaxtree.type;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import org.antlr.v4.runtime.Token;
public abstract class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode
{
public class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode{
public RefTypeOrTPHOrWildcardOrGeneric(Token offset) {
super(offset);
}
}

View File

@ -1,9 +1,22 @@
package de.dhbwstuttgart.typecheck;
import de.dhbwstuttgart.syntaxtree.GTVDeclarationContext;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
public class GenericTypeName extends JavaClassName {
public GenericTypeName(String name) {
private final static String DELIMITER = "%";
private final JavaClassName parentClass;
private final String methodName;
public GenericTypeName(GenericContext parentClass, String name) {
super(name);
this.parentClass = parentClass.parentClass;
this.methodName = parentClass.parentMethod;
}
public String toString(){
return parentClass.toString()
+ DELIMITER + methodName
+ DELIMITER + super.toString();
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.typeinference.assumptions;
import com.sun.istack.internal.NotNull;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
@ -10,7 +11,7 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation {
private Method methodContext;
private ClassOrInterface currentClass;
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses, ClassOrInterface currentClass, Method methodContext) {
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses, @NotNull ClassOrInterface currentClass, Method methodContext) {
super(availableClasses);
this.methodContext = methodContext;
this.currentClass = currentClass;
@ -19,4 +20,7 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation {
public ClassOrInterface getCurrentClass() {
return currentClass;
}
public Method getCurrentMethod() {
return methodContext;
}
}

View File

@ -0,0 +1,47 @@
package de.dhbwstuttgart.typeinference.constraints;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ConstraintsFactory {
public static Pair createPair(RefTypeOrTPHOrWildcardOrGeneric t1, RefTypeOrTPHOrWildcardOrGeneric t2, PairOperator equalsdot, TypeInferenceBlockInformation info){
//Check whether Generics are in the same class:
return new Pair(checkGeneric(t1, info), checkGeneric(t2, info));
}
public static Pair createPair(RefTypeOrTPHOrWildcardOrGeneric t1,
RefTypeOrTPHOrWildcardOrGeneric t2, TypeInferenceBlockInformation info){
return createPair(t1,t2,PairOperator.SMALLERDOT, info);
}
private static RefTypeOrTPHOrWildcardOrGeneric checkGeneric(RefTypeOrTPHOrWildcardOrGeneric type, TypeInferenceBlockInformation info){
if(type instanceof GenericRefType){
List<GenericTypeVar> genericsInContext = new ArrayList<>();
for(GenericTypeVar genericTypeVar : info.getCurrentClass().getGenerics()){
genericsInContext.add(genericTypeVar);
}
if(info.getCurrentMethod() != null)for(GenericTypeVar genericTypeVar : info.getCurrentMethod().getGenerics()){
genericsInContext.add(genericTypeVar);
}
for(GenericTypeVar genericTypeVar : genericsInContext){
if(genericTypeVar.getName().equals(((GenericRefType)type).getName())){
return new RefType(((GenericRefType)type).getName(),type.getOffset());
}
}
//Nicht in den Generics in diesem Kontext enthalten:
return TypePlaceholder.fresh(type.getOffset());
}else{
return type;
}
}
}

View File

@ -4,6 +4,7 @@ import java.io.Serializable;
import com.sun.istack.internal.NotNull;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class Pair implements Serializable
@ -13,7 +14,7 @@ public class Pair implements Serializable
private PairOperator eOperator = PairOperator.SMALLER;
public Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2 )
Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2 )
{
this.TA1 = TA1;
this.TA2 = TA2;
@ -22,7 +23,7 @@ public class Pair implements Serializable
eOperator = PairOperator.SMALLER;
}
public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp)
Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp)
{
// Konstruktor
this(TA1,TA2);

View File

@ -27,9 +27,11 @@ public class GeneralParserTest{
List<String> filenames = new ArrayList<String>();
filenames.add("FieldInitializationTest.jav");
filenames.add("ImportTest.jav");
filenames.add("CastTest.jav");
filenames.add("StatementsTest.jav");
filenames.add("Methods.jav");
filenames.add("ImportTestGeneric.jav");
filenames.add("CastTest.jav");
//filenames.add("BoundedParameter.jav");
//filenames.add("GenericFieldVarTest.jav");
filenames.add("FieldVarTest.jav");

View File

@ -16,7 +16,7 @@ public class JavaTXCompilerTest {
@Test
public void test() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler();
compiler.parse(new File(rootDirectory+"Methods.jav"));
//compiler.parse(new File(rootDirectory+"Methods.jav"));
compiler.parse(new File(rootDirectory+"Generics.jav"));
compiler.typeInference();
}