From 49917239c1a996f0862e6d8d7e5d537fd11d514d Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 16 Sep 2014 17:34:04 +0200 Subject: [PATCH] GenericVarDeclarationList extends SyntaxTreeNode --- src/de/dhbwstuttgart/antlr/VisitorTest.java | 25 ++++++++++ src/de/dhbwstuttgart/syntaxtree/Class.java | 2 +- src/de/dhbwstuttgart/syntaxtree/Field.java | 2 +- .../syntaxtree/GenericDeclarationList.java | 49 ++++++++++++++++++- .../assumptions/TypeAssumptions.java | 3 +- 5 files changed, 76 insertions(+), 5 deletions(-) diff --git a/src/de/dhbwstuttgart/antlr/VisitorTest.java b/src/de/dhbwstuttgart/antlr/VisitorTest.java index e3cf519ad..eeaf4478f 100644 --- a/src/de/dhbwstuttgart/antlr/VisitorTest.java +++ b/src/de/dhbwstuttgart/antlr/VisitorTest.java @@ -12,9 +12,13 @@ import org.junit.Test; import de.dhbwstuttgart.antlr.Java8Parser.ClassDeclarationContext; import de.dhbwstuttgart.antlr.Java8Parser.CompilationUnitContext; import de.dhbwstuttgart.antlr.Java8Parser.TypeDeclarationContext; +import de.dhbwstuttgart.antlr.Java8Parser.TypeParameterContext; +import de.dhbwstuttgart.antlr.Java8Parser.TypeParametersContext; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import de.dhbwstuttgart.syntaxtree.Class; +import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; public class VisitorTest { @@ -62,7 +66,28 @@ class EvalVisitor extends Java8BaseVisitor { public Class visitClassDeclaration(ClassDeclarationContext ctx) { String name = ctx.Identifier().getText(); int offset = ctx.Identifier().getSymbol().getStartIndex(); + TypeParametersContext tpctx = ctx.typeParameters(); + GenericDeclarationList gtvList = (GenericDeclarationList) visit(tpctx); + return new Class(name, offset); } + @Override + public GenericDeclarationList visitTypeParameters(TypeParametersContext ctx) { + Vector list = new Vector<>(); + int endOffset = 0; + for(TypeParameterContext tpctx : ctx.typeParameter()){ + tpctx.Identifier(); + } + GenericDeclarationList ret = new GenericDeclarationList(list, endOffset); + return ret; + } + + @Override + public GenericTypeVar visitTypeParameter(TypeParameterContext ctx) { + GenericTypeVar ret = new GenericTypeVar(name, parent, offset); + return ret; + } + + } diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 10b468074..7becd842a 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -1184,7 +1184,7 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit @Override public Vector getGenericParameter() { if(this.genericClassParameters == null)return new Vector(); - return this.genericClassParameters; + return this.genericClassParameters.getVector(); } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index 51766f7a0..2ba3ce6b9 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -57,7 +57,7 @@ public abstract class Field extends SyntaxTreeNode implements TypeInsertable, Ty public Vector getGenericParameter() { Vector ret = new Vector<>(); if(this.genericParameters == null)return ret; - ret.addAll(this.genericParameters); + ret.addAll(this.genericParameters.getVector()); return ret; } diff --git a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java index 115e60e33..9a722981e 100644 --- a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java +++ b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java @@ -1,5 +1,9 @@ package de.dhbwstuttgart.syntaxtree; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; import java.util.Vector; import de.dhbwstuttgart.parser.GenericVarDeclarationList; @@ -12,16 +16,57 @@ import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; * @author janulrich * */ -public class GenericDeclarationList extends Vector{ +public class GenericDeclarationList extends SyntaxTreeNode implements Iterable{ private int offsetOfLastElement; - + private Vector gtvs = new Vector<>(); + public GenericDeclarationList(Vector values, int endOffset) { this.addAll(values); this.offsetOfLastElement = endOffset; } + private void addAll(Vector values) { + this.gtvs.addAll(values); + } + public int getEndOffset(){ return offsetOfLastElement; } + + @Override + public int getOffset() { + return getEndOffset(); + } + + @Override + public int getVariableLength() { + return 0; + } + + @Override + public Iterator iterator() { + return this.gtvs.iterator(); + } + + @Override + public Vector getChildren() { + Vector ret = new Vector<>(); + for(GenericTypeVar gtv : gtvs){ + ret.add(gtv); + } + return ret; + } + + public int size() { + return gtvs.size(); + } + + public Vector getVector() { + return this.gtvs; + } + + public void add(GenericTypeVar t) { + this.gtvs.add(t); + } } diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/TypeAssumptions.java b/src/de/dhbwstuttgart/typeinference/assumptions/TypeAssumptions.java index befd80002..227592602 100755 --- a/src/de/dhbwstuttgart/typeinference/assumptions/TypeAssumptions.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/TypeAssumptions.java @@ -316,7 +316,8 @@ public class TypeAssumptions { return new ConstraintType(ass.getAssumedType()); } } - //return null; + //Spezialfälle bei char, int, boolean, .... + //TODO throw new TypeinferenceException("Der Typ "+t.getName()+" ist nicht korrekt",inNode); }