1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-12-24 04:31:02 +00:00

Added array length mismatch error in Pass1.

This commit is contained in:
jespergravgaard 2017-12-28 00:17:39 +01:00
parent 19a040345f
commit 6532fdd002
5 changed files with 62 additions and 0 deletions

View File

@ -116,6 +116,7 @@ public class Compiler {
new Pass1FixLValuesLoHi(program).execute(); new Pass1FixLValuesLoHi(program).execute();
new Pass1AssertNoLValueIntermediate(program).execute(); new Pass1AssertNoLValueIntermediate(program).execute();
new Pass1AddTypePromotions(program).execute(); new Pass1AddTypePromotions(program).execute();
new Pass1AssertArrayLengths(program).execute();
getLog().append("INITIAL CONTROL FLOW GRAPH"); getLog().append("INITIAL CONTROL FLOW GRAPH");
getLog().append(program.getGraph().toString(program)); getLog().append(program.getGraph().toString(program));

View File

@ -0,0 +1,47 @@
package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.*;
/**
* Asserts that all arrays with lengths and initializers have matching lengths.
*/
public class Pass1AssertArrayLengths extends Pass1Base {
public Pass1AssertArrayLengths(Program program) {
super(program);
}
@Override
public boolean step() {
for(ControlFlowBlock block : getGraph().getAllBlocks()) {
for(Statement statement : block.getStatements()) {
if(statement instanceof StatementAssignment) {
StatementAssignment assignment = (StatementAssignment) statement;
LValue lValue = assignment.getlValue();
if(lValue instanceof VariableRef) {
Variable variable = getScope().getVariable((VariableRef) lValue);
if(variable.getType() instanceof SymbolTypeArray) {
Integer declaredSize = ((SymbolTypeArray) variable.getType()).getSize();
if(declaredSize != null) {
if(assignment.getrValue1() == null && assignment.getOperator() == null) {
RValue value = assignment.getrValue2();
if(value instanceof ValueList) {
if(((ValueList) value).getList().size()!=declaredSize) {
throw new CompileError("Error! Array length mismatch "+statement.toString(getProgram(), false));
}
} else if(value instanceof ConstantString) {
if(((ConstantString) value).getValue().length()!=declaredSize) {
throw new CompileError("Error! Array length mismatch "+statement.toString(getProgram(), false));
}
}
}
}
}
}
}
}
}
return false;
}
}

View File

@ -351,6 +351,14 @@ public class TestPrograms extends TestCase {
assertError("array-uninitialized", "Cannot determine array size."); assertError("array-uninitialized", "Cannot determine array size.");
} }
public void testArrayLengthMismatch() throws IOException, URISyntaxException {
assertError("array-length-mismatch", "Array length mismatch");
}
public void testStringLengthMismatch() throws IOException, URISyntaxException {
assertError("string-length-mismatch", "Array length mismatch");
}
private void assertError(String kcFile, String expectError) throws IOException, URISyntaxException { private void assertError(String kcFile, String expectError) throws IOException, URISyntaxException {
try { try {
compileAndCompare(kcFile); compileAndCompare(kcFile);

View File

@ -0,0 +1,3 @@
byte[3] b = { 1, 2 };
void main() {}

View File

@ -0,0 +1,3 @@
byte[3] b = "qwe!";
void main() {}