diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.html index ca565e83..204c7422 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.Expr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/LocalOrFieldVar.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/LocalOrFieldVar.html index 06ed2e83..74288c3b 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/LocalOrFieldVar.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/LocalOrFieldVar.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar +Uses of Class de.dhbwstuttgart.syntaxtree.statement.LocalVar @@ -70,9 +70,9 @@
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.LocalVar
    diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 0c5f9a00..c98461d7 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.core; import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import de.dhbwstuttgart.typeinference.ConstraintSet; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; @@ -17,8 +18,9 @@ public class JavaTXCompiler { public void typeInference(){ ConstraintSet cons = new ConstraintSet(); for(SourceFile sf : sourceFiles){ - cons.addAll(sf.getConstraints(new TypeInferenceInformation())); + cons.addAll(sf.getConstraints(sf.getTypeInferenceInformation(sourceFiles))); } + System.out.println(cons); } public void parse(File sourceFile) throws IOException, ClassNotFoundException { diff --git a/src/de/dhbwstuttgart/parser/JavaTXParser.java b/src/de/dhbwstuttgart/parser/JavaTXParser.java index 7b206bf8..4fa172cf 100644 --- a/src/de/dhbwstuttgart/parser/JavaTXParser.java +++ b/src/de/dhbwstuttgart/parser/JavaTXParser.java @@ -1,8 +1,8 @@ package de.dhbwstuttgart.parser; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator; import de.dhbwstuttgart.parser.antlr.Java8Lexer; import de.dhbwstuttgart.parser.antlr.Java8Parser; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typecheck.JavaClassRegistry; import org.antlr.v4.runtime.ANTLRInputStream; diff --git a/src/de/dhbwstuttgart/parser/RunParser.java b/src/de/dhbwstuttgart/parser/RunParser.java index 0078a934..4480eb48 100644 --- a/src/de/dhbwstuttgart/parser/RunParser.java +++ b/src/de/dhbwstuttgart/parser/RunParser.java @@ -1,4 +1,5 @@ package de.dhbwstuttgart.parser; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator; import de.dhbwstuttgart.parser.antlr.Java8Lexer; import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.typecheck.JavaClassName; @@ -39,7 +40,7 @@ public class RunParser{ System.out.println(c.toString()); } System.out.println("classes:"); - for(ClassOrInterface c : f.KlassenVektor){ + for(ClassOrInterface c : f.getClasses()){ int mod = c.getModifiers(); System.out.println(Modifier.toString(mod)); System.out.println(c.getClassName().toString()); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java deleted file mode 100644 index c9c0f140..00000000 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java +++ /dev/null @@ -1,1027 +0,0 @@ -package de.dhbwstuttgart.parser; - -import de.dhbwstuttgart.parser.antlr.Java8Parser; -import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.operator.AndOp; -import de.dhbwstuttgart.syntaxtree.operator.OrOp; -import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.Super; -import de.dhbwstuttgart.syntaxtree.statement.literal.*; -import de.dhbwstuttgart.syntaxtree.type.RefType; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.typecheck.*; - -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.tree.TerminalNode; - -import sun.reflect.generics.reflectiveObjects.NotImplementedException; - -public class SyntaxTreeGenerator{ - private JavaClassRegistry reg; - private String pkgName = ""; - List imports = new ArrayList(); - - public SyntaxTreeGenerator(JavaClassRegistry reg){ - this.reg = reg; - } - - public void setPackageName(Java8Parser.CompilationUnitContext ctx){ - if(ctx.packageDeclaration() != null){ - for(TerminalNode t : ctx.packageDeclaration().Identifier()){ - this.pkgName = this.pkgName + "." + t.toString(); - } - this.pkgName = this.pkgName.substring(1); - } - } - - public void getNames(Java8Parser.CompilationUnitContext ctx){ - if(this.pkgName == "") this.setPackageName(ctx); - String nameString = ""; - for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ - if(typeDecl.interfaceDeclaration() != null){ - if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){ - if(this.pkgName != ""){ - nameString = this.pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); - } - else{ - nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); - } - //Die Generic TypeParameter Definitionen ebenfalls an die JavaClassName-Registry anfügen: - if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){ - for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){ - this.reg.add(tp.Identifier().toString()); - } - } - this.reg.add(nameString); - } - } - else{ - if(typeDecl.classDeclaration().normalClassDeclaration() != null){ - if(this.pkgName != ""){ - nameString = this.pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); - } - else{ - nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); - } - //Die Generic TypeParameter Definitionen ebenfalls an die JavaClassName-Registry anfügen: - if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){ - for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){ - this.reg.add(tp.Identifier().toString()); - } - } - this.reg.add(nameString); - } - } - } - } - - public JavaClassRegistry getReg(){ - return this.reg; - } - - // Converts type name to String. - public String convertTypeName(Java8Parser.TypeNameContext ctx){ - String ret; - if(ctx.packageOrTypeName() == null){ - ret = ctx.Identifier().toString(); - } - else{ - ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." + ctx.Identifier().toString(); - } - return ret; - } - - // Converts PackageOrTypeName to String. - public String convertPackageOrTypeName(Java8Parser.PackageOrTypeNameContext ctx){ - String ret; - if(ctx.packageOrTypeName() == null){ - ret = ctx.Identifier().toString(); - } - else{ - ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." + ctx.Identifier().toString(); - } - return ret; - } - - public void setImports(Java8Parser.CompilationUnitContext ctx) throws InvalidClassNameException{ - List newImports = new ArrayList(); - for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ - if(importDeclCtx.singleTypeImportDeclaration() != null){ - newImports.add(convertSingleTypeImportDeclaration(importDeclCtx.singleTypeImportDeclaration())); - } - else if(importDeclCtx.typeImportOnDemandDeclaration() != null){ - newImports.add(convertTypeImportOnDemandDeclaration(importDeclCtx.typeImportOnDemandDeclaration())); - } - else if(importDeclCtx.singleStaticImportDeclaration() != null){ - newImports.add(convertSingleStaticImportDeclaration(importDeclCtx.singleStaticImportDeclaration())); - } - else{ - newImports.add(convertStaticImportOnDemandDeclaration(importDeclCtx.staticImportOnDemandDeclaration())); - } - } - this.imports.addAll(newImports); - } - - private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws InvalidClassNameException{ - String typeName = convertTypeName(ctx.typeName()); - String packageName = getPackageFromClass(typeName); - List classes = PackageCrawler.getClassNames(packageName); - reg.add(typeName); - JavaClassName ret = reg.getName(typeName); - if(classes.contains(ret)){ - return ret; - } - else{ - throw new InvalidClassNameException(); - } - } - - private JavaClassName convertTypeImportOnDemandDeclaration(Java8Parser.TypeImportOnDemandDeclarationContext ctx){ - return null; - } - - private JavaClassName convertSingleStaticImportDeclaration(Java8Parser.SingleStaticImportDeclarationContext ctx){ - return null; - } - - private JavaClassName convertStaticImportOnDemandDeclaration(Java8Parser.StaticImportOnDemandDeclarationContext ctx){ - return null; - } - - private String getPackageFromClass(String cls){ - String ret = ""; - String[] parts = cls.split("\\."); - for(int i = 0; i < parts.length - 1; i++){ - ret = ret + "." + parts[i]; - } - ret = ret.substring(1); - return ret; - } - - public SourceFile convert(Java8Parser.CompilationUnitContext ctx){ - List classes = new ArrayList<>(); - this.getNames(ctx); - for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ - ClassOrInterface newClass; - if(typeDecl.classDeclaration() != null){ - newClass = convertClass(typeDecl.classDeclaration()); - } - else{ - newClass = convertInterface(typeDecl.interfaceDeclaration()); - } - classes.add(newClass); - } - return new SourceFile(this.pkgName, classes, this.imports); - } - - private ClassOrInterface convertClass(Java8Parser.ClassDeclarationContext ctx) { - ClassOrInterface newClass; - if(ctx.normalClassDeclaration() != null){ - newClass = convertNormal(ctx.normalClassDeclaration()); - } - else{ - newClass = convertEnum(ctx.enumDeclaration()); - } - return newClass; - } - - private ClassOrInterface convertNormal(Java8Parser.NormalClassDeclarationContext ctx){ - int modifiers = 0; - if(ctx.classModifier() != null){ - for(Java8Parser.ClassModifierContext mod : ctx.classModifier()){ - int newModifier = convert(mod); - modifiers += newModifier; - } - } - JavaClassName name = convert(ctx.Identifier()); - Block class_block = null; - List fielddecl = convertFields(ctx.classBody()); - List methods = convertMethods(ctx.classBody()); - GenericDeclarationList genericClassParameters = null; - Token offset = ctx.getStart(); - RefTypeOrTPH superClass = null; - Boolean isInterface = false; - List implementedInterfaces = null; - return new ClassOrInterface(modifiers, name, fielddecl, methods, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); - } - - private List convertMethods(Java8Parser.ClassBodyContext classBodyContext) { - List ret = new ArrayList<>(); - for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ - if(classMember.classMemberDeclaration() != null){ - Java8Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); - if(classMemberDeclarationContext.fieldDeclaration() != null){ - //Do nothing! - }else if(classMemberDeclarationContext.methodDeclaration()!= null){ - ret.add(convert(classMemberDeclarationContext.methodDeclaration())); - } - } - } - return ret; - } - - private List convertFields(Java8Parser.ClassBodyContext classBodyContext) { - List ret = new ArrayList<>(); - for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ - if(classMember.classMemberDeclaration() != null){ - Java8Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); - if(classMemberDeclarationContext.fieldDeclaration() != null){ - ret.addAll(convert(classMember.classMemberDeclaration().fieldDeclaration())); - }else if(classMemberDeclarationContext.methodDeclaration()!= null){ - //Do nothing! - } - } - } - return ret; - } - - private Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext) { - Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader(); - String name = header.methodDeclarator().Identifier().getText(); - - RefTypeOrTPH retType; - if(header.result() != null){ - if(header.result().unannType() != null){ - retType = convert(header.result().unannType()); - } - else retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart()); - }else{ - retType = TypePlaceholder.fresh(header.getStart()); - } - int modifiers = convert(methodDeclarationContext.methodModifier()); - ParameterList parameterList = convert(header.methodDeclarator().formalParameterList()); - Block block = null; - if(methodDeclarationContext.methodBody().block() == null){ - if(! Modifier.isAbstract(modifiers)){ - //TODO: Error! Abstrakte Methode ohne abstrakt Keyword - } - }else{ - block = convert(methodDeclarationContext.methodBody().block()); - } - GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart()); - if(methodDeclarationContext.methodHeader().typeParameters() != null){ - gtvDeclarations = convert(methodDeclarationContext.methodHeader().typeParameters()); - } - return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, methodDeclarationContext.getStart()); - } - - private GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext) { - return null; - } - - private ParameterList convert(Java8Parser.FormalParameterListContext formalParameterListContext) { - List ret = new ArrayList<>(); - List fps = new ArrayList<>(); - if(formalParameterListContext == null || formalParameterListContext.lastFormalParameter() == null) - return new ParameterList(ret, new NullToken()); //Dann ist die Parameterliste leer - - if(formalParameterListContext.lastFormalParameter().formalParameter() == null)throw new NotImplementedException(); - - if(formalParameterListContext != null && formalParameterListContext.formalParameters() != null - && formalParameterListContext.formalParameters().formalParameter() != null){ - fps = new ArrayList<>(formalParameterListContext.formalParameters().formalParameter()); - } - fps.add(formalParameterListContext.lastFormalParameter().formalParameter()); - - for(Java8Parser.FormalParameterContext fp : fps){ - String paramName = convert(fp.variableDeclaratorId()); - RefTypeOrTPH type; - if(fp.unannType() != null){ - type = convert(fp.unannType()); - }else{ - type = TypePlaceholder.fresh(fp.getStart()); - } - ret.add(new FormalParameter(paramName, type, fp.getStart())); - } - return new ParameterList(ret, ret.get(0).getOffset()); - } - - private int convert(List methodModifierContexts) { - int ret = 0; - for(Java8Parser.MethodModifierContext mod : methodModifierContexts){ - if(mod.annotation() == null)convertModifier(mod.getText()); - } - return ret; - } - - private List convert(Java8Parser.FieldDeclarationContext fieldDeclarationContext) { - List ret = new ArrayList<>(); - int modifiers = 0; - for(Java8Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()){ - modifiers+=(convert(fieldModifierContext)); - } - RefTypeOrTPH fieldType = convert(fieldDeclarationContext.unannType()); - for(Java8Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){ - String fieldName = convert(varCtx.variableDeclaratorId()); - if(varCtx.variableInitializer() != null){ - initializeField(fieldDeclarationContext); - } - else{ - ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart())); - } - } - return ret; - } - - private String convert(Java8Parser.VariableDeclaratorIdContext variableDeclaratorIdContext) { - return variableDeclaratorIdContext.getText(); - } - - // Initialize a field by creating implicit constructor. - private void initializeField(Java8Parser.FieldDeclarationContext ctx){ - //TODO - } - - private RefTypeOrTPH convert(Java8Parser.UnannTypeContext unannTypeContext) { - if(unannTypeContext.unannPrimitiveType()!=null){ - throw new NotImplementedException(); - }else - if(unannTypeContext.unannReferenceType().unannArrayType()!=null){ - throw new NotImplementedException(); - }else - if(unannTypeContext.unannReferenceType().unannTypeVariable()!=null){ - JavaClassName name = reg.getName(unannTypeContext.unannReferenceType().unannTypeVariable().Identifier().toString()); - return new RefType(name, unannTypeContext.getStart()); - } - return convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType()); - } - - private RefTypeOrTPH convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext) { - String name = unannClassOrInterfaceTypeContext.getText(); - - return new RefType(reg.getName(name), unannClassOrInterfaceTypeContext.getStart()); - } - - private int convertModifier(String modifier){ - HashMap modifiers = new HashMap<>(); - modifiers.put(Modifier.toString(Modifier.PUBLIC), Modifier.PUBLIC); - modifiers.put(Modifier.toString(Modifier.PRIVATE), Modifier.PRIVATE); - modifiers.put(Modifier.toString(Modifier.PROTECTED), Modifier.PROTECTED); - modifiers.put(Modifier.toString(Modifier.ABSTRACT), Modifier.ABSTRACT); - modifiers.put(Modifier.toString(Modifier.STATIC), Modifier.STATIC); - modifiers.put(Modifier.toString(Modifier.STRICT), Modifier.STRICT); - modifiers.put(Modifier.toString(Modifier.FINAL), Modifier.FINAL); - modifiers.put(Modifier.toString(Modifier.TRANSIENT), Modifier.TRANSIENT); - modifiers.put(Modifier.toString(Modifier.VOLATILE), Modifier.VOLATILE); - modifiers.put(Modifier.toString(Modifier.SYNCHRONIZED), Modifier.SYNCHRONIZED); - modifiers.put(Modifier.toString(Modifier.NATIVE), Modifier.NATIVE); - modifiers.put(Modifier.toString(Modifier.INTERFACE), Modifier.INTERFACE); - int ret = 0; - for(String m : modifiers.keySet()){ - if(modifier.startsWith(m))ret+=modifiers.get(m); - } - return ret; - } - - private int convert(Java8Parser.ClassModifierContext ctx){ - if(ctx.annotation() != null)return 0; - return convertModifier(ctx.getText()); - } - - private int convert(Java8Parser.FieldModifierContext ctx){ - if(ctx.annotation() != null)return 0; - return convertModifier(ctx.getText()); - } - - - /** - Converts a TerminalNode to JavaClassName. If pkgName is set, it will be included like expected. - **/ - private JavaClassName convert(TerminalNode t){ - String name = ""; - if(this.pkgName != null){ - name = this.pkgName + "." + t.toString(); - } - else{ - name = t.toString(); - } - return this.reg.getName(name); - } - - private ClassOrInterface convertEnum(Java8Parser.EnumDeclarationContext ctx){ - return null; - } - - private ClassOrInterface convertInterface(Java8Parser.InterfaceDeclarationContext ctx){ - return null; - } - - /* - * StatementGeneration: - */ - - public Statement convert(Java8Parser.StatementContext stmt) { - if (stmt.statementWithoutTrailingSubstatement() != null) { - return convert(stmt.statementWithoutTrailingSubstatement()); - } else if (stmt.whileStatement() != null) { - return convert(stmt.whileStatement()); - } else if (stmt.forStatement() != null) { - return convert(stmt.forStatement()); - } else if (stmt.ifThenElseStatement() != null) { - return convert(stmt.ifThenElseStatement()); - } else if (stmt.ifThenStatement() != null) { - return convert(stmt.ifThenStatement()); - } else if (stmt.labeledStatement() != null) { - return convert(stmt.labeledStatement() ); - }else throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.StatementNoShortIfContext stmt){ - if(stmt.statementWithoutTrailingSubstatement() != null){ - return convert(stmt.statementWithoutTrailingSubstatement()); - }else if(stmt.labeledStatementNoShortIf() != null){ - return convert(stmt.labeledStatementNoShortIf()); - }else if(stmt.ifThenElseStatementNoShortIf() != null){ - return convert(stmt.ifThenElseStatementNoShortIf()); - }else if(stmt.whileStatementNoShortIf() != null){ - return convert(stmt.whileStatementNoShortIf()); - }else if(stmt.forStatementNoShortIf() != null){ - return convert(stmt.forStatementNoShortIf()); - }else throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.StatementWithoutTrailingSubstatementContext stmt) { - if(stmt.block() != null){ - return convert(stmt.block()); - }else if(stmt.emptyStatement() != null){ - return new EmptyStmt(); - }else if(stmt.expressionStatement() != null){ - return convert(stmt.expressionStatement()); - }else if(stmt.assertStatement() != null){ - return convert(stmt.assertStatement()); - }else if(stmt.switchStatement() != null){ - return convert(stmt.switchStatement()); - }else if(stmt.doStatement() != null){ - return convert(stmt.doStatement()); - }else if(stmt.breakStatement() != null){ - return convert(stmt.breakStatement()); - }else if(stmt.continueStatement() != null){ - return convert(stmt.continueStatement()); - }else if(stmt.returnStatement() != null){ - return convert(stmt.returnStatement()); - }else if(stmt.synchronizedStatement() != null){ - return convert(stmt.synchronizedStatement()); - }else if(stmt.throwStatement() != null){ - return convert(stmt.throwStatement()); - }else if(stmt.tryStatement() != null){ - return convert(stmt.tryStatement()); - }else throw new NotImplementedException(); - } - - private Block convert(Java8Parser.BlockContext block) { - List statements = new ArrayList<>(); - if(block.blockStatements() != null) - for(Java8Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()){ - Statement stmt = convert(statementContext); - statements.add(stmt); - } - - return new Block(statements, block.getStart()); - } - - private de.dhbwstuttgart.syntaxtree.statement.Statement convert(Java8Parser.BlockStatementContext statementContext) { - List ret = new ArrayList<>(); - if(statementContext.localVariableDeclarationStatement() != null){ - return convert(statementContext.localVariableDeclarationStatement()); - }else if(statementContext.classDeclaration() != null){ - throw new NotImplementedException(); - }else{ - Java8Parser.StatementContext stmt = statementContext.statement(); - return convert(stmt); - } - } - - private Statement convert(Java8Parser.LocalVariableDeclarationStatementContext stmt) { - Java8Parser.LocalVariableDeclarationContext declaration = stmt.localVariableDeclaration(); - return convert(declaration); - } - - private Statement convert(Java8Parser.LabeledStatementContext labeledStatementContext) { - throw new NotImplementedException(); - //return convert(labeledStatementContext.statement()); - } - - private Statement convert(Java8Parser.LabeledStatementNoShortIfContext stmt){ - throw new NotImplementedException(); - //return convert(stmt.statementNoShortIf()); - } - - private Statement convert(Java8Parser.ExpressionStatementContext stmt){ - return convert(stmt.statementExpression()); - } - - private Statement convert(Java8Parser.StatementExpressionContext stmt) { - if(stmt.assignment() != null){ - return convert(stmt.assignment()); - }else if(stmt.preIncrementExpression() != null){ - return convert(stmt.preIncrementExpression()); - }else if(stmt.preDecrementExpression() != null){ - return convert(stmt.preDecrementExpression()); - }else if(stmt.postIncrementExpression() != null){ - return convert(stmt.postIncrementExpression()); - }else if(stmt.postDecrementExpression() != null){ - return convert(stmt.postDecrementExpression()); - }else if(stmt.methodInvocation() != null){ - return convert(stmt.methodInvocation()); - }else if(stmt.classInstanceCreationExpression() != null){ - return convert(stmt.classInstanceCreationExpression()); - }else throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.MethodInvocationContext methodInvocationContext) { - String name; - if(methodInvocationContext.methodName()!=null){ - name = methodInvocationContext.methodName().Identifier().getText(); - }else{// if(methodInvocationContext.Identifier() != null){ - name = methodInvocationContext.Identifier().getText(); - } - Expression receiver; - if(methodInvocationContext.typeName() != null){ - receiver = new LocalOrFieldVarOrClassname(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart()); - }else if(methodInvocationContext.expressionName()!=null){ - receiver = convert(methodInvocationContext.expressionName()); - }else if(methodInvocationContext.primary() != null){ - receiver = convert(methodInvocationContext.primary()); - }else if(methodInvocationContext.toString().startsWith("super")){ - receiver = new Super(methodInvocationContext.getStart()); - }else throw new NotImplementedException(); - - ArgumentList argumentList = null; - MethodCall ret = new MethodCall(new Receiver(receiver), name, argumentList, methodInvocationContext.getStart()); - return ret; - } - - private Expression convert(Java8Parser.ExpressionNameContext expressionNameContext) { - /* - Der Parser könnte feststellen, ob es sich um einen Klassennamen, - eine lokale Variable oder einen Feldzugriff handelt. - Das überprüft allerdings sowieso der Typinferenzalgorithmus, - da er auch die Typchecks übernimmt. Code wäre redundant und würde - Möglichkeiten des Typinferenzalgorithmus einschränken. - */ - return new LocalOrFieldVarOrClassname(expressionNameContext.getText(), expressionNameContext.getStart()); - } - - - private Statement convert(Java8Parser.ClassInstanceCreationExpressionContext stmt) { - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.AssignmentContext stmt) { - LocalOrFieldVar leftHandSide = convert(stmt.leftHandSide()); - return new Assign(leftHandSide, convert(stmt.expression()), stmt.getStart()); - } - - private LocalOrFieldVar convert(Java8Parser.LeftHandSideContext leftHandSide) { - return new LocalOrFieldVar(leftHandSide.toString(), leftHandSide.getStart()); - } - - private Statement convert(Java8Parser.IfThenStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.AssertStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.SwitchStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.SwitchBlockContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.SwitchBlockStatementGroupContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.WhileStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.WhileStatementNoShortIfContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.DoStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.ForStatementContext stmt){ - if(stmt.basicForStatement() != null){ - return convert(stmt.basicForStatement()); - }else if(stmt.enhancedForStatement() != null){ - return convert(stmt.enhancedForStatement()); - }else throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.ForStatementNoShortIfContext stmt){ - if(stmt.basicForStatementNoShortIf() != null){ - return convert(stmt.basicForStatementNoShortIf()); - }else if(stmt.enhancedForStatementNoShortIf() != null){ - return convert(stmt.enhancedForStatementNoShortIf()); - }else throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.BasicForStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.BasicForStatementNoShortIfContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.ForInitContext stmt){ - if(stmt.statementExpressionList() != null){ - return convert(stmt.statementExpressionList()); - }else if(stmt.localVariableDeclaration() != null){ - return convert(stmt.localVariableDeclaration()); - }else throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.LocalVariableDeclarationContext declaration) { - List declarations = new ArrayList<>(); - if(declaration.variableModifier() != null && declaration.variableModifier().size() > 0){ - //TODO - } - RefTypeOrTPH type; - if(declaration.unannType()==null){ - type = TypePlaceholder.fresh(declaration.getStart()); - }else{ - type = convert(declaration.unannType()); - } - for(Java8Parser.VariableDeclaratorContext varDecl : declaration.variableDeclaratorList().variableDeclarator()){ - TerminalNode name = varDecl.variableDeclaratorId().Identifier(); - declarations.add(new LocalVarDecl(name.getText(), type, name.getSymbol())); - } - if(declarations.size()==1)return declarations.get(0); - return new LocalVarBunchDeclaration(declarations, declaration.getStart()); - } - - private Statement convert(Java8Parser.ForUpdateContext stmt){ - return convert(stmt.statementExpressionList()); - } - - private Statement convert(Java8Parser.StatementExpressionListContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.EnhancedForStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.EnhancedForStatementNoShortIfContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.BreakStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.ContinueStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.ReturnStatementContext stmt){ - return new Return(convert(stmt.expression()),stmt.getStart()); - //throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.ThrowStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.SynchronizedStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.TryStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.CatchesContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java8Parser.CatchClauseContext stmt){ - //TODO - throw new NotImplementedException(); - } - -/* -***************+ Expression Conversions: - */ - - private Expression convert(Java8Parser.ExpressionContext expression) { - if(expression.lambdaExpression()!=null){ - return convert(expression.lambdaExpression()); - }else{ - return convert(expression.assignmentExpression()); - } - } - - private Expression convert(Java8Parser.AssignmentExpressionContext expression) { - if(expression.conditionalExpression() != null){ - return convert(expression.conditionalExpression()); - }else{ - return convert(expression.assignment()); - } - } - - private Expression convert(Java8Parser.ConditionalExpressionContext expression) { - if(expression.conditionalOrExpression() != null){ - return convert(expression.conditionalOrExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.ConditionalOrExpressionContext expression) { - if(expression.conditionalOrExpression() == null){ - return convert(expression.conditionalAndExpression()); - }else{ - return new Binary(convert(expression.conditionalOrExpression()), - convert(expression.conditionalAndExpression()), new OrOp(null)); - } - } - - private Expression convert(Java8Parser.ConditionalAndExpressionContext expression) { - if(expression.conditionalAndExpression() == null){ - return convert(expression.inclusiveOrExpression()); - }else{ - return new Binary(convert(expression.conditionalAndExpression()), - convert(expression.inclusiveOrExpression()), new AndOp(null)); - } - } - - private Expression convert(Java8Parser.InclusiveOrExpressionContext expression) { - if(expression.inclusiveOrExpression() == null){ - return convert(expression.exclusiveOrExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.ExclusiveOrExpressionContext expression) { - if(expression.exclusiveOrExpression() == null){ - return convert(expression.andExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.AndExpressionContext expression) { - if(expression.andExpression() == null){ - return convert(expression.equalityExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.EqualityExpressionContext expression) { - if(expression.equalityExpression() == null){ - return convert(expression.relationalExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.RelationalExpressionContext expression) { - if(expression.relationalExpression() == null){ - return convert(expression.shiftExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.ShiftExpressionContext expression) { - if(expression.shiftExpression() == null){ - return convert(expression.additiveExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.AdditiveExpressionContext expression) { - if(expression.additiveExpression() == null){ - return convert(expression.multiplicativeExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.MultiplicativeExpressionContext expression) { - if(expression.multiplicativeExpression() == null){ - return convert(expression.unaryExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.UnaryExpressionContext expression) { - if(expression.preIncrementExpression() != null){ - return convert(expression.preIncrementExpression()); - }else if(expression.preDecrementExpression() != null){ - return convert(expression.preDecrementExpression()); - }else if(expression.unaryExpressionNotPlusMinus() != null){ - return convert(expression.unaryExpressionNotPlusMinus()); - }else if(expression.getText().startsWith("+")){ - return new UnaryPlus(convert(expression.unaryExpression())); - }else if(expression.getText().startsWith("-")){ - return new UnaryMinus(convert(expression.unaryExpression())); - }else{ - //Diese Exceptions sollte nie geworfen werden. - //Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben. - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.UnaryExpressionNotPlusMinusContext expression) { - if(expression.postfixExpression() != null){ - return convert(expression.postfixExpression()); - }else if(expression.castExpression() != null){ - return convert(expression.castExpression()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.CastExpressionContext expressionContext) { - throw new NotImplementedException(); - } - - private Expression convert(Java8Parser.PostfixExpressionContext expression) { - Expression expr; - if(expression.primary() != null){ - expr = convert(expression.primary()); - }else{ - expr = convert(expression.expressionName()); - } - if(expression.postDecrementExpression_lf_postfixExpression() == null && - expression.postIncrementExpression_lf_postfixExpression() == null){ - return expr; - } - - for(Java8Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression.postIncrementExpression_lf_postfixExpression()){ - expr = new PostIncExpr(expr); - } - for(Java8Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression.postDecrementExpression_lf_postfixExpression()){ - expr = new PostDecExpr(expr); - } - - return expr; - } - - private Expression convert(Java8Parser.PrimaryContext primary) { - Expression expr; - if(primary.primaryNoNewArray_lfno_primary()!=null){ - expr = convert(primary.primaryNoNewArray_lfno_primary()); - }else{ - expr = convert(primary.arrayCreationExpression()); - } - - if(primary.primaryNoNewArray_lf_primary() != null && primary.primaryNoNewArray_lf_primary().size()>0){ - for(Java8Parser.PrimaryNoNewArray_lf_primaryContext e : primary.primaryNoNewArray_lf_primary()){ - expr = new LocalOrFieldVarOrClassname(expr, e.getText()); - } - } - return expr; - } - - private Expression convert(Java8Parser.ArrayCreationExpressionContext expression) { - throw new NotImplementedException(); - } - - private Expression convert(Java8Parser.PrimaryNoNewArray_lfno_primaryContext expression) { - if(expression.literal() != null){ - return convert(expression.literal()); - }else if(expression.expression()!=null){ - return convert(expression.expression()); - }else if(expression.methodInvocation_lfno_primary() != null){ - return convert(expression.methodInvocation_lfno_primary()); - }else{ - throw new NotImplementedException(); - } - } - - private Expression convert(Java8Parser.LiteralContext literal) { - /*if(literal.IntegerLiteral() != null){ - - RefType type = new RefType(reg.getName("java.lang.Integer"),literal.getStart()); - return new IntLiteral(type, - Integer.parseInt(literal.toString()), - literal.getStart()); - }else if(literal.FloatingPointLiteral() != null){ - RefType type = new RefType(reg.getName("java.lang.Float"),literal.getStart()); - return new FloatLiteral(type, - Float.parseFloat(literal.toString()), - literal.getStart()); - */ - if(literal.IntegerLiteral() != null){ - Number value = Double.parseDouble(literal.toString()); - return new NumberLiteral(TypePlaceholder.fresh(literal.getStart()), - value, literal.getStart()); - }else if(literal.BooleanLiteral() != null){ - RefType type = new RefType(reg.getName("java.lang.Boolean"),literal.getStart()); - return new BoolLiteral(type, - Boolean.parseBoolean(literal.toString()), - literal.getStart()); - }else if(literal.CharacterLiteral() != null){ - RefType type = new RefType(reg.getName("java.lang.Character"),literal.getStart()); - return new CharLiteral(type, - literal.toString().charAt(0), - literal.getStart()); - }else if(literal.StringLiteral()!=null){ - RefType type = new RefType(reg.getName("java.lang.String"),literal.getStart()); - return new StringLiteral(type, - literal.toString(), - literal.getStart()); - }else {// if(literal.NullLiteral() != null){ - return new Null(TypePlaceholder.fresh(literal.getStart()), - literal.getStart()); - } - } - - private Expression convert(Java8Parser.MethodInvocation_lfno_primaryContext methodInvocationContext) { - String name; - if(methodInvocationContext.methodName()!=null){ - name = methodInvocationContext.methodName().Identifier().getText(); - }else{// if(methodInvocationContext.Identifier() != null){ - name = methodInvocationContext.Identifier().getText(); - } - Expression receiver; - if(methodInvocationContext.typeName() != null){ - receiver = new LocalOrFieldVarOrClassname(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart()); - }else if(methodInvocationContext.expressionName()!=null){ - receiver = convert(methodInvocationContext.expressionName()); - }else if(methodInvocationContext.toString().startsWith("super")){ - receiver = new Super(methodInvocationContext.getStart()); - }else throw new NotImplementedException(); - - ArgumentList argumentList = null; - MethodCall ret = new MethodCall(new Receiver(receiver), name, argumentList, methodInvocationContext.getStart()); - return ret; - } - - private static Expression convert(Java8Parser.LambdaExpressionContext expression) { - throw new NotImplementedException(); - } - -} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java new file mode 100644 index 00000000..acfda933 --- /dev/null +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -0,0 +1,770 @@ +package de.dhbwstuttgart.parser.SyntaxTreeGenerator; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.antlr.Java8Parser; +import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.operator.AndOp; +import de.dhbwstuttgart.syntaxtree.operator.OrOp; +import de.dhbwstuttgart.syntaxtree.statement.*; +import de.dhbwstuttgart.syntaxtree.statement.literal.*; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typecheck.JavaClassName; +import de.dhbwstuttgart.typecheck.JavaClassRegistry; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.tree.TerminalNode; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class StatementGenerator { + + private JavaClassRegistry reg; + private Set localVars = new HashSet<>(); + + public StatementGenerator(JavaClassRegistry reg){ + this.reg = reg; + } + + + public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext) { + Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader(); + String name = header.methodDeclarator().Identifier().getText(); + + RefTypeOrTPH retType; + if(header.result() != null){ + if(header.result().unannType() != null){ + retType = TypeGenerator.convert(header.result().unannType(), reg); + } + else retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart()); + }else{ + retType = TypePlaceholder.fresh(header.getStart()); + } + int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier()); + ParameterList parameterList = convert(header.methodDeclarator().formalParameterList()); + Block block = null; + if(methodDeclarationContext.methodBody().block() == null){ + if(! Modifier.isAbstract(modifiers)){ + //TODO: Error! Abstrakte Methode ohne abstrakt Keyword + } + }else{ + block = this.convert(methodDeclarationContext.methodBody().block()); + } + GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart()); + if(methodDeclarationContext.methodHeader().typeParameters() != null){ + gtvDeclarations = TypeGenerator.convert(methodDeclarationContext.methodHeader().typeParameters()); + } + return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, methodDeclarationContext.getStart()); + } + + private ParameterList convert(Java8Parser.FormalParameterListContext formalParameterListContext) { + List ret = new ArrayList<>(); + List fps = new ArrayList<>(); + if(formalParameterListContext == null || formalParameterListContext.lastFormalParameter() == null) + return new ParameterList(ret, new NullToken()); //Dann ist die Parameterliste leer + + if(formalParameterListContext.lastFormalParameter().formalParameter() == null)throw new NotImplementedException(); + + if(formalParameterListContext != null && formalParameterListContext.formalParameters() != null + && formalParameterListContext.formalParameters().formalParameter() != null){ + fps = new ArrayList<>(formalParameterListContext.formalParameters().formalParameter()); + } + fps.add(formalParameterListContext.lastFormalParameter().formalParameter()); + + for(Java8Parser.FormalParameterContext fp : fps){ + String paramName = SyntaxTreeGenerator.convert(fp.variableDeclaratorId()); + RefTypeOrTPH type; + if(fp.unannType() != null){ + type = TypeGenerator.convert(fp.unannType(), reg); + }else{ + type = TypePlaceholder.fresh(fp.getStart()); + } + ret.add(new FormalParameter(paramName, type, fp.getStart())); + localVars.add(paramName); + } + return new ParameterList(ret, ret.get(0).getOffset()); + } + + /* + * StatementGeneration: + */ + + private Statement convert(Java8Parser.StatementContext stmt) { + if (stmt.statementWithoutTrailingSubstatement() != null) { + return convert(stmt.statementWithoutTrailingSubstatement()); + } else if (stmt.whileStatement() != null) { + return convert(stmt.whileStatement()); + } else if (stmt.forStatement() != null) { + return convert(stmt.forStatement()); + } else if (stmt.ifThenElseStatement() != null) { + return convert(stmt.ifThenElseStatement()); + } else if (stmt.ifThenStatement() != null) { + return convert(stmt.ifThenStatement()); + } else if (stmt.labeledStatement() != null) { + return convert(stmt.labeledStatement() ); + }else throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.StatementNoShortIfContext stmt){ + if(stmt.statementWithoutTrailingSubstatement() != null){ + return convert(stmt.statementWithoutTrailingSubstatement()); + }else if(stmt.labeledStatementNoShortIf() != null){ + return convert(stmt.labeledStatementNoShortIf()); + }else if(stmt.ifThenElseStatementNoShortIf() != null){ + return convert(stmt.ifThenElseStatementNoShortIf()); + }else if(stmt.whileStatementNoShortIf() != null){ + return convert(stmt.whileStatementNoShortIf()); + }else if(stmt.forStatementNoShortIf() != null){ + return convert(stmt.forStatementNoShortIf()); + }else throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.StatementWithoutTrailingSubstatementContext stmt) { + if(stmt.block() != null){ + return convert(stmt.block()); + }else if(stmt.emptyStatement() != null){ + return new EmptyStmt(); + }else if(stmt.expressionStatement() != null){ + return convert(stmt.expressionStatement()); + }else if(stmt.assertStatement() != null){ + return convert(stmt.assertStatement()); + }else if(stmt.switchStatement() != null){ + return convert(stmt.switchStatement()); + }else if(stmt.doStatement() != null){ + return convert(stmt.doStatement()); + }else if(stmt.breakStatement() != null){ + return convert(stmt.breakStatement()); + }else if(stmt.continueStatement() != null){ + return convert(stmt.continueStatement()); + }else if(stmt.returnStatement() != null){ + return convert(stmt.returnStatement()); + }else if(stmt.synchronizedStatement() != null){ + return convert(stmt.synchronizedStatement()); + }else if(stmt.throwStatement() != null){ + return convert(stmt.throwStatement()); + }else if(stmt.tryStatement() != null){ + return convert(stmt.tryStatement()); + }else throw new NotImplementedException(); + } + + public Block convert(Java8Parser.BlockContext block) { + List statements = new ArrayList<>(); + if(block.blockStatements() != null) + for(Java8Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()){ + Statement stmt = convert(statementContext); + statements.add(stmt); + } + + return new Block(statements, block.getStart()); + } + + private de.dhbwstuttgart.syntaxtree.statement.Statement convert(Java8Parser.BlockStatementContext statementContext) { + List ret = new ArrayList<>(); + if(statementContext.localVariableDeclarationStatement() != null){ + return convert(statementContext.localVariableDeclarationStatement()); + }else if(statementContext.classDeclaration() != null){ + throw new NotImplementedException(); + }else{ + Java8Parser.StatementContext stmt = statementContext.statement(); + return convert(stmt); + } + } + + private Statement convert(Java8Parser.LocalVariableDeclarationStatementContext stmt) { + Java8Parser.LocalVariableDeclarationContext declaration = stmt.localVariableDeclaration(); + return convert(declaration); + } + + private Statement convert(Java8Parser.LabeledStatementContext labeledStatementContext) { + throw new NotImplementedException(); + //return convert(labeledStatementContext.statement()); + } + + private Statement convert(Java8Parser.LabeledStatementNoShortIfContext stmt){ + throw new NotImplementedException(); + //return convert(stmt.statementNoShortIf()); + } + + private Statement convert(Java8Parser.ExpressionStatementContext stmt){ + return convert(stmt.statementExpression()); + } + + private Statement convert(Java8Parser.StatementExpressionContext stmt) { + if(stmt.assignment() != null){ + return convert(stmt.assignment()); + }else if(stmt.preIncrementExpression() != null){ + return convert(stmt.preIncrementExpression()); + }else if(stmt.preDecrementExpression() != null){ + return convert(stmt.preDecrementExpression()); + }else if(stmt.postIncrementExpression() != null){ + return convert(stmt.postIncrementExpression()); + }else if(stmt.postDecrementExpression() != null){ + return convert(stmt.postDecrementExpression()); + }else if(stmt.methodInvocation() != null){ + return convert(stmt.methodInvocation()); + }else if(stmt.classInstanceCreationExpression() != null){ + return convert(stmt.classInstanceCreationExpression()); + }else throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.MethodInvocationContext methodInvocationContext) { + String name; + if(methodInvocationContext.methodName()!=null){ + name = methodInvocationContext.methodName().Identifier().getText(); + }else{// if(methodInvocationContext.Identifier() != null){ + name = methodInvocationContext.Identifier().getText(); + } + Expression receiver; + if(methodInvocationContext.typeName() != null){ + receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart()); + }else if(methodInvocationContext.expressionName()!=null){ + receiver = convert(methodInvocationContext.expressionName()); + }else if(methodInvocationContext.primary() != null){ + receiver = convert(methodInvocationContext.primary()); + }else if(methodInvocationContext.toString().startsWith("super")){ + receiver = new Super(methodInvocationContext.getStart()); + }else throw new NotImplementedException(); + + ArgumentList argumentList = convert(methodInvocationContext.argumentList()); + MethodCall ret = new MethodCall(new Receiver(receiver), name, argumentList, methodInvocationContext.getStart()); + return ret; + } + + private ArgumentList convert(Java8Parser.ArgumentListContext argumentListContext) { + if(argumentListContext == null)return new ArgumentList(new ArrayList<>(), new NullToken()); + List args = new ArrayList<>(); + + Token offset = new NullToken(); + for(Java8Parser.ExpressionContext expr : argumentListContext.expression()){ + args.add(convert(expr)); + } + if(args.size()>0)offset = args.get(0).getOffset(); + + return new ArgumentList(args, offset); + } + + /** + * Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und + * einer Klassenangabe unterscheiden. + * @param expression + * @param offset + * @return + */ + private Expression generateLocalOrFieldVarOrClassName(String expression, Token offset){ + String[] parts = expression.split("\\."); + if(parts.length < 2){ + //Check for localVar: + if(localVars.contains(expression)){ + return new LocalVar(expression, offset); + }else { + throw new NotImplementedException(); + } + } + return generateFieldVarOrClassname(expression, offset); + } + + private Expression generateFieldVarOrClassname(String expression, Token offset){ + String[] parts = expression.split("\\."); + String whole = ""; + Expression receiver = null; + for(String part : parts){ + whole+=part; + //Check for Classname: + if(reg.contains(whole)){ + receiver = new StaticClassName(reg.getName(whole), offset); + } + whole+="."; + } + + if(parts.length < 2){ + receiver = new This(offset); + }else if(receiver == null){ //Handelt es sich um keinen Statischen Klassennamen: + String part = expression.substring(0,expression.length() - (1 + parts[parts.length-1].length())); + receiver = generateFieldVarOrClassname(part, offset); + } + return new FieldVar(receiver, parts[parts.length-1], TypePlaceholder.fresh(offset) ,offset); + } + + private Expression convert(Java8Parser.ExpressionNameContext expressionNameContext) { + return generateLocalOrFieldVarOrClassName(expressionNameContext.getText(), expressionNameContext.getStart()); + } + + + private Statement convert(Java8Parser.ClassInstanceCreationExpressionContext stmt) { + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) { + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.AssignmentContext stmt) { + LocalVar leftHandSide = convert(stmt.leftHandSide()); + return new Assign(leftHandSide, convert(stmt.expression()), stmt.getStart()); + } + + private LocalVar convert(Java8Parser.LeftHandSideContext leftHandSide) { + return new LocalVar(leftHandSide.toString(), leftHandSide.getStart()); + } + + private Statement convert(Java8Parser.IfThenStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.AssertStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.SwitchStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.SwitchBlockContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.SwitchBlockStatementGroupContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.WhileStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.WhileStatementNoShortIfContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.DoStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.ForStatementContext stmt){ + if(stmt.basicForStatement() != null){ + return convert(stmt.basicForStatement()); + }else if(stmt.enhancedForStatement() != null){ + return convert(stmt.enhancedForStatement()); + }else throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.ForStatementNoShortIfContext stmt){ + if(stmt.basicForStatementNoShortIf() != null){ + return convert(stmt.basicForStatementNoShortIf()); + }else if(stmt.enhancedForStatementNoShortIf() != null){ + return convert(stmt.enhancedForStatementNoShortIf()); + }else throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.BasicForStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.BasicForStatementNoShortIfContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.ForInitContext stmt){ + if(stmt.statementExpressionList() != null){ + return convert(stmt.statementExpressionList()); + }else if(stmt.localVariableDeclaration() != null){ + return convert(stmt.localVariableDeclaration()); + }else throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.LocalVariableDeclarationContext declaration) { + List declarations = new ArrayList<>(); + if(declaration.variableModifier() != null && declaration.variableModifier().size() > 0){ + //TODO + } + RefTypeOrTPH type; + if(declaration.unannType()==null){ + type = TypePlaceholder.fresh(declaration.getStart()); + }else{ + type = TypeGenerator.convert(declaration.unannType(), reg); + } + for(Java8Parser.VariableDeclaratorContext varDecl : declaration.variableDeclaratorList().variableDeclarator()){ + TerminalNode name = varDecl.variableDeclaratorId().Identifier(); + declarations.add(new LocalVarDecl(name.getText(), type, name.getSymbol())); + this.localVars.add(name.getText()); + } + if(declarations.size()==1)return declarations.get(0); + return new LocalVarBunchDeclaration(declarations, declaration.getStart()); + } + + private Statement convert(Java8Parser.ForUpdateContext stmt){ + return convert(stmt.statementExpressionList()); + } + + private Statement convert(Java8Parser.StatementExpressionListContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.EnhancedForStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.EnhancedForStatementNoShortIfContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.BreakStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.ContinueStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.ReturnStatementContext stmt){ + return new Return(convert(stmt.expression()),stmt.getStart()); + //throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.ThrowStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.SynchronizedStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.TryStatementContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.CatchesContext stmt){ + //TODO + throw new NotImplementedException(); + } + + private Statement convert(Java8Parser.CatchClauseContext stmt){ + //TODO + throw new NotImplementedException(); + } + +/* +***************+ Expression Conversions: + */ + + private Expression convert(Java8Parser.ExpressionContext expression) { + if(expression.lambdaExpression()!=null){ + return convert(expression.lambdaExpression()); + }else{ + return convert(expression.assignmentExpression()); + } + } + + private Expression convert(Java8Parser.AssignmentExpressionContext expression) { + if(expression.conditionalExpression() != null){ + return convert(expression.conditionalExpression()); + }else{ + return convert(expression.assignment()); + } + } + + private Expression convert(Java8Parser.ConditionalExpressionContext expression) { + if(expression.conditionalOrExpression() != null){ + return convert(expression.conditionalOrExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.ConditionalOrExpressionContext expression) { + if(expression.conditionalOrExpression() == null){ + return convert(expression.conditionalAndExpression()); + }else{ + return new Binary(convert(expression.conditionalOrExpression()), + convert(expression.conditionalAndExpression()), new OrOp(null)); + } + } + + private Expression convert(Java8Parser.ConditionalAndExpressionContext expression) { + if(expression.conditionalAndExpression() == null){ + return convert(expression.inclusiveOrExpression()); + }else{ + return new Binary(convert(expression.conditionalAndExpression()), + convert(expression.inclusiveOrExpression()), new AndOp(null)); + } + } + + private Expression convert(Java8Parser.InclusiveOrExpressionContext expression) { + if(expression.inclusiveOrExpression() == null){ + return convert(expression.exclusiveOrExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.ExclusiveOrExpressionContext expression) { + if(expression.exclusiveOrExpression() == null){ + return convert(expression.andExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.AndExpressionContext expression) { + if(expression.andExpression() == null){ + return convert(expression.equalityExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.EqualityExpressionContext expression) { + if(expression.equalityExpression() == null){ + return convert(expression.relationalExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.RelationalExpressionContext expression) { + if(expression.relationalExpression() == null){ + return convert(expression.shiftExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.ShiftExpressionContext expression) { + if(expression.shiftExpression() == null){ + return convert(expression.additiveExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.AdditiveExpressionContext expression) { + if(expression.additiveExpression() == null){ + return convert(expression.multiplicativeExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.MultiplicativeExpressionContext expression) { + if(expression.multiplicativeExpression() == null){ + return convert(expression.unaryExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.UnaryExpressionContext expression) { + if(expression.preIncrementExpression() != null){ + return convert(expression.preIncrementExpression()); + }else if(expression.preDecrementExpression() != null){ + return convert(expression.preDecrementExpression()); + }else if(expression.unaryExpressionNotPlusMinus() != null){ + return convert(expression.unaryExpressionNotPlusMinus()); + }else if(expression.getText().startsWith("+")){ + return new UnaryPlus(convert(expression.unaryExpression())); + }else if(expression.getText().startsWith("-")){ + return new UnaryMinus(convert(expression.unaryExpression())); + }else{ + //Diese Exceptions sollte nie geworfen werden. + //Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben. + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.UnaryExpressionNotPlusMinusContext expression) { + if(expression.postfixExpression() != null){ + return convert(expression.postfixExpression()); + }else if(expression.castExpression() != null){ + return convert(expression.castExpression()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.CastExpressionContext expressionContext) { + throw new NotImplementedException(); + } + + private Expression convert(Java8Parser.PostfixExpressionContext expression) { + Expression expr; + if(expression.primary() != null){ + expr = convert(expression.primary()); + }else{ + expr = convert(expression.expressionName()); + } + if(expression.postDecrementExpression_lf_postfixExpression() == null && + expression.postIncrementExpression_lf_postfixExpression() == null){ + return expr; + } + + for(Java8Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression.postIncrementExpression_lf_postfixExpression()){ + expr = new PostIncExpr(expr); + } + for(Java8Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression.postDecrementExpression_lf_postfixExpression()){ + expr = new PostDecExpr(expr); + } + + return expr; + } + + private Expression convert(Java8Parser.PrimaryContext primary) { + Expression expr; + if(primary.primaryNoNewArray_lfno_primary()!=null){ + expr = convert(primary.primaryNoNewArray_lfno_primary()); + }else{ + expr = convert(primary.arrayCreationExpression()); + } + + if(primary.primaryNoNewArray_lf_primary() != null && primary.primaryNoNewArray_lf_primary().size()>0){ + for(Java8Parser.PrimaryNoNewArray_lf_primaryContext e : primary.primaryNoNewArray_lf_primary()){ + expr = convert(expr, e); + } + } + return expr; + } + + private Expression convert(Expression expr, Java8Parser.PrimaryNoNewArray_lf_primaryContext e) { + if(e.classInstanceCreationExpression_lf_primary() != null){ + throw new NotImplementedException(); + }else if(e.fieldAccess_lf_primary() != null){ + throw new NotImplementedException(); + }else if(e.arrayAccess_lf_primary() != null){ + throw new NotImplementedException(); + }else if(e.methodReference_lf_primary() != null){ + throw new NotImplementedException(); + }else { + Java8Parser.MethodInvocation_lf_primaryContext ctxt = e.methodInvocation_lf_primary(); + String methodName = ctxt.Identifier().toString(); + return new MethodCall(new Receiver(expr), methodName, convert(ctxt.argumentList()), e.getStart()); + } + } + + private Expression convert(Java8Parser.ArrayCreationExpressionContext expression) { + throw new NotImplementedException(); + } + + private Expression convert(Java8Parser.PrimaryNoNewArray_lfno_primaryContext expression) { + if(expression.literal() != null){ + return convert(expression.literal()); + }else if(expression.expression()!=null){ + return convert(expression.expression()); + }else if(expression.methodInvocation_lfno_primary() != null){ + return convert(expression.methodInvocation_lfno_primary()); + }else{ + throw new NotImplementedException(); + } + } + + private Expression convert(Java8Parser.LiteralContext literal) { + /*if(literal.IntegerLiteral() != null){ + + RefType type = new RefType(reg.getName("java.lang.Integer"),literal.getStart()); + return new IntLiteral(type, + Integer.parseInt(literal.toString()), + literal.getStart()); + }else if(literal.FloatingPointLiteral() != null){ + RefType type = new RefType(reg.getName("java.lang.Float"),literal.getStart()); + return new FloatLiteral(type, + Float.parseFloat(literal.toString()), + literal.getStart()); + */ + if(literal.IntegerLiteral() != null){ + Number value = Double.parseDouble(literal.toString()); + return new NumberLiteral(TypePlaceholder.fresh(literal.getStart()), + value, literal.getStart()); + }else if(literal.BooleanLiteral() != null){ + RefType type = new RefType(reg.getName("java.lang.Boolean"),literal.getStart()); + return new BoolLiteral(type, + Boolean.parseBoolean(literal.toString()), + literal.getStart()); + }else if(literal.CharacterLiteral() != null){ + RefType type = new RefType(reg.getName("java.lang.Character"),literal.getStart()); + return new CharLiteral(type, + literal.toString().charAt(0), + literal.getStart()); + }else if(literal.StringLiteral()!=null){ + RefType type = new RefType(reg.getName("java.lang.String"),literal.getStart()); + return new StringLiteral(type, + literal.toString(), + literal.getStart()); + }else {// if(literal.NullLiteral() != null){ + return new Null(TypePlaceholder.fresh(literal.getStart()), + literal.getStart()); + } + } + + private Expression convert(Java8Parser.MethodInvocation_lfno_primaryContext methodInvocationContext) { + String name; + if(methodInvocationContext.methodName()!=null){ + name = methodInvocationContext.methodName().Identifier().getText(); + }else{// if(methodInvocationContext.Identifier() != null){ + name = methodInvocationContext.Identifier().getText(); + } + Expression receiver; + if(methodInvocationContext.typeName() != null){ + receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart()); + }else if(methodInvocationContext.expressionName()!=null){ + receiver = convert(methodInvocationContext.expressionName()); + }else if(methodInvocationContext.toString().startsWith("super")){ + receiver = new Super(methodInvocationContext.getStart()); + }else throw new NotImplementedException(); + + ArgumentList argumentList = convert(methodInvocationContext.argumentList()); + MethodCall ret = new MethodCall(new Receiver(receiver), name, argumentList, methodInvocationContext.getStart()); + return ret; + } + + private static Expression convert(Java8Parser.LambdaExpressionContext expression) { + throw new NotImplementedException(); + } + +} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java new file mode 100644 index 00000000..d313e0b9 --- /dev/null +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -0,0 +1,334 @@ +package de.dhbwstuttgart.parser.SyntaxTreeGenerator; + +import de.dhbwstuttgart.parser.InvalidClassNameException; +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.PackageCrawler; +import de.dhbwstuttgart.parser.antlr.Java8Parser; +import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.statement.*; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typecheck.*; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.tree.TerminalNode; + +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +public class SyntaxTreeGenerator{ + private JavaClassRegistry reg; + private String pkgName = ""; + List imports = new ArrayList(); + + public SyntaxTreeGenerator(JavaClassRegistry reg){ + this.reg = reg; + } + + public void setPackageName(Java8Parser.CompilationUnitContext ctx){ + if(ctx.packageDeclaration() != null){ + for(TerminalNode t : ctx.packageDeclaration().Identifier()){ + this.pkgName = this.pkgName + "." + t.toString(); + } + this.pkgName = this.pkgName.substring(1); + } + } + + public void getNames(Java8Parser.CompilationUnitContext ctx){ + if(this.pkgName == "") this.setPackageName(ctx); + String nameString = ""; + for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ + if(typeDecl.interfaceDeclaration() != null){ + if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){ + if(this.pkgName != ""){ + nameString = this.pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); + } + else{ + nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); + } + //Die Generic TypeParameter Definitionen ebenfalls an die JavaClassName-Registry anfügen: + if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){ + for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){ + this.reg.add(tp.Identifier().toString()); + } + } + this.reg.add(nameString); + } + } + else{ + if(typeDecl.classDeclaration().normalClassDeclaration() != null){ + if(this.pkgName != ""){ + nameString = this.pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); + } + else{ + nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); + } + //Die Generic TypeParameter Definitionen ebenfalls an die JavaClassName-Registry anfügen: + if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){ + for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){ + this.reg.add(tp.Identifier().toString()); + } + } + this.reg.add(nameString); + } + } + } + } + + public JavaClassRegistry getReg(){ + return this.reg; + } + + // Converts type name to String. + public String convertTypeName(Java8Parser.TypeNameContext ctx){ + String ret; + if(ctx.packageOrTypeName() == null){ + ret = ctx.Identifier().toString(); + } + else{ + ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." + ctx.Identifier().toString(); + } + return ret; + } + + // Converts PackageOrTypeName to String. + public String convertPackageOrTypeName(Java8Parser.PackageOrTypeNameContext ctx){ + String ret; + if(ctx.packageOrTypeName() == null){ + ret = ctx.Identifier().toString(); + } + else{ + ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." + ctx.Identifier().toString(); + } + return ret; + } + + public void setImports(Java8Parser.CompilationUnitContext ctx) throws InvalidClassNameException { + List newImports = new ArrayList(); + for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ + if(importDeclCtx.singleTypeImportDeclaration() != null){ + newImports.add(convertSingleTypeImportDeclaration(importDeclCtx.singleTypeImportDeclaration())); + } + else if(importDeclCtx.typeImportOnDemandDeclaration() != null){ + newImports.add(convertTypeImportOnDemandDeclaration(importDeclCtx.typeImportOnDemandDeclaration())); + } + else if(importDeclCtx.singleStaticImportDeclaration() != null){ + newImports.add(convertSingleStaticImportDeclaration(importDeclCtx.singleStaticImportDeclaration())); + } + else{ + newImports.add(convertStaticImportOnDemandDeclaration(importDeclCtx.staticImportOnDemandDeclaration())); + } + } + this.imports.addAll(newImports); + } + + private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws InvalidClassNameException{ + String typeName = convertTypeName(ctx.typeName()); + String packageName = getPackageFromClass(typeName); + List classes = PackageCrawler.getClassNames(packageName); + reg.add(typeName); + JavaClassName ret = reg.getName(typeName); + if(classes.contains(ret)){ + return ret; + } + else{ + throw new InvalidClassNameException(); + } + } + + private JavaClassName convertTypeImportOnDemandDeclaration(Java8Parser.TypeImportOnDemandDeclarationContext ctx){ + return null; + } + + private JavaClassName convertSingleStaticImportDeclaration(Java8Parser.SingleStaticImportDeclarationContext ctx){ + return null; + } + + private JavaClassName convertStaticImportOnDemandDeclaration(Java8Parser.StaticImportOnDemandDeclarationContext ctx){ + return null; + } + + private String getPackageFromClass(String cls){ + String ret = ""; + String[] parts = cls.split("\\."); + for(int i = 0; i < parts.length - 1; i++){ + ret = ret + "." + parts[i]; + } + ret = ret.substring(1); + return ret; + } + + public SourceFile convert(Java8Parser.CompilationUnitContext ctx){ + List classes = new ArrayList<>(); + this.getNames(ctx); + for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ + ClassOrInterface newClass; + if(typeDecl.classDeclaration() != null){ + newClass = convertClass(typeDecl.classDeclaration()); + } + else{ + newClass = convertInterface(typeDecl.interfaceDeclaration()); + } + classes.add(newClass); + } + return new SourceFile(this.pkgName, classes, this.imports); + } + + private ClassOrInterface convertClass(Java8Parser.ClassDeclarationContext ctx) { + ClassOrInterface newClass; + if(ctx.normalClassDeclaration() != null){ + newClass = convertNormal(ctx.normalClassDeclaration()); + } + else{ + newClass = convertEnum(ctx.enumDeclaration()); + } + return newClass; + } + + private ClassOrInterface convertNormal(Java8Parser.NormalClassDeclarationContext ctx){ + int modifiers = 0; + if(ctx.classModifier() != null){ + for(Java8Parser.ClassModifierContext mod : ctx.classModifier()){ + int newModifier = convert(mod); + modifiers += newModifier; + } + } + JavaClassName name = convert(ctx.Identifier()); + Block class_block = null; + List fielddecl = convertFields(ctx.classBody()); + List methods = convertMethods(ctx.classBody()); + GenericDeclarationList genericClassParameters = null; + Token offset = ctx.getStart(); + RefTypeOrTPH superClass = null; + Boolean isInterface = false; + List implementedInterfaces = null; + return new ClassOrInterface(modifiers, name, fielddecl, methods, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); + } + + private List convertMethods(Java8Parser.ClassBodyContext classBodyContext) { + List ret = new ArrayList<>(); + for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ + if(classMember.classMemberDeclaration() != null){ + Java8Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); + if(classMemberDeclarationContext.fieldDeclaration() != null){ + //Do nothing! + }else if(classMemberDeclarationContext.methodDeclaration()!= null){ + StatementGenerator stmtGen = new StatementGenerator(reg); + ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration())); + } + } + } + return ret; + } + + private List convertFields(Java8Parser.ClassBodyContext classBodyContext) { + List ret = new ArrayList<>(); + for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ + if(classMember.classMemberDeclaration() != null){ + Java8Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); + if(classMemberDeclarationContext.fieldDeclaration() != null){ + ret.addAll(convert(classMember.classMemberDeclaration().fieldDeclaration())); + }else if(classMemberDeclarationContext.methodDeclaration()!= null){ + //Do nothing! + } + } + } + return ret; + } + + public static int convert(List methodModifierContexts) { + int ret = 0; + for(Java8Parser.MethodModifierContext mod : methodModifierContexts){ + if(mod.annotation() == null)convertModifier(mod.getText()); + } + return ret; + } + + private List convert(Java8Parser.FieldDeclarationContext fieldDeclarationContext) { + List ret = new ArrayList<>(); + int modifiers = 0; + for(Java8Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()){ + modifiers+=(convert(fieldModifierContext)); + } + RefTypeOrTPH fieldType = TypeGenerator.convert(fieldDeclarationContext.unannType(), reg); + for(Java8Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){ + String fieldName = convert(varCtx.variableDeclaratorId()); + if(varCtx.variableInitializer() != null){ + initializeField(fieldDeclarationContext); + } + else{ + ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart())); + } + } + return ret; + } + + public static String convert(Java8Parser.VariableDeclaratorIdContext variableDeclaratorIdContext) { + return variableDeclaratorIdContext.getText(); + } + + // Initialize a field by creating implicit constructor. + private void initializeField(Java8Parser.FieldDeclarationContext ctx){ + //TODO + } + + public static int convertModifier(String modifier){ + HashMap modifiers = new HashMap<>(); + modifiers.put(Modifier.toString(Modifier.PUBLIC), Modifier.PUBLIC); + modifiers.put(Modifier.toString(Modifier.PRIVATE), Modifier.PRIVATE); + modifiers.put(Modifier.toString(Modifier.PROTECTED), Modifier.PROTECTED); + modifiers.put(Modifier.toString(Modifier.ABSTRACT), Modifier.ABSTRACT); + modifiers.put(Modifier.toString(Modifier.STATIC), Modifier.STATIC); + modifiers.put(Modifier.toString(Modifier.STRICT), Modifier.STRICT); + modifiers.put(Modifier.toString(Modifier.FINAL), Modifier.FINAL); + modifiers.put(Modifier.toString(Modifier.TRANSIENT), Modifier.TRANSIENT); + modifiers.put(Modifier.toString(Modifier.VOLATILE), Modifier.VOLATILE); + modifiers.put(Modifier.toString(Modifier.SYNCHRONIZED), Modifier.SYNCHRONIZED); + modifiers.put(Modifier.toString(Modifier.NATIVE), Modifier.NATIVE); + modifiers.put(Modifier.toString(Modifier.INTERFACE), Modifier.INTERFACE); + int ret = 0; + for(String m : modifiers.keySet()){ + if(modifier.startsWith(m))ret+=modifiers.get(m); + } + return ret; + } + + private int convert(Java8Parser.ClassModifierContext ctx){ + if(ctx.annotation() != null)return 0; + return convertModifier(ctx.getText()); + } + + private int convert(Java8Parser.FieldModifierContext ctx){ + if(ctx.annotation() != null)return 0; + return convertModifier(ctx.getText()); + } + + + /** + Converts a TerminalNode to JavaClassName. If pkgName is set, it will be included like expected. + **/ + private JavaClassName convert(TerminalNode t){ + String name = ""; + if(this.pkgName != null){ + name = this.pkgName + "." + t.toString(); + } + else{ + name = t.toString(); + } + return this.reg.getName(name); + } + + private ClassOrInterface convertEnum(Java8Parser.EnumDeclarationContext ctx){ + return null; + } + + private ClassOrInterface convertInterface(Java8Parser.InterfaceDeclarationContext ctx){ + return null; + } + +} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java new file mode 100644 index 00000000..d232700a --- /dev/null +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -0,0 +1,37 @@ +package de.dhbwstuttgart.parser.SyntaxTreeGenerator; + +import de.dhbwstuttgart.parser.antlr.Java8Parser; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH; +import de.dhbwstuttgart.typecheck.JavaClassName; +import de.dhbwstuttgart.typecheck.JavaClassRegistry; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +public class TypeGenerator { + + public static RefTypeOrTPH convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg) { + String name = unannClassOrInterfaceTypeContext.getText(); + + return new RefType(reg.getName(name), unannClassOrInterfaceTypeContext.getStart()); + } + + public static RefTypeOrTPH convert(Java8Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg) { + if(unannTypeContext.unannPrimitiveType()!=null){ + throw new NotImplementedException(); + }else + if(unannTypeContext.unannReferenceType().unannArrayType()!=null){ + throw new NotImplementedException(); + }else + if(unannTypeContext.unannReferenceType().unannTypeVariable()!=null){ + JavaClassName name = reg.getName(unannTypeContext.unannReferenceType().unannTypeVariable().Identifier().toString()); + return new RefType(name, unannTypeContext.getStart()); + } + return TypeGenerator.convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType(), reg); + } + + public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext) { + throw new NotImplementedException(); + } + +} diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 1ab9c142..7f8181c9 100755 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree; import de.dhbwstuttgart.core.IItemWithOffset; +import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.typecheck.JavaClassName; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH; import de.dhbwstuttgart.typeinference.ConstraintSet; @@ -72,4 +73,8 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith } return ret; } + + public RefType getType() { + return new RefType(this.getClassName(), this.getOffset()); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index aed6bc5f..cb5bc7ab 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -22,5 +22,9 @@ public class Field extends GTVDeclarationContext implements Generic { public String getName(){ return this.name; } + + public RefTypeOrTPH getType() { + return type; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 02aef7f5..6dae2e1d 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -23,12 +23,13 @@ import de.dhbwstuttgart.syntaxtree.statement.Block; public class Method extends Field implements IItemWithOffset { private Block block; - public ParameterList parameterlist = new ParameterList(null, new NullToken()); + private ParameterList parameterlist = new ParameterList(null, new NullToken()); private ExceptionList exceptionlist; private String name; private List types_in_parameterlist = new ArrayList<>(); private int modifiers; + private ParameterList parameterList; public Method(String name, RefTypeOrTPH returnType, int modifiers, ParameterList parameterList, Block block, GenericDeclarationList gtvDeclarations, Token offset) { @@ -44,4 +45,8 @@ public class Method extends Field implements IItemWithOffset ret.addAll(block.getConstraints(info)); return ret; } + + public ParameterList getParameterList() { + return parameterList; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/ParameterList.java b/src/de/dhbwstuttgart/syntaxtree/ParameterList.java index d4e71dec..b7d6860f 100755 --- a/src/de/dhbwstuttgart/syntaxtree/ParameterList.java +++ b/src/de/dhbwstuttgart/syntaxtree/ParameterList.java @@ -10,7 +10,7 @@ import java.util.List; public class ParameterList extends SyntaxTreeNode implements Iterable { - public List formalparameter; + private List formalparameter; public ParameterList(List params, Token offset){ super(offset); @@ -25,7 +25,7 @@ public class ParameterList extends SyntaxTreeNode implements Iterable sc_get_Formalparalist() + public List getFormalparalist() { return formalparameter; } diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index 792cb2c2..3938e345 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -11,7 +11,7 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; public class SourceFile extends SyntaxTreeNode{ private String pkgName; - public List KlassenVektor = new ArrayList<>(); + private List KlassenVektor = new ArrayList<>(); private List imports; /** @@ -44,17 +44,30 @@ public class SourceFile extends SyntaxTreeNode{ public String getPkgName(){ return this.pkgName; } - + // Get imports (to test implementation) public List getImports(){ return this.imports; } - public ConstraintSet getConstraints(TypeInferenceInformation info){ + public ConstraintSet getConstraints(TypeInferenceInformation info) { ConstraintSet ret = new ConstraintSet(); - for(ClassOrInterface cl : this.KlassenVektor){ + for (ClassOrInterface cl : this.KlassenVektor) { ret.addAll(cl.getConstraints(info)); } return ret; } + + public TypeInferenceInformation getTypeInferenceInformation(List sourceFiles){ + Set classes = new HashSet<>(); + for(SourceFile sourceFile : sourceFiles){ + classes.addAll(sourceFile.KlassenVektor); + } + + return new TypeInferenceInformation(classes); + } + + public List getClasses() { + return KlassenVektor; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/SyntaxTreeNode.java b/src/de/dhbwstuttgart/syntaxtree/SyntaxTreeNode.java index 9eb6d5e1..58155562 100644 --- a/src/de/dhbwstuttgart/syntaxtree/SyntaxTreeNode.java +++ b/src/de/dhbwstuttgart/syntaxtree/SyntaxTreeNode.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import de.dhbwstuttgart.core.IItemWithOffset; +import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.misc.Pair; @@ -17,4 +18,5 @@ public abstract class SyntaxTreeNode implements IItemWithOffset{ public Token getOffset(){ return offset; } + } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index de1cf989..aed70370 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -10,9 +10,9 @@ import org.antlr.v4.runtime.Token; public class Assign extends Statement { private final Expression rightSide; - private final LocalOrFieldVar lefSide; + private final LocalVar lefSide; - public Assign(LocalOrFieldVar leftHandSide, Expression value, Token offset) { + public Assign(LocalVar leftHandSide, Expression value, Token offset) { super(leftHandSide.getType(), offset); this.rightSide = value; this.lefSide = leftHandSide; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java new file mode 100644 index 00000000..cfb6fc61 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java @@ -0,0 +1,22 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.ConstraintSet; +import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; +import org.antlr.v4.runtime.Token; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +public class FieldVar extends Expression { + public FieldVar(Expression receiver, String fieldVarName, RefTypeOrTPH type, Token offset) { + super(type, offset); + } + + @Override + public ConstraintSet getConstraints(TypeInferenceInformation info) { + //for(FieldAssumption fieldAssumption : info.getFields(fieldVarName)){ + + //} + throw new NotImplementedException(); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVarOrClassname.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVarOrClassname.java deleted file mode 100755 index 131722b3..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVarOrClassname.java +++ /dev/null @@ -1,31 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - -import de.dhbwstuttgart.typeinference.ConstraintSet; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; -import org.antlr.v4.runtime.Token; - -import sun.reflect.generics.reflectiveObjects.NotImplementedException; - - -/* -* Ausdrücke der Form [Identifier....]Identifier werden zu dieser Klasse geparst. -* Typinferenzalgorithmus muss herausfinden um was es sich handelt: -* Es können folgende Dinge sein -* package.name.Classname - Classname -* LocalOrFieldVarOrClassname.FieldVar[.Fieldvar...] - FieldVar -* Identifier - Lokale Variable oder FieldVar -*/ -public class LocalOrFieldVarOrClassname extends LocalOrFieldVar -{ - public LocalOrFieldVarOrClassname(String n, Token offset) { - super(n, offset); - } - public LocalOrFieldVarOrClassname(Expression e1, String access){ - super(e1, access); - } - - @Override - public ConstraintSet getConstraints(TypeInferenceInformation info) { - throw new NotImplementedException(); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java similarity index 71% rename from src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java rename to src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java index 25e8eafd..55072f00 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java @@ -4,18 +4,19 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.ConstraintSet; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import org.antlr.v4.runtime.Token; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; -public class LocalOrFieldVar extends Statement{ +public class LocalVar extends Statement{ protected final String expression; - public LocalOrFieldVar(String n, Token offset) + public LocalVar(String n, Token offset) { super(TypePlaceholder.fresh(offset),offset); this.expression = n; } - public LocalOrFieldVar(Expression e1, String access) + public LocalVar(Expression e1, String access) { super(TypePlaceholder.fresh(e1.getOffset()),e1.getOffset()); this.expression = access; @@ -23,6 +24,6 @@ public class LocalOrFieldVar extends Statement{ @Override public ConstraintSet getConstraints(TypeInferenceInformation info) { - return null; + return new ConstraintSet(); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java index a7d92744..f2537cfc 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java @@ -22,6 +22,7 @@ public class MethodCall extends Statement super(TypePlaceholder.fresh(offset),offset); this.arglist = argumentList; this.name = methodName; + this.receiver = receiver; } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java b/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java new file mode 100644 index 00000000..2df7c44f --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java @@ -0,0 +1,18 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.typecheck.JavaClassName; +import de.dhbwstuttgart.typeinference.ConstraintSet; +import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; +import org.antlr.v4.runtime.Token; + +public class StaticClassName extends Expression { + public StaticClassName(JavaClassName className, Token offset) { + super(new RefType(className, offset), offset); + } + + @Override + public ConstraintSet getConstraints(TypeInferenceInformation info) { + return new ConstraintSet(); + } +} diff --git a/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java b/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java index e8914ae0..afb8d4d9 100644 --- a/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java +++ b/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java @@ -30,4 +30,8 @@ public class JavaClassRegistry { public String toString(){ return existingClasses.toString(); } + + public boolean contains(String whole) { + return existingClasses.contains(whole); + } } diff --git a/src/de/dhbwstuttgart/typeinference/UndConstraint.java b/src/de/dhbwstuttgart/typeinference/UndConstraint.java index 304e1cda..51b3819f 100755 --- a/src/de/dhbwstuttgart/typeinference/UndConstraint.java +++ b/src/de/dhbwstuttgart/typeinference/UndConstraint.java @@ -25,6 +25,7 @@ public class UndConstraint extends UndMenge { public Set getConstraintPairs() { Set> ret = this.cartesianProduct(); + if(ret.size() == 0)return new TreeSet<>(); if(ret.size() != 1){ //UndConstraints enthalten nur SingleConstraints, wodurch das Karthesische Produkt nur aus einem Element bestehen kann. throw new DebugException("Fehler in ConstraintPairs-Bildung"); diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java index e1eff887..82a5c268 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java @@ -28,7 +28,7 @@ public class MethodAssumption { } public List getArgTypes() { - return params.formalparameter.stream(). + return params.getFormalparalist().stream(). map(formalParameter -> formalParameter.getType()).collect(Collectors.toList()); } } diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java index 44110f7e..6440a303 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceInformation.java @@ -1,11 +1,12 @@ package de.dhbwstuttgart.typeinference.assumptions; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Method; -import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; -import sun.reflect.generics.reflectiveObjects.NotImplementedException; +import java.util.ArrayList; import java.util.List; +import java.util.Set; /* Anmerkung: @@ -19,9 +20,10 @@ Die ganze Logik steckt in dieser Klasse. */ public class TypeInferenceInformation { private Method methodContext; + private Set classes; - public void setSourceFileContext(SourceFile sf){ - + public TypeInferenceInformation(Set availableClasses){ + classes = availableClasses; } public void setMethodContext(Method methodContext) { @@ -29,7 +31,15 @@ public class TypeInferenceInformation { } public List getMethods(String name, ArgumentList arglist) { - - throw new NotImplementedException(); + List ret = new ArrayList<>(); + for(ClassOrInterface cl : classes){ + for(Method m : cl.getMethods()){ + if(m.getName().equals(name) && + m.getParameterList().getFormalparalist().size() == arglist.getArguments().size()){ + ret.add(new MethodAssumption(cl.getType(), m.getType(), m.getParameterList())); + } + } + } + return ret; } } diff --git a/test/javFiles/MethodsEasy.jav b/test/javFiles/MethodsEasy.jav new file mode 100644 index 00000000..24cbd52d --- /dev/null +++ b/test/javFiles/MethodsEasy.jav @@ -0,0 +1,7 @@ + +class Methods { + mt4(a,b,c) { return a.add(b).sub(c) ; } + + mt3(a) {return a.add(); } +} + diff --git a/test/parser/FeatherWeightJavaTest.java b/test/parser/FeatherWeightJavaTest.java index 9900bb80..84113e0c 100644 --- a/test/parser/FeatherWeightJavaTest.java +++ b/test/parser/FeatherWeightJavaTest.java @@ -23,7 +23,7 @@ public class FeatherWeightJavaTest { String pkgName = f.getPkgName(); System.out.println("package: " + pkgName); System.out.println("classes:"); - for(ClassOrInterface c : f.KlassenVektor){ + for(ClassOrInterface c : f.getClasses()){ int mod = c.getModifiers(); System.out.println(Modifier.toString(mod)); System.out.println(c.getClassName().toString()); @@ -31,7 +31,7 @@ public class FeatherWeightJavaTest { for(Field field : c.getFieldDecl()){ System.out.println(field.getName()); if(field instanceof Method){ - System.out.println(((Method) field).parameterlist.formalparameter.toString()); + System.out.println(((Method) field).getParameterList().getFormalparalist().toString()); } } System.out.println("}"); diff --git a/test/parser/FieldTest.java b/test/parser/FieldTest.java index f5a1a77f..067ff23e 100644 --- a/test/parser/FieldTest.java +++ b/test/parser/FieldTest.java @@ -22,7 +22,7 @@ public class FieldTest { String pkgName = f.getPkgName(); System.out.println("package: " + pkgName); System.out.println("classes:"); - for(ClassOrInterface c : f.KlassenVektor){ + for(ClassOrInterface c : f.getClasses()){ int mod = c.getModifiers(); System.out.println(Modifier.toString(mod)); System.out.println(c.getClassName().toString()); diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index aa528eac..996e7588 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -16,7 +16,7 @@ public class JavaTXCompilerTest { @Test public void test() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler(); - compiler.parse(new File(rootDirectory+"Methods.jav")); + compiler.parse(new File(rootDirectory+"MethodsEasy.jav")); compiler.typeInference(); } } \ No newline at end of file