Compare commits
24
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d456d2ecb | ||
|
|
cbfda2da47 | ||
|
|
c63f1acce5 | ||
|
|
c06079058e | ||
|
|
e1b9766a53 | ||
|
|
35e8dda6bd | ||
|
|
bf22cf246e | ||
|
|
034cdf2e0b | ||
|
|
95ee84cb99 | ||
|
|
3fc098b155 | ||
|
|
32ddb1132f | ||
|
|
174c999543 | ||
|
|
4b576da19d | ||
|
|
942b8ca488 | ||
|
|
de96cda327 | ||
|
|
2710685db3 | ||
|
|
e951a60ae6 | ||
|
|
825df05fac | ||
|
|
fab3e73596 | ||
|
|
392c5fa619 | ||
|
|
e21d2d7ced | ||
|
|
5a12545e8f | ||
|
|
d6c59f07f6 | ||
|
|
eb1c5c6629 |
No files matched your search
@@ -0,0 +1,13 @@
|
||||
import java.lang.Integer;
|
||||
|
||||
public class Bug409 {
|
||||
public static m() {
|
||||
var n = 0;
|
||||
for (var i = 0; i < 5; i++) {
|
||||
n = n + 1;
|
||||
if (n > 100) { return -1; }
|
||||
if (i == 2) { continue; }
|
||||
}
|
||||
return n;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import java.lang.Integer;
|
||||
|
||||
public interface Bug411 {
|
||||
default f() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import java.lang.Integer;
|
||||
|
||||
public class Bug412 {
|
||||
public m(Integer i) {
|
||||
var r = 0;
|
||||
switch (i) {
|
||||
case 1: r = 10; break;
|
||||
default: r = 20;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import java.lang.Integer;
|
||||
sealed interface L<T> permits Cons, Nil {}
|
||||
public record Cons<T>(T head, L<T> tail) implements L<T> {}
|
||||
public record Nil<T>() implements L<T> {}
|
||||
|
||||
public class Bug416 {
|
||||
public static <T> len(Cons(h, t)) { return 1 + len(t); }
|
||||
public static <T> len(Nil()) { return 0; }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import java.lang.Integer;
|
||||
import java.lang.String;
|
||||
|
||||
public class Bug417 {
|
||||
public name(Integer i) {
|
||||
return switch (i) {
|
||||
case 1 -> "one";
|
||||
default -> "many";
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
public class Bug421 {
|
||||
public m() { return 1 } // Missing semicolon
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
public class Bug422 {}
|
||||
public class Bug422 {}
|
||||
@@ -0,0 +1,5 @@
|
||||
import java.lang.Integer;
|
||||
|
||||
interface Bug423I { Integer f(); }
|
||||
|
||||
public class Bug423 implements Bug423I { }
|
||||
@@ -0,0 +1,13 @@
|
||||
import java.lang.Boolean;
|
||||
import java.lang.Integer;
|
||||
import java.lang.System;
|
||||
import java.io.PrintStream;
|
||||
|
||||
public class Bug436 {
|
||||
public not(Boolean b) {
|
||||
return !b;
|
||||
}
|
||||
public not(Integer i) {
|
||||
return ~i;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// 24 type parameters with 24 typed getters: StackOverflowError in unification (20/20 and 24 params with 18 getters compile)
|
||||
public class Bug468<
|
||||
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
|
||||
T21, T22, T23, T24, T25, T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44> {
|
||||
public T0 f0;
|
||||
public T1 f1;
|
||||
public T2 f2;
|
||||
public T3 f3;
|
||||
public T4 f4;
|
||||
public T5 f5;
|
||||
public T6 f6;
|
||||
public T7 f7;
|
||||
public T8 f8;
|
||||
public T9 f9;
|
||||
public T10 f10;
|
||||
public T11 f11;
|
||||
public T12 f12;
|
||||
public T13 f13;
|
||||
public T14 f14;
|
||||
public T15 f15;
|
||||
public T16 f16;
|
||||
public T17 f17;
|
||||
public T18 f18;
|
||||
public T19 f19;
|
||||
public T20 f20;
|
||||
public T21 f21;
|
||||
public T22 f22;
|
||||
public T23 f23;
|
||||
public T24 f24;
|
||||
public T25 f25;
|
||||
public T26 f26;
|
||||
public T27 f27;
|
||||
public T28 f28;
|
||||
public T29 f29;
|
||||
public T30 f30;
|
||||
public T31 f31;
|
||||
public T32 f32;
|
||||
public T33 f33;
|
||||
public T34 f34;
|
||||
public T35 f35;
|
||||
public T36 f36;
|
||||
public T37 f37;
|
||||
public T38 f38;
|
||||
public T39 f39;
|
||||
public T40 f40;
|
||||
public T41 f41;
|
||||
public T42 f42;
|
||||
public T43 f43;
|
||||
public T44 f44;
|
||||
|
||||
public T0 g0() { return f0; }
|
||||
public T1 g1() { return f1; }
|
||||
public T2 g2() { return f2; }
|
||||
public T3 g3() { return f3; }
|
||||
public T4 g4() { return f4; }
|
||||
public T5 g5() { return f5; }
|
||||
public T6 g6() { return f6; }
|
||||
public T7 g7() { return f7; }
|
||||
public T8 g8() { return f8; }
|
||||
public T9 g9() { return f9; }
|
||||
public T10 g10() { return f10; }
|
||||
public T11 g11() { return f11; }
|
||||
public T12 g12() { return f12; }
|
||||
public T13 g13() { return f13; }
|
||||
public T14 g14() { return f14; }
|
||||
public T15 g15() { return f15; }
|
||||
public T16 g16() { return f16; }
|
||||
public T17 g17() { return f17; }
|
||||
public T18 g18() { return f18; }
|
||||
public T19 g19() { return f19; }
|
||||
public T20 g20() { return f20; }
|
||||
public T21 g21() { return f21; }
|
||||
public T22 g22() { return f22; }
|
||||
public T23 g23() { return f23; }
|
||||
public T24 g24() { return f24; }
|
||||
public T25 g25() { return f25; }
|
||||
public T26 g26() { return f26; }
|
||||
public T27 g27() { return f27; }
|
||||
public T28 g28() { return f28; }
|
||||
public T29 g29() { return f29; }
|
||||
public T30 g30() { return f30; }
|
||||
public T31 g31() { return f31; }
|
||||
public T32 g32() { return f32; }
|
||||
public T33 g33() { return f33; }
|
||||
public T34 g34() { return f34; }
|
||||
public T35 g35() { return f35; }
|
||||
public T36 g36() { return f36; }
|
||||
public T37 g37() { return f37; }
|
||||
public T38 g38() { return f38; }
|
||||
public T39 g39() { return f39; }
|
||||
public T40 g40() { return f40; }
|
||||
public T41 g41() { return f41; }
|
||||
public T42 g42() { return f42; }
|
||||
public T43 g43() { return f43; }
|
||||
public T44 g44() { return f44; }
|
||||
}
|
||||
@@ -15,6 +15,7 @@ interface C {
|
||||
}
|
||||
|
||||
class ClassX implements A {
|
||||
void method1() {}
|
||||
}
|
||||
|
||||
record ClassY(Integer myInt) implements C {}
|
||||
|
||||
@@ -5,4 +5,8 @@ public class Literal {
|
||||
public m2() { return 'C'; }
|
||||
public m3() { return 10L; }
|
||||
public m4() { return 10.5F; }
|
||||
public m5() { return 10.5D; }
|
||||
public m6() { return 0xFF; }
|
||||
public m7() { return 0b101010; }
|
||||
public m8() { return 010; }
|
||||
}
|
||||
@@ -735,6 +735,13 @@ public class Codegen {
|
||||
case TargetUnaryOp.Not not -> {
|
||||
generate(state, not.expr());
|
||||
convertTo(state, not.expr().type(), TargetType.boolean_);
|
||||
mv.visitInsn(ICONST_1);
|
||||
mv.visitInsn(IXOR);
|
||||
boxPrimitive(state, TargetType.toPrimitive(not.expr().type()));
|
||||
}
|
||||
case TargetUnaryOp.BNot not -> {
|
||||
generate(state, not.expr());
|
||||
convertTo(state, not.expr().type(), TargetType.toPrimitive(not.expr().type()));
|
||||
if (not.type().equals(TargetType.Long)) {
|
||||
mv.visitLdcInsn(-1L);
|
||||
mv.visitInsn(LXOR);
|
||||
@@ -1056,6 +1063,7 @@ public class Codegen {
|
||||
}
|
||||
|
||||
Label start = new Label();
|
||||
Label skip = new Label();
|
||||
Label end = new Label();
|
||||
mv.visitLabel(start);
|
||||
if (_for.termination() != null) {
|
||||
@@ -1068,13 +1076,14 @@ public class Codegen {
|
||||
}
|
||||
|
||||
var env = new BreakEnv();
|
||||
env.startLabel = start;
|
||||
env.startLabel = skip;
|
||||
env.endLabel = end;
|
||||
|
||||
state.breakStack.push(env);
|
||||
generate(state, _for.body());
|
||||
state.breakStack.pop();
|
||||
|
||||
mv.visitLabel(skip);
|
||||
if (_for.increment() != null) {
|
||||
_for.increment().forEach(e -> {
|
||||
generate(state, e);
|
||||
@@ -1435,13 +1444,16 @@ public class Codegen {
|
||||
if (label instanceof TargetTypePattern || label instanceof TargetComplexPattern) {
|
||||
if (label.type() instanceof TargetGenericType) types.add(Type.getType(Object.class));
|
||||
else types.add(Type.getObjectType(label.type().getInternalName()));
|
||||
} else if (label instanceof TargetLiteral lit) {
|
||||
types.add(lit.value());
|
||||
} else if (label instanceof TargetExpressionPattern expr) {
|
||||
if (expr.expression() instanceof TargetLiteral literal) {
|
||||
types.add(Type.getObjectType(TargetType.toWrapper(literal.type()).getInternalName()));
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
} else if (label instanceof TargetGuard guard) {
|
||||
types.add(Type.getObjectType(guard.inner().type().getInternalName()));
|
||||
// TODO Same here we need to evaluate constant;
|
||||
} else {
|
||||
logger.info(label);
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -1775,7 +1787,7 @@ public class Codegen {
|
||||
if (parent != null) {
|
||||
state.scope.parent = parent.scope;
|
||||
}
|
||||
var offset = 1;
|
||||
var offset = method.isStatic() ? 0 : 1;
|
||||
for (var param : method.signature().parameters()) {
|
||||
state.createVariable(param.pattern().name(), param.pattern().type());
|
||||
}
|
||||
@@ -1846,6 +1858,35 @@ public class Codegen {
|
||||
clazz.constructors().forEach(this::generateConstructor);
|
||||
clazz.methods().forEach(this::generateMethod);
|
||||
|
||||
// Check if non abstract class implements all interface methods
|
||||
if ((access & ACC_ABSTRACT) == 0) {
|
||||
for (var intf : clazz.implementingInterfaces()) {
|
||||
var intfClass = compiler.getClass(new JavaClassName(intf.name()));
|
||||
for (var method : intfClass.getMethods()) {
|
||||
if ((method.modifier & ACC_ABSTRACT) != 0) {
|
||||
// Check if the method is implemented in the current class
|
||||
// TODO This is duplicate code, we should probably refactor instances of this pattern
|
||||
var foundMethods = clazz.methods().stream().filter(m -> {
|
||||
if (!m.name().equals(method.name)) return false;
|
||||
if (m.signature().parameters().size() != method.getParameterList().getFormalparalist().size())
|
||||
return false;
|
||||
for (var i = 0; i < m.signature().parameters().size(); i++) {
|
||||
var param1 = m.signature().parameters().get(i).pattern().type();
|
||||
var param2 = converter.convert(
|
||||
method.getParameterList().getParameterAt(i).getType(),
|
||||
ASTToTargetAST.Generics.nullGenerics(compiler).javaGenerics());
|
||||
if (!param1.equals(param2)) return false;
|
||||
}
|
||||
return true;
|
||||
}).toList();
|
||||
if (foundMethods.size() != 1) {
|
||||
throw new CodeGenException("Class " + clazz.qualifiedName() + " does not implement method " + method.name + " from interface " + intfClass.getClassName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (clazz.staticConstructor() != null)
|
||||
generateStaticConstructor(clazz.staticConstructor());
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import de.dhbwstuttgart.bytecode.Codegen;
|
||||
import de.dhbwstuttgart.bytecode.FunNGenerator;
|
||||
import de.dhbwstuttgart.environment.CompilationEnvironment;
|
||||
import de.dhbwstuttgart.environment.DirectoryClassLoader;
|
||||
import de.dhbwstuttgart.exceptions.CompilerException;
|
||||
import de.dhbwstuttgart.exceptions.CompilerWarning;
|
||||
import de.dhbwstuttgart.exceptions.DebugException;
|
||||
import de.dhbwstuttgart.exceptions.WarningsException;
|
||||
@@ -171,6 +172,8 @@ public class JavaTXCompiler {
|
||||
|
||||
var numberOfGenerics = decl == null ? 0 : decl.genericTypeVar().size();
|
||||
var className = pkgName + (!pkgName.isEmpty() ? "." : "") + name;
|
||||
if (classRegistry.contains(className))
|
||||
throw new CompilerException("Duplicate class definition: " + className);
|
||||
classRegistry.addName(className, numberOfGenerics);
|
||||
input.add(classRegistry.getName(className));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package de.dhbwstuttgart.exceptions;
|
||||
|
||||
public class CompilerException extends RuntimeException {
|
||||
public CompilerException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,8 @@ package de.dhbwstuttgart.exceptions;
|
||||
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
public class ParserError extends TypeinferenceException{
|
||||
|
||||
public ParserError(Token offset){
|
||||
super("Fehler beim Parsen", offset);
|
||||
public class ParserError extends RuntimeException{
|
||||
public ParserError(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.dhbwstuttgart.parser;
|
||||
|
||||
import de.dhbwstuttgart.environment.CompilationEnvironment;
|
||||
import de.dhbwstuttgart.exceptions.ParserError;
|
||||
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Lexer;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser;
|
||||
@@ -8,9 +9,7 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
|
||||
import de.dhbwstuttgart.util.Logger;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -21,13 +20,25 @@ public class JavaTXParser {
|
||||
|
||||
public static Logger logger = new Logger("Parser");
|
||||
|
||||
public static class ThrowingErrorListener extends BaseErrorListener {
|
||||
|
||||
@Override
|
||||
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
|
||||
int line, int charPositionInLine, String msg, RecognitionException e) {
|
||||
throw new ParserError("Syntax error at line " + line + ":" + charPositionInLine + " - " + msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static Java17Parser.SourceFileContext parse(File source) throws IOException, java.lang.ClassNotFoundException {
|
||||
InputStream stream = new FileInputStream(source);
|
||||
// DEPRECATED: ANTLRInputStream input = new ANTLRInputStream(stream);
|
||||
var errorListener = new ThrowingErrorListener();
|
||||
CharStream input = CharStreams.fromStream(stream);
|
||||
Java17Lexer lexer = new Java17Lexer(input);
|
||||
lexer.addErrorListener(errorListener);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
Java17Parser parser = new Java17Parser(tokens);
|
||||
parser.addErrorListener(errorListener);
|
||||
return parser.sourceFile();
|
||||
/*
|
||||
* SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(source)); return generator.convert(tree);
|
||||
|
||||
@@ -15,6 +15,7 @@ import de.dhbwstuttgart.typeinference.constraints.Pair;
|
||||
import de.dhbwstuttgart.typeinference.unify.PlaceholderRegistry;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.*;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -54,7 +55,8 @@ public class FCGenerator {
|
||||
|
||||
|
||||
private static Boolean isFunctionalInterface(ClassOrInterface cly) {
|
||||
return (cly.isInterface() && (cly.isFunctionalInterface() || cly.getMethods().size() == 1));
|
||||
// TODO isFunctionalInterface should be set properly, we shouldn't need to check it again
|
||||
return (cly.isInterface() && (cly.isFunctionalInterface() || cly.getMethods().size() == 1 && Modifier.isAbstract(cly.getMethods().getFirst().modifier)));
|
||||
}
|
||||
|
||||
private static Pair genImplFunType(ClassOrInterface cly, RefTypeOrTPHOrWildcardOrGeneric fIType, HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> gtvs) {
|
||||
|
||||
@@ -405,14 +405,16 @@ public class StatementGenerator {
|
||||
|
||||
private SwitchBlock convert(Java17Parser.SwitchBlockStatementGroupContext stmt) {
|
||||
List<SwitchLabel> labels = new ArrayList<>();
|
||||
stmt.switchLabel().forEach((label) -> {
|
||||
var isDefault = false;
|
||||
for (var label : stmt.switchLabel()) {
|
||||
if (label instanceof SwitchLabelDefaultContext) isDefault = true;
|
||||
labels.add(convert(label));
|
||||
});
|
||||
}
|
||||
List<Statement> block = new ArrayList<>();
|
||||
stmt.blockStatement().stream().forEach((blockStmt) -> {
|
||||
block.addAll(convert(blockStmt));
|
||||
});
|
||||
return new SwitchBlock(labels, new Block(block, stmt.blockStatement(0).getStart()), false, stmt.getStart());
|
||||
return new SwitchBlock(labels, new Block(block, stmt.blockStatement(0).getStart()), isDefault,false, stmt.getStart());
|
||||
}
|
||||
|
||||
private SwitchLabel convert(SwitchLabelContext switchLabel) {
|
||||
@@ -985,6 +987,9 @@ public class StatementGenerator {
|
||||
} else if (op.getText().equals("-")) {
|
||||
ret = new UnaryExpr(UnaryExpr.Operation.MINUS, expr, TypePlaceholder.fresh(op), op);
|
||||
return ret;
|
||||
} else if (op.getText().equals("~")) {
|
||||
ret = new UnaryExpr(UnaryExpr.Operation.BNOT, expr, TypePlaceholder.fresh(op), op);
|
||||
return ret;
|
||||
} else {
|
||||
throw new NotImplementedException(op.getText());
|
||||
}
|
||||
@@ -1033,10 +1038,22 @@ public class StatementGenerator {
|
||||
private Expression convert(Java17Parser.LiteralContext literal) {
|
||||
switch (literal) {
|
||||
case IntLiteralContext intliteral:
|
||||
var text = intliteral.getText();
|
||||
var radix = 10;
|
||||
if (text.startsWith("0x") || text.startsWith("0X")) {
|
||||
radix = 16;
|
||||
text = text.substring(2);
|
||||
} else if (text.startsWith("0b") || text.startsWith("0B")) {
|
||||
radix = 2;
|
||||
text = text.substring(2);
|
||||
} else if (text.startsWith("0") && text.length() > 1) {
|
||||
radix = 8;
|
||||
text = text.substring(1);
|
||||
}
|
||||
Number value;
|
||||
if (intliteral.getText().endsWith("l") || intliteral.getText().endsWith("L"))
|
||||
value = Long.parseLong(intliteral.getText().substring(0, intliteral.getText().length() - 1));
|
||||
else value = Integer.parseInt(intliteral.getText());
|
||||
if (text.endsWith("l") || text.endsWith("L"))
|
||||
value = Long.parseLong(text.substring(0, text.length() - 1), radix);
|
||||
else value = Integer.parseInt(text, radix);
|
||||
return new Literal(TypePlaceholder.fresh(literal.getStart()), value, intliteral.getStart());
|
||||
case FltLiteralContext floatliteral:
|
||||
if (floatliteral.getText().endsWith("f") || floatliteral.getText().endsWith("F"))
|
||||
|
||||
@@ -409,8 +409,9 @@ public class SyntaxTreeGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
boolean isFunctonalInterfce = methods.size() == 1 && Modifier.isAbstract(methods.getFirst().modifier);
|
||||
var staticCtor = Optional.of(this.generateStaticConstructor(ctx.identifier().getText(), staticFieldInitializations, genericParams, ctx.getStart()));
|
||||
return new ClassOrInterface(modifiers, name, fields, Optional.empty(), staticCtor, methods, new ArrayList<>(), genericParams, superClass, true, methods.size() == 1 ? true : false, extendedInterfaces, permittedSubtypes, ctx.getStart(), fileName);
|
||||
return new ClassOrInterface(modifiers, name, fields, Optional.empty(), staticCtor, methods, new ArrayList<>(), genericParams, superClass, true, isFunctonalInterfce, extendedInterfaces, permittedSubtypes, ctx.getStart(), fileName);
|
||||
}
|
||||
|
||||
private GenericDeclarationList createEmptyGenericDeclarationList(Token classNameIdentifier) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.antlr.v4.runtime.Token;
|
||||
public class UnaryExpr extends JavaInternalExpression
|
||||
{
|
||||
public enum Operation{
|
||||
BNOT,
|
||||
NOT,
|
||||
MINUS,
|
||||
PREINCREMENT,
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.lang.classfile.Opcode;
|
||||
import java.lang.reflect.AccessFlag;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
@@ -630,7 +631,7 @@ public class ASTToTargetAST {
|
||||
mParams,
|
||||
classType,
|
||||
method.name(),
|
||||
false, false, method.isPrivate()
|
||||
method.isStatic(), false, method.isPrivate()
|
||||
);
|
||||
if (method.signature().returnType() != null) {
|
||||
caseBody = new TargetReturn(caseBody);
|
||||
|
||||
@@ -425,6 +425,7 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
@Override
|
||||
public void visit(UnaryExpr unaryExpr) {
|
||||
result = switch (unaryExpr.operation) {
|
||||
case BNOT -> new TargetUnaryOp.BNot(converter.convert(unaryExpr.getType(), generics), converter.convert(unaryExpr.expr, generics));
|
||||
case NOT -> new TargetUnaryOp.Not(converter.convert(unaryExpr.getType(), generics), converter.convert(unaryExpr.expr, generics));
|
||||
case MINUS -> new TargetUnaryOp.Negate(converter.convert(unaryExpr.getType(), generics), converter.convert(unaryExpr.expr, generics));
|
||||
case PREINCREMENT -> new TargetUnaryOp.PreIncrement(converter.convert(unaryExpr.getType(), generics), converter.convert(unaryExpr.expr, generics));
|
||||
@@ -476,7 +477,7 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
.map(this::extractAllPatterns)
|
||||
.flatMap(List::stream).toList();
|
||||
case FormalParameter param -> List.of(new TypeVariants(param.getType(), converter.findAllVariants(param.getType())));
|
||||
default -> List.of();
|
||||
case null, default -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ public sealed interface TargetUnaryOp extends TargetExpression {
|
||||
|
||||
record Negate(TargetType type, TargetExpression expr) implements TargetUnaryOp {}
|
||||
record Add(TargetType type, TargetExpression expr) implements TargetUnaryOp {}
|
||||
record BNot(TargetType type, TargetExpression expr) implements TargetUnaryOp {}
|
||||
record Not(TargetType type, TargetExpression expr) implements TargetUnaryOp {}
|
||||
|
||||
record PreIncrement(TargetType type, TargetExpression expr) implements TargetStatementExpression, TargetUnaryOp {}
|
||||
|
||||
@@ -254,7 +254,7 @@ public class TYPEStmt implements StatementVisitor {
|
||||
} else if (unaryExpr.operation == UnaryExpr.Operation.NOT) {
|
||||
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT, loc(unaryExpr.getOffset())));
|
||||
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()), PairOperator.EQUALSDOT, loc(unaryExpr.getOffset())));
|
||||
} else if (unaryExpr.operation == UnaryExpr.Operation.MINUS) {
|
||||
} else if (unaryExpr.operation == UnaryExpr.Operation.MINUS || unaryExpr.operation == UnaryExpr.Operation.BNOT) {
|
||||
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), number, PairOperator.SMALLERDOT, loc(unaryExpr.getOffset())));
|
||||
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT, loc(unaryExpr.getOffset())));
|
||||
} else {
|
||||
@@ -875,8 +875,8 @@ public class TYPEStmt implements StatementVisitor {
|
||||
if (caseExpressionType == null) {
|
||||
for (var child : switchStmt.getBlocks()) {
|
||||
for (var label : child.getLabels()) {
|
||||
if (label.getPattern() == null) {
|
||||
constraintsSet.addUndConstraint(new Pair(new RefType(new JavaClassName("java.lang.Object"), new NullToken()), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT, loc(label.getOffset())));
|
||||
if (label.getPattern() == null && switchStmt.getSwitch().getType() instanceof TypePlaceholder) {
|
||||
constraintsSet.addUndConstraint(new Pair(new RefType(new JavaClassName("java.lang.Object"), new NullToken()), switchStmt.getSwitch().getType(), PairOperator.EQUALSDOT, loc(label.getOffset())));
|
||||
}
|
||||
if (label.getPattern() instanceof RecordPattern) {
|
||||
RecordPattern recordPattern = (RecordPattern) label.getPattern();
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
|
||||
import de.dhbwstuttgart.bytecode.CodeGenException;
|
||||
import de.dhbwstuttgart.core.ConsoleInterface;
|
||||
import de.dhbwstuttgart.exceptions.CompilerException;
|
||||
import de.dhbwstuttgart.exceptions.ParserError;
|
||||
import de.dhbwstuttgart.exceptions.WarningsException;
|
||||
import de.dhbwstuttgart.util.Logger;
|
||||
import de.dhbwstuttgart.util.Logger.LogLevel;
|
||||
@@ -1081,9 +1083,9 @@ public class TestComplete {
|
||||
assertEquals(40.0, m.invoke(instance, d, 0));
|
||||
}
|
||||
|
||||
@Disabled("Waiting for #407, skipping for now")
|
||||
@Test
|
||||
public void testPatternMatchingInterface() throws Exception {
|
||||
ConsoleInterface.logLevel = LogLevel.INFO;
|
||||
var classFiles = generateClassFiles(createClassLoader(), false, "Matcher.jav", "PatternMatchingInterface.jav");
|
||||
var Intf = classFiles.get("Interface");
|
||||
var clazz = classFiles.get("PatternMatchingInterface");
|
||||
@@ -1228,6 +1230,10 @@ public class TestComplete {
|
||||
assertEquals('C', clazz.getDeclaredMethod("m2").invoke(instance));
|
||||
assertEquals(10L, clazz.getDeclaredMethod("m3").invoke(instance));
|
||||
assertEquals(10.5F, clazz.getDeclaredMethod("m4").invoke(instance));
|
||||
assertEquals(10.5D, clazz.getDeclaredMethod("m5").invoke(instance));
|
||||
assertEquals(0xFF, clazz.getDeclaredMethod("m6").invoke(instance));
|
||||
assertEquals(0b101010, clazz.getDeclaredMethod("m7").invoke(instance));
|
||||
assertEquals(010, clazz.getDeclaredMethod("m8").invoke(instance));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1704,4 +1710,107 @@ public class TestComplete {
|
||||
var clazz = classFiles.get("Bug406");
|
||||
clazz.getDeclaredMethod("main", List.class).invoke(null, List.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug423() throws Exception {
|
||||
try {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug423.jav");
|
||||
fail("No exception thrown!");
|
||||
} catch (CodeGenException e) {
|
||||
assertEquals("Class Bug423 does not implement method f from interface Bug423I", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug422() throws Exception {
|
||||
try {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug422.jav");
|
||||
fail("Shouldn't compile!");
|
||||
} catch (CompilerException e) {
|
||||
assertEquals("Duplicate class definition: Bug422", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug421() throws Exception {
|
||||
try {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug421.jav");
|
||||
fail("Shouldn't compile!");
|
||||
} catch (ParserError e) {
|
||||
assertEquals("Syntax error at line 2:26 - no viable alternative at input 'm() { return 1 }'", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug409() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug409.jav");
|
||||
var clazz = classFiles.get("Bug409");
|
||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||
var m = clazz.getDeclaredMethod("m");
|
||||
assertEquals(5, m.invoke(instance));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug417() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug417.jav");
|
||||
var clazz = classFiles.get("Bug417");
|
||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||
var name = clazz.getDeclaredMethod("name", Integer.class);
|
||||
assertEquals("one", name.invoke(instance, 1));
|
||||
assertEquals("many", name.invoke(instance, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug411() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug411.jav");
|
||||
var clazz = classFiles.get("Bug411");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug412() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug412.jav");
|
||||
var clazz = classFiles.get("Bug412");
|
||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||
var m = clazz.getDeclaredMethod("m", Integer.class);
|
||||
assertEquals(10, m.invoke(instance, 1));
|
||||
assertEquals(20, m.invoke(instance, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug416() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug416.jav");
|
||||
var clazz = classFiles.get("Bug416");
|
||||
var L = classFiles.get("L");
|
||||
var Cons = classFiles.get("Cons");
|
||||
var Nil = classFiles.get("Nil");
|
||||
|
||||
var consCtor = Cons.getDeclaredConstructor(Object.class, L);
|
||||
var nilCtor = Nil.getDeclaredConstructor();
|
||||
|
||||
var list = consCtor.newInstance(1, consCtor.newInstance(2, consCtor.newInstance(3, nilCtor.newInstance())));
|
||||
|
||||
var len = clazz.getDeclaredMethod("len", L);
|
||||
assertEquals(3, len.invoke(null, list));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug436() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug436.jav");
|
||||
var clazz = classFiles.get("Bug436");
|
||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||
|
||||
var not = clazz.getDeclaredMethod("not", Boolean.class);
|
||||
assertEquals(false, not.invoke(instance, true));
|
||||
assertEquals(true, not.invoke(instance, false));
|
||||
|
||||
var bnot = clazz.getDeclaredMethod("not", Integer.class);
|
||||
assertEquals(-11, bnot.invoke(instance, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug468() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug468.jav");
|
||||
var clazz = classFiles.get("Bug468");
|
||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
}
|
||||
@@ -143,7 +143,7 @@ public class TestCodegen {
|
||||
public void testUnary() throws Exception {
|
||||
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, new JavaClassName("Unary"));
|
||||
|
||||
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "not", List.of(new MethodParameter(TargetType.Integer, "a")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetUnaryOp.Not(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"))))));
|
||||
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "not", List.of(new MethodParameter(TargetType.Integer, "a")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetUnaryOp.BNot(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"))))));
|
||||
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "neg", List.of(new MethodParameter(TargetType.Integer, "a")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetUnaryOp.Negate(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"))))));
|
||||
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "add", List.of(new MethodParameter(TargetType.Integer, "a")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetUnaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"))))));
|
||||
|
||||
@@ -202,9 +202,20 @@ public class TestCodegen {
|
||||
@Test
|
||||
public void testFor() throws Exception {
|
||||
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, new JavaClassName("For"));
|
||||
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "forLoop", List.of(), TargetType.Integer, new TargetBlock(List.of(new TargetVarDecl(TargetType.Integer, "sum", new TargetLiteral.IntLiteral(0)), new TargetFor(List.of(new TargetVarDecl(TargetType.Integer, "i", new TargetLiteral.IntLiteral(0))), new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(10)), List.of(new TargetAssign(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(1)))), new TargetBlock(List.of(new TargetAssign(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"), new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"), new TargetLocalVar(TargetType.Integer, "i")))))), new TargetReturn(new TargetLocalVar(TargetType.Integer, "sum")))));
|
||||
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "forLoop", List.of(), TargetType.Integer,
|
||||
new TargetBlock(List.of(new TargetVarDecl(TargetType.Integer, "sum", new TargetLiteral.IntLiteral(0)),
|
||||
new TargetFor(List.of(new TargetVarDecl(TargetType.Integer, "i", new TargetLiteral.IntLiteral(0))),
|
||||
new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"),
|
||||
new TargetLiteral.IntLiteral(10)), List.of(new TargetAssign(TargetType.Integer,
|
||||
new TargetLocalVar(TargetType.Integer, "i"),
|
||||
new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(1)))),
|
||||
new TargetBlock(List.of(
|
||||
new TargetAssign(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"),
|
||||
new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"),
|
||||
new TargetLocalVar(TargetType.Integer, "i")))))),
|
||||
new TargetReturn(new TargetLocalVar(TargetType.Integer, "sum")))));
|
||||
var clazz = generateClass(targetClass, createClassLoader());
|
||||
assertEquals(clazz.getDeclaredMethod("forLoop").invoke(null), 45);
|
||||
assertEquals(45, clazz.getDeclaredMethod("forLoop").invoke(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user