From 934e5f85f817f709559f5d86fe08487929871b7a Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 26 Oct 2017 21:45:04 +0200 Subject: [PATCH] Unterschied zwischen Expr-Receiver und Classname-Receiver --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 39 +++--------------- .../bytecode/BytecodeGenLambda.java | 30 +------------- .../bytecode/BytecodeGenMethod.java | 41 ++++--------------- .../StatementGenerator.java | 14 +++++-- .../syntaxtree/AbstractASTWalker.java | 2 +- .../syntaxtree/StatementVisitor.java | 4 +- .../statement/ExpressionReceiver.java | 21 ++++++++++ .../syntaxtree/statement/NewClass.java | 2 +- .../syntaxtree/statement/Receiver.java | 24 +++-------- .../syntaxtree/statement/StaticClassName.java | 2 +- .../syntaxtree/statement/SuperCall.java | 2 +- .../syntaxtree/visual/OutputGenerator.java | 10 ++--- .../visual/TypeOutputGenerator.java | 2 +- .../typeinference/typeAlgo/TYPEStmt.java | 2 +- 14 files changed, 67 insertions(+), 128 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/ExpressionReceiver.java diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 6f0ac91e..c21ee60b 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import de.dhbwstuttgart.syntaxtree.statement.*; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; @@ -12,33 +13,6 @@ import org.objectweb.asm.Opcodes; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; -import de.dhbwstuttgart.syntaxtree.statement.Assign; -import de.dhbwstuttgart.syntaxtree.statement.AssignToField; -import de.dhbwstuttgart.syntaxtree.statement.Binary; -import de.dhbwstuttgart.syntaxtree.statement.Block; -import de.dhbwstuttgart.syntaxtree.statement.CastExpr; -import de.dhbwstuttgart.syntaxtree.statement.DoStmt; -import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt; -import de.dhbwstuttgart.syntaxtree.statement.FieldVar; -import de.dhbwstuttgart.syntaxtree.statement.ForStmt; -import de.dhbwstuttgart.syntaxtree.statement.IfStmt; -import de.dhbwstuttgart.syntaxtree.statement.InstanceOf; -import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; -import de.dhbwstuttgart.syntaxtree.statement.LocalVar; -import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; -import de.dhbwstuttgart.syntaxtree.statement.MethodCall; -import de.dhbwstuttgart.syntaxtree.statement.NewArray; -import de.dhbwstuttgart.syntaxtree.statement.NewClass; -import de.dhbwstuttgart.syntaxtree.statement.Receiver; -import de.dhbwstuttgart.syntaxtree.statement.Return; -import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid; -import de.dhbwstuttgart.syntaxtree.statement.StaticClassName; -import de.dhbwstuttgart.syntaxtree.statement.Super; -import de.dhbwstuttgart.syntaxtree.statement.SuperCall; -import de.dhbwstuttgart.syntaxtree.statement.This; -import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus; -import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; @@ -281,12 +255,6 @@ public class BytecodeGen implements ASTVisitor { } - @Override - public void visit(Receiver receiver) { - // TODO Auto-generated method stub - - } - @Override public void visit(Return aReturn) { // TODO Auto-generated method stub @@ -379,6 +347,11 @@ public class BytecodeGen implements ASTVisitor { @Override public void visit(SuperCall superCall) { + + } + + @Override + public void visit(ExpressionReceiver expressionReceiver) { // TODO Auto-generated method stub } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java index 4b1557b0..679a1365 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java @@ -1,36 +1,10 @@ package de.dhbwstuttgart.bytecode; +import de.dhbwstuttgart.syntaxtree.statement.*; import org.objectweb.asm.MethodVisitor; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; -import de.dhbwstuttgart.syntaxtree.statement.Assign; -import de.dhbwstuttgart.syntaxtree.statement.AssignToField; -import de.dhbwstuttgart.syntaxtree.statement.Binary; -import de.dhbwstuttgart.syntaxtree.statement.Block; -import de.dhbwstuttgart.syntaxtree.statement.CastExpr; -import de.dhbwstuttgart.syntaxtree.statement.DoStmt; -import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt; -import de.dhbwstuttgart.syntaxtree.statement.FieldVar; -import de.dhbwstuttgart.syntaxtree.statement.ForStmt; -import de.dhbwstuttgart.syntaxtree.statement.IfStmt; -import de.dhbwstuttgart.syntaxtree.statement.InstanceOf; -import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; -import de.dhbwstuttgart.syntaxtree.statement.LocalVar; -import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; -import de.dhbwstuttgart.syntaxtree.statement.MethodCall; -import de.dhbwstuttgart.syntaxtree.statement.NewArray; -import de.dhbwstuttgart.syntaxtree.statement.NewClass; -import de.dhbwstuttgart.syntaxtree.statement.Receiver; -import de.dhbwstuttgart.syntaxtree.statement.Return; -import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid; -import de.dhbwstuttgart.syntaxtree.statement.StaticClassName; -import de.dhbwstuttgart.syntaxtree.statement.Super; -import de.dhbwstuttgart.syntaxtree.statement.SuperCall; -import de.dhbwstuttgart.syntaxtree.statement.This; -import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus; -import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; @@ -142,7 +116,7 @@ public class BytecodeGenLambda implements StatementVisitor{ } @Override - public void visit(Receiver receiver) { + public void visit(ExpressionReceiver receiver) { // TODO Auto-generated method stub } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index c65e691c..d92ebfc3 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -8,6 +8,7 @@ import java.lang.invoke.MethodType; import java.security.GeneralSecurityException; import java.util.HashMap; +import de.dhbwstuttgart.syntaxtree.statement.*; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Handle; import org.objectweb.asm.MethodVisitor; @@ -17,35 +18,6 @@ import org.objectweb.asm.Type; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; -import de.dhbwstuttgart.syntaxtree.statement.Assign; -import de.dhbwstuttgart.syntaxtree.statement.AssignToField; -import de.dhbwstuttgart.syntaxtree.statement.Binary; -import de.dhbwstuttgart.syntaxtree.statement.Block; -import de.dhbwstuttgart.syntaxtree.statement.CastExpr; -import de.dhbwstuttgart.syntaxtree.statement.DoStmt; -import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt; -import de.dhbwstuttgart.syntaxtree.statement.Expression; -import de.dhbwstuttgart.syntaxtree.statement.FieldVar; -import de.dhbwstuttgart.syntaxtree.statement.ForStmt; -import de.dhbwstuttgart.syntaxtree.statement.IfStmt; -import de.dhbwstuttgart.syntaxtree.statement.InstanceOf; -import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; -import de.dhbwstuttgart.syntaxtree.statement.LocalVar; -import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; -import de.dhbwstuttgart.syntaxtree.statement.MethodCall; -import de.dhbwstuttgart.syntaxtree.statement.NewArray; -import de.dhbwstuttgart.syntaxtree.statement.NewClass; -import de.dhbwstuttgart.syntaxtree.statement.Receiver; -import de.dhbwstuttgart.syntaxtree.statement.Return; -import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid; -import de.dhbwstuttgart.syntaxtree.statement.Statement; -import de.dhbwstuttgart.syntaxtree.statement.StaticClassName; -import de.dhbwstuttgart.syntaxtree.statement.Super; -import de.dhbwstuttgart.syntaxtree.statement.SuperCall; -import de.dhbwstuttgart.syntaxtree.statement.This; -import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus; -import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -230,14 +202,19 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(MethodCall methodCall) { System.out.println(" In Methodcall: (" +methodCall.name+")" ); - System.out.println(" Method-Receiver: "+methodCall.receiver.expr); + System.out.print(" Method-Receiver: "); + if(methodCall.receiver instanceof ExpressionReceiver){ + System.out.print(((ExpressionReceiver) methodCall.receiver).expr + "\n"); + }else{ + System.out.print(((StaticClassName) methodCall.receiver).getType().toString() + "\n"); + } methodCall.receiver.accept(this); methodCall.arglist.accept(this); Descriptor mDesc = new Descriptor(methodCall.arglist, methodCall.getType()); - mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, methodCall.receiver.expr.getType().toString(), + mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, methodCall.receiver.getType().toString(), methodCall.name, mDesc.getDesc(), false); // test if(!methodCall.getType().toString().equals("V")) { @@ -270,7 +247,7 @@ public class BytecodeGenMethod implements StatementVisitor{ } @Override - public void visit(Receiver receiver) { + public void visit(ExpressionReceiver receiver) { System.out.println(" in Receiver"); System.out.println(" expr : " + receiver.expr); receiver.expr.accept(this); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 9da5777d..f4ada6b2 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -179,6 +179,14 @@ public class StatementGenerator { }else throw new NotImplementedException(); } + public Receiver getReceiver(Expression expr){ + if(expr instanceof StaticClassName){ + return (Receiver) expr; + }else { + return new ExpressionReceiver(expr); + } + } + private Statement convert(Java8Parser.MethodInvocationContext methodInvocationContext) { String name; if(methodInvocationContext.methodName()!=null){ @@ -200,7 +208,7 @@ public class StatementGenerator { }else throw new NotImplementedException(); ArgumentList argumentList = convert(methodInvocationContext.argumentList()); - MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()),new Receiver(receiver), name, argumentList, methodInvocationContext.getStart()); + MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), getReceiver(receiver), name, argumentList, methodInvocationContext.getStart()); return ret; } @@ -701,7 +709,7 @@ public class StatementGenerator { }else { Java8Parser.MethodInvocation_lf_primaryContext ctxt = e.methodInvocation_lf_primary(); String methodName = ctxt.Identifier().toString(); - return new MethodCall(TypePlaceholder.fresh(e.getStart()), new Receiver(expr), methodName, convert(ctxt.argumentList()), e.getStart()); + return new MethodCall(TypePlaceholder.fresh(e.getStart()), getReceiver(expr), methodName, convert(ctxt.argumentList()), e.getStart()); } } @@ -809,7 +817,7 @@ public class StatementGenerator { } ArgumentList argumentList = convert(methodInvocationContext.argumentList()); - MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), new Receiver(receiver), name, argumentList, methodInvocationContext.getStart()); + MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), getReceiver(receiver), name, argumentList, methodInvocationContext.getStart()); return ret; } diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index cd3027f5..50b4b470 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -200,7 +200,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{ } @Override - public void visit(Receiver receiver) { + public void visit(ExpressionReceiver receiver) { receiver.expr.accept(this); } diff --git a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index b63e0438..195db230 100644 --- a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -40,8 +40,6 @@ public interface StatementVisitor { void visit(NewArray newArray); - void visit(Receiver receiver); - void visit(Return aReturn); void visit(ReturnVoid aReturn); @@ -67,4 +65,6 @@ public interface StatementVisitor { void visit(AssignToLocal assignLeftSide); void visit(SuperCall superCall); + + void visit(ExpressionReceiver expressionReceiver); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ExpressionReceiver.java b/src/de/dhbwstuttgart/syntaxtree/statement/ExpressionReceiver.java new file mode 100644 index 00000000..409cfe68 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ExpressionReceiver.java @@ -0,0 +1,21 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; + +public class ExpressionReceiver extends Receiver +{ + public final Expression expr; + + public ExpressionReceiver(Expression expr) + { + super(expr.getType(), expr.getOffset()); + this.expr = expr; + } + + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java index b6f2a795..258da2e5 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java @@ -31,7 +31,7 @@ public class NewClass extends MethodCall * @param start */ public NewClass(RefType newClass, ArgumentList args, Token start) { - super(newClass, new Receiver(new EmptyStmt(start)), newClass.getName().toString(), args, start); + super(newClass, new ExpressionReceiver(new EmptyStmt(start)), newClass.getName().toString(), args, start); } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Receiver.java b/src/de/dhbwstuttgart/syntaxtree/statement/Receiver.java index 09bc191c..93ee36a0 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Receiver.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Receiver.java @@ -1,26 +1,12 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; - -public class Receiver extends Expression +public abstract class Receiver extends Expression { - public Expression expr; - /** - * Autor: J�rg B�uerle - * @param expr - */ - public Receiver(Expression expr) + public Receiver(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { - super(expr.getType(), expr.getOffset()); - this.expr = expr; - } - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); + super(type, offset); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java b/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java index d0ddb31c..a7089d8c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java @@ -8,7 +8,7 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import org.antlr.v4.runtime.Token; -public class StaticClassName extends Expression { +public class StaticClassName extends Receiver { public StaticClassName(JavaClassName className, Token offset) { super(new RefType(className, offset), offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java index 572f1b11..42cfb8ee 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java @@ -17,7 +17,7 @@ public class SuperCall extends MethodCall } public SuperCall(ArgumentList argumentList, Token offset){ - super(new Void(offset), new Receiver(new This(offset)), "", argumentList, offset); + super(new Void(offset), new ExpressionReceiver(new This(offset)), "", argumentList, offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 0befcb2b..4b4ac7dc 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -270,11 +270,6 @@ public class OutputGenerator implements ASTVisitor { } - @Override - public void visit(Receiver receiver) { - receiver.expr.accept(this); - } - @Override public void visit(Return aReturn) { out.append("return "); @@ -349,4 +344,9 @@ public class OutputGenerator implements ASTVisitor { superCall.arglist.accept(this); out.append(")"); } + + @Override + public void visit(ExpressionReceiver receiver) { + receiver.expr.accept(this); + } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java index c0628ef9..dcefafa4 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java @@ -181,7 +181,7 @@ public class TypeOutputGenerator extends OutputGenerator { } @Override - public void visit(Receiver receiver) { + public void visit(ExpressionReceiver receiver) { super.visit(receiver); } diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index e50a8b1e..bf063d71 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -184,7 +184,7 @@ public class TYPEStmt implements StatementVisitor{ } @Override - public void visit(Receiver receiver) { + public void visit(ExpressionReceiver receiver) { receiver.expr.accept(this); }