MethodCall implementiert

This commit is contained in:
JanUlrich 2015-08-27 16:36:19 +02:00
parent 133740d1f8
commit 64bed0c60d
15 changed files with 73 additions and 16 deletions

View File

@ -8,6 +8,6 @@
<classpathentry kind="lib" path="lib/junit-4.0.jar" sourcepath="/home/janulrich/.m2/repository/junit/junit/4.0/junit-4.0-sources.jar"/>
<classpathentry kind="lib" path="lib/cloning.jar"/>
<classpathentry kind="lib" path="lib/guava-10.0.1.jar"/>
<classpathentry kind="lib" path="lib/bcel-6.0-SNAPSHOT.jar" sourcepath="/home/janulrich/Downloads/bcel-5.2-src.zip"/>
<classpathentry kind="lib" path="lib/bcel-6.0-SNAPSHOT.jar" sourcepath="lib/bcel-5.2-src.zip"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -1353,5 +1353,9 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
public boolean isClass() {
return true;
}
public boolean isInterface(){
return false;
}
}
// ino.end

View File

@ -174,7 +174,7 @@ public abstract class Field extends GTVDeclarationContext implements TypeInserta
}
/**
* Fügt das das Feld zu ClassGen hinzu
* F<EFBFBD>gt das das Feld zu ClassGen hinzu
* @param cg
*/
public abstract void genByteCode(ClassGen cg);

View File

@ -4,7 +4,6 @@ package de.dhbwstuttgart.syntaxtree;
// ino.module.Interface.8582.import
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.core.AClassOrInterface;
import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
@ -44,6 +43,10 @@ public class Interface extends Class {
}
@Override
public boolean isInterface(){
return true;
}
}

View File

@ -129,6 +129,7 @@ public class SourceFile
public Menge<Interface> InterfaceVektor = new Menge<Interface>();
// ino.end
/**
* Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei.
* SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar.

View File

@ -175,14 +175,14 @@ public class Assign extends Expr
}
public static int counterAssign = 0; //Zaehlvariable für ISTORE
public static int counterAssign = 0; //Zaehlvariable f<EFBFBD>r ISTORE
@Override
public InstructionList genByteCode(ClassGen cg) {
public InstructionList genByteCode(ClassGen cg, TypeAssumptions ass) {
// TODO Auto-generated method stub
InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool());
InstructionList il = expr2.genByteCode(cg);//expr2 rechte expr
counterAssign++; //macht STORE für meherere Variable nutzbar (nicht nur ISTORE_1, ISTORE_2, etc.)
InstructionList il = expr2.genByteCode(cg, ass);//expr2 rechte expr
counterAssign++; //macht STORE f<EFBFBD>r meherere Variable nutzbar (nicht nur ISTORE_1, ISTORE_2, etc.)
String expr2Type = expr2.getType().get_Name().toString();

View File

@ -217,7 +217,7 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) {
@Override
public InstructionList genByteCode(ClassGen _cg) {
public InstructionList genByteCode(ClassGen _cg, TypeAssumptions ass) {
InstructionList linkeSeite = this.expr1.genByteCode(_cg);
InstructionList rechteSeite = this.expr2.genByteCode(_cg);
if(this.getReturnType().getName().equals(new JavaClassName("String"))){

View File

@ -113,6 +113,6 @@ public abstract class Expr extends ExprStmt
throw new NotImplementedException(); //wird die TYPEStmt-Methode innerhalb einer Expr aufgerufen, dann ist etwas schief gelaufen.
}
public abstract InstructionList genByteCode(ClassGen _cg);
//public abstract InstructionList genByteCode(ClassGen _cg, TypeAssumptions ass);
}
// ino.end

View File

@ -4,7 +4,10 @@ package de.dhbwstuttgart.syntaxtree.statement;
// ino.module.MethodCall.8639.import
import java.util.Hashtable;
import org.apache.bcel.Constants;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import de.dhbwstuttgart.typeinference.Menge;
@ -206,6 +209,9 @@ public class MethodCall extends Expr
//Noch das Overloading-Constraint anhängen:
ret.add(overloading(assumptions));
de.dhbwstuttgart.syntaxtree.Class receiverCl = assumptions.getClassAssumptionFor(receiver.get_Expr().getType()).getAssumedClass();
this.receiver.setClass(receiverCl);
return ret;
}
@ -308,9 +314,36 @@ public class MethodCall extends Expr
@Override
public InstructionList genByteCode(ClassGen _cg) {
//Herausfinden, ob eine Methode oder ein Interface aufgerufen wird:
//this.receiver.get_Expr().getType()
InstructionList il = new InstructionList();
InstructionFactory _factory = new InstructionFactory(_cg, _cg.getConstantPool());
il.append(receiver.get_Expr().genByteCode(_cg));
//Herausfinden, ob eine Methode oder ein Interface aufgerufen wird:
Type receiverType = this.receiver.get_Expr().getType();
de.dhbwstuttgart.syntaxtree.Class receiverClass = this.receiver.getReceiverClass();
short kind = 0;
if(receiverClass.isInterface()){
kind = Constants.INVOKEINTERFACE;
}else{//Ansonsten muss es eine Klasse sein:
kind = Constants.INVOKEVIRTUAL;
}
org.apache.bcel.generic.Type[] argumentTypen = org.apache.bcel.generic.Type.NO_ARGS;
if(this.getArgumentList() != null && this.getArgumentList().size()>0){
argumentTypen = new org.apache.bcel.generic.Type[this.getArgumentList().size()];
int i = 0;
for(Expr argument : this.arglist.expr){
argumentTypen[i] = argument.getType().getBytecodeType();
//Das Argument auf den Stack legen:
il.append(argument.genByteCode(_cg));
i++;
}
}
org.apache.bcel.generic.Type returnType = this.getType().getBytecodeType();
il.append(_factory.createInvoke(receiver.getReceiverClass().getName().toString(), this.get_Name(), returnType , argumentTypen, kind));
return il;
}
}

View File

@ -27,6 +27,7 @@ public class Receiver
{
// ino.attribute.expr.26126.declaration
private Expr expr;
private de.dhbwstuttgart.syntaxtree.Class receiverClass;
// ino.end
// ino.attribute.parserlog.26129.declaration
protected static Logger parserlog = Logger.getLogger("parser");
@ -89,6 +90,13 @@ public class Receiver
}
// ino.end
public void setClass(Class cl){
this.receiverClass = cl;
}
public Class getReceiverClass(){
return this.receiverClass;
}
public JavaCodeResult printJavaCode(ResultSet resultSet) {
return new JavaCodeResult().attach(this.get_Expr().printJavaCode(resultSet));

View File

@ -151,7 +151,6 @@ public class This extends Expr
public InstructionList genByteCode(ClassGen _cg) {
InstructionList il = new InstructionList();
InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(org.apache.bcel.generic.Type.OBJECT, 0));
InstructionHandle ih_1 = il.append(InstructionFactory.createReturn(org.apache.bcel.generic.Type.OBJECT));
return il;
}

View File

@ -6,12 +6,13 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.core.IItemWithOffset;
import de.dhbwstuttgart.myexception.SCException;
import de.dhbwstuttgart.parser.JavaClassName;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
import de.dhbwstuttgart.typeinference.JavaCodeResult;

View File

@ -408,6 +408,13 @@ public class TypeAssumptions {
return this.classAssumptions;
}
public ClassAssumption getClassAssumptionFor(Type t){
for(ClassAssumption cA : this.getClassAssumptions()){
if(cA.getAssumedClass().getType().equals(t))return cA;
}
throw new TypeinferenceException("Der Typ "+t+" ist nicht in den Assumptions vorhanden", t);
}
public Type checkType(RefType type, SyntaxTreeNode parent) {
Type t = this.getTypeFor(type, parent); //Richtigkeit des Typnahmensprüfen
type.setName(t.getName()); //Und diesen auf den Typ anwenden

View File

@ -1,7 +1,7 @@
class Assign{
class MethodCall{
void method() {
method();
method2();
}
void method2(){}

View File

@ -6,5 +6,6 @@ public static void main(String[] args){
new EmptyClass();
new Assign();
System.out.println(new Return().method());
new MethodCall().method();
}
}