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

Merge dass Interfaces geparst werden können
This commit is contained in:
sebastian 2017-05-05 16:00:51 +02:00
commit bad80f3fe1
42 changed files with 3090 additions and 4790 deletions

View File

@ -5,6 +5,8 @@ import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.unify.TypeUnify;
@ -21,7 +23,11 @@ public class JavaTXCompiler {
private List<SourceFile> sourceFiles = new ArrayList<>();
public void typeInference(){
public List<TypeInsertPoint> getTypeInserts(File forSourceFile){
return null;
}
public ResultSet typeInference(){
ConstraintSet cons = new ConstraintSet();
List<ClassOrInterface> allClasses = new ArrayList<>();
for(SourceFile sf : sourceFiles){
@ -34,6 +40,7 @@ public class JavaTXCompiler {
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
TypeUnify unify = new TypeUnify();
Set<Set<UnifyPair>> results = new HashSet<>();
for(List<Constraint<UnifyPair>> xCons : unifyCons.cartesianProduct()){
Set<UnifyPair> xConsSet = new HashSet<>();
for(Constraint<UnifyPair> constraint : xCons){
@ -43,8 +50,9 @@ public class JavaTXCompiler {
System.out.println(xConsSet);
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
System.out.println(result);
results.addAll(result);
}
return new ResultSet(results);
}
public void parse(File sourceFile) throws IOException, ClassNotFoundException {

View File

@ -0,0 +1,4 @@
package de.dhbwstuttgart.parser;
public class ClassNotFoundException extends Exception{
}

View File

@ -1,4 +0,0 @@
package de.dhbwstuttgart.parser;
public class InvalidClassNameException extends Exception{
}

View File

@ -17,22 +17,11 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class RunParser{
public class RunTXParser{
public static void main(String[] args){
try{
File file = new File(args[0]);
Scanner sc = new Scanner(file);
String inputString = sc.nextLine();
while(sc.hasNextLine()) inputString = inputString + sc.nextLine() + "\n";
InputStream stream = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8));
ANTLRInputStream input = new ANTLRInputStream(stream);
Java8Lexer lexer = new Java8Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java8Parser parser = new Java8Parser(tokens);
Java8Parser.CompilationUnitContext tree = parser.compilationUnit();
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(new JavaClassRegistry(new ArrayList<>()));
generator.setImports(tree);
SourceFile f = generator.convert((Java8Parser.CompilationUnitContext) tree);
JavaTXParser parser = new JavaTXParser();
SourceFile f = parser.parse(new File(args[0]));
String pkgName = f.getPkgName();
System.out.println("package: " + pkgName);
System.out.println("Imports:");
@ -54,12 +43,12 @@ public class RunParser{
catch(java.util.NoSuchElementException e){
System.out.println("Error: Source seems to be empty.");
}
catch(ClassNotFoundException e){
System.out.println("Class not found.");
}
catch(FileNotFoundException e){
System.out.println("File not found.");
}
catch(InvalidClassNameException e){
e.printStackTrace();
}
catch(IOException e){
System.out.println("An exception occured which is on our TODO list.");
e.printStackTrace();

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*;
@ -14,7 +15,6 @@ 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;
@ -33,7 +33,6 @@ public class StatementGenerator {
this.generics = generics;
}
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass) {
Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader();
String name = header.methodDeclarator().Identifier().getText();
@ -60,7 +59,11 @@ public class StatementGenerator {
}else{
block = this.convert(methodDeclarationContext.methodBody().block());
}
return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, methodDeclarationContext.getStart());
if(parentClass.equals(new JavaClassName(name))){
return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, methodDeclarationContext.getStart());
}else{
return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, methodDeclarationContext.getStart());
}
}
private ParameterList convert(Java8Parser.FormalParameterListContext formalParameterListContext) {
@ -92,7 +95,7 @@ public class StatementGenerator {
}
/*
* StatementGeneration:
* StatementGeneration:
*/
private Statement convert(Java8Parser.StatementContext stmt) {
@ -234,7 +237,7 @@ public class StatementGenerator {
}else throw new NotImplementedException();
ArgumentList argumentList = convert(methodInvocationContext.argumentList());
MethodCall ret = new MethodCall(new Receiver(receiver), name, argumentList, methodInvocationContext.getStart());
MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()),new Receiver(receiver), name, argumentList, methodInvocationContext.getStart());
return ret;
}
@ -264,8 +267,9 @@ public class StatementGenerator {
//Check for localVar:
if(localVars.contains(expression)){
return new LocalVar(expression, offset);
}else {
throw new NotImplementedException();
}else{
//throw new NotImplementedException();
//Dann Muss es ein Feld sein!
}
}
return generateFieldVarOrClassname(expression, offset);
@ -695,7 +699,7 @@ public class StatementGenerator {
}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());
return new MethodCall(TypePlaceholder.fresh(e.getStart()), new Receiver(expr), methodName, convert(ctxt.argumentList()), e.getStart());
}
}
@ -712,13 +716,33 @@ public class StatementGenerator {
return convert(expression.methodInvocation_lfno_primary());
}else if(expression.classInstanceCreationExpression_lfno_primary() != null) {
return convert(expression.classInstanceCreationExpression_lfno_primary());
}else if(expression.getText().equals("this")) {
return new This(expression.getStart());
}else if(expression.fieldAccess_lfno_primary() != null){
return convert(expression.fieldAccess_lfno_primary());
}else if(expression.methodReference_lfno_primary() != null){
throw new NotImplementedException();
}else if(expression.typeName() != null){
throw new NotImplementedException();
}else if(expression.unannPrimitiveType() != null){
throw new NotImplementedException();
}else if(expression.arrayAccess_lfno_primary() != null){
throw new NotImplementedException();
}else if(expression.fieldAccess_lfno_primary() != null){
throw new NotImplementedException();
}else{
throw new NotImplementedException();
}
}
private Expression convert(Java8Parser.FieldAccess_lfno_primaryContext fieldAccess_lfno_primaryContext) {
throw new NotImplementedException();
}
private Expression convert(Java8Parser.ClassInstanceCreationExpression_lfno_primaryContext newExpression) {
if(newExpression.expressionName()!= null)throw new NotImplementedException();
if(newExpression.typeArgumentsOrDiamond()!= null)throw new NotImplementedException();
if(newExpression.typeArguments()!= null)throw new NotImplementedException();
TerminalNode identifier = newExpression.Identifier(0);
RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(),identifier.getSymbol(),reg,generics);
@ -776,12 +800,41 @@ public class StatementGenerator {
}
ArgumentList argumentList = convert(methodInvocationContext.argumentList());
MethodCall ret = new MethodCall(new Receiver(receiver), name, argumentList, methodInvocationContext.getStart());
MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), new Receiver(receiver), name, argumentList, methodInvocationContext.getStart());
return ret;
}
private static Expression convert(Java8Parser.LambdaExpressionContext expression) {
throw new NotImplementedException();
private Expression convert(Java8Parser.LambdaExpressionContext expression) {
Java8Parser.LambdaParametersContext lambdaParams = expression.lambdaParameters();
ParameterList params;
if(lambdaParams.Identifier() != null){
List<FormalParameter> parameterList = new ArrayList<>();
parameterList.add(new FormalParameter(lambdaParams.Identifier().getText(),
TypePlaceholder.fresh(lambdaParams.getStart()), lambdaParams.getStart()));
params = new ParameterList(parameterList, lambdaParams.getStart());
}else if(lambdaParams.formalParameterList() != null){
params = convert(lambdaParams.formalParameterList());
//}else if( lambdaParams.inferredFormalParameterList != null){
}else {
params = new ParameterList(new ArrayList<>(), expression.getStart());
}
Block block;
if(expression.lambdaBody().expression() != null){
List<Statement> statements = new ArrayList<>();
statements.add(new Return(convert(expression.lambdaBody().expression()),
expression.lambdaBody().expression().getStart()));
block = new Block(statements, expression.lambdaBody().getStart());
}else{
block = convert(expression.lambdaBody().block());
}
List<RefTypeOrTPHOrWildcardOrGeneric> funNParams = new ArrayList<>();
funNParams.add(TypePlaceholder.fresh(expression.getStart()));//ret-Type
params.getFormalparalist().forEach(formalParameter -> //Für jeden Parameter einen TPH anfügen:
funNParams.add(TypePlaceholder.fresh(expression.getStart())));
RefType lambdaType = new RefType(reg.getName("Fun"+params.getFormalparalist().size()),
funNParams, expression.getStart());
return new LambdaExpression(lambdaType, params, block, expression.getStart());
}
}

View File

@ -1,8 +1,7 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.InvalidClassNameException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.parser.PackageCrawler;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*;
@ -109,7 +108,7 @@ public class SyntaxTreeGenerator{
return ret;
}
public void setImports(Java8Parser.CompilationUnitContext ctx) throws InvalidClassNameException {
public void setImports(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException {
List<JavaClassName> newImports = new ArrayList();
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
if(importDeclCtx.singleTypeImportDeclaration() != null){
@ -128,7 +127,7 @@ public class SyntaxTreeGenerator{
this.imports.addAll(newImports);
}
private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws InvalidClassNameException{
private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws ClassNotFoundException{
String typeName = convertTypeName(ctx.typeName());
String packageName = getPackageFromClass(typeName);
List<JavaClassName> classes = PackageCrawler.getClassNames(packageName);
@ -138,7 +137,7 @@ public class SyntaxTreeGenerator{
return ret;
}
else{
throw new InvalidClassNameException();
throw new ClassNotFoundException();
}
}
@ -164,9 +163,10 @@ public class SyntaxTreeGenerator{
return ret;
}
public SourceFile convert(Java8Parser.CompilationUnitContext ctx){
public SourceFile convert(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException{
List<ClassOrInterface> classes = new ArrayList<>();
this.getNames(ctx);
this.setImports(ctx);
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
ClassOrInterface newClass;
if(typeDecl.classDeclaration() != null){
@ -200,11 +200,26 @@ public class SyntaxTreeGenerator{
}
}
JavaClassName name = reg.getName(ctx.Identifier().getText());
Token offset = ctx.getStart();
GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics);
List<Field> fielddecl = convertFields(ctx.classBody());
List<Method> methods = convertMethods(ctx.classBody(), name);
List<Constructor> konstruktoren = new ArrayList<>();
for(int i = 0; i<methods.size();i++){
Method m = methods.get(i);
if(m instanceof Constructor){
methods.remove(i);
konstruktoren.add((Constructor) m);
}
}
if(konstruktoren.size()<1){//Standardkonstruktor anfügen:
konstruktoren.add(
generateStandardConstructor(
ctx.Identifier().getText(),
genericClassParameters, offset)
);
}
Token offset = ctx.getStart();
RefType superClass ;
if(ctx.superclass() != null){
superClass = convert(ctx.superclass());
@ -213,7 +228,16 @@ public class SyntaxTreeGenerator{
}
Boolean isInterface = false;
List<RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = null;
return new ClassOrInterface(modifiers, name, fielddecl, methods, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);
return new ClassOrInterface(modifiers, name, fielddecl, methods, konstruktoren, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);
}
private Constructor generateStandardConstructor(String className, GenericDeclarationList classGenerics, Token offset){
RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset);
int modifiers = 0;
ParameterList params = new ParameterList(new ArrayList<>(), offset);
//TODO: Konstruktor muss Felder initialisieren:
Block block = new Block(new ArrayList<>(), offset);
return new Constructor(className, classType, modifiers, params, block, classGenerics, offset);
}
private RefType convert(Java8Parser.SuperclassContext superclass) {
@ -290,6 +314,7 @@ public class SyntaxTreeGenerator{
// Initialize a field by creating implicit constructor.
private void initializeField(Java8Parser.FieldDeclarationContext ctx){
//TODO
throw new NotImplementedException();
}
public static int convertModifier(String modifier){

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
@ -7,6 +8,7 @@ import de.dhbwstuttgart.syntaxtree.GTVDeclarationContext;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
@ -15,7 +17,6 @@ import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.ArrayList;
import java.util.List;
@ -23,8 +24,23 @@ import java.util.List;
public class TypeGenerator {
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
String name = unannClassOrInterfaceTypeContext.getText();
return convertTypeName(name,unannClassOrInterfaceTypeContext.getStart(), reg, generics);
String name;
if(unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType() != null){
name = unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().Identifier().getText();
}
Java8Parser.TypeArgumentsContext arguments;
if(unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType() != null){
name = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().Identifier().getText();
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
}else{// if(unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType() != null){
name = unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().getText();
arguments = unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
}
if(arguments == null){
return convertTypeName(name,unannClassOrInterfaceTypeContext.getStart(), reg, generics);
}else{
return null;
}
}
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) {
@ -78,6 +94,11 @@ public class TypeGenerator {
}
public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(String name, Token offset, JavaClassRegistry reg, GenericsRegistry generics){
return convertTypeName(name, null, offset, reg, generics);
}
public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(
String name, Java8Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg, GenericsRegistry generics){
if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein:
if(generics.keySet().contains(name)){
return new GenericRefType(new GenericTypeName(generics.get(name),name), offset);
@ -85,7 +106,23 @@ public class TypeGenerator {
throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset);
}
}
return new RefType(reg.getName(name), offset);
if(typeArguments == null){
return new RefType(reg.getName(name), offset);
}else{
return new RefType(reg.getName(name), convert(typeArguments, reg, generics), offset);
}
}
public static List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java8Parser.TypeArgumentsContext typeArguments,
JavaClassRegistry reg, GenericsRegistry generics){
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
for(Java8Parser.TypeArgumentContext arg : typeArguments.typeArgumentList().typeArgument()){
if(arg.wildcard() != null){
throw new NotImplementedException();
}else{
ret.add(convert(arg.referenceType(), reg, generics));
}
}
return ret;
}
}

View File

@ -352,7 +352,7 @@ variableDeclaratorList
;
variableDeclarator
: variableDeclaratorId //('=' variableInitializer)? //auskommentiert, weil variablenDecklaration sonst nicht eindeutig
: variableDeclaratorId ('=' variableInitializer)? //auskommentiert, weil variablenDecklaration sonst nicht eindeutig
;
variableDeclaratorId
@ -1011,17 +1011,17 @@ primaryNoNewArray_lf_primary_lfno_arrayAccess_lf_primary
;
primaryNoNewArray_lfno_primary
: literal
: literal //done
| typeName ('[' ']')* '.' 'class'
| unannPrimitiveType ('[' ']')* '.' 'class'
| 'void' '.' 'class'
| 'this'
| typeName '.' 'this'
| '(' expression ')'
| classInstanceCreationExpression_lfno_primary
| '(' expression ')' //done
| classInstanceCreationExpression_lfno_primary //done
| fieldAccess_lfno_primary
| arrayAccess_lfno_primary
| methodInvocation_lfno_primary
| methodInvocation_lfno_primary //done
| methodReference_lfno_primary
;
@ -1180,7 +1180,7 @@ lambdaExpression
lambdaParameters
: Identifier
| '(' formalParameterList? ')'
| '(' inferredFormalParameterList ')'
//| '(' inferredFormalParameterList ')'
;
inferredFormalParameterList

View File

@ -1,4 +1,4 @@
// Generated from Java8.g4 by ANTLR 4.5.3
// Generated from /home/janulrich/Development/intellijworkspace/JavaCompilerCore/src/de/dhbwstuttgart/parser/antlr/Java8.g4 by ANTLR 4.5.1
package de.dhbwstuttgart.parser.antlr;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
@ -11,7 +11,7 @@ import org.antlr.v4.runtime.misc.*;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class Java8Lexer extends Lexer {
static { RuntimeMetaData.checkVersion("4.5.3", RuntimeMetaData.VERSION); }
static { RuntimeMetaData.checkVersion("4.5.1", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@ import de.dhbwstuttgart.core.IItemWithOffset;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
@ -24,8 +25,9 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
private RefTypeOrTPHOrWildcardOrGeneric superClass;
protected boolean isInterface;
private List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces;
public ClassOrInterface(int modifiers, JavaClassName name, List<Field> fielddecl, List<Method> methods, GenericDeclarationList genericClassParameters,
private List<Constructor> constructors;
public ClassOrInterface(int modifiers, JavaClassName name, List<Field> fielddecl, List<Method> methods, List<Constructor> constructors, GenericDeclarationList genericClassParameters,
RefTypeOrTPHOrWildcardOrGeneric superClass, Boolean isInterface, List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces, Token offset){
super(offset);
this.modifiers = modifiers;
@ -47,6 +49,7 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
this.implementedInterfaces = implementedInterfaces;
}
this.methods = methods;
this.constructors = constructors;
}
// Gets class name
@ -79,14 +82,22 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
}
public RefType getType() {
return new RefType(this.getClassName(), this.getOffset());
return generateTypeOfClass(this.getClassName(), this.getGenerics(), this.getOffset());
}
public static RefType generateTypeOfClass(JavaClassName name, GenericDeclarationList genericsOfClass ,Token offset){
return new RefType(name, offset);
}
public RefTypeOrTPHOrWildcardOrGeneric getSuperClass() {
return superClass;
}
public Iterable<? extends GenericTypeVar> getGenerics() {
public GenericDeclarationList getGenerics() {
return this.genericClassParameters;
}
public List<? extends Method> getConstructors() {
return constructors;
}
}

View File

@ -5,11 +5,8 @@ import java.util.ArrayList;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.core.IItemWithOffset;
@ -23,7 +20,7 @@ import de.dhbwstuttgart.syntaxtree.statement.Block;
* @author janulrich
*
*/
public class Method extends Field implements IItemWithOffset
public class Method extends Field implements IItemWithOffset, TypeScope
{
private Block block;
private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken());
@ -62,4 +59,9 @@ public class Method extends Field implements IItemWithOffset
public Iterable<? extends GenericTypeVar> getGenerics() {
return generics;
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
return this.getType();
}
}

View File

@ -0,0 +1,10 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public interface TypeScope {
Iterable<? extends GenericTypeVar> getGenerics();
RefTypeOrTPHOrWildcardOrGeneric getReturnType();
}

View File

@ -35,8 +35,9 @@ public class ASTFactory {
public ClassOrInterface createClass(java.lang.Class jreClass){
JavaClassName name = names.getName(jreClass.getName());
List<Method> methoden = new ArrayList<>();
List<de.dhbwstuttgart.syntaxtree.Constructor> konstruktoren = new ArrayList<>();
for(java.lang.reflect.Constructor constructor : jreClass.getConstructors()){
methoden.add(createConstructor(constructor, jreClass));
konstruktoren.add(createConstructor(constructor, jreClass));
}
for(java.lang.reflect.Method method : jreClass.getMethods()){
methoden.add(createMethod(method, jreClass));
@ -58,7 +59,8 @@ public class ASTFactory {
GenericDeclarationList genericDeclarationList = createGenerics(jreClass.getTypeParameters(), jreClass, null);
Token offset = new NullToken(); //Braucht keinen Offset, da diese Klasse nicht aus einem Quellcode geparst wurde
return new ClassOrInterface(modifier, name, felder, methoden, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset);
return new ClassOrInterface(modifier, name, felder, methoden, konstruktoren, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset);
}
private de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) {

View File

@ -1,11 +1,11 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.syntaxtree.operator.Operator;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
// neu von Felix

View File

@ -1,10 +1,10 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class CastExpr extends Expression

View File

@ -1,10 +1,9 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class ForStmt extends Statement
{

View File

@ -1,10 +1,10 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class InstVar extends Expression

View File

@ -1,9 +1,9 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class InstanceOf extends BinaryExpr

View File

@ -1,37 +1,54 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.type.FunN;
import de.dhbwstuttgart.syntaxtree.TypeScope;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.ArrayList;
/**
* @author A10023 - Andreas Stadelmeier
* Momentan erweitert LambdaExpression noch Expr und erbt dadurch auch von ExprStatement ist also auch ein Statement
*
* LambdaExpression Aufbau:
* ( ParameterList ) -> { method_body };
* ( ParameterList ) -> { methodBody };
*/
public class LambdaExpression extends Expression{
/**
* Wird bei der TYPE Methode gesetzt. Speichert den errechneten Typ des LambdaAusdrucks
*/
private FunN lambdaType;
private Block method_body;
public class LambdaExpression extends Expression implements TypeScope {
private Block methodBody;
private ParameterList params;
public LambdaExpression(int offset, int variableLength) {
super(null,null);
public LambdaExpression(RefTypeOrTPHOrWildcardOrGeneric type, ParameterList params, Block methodBody,Token offset) {
super(type,offset);
this.methodBody = methodBody;
this.params = params;
}
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
TypeInferenceBlockInformation lambdaScope = new TypeInferenceBlockInformation(info, this);
ConstraintSet ret = methodBody.getConstraints(lambdaScope);
throw new NotImplementedException();
//return ret;
}
@Override
public Iterable<? extends GenericTypeVar> getGenerics() {
//Lambda-Ausdrücke haben keine Generics
return new ArrayList<>();
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
RefType type = (RefType) this.getType();
return type.getParaList().get(0);
}
}

View File

@ -1,11 +1,11 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.List;

View File

@ -1,11 +1,12 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class LocalVarDecl extends Statement
{

View File

@ -1,18 +1,24 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
public class MethodCall extends Statement
@ -21,8 +27,8 @@ public class MethodCall extends Statement
private Receiver receiver;
private ArgumentList arglist;
public MethodCall(Receiver receiver, String methodName, ArgumentList argumentList, Token offset){
super(TypePlaceholder.fresh(offset),offset);
public MethodCall(RefTypeOrTPHOrWildcardOrGeneric retType, Receiver receiver, String methodName, ArgumentList argumentList, Token offset){
super(retType,offset);
this.arglist = argumentList;
this.name = methodName;
this.receiver = receiver;
@ -49,21 +55,11 @@ public class MethodCall extends Statement
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = receiver.getConstraints(info);
ret.addAll(receiver.getConstraints(info));
for(int i = 0;i<arglist.getArguments().size();i++){
ret.addAll(arglist.getArguments().get(i).getConstraints(info));
}
ret.addAll(this.getArgumentListConstraints(info));
//Overloading:
Set<Constraint> methodConstraints = new HashSet<>();
for(MethodAssumption m : info.getMethods(name, arglist)){
Constraint methodConstraint = new Constraint();
methodConstraint.add(ConstraintsFactory.createPair(receiver.getType(), m.getReceiverType(), PairOperator.SMALLERDOT, info));
methodConstraint.add(ConstraintsFactory.createPair(m.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info));
for(int i = 0;i<arglist.getArguments().size();i++){
methodConstraint.add(ConstraintsFactory.createPair(m.getArgTypes().get(i),
arglist.getArguments().get(i).getType(), PairOperator.SMALLERDOT, info));
}
methodConstraints.add(methodConstraint);
for(MethodAssumption m : this.getMethods(name, arglist, info)){
methodConstraints.add(generateConstraint(m, info));
}
if(methodConstraints.size()<1){
throw new TypeinferenceException("Methode "+name+" ist nicht vorhanden!",getOffset());
@ -71,4 +67,57 @@ public class MethodCall extends Statement
ret.addOderConstraint(methodConstraints);
return ret;
}
protected Constraint<Pair> generateConstraint(MethodAssumption forMethod, TypeInferenceBlockInformation info){
Constraint methodConstraint = new Constraint();
methodConstraint.add(ConstraintsFactory.createPair(receiver.getType(), forMethod.getReceiverType(), PairOperator.SMALLERDOT, info));
methodConstraint.add(ConstraintsFactory.createPair(forMethod.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info));
methodConstraint.addAll(generateParameterConstraints(forMethod, info));
return methodConstraint;
}
protected Set<Pair> generateParameterConstraints(MethodAssumption forMethod, TypeInferenceBlockInformation info) {
Set<Pair> ret = new HashSet<>();
for(int i = 0;i<arglist.getArguments().size();i++){
ret.add(ConstraintsFactory.createPair(forMethod.getArgTypes().get(i),
arglist.getArguments().get(i).getType(), PairOperator.SMALLERDOT, info));
}
return ret;
}
public List<MethodAssumption> getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) {
List<MethodAssumption> ret = new ArrayList<>();
for(ClassOrInterface cl : info.getAvailableClasses()){
for(Method m : cl.getMethods()){
if(m.getName().equals(name) &&
m.getParameterList().getFormalparalist().size() == arglist.getArguments().size()){
RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType());
ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info)));
}
}
}
return ret;
}
protected List<RefTypeOrTPHOrWildcardOrGeneric> convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
for(FormalParameter fp : parameterList.getFormalparalist()){
params.add(info.checkGTV(fp.getType()));
}
return params;
}
public ArgumentList getArgumentList() {
return arglist;
}
public ConstraintSet getArgumentListConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = new ConstraintSet();
for(int i = 0;i<arglist.getArguments().size();i++){
ret.addAll(arglist.getArguments().get(i).getConstraints(info));
}
return ret;
}
}

View File

@ -4,7 +4,7 @@ import java.util.List;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
public class NewArray extends Expression

View File

@ -1,25 +1,28 @@
package de.dhbwstuttgart.syntaxtree.statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
public class NewClass extends Statement
public class NewClass extends MethodCall
{
public NewClass(int offset,int variableLength)
{
super(null,null);
}
private ArgumentList arglist;
private boolean isStatement = false;
/**
*
* @param newClass Typ der Instanzierten Klasse
@ -27,19 +30,49 @@ public class NewClass extends Statement
* @param start
*/
public NewClass(RefType newClass, ArgumentList args, Token start) {
super(newClass, start);
this.arglist = args;
super(newClass, new Receiver(new EmptyStmt(start)), "new "+newClass.getName().toString(), args, start);
}
public ArgumentList getArgumentList()
{
return this.arglist;
public List<MethodAssumption> getConstructors(TypeInferenceBlockInformation info, RefType ofType, ArgumentList argList){
List<MethodAssumption> ret = new ArrayList<>();
for(ClassOrInterface cl : info.getAvailableClasses()){
if(cl.getClassName().equals(ofType.getName())){
for(Method m : cl.getConstructors()){
if(m.getParameterList().getFormalparalist().size() == argList.getArguments().size()){
ret.add(new MethodAssumption(ofType, ofType, convertParams(m.getParameterList(), info)));
}
}
}
}
return ret;
}
@Override
protected Constraint<Pair> generateConstraint(MethodAssumption forMethod, TypeInferenceBlockInformation info){
Constraint methodConstraint = new Constraint();
methodConstraint.add(ConstraintsFactory.createPair(forMethod.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info));
methodConstraint.addAll(generateParameterConstraints(forMethod, info));
return methodConstraint;
}
@Override
public List<MethodAssumption> getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) {
return getConstructors(info, (RefType) this.getType(), arglist);
}
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
ConstraintSet ret = new ConstraintSet();
ret.addAll(this.getArgumentListConstraints(info));
//Overloading:
Set<Constraint> methodConstraints = new HashSet<>();
for(MethodAssumption m : this.getConstructors(info, (RefType) this.getType(), this.getArgumentList())){
methodConstraints.add(generateConstraint(m, info));
}
if(methodConstraints.size()<1){
throw new TypeinferenceException("Konstruktor in Klasse "+this.getType().toString()+" ist nicht vorhanden!",getOffset());
}
ret.addOderConstraint(methodConstraints);
return ret;
}
}

View File

@ -3,8 +3,7 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
public class PostIncExpr extends UnaryExpr

View File

@ -2,7 +2,6 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import org.antlr.v4.runtime.Token;
@ -21,7 +20,7 @@ public class Return extends Statement
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = retexpr.getConstraints(info);
ret.addUndConstraint(ConstraintsFactory.createPair(
this.getType(),info.getCurrentMethod().getType(), info));
this.getType(),info.getCurrentTypeScope().getReturnType(), info));
return ret;
}

View File

@ -4,7 +4,7 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
public class Super extends Expression
{

View File

@ -11,7 +11,7 @@ import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.atn.SemanticContext;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
public class This extends Expression
{

View File

@ -8,7 +8,7 @@ public class ThisCall extends MethodCall
{
public ThisCall(Receiver receiver, ArgumentList arglist, int offset)
{
super(null, null, null, null);
super(null, null, null, null, null);
}

View File

@ -9,6 +9,6 @@ public abstract class UnaryExpr extends MethodCall
public UnaryExpr(Token offset)
{
super(null,null,null,null);
super(null,null,null,null,null);
}
}

View File

@ -3,7 +3,7 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
public class UnaryPlus extends Expression
{

View File

@ -4,7 +4,7 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
public class WhileStmt extends Statement
{

View File

@ -0,0 +1,17 @@
package de.dhbwstuttgart.typedeployment;
import org.antlr.v4.runtime.Token;
public class TypeInsertPoint {
Token point;
private String insertString;
public TypeInsertPoint(Token point, String toInsert){
this.point = point;
this.insertString = toInsert;
}
public String insert(String intoSource){
return new StringBuilder(intoSource).insert(point.getStartIndex(), insertString).toString();
}
}

View File

@ -0,0 +1,11 @@
package de.dhbwstuttgart.typeinference;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import java.util.Set;
public class ResultSet {
public ResultSet(Set<Set<UnifyPair>> results){
}
}

View File

@ -1,25 +1,60 @@
package de.dhbwstuttgart.typeinference.assumptions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.TypeScope;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import java.util.ArrayList;
import java.util.Set;
import java.util.Stack;
import java.util.stream.Collectors;
public class TypeInferenceBlockInformation extends TypeInferenceInformation {
private Method methodContext;
private TypeScope methodContext;
private ClassOrInterface currentClass;
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses, ClassOrInterface currentClass, Method methodContext) {
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses,
ClassOrInterface currentClass, TypeScope methodContext) {
super(availableClasses);
this.methodContext = methodContext;
this.currentClass = currentClass;
}
public TypeInferenceBlockInformation(TypeInferenceBlockInformation oldScope, TypeScope newScope) {
this(oldScope.getAvailableClasses(), oldScope.currentClass, oldScope.methodContext);
methodContext = new TypeScopeContainer(methodContext, newScope);
}
public ClassOrInterface getCurrentClass() {
return currentClass;
}
public Method getCurrentMethod() {
public TypeScope getCurrentTypeScope() {
return methodContext;
}
private class TypeScopeContainer implements TypeScope{
ArrayList<TypeScope> scopes = new ArrayList<>();
Stack<RefTypeOrTPHOrWildcardOrGeneric> types = new Stack<>();
public TypeScopeContainer(TypeScope scope1, TypeScope scope2){
scopes.add(scope1);
scopes.add(scope2);
types.push(scope1.getReturnType());
types.push(scope2.getReturnType());
}
public void add(TypeScope scope){
}
@Override
public Iterable<? extends GenericTypeVar> getGenerics() {
return Iterables.concat(scopes.stream().
map(TypeScope::getGenerics).collect(Collectors.toList()).toArray(new Iterable[0]));
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
return types.peek();
}
}
}

View File

@ -2,14 +2,14 @@ package de.dhbwstuttgart.typeinference.assumptions;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import java.util.ArrayList;
import java.util.List;
@ -32,7 +32,7 @@ public class TypeInferenceInformation {
classes = availableClasses;
}
private RefTypeOrTPHOrWildcardOrGeneric checkGTV(RefTypeOrTPHOrWildcardOrGeneric type){
public RefTypeOrTPHOrWildcardOrGeneric checkGTV(RefTypeOrTPHOrWildcardOrGeneric type){
if(type instanceof GenericRefType){
return TypePlaceholder.fresh(new NullToken());
}else{
@ -40,25 +40,6 @@ public class TypeInferenceInformation {
}
}
public List<MethodAssumption> getMethods(String name, ArgumentList arglist) {
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()){
RefTypeOrTPHOrWildcardOrGeneric retType = checkGTV(m.getType());
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
for(FormalParameter fp : m.getParameterList().getFormalparalist()){
params.add(checkGTV(fp.getType()));
}
ret.add(new MethodAssumption(cl.getType(), retType, params));
}
}
}
return ret;
}
public List<FieldAssumption> getFields(String name){
List<FieldAssumption> ret = new ArrayList<>();
for(ClassOrInterface cl : classes){

View File

@ -9,7 +9,6 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ConstraintsFactory {
@ -24,15 +23,7 @@ public class ConstraintsFactory {
private static RefTypeOrTPHOrWildcardOrGeneric checkGeneric(RefTypeOrTPHOrWildcardOrGeneric type, TypeInferenceBlockInformation info){
if(type instanceof GenericRefType){
List<GenericTypeVar> genericsInContext = new ArrayList<>();
for(GenericTypeVar genericTypeVar : info.getCurrentClass().getGenerics()){
genericsInContext.add(genericTypeVar);
}
if(info.getCurrentMethod() != null)for(GenericTypeVar genericTypeVar : info.getCurrentMethod().getGenerics()){
genericsInContext.add(genericTypeVar);
}
for(GenericTypeVar genericTypeVar : genericsInContext){
for(GenericTypeVar genericTypeVar : info.getCurrentTypeScope().getGenerics()){
if(genericTypeVar.getName().equals(((GenericRefType)type).getName())){
return new RefType(((GenericRefType)type).getName(),type.getOffset());
}

View File

@ -4,3 +4,9 @@ class Generics<B> {
return mt1(a, a);
}
}
class Test {
methode(String s){
return new Generics<String>().mt1(s,s);
}
}

12
test/javFiles/Lambda.jav Normal file
View File

@ -0,0 +1,12 @@
class Lambda{
String var;
methode(){
return () -> (f) -> {
f.apply(this,var);
return var;
};
}
}

View File

@ -30,6 +30,10 @@ public class GeneralParserTest{
filenames.add("ImportTest.jav");
filenames.add("CastTest.jav");
filenames.add("StatementsTest.jav");
<<<<<<< HEAD
//filenames.add("Methods.jav");
=======
>>>>>>> f05222fb563282945ba395047f54f6b3059b10c5
filenames.add("ImportTestGeneric.jav");
filenames.add("CastTest.jav");
//filenames.add("BoundedParameter.jav");

View File

@ -17,7 +17,9 @@ public class JavaTXCompilerTest {
public void test() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler();
//compiler.parse(new File(rootDirectory+"Methods.jav"));
compiler.parse(new File(rootDirectory+"Generics.jav"));
//compiler.parse(new File(rootDirectory+"Generics.jav"));
compiler.parse(new File(rootDirectory+"MethodsEasy.jav"));
//compiler.parse(new File(rootDirectory+"Lambda.jav"));
compiler.typeInference();
}
}