diff --git a/src/de/dhbwstuttgart/parser/JavaParser.java b/src/de/dhbwstuttgart/parser/JavaParser.java index 8d69c14d..6728d76b 100644 --- a/src/de/dhbwstuttgart/parser/JavaParser.java +++ b/src/de/dhbwstuttgart/parser/JavaParser.java @@ -1852,14 +1852,14 @@ case 146: case 147: // line 1224 "./../src/de/dhbwstuttgart/parser/JavaParser.jay" { - This THCON = new This(((Token)yyVals[-3+yyTop]).getOffset(),((Token)yyVals[-3+yyTop]).getLexem().length()); + ThisCall THCON = new ThisCall(((Token)yyVals[-3+yyTop]).getOffset(),((Token)yyVals[-3+yyTop]).getLexem().length()); yyVal=THCON; } break; case 148: // line 1229 "./../src/de/dhbwstuttgart/parser/JavaParser.jay" { - This THCONargl = new This(((Token)yyVals[-4+yyTop]).getOffset(),((Token)yyVals[-4+yyTop]).getLexem().length()); + ThisCall THCONargl = new ThisCall(((Token)yyVals[-4+yyTop]).getOffset(),((Token)yyVals[-4+yyTop]).getLexem().length()); THCONargl.set_ArgumentList(((ArgumentList)yyVals[-2+yyTop])); yyVal=THCONargl; } diff --git a/src/de/dhbwstuttgart/parser/JavaParser.jay b/src/de/dhbwstuttgart/parser/JavaParser.jay index 4e3c77c6..75980805 100755 --- a/src/de/dhbwstuttgart/parser/JavaParser.jay +++ b/src/de/dhbwstuttgart/parser/JavaParser.jay @@ -1222,12 +1222,12 @@ formalparameterlist :formalparameter explicitconstructorinvocation : THIS '(' ')' ';' { - This THCON = new This($1.getOffset(),$1.getLexem().length()); + ThisCall THCON = new ThisCall($1.getOffset(),$1.getLexem().length()); $$=THCON; } |THIS '(' argumentlist ')' ';' { - This THCONargl = new This($1.getOffset(),$1.getLexem().length()); + ThisCall THCONargl = new ThisCall($1.getOffset(),$1.getLexem().length()); THCONargl.set_ArgumentList($3); $$=THCONargl; } diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 1269e6b7..515364c4 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -43,6 +43,7 @@ public class Constructor extends Method { ret.addAssumption(new ConstructorAssumption(this, parentClass)); return ret; } + /* @Override public void parserPostProcessing(SyntaxTreeNode parent){ if(this.parameterlist != null){ @@ -54,6 +55,7 @@ public class Constructor extends Method { gtv.parserPostProcessing(this); } } + */ @Override public ConstraintsSet TYPE(TypeAssumptions ass) { //super.setType(this.getParentClass().getType()); @@ -62,7 +64,8 @@ public class Constructor extends Method { } @Override public void setType(Type t) { - throw new TypeinferenceException("Einem Konstruktor kann kein Typ zugewiesen werden", this); + super.setType(new Void(this, this.getOffset())); + //throw new TypeinferenceException("Einem Konstruktor kann kein Typ zugewiesen werden", this); //this.methode.setType(t); } /* diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java index ce314c62..fd7526bd 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java @@ -6,7 +6,6 @@ import java.util.Hashtable; import java.util.Vector; import de.dhbwstuttgart.logger.Logger; - import de.dhbwstuttgart.bytecode.ClassFile; import de.dhbwstuttgart.bytecode.CodeAttribute; import de.dhbwstuttgart.myexception.JVMCodeException; @@ -17,11 +16,16 @@ import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.Void; +import de.dhbwstuttgart.typeinference.ConstraintType; import de.dhbwstuttgart.typeinference.ConstraintsSet; import de.dhbwstuttgart.typeinference.JavaCodeResult; +import de.dhbwstuttgart.typeinference.OderConstraint; import de.dhbwstuttgart.typeinference.Overloading; import de.dhbwstuttgart.typeinference.ResultSet; +import de.dhbwstuttgart.typeinference.UndConstraint; +import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; +import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; @@ -202,11 +206,67 @@ public class MethodCall extends Expr } //Noch das Overloading-Constraint anhängen: - ret.add(new Overloading(assumptions, this, this.getType()).generateConsstraints()); + ret.add(overloading(assumptions)); return ret; } + /** + * Erstellt die Constraints für den eigentlichen Methodenaufruf. + * Sucht in den Assumptions nach passenden Methoden und erstellt ein OderConstraintSet. + * @param assumptions + * @return + */ + ConstraintsSet overloading(TypeAssumptions assumptions){ + ConstraintsSet ret = new ConstraintsSet(); + //ret.add(new Overloading(assumptions, this, this.getType()).generateConsstraints()); + OderConstraint oCons = new OderConstraint(); + Vector methodAssumptions = assumptions.getMethodAssumptions(this.getName(), this.getArgumentList().size()); + if(methodAssumptions.size()==0)throw new TypeinferenceException("Eine Methode "+this.get_Name()+" ist in den Assumptions nicht vorhanden", this); + //Alle möglichen Methoden durchgehen: + for(MethodAssumption methodAssumption : methodAssumptions){ + //Constraint nicht erstellen, falls Rückgabetyp von vorne herein nicht übereinstimmt: + if(!(this.type instanceof TypePlaceholder) && !this.type.equals(methodAssumption.getAssumedType()))break; + oCons.addConstraint(constraintsFromMethodAssumption(methodAssumption, assumptions)); + } + return ret; + } + + /** + * Spezifikation: + * overloading determines for all possible overloadings and overridings + * of a method the constraints, where constraints itself forms + * the constraints from the receiver type, the argument types, the return + * type and a given type assumption for the method. If it is a + * method from a class, which is not the actual class (this), all type + * variables are replaced by fresh type variables (fresh), as different + * instances can occur. sargs determines all type assumptions of a + * method, where the argument types are supertypes of a minimal type + * assumption. + * + * @TODO: wenn es sich um eine Methode einer anderen Klasse handelt, müssen neue TPH vergeben werden und nicht die der Assumption verwendet werden. + * + * @return + */ + UndConstraint constraintsFromMethodAssumption(MethodAssumption methodAssumption, TypeAssumptions assumptions){ + UndConstraint methodConstraint = new UndConstraint(); + //Ein Constraint für den ReturnType der Methode... + methodConstraint.addConstraint(methodAssumption.getAssumedType().TYPE(assumptions, this), type.TYPE(assumptions, this)); + //Ein Constraint für die Parameter der Methode... + for(int i=0; i parameterList = new Vector(); for(Expr argument : methodCall.getArgumentList().expr){ @@ -64,25 +64,13 @@ public class Overloading{ if(methodAssumptions.size()==0)throw new TypeinferenceException("Eine Methode "+methodCall.get_Name()+" ist in den Assumptions nicht vorhanden", methodCall); //Alle möglichen Methoden durchgehen: for(MethodAssumption methodAssumption : methodAssumptions){ + //Constraint nicht erstellen, falls Rückgabetyp von vorne herein nicht übereinstimmt: if(!(this.type instanceof TypePlaceholder) && !this.type.equals(methodAssumption.getAssumedType()))break; - UndConstraint methodConstraint = new UndConstraint(); - //Ein Constraint für den ReturnType der Methode... - methodConstraint.addConstraint(methodAssumption.getAssumedType().TYPE(assumptions, methodCall), type.TYPE(assumptions, methodCall)); - //Ein Constraint für die Parameter der Methode... - for(int i=0; i getMethodAssumptions(String methodName, Vector parameter){ - int parameterCount = parameter.size(); + public Vector getMethodAssumptions(String methodName, int parameterCount){ Vector ret = new Vector(); for(MethodAssumption ass : this.methodAssumptions){ if(ass.getMethodName().equals(methodName) && ass.getParaCount() == parameterCount){ - ret.add(ass); } } @@ -375,14 +373,12 @@ public class TypeAssumptions { /** * - * @param name - * @param size * @return Null, falls kein Konstruktor vorhanden. */ - public ConstructorAssumption getConstructorAssumption(String name, int size) { + public ConstructorAssumption getConstructorAssumption(String className, int paramCount) { for(ConstructorAssumption ca : this.constructorAssumptions){ log.debug("Durchsuche Assumptions: "+ca.getIdentifier().toString() +" -Anzahl Parameter: "+ ca.getParaCount(), Section.TYPEINFERENCE); - if(ca.getParaCount()==size && ca.getIdentifier().equals(name))return ca; + if(ca.getParaCount()==paramCount && ca.getIdentifier().equals(className))return ca; } return null; }