TypeinferenceException überarbeitet

This commit is contained in:
JanUlrich 2014-04-15 14:56:20 +02:00
parent 0345dceb8e
commit 87e4f2fd36
27 changed files with 163 additions and 85 deletions

View File

@ -46,8 +46,9 @@ import typinferenz.FunNInterface;
import typinferenz.ResultSet; import typinferenz.ResultSet;
// ino.end // ino.end
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.ParserException; import typinferenz.exceptions.DebugException;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.ParserError;
import typinferenz.exceptions.TypeinferenceException;
@ -756,7 +757,7 @@ public class MyCompiler implements MyCompilerAPI
/** /**
* Parst den Inhalt einer Datei zu einem Syntaxbaum. * Parst den Inhalt einer Datei zu einem Syntaxbaum.
*/ */
private SourceFile parse2SyntaxTree(Reader fileContent){ private SourceFile parse2SyntaxTree(Reader fileContent) throws ParserError{
//StringReader reader = new StringReader(fileContent); //StringReader reader = new StringReader(fileContent);
////////////////////////////////////// //////////////////////////////////////
@ -773,7 +774,7 @@ public class MyCompiler implements MyCompilerAPI
srcFile = (SourceFile) parser.yyparse( scanner ); srcFile = (SourceFile) parser.yyparse( scanner );
} catch (IOException | yyException e) { } catch (IOException | yyException e) {
e.printStackTrace(); e.printStackTrace();
if(e instanceof yyException)throw new ParserException((yyException)e); if(e instanceof yyException)throw new ParserError((yyException)e);
} }
////////////////////////////////////// //////////////////////////////////////
// Postprocessing: // Postprocessing:
@ -786,7 +787,7 @@ public class MyCompiler implements MyCompilerAPI
/** /**
* Diese Funktion nimmt einen Vector von Dateinamen. Alle diese Dateien werden zu einem SyntaxBaum geparst. * Diese Funktion nimmt einen Vector von Dateinamen. Alle diese Dateien werden zu einem SyntaxBaum geparst.
*/ */
public void parse(Vector<String> filenames) { public void parse(Vector<String> filenames) throws ParserError {
for(String filename : filenames){ for(String filename : filenames){
StringBuffer fileData = new StringBuffer(); StringBuffer fileData = new StringBuffer();
@ -795,7 +796,7 @@ public class MyCompiler implements MyCompilerAPI
reader = new BufferedReader( reader = new BufferedReader(
new FileReader(filename)); new FileReader(filename));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new TypinferenzException("Die Datei "+ filename+" konnte nicht gelesen werden."); throw new DebugException("Die Datei "+ filename+" konnte nicht gelesen werden.");
} }
char[] buf = new char[1024]; char[] buf = new char[1024];
int numRead=0; int numRead=0;

View File

@ -9,6 +9,8 @@ import java.io.IOException;
import java.util.Vector; import java.util.Vector;
import typinferenz.ResultSet; import typinferenz.ResultSet;
import typinferenz.exceptions.ParserError;
import typinferenz.exceptions.TypeinferenceException;
import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.ClassFile;
import mycompiler.myexception.CTypeReconstructionException; import mycompiler.myexception.CTypeReconstructionException;
import mycompiler.myexception.JVMCodeException; import mycompiler.myexception.JVMCodeException;
@ -84,7 +86,7 @@ public interface MyCompilerAPI
// ino.end // ino.end
// ino.method.typeReconstruction.21340.declaration // ino.method.typeReconstruction.21340.declaration
public Vector<TypeinferenceResultSet> typeReconstruction() public Vector<TypeinferenceResultSet> typeReconstruction()
throws NullPointerException, CTypeReconstructionException; throws NullPointerException, TypeinferenceException;
// ino.end // ino.end
// ino.method.codeGeneration.21346.decldescription type=javadoc // ino.method.codeGeneration.21346.decldescription type=javadoc
@ -125,14 +127,14 @@ public interface MyCompilerAPI
* Parst zusammenhängende JavaKlassen in verschiedenen Dateien. * Parst zusammenhängende JavaKlassen in verschiedenen Dateien.
* @param filenames - Eine Liste von Quellcodedateien, welche gseparst werden sollen * @param filenames - Eine Liste von Quellcodedateien, welche gseparst werden sollen
*/ */
public void parse(Vector<String> filenames); public void parse(Vector<String> filenames) throws ParserError;
/** /**
* Parst den SourceCode einer Datei. * Parst den SourceCode einer Datei.
* @param sourceCode - SourceCode einer Java-Quellcodedatei * @param sourceCode - SourceCode einer Java-Quellcodedatei
* @return den aus dem sourceCode generierten Syntaxbaum * @return den aus dem sourceCode generierten Syntaxbaum
*/ */
public SourceFile parse(String sourceCode); public SourceFile parse(String sourceCode) throws ParserError;
} }
// ino.end // ino.end

View File

@ -58,7 +58,8 @@ import typinferenz.assumptions.ClassAssumption;
import typinferenz.assumptions.MethodAssumption; import typinferenz.assumptions.MethodAssumption;
import typinferenz.assumptions.ParameterAssumption; import typinferenz.assumptions.ParameterAssumption;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.DebugException;
import typinferenz.exceptions.TypeinferenceException;
@ -1584,7 +1585,7 @@ public class SourceFile
@Override @Override
public void parserPostProcessing(SyntaxTreeNode parent) { public void parserPostProcessing(SyntaxTreeNode parent) {
if(parent!=null)throw new TypinferenzException("Eine SourceFile hat keine Elternelement im Syntaxbaum"); if(parent!=null)throw new DebugException("Eine SourceFile hat kein Elternelement im Syntaxbaum");
super.parserPostProcessing(parent); super.parserPostProcessing(parent);
//for(SyntaxTreeNode node : this.getChildren())node.parserPostProcessing(this); //for(SyntaxTreeNode node : this.getChildren())node.parserPostProcessing(this);
} }

View File

@ -2,7 +2,8 @@ package mycompiler;
import java.util.Vector; import java.util.Vector;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.DebugException;
import typinferenz.exceptions.TypeinferenceException;
import mycompiler.myclass.Class; import mycompiler.myclass.Class;
import mycompiler.mytype.GenericTypeVar; import mycompiler.mytype.GenericTypeVar;
@ -33,7 +34,7 @@ public abstract class SyntaxTreeNode {
public Class getParentClass(){ public Class getParentClass(){
SyntaxTreeNode parent = this.getParent(); SyntaxTreeNode parent = this.getParent();
if(parent instanceof Class)return (Class)parent; if(parent instanceof Class)return (Class)parent;
if(parent == null)throw new TypinferenzException("Das Wurzelelement eines Syntaxbaumes muss Class sein"); if(parent == null)throw new DebugException("Das Wurzelelement eines Syntaxbaumes muss Class sein");
return parent.getParentClass(); return parent.getParentClass();
} }

View File

@ -70,7 +70,7 @@ import typinferenz.UndConstraint;
import typinferenz.FunN; import typinferenz.FunN;
import typinferenz.assumptions.ClassAssumption; import typinferenz.assumptions.ClassAssumption;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
import typinferenz.*; import typinferenz.*;
@ -407,7 +407,7 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface
* -Rückgabetyp der Methode/Konstruktors ist der Typ der Klasse * -Rückgabetyp der Methode/Konstruktors ist der Typ der Klasse
* -Ein Konstruktor kann nicht aufgerufen werden (nur mit new) * -Ein Konstruktor kann nicht aufgerufen werden (nur mit new)
*/ */
if(m.get_Method_Name().equals("<init>"))throw new TypinferenzException("<init> ist kein gültiger Methodenname"); if(m.get_Method_Name().equals("<init>"))throw new TypeinferenceException("<init> ist kein gültiger Methodenname", m);
if((m.get_Method_Name().equals(this.getName()))) { if((m.get_Method_Name().equals(this.getName()))) {
Constructor constructor = new Constructor(m); Constructor constructor = new Constructor(m);
tempFields.add(constructor); //Den Konstruktor anstatt der Methode anfügen tempFields.add(constructor); //Den Konstruktor anstatt der Methode anfügen

View File

@ -18,7 +18,7 @@ import typinferenz.SingleConstraint;
import typinferenz.assumptions.ConstructorAssumption; import typinferenz.assumptions.ConstructorAssumption;
import typinferenz.assumptions.MethodAssumption; import typinferenz.assumptions.MethodAssumption;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
public class Constructor extends Method { public class Constructor extends Method {
private Method methode; private Method methode;
@ -265,7 +265,7 @@ public class Constructor extends Method {
@Override @Override
public void setType(Type t) { public void setType(Type t) {
throw new TypinferenzException("Einem Konstruktor kann kein Typ zugewiesen werden"); throw new TypeinferenceException("Einem Konstruktor kann kein Typ zugewiesen werden", this);
//this.methode.setType(t); //this.methode.setType(t);
} }

View File

@ -9,7 +9,7 @@ import typinferenz.ResultSet;
import typinferenz.SingleConstraint; import typinferenz.SingleConstraint;
import typinferenz.assumptions.FieldAssumption; import typinferenz.assumptions.FieldAssumption;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
import mycompiler.SyntaxTreeNode; import mycompiler.SyntaxTreeNode;
import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.ClassFile;
import mycompiler.myexception.JVMCodeException; import mycompiler.myexception.JVMCodeException;
@ -90,7 +90,7 @@ public class FieldDeclaration extends Field{
* Der Feld-Assumption muss ein TPH als Typ hinzugefügt werden, falls er Typlos initialisiert wurde. Dies kann auch der Type-Algorithmus der Inst/FieldVar - Klasse machen. * Der Feld-Assumption muss ein TPH als Typ hinzugefügt werden, falls er Typlos initialisiert wurde. Dies kann auch der Type-Algorithmus der Inst/FieldVar - Klasse machen.
* Wird das Feld mit einem Typ initialisiert so muss dieser auch in die Assumptions. * Wird das Feld mit einem Typ initialisiert so muss dieser auch in die Assumptions.
*/ */
if(this.getType() == null)throw new TypinferenzException("Der Typ eines Feldes darf nicht null sein"); if(this.getType() == null)throw new TypeinferenceException("Der Typ eines Feldes darf nicht null sein", this);
//assumptions.add(TypeAssumptions.createFieldVarAssumption(classmember.getName(), this.getName(), this.getType())); //assumptions.add(TypeAssumptions.createFieldVarAssumption(classmember.getName(), this.getName(), this.getType()));
assumptions.addAssumption(new FieldAssumption(this,classmember)); assumptions.addAssumption(new FieldAssumption(this,classmember));
return assumptions; return assumptions;
@ -134,7 +134,15 @@ public class FieldDeclaration extends Field{
GenericTypeVar gtv = (GenericTypeVar) this.getType(); GenericTypeVar gtv = (GenericTypeVar) this.getType();
} }
*/ */
if(this.getType()!=null && (this.getType() instanceof RefType))this.setType(publicAssumptions.getTypeFor((RefType)this.getType()));
//TypeCheck, falls es sich um einen RefType handelt:
if(this.getType()!=null && (this.getType() instanceof RefType)){
Type replaceType = null;
replaceType = publicAssumptions.getTypeFor((RefType)this.getType());
if(replaceType == null)throw new TypeinferenceException("Der Typ "+this.getType().getName()+" ist nicht korrekt",this);
this.setType(replaceType);
}
SingleConstraint c1 = new SingleConstraint(this.getType(), this.getType()); SingleConstraint c1 = new SingleConstraint(this.getType(), this.getType());
ret.add(c1); //Damit die TypVariable des Felds in den Constraints auftaucht ret.add(c1); //Damit die TypVariable des Felds in den Constraints auftaucht

View File

@ -32,7 +32,7 @@ import typinferenz.ResultSet;
import typinferenz.TypeInsertPoint; import typinferenz.TypeInsertPoint;
import typinferenz.Typeable; import typinferenz.Typeable;
import typinferenz.TypeInsertable; import typinferenz.TypeInsertable;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
// ino.class.FormalParameter.23391.declaration // ino.class.FormalParameter.23391.declaration
public class FormalParameter extends SyntaxTreeNode implements ITypeReplacementListener, Typeable, TypeInsertable public class FormalParameter extends SyntaxTreeNode implements ITypeReplacementListener, Typeable, TypeInsertable
@ -127,7 +127,7 @@ public class FormalParameter extends SyntaxTreeNode implements ITypeReplacementL
public String getTypeName() public String getTypeName()
// ino.end // ino.end
// ino.method.getTypeName.23416.body // ino.method.getTypeName.23416.body
{ { if(this.getType() == null)return "";
return this.getType().getName(); return this.getType().getName();
} }
// ino.end // ino.end
@ -263,5 +263,14 @@ public class FormalParameter extends SyntaxTreeNode implements ITypeReplacementL
} }
@Override
public int getVariableLength() {
int ret = 0;
ret += this.getTypeName().length();
ret +=this.getIdentifier().length();
return ret;
}
} }
// ino.end // ino.end

View File

@ -41,6 +41,7 @@ import typinferenz.TypeInsertable;
import typinferenz.assumptions.MethodAssumption; import typinferenz.assumptions.MethodAssumption;
import typinferenz.assumptions.ParameterAssumption; import typinferenz.assumptions.ParameterAssumption;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypeinferenceException;
@ -541,15 +542,30 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
public ConstraintsSet TYPE(TypeAssumptions ass) { public ConstraintsSet TYPE(TypeAssumptions ass) {
if((this.returntype instanceof RefType) && //TypeCheck, falls es sich um einen RefType handelt:
!(this.returntype instanceof mycompiler.mytype.Void))//Sonderfall der Methode: Ihr Typ darf Void definiert werden. if(this.returntype!=null && (this.returntype instanceof RefType)&&
this.returntype = ass.getTypeFor((RefType)this.returntype); !(this.returntype instanceof mycompiler.mytype.Void)){//Sonderfall der Methode: Ihr Typ darf Void definiert werden.
Type replaceType = null;
replaceType = ass.getTypeFor((RefType)this.returntype);
if(replaceType == null)throw new TypeinferenceException("Der Typ "+this.getType().getName()+" ist nicht korrekt",this);
this.returntype = replaceType;
}
ConstraintsSet ret = new ConstraintsSet(); ConstraintsSet ret = new ConstraintsSet();
TypeAssumptions localAss = new TypeAssumptions(); TypeAssumptions localAss = new TypeAssumptions();
localAss.add(ass); //Die globalen Assumptions anhängen localAss.add(ass); //Die globalen Assumptions anhängen
//Die Parameter zu den Assumptions hinzufügen: //Die Parameter zu den Assumptions hinzufügen:
if(this.parameterlist!=null)for(FormalParameter param : this.parameterlist){ if(this.parameterlist!=null)for(FormalParameter param : this.parameterlist){
if(param.getType() instanceof RefType)param.setType(ass.getTypeFor((RefType)param.getType()));
if(param.getType() instanceof RefType)
{
Type replaceType = null;
replaceType = ass.getTypeFor((RefType)param.getType());
if(replaceType == null)throw new TypeinferenceException("Der Typ "+this.getType().getName()+" ist nicht korrekt",param);
param.setType(replaceType);
}
localAss.addAssumption(new ParameterAssumption(param)); localAss.addAssumption(new ParameterAssumption(param));
} }
ret.add(this.block.TYPEStmt(localAss)); ret.add(this.block.TYPEStmt(localAss));

View File

@ -7,7 +7,7 @@ import java.util.Vector;
import typinferenz.JavaCodeResult; import typinferenz.JavaCodeResult;
import typinferenz.ResultSet; import typinferenz.ResultSet;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
import mycompiler.IItemWithOffset; import mycompiler.IItemWithOffset;
import mycompiler.mybytecode.JVMCode; import mycompiler.mybytecode.JVMCode;
import mycompiler.mytype.Type; import mycompiler.mytype.Type;
@ -312,7 +312,7 @@ public class UsedId implements IItemWithOffset
// ino.end // ino.end
public JavaCodeResult printJavaCode(ResultSet resultSet) { public JavaCodeResult printJavaCode(ResultSet resultSet) {
if(this.name.size()>1)throw new TypinferenzException("Es kann maximal nur eine Superklasse pro Klasse geben"); if(this.name.size()>1)throw new TypeinferenceException("Es kann maximal nur eine Superklasse pro Klasse geben", this);
JavaCodeResult ret = new JavaCodeResult(this.getQualifiedName()); JavaCodeResult ret = new JavaCodeResult(this.getQualifiedName());
if(this.paralist != null){ if(this.paralist != null){
ret.attach( "<" ); ret.attach( "<" );

View File

@ -40,7 +40,7 @@ import typinferenz.ConstraintsSet;
import typinferenz.FreshTypeVariable; import typinferenz.FreshTypeVariable;
import typinferenz.ResultSet; import typinferenz.ResultSet;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
@ -236,7 +236,7 @@ public class Block extends Statement
if (!(stmt.getType() instanceof Void)) if (!(stmt.getType() instanceof Void))
if (this.getType() instanceof Void) { if (this.getType() instanceof Void) {
//this.setTypeVariable(stmt.getTypeVariable()); //this.setTypeVariable(stmt.getTypeVariable());
throw new TypinferenzException("Falscher Return Type"); throw new TypeinferenceException("Block besitzt falschen Rückgabetyp (fehlendes return-stmt)", this);
} }
else { else {
TypePlaceholder tph = TypePlaceholder.fresh(this); TypePlaceholder tph = TypePlaceholder.fresh(this);

View File

@ -12,7 +12,7 @@ import typinferenz.ResultSet;
import typinferenz.Typeable; import typinferenz.Typeable;
import typinferenz.assumptions.ParameterAssumption; import typinferenz.assumptions.ParameterAssumption;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
import mycompiler.SyntaxTreeNode; import mycompiler.SyntaxTreeNode;
import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.ClassFile;
import mycompiler.mybytecode.CodeAttribute; import mycompiler.mybytecode.CodeAttribute;
@ -171,7 +171,7 @@ public class LambdaExpression extends Expr{
@Override @Override
public ConstraintsSet TYPEStmt(TypeAssumptions ass){ public ConstraintsSet TYPEStmt(TypeAssumptions ass){
throw new TypinferenzException("Eine LambdaExpression darf nicht als Statement verwendet werden."); throw new TypeinferenceException("Eine LambdaExpression darf nicht als Statement verwendet werden.", this);
} }
@Override @Override

View File

@ -34,6 +34,7 @@ import org.apache.log4j.Logger;
import sun.reflect.generics.reflectiveObjects.NotImplementedException; import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import typinferenz.JavaCodeResult; import typinferenz.JavaCodeResult;
import typinferenz.SingleConstraint; import typinferenz.SingleConstraint;
@ -41,6 +42,7 @@ import typinferenz.ConstraintsSet;
import typinferenz.FreshTypeVariable; import typinferenz.FreshTypeVariable;
import typinferenz.ResultSet; import typinferenz.ResultSet;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypeinferenceException;
@ -207,6 +209,7 @@ public class LocalOrFieldVar extends Expr
ConstraintsSet ret = new ConstraintsSet(); ConstraintsSet ret = new ConstraintsSet();
//gibt es eine Assumption für den die LocalOrFieldVar-Variablen, dann folgendes ausführen: //gibt es eine Assumption für den die LocalOrFieldVar-Variablen, dann folgendes ausführen:
Type thisTypeAssumption = assumptions.getVarType(this.get_Name(), this.getParentClass()); Type thisTypeAssumption = assumptions.getVarType(this.get_Name(), this.getParentClass());
if(thisTypeAssumption == null)throw new TypeinferenceException("Eine Variable "+this.get_Name()+" ist in den Assumptions nicht vorhanden",this);
this.setType(thisTypeAssumption); this.setType(thisTypeAssumption);
//ret.add(new Constraint(thisTypeAssumption, this.getTypeVariable())); //ret.add(new Constraint(thisTypeAssumption, this.getTypeVariable()));
return ret; return ret;

View File

@ -40,6 +40,7 @@ import org.apache.log4j.Logger;
import typinferenz.ConstraintsSet; import typinferenz.ConstraintsSet;
import typinferenz.FreshTypeVariable; import typinferenz.FreshTypeVariable;
import typinferenz.JavaCodeResult; import typinferenz.JavaCodeResult;
@ -48,6 +49,7 @@ import typinferenz.TypeInsertPoint;
import typinferenz.TypeInsertable; import typinferenz.TypeInsertable;
import typinferenz.assumptions.LocalVarAssumption; import typinferenz.assumptions.LocalVarAssumption;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypeinferenceException;
@ -463,7 +465,12 @@ public class LocalVarDecl extends Statement implements TypeInsertable
@Override @Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) { public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet(); ConstraintsSet ret = new ConstraintsSet();
if((this.getType() instanceof RefType))this.setType(assumptions.getTypeFor((RefType)this.getType())); if((this.getType() instanceof RefType)){
Type replaceType = null;
replaceType = assumptions.getTypeFor((RefType)this.getType());
if(replaceType == null)throw new TypeinferenceException("Der Typ "+this.getType().getName()+" ist nicht korrekt",this);
this.setType(replaceType);
}
assumptions.addAssumption(new LocalVarAssumption(this)); assumptions.addAssumption(new LocalVarAssumption(this));
//assumptions.remove(null); // falls Variable mit diesem Namen bereits vorhanden. //assumptions.remove(null); // falls Variable mit diesem Namen bereits vorhanden.
this.setType(new Void(0)); //Return typ einer Variablendeklaration ist Void this.setType(new Void(0)); //Return typ einer Variablendeklaration ist Void

View File

@ -27,10 +27,12 @@ import org.apache.log4j.Logger;
import typinferenz.ConstraintsSet; import typinferenz.ConstraintsSet;
import typinferenz.JavaCodeResult; import typinferenz.JavaCodeResult;
import typinferenz.ResultSet; import typinferenz.ResultSet;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypeinferenceException;
@ -138,6 +140,7 @@ public class StringLiteral extends Literal
@Override @Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) { public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
this.set_Type(assumptions.getTypeFor(new RefType("String",0))); this.set_Type(assumptions.getTypeFor(new RefType("String",0)));
if(this.getType() == null)throw new TypeinferenceException("java.lang.String nicht importiert",this);
return new ConstraintsSet(); return new ConstraintsSet();
} }

View File

@ -16,7 +16,7 @@ import org.apache.log4j.PatternLayout;
import org.apache.log4j.SimpleLayout; import org.apache.log4j.SimpleLayout;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
import mycompiler.MyCompiler; import mycompiler.MyCompiler;
import mycompiler.MyCompilerAPI; import mycompiler.MyCompilerAPI;
import mycompiler.mytype.Type; import mycompiler.mytype.Type;
@ -145,7 +145,7 @@ public class LambdaTest {
///////////////////////// /////////////////////////
try{ try{
resultSet = compiler.typeReconstruction(); resultSet = compiler.typeReconstruction();
}catch(TypinferenzException texc){ }catch(TypeinferenceException texc){
texc.printStackTrace(); texc.printStackTrace();
fail("Fehler bei Typinferenzalgorithmus. Message: "+texc.getMessage()); fail("Fehler bei Typinferenzalgorithmus. Message: "+texc.getMessage());
} }

View File

@ -12,7 +12,7 @@ import typinferenz.ResultSet;
import typinferenz.TypeInsertPoint; import typinferenz.TypeInsertPoint;
import typinferenz.TypeInsertSet; import typinferenz.TypeInsertSet;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
import mycompiler.mytype.GenericTypeVar; import mycompiler.mytype.GenericTypeVar;
import mycompiler.mytype.Pair; import mycompiler.mytype.Pair;
import mycompiler.mytype.RefType; import mycompiler.mytype.RefType;

View File

@ -4,6 +4,7 @@ import java.util.Vector;
import typinferenz.assumptions.MethodAssumption; import typinferenz.assumptions.MethodAssumption;
import typinferenz.assumptions.TypeAssumptions; import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypeinferenceException;
import mycompiler.mystatement.Expr; import mycompiler.mystatement.Expr;
import mycompiler.mystatement.MethodCall; import mycompiler.mystatement.MethodCall;
import mycompiler.mytype.RefType; import mycompiler.mytype.RefType;
@ -60,7 +61,9 @@ public class Overloading{
for(Expr argument : methodCall.getArgumentList().expr){ for(Expr argument : methodCall.getArgumentList().expr){
parameterList.add(argument.getType()); parameterList.add(argument.getType());
} }
for(MethodAssumption methodAssumption : assumptions.getMethodAssumptions(methodCall.getName(), parameterList)){ Vector<MethodAssumption> methodAssumptions = assumptions.getMethodAssumptions(methodCall.getName(), parameterList);
if(methodAssumptions.size()==0)throw new TypeinferenceException("Eine Methode "+methodCall.get_Name()+" ist in den Assumptions nicht vorhanden", methodCall);
for(MethodAssumption methodAssumption : methodAssumptions){
if(!(this.type instanceof TypePlaceholder) && !this.type.equals(methodAssumption.getAssumedType()))break; if(!(this.type instanceof TypePlaceholder) && !this.type.equals(methodAssumption.getAssumedType()))break;
UndConstraint methodConstraint = new UndConstraint(); UndConstraint methodConstraint = new UndConstraint();
//Ein Constraint für den ReturnType der Methode... //Ein Constraint für den ReturnType der Methode...

View File

@ -2,7 +2,8 @@ package typinferenz;
import java.util.Vector; import java.util.Vector;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.DebugException;
import typinferenz.exceptions.TypeinferenceException;
import mycompiler.mytype.GenericTypeVar; import mycompiler.mytype.GenericTypeVar;
import mycompiler.mytype.Pair; import mycompiler.mytype.Pair;
import mycompiler.mytype.RefType; import mycompiler.mytype.RefType;
@ -43,7 +44,7 @@ public class SingleConstraint extends UndConstraint{
} }
public void addConstraint(Pair toAdd){ public void addConstraint(Pair toAdd){
if(constraintPair != null)throw new TypinferenzException("Ein Constraint darf nur aus einem ConstraintPair bestehen. Das hinzufügen von "+ toAdd + " ist nicht möglich."); if(constraintPair != null)throw new DebugException("Ein Constraint darf nur aus einem ConstraintPair bestehen. Das hinzufügen von "+ toAdd + " ist nicht möglich.");
Type p1 = toAdd.TA1; Type p1 = toAdd.TA1;
Type p2 = toAdd.TA2; Type p2 = toAdd.TA2;

View File

@ -1,9 +1,10 @@
package typinferenz; package typinferenz;
import mycompiler.IItemWithOffset;
import mycompiler.mytype.TypePlaceholder; import mycompiler.mytype.TypePlaceholder;
import mycompiler.mytypereconstruction.replacementlistener.ITypeReplacementListener; import mycompiler.mytypereconstruction.replacementlistener.ITypeReplacementListener;
public interface TypeInsertable extends ITypeReplacementListener, Typeable { public interface TypeInsertable extends ITypeReplacementListener, Typeable, IItemWithOffset {
public int getOffset(); public int getOffset();
public void setOffset(int offset); public void setOffset(int offset);

View File

@ -10,7 +10,7 @@ import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import typinferenz.FunN; import typinferenz.FunN;
import typinferenz.FunNInterface; import typinferenz.FunNInterface;
import typinferenz.FunNMethod; import typinferenz.FunNMethod;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
import mycompiler.mytype.GenericTypeVar; import mycompiler.mytype.GenericTypeVar;
import mycompiler.mytype.RefType; import mycompiler.mytype.RefType;
import mycompiler.mytype.Type; import mycompiler.mytype.Type;
@ -81,11 +81,18 @@ public class TypeAssumptions {
return ret; return ret;
} }
/**
* Liefert den Typ einer Feldvariable der Klasse inClass
* @param withName
* @param inClass
* @return null, falls die Klasse kein zugängliches Feld enthält
*/
public Type getTypeOfFieldVar(String withName, Class inClass){ public Type getTypeOfFieldVar(String withName, Class inClass){
for(FieldAssumption fa : this.getFieldVars(withName)){ for(FieldAssumption fa : this.getFieldVars(withName)){
if(fa.getParentClass().equals(inClass))return fa.getAssumedType(); if(fa.getParentClass().equals(inClass))return fa.getAssumedType();
} }
throw new TypinferenzException("Das Feld "+withName+" ist in der Klasse "+inClass.getName()+" nicht vorhanden."); //throw new TypeinferenceException("Das Feld "+withName+" ist in der Klasse "+inClass.getName()+" nicht vorhanden.");
return null;
} }
/** /**
@ -137,7 +144,7 @@ public class TypeAssumptions {
// MethodAssumption funNAssumption = new MethodAssumption(new FunNMethod(parameterCount), new FunNInterface(parameter)); // MethodAssumption funNAssumption = new MethodAssumption(new FunNMethod(parameterCount), new FunNInterface(parameter));
// ret.add(funNAssumption); // ret.add(funNAssumption);
//} //}
if(ret.size()==0)throw new TypinferenzException("Eine Methode "+methodName+" ist in den Assumptions nicht vorhanden"); //if(ret.size()==0)throw new TypeinferenceException("Eine Methode "+methodName+" ist in den Assumptions nicht vorhanden");
return ret; return ret;
} }
@ -147,7 +154,7 @@ public class TypeAssumptions {
* In den Assumptions wird dann in der Reihenfolge LocalVarAssumptions, FieldAssumption nach dem übergebenen Variablennamen gesucht. * In den Assumptions wird dann in der Reihenfolge LocalVarAssumptions, FieldAssumption nach dem übergebenen Variablennamen gesucht.
* @param variableName - der Identifier der gesuchten Variablen * @param variableName - der Identifier der gesuchten Variablen
* @param inScope - Sucht auch die Felder der übergebenen Klasse ab. Hier kann auch null übergeben werden, dann werden nur die Lokalen Variablen dieser TypeAssumption durchsucht. * @param inScope - Sucht auch die Felder der übergebenen Klasse ab. Hier kann auch null übergeben werden, dann werden nur die Lokalen Variablen dieser TypeAssumption durchsucht.
* @return - Der Typ für diesen Identifier * @return - Der Typ für diesen Identifier, oder null, falls kein Typ vorliegt.
*/ */
public Type getVarType(String variableName, Class inScope){ public Type getVarType(String variableName, Class inScope){
//Zuerst die Parameter durchsuchen //Zuerst die Parameter durchsuchen
@ -166,7 +173,8 @@ public class TypeAssumptions {
} }
} }
//Wird keine Assumption gefunden, muss ein Fehler vorliegen: //Wird keine Assumption gefunden, muss ein Fehler vorliegen:
throw new TypinferenzException("Eine Variable "+variableName+" ist in den Assumptions nicht vorhanden"); //throw new TypeinferenceException("Eine Variable "+variableName+" ist in den Assumptions nicht vorhanden");
return null;
} }
/** /**
@ -271,20 +279,14 @@ public class TypeAssumptions {
return ret; return ret;
} }
@Deprecated
public Type getThisValue2() {
if(thisClassName == null)throw new TypinferenzException("Kein Wert für this vorhanden, in diesem Kontext");
return new RefType(thisClassName, 0);
}
/** /**
* Kontrolliert den vom Parser gesetzten Typ. * Kontrolliert den vom Parser gesetzten Typ.
* Erweitert dessen Bezeichnung, wenn nötig. * Erweitert dessen Bezeichnung, wenn nötig.
* @param t * @param t
* @return * @return null, falls der Typ nicht vorhanden ist.
* @throws TypinferenzException
*/ */
public Type getTypeFor(RefType t) throws TypinferenzException{ public Type getTypeFor(RefType t){
//TODO: Die Parameterliste noch kontrollieren: (hier könnte es Constraints geben: "? extends String") //TODO: Die Parameterliste noch kontrollieren: (hier könnte es Constraints geben: "? extends String")
//Alle bekannten Klassen nach diesem Typ durchsuchen: //Alle bekannten Klassen nach diesem Typ durchsuchen:
String typName = t.getName(); String typName = t.getName();
@ -306,7 +308,7 @@ public class TypeAssumptions {
return ret; return ret;
} }
} }
throw new TypinferenzException("Der Typ "+t.getName()+" ist nicht korrekt"); return null;
} }
/** /**

View File

@ -0,0 +1,12 @@
package typinferenz.exceptions;
import mycompiler.myparser.JavaParser.yyException;
public class ParserError extends TypeinferenceException{
public ParserError(yyException exc){
super(exc.getMessage(), exc.token.getOffset());
}
}

View File

@ -1,16 +0,0 @@
package typinferenz.exceptions;
import mycompiler.myparser.JavaParser.yyException;
public class ParserException extends TypinferenzException {
public ParserException(String message) {
super(message);
}
public ParserException(yyException exc){
super("Parserfehler");
}
}

View File

@ -0,0 +1,31 @@
package typinferenz.exceptions;
import mycompiler.IItemWithOffset;
/**
* Eine RuntimeException, welche bei einem Fehler während des Typinferenzalgorithmus ausgelöst wird.
* Dies wird zum Beispiel durch Programmierfehler in der Java-Eingabedatei ausgelöst.
* @author Andreas Stadelmeier, a10023
*
*/
//TODO: Diese Klasse muss von Exception erben
public class TypeinferenceException extends RuntimeException {
/**
* Das Offset im Quelltext bei dem das Problem aufgetaucht ist
*/
private int offset;
private String message;
public TypeinferenceException(String message, IItemWithOffset problemSource)
{
super(message);
this.message=message;
this.offset=problemSource.getOffset();
}
public TypeinferenceException(String message, int offset){
this.message=message;
}
}

View File

@ -1,13 +0,0 @@
package typinferenz.exceptions;
/**
* Eine RuntimeException, welche bei einem Fehler während des Typinferenzalgorithmus ausgelöst wird.
* Dies wird zum Beispiel durch Programmierfehler in der Java-Eingabedatei ausgelöst.
* @author Andreas Stadelmeier, a10023
*
*/
public class TypinferenzException extends RuntimeException {
public TypinferenzException(String message){
super(message);
}
}

View File

@ -8,7 +8,7 @@ import java.util.Vector;
import mycompiler.MyCompiler; import mycompiler.MyCompiler;
import mycompiler.MyCompilerAPI; import mycompiler.MyCompilerAPI;
import mycompiler.mytypereconstruction.TypeinferenceResultSet; import mycompiler.mytypereconstruction.TypeinferenceResultSet;
import typinferenz.exceptions.TypinferenzException; import typinferenz.exceptions.TypeinferenceException;
import java.util.*; import java.util.*;
@ -45,7 +45,7 @@ public class ConsoleInterface {
///////////////////////// /////////////////////////
try{ try{
resultSet = compiler.typeReconstruction(); resultSet = compiler.typeReconstruction();
}catch(TypinferenzException texc){ }catch(TypeinferenceException texc){
texc.printStackTrace(); texc.printStackTrace();
fail("Fehler bei Typinferenzalgorithmus. Message: "+texc.getMessage()); fail("Fehler bei Typinferenzalgorithmus. Message: "+texc.getMessage());
} }

View File

@ -110,6 +110,12 @@ class TestNode implements TypeInsertable{
ResultSet resultSet) { ResultSet resultSet) {
return new TypeInsertPoint(tph, this, resultSet.getTypeEqualTo(tph), resultSet); return new TypeInsertPoint(tph, this, resultSet.getTypeEqualTo(tph), resultSet);
} }
@Override
public int getVariableLength() {
// TODO Auto-generated method stub
return 0;
}
} }