Commit 187bbb40 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

OOP: semantic check - constant initialization (issue #427)


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 37946a16
...@@ -168,6 +168,23 @@ type class OuterClass { ...@@ -168,6 +168,23 @@ type class OuterClass {
} }
} }
// constant initialization
type class ConstantInitialization1 {
const integer const_int;
function f_create(integer pl_create_int) {
const_int := pl_create_int;
}
}
type class ConstantInitialization2 {
const integer const_int := 0;
create(integer pl_create_int) {
const_int := pl_create_int;
}
}
type class ReuseClass { type class ReuseClass {
var charstring vl_orig := "abc"; var charstring vl_orig := "abc";
......
...@@ -136,6 +136,14 @@ type class ConstructorOverrideSub { ...@@ -136,6 +136,14 @@ type class ConstructorOverrideSub {
} }
} }
type class ConstantInitialization {
const integer const_int;
create(integer pl_create_int) {
const_int := pl_create_int;
}
}
// runs on compatibility // runs on compatibility
type class RunsonClass1 runs on CT { } type class RunsonClass1 runs on CT { }
type class RunsonClass2 extends RunsonClass1 runs on CT { } type class RunsonClass2 extends RunsonClass1 runs on CT { }
......
...@@ -74,6 +74,9 @@ public final class Assignment_Statement extends Statement { ...@@ -74,6 +74,9 @@ public final class Assignment_Statement extends Statement {
private static final String OMITTOMANDATORYASSIGNMENT1 = "Omit value can only be assigned to an optional field of a record or set value"; private static final String OMITTOMANDATORYASSIGNMENT1 = "Omit value can only be assigned to an optional field of a record or set value";
private static final String OMITTOMANDATORYASSIGNMENT2 = "Assignment of `omit'' to mandatory field `{0}'' of type `{1}''"; private static final String OMITTOMANDATORYASSIGNMENT2 = "Assignment of `omit'' to mandatory field `{0}'' of type `{1}''";
private static final String STATEMENT_NAME = "assignment"; private static final String STATEMENT_NAME = "assignment";
private static final String VALUEALREADYSET = "{0} value can only be assigned once";
private static final String BADCONSTANTASSIGNMENT = "Constant can only be initialized in a constructor";
private final Reference reference; private final Reference reference;
private final TTCN3Template template; private final TTCN3Template template;
...@@ -235,12 +238,40 @@ public final class Assignment_Statement extends Statement { ...@@ -235,12 +238,40 @@ public final class Assignment_Statement extends Statement {
((Def_Var_Template) assignment).setWritten(); ((Def_Var_Template) assignment).setWritten();
checkTemplateAssignment(timestamp, assignment,Expected_Value_type.EXPECTED_TEMPLATE,null); checkTemplateAssignment(timestamp, assignment,Expected_Value_type.EXPECTED_TEMPLATE,null);
break; break;
case A_CONST:
final IValue temporalConstValue = template.getValue();
if (temporalConstValue != null) {
checkConstAssignment(timestamp, assignment, temporalConstValue);
template.setMyGovernor(temporalConstValue.getMyGovernor());
break;
}
break;
default: default:
reference.getLocation().reportSemanticError(MessageFormat.format(VARIABLEREFERENCEEXPECTED, assignment.getAssignmentName())); reference.getLocation().reportSemanticError(MessageFormat.format(VARIABLEREFERENCEEXPECTED, assignment.getAssignmentName()));
reference.setIsErroneous(true); reference.setIsErroneous(true);
isErroneous = true; isErroneous = true;
} }
} }
private void checkConstAssignment(final CompilationTimeStamp timestamp, final Assignment assignment, final IValue value) {
Def_Const def = (Def_Const)assignment;
if (def.getValue() != null) {
value.getLocation().reportSemanticError(
MessageFormat.format(VALUEALREADYSET, "Constant"));
}
final Scope scope = value.getMyScope();
if (scope instanceof StatementBlock) {
Definition definition = ((StatementBlock)scope).getMyDefinition();
if (definition instanceof Def_Function) {
final Identifier id = ((Def_Function)definition).getIdentifier();
if (id.getName().equals("create")) {
return;
}
}
}
value.getLocation().reportSemanticError(BADCONSTANTASSIGNMENT);
}
private void checkVarAssignment(final CompilationTimeStamp timestamp, final Assignment assignment, final IValue value) { private void checkVarAssignment(final CompilationTimeStamp timestamp, final Assignment assignment, final IValue value) {
final IType varType = getType(timestamp, assignment); final IType varType = getType(timestamp, assignment);
......
...@@ -152,7 +152,7 @@ public class OOP_Semantic_tests { ...@@ -152,7 +152,7 @@ public class OOP_Semantic_tests {
private ArrayList<MarkerToCheck> oopNegative_ttcn_initializer() { private ArrayList<MarkerToCheck> oopNegative_ttcn_initializer() {
//oopNegativeSemanticTest.ttcn //oopNegativeSemanticTest.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(77); ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(79);
int lineNum = 32; int lineNum = 32;
markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 10; lineNum += 10;
...@@ -219,7 +219,11 @@ public class OOP_Semantic_tests { ...@@ -219,7 +219,11 @@ public class OOP_Semantic_tests {
markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", lineNum++, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", lineNum++, IMarker.SEVERITY_ERROR));
} }
markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", ++lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", ++lineNum, IMarker.SEVERITY_ERROR));
lineNum += 16; lineNum += 15;
markersToCheck.add(new MarkerToCheck("Constant can only be initialized in a constructor", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 8;
markersToCheck.add(new MarkerToCheck("Constant value can only be assigned once", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 10;
markersToCheck.add(new MarkerToCheck("integer value was expected", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("integer value was expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 4; lineNum += 4;
markersToCheck.add(new MarkerToCheck("Constant must be initialized", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Constant must be initialized", lineNum, IMarker.SEVERITY_ERROR));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment