Refactor
This commit is contained in:
parent
729e88bc6d
commit
5f1f698530
@ -304,6 +304,7 @@ formalParameterList
|
|||||||
|
|
||||||
formalParameter
|
formalParameter
|
||||||
: variableModifier* typeType? variableDeclaratorId
|
: variableModifier* typeType? variableDeclaratorId
|
||||||
|
| pattern // Pattern matching for Methods
|
||||||
;
|
;
|
||||||
|
|
||||||
lastFormalParameter
|
lastFormalParameter
|
||||||
|
@ -720,7 +720,7 @@ public class Codegen {
|
|||||||
} else {
|
} else {
|
||||||
var name = "lambda$" + lambdaCounter++;
|
var name = "lambda$" + lambdaCounter++;
|
||||||
var parameters = new ArrayList<>(lambda.captures());
|
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);
|
impl = new TargetMethod(0, name, lambda.block(), new TargetMethod.Signature(Set.of(), parameters, lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType()), null);
|
||||||
generateMethod(impl);
|
generateMethod(impl);
|
||||||
@ -744,14 +744,16 @@ public class Codegen {
|
|||||||
|
|
||||||
var params = new ArrayList<TargetType>();
|
var params = new ArrayList<TargetType>();
|
||||||
params.add(new TargetRefType(clazz.qualifiedName()));
|
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));
|
var descriptor = TargetMethod.getDescriptor(lambda.type(), params.toArray(TargetType[]::new));
|
||||||
mv.visitVarInsn(ALOAD, 0);
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
for (var capture : lambda.captures())
|
for (var capture : lambda.captures()) {
|
||||||
mv.visitVarInsn(ALOAD, state.scope.get(capture.name()).index);
|
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) {
|
private void generate(State state, TargetExpression expr) {
|
||||||
@ -1287,8 +1289,12 @@ public class Codegen {
|
|||||||
|
|
||||||
mv.visitCode();
|
mv.visitCode();
|
||||||
var state = new State(null, mv, 1);
|
var state = new State(null, mv, 1);
|
||||||
for (var param : constructor.parameters())
|
for (var param : constructor.parameters()) {
|
||||||
state.createVariable(param.name(), param.type());
|
var pattern = param.pattern();
|
||||||
|
if (pattern instanceof TargetTypePattern tp)
|
||||||
|
state.createVariable(tp.name(), tp.type());
|
||||||
|
else throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
var stmts = constructor.block().statements();
|
var stmts = constructor.block().statements();
|
||||||
generate(state, stmts.get(0));
|
generate(state, stmts.get(0));
|
||||||
@ -1315,8 +1321,12 @@ public class Codegen {
|
|||||||
|
|
||||||
mv.visitCode();
|
mv.visitCode();
|
||||||
var state = new State(method.signature().returnType(), mv, method.isStatic() ? 0 : 1);
|
var state = new State(method.signature().returnType(), mv, method.isStatic() ? 0 : 1);
|
||||||
for (var param : method.signature().parameters())
|
for (var param : method.signature().parameters()) {
|
||||||
state.createVariable(param.name(), param.type());
|
var pattern = param.pattern();
|
||||||
|
if (pattern instanceof TargetTypePattern tp)
|
||||||
|
state.createVariable(tp.name(), tp.type());
|
||||||
|
else throw new NotImplementedException();
|
||||||
|
}
|
||||||
generate(state, method.block());
|
generate(state, method.block());
|
||||||
if (method.signature().returnType() == null)
|
if (method.signature().returnType() == null)
|
||||||
mv.visitInsn(RETURN);
|
mv.visitInsn(RETURN);
|
||||||
|
@ -168,7 +168,7 @@ public class JavaTXCompiler {
|
|||||||
// werden
|
// werden
|
||||||
while (methodIt.hasNext()) {
|
while (methodIt.hasNext()) {
|
||||||
Method m = methodIt.next();
|
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,
|
cl.getMethods().add(new Method(m.modifier, m.name, m.getReturnType().acceptTV(new TypeExchanger(gtvs)), newParaList, m.block,
|
||||||
// new GenericDeclarationList(newGenericsList,
|
// new GenericDeclarationList(newGenericsList,
|
||||||
// ((GenericDeclarationList)m.getGenerics()).getOffset()),
|
// ((GenericDeclarationList)m.getGenerics()).getOffset()),
|
||||||
|
@ -148,7 +148,7 @@ public class StatementGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ParameterList convert(Java17Parser.FormalParameterListContext formalParameterListContext) {
|
public ParameterList convert(Java17Parser.FormalParameterListContext formalParameterListContext) {
|
||||||
List<FormalParameter> ret = new ArrayList<>();
|
List<Pattern> ret = new ArrayList<>();
|
||||||
List<Java17Parser.FormalParameterContext> fps = new ArrayList<>();
|
List<Java17Parser.FormalParameterContext> fps = new ArrayList<>();
|
||||||
if (Objects.isNull(formalParameterListContext))
|
if (Objects.isNull(formalParameterListContext))
|
||||||
return new ParameterList(ret, new NullToken()); // Dann ist die Parameterliste leer
|
return new ParameterList(ret, new NullToken()); // Dann ist die Parameterliste leer
|
||||||
@ -161,6 +161,7 @@ public class StatementGenerator {
|
|||||||
|
|
||||||
fps = formalParameterListContext.formalParameter();
|
fps = formalParameterListContext.formalParameter();
|
||||||
for (Java17Parser.FormalParameterContext fp : fps) {
|
for (Java17Parser.FormalParameterContext fp : fps) {
|
||||||
|
if (fp.pattern() != null) throw new NotImplementedException();
|
||||||
String paramName = SyntaxTreeGenerator.convert(fp.variableDeclaratorId());
|
String paramName = SyntaxTreeGenerator.convert(fp.variableDeclaratorId());
|
||||||
RefTypeOrTPHOrWildcardOrGeneric type;
|
RefTypeOrTPHOrWildcardOrGeneric type;
|
||||||
if (fp.typeType() != null) {
|
if (fp.typeType() != null) {
|
||||||
@ -1033,7 +1034,7 @@ public class StatementGenerator {
|
|||||||
Java17Parser.LambdaParametersContext lambdaParams = expression.lambdaParameters();
|
Java17Parser.LambdaParametersContext lambdaParams = expression.lambdaParameters();
|
||||||
ParameterList params;
|
ParameterList params;
|
||||||
if (lambdaParams.identifier().size() > 0) {
|
if (lambdaParams.identifier().size() > 0) {
|
||||||
List<FormalParameter> parameterList = new ArrayList<>();
|
List<Pattern> parameterList = new ArrayList<>();
|
||||||
for (IdentifierContext identifier : lambdaParams.identifier()) {
|
for (IdentifierContext identifier : lambdaParams.identifier()) {
|
||||||
Token offset = identifier.getStart();
|
Token offset = identifier.getStart();
|
||||||
parameterList.add(new FormalParameter(identifier.getText(), TypePlaceholder.fresh(offset), offset));
|
parameterList.add(new FormalParameter(identifier.getText(), TypePlaceholder.fresh(offset), offset));
|
||||||
@ -1043,7 +1044,7 @@ public class StatementGenerator {
|
|||||||
params = convert(lambdaParams.formalParameterList());
|
params = convert(lambdaParams.formalParameterList());
|
||||||
// }else if( lambdaParams.inferredFormalParameterList != null){
|
// }else if( lambdaParams.inferredFormalParameterList != null){
|
||||||
} else if (!Objects.isNull(lambdaParams.lambdaLVTIList())) {
|
} else if (!Objects.isNull(lambdaParams.lambdaLVTIList())) {
|
||||||
List<FormalParameter> parameterList = new ArrayList<>();
|
List<Pattern> parameterList = new ArrayList<>();
|
||||||
for (LambdaLVTIParameterContext param : lambdaParams.lambdaLVTIList().lambdaLVTIParameter()) {
|
for (LambdaLVTIParameterContext param : lambdaParams.lambdaLVTIList().lambdaLVTIParameter()) {
|
||||||
Token offset = param.getStart();
|
Token offset = param.getStart();
|
||||||
parameterList.add(new FormalParameter(param.identifier().getText(), TypePlaceholder.fresh(offset), offset));
|
parameterList.add(new FormalParameter(param.identifier().getText(), TypePlaceholder.fresh(offset), offset));
|
||||||
@ -1055,8 +1056,9 @@ public class StatementGenerator {
|
|||||||
|
|
||||||
HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> lambdaLocals = new HashMap<>();
|
HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> lambdaLocals = new HashMap<>();
|
||||||
lambdaLocals.putAll(localVars);
|
lambdaLocals.putAll(localVars);
|
||||||
for (FormalParameter param : params.getFormalparalist()) {
|
for (Pattern param : params.getFormalparalist()) {
|
||||||
lambdaLocals.put(param.getName(), param.getType());
|
if (!(param instanceof FormalParameter fp)) throw new IllegalArgumentException();
|
||||||
|
lambdaLocals.put(fp.getName(), fp.getType());
|
||||||
}
|
}
|
||||||
StatementGenerator lambdaGenerator = new StatementGenerator(reg, generics, fields, lambdaLocals);
|
StatementGenerator lambdaGenerator = new StatementGenerator(reg, generics, fields, lambdaLocals);
|
||||||
|
|
||||||
|
@ -12,6 +12,8 @@ import java.util.Optional;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
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.CommonToken;
|
||||||
import org.antlr.v4.runtime.Token;
|
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.GenericsRegistry;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
|
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.factory.ASTFactory;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Assign;
|
import de.dhbwstuttgart.syntaxtree.statement.Assign;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide;
|
import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide;
|
||||||
@ -256,7 +249,7 @@ public class SyntaxTreeGenerator {
|
|||||||
List<Constructor> constructors = new ArrayList<>();
|
List<Constructor> constructors = new ArrayList<>();
|
||||||
Boolean isInterface = false;
|
Boolean isInterface = false;
|
||||||
List<RefType> implementedInterfaces = new ArrayList<>();
|
List<RefType> implementedInterfaces = new ArrayList<>();
|
||||||
List<FormalParameter> constructorParameters = new ArrayList<>();
|
List<Pattern> constructorParameters = new ArrayList<>();
|
||||||
List<Statement> constructorStatements = new ArrayList<>();
|
List<Statement> constructorStatements = new ArrayList<>();
|
||||||
for (RecordComponentContext component : recordDeclaration.recordHeader().recordComponentList().recordComponent()) {
|
for (RecordComponentContext component : recordDeclaration.recordHeader().recordComponentList().recordComponent()) {
|
||||||
int fieldmodifiers = allmodifiers.get("private") + allmodifiers.get("final");
|
int fieldmodifiers = allmodifiers.get("private") + allmodifiers.get("final");
|
||||||
|
@ -66,7 +66,7 @@ public abstract class AbstractASTWalker implements ASTVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(ParameterList formalParameters) {
|
public void visit(ParameterList formalParameters) {
|
||||||
Iterator<FormalParameter> it = formalParameters.getFormalparalist().iterator();
|
Iterator<Pattern> it = formalParameters.getFormalparalist().iterator();
|
||||||
if (it.hasNext()) {
|
if (it.hasNext()) {
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
it.next().accept(this);
|
it.next().accept(this);
|
||||||
|
@ -20,4 +20,9 @@ public class ExpressionPattern extends Pattern {
|
|||||||
public void accept(ASTVisitor visitor) {
|
public void accept(ASTVisitor visitor) {
|
||||||
visitor.visit(this);
|
visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ExpressionPattern withType(RefTypeOrTPHOrWildcardOrGeneric type) {
|
||||||
|
return new ExpressionPattern(expression, getOffset());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,4 +19,9 @@ public class FormalParameter extends Pattern {
|
|||||||
public void accept(ASTVisitor visitor) {
|
public void accept(ASTVisitor visitor) {
|
||||||
visitor.visit(this);
|
visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FormalParameter withType(RefTypeOrTPHOrWildcardOrGeneric type) {
|
||||||
|
return new FormalParameter(name, type, getOffset());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package de.dhbwstuttgart.syntaxtree;
|
package de.dhbwstuttgart.syntaxtree;
|
||||||
|
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
public class GuardedPattern extends Pattern {
|
public class GuardedPattern extends Pattern {
|
||||||
@ -26,4 +27,9 @@ public class GuardedPattern extends Pattern {
|
|||||||
public void accept(ASTVisitor visitor) {
|
public void accept(ASTVisitor visitor) {
|
||||||
visitor.visit(this);
|
visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GuardedPattern withType(RefTypeOrTPHOrWildcardOrGeneric type) {
|
||||||
|
return new GuardedPattern(condition, nested, getOffset());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,27 +5,27 @@ import org.antlr.v4.runtime.Token;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ParameterList extends SyntaxTreeNode implements Iterable<FormalParameter> {
|
public class ParameterList extends SyntaxTreeNode implements Iterable<Pattern> {
|
||||||
private List<FormalParameter> formalparameter;
|
private List<Pattern> formalparameter;
|
||||||
|
|
||||||
public ParameterList(List<FormalParameter> params, Token offset) {
|
public ParameterList(List<Pattern> params, Token offset) {
|
||||||
super(offset);
|
super(offset);
|
||||||
this.formalparameter = params;
|
this.formalparameter = params;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FormalParameter getParameterAt(int i) {
|
public Pattern getParameterAt(int i) {
|
||||||
if (i >= formalparameter.size())
|
if (i >= formalparameter.size())
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return formalparameter.get(i);
|
return formalparameter.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<FormalParameter> getFormalparalist() {
|
public List<Pattern> getFormalparalist() {
|
||||||
return formalparameter;
|
return formalparameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<FormalParameter> iterator() {
|
public Iterator<Pattern> iterator() {
|
||||||
return formalparameter.iterator();
|
return formalparameter.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,4 +14,6 @@ public abstract class Pattern extends SyntaxTreeNode {
|
|||||||
public RefTypeOrTPHOrWildcardOrGeneric getType(){
|
public RefTypeOrTPHOrWildcardOrGeneric getType(){
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract Pattern withType(RefTypeOrTPHOrWildcardOrGeneric type);
|
||||||
}
|
}
|
||||||
|
@ -163,7 +163,7 @@ public class ASTFactory {
|
|||||||
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass);
|
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass);
|
||||||
Parameter[] jreParams = constructor.getParameters();
|
Parameter[] jreParams = constructor.getParameters();
|
||||||
Type[] jreGenericParams = constructor.getGenericParameterTypes();
|
Type[] jreGenericParams = constructor.getGenericParameterTypes();
|
||||||
List<FormalParameter> params = new ArrayList<>();
|
List<Pattern> params = new ArrayList<>();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Type jreParam : jreGenericParams) {
|
for (Type jreParam : jreGenericParams) {
|
||||||
if (jreParam == null)
|
if (jreParam == null)
|
||||||
@ -197,7 +197,7 @@ public class ASTFactory {
|
|||||||
returnType = createType(jreRetType);
|
returnType = createType(jreRetType);
|
||||||
Parameter[] jreParams = jreMethod.getParameters();
|
Parameter[] jreParams = jreMethod.getParameters();
|
||||||
Type[] jreGenericParams = jreMethod.getGenericParameterTypes();
|
Type[] jreGenericParams = jreMethod.getGenericParameterTypes();
|
||||||
List<FormalParameter> params = new ArrayList<>();
|
List<Pattern> params = new ArrayList<>();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Type jreParam : jreGenericParams) {
|
for (Type jreParam : jreGenericParams) {
|
||||||
if (jreParam == null)
|
if (jreParam == null)
|
||||||
|
@ -100,7 +100,7 @@ public class OutputGenerator implements ASTVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visit(ParameterList formalParameters) {
|
public void visit(ParameterList formalParameters) {
|
||||||
out.append("(");
|
out.append("(");
|
||||||
Iterator<FormalParameter> genericIterator = formalParameters.getFormalparalist().iterator();
|
Iterator<Pattern> genericIterator = formalParameters.getFormalparalist().iterator();
|
||||||
if (genericIterator.hasNext()) {
|
if (genericIterator.hasNext()) {
|
||||||
while (genericIterator.hasNext()) {
|
while (genericIterator.hasNext()) {
|
||||||
genericIterator.next().accept(this);
|
genericIterator.next().accept(this);
|
||||||
@ -264,9 +264,9 @@ public class OutputGenerator implements ASTVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(InstanceOf instanceOf) {
|
public void visit(InstanceOf instanceOf) {
|
||||||
instanceOf.lexpr.accept(this);
|
instanceOf.getExpression().accept(this);
|
||||||
out.append(" instanceof ");
|
out.append(" instanceof ");
|
||||||
instanceOf.rexpr.accept(this);
|
instanceOf.getPattern().accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -3,6 +3,7 @@ package de.dhbwstuttgart.target.generate;
|
|||||||
import de.dhbwstuttgart.bytecode.FunNGenerator;
|
import de.dhbwstuttgart.bytecode.FunNGenerator;
|
||||||
import de.dhbwstuttgart.environment.ByteArrayClassLoader;
|
import de.dhbwstuttgart.environment.ByteArrayClassLoader;
|
||||||
import de.dhbwstuttgart.environment.IByteArrayClassLoader;
|
import de.dhbwstuttgart.environment.IByteArrayClassLoader;
|
||||||
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import de.dhbwstuttgart.syntaxtree.*;
|
import de.dhbwstuttgart.syntaxtree.*;
|
||||||
import de.dhbwstuttgart.syntaxtree.Record;
|
import de.dhbwstuttgart.syntaxtree.Record;
|
||||||
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
|
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.TargetBlock;
|
||||||
import de.dhbwstuttgart.target.tree.expression.TargetExpression;
|
import de.dhbwstuttgart.target.tree.expression.TargetExpression;
|
||||||
import de.dhbwstuttgart.target.tree.expression.TargetSwitch;
|
import de.dhbwstuttgart.target.tree.expression.TargetSwitch;
|
||||||
|
import de.dhbwstuttgart.target.tree.expression.TargetTypePattern;
|
||||||
import de.dhbwstuttgart.target.tree.type.*;
|
import de.dhbwstuttgart.target.tree.type.*;
|
||||||
import de.dhbwstuttgart.typeinference.result.*;
|
import de.dhbwstuttgart.typeinference.result.*;
|
||||||
|
|
||||||
@ -150,7 +152,10 @@ public class ASTToTargetAST {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<MethodParameter> convert(ParameterList input, GenerateGenerics generics) {
|
private List<MethodParameter> 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<TargetGeneric> generics, GenericRefType type) {
|
private boolean hasGeneric(Set<TargetGeneric> generics, GenericRefType type) {
|
||||||
|
@ -31,7 +31,10 @@ public class StatementToTargetExpression implements ASTVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(LambdaExpression lambdaExpression) {
|
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<MethodParameter> captures = new ArrayList<>();
|
List<MethodParameter> captures = new ArrayList<>();
|
||||||
lambdaExpression.methodBody.accept(new TracingStatementVisitor() {
|
lambdaExpression.methodBody.accept(new TracingStatementVisitor() {
|
||||||
@ -59,7 +62,7 @@ public class StatementToTargetExpression implements ASTVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visit(LocalVar localVar) {
|
public void visit(LocalVar localVar) {
|
||||||
super.visit(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))
|
if (!hasLocalVar(localVar.name) && !parameters.contains(capture) && !captures.contains(capture))
|
||||||
captures.add(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 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 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 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));
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
package de.dhbwstuttgart.target.tree;
|
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;
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import java.util.Set;
|
|||||||
public record TargetConstructor(int access, Set<TargetGeneric> generics, Set<TargetGeneric> txGenerics, List<MethodParameter> parameters, List<MethodParameter> txParameters, TargetBlock block, TargetBlock fieldInitializer) {
|
public record TargetConstructor(int access, Set<TargetGeneric> generics, Set<TargetGeneric> txGenerics, List<MethodParameter> parameters, List<MethodParameter> txParameters, TargetBlock block, TargetBlock fieldInitializer) {
|
||||||
|
|
||||||
public String getDescriptor() {
|
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() {
|
public String getSignature() {
|
||||||
|
@ -32,7 +32,7 @@ public record TargetMethod(int access, String name, TargetBlock block, Signature
|
|||||||
}
|
}
|
||||||
ret += "(";
|
ret += "(";
|
||||||
for (var param : parameters) {
|
for (var param : parameters) {
|
||||||
ret += param.type().toDescriptor();
|
ret += param.pattern().type().toDescriptor();
|
||||||
}
|
}
|
||||||
ret += ")";
|
ret += ")";
|
||||||
if (returnType == null) ret += "V";
|
if (returnType == null) ret += "V";
|
||||||
@ -41,7 +41,7 @@ public record TargetMethod(int access, String name, TargetBlock block, Signature
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getDescriptor() {
|
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() {
|
public String getSignature() {
|
||||||
|
@ -5,4 +5,8 @@ import de.dhbwstuttgart.target.tree.type.TargetType;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public record TargetComplexPattern(TargetType type, String name, List<TargetPattern> subPatterns) implements TargetPattern {
|
public record TargetComplexPattern(TargetType type, String name, List<TargetPattern> subPatterns) implements TargetPattern {
|
||||||
|
@Override
|
||||||
|
public TargetComplexPattern withType(TargetType type) {
|
||||||
|
return new TargetComplexPattern(type, name, subPatterns);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
package de.dhbwstuttgart.target.tree.expression;
|
package de.dhbwstuttgart.target.tree.expression;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.target.tree.type.TargetType;
|
||||||
|
|
||||||
public record TargetGuard(TargetPattern inner, TargetExpression expression) implements TargetPattern {
|
public record TargetGuard(TargetPattern inner, TargetExpression expression) implements TargetPattern {
|
||||||
|
@Override
|
||||||
|
public TargetGuard withType(TargetType type) {
|
||||||
|
return new TargetGuard(inner.withType(type), expression);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
package de.dhbwstuttgart.target.tree.expression;
|
package de.dhbwstuttgart.target.tree.expression;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.target.tree.type.TargetType;
|
||||||
|
|
||||||
public sealed interface TargetPattern extends TargetExpression permits TargetComplexPattern, TargetGuard, TargetTypePattern {
|
public sealed interface TargetPattern extends TargetExpression permits TargetComplexPattern, TargetGuard, TargetTypePattern {
|
||||||
default String name() {
|
default String name() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TargetPattern withType(TargetType type);
|
||||||
}
|
}
|
||||||
|
@ -3,4 +3,8 @@ package de.dhbwstuttgart.target.tree.expression;
|
|||||||
import de.dhbwstuttgart.target.tree.type.TargetType;
|
import de.dhbwstuttgart.target.tree.type.TargetType;
|
||||||
|
|
||||||
public record TargetTypePattern(TargetType type, String name) implements TargetPattern {
|
public record TargetTypePattern(TargetType type, String name) implements TargetPattern {
|
||||||
|
@Override
|
||||||
|
public TargetTypePattern withType(TargetType type) {
|
||||||
|
return new TargetTypePattern(type, name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,13 +9,7 @@ import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
|||||||
import de.dhbwstuttgart.parser.NullToken;
|
import de.dhbwstuttgart.parser.NullToken;
|
||||||
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
import de.dhbwstuttgart.syntaxtree.*;
|
||||||
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.factory.ASTFactory;
|
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
|
||||||
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
|
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
|
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<TPH A> Dann mus bei dem Parameter der COnstraint entstehen: TPH A <. String
|
* Hier kriegt der Receiver ja den COnstraint TPH REceiver <. List<TPH A> Dann mus bei dem Parameter der COnstraint entstehen: TPH A <. String
|
||||||
*/
|
*/
|
||||||
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
|
List<RefTypeOrTPHOrWildcardOrGeneric> 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
|
params.add(fp.getType()); // info.checkGTV(fp.getType())); //PL 2018-06-22 GTV sollen in Argumenten erhalten bleiben
|
||||||
}
|
}
|
||||||
return params;
|
return params;
|
||||||
|
@ -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<ResultSet> results = new ArrayList<ResultSet>(); GeneratedGenericsFinder generatedGenericsFinder = new GeneratedGenericsFinder(sf, results);
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SourceFile generateAST() {
|
|
||||||
ArrayList<ClassOrInterface> classes = new ArrayList<>();
|
|
||||||
ArrayList<Field> fields = new ArrayList<>();
|
|
||||||
ArrayList<Method> 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<FormalParameter> 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());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user