Commit acbc9aad authored by Miklos Magyari's avatar Miklos Magyari
Browse files

OOP: semantic check - field visibility must be private or protected (issue #427)


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 529aa61f
......@@ -79,7 +79,7 @@ type class InheritedAbstractMethods extends AbstractMethods {
type class MembersClass {
private var integer m_int_priv := 1;
var integer m_int_prot := 2;
public var integer m_int_pub := 3;
var integer m_int_pub := 3;
public function f_get1() return integer {
return m_int_priv;
......@@ -107,8 +107,8 @@ type union MyUnion {
type class VariousMembers {
private timer Timer;
private const octetstring m_var_octet := 'ABCD'O;
public var charstring m_var1 := "abc";
public template MyUnion t_union := { choice1 := 12 }
var charstring m_var1 := "abc";
private template MyUnion t_union := { choice1 := 12 }
template MyUnion t_union2;
create (charstring pl_p1, charstring pl_p2) {
......@@ -139,7 +139,7 @@ type class ConstructorOverrideSub {
// nested classes
type class OuterClass {
private var integer m_int := 0;
public var charstring m_string := "abc";
private var charstring m_string := "abc";
private class NestedClass {
private function f_dummy(in integer pl_param) return integer {
......@@ -150,7 +150,7 @@ type class OuterClass {
const integer m_const := 0;
class EvenDeeper {
const charstring m_str := "abc";
public class Core {
class Core {
function f_inside(in integer pl_int) return float {
return 0.1;
}
......@@ -162,14 +162,14 @@ type class OuterClass {
// finally block
type class @abstract FinallyClass {
public const integer m_const := 0;
private const integer m_const := 0;
} finally {
log(this.m_const);
}
// `this` reference
type class ClassWithThis {
public var integer vl_a;
private var integer vl_a;
const float vl_b;
public function pl_dummy(in integer pl_int) return integer {
......
/*****************************************************************
** @author STF 572
** @version 0.0.1
** @purpose 5.1.1.9, Ensure that fields can be declared as private or protected only.
** @verdict pass reject
**
** modified by Miklos Magyari
*****************************************************************/
module NegSem_5010109_Visibility_001 "TTCN-3:2018 Object-Oriented" {
type component GeneralComp {
}
type class MyClass {
public var integer v_i; // not allowed
}
testcase tc_NegSem_5010109_Visibility_001() runs on GeneralComp {
var MyClass vl_a := MyClass.create(1);
if (vl_a.v_i == 1) {
setverdict(pass);
} else {
setverdict(fail);
}
}
control {
execute(tc_NegSem_5010109_Visibility_001());
}
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ module Sem_5010105_Constructors_002 "TTCN-3:2018 Object-Oriented" {
}
type class MySuperClass {
public var octetstring v_o;
private var octetstring v_o;
create(octetstring pl_o) {
this.v_o := pl_o;
......@@ -24,7 +24,7 @@ module Sem_5010105_Constructors_002 "TTCN-3:2018 Object-Oriented" {
}
type class MySubClass extends MySuperClass {
public var template charstring vt_cstr;
private var template charstring vt_cstr;
create(template charstring pl_cstr) : MySuperClass('AA5600'O) {
this.vt_cstr := pl_cstr;
......
......@@ -57,7 +57,7 @@ public final class ClassTypeBody extends TTCN3Scope implements IReferenceChainEl
public static final String OVERRIDDENFORMALPARAM = "Definition is overridden with different formal parameters";
public static final String PUBLICOVERRIDEPUBLIC = "Public method can only be overriden by a public method";
public static final String PROTECTEDOVERRIDE = "Protected method can only be overriden by a public or protected method";
public static final String VISIBILITYPRIVATEPROTECTED = "Class field visibility must be private or protected";
public static final String TRAITMETHODSONLY = "Trait classes can only declare methods";
public static final String TRAITMETHODHASBODY = "Trait method cannot have a function body";
public static final String TRAITCONSTRUCTOR = "Trait classes cannot have a constructor";
......@@ -366,6 +366,15 @@ public final class ClassTypeBody extends TTCN3Scope implements IReferenceChainEl
definitions.checkAll(timestamp);
for (Definition def : definitions.getDefinitions()) {
if (! (def instanceof Def_Function)) {
final VisibilityModifier visibility = def.getVisibilityModifier();
if (visibility != VisibilityModifier.Private && visibility != VisibilityModifier.Protected) {
def.getLocation().reportSemanticError(VISIBILITYPRIVATEPROTECTED);
}
}
}
if (myType.isAbstract()) {
for (Definition def : definitions) {
if (def instanceof Def_Function) {
......
......@@ -58,6 +58,9 @@ public class OOP_Semantic_tests {
Designer_plugin_tests.checkSemanticMarkersOnFile(NegSem_5010102_abstractClasses_001_ttcn_initializer(),
OOP_Syntax_tests.OOP_DIR_PATH + "negative/NegSem_5010102_abstractClasses_001.ttcn");
Designer_plugin_tests.checkSemanticMarkersOnFile(NegSem_5010109_Visibility_001_ttcn_initializer(),
OOP_Syntax_tests.OOP_DIR_PATH + "negative/NegSem_5010109_Visibility_001.ttcn");
}
}
......@@ -103,9 +106,18 @@ public class OOP_Semantic_tests {
return markersToCheck;
}
private ArrayList<MarkerToCheck> NegSem_5010109_Visibility_001_ttcn_initializer() {
//test.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(1);
int lineNum = 15;
markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", lineNum, IMarker.SEVERITY_ERROR));
return markersToCheck;
}
private ArrayList<MarkerToCheck> oopNegative_ttcn_initializer() {
//oopNegativeSemanticTest.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(61);
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(71);
int lineNum = 28;
markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 10;
......@@ -132,10 +144,12 @@ public class OOP_Semantic_tests {
markersToCheck.add(new MarkerToCheck("A class cannot extend itself neither directly nor indirectly", lineNum++, IMarker.SEVERITY_ERROR));
}
lineNum += 2;
for (i = 0; i < 2; i++) {
markersToCheck.add(new MarkerToCheck("Trait classes can only declare methods", lineNum++, IMarker.SEVERITY_ERROR));
}
markersToCheck.add(new MarkerToCheck("Trait classes cannot have a constructor", ++lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("Trait classes can only declare methods", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", ++lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("Trait classes can only declare methods", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 2;
markersToCheck.add(new MarkerToCheck("Trait classes cannot have a constructor", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 5;
markersToCheck.add(new MarkerToCheck("Trait classes can only declare abstract methods", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("Trait method cannot have a function body", ++lineNum, IMarker.SEVERITY_ERROR));
......@@ -146,7 +160,9 @@ public class OOP_Semantic_tests {
lineNum += 15;
markersToCheck.add(new MarkerToCheck("Class must implement abstract method `f__abs1' inherited from class `@classesNegativeSemantic.ClassWithAbstract4'", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("Class must implement abstract method `f__abs2' inherited from class `@classesNegativeSemantic.ClassWithAbstract4'", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 13;
lineNum += 8;
markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", lineNum, IMarker.SEVERITY_ERROR));
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));
......@@ -155,19 +171,31 @@ public class OOP_Semantic_tests {
markersToCheck.add(new MarkerToCheck("All formal parameters of a constructor shall be `in` parameters", lineNum, IMarker.SEVERITY_ERROR));
}
lineNum += 11;
markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("integer value was expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 21;
lineNum += 2;
for (i = 0; i < 2; i++) {
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;
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));
lineNum += 19;
lineNum += 12;
markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 7;
markersToCheck.add(new MarkerToCheck("Trait classes can only declare abstract methods", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("A trait class should not define a finally block", ++lineNum, IMarker.SEVERITY_ERROR));
lineNum += 5;
markersToCheck.add(new MarkerToCheck("An absract class cannot be final", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("A trait class cannot be final", ++lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("A class cannot be both abstract and trait", ++lineNum, IMarker.SEVERITY_ERROR));
lineNum += 12;
lineNum += 4;
for (i = 0; i < 2; i++) {
markersToCheck.add(new MarkerToCheck("Class field visibility must be private or protected", lineNum++, IMarker.SEVERITY_ERROR));
}
lineNum += 6;
markersToCheck.add(new MarkerToCheck("Type mismatch: a value of type `charstring' was expected instead of `integer'", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 5;
markersToCheck.add(new MarkerToCheck("Type mismatch: a value of type `integer' was expected instead of `float'", lineNum, IMarker.SEVERITY_ERROR));
......
......@@ -44,7 +44,8 @@ public class OOP_Syntax_tests {
"NegSem_50101_top_level_002.ttcn",
"NegSem_50101_top_level_007.ttcn",
"NegSem_50101_top_level_008.ttcn",
"NegSem_5010102_abstractClasses_001.ttcn"
"NegSem_5010102_abstractClasses_001.ttcn",
"NegSem_5010109_Visibility_001.ttcn"
);
private static boolean parseOOP;
......
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