From 9a7e717c25e13139f9b33388fcd4fa288085cab4 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 23 Aug 2017 16:24:25 +0200 Subject: [PATCH] Constructor um fieldInits erweitern, auch der TYPE algo --- src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java | 3 +++ .../syntaxtree/AbstractASTWalker.java | 8 ++++++++ .../syntaxtree/ClassOrInterface.java | 2 +- .../dhbwstuttgart/syntaxtree/Constructor.java | 17 +++++++++++++++++ .../syntaxtree/visual/OutputGenerator.java | 13 +++++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java b/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java index d7527634..fde9fc83 100644 --- a/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java @@ -4,6 +4,7 @@ import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.constraints.Constraint; public interface ASTVisitor extends StatementVisitor{ @@ -21,6 +22,8 @@ public interface ASTVisitor extends StatementVisitor{ void visit(Method field); + void visit(Constructor field); + void visit(ParameterList formalParameters); void visit(ClassOrInterface classOrInterface); diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index bb200559..420b8257 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -9,6 +9,10 @@ import java.lang.reflect.Modifier; import java.util.Iterator; public abstract class AbstractASTWalker implements ASTVisitor{ + @Override + public void visit(Constructor cons) { + visitMethod(cons); + } @Override public void visit(SourceFile sourceFile) { @@ -51,6 +55,10 @@ public abstract class AbstractASTWalker implements ASTVisitor{ @Override public void visit(Method method) { + visitMethod(method); + } + + private void visitMethod(Method method){ method.getType().accept(this); method.getParameterList().accept(this); method.block.accept(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 1604c8c5..1733a3ac 100755 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -82,7 +82,7 @@ public class ClassOrInterface extends SyntaxTreeNode { return this.genericClassParameters; } - public List getConstructors() { + public List getConstructors() { return constructors; } diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index a56a37b0..219aeefe 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -2,6 +2,10 @@ package de.dhbwstuttgart.syntaxtree; import de.dhbwstuttgart.syntaxtree.statement.Statement; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; +import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.syntaxtree.statement.Block; @@ -23,4 +27,17 @@ public class Constructor extends Method { this.fieldInitializations = fieldInitializations; } + public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) { + TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, null); + TYPE methodScope = new TYPE(blockInfo); + for(Statement stmt : fieldInitializations)stmt.accept(methodScope); + ConstraintSet ret = super.getConstraints(info, currentClass); + ret.addAll(methodScope.getConstraints()); + return ret; + } + + @Override + public void accept(ASTVisitor visitor) { + visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 7e58fd3f..0adac8a8 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -87,6 +87,14 @@ public class OutputGenerator implements ASTVisitor { out.append("\n"); } + @Override + public void visit(Constructor method) { + out.append(method.getName()); + method.getParameterList().accept(this); + method.block.accept(this); + out.append("\n"); + } + @Override public void visit(ParameterList formalParameters) { out.append("("); @@ -121,6 +129,11 @@ public class OutputGenerator implements ASTVisitor { m.accept(this); out.append("\n"); } + for(Constructor m : classOrInterface.getConstructors()){ + out.append(tabs); + m.accept(this); + out.append("\n"); + } untab(); out.append("}"); }