Merge branch 'antlr' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into strucTypes_dev2

neuaufsetzen nach jre problem
This commit is contained in:
sebastian 2017-03-18 10:55:36 +01:00
commit fbdd81a35f
32 changed files with 1294 additions and 1104 deletions

View File

@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
<li><a href="../../../../de/dhbwstuttgart/syntaxtree/statement/Expr.html" title="class in de.dhbwstuttgart.syntaxtree.statement">de.dhbwstuttgart.syntaxtree.statement.Expr</a></li>
<li>
<ul class="inheritance">
<li>de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar</li>
<li>de.dhbwstuttgart.syntaxtree.statement.LocalVar</li>
</ul>
</li>
</ul>

View File

@ -3,7 +3,7 @@
<html lang="de">
<head>
<!-- Generated by javadoc (1.8.0_05) on Tue May 12 19:56:24 CEST 2015 -->
<title>Uses of Class de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar</title>
<title>Uses of Class de.dhbwstuttgart.syntaxtree.statement.LocalVar</title>
<meta name="date" content="2015-05-12">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
<script type="text/javascript" src="../../../../../script.js"></script>
@ -70,9 +70,9 @@
</a></div>
<!-- ========= END OF TOP NAVBAR ========= -->
<div class="header">
<h2 title="Uses of Class de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar" class="title">Uses of Class<br>de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar</h2>
<h2 title="Uses of Class de.dhbwstuttgart.syntaxtree.statement.LocalVar" class="title">Uses of Class<br>de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar</h2>
</div>
<div class="classUseContainer">No usage of de.dhbwstuttgart.syntaxtree.statement.LocalOrFieldVar</div>
<div class="classUseContainer">No usage of de.dhbwstuttgart.syntaxtree.statement.LocalVar</div>
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<div class="bottomNav"><a name="navbar.bottom">
<!-- -->

View File

@ -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 {

View File

@ -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;

View File

@ -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());

File diff suppressed because it is too large Load Diff

View File

@ -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<String> 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<FormalParameter> ret = new ArrayList<>();
List<Java8Parser.FormalParameterContext> 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<Statement> 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<de.dhbwstuttgart.syntaxtree.statement.Statement> 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<Expression> 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<LocalVarDecl> 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();
}
}

View File

@ -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<JavaClassName> 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<JavaClassName> 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<JavaClassName> 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<ClassOrInterface> 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<Field> fielddecl = convertFields(ctx.classBody());
List<Method> methods = convertMethods(ctx.classBody());
GenericDeclarationList genericClassParameters = null;
Token offset = ctx.getStart();
RefTypeOrTPH superClass = null;
Boolean isInterface = false;
List<RefTypeOrTPH> implementedInterfaces = null;
return new ClassOrInterface(modifiers, name, fielddecl, methods, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);
}
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext) {
List<Method> 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<Field> convertFields(Java8Parser.ClassBodyContext classBodyContext) {
List<Field> 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<Java8Parser.MethodModifierContext> methodModifierContexts) {
int ret = 0;
for(Java8Parser.MethodModifierContext mod : methodModifierContexts){
if(mod.annotation() == null)convertModifier(mod.getText());
}
return ret;
}
private List<? extends Field> convert(Java8Parser.FieldDeclarationContext fieldDeclarationContext) {
List<Field> 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<String, Integer> 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;
}
}

View File

@ -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();
}
}

View File

@ -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());
}
}

View File

@ -22,5 +22,9 @@ public class Field extends GTVDeclarationContext implements Generic {
public String getName(){
return this.name;
}
public RefTypeOrTPH getType() {
return type;
}
}

View File

@ -23,17 +23,12 @@ 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(new ArrayList<>(), new NullToken());
private ExceptionList exceptionlist;
private String name;
private List<String> types_in_parameterlist = new ArrayList<>();
private int modifiers;
public Method(String name, RefTypeOrTPH returnType, int modifiers, ParameterList parameterList, Block block,
GenericDeclarationList gtvDeclarations, Token offset) {
super(name, returnType, modifiers, offset);
this.name = name;
this.parameterlist = parameterList;
this.block = block;
}
@ -44,4 +39,8 @@ public class Method extends Field implements IItemWithOffset
ret.addAll(block.getConstraints(info));
return ret;
}
public ParameterList getParameterList() {
return parameterlist;
}
}

View File

@ -10,7 +10,7 @@ import java.util.List;
public class ParameterList extends SyntaxTreeNode implements Iterable<FormalParameter>
{
public List<FormalParameter> formalparameter;
private List<FormalParameter> formalparameter;
public ParameterList(List<FormalParameter> params, Token offset){
super(offset);
@ -25,7 +25,7 @@ public class ParameterList extends SyntaxTreeNode implements Iterable<FormalPara
}
public List<FormalParameter> sc_get_Formalparalist()
public List<FormalParameter> getFormalparalist()
{
return formalparameter;
}

View File

@ -11,7 +11,7 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
public class SourceFile extends SyntaxTreeNode{
private String pkgName;
public List<ClassOrInterface> KlassenVektor = new ArrayList<>();
private List<ClassOrInterface> KlassenVektor = new ArrayList<>();
private List<JavaClassName> imports;
/**
@ -44,17 +44,30 @@ public class SourceFile extends SyntaxTreeNode{
public String getPkgName(){
return this.pkgName;
}
// Get imports (to test implementation)
public List<JavaClassName> 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<SourceFile> sourceFiles){
Set<ClassOrInterface> classes = new HashSet<>();
for(SourceFile sourceFile : sourceFiles){
classes.addAll(sourceFile.KlassenVektor);
}
return new TypeInferenceInformation(classes);
}
public List<ClassOrInterface> getClasses() {
return KlassenVektor;
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -22,6 +22,7 @@ public class MethodCall extends Statement
super(TypePlaceholder.fresh(offset),offset);
this.arglist = argumentList;
this.name = methodName;
this.receiver = receiver;
}
@Override
@ -37,6 +38,7 @@ public class MethodCall extends Statement
methodConstraints.addConstraint(m.getArgTypes().get(i),
arglist.getArguments().get(i).getType());
}
overloading.addConstraint(methodConstraints);
}
ret.add(overloading);
return ret;

View File

@ -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();
}
}

View File

@ -30,4 +30,8 @@ public class JavaClassRegistry {
public String toString(){
return existingClasses.toString();
}
public boolean contains(String whole) {
return existingClasses.contains(whole);
}
}

View File

@ -1,16 +1,13 @@
package de.dhbwstuttgart.typeinference;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import java.util.*;
public class OderConstraint extends OderMenge<Pair>{
private Set<UndConstraint> oderConstraintPairs;
public OderConstraint(){
oderConstraintPairs = new TreeSet<>();
oderConstraintPairs = new HashSet<>();
}

View File

@ -1,9 +1,6 @@
package de.dhbwstuttgart.typeinference;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import java.util.*;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH;
@ -16,7 +13,7 @@ import de.dhbwstuttgart.typeinference.unify.Unifikationsalgorithmus;
*/
public class UndConstraint extends UndMenge<Pair> {
Set<EinzelElement<Pair>> set = new TreeSet<>();
Set<EinzelElement<Pair>> set = new HashSet<>();
@Override
public Set<? extends KomplexeMenge<Pair>> getSet() {
@ -25,6 +22,7 @@ public class UndConstraint extends UndMenge<Pair> {
public Set<Pair> getConstraintPairs() {
Set<Set<Pair>> 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");

View File

@ -28,7 +28,7 @@ public class MethodAssumption {
}
public List<RefTypeOrTPH> getArgTypes() {
return params.formalparameter.stream().
return params.getFormalparalist().stream().
map(formalParameter -> formalParameter.getType()).collect(Collectors.toList());
}
}

View File

@ -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<ClassOrInterface> classes;
public void setSourceFileContext(SourceFile sf){
public TypeInferenceInformation(Set<ClassOrInterface> availableClasses){
classes = availableClasses;
}
public void setMethodContext(Method methodContext) {
@ -29,7 +31,15 @@ public class TypeInferenceInformation {
}
public List<MethodAssumption> getMethods(String name, ArgumentList arglist) {
throw new NotImplementedException();
List<MethodAssumption> 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;
}
}

View File

@ -0,0 +1,7 @@
class Methods {
mt4(a,b,c) { return a.mt3(b).mt3(c) ; }
mt3(a) {return a.add(); }
}

View File

@ -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("}");

View File

@ -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());

View File

@ -0,0 +1,4 @@
import java.util.ArrayList;
class ImportTest{
}

View File

@ -0,0 +1,18 @@
package parser;
import de.dhbwstuttgart.parser.RunParser;
import org.junit.Test;
import static org.junit.Assert.*;
public class RunParserTest {
private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/";
@Test
public void testMain() throws Exception {
String[] args = new String[1];
args[0] = rootDirectory+"ImportTest2.jav";
RunParser.main(args);
}
}

View File

@ -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();
}
}