diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index d2bdf496..ce974a10 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -304,6 +304,7 @@ formalParameterList formalParameter : variableModifier* typeType? variableDeclaratorId + | pattern // Pattern matching for Methods ; lastFormalParameter diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index d75a8236..ab3d9538 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -720,7 +720,7 @@ public class Codegen { } else { var name = "lambda$" + lambdaCounter++; var parameters = new ArrayList<>(lambda.captures()); - parameters.addAll(lambda.params().stream().map(param -> param.type() instanceof TargetGenericType ? new MethodParameter(TargetType.Object, param.name()) : param).toList()); + parameters.addAll(lambda.params().stream().map(param -> param.pattern().type() instanceof TargetGenericType ? param.withType(TargetType.Object) : param).toList()); impl = new TargetMethod(0, name, lambda.block(), new TargetMethod.Signature(Set.of(), parameters, lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType()), null); generateMethod(impl); @@ -744,14 +744,16 @@ public class Codegen { var params = new ArrayList(); params.add(new TargetRefType(clazz.qualifiedName())); - params.addAll(lambda.captures().stream().map(MethodParameter::type).toList()); + params.addAll(lambda.captures().stream().map(mp -> mp.pattern().type()).toList()); var descriptor = TargetMethod.getDescriptor(lambda.type(), params.toArray(TargetType[]::new)); mv.visitVarInsn(ALOAD, 0); - for (var capture : lambda.captures()) - mv.visitVarInsn(ALOAD, state.scope.get(capture.name()).index); + for (var capture : lambda.captures()) { + var pattern = (TargetTypePattern) capture.pattern(); + mv.visitVarInsn(ALOAD, state.scope.get(pattern.name()).index); + } - mv.visitInvokeDynamicInsn("apply", descriptor, bootstrap, Type.getType(desugared), handle, Type.getType(TargetMethod.getDescriptor(impl.signature().returnType(), lambda.params().stream().map(MethodParameter::type).toArray(TargetType[]::new)))); + mv.visitInvokeDynamicInsn("apply", descriptor, bootstrap, Type.getType(desugared), handle, Type.getType(TargetMethod.getDescriptor(impl.signature().returnType(), lambda.params().stream().map(mp -> mp.pattern().type()).toArray(TargetType[]::new)))); } private void generate(State state, TargetExpression expr) { @@ -1287,8 +1289,12 @@ public class Codegen { mv.visitCode(); var state = new State(null, mv, 1); - for (var param : constructor.parameters()) - state.createVariable(param.name(), param.type()); + for (var param : constructor.parameters()) { + var pattern = param.pattern(); + if (pattern instanceof TargetTypePattern tp) + state.createVariable(tp.name(), tp.type()); + else throw new NotImplementedException(); + } var stmts = constructor.block().statements(); generate(state, stmts.get(0)); @@ -1315,8 +1321,12 @@ public class Codegen { mv.visitCode(); var state = new State(method.signature().returnType(), mv, method.isStatic() ? 0 : 1); - for (var param : method.signature().parameters()) - state.createVariable(param.name(), param.type()); + for (var param : method.signature().parameters()) { + var pattern = param.pattern(); + if (pattern instanceof TargetTypePattern tp) + state.createVariable(tp.name(), tp.type()); + else throw new NotImplementedException(); + } generate(state, method.block()); if (method.signature().returnType() == null) mv.visitInsn(RETURN); diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index a6b809c1..bfe712a9 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -168,7 +168,7 @@ public class JavaTXCompiler { // werden while (methodIt.hasNext()) { Method m = methodIt.next(); - ParameterList newParaList = new ParameterList(m.getParameterList().getFormalparalist().stream().map(fp -> new FormalParameter(fp.getName(), fp.getType().acceptTV(new TypeExchanger(gtvs)), fp.getOffset())).collect(Collectors.toCollection(ArrayList::new)), m.getParameterList().getOffset()); + ParameterList newParaList = new ParameterList(m.getParameterList().getFormalparalist().stream().map(fp -> fp.withType(fp.getType().acceptTV(new TypeExchanger(gtvs)))).collect(Collectors.toCollection(ArrayList::new)), m.getParameterList().getOffset()); cl.getMethods().add(new Method(m.modifier, m.name, m.getReturnType().acceptTV(new TypeExchanger(gtvs)), newParaList, m.block, // new GenericDeclarationList(newGenericsList, // ((GenericDeclarationList)m.getGenerics()).getOffset()), diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 5fbda898..611e5c17 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -148,7 +148,7 @@ public class StatementGenerator { } public ParameterList convert(Java17Parser.FormalParameterListContext formalParameterListContext) { - List ret = new ArrayList<>(); + List ret = new ArrayList<>(); List fps = new ArrayList<>(); if (Objects.isNull(formalParameterListContext)) return new ParameterList(ret, new NullToken()); // Dann ist die Parameterliste leer @@ -161,6 +161,7 @@ public class StatementGenerator { fps = formalParameterListContext.formalParameter(); for (Java17Parser.FormalParameterContext fp : fps) { + if (fp.pattern() != null) throw new NotImplementedException(); String paramName = SyntaxTreeGenerator.convert(fp.variableDeclaratorId()); RefTypeOrTPHOrWildcardOrGeneric type; if (fp.typeType() != null) { @@ -1033,7 +1034,7 @@ public class StatementGenerator { Java17Parser.LambdaParametersContext lambdaParams = expression.lambdaParameters(); ParameterList params; if (lambdaParams.identifier().size() > 0) { - List parameterList = new ArrayList<>(); + List parameterList = new ArrayList<>(); for (IdentifierContext identifier : lambdaParams.identifier()) { Token offset = identifier.getStart(); parameterList.add(new FormalParameter(identifier.getText(), TypePlaceholder.fresh(offset), offset)); @@ -1043,7 +1044,7 @@ public class StatementGenerator { params = convert(lambdaParams.formalParameterList()); // }else if( lambdaParams.inferredFormalParameterList != null){ } else if (!Objects.isNull(lambdaParams.lambdaLVTIList())) { - List parameterList = new ArrayList<>(); + List parameterList = new ArrayList<>(); for (LambdaLVTIParameterContext param : lambdaParams.lambdaLVTIList().lambdaLVTIParameter()) { Token offset = param.getStart(); parameterList.add(new FormalParameter(param.identifier().getText(), TypePlaceholder.fresh(offset), offset)); @@ -1055,8 +1056,9 @@ public class StatementGenerator { HashMap lambdaLocals = new HashMap<>(); lambdaLocals.putAll(localVars); - for (FormalParameter param : params.getFormalparalist()) { - lambdaLocals.put(param.getName(), param.getType()); + for (Pattern param : params.getFormalparalist()) { + if (!(param instanceof FormalParameter fp)) throw new IllegalArgumentException(); + lambdaLocals.put(fp.getName(), fp.getType()); } StatementGenerator lambdaGenerator = new StatementGenerator(reg, generics, fields, lambdaLocals); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index e4f031a9..b84f89b4 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -12,6 +12,8 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.Record; import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.Token; @@ -61,15 +63,6 @@ import de.dhbwstuttgart.parser.scope.GatherNames; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.Constructor; -import de.dhbwstuttgart.syntaxtree.Field; -import de.dhbwstuttgart.syntaxtree.FormalParameter; -import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; -import de.dhbwstuttgart.syntaxtree.Method; -import de.dhbwstuttgart.syntaxtree.ParameterList; -import de.dhbwstuttgart.syntaxtree.Record; -import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.statement.Assign; import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide; @@ -256,7 +249,7 @@ public class SyntaxTreeGenerator { List constructors = new ArrayList<>(); Boolean isInterface = false; List implementedInterfaces = new ArrayList<>(); - List constructorParameters = new ArrayList<>(); + List constructorParameters = new ArrayList<>(); List constructorStatements = new ArrayList<>(); for (RecordComponentContext component : recordDeclaration.recordHeader().recordComponentList().recordComponent()) { int fieldmodifiers = allmodifiers.get("private") + allmodifiers.get("final"); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 08e9bdf0..d4732079 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -66,7 +66,7 @@ public abstract class AbstractASTWalker implements ASTVisitor { @Override public void visit(ParameterList formalParameters) { - Iterator it = formalParameters.getFormalparalist().iterator(); + Iterator it = formalParameters.getFormalparalist().iterator(); if (it.hasNext()) { while (it.hasNext()) { it.next().accept(this); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/ExpressionPattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/ExpressionPattern.java index 5cdb2995..4a050b13 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/ExpressionPattern.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/ExpressionPattern.java @@ -20,4 +20,9 @@ public class ExpressionPattern extends Pattern { public void accept(ASTVisitor visitor) { visitor.visit(this); } + + @Override + public ExpressionPattern withType(RefTypeOrTPHOrWildcardOrGeneric type) { + return new ExpressionPattern(expression, getOffset()); + } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java b/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java index eac04f2b..841147e3 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java @@ -19,4 +19,9 @@ public class FormalParameter extends Pattern { public void accept(ASTVisitor visitor) { visitor.visit(this); } + + @Override + public FormalParameter withType(RefTypeOrTPHOrWildcardOrGeneric type) { + return new FormalParameter(name, type, getOffset()); + } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/GuardedPattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/GuardedPattern.java index 55a95e9c..c8ca1d76 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/GuardedPattern.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/GuardedPattern.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree; import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; public class GuardedPattern extends Pattern { @@ -26,4 +27,9 @@ public class GuardedPattern extends Pattern { public void accept(ASTVisitor visitor) { visitor.visit(this); } + + @Override + public GuardedPattern withType(RefTypeOrTPHOrWildcardOrGeneric type) { + return new GuardedPattern(condition, nested, getOffset()); + } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/ParameterList.java b/src/main/java/de/dhbwstuttgart/syntaxtree/ParameterList.java index 33a45c5d..cc18290c 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/ParameterList.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/ParameterList.java @@ -5,27 +5,27 @@ import org.antlr.v4.runtime.Token; import java.util.Iterator; import java.util.List; -public class ParameterList extends SyntaxTreeNode implements Iterable { - private List formalparameter; +public class ParameterList extends SyntaxTreeNode implements Iterable { + private List formalparameter; - public ParameterList(List params, Token offset) { + public ParameterList(List params, Token offset) { super(offset); this.formalparameter = params; } - public FormalParameter getParameterAt(int i) { + public Pattern getParameterAt(int i) { if (i >= formalparameter.size()) return null; return formalparameter.get(i); } - public List getFormalparalist() { + public List getFormalparalist() { return formalparameter; } @Override - public Iterator iterator() { + public Iterator iterator() { return formalparameter.iterator(); } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java index 22441b36..85c880ee 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java @@ -14,4 +14,6 @@ public abstract class Pattern extends SyntaxTreeNode { public RefTypeOrTPHOrWildcardOrGeneric getType(){ return type; } + + public abstract Pattern withType(RefTypeOrTPHOrWildcardOrGeneric type); } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index ee2000b3..cb9c0488 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -163,7 +163,7 @@ public class ASTFactory { RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass); Parameter[] jreParams = constructor.getParameters(); Type[] jreGenericParams = constructor.getGenericParameterTypes(); - List params = new ArrayList<>(); + List params = new ArrayList<>(); int i = 0; for (Type jreParam : jreGenericParams) { if (jreParam == null) @@ -197,7 +197,7 @@ public class ASTFactory { returnType = createType(jreRetType); Parameter[] jreParams = jreMethod.getParameters(); Type[] jreGenericParams = jreMethod.getGenericParameterTypes(); - List params = new ArrayList<>(); + List params = new ArrayList<>(); int i = 0; for (Type jreParam : jreGenericParams) { if (jreParam == null) diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index b32c325d..b4024723 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -100,7 +100,7 @@ public class OutputGenerator implements ASTVisitor { @Override public void visit(ParameterList formalParameters) { out.append("("); - Iterator genericIterator = formalParameters.getFormalparalist().iterator(); + Iterator genericIterator = formalParameters.getFormalparalist().iterator(); if (genericIterator.hasNext()) { while (genericIterator.hasNext()) { genericIterator.next().accept(this); @@ -264,9 +264,9 @@ public class OutputGenerator implements ASTVisitor { @Override public void visit(InstanceOf instanceOf) { - instanceOf.lexpr.accept(this); + instanceOf.getExpression().accept(this); out.append(" instanceof "); - instanceOf.rexpr.accept(this); + instanceOf.getPattern().accept(this); } @Override diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index fa63b7a7..b978ad6b 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.target.generate; import de.dhbwstuttgart.bytecode.FunNGenerator; import de.dhbwstuttgart.environment.ByteArrayClassLoader; import de.dhbwstuttgart.environment.IByteArrayClassLoader; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.Record; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; @@ -12,6 +13,7 @@ import de.dhbwstuttgart.target.tree.*; import de.dhbwstuttgart.target.tree.expression.TargetBlock; import de.dhbwstuttgart.target.tree.expression.TargetExpression; import de.dhbwstuttgart.target.tree.expression.TargetSwitch; +import de.dhbwstuttgart.target.tree.expression.TargetTypePattern; import de.dhbwstuttgart.target.tree.type.*; import de.dhbwstuttgart.typeinference.result.*; @@ -150,7 +152,10 @@ public class ASTToTargetAST { } private List convert(ParameterList input, GenerateGenerics generics) { - return input.getFormalparalist().stream().map(param -> new MethodParameter(convert(param.getType(), generics), param.getName())).toList(); + return input.getFormalparalist().stream().map(param -> switch(param) { + case FormalParameter fpm -> new MethodParameter(new TargetTypePattern(convert(param.getType(), generics), fpm.getName())); + default -> throw new NotImplementedException(); + }).toList(); } private boolean hasGeneric(Set generics, GenericRefType type) { diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java index 7a59dbcc..59298379 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -31,7 +31,10 @@ public class StatementToTargetExpression implements ASTVisitor { @Override public void visit(LambdaExpression lambdaExpression) { - var parameters = StreamSupport.stream(lambdaExpression.params.spliterator(), false).map(p -> new MethodParameter(converter.convert(p.getType()), p.getName())).toList(); + var parameters = StreamSupport.stream(lambdaExpression.params.spliterator(), false) + .map(p -> (FormalParameter) p) + .map(p -> new MethodParameter(new TargetTypePattern(converter.convert(p.getType()), p.getName()))) + .toList(); List captures = new ArrayList<>(); lambdaExpression.methodBody.accept(new TracingStatementVisitor() { @@ -59,7 +62,7 @@ public class StatementToTargetExpression implements ASTVisitor { @Override public void visit(LocalVar localVar) { super.visit(localVar); - var capture = new MethodParameter(converter.convert(localVar.getType()), localVar.name); + var capture = new MethodParameter(new TargetTypePattern(converter.convert(localVar.getType()), localVar.name)); if (!hasLocalVar(localVar.name) && !parameters.contains(capture) && !captures.contains(capture)) captures.add(capture); } @@ -106,7 +109,6 @@ public class StatementToTargetExpression implements ASTVisitor { case BIGGEREQUAL -> new TargetBinaryOp.GreaterOrEqual(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case EQUAL -> new TargetBinaryOp.Equal(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case NOTEQUAL -> new TargetBinaryOp.NotEqual(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); - case INSTOF -> new TargetBinaryOp.Instof(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); }; } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/MethodParameter.java b/src/main/java/de/dhbwstuttgart/target/tree/MethodParameter.java index 4ae31828..48ae0916 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/MethodParameter.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/MethodParameter.java @@ -1,6 +1,14 @@ package de.dhbwstuttgart.target.tree; +import de.dhbwstuttgart.target.tree.expression.TargetPattern; +import de.dhbwstuttgart.target.tree.expression.TargetTypePattern; import de.dhbwstuttgart.target.tree.type.TargetType; -public record MethodParameter(TargetType type, String name) { +public record MethodParameter(TargetPattern pattern) { + public MethodParameter(TargetType type, String name) { + this(new TargetTypePattern(type, name)); + } + public MethodParameter withType(TargetType type) { + return new MethodParameter(pattern.withType(type)); + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java index 1da5dc93..55e18109 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java @@ -10,7 +10,7 @@ import java.util.Set; public record TargetConstructor(int access, Set generics, Set txGenerics, List parameters, List txParameters, TargetBlock block, TargetBlock fieldInitializer) { public String getDescriptor() { - return TargetMethod.getDescriptor(null, parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); + return TargetMethod.getDescriptor(null, parameters.stream().map(mp -> mp.pattern().type()).toArray(TargetType[]::new)); } public String getSignature() { diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java index 7439675a..bbbe7290 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java @@ -32,7 +32,7 @@ public record TargetMethod(int access, String name, TargetBlock block, Signature } ret += "("; for (var param : parameters) { - ret += param.type().toDescriptor(); + ret += param.pattern().type().toDescriptor(); } ret += ")"; if (returnType == null) ret += "V"; @@ -41,7 +41,7 @@ public record TargetMethod(int access, String name, TargetBlock block, Signature } public String getDescriptor() { - return getDescriptor(signature.returnType, signature.parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); + return getDescriptor(signature.returnType, signature.parameters.stream().map(mp -> mp.pattern().type()).toArray(TargetType[]::new)); } public String getSignature() { diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetComplexPattern.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetComplexPattern.java index 761ddeed..6ce331f6 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetComplexPattern.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetComplexPattern.java @@ -5,4 +5,8 @@ import de.dhbwstuttgart.target.tree.type.TargetType; import java.util.List; public record TargetComplexPattern(TargetType type, String name, List subPatterns) implements TargetPattern { + @Override + public TargetComplexPattern withType(TargetType type) { + return new TargetComplexPattern(type, name, subPatterns); + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetGuard.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetGuard.java index 11dcf10d..14466bf8 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetGuard.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetGuard.java @@ -1,4 +1,10 @@ package de.dhbwstuttgart.target.tree.expression; +import de.dhbwstuttgart.target.tree.type.TargetType; + public record TargetGuard(TargetPattern inner, TargetExpression expression) implements TargetPattern { + @Override + public TargetGuard withType(TargetType type) { + return new TargetGuard(inner.withType(type), expression); + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetPattern.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetPattern.java index 4f5bd943..58efa37c 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetPattern.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetPattern.java @@ -1,7 +1,11 @@ package de.dhbwstuttgart.target.tree.expression; +import de.dhbwstuttgart.target.tree.type.TargetType; + public sealed interface TargetPattern extends TargetExpression permits TargetComplexPattern, TargetGuard, TargetTypePattern { default String name() { return null; } + + TargetPattern withType(TargetType type); } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetTypePattern.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetTypePattern.java index 856496f9..e8353efa 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetTypePattern.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetTypePattern.java @@ -3,4 +3,8 @@ package de.dhbwstuttgart.target.tree.expression; import de.dhbwstuttgart.target.tree.type.TargetType; public record TargetTypePattern(TargetType type, String name) implements TargetPattern { + @Override + public TargetTypePattern withType(TargetType type) { + return new TargetTypePattern(type, name); + } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 52801b18..b180fc96 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -9,13 +9,7 @@ import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.FormalParameter; -import de.dhbwstuttgart.syntaxtree.GenericTypeVar; -import de.dhbwstuttgart.syntaxtree.Method; -import de.dhbwstuttgart.syntaxtree.ParameterList; -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.TypeScope; +import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; @@ -699,7 +693,7 @@ public class TYPEStmt implements StatementVisitor { * Hier kriegt der Receiver ja den COnstraint TPH REceiver <. List Dann mus bei dem Parameter der COnstraint entstehen: TPH A <. String */ List params = new ArrayList<>(); - for (FormalParameter fp : parameterList.getFormalparalist()) { + for (Pattern fp : parameterList.getFormalparalist()) { params.add(fp.getType()); // info.checkGTV(fp.getType())); //PL 2018-06-22 GTV sollen in Argumenten erhalten bleiben } return params; diff --git a/src/test/java/constraintSimplify/FamilyOfGenerics.java b/src/test/java/constraintSimplify/FamilyOfGenerics.java deleted file mode 100644 index 54681cac..00000000 --- a/src/test/java/constraintSimplify/FamilyOfGenerics.java +++ /dev/null @@ -1,57 +0,0 @@ -package constraintSimplify; - -import de.dhbwstuttgart.parser.NullToken; -import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.statement.Block; -import de.dhbwstuttgart.syntaxtree.type.RefType; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import org.junit.Test; - -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Optional; - -public class FamilyOfGenerics { - - @Test - public void generateBC() throws Exception { - /* - * SourceFile sf = generateAST(); PositionFinder.getPositionOfTPH(sf, null); TPHExtractor tphExtractor = new TPHExtractor(); List results = new ArrayList(); GeneratedGenericsFinder generatedGenericsFinder = new GeneratedGenericsFinder(sf, results); - */ - } - - public static SourceFile generateAST() { - ArrayList classes = new ArrayList<>(); - ArrayList fields = new ArrayList<>(); - ArrayList methods = new ArrayList<>(); - - fields.add(generateField("fld1")); - String[] paramNames = { "a" }; - methods.add(generateMethod("testMethode", paramNames)); - - classes.add(new ClassOrInterface(Modifier.PUBLIC, new JavaClassName("Test"), fields, Optional.empty(), methods, new ArrayList<>(), generateEmptyGenericDeclList(), new RefType(new JavaClassName("java.lang.Object"), new NullToken()), false, new ArrayList<>(), new ArrayList<>(), new NullToken())); - - return new SourceFile("Test.jav", classes, new HashSet<>()); - } - - public static Method generateMethod(String methodName, String[] paramNames) { - ArrayList parameters = new ArrayList<>(); - for (String str : paramNames) { - FormalParameter fp = new FormalParameter(str, TypePlaceholder.fresh(new NullToken()), new NullToken()); - parameters.add(fp); - - } - ParameterList parameterList = new ParameterList(parameters, new NullToken()); - return new Method(Modifier.PUBLIC, methodName, TypePlaceholder.fresh(new NullToken()), parameterList, new Block(new ArrayList<>(), new NullToken()), generateEmptyGenericDeclList(), new NullToken()); - } - - public static GenericDeclarationList generateEmptyGenericDeclList() { - return new GenericDeclarationList(new ArrayList<>(), new NullToken()); - } - - public static Field generateField(String fieldName) { - return new Field(fieldName, TypePlaceholder.fresh(new NullToken()), Modifier.PUBLIC, new NullToken()); - } -}