diff --git a/Semantic_Analizer_Tests/src/Basic_tests/OopNegativeSemanticTest.ttcn b/Semantic_Analizer_Tests/src/Basic_tests/OopNegativeSemanticTest.ttcn index 6b0103e7b423e9972c6ff3a2b9a96e300572a6f3..8b486e3284e7c001fa7f826d520ee3eaf1e88543 100755 --- a/Semantic_Analizer_Tests/src/Basic_tests/OopNegativeSemanticTest.ttcn +++ b/Semantic_Analizer_Tests/src/Basic_tests/OopNegativeSemanticTest.ttcn @@ -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 { var charstring vl_orig := "abc"; diff --git a/Semantic_Analizer_Tests/src/Basic_tests/OopPositiveBasicSyntax.ttcn b/Semantic_Analizer_Tests/src/Basic_tests/OopPositiveBasicSyntax.ttcn index c8b1cd0988777034e6abc52b2d7e190d6bd1f125..6cbbacdbdf4a034fa21399357e98b4f7403c7385 100644 --- a/Semantic_Analizer_Tests/src/Basic_tests/OopPositiveBasicSyntax.ttcn +++ b/Semantic_Analizer_Tests/src/Basic_tests/OopPositiveBasicSyntax.ttcn @@ -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 type class RunsonClass1 runs on CT { } type class RunsonClass2 extends RunsonClass1 runs on CT { } diff --git a/org.eclipse.titan.designer/src/org/eclipse/titan/designer/AST/TTCN3/statements/Assignment_Statement.java b/org.eclipse.titan.designer/src/org/eclipse/titan/designer/AST/TTCN3/statements/Assignment_Statement.java index 881c2d227960bbd6723cb67175f84dd331705f78..55d7314429b9b5eecc792efe83563def00431893 100644 --- a/org.eclipse.titan.designer/src/org/eclipse/titan/designer/AST/TTCN3/statements/Assignment_Statement.java +++ b/org.eclipse.titan.designer/src/org/eclipse/titan/designer/AST/TTCN3/statements/Assignment_Statement.java @@ -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 OMITTOMANDATORYASSIGNMENT2 = "Assignment of `omit'' to mandatory field `{0}'' of type `{1}''"; 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 TTCN3Template template; @@ -235,12 +238,40 @@ public final class Assignment_Statement extends Statement { ((Def_Var_Template) assignment).setWritten(); checkTemplateAssignment(timestamp, assignment,Expected_Value_type.EXPECTED_TEMPLATE,null); break; + case A_CONST: + final IValue temporalConstValue = template.getValue(); + if (temporalConstValue != null) { + checkConstAssignment(timestamp, assignment, temporalConstValue); + template.setMyGovernor(temporalConstValue.getMyGovernor()); + break; + } + break; default: reference.getLocation().reportSemanticError(MessageFormat.format(VARIABLEREFERENCEEXPECTED, assignment.getAssignmentName())); reference.setIsErroneous(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) { final IType varType = getType(timestamp, assignment); diff --git a/org.eclipse.titan.regressiontests/src/org/eclipse/titan/regressiontests/designer/statictests/Basic_tests/OOP_Semantic_tests.java b/org.eclipse.titan.regressiontests/src/org/eclipse/titan/regressiontests/designer/statictests/Basic_tests/OOP_Semantic_tests.java index dc53c78b67f8383e9718d65ea8d162971cab53c8..b584aecd113a6263c1808694dc34ba0846492a89 100755 --- a/org.eclipse.titan.regressiontests/src/org/eclipse/titan/regressiontests/designer/statictests/Basic_tests/OOP_Semantic_tests.java +++ b/org.eclipse.titan.regressiontests/src/org/eclipse/titan/regressiontests/designer/statictests/Basic_tests/OOP_Semantic_tests.java @@ -152,7 +152,7 @@ public class OOP_Semantic_tests { private ArrayList oopNegative_ttcn_initializer() { //oopNegativeSemanticTest.ttcn - ArrayList markersToCheck = new ArrayList(77); + ArrayList markersToCheck = new ArrayList(79); int lineNum = 32; markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR)); lineNum += 10; @@ -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)); - 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)); lineNum += 4; markersToCheck.add(new MarkerToCheck("Constant must be initialized", lineNum, IMarker.SEVERITY_ERROR));