Commit 9560a988 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

OOP: semantic check - field of any visibility cannot be overridden (issue #427)


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 187bbb40
......@@ -298,6 +298,16 @@ type class ClassWithPrivate {
}
}
type class ClassOverride1 {
private var charstring vl_chr;
var float vl_float;
}
type class ClassOverride2 extends ClassOverride1 {
var charstring vl_chr;
var float vl_float;
}
type class ClassWithPrivateExt extends ClassWithPrivate {
public function f_override(charstring pl_chr) return charstring {
......
/*****************************************************************
** @author STF 572
** @version 0.0.1
** @purpose 5.1.1.9, Ensure that field of any visibility cannot be overridden by a subclass.
** @verdict pass reject
**
** modified by Miklos Magyari
*****************************************************************/
module NegSem_5010109_Visibility_002 "TTCN-3:2018 Object-Oriented" {
type component GeneralComp {
}
type charstring MyType;
type class MySuperClass {
private var MyType v_my;
}
type class MySubClass extends MySuperClass {
var MyType v_my; //not allowed
}
testcase tc_NegSem_5010109_Visibility_002() runs on GeneralComp {
var MySubClass vl_a := MySubClass.create("abc");
if (vl_a.v_my == "abc") {
setverdict(pass);
} else {
setverdict(fail);
}
}
control {
execute(tc_NegSem_5010109_Visibility_002());
}
}
\ No newline at end of file
......@@ -63,6 +63,7 @@ public final class ClassTypeBody extends TTCN3Scope implements IReferenceChainEl
public static final String TRAITCONSTRUCTOR = "Trait classes cannot have a constructor";
public static final String TRAITMETHODABSTRACT = "Trait classes can only declare abstract methods";
public static final String ABSTRACTMETHODHASBODY = "Abstract method cannot have a function body";
public static final String FIELDNOOVERRIDE = "A field of any visibility cannot be overriden by a subclass";
private final Identifier identifier;
/** The class's own definitions */
......@@ -247,7 +248,11 @@ public final class ClassTypeBody extends TTCN3Scope implements IReferenceChainEl
final String name = definition.getIdentifier().getName();
if (definitions.hasDefinition(name)) {
final Definition localDefinition = definitions.getDefinition(name);
final VisibilityModifier modifier = definition.getVisibilityModifier();
final VisibilityModifier modifier = definition.getVisibilityModifier();
if (!(definition instanceof Def_Function)) {
localDefinition.getLocation().reportSemanticError(FIELDNOOVERRIDE);
continue;
}
if (modifier == VisibilityModifier.Public || modifier == VisibilityModifier.Protected) {
if (definition instanceof Def_Function && localDefinition instanceof Def_Function ) {
if (! isConstructor(definition)) {
......
......@@ -65,6 +65,9 @@ public class OOP_Semantic_tests {
Designer_plugin_tests.checkSemanticMarkersOnFile(NegSem_5010109_Visibility_001_ttcn_initializer(),
OOP_Syntax_tests.OOP_DIR_PATH + "negative/NegSem_5010109_Visibility_001.ttcn");
Designer_plugin_tests.checkSemanticMarkersOnFile(NegSem_5010109_Visibility_002_ttcn_initializer(),
OOP_Syntax_tests.OOP_DIR_PATH + "negative/NegSem_5010109_Visibility_002.ttcn");
Designer_plugin_tests.checkSemanticMarkersOnFile(NegSem_5010109_Visibility_004_ttcn_initializer(),
OOP_Syntax_tests.OOP_DIR_PATH + "negative/NegSem_5010109_Visibility_004.ttcn");
}
......@@ -141,6 +144,15 @@ public class OOP_Semantic_tests {
return markersToCheck;
}
private ArrayList<MarkerToCheck> NegSem_5010109_Visibility_002_ttcn_initializer() {
//NegSem_5010109_Visibility_002.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(1);
int lineNum = 21;
markersToCheck.add(new MarkerToCheck("A field of any visibility cannot be overriden by a subclass", lineNum, IMarker.SEVERITY_ERROR));
return markersToCheck;
}
private ArrayList<MarkerToCheck> NegSem_5010109_Visibility_004_ttcn_initializer() {
//NegSem_5010109_Visibility_004.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(1);
......@@ -152,7 +164,7 @@ public class OOP_Semantic_tests {
private ArrayList<MarkerToCheck> oopNegative_ttcn_initializer() {
//oopNegativeSemanticTest.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(79);
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(81);
int lineNum = 32;
markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 10;
......@@ -206,7 +218,7 @@ public class OOP_Semantic_tests {
lineNum += 5;
markersToCheck.add(new MarkerToCheck("Reference to non-existent field `nonexist' in union template for type `@classesNegativeSemantic.MyUnion'", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 4;
markersToCheck.add(new MarkerToCheck("Local Definiton `vl_b' collides with definition inherited from class type `@classesNegativeSemantic.SubClass'", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("A field of any visibility cannot be overriden by a subclass", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 11;
for (i = 0; i < 2; i++) {
markersToCheck.add(new MarkerToCheck("All formal parameters of a constructor shall be `in` parameters", lineNum, IMarker.SEVERITY_ERROR));
......@@ -255,6 +267,10 @@ public class OOP_Semantic_tests {
lineNum += 19;
markersToCheck.add(new MarkerToCheck("Definition is overridden with different formal parameters", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 19;
for (i = 0; i < 2; i++) {
markersToCheck.add(new MarkerToCheck("A field of any visibility cannot be overriden by a subclass", lineNum++, IMarker.SEVERITY_ERROR));
}
lineNum += 8;
markersToCheck.add(new MarkerToCheck("Type mismatch: a value of type `charstring' was expected instead of `float'", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 3;
markersToCheck.add(new MarkerToCheck("Formal parameter list differs from previous definition", lineNum, IMarker.SEVERITY_ERROR));
......
......@@ -48,6 +48,7 @@ public class OOP_Syntax_tests {
"NegSem_50101_top_level_011.ttcn",
"NegSem_5010102_abstractClasses_001.ttcn",
"NegSem_5010109_Visibility_001.ttcn",
"NegSem_5010109_Visibility_002.ttcn",
"NegSem_5010109_Visibility_004.ttcn"
);
......
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