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

OOP: improved handling for class finally blocks (issue #417)


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent c76911a6
......@@ -86,6 +86,20 @@ type class SubFunctionClass extends BaseFunctionClass {
}
}
// an abstract class can have a finally block
type class @abstract FinallyClass {
public const integer m_const := 0;
} finally {
log(this);
}
// a trait class cannot have a finally block
type class @trait FinallyClass2 {
public const integer m_const := 0;
} finally {
log(this);
}
// bad combination of class modifiers
type class @final @abstract BadClass1 { }
type class @final @trait BadClass3 { }
......
......@@ -117,6 +117,13 @@ type class OuterClass {
}
}
// finally block
type class @abstract FinallyClass {
public const integer m_const := 0;
} finally {
log(this);
}
// `this` reference
type class ClassWithThis {
public var integer vl_a;
......
......@@ -31,6 +31,7 @@ import org.eclipse.titan.designer.AST.Type;
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.statements.StatementBlock;
import org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template;
import org.eclipse.titan.designer.compiler.JavaGenData;
import org.eclipse.titan.designer.parsers.CompilationTimeStamp;
......@@ -39,7 +40,8 @@ import org.eclipse.titan.designer.parsers.CompilationTimeStamp;
public final class Class_Type extends Type implements ITypeWithComponents {
private static final String ABSTRACTCANNOTBEFINAL = "An absract class cannot be final";
private static final String TRAITCANNOTBEFINAL = "A trait class cannot be final";
private static final String ABSTRACTCANNOTBETRAIT = "An class cannot be both abstract and trait";
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 final ClassTypeBody classBody;
......@@ -47,16 +49,23 @@ public final class Class_Type extends Type implements ITypeWithComponents {
private final boolean isFinal;
private final boolean isTrait;
private final Location modifierLocation;
private final StatementBlock finallyBlock;
public Class_Type(ClassTypeBody classBody, boolean isAbstract, boolean isFinal, boolean isTrait, Location modifierLocation) {
public Class_Type(ClassTypeBody classBody, boolean isAbstract, boolean isFinal, boolean isTrait, Location modifierLocation, StatementBlock finallyBlock) {
this.classBody = classBody;
this.isAbstract = isAbstract;
this.isFinal = isFinal;
this.isTrait = isTrait;
this.modifierLocation = modifierLocation;
this.finallyBlock = finallyBlock;
classBody.setFullNameParent(this);
classBody.setMyType(this);
if (classBody != null) {
classBody.setFullNameParent(this);
classBody.setMyType(this);
}
if (finallyBlock != null) {
finallyBlock.setFullNameParent(this);
}
}
@Override
......@@ -132,6 +141,10 @@ public final class Class_Type extends Type implements ITypeWithComponents {
modifierLocation.reportSemanticError(ABSTRACTCANNOTBETRAIT);
setIsErroneous(true);
}
if (isTrait && finallyBlock != null) {
finallyBlock.getLocation().reportSemanticError(TRAITWITHFINALLY);
}
initAttributes(timestamp);
......
......@@ -8974,6 +8974,7 @@ pr_ClassTypeDef returns[Def_Type def_type]
boolean isAbstract = false;
boolean isFinal = false;
boolean isTrait = false;
StatementBlock finallyBlock = null;
}:
( ( ext = pr_ExtKeyword )?
kw = pr_ClassKeyword
......@@ -8988,14 +8989,16 @@ pr_ClassTypeDef returns[Def_Type def_type]
pr_BeginChar
cml = pr_ClassMemberList[$i.identifier, refs] { body = $cml.body; }
pr_EndChar
fd = pr_FinallyDef
fd = pr_FinallyDef {
finallyBlock = $fd.statementBlock;
}
)
{
if ($i.identifier != null) {
if (body == null) {
body = new ClassTypeBody($i.identifier, refs);
}
Type type = new Class_Type(body, isAbstract, isFinal, isTrait, getLocation($kw.stop, $i.start));
Type type = new Class_Type(body, isAbstract, isFinal, isTrait, getLocation($kw.stop, $i.start), finallyBlock);
type.setLocation(getLocation($start, getLastVisibleToken()));
$def_type = new Def_Type($i.identifier, type);
$def_type.setLocation(getLocation($kw.start, $i.stop));
......@@ -9133,10 +9136,15 @@ pr_OopVisibility returns[VisibilityModifier visibility]
| PUBLIC { $visibility = VisibilityModifier.Public; }
);
pr_FinallyDef
:
pr_FinallyDef returns[StatementBlock statementBlock]
@init {
$statementBlock = null;
}:
(
FINALLY sb=pr_StatementBlock
FINALLY sb=pr_StatementBlock {
$statementBlock = $sb.statementblock;
$statementBlock.setLocation(getLocation($sb.start, $sb.stop));
}
)?;
pr_ClassFunctionDef returns[Def_Function def_func]
......
......@@ -45,7 +45,7 @@ public class OOP_Semantic_tests {
private ArrayList<MarkerToCheck> oopNegative_ttcn_initializer() {
//oopNegativeSemanticTest.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(12);
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(13);
int lineNum = 28;
markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 15;
......@@ -58,7 +58,9 @@ public class OOP_Semantic_tests {
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 += 11;
lineNum += 20;
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));
......
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