Commit 2a6c8290 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

OOP: semantic check - visibility improvements (issue #427)


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent fb890bd1
......@@ -331,6 +331,9 @@ testcase tc_basicSyntax() runs on CT {
// instantiating abstract and trait classes is not allowed
var object vl_trait := MinimalTraitClass.create;
var MinimalAbstractClass vl_abstract := MinimalAbstractClass.create;
var SubFunctionClass vl_sub := SubFunctionClass.create;
var integer vl_subint := vl_sub.m_const;
}
}
\ No newline at end of file
......@@ -139,9 +139,9 @@ type class ConstructorOverrideSub {
// nested classes
type class OuterClass {
private var integer m_int := 0;
private var charstring m_string := "abc";
var charstring m_string := "abc";
private class NestedClass {
class NestedClass {
private function f_dummy(in integer pl_param) return integer {
return 1;
}
......@@ -169,7 +169,7 @@ type class @abstract FinallyClass {
// `this` reference
type class ClassWithThis {
private var integer vl_a;
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 private members can only be accessed directly from inside their surrounding class's scope.
** @verdict pass reject
**
** modified by Miklos Magyari
*****************************************************************/
module NegSem_5010109_Visibility_004 "TTCN-3:2018 Object-Oriented" {
type component GeneralComp {
}
type class MyClass {
private var integer v_i;
}
testcase tc_NegSem_5010109_Visibility_004() runs on GeneralComp {
var MyClass vl_a := MyClass.create(1);
if (vl_a.v_i == 1) { //not allowed
setverdict(pass);
} else {
setverdict(fail);
}
}
control {
execute(tc_NegSem_5010109_Visibility_004());
}
}
\ No newline at end of file
......@@ -175,7 +175,8 @@ public final class ClassTypeBody extends TTCN3Scope implements IReferenceChainEl
definition = extendsGainedDefinitions.get(name);
if (definition != null) {
if (VisibilityModifier.Public.equals(definition.getVisibilityModifier())) {
if (VisibilityModifier.Public.equals(definition.getVisibilityModifier()) ||
VisibilityModifier.Protected.equals(definition.getVisibilityModifier())) {
return definition;
}
......
......@@ -34,8 +34,10 @@ import org.eclipse.titan.designer.AST.TypeCompatibilityInfo;
import org.eclipse.titan.designer.AST.TypeCompatibilityInfo.Chain;
import org.eclipse.titan.designer.AST.TTCN3.Expected_Value_type;
import org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Function;
import org.eclipse.titan.designer.AST.TTCN3.definitions.Definition;
import org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameter;
import org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameterList;
import org.eclipse.titan.designer.AST.TTCN3.definitions.VisibilityModifier;
import org.eclipse.titan.designer.AST.TTCN3.statements.StatementBlock;
import org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template;
import org.eclipse.titan.designer.compiler.JavaGenData;
......@@ -52,6 +54,7 @@ public final class Class_Type extends Type implements ITypeWithComponents {
private static final String ABSTRACTCANNOTBETRAIT = "A class cannot be both abstract and trait";
private static final String TRAITWITHFINALLY = "A trait class should not define a finally block";
private static final String UNKNOWNFIELD = "Unknown field reference";
private static final String PRIVATEINACCESSIBLE = "Private member is inaccessible due to its protection level";
private final ClassTypeBody classBody;
......@@ -103,6 +106,13 @@ public final class Class_Type extends Type implements ITypeWithComponents {
super.setMyScope(scope);
if (classBody != null) {
classBody.setMyScope(scope);
if (finallyBlock != null) {
finallyBlock.setMyScope(classBody);
}
} else {
if (finallyBlock != null) {
finallyBlock.setMyScope(scope);
}
}
}
......@@ -143,6 +153,11 @@ public final class Class_Type extends Type implements ITypeWithComponents {
subreference.getLocation().reportSemanticError(UNKNOWNFIELD);
return null;
}
final Definition def = (Definition)assignment;
if (def.getVisibilityModifier() == VisibilityModifier.Private) {
subreference.getLocation().reportSemanticError(PRIVATEINACCESSIBLE);
return null;
}
if (subreferences.size() == actualSubReference + 1) {
return assignment.getType(timestamp);
......
......@@ -9,8 +9,6 @@
package org.eclipse.titan.regressiontests.designer.statictests.Basic_tests;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IMarker;
import org.eclipse.titan.designer.Activator;
import org.eclipse.titan.designer.preferences.PreferenceConstants;
......@@ -26,7 +24,6 @@ import org.junit.Test;
public class OOP_Semantic_tests {
private static final String DIR_PATH = "src/Basic_tests/";
private static final String OOP_NEGATIVE_BASIC_SEMANTIC = "/OopNegativeSemanticTest.ttcn";
private List<String> testFilesPositive = OOP_Syntax_tests.testFilesPositive;
private static boolean parseOOP;
......@@ -61,6 +58,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_004_ttcn_initializer(),
OOP_Syntax_tests.OOP_DIR_PATH + "negative/NegSem_5010109_Visibility_004.ttcn");
}
}
......@@ -107,7 +107,7 @@ public class OOP_Semantic_tests {
}
private ArrayList<MarkerToCheck> NegSem_5010109_Visibility_001_ttcn_initializer() {
//test.ttcn
//NegSem_5010109_Visibility_001.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));
......@@ -115,9 +115,18 @@ public class OOP_Semantic_tests {
return markersToCheck;
}
private ArrayList<MarkerToCheck> NegSem_5010109_Visibility_004_ttcn_initializer() {
//NegSem_5010109_Visibility_004.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(1);
int lineNum = 20;
markersToCheck.add(new MarkerToCheck("Private member is inaccessible due to its protection level", lineNum, IMarker.SEVERITY_ERROR));
return markersToCheck;
}
private ArrayList<MarkerToCheck> oopNegative_ttcn_initializer() {
//oopNegativeSemanticTest.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(72);
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(74);
int lineNum = 28;
markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 10;
......@@ -187,7 +196,8 @@ public class OOP_Semantic_tests {
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 += 8;
markersToCheck.add(new MarkerToCheck("There is no visible definition with name `m_const' in module `classesNegativeSemantic'", ++lineNum, IMarker.SEVERITY_ERROR));
lineNum += 7;
markersToCheck.add(new MarkerToCheck("Return statement cannot be used in a class destructor", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 4;
markersToCheck.add(new MarkerToCheck("An absract class cannot be final", lineNum, IMarker.SEVERITY_ERROR));
......@@ -238,6 +248,8 @@ public class OOP_Semantic_tests {
lineNum += 3;
markersToCheck.add(new MarkerToCheck("A trait class cannot be instantiated", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("An abstract class cannot be instantiated", ++lineNum, IMarker.SEVERITY_ERROR));
lineNum += 3;
markersToCheck.add(new MarkerToCheck("Private member is inaccessible due to its protection level", lineNum, IMarker.SEVERITY_ERROR));
return markersToCheck;
}
......
......@@ -45,7 +45,8 @@ public class OOP_Syntax_tests {
"NegSem_50101_top_level_007.ttcn",
"NegSem_50101_top_level_008.ttcn",
"NegSem_5010102_abstractClasses_001.ttcn",
"NegSem_5010109_Visibility_001.ttcn"
"NegSem_5010109_Visibility_001.ttcn",
"NegSem_5010109_Visibility_004.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