Commit 35234188 authored by Miklos Magyari's avatar Miklos Magyari Committed by Adam Knapp
Browse files

OOP: allowing constructor override in subclasses (issue #427)


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent bb8261cc
......@@ -16,7 +16,7 @@
*
******************************************************************************/
module Sem_5010104_finalClasses_001 language "TTCN-3:2018 Object-Oriented" {
module oopPositive language "TTCN-3:2018 Object-Oriented" {
type component CT {
port PT pt_PT;
......@@ -106,6 +106,20 @@ type class @trait AbstractMembersClass {
function @abstract f3(in integer pl_int) return charstring;
}
type class ConstructorOverride {
var integer m_int := 0;
create (integer pl_int) {
m_int := pl_int;
}
}
type class ConstructorOverrideSub {
var float m_float;
create (float pl_float) : ConstructorOverride(1) {
m_float := pl_float;
}
}
// nested classes
type class OuterClass {
private var integer m_int := 0;
......
......@@ -305,7 +305,9 @@ public final class Def_Function extends Definition implements IParameterisedAssi
if (portReference != null) {
portReference.setMyScope(bridgeScope);
}
formalParList.setMyScope(bridgeScope);
if (formalParList != null) {
formalParList.setMyScope(bridgeScope);
}
if (returnType != null) {
returnType.setMyScope(bridgeScope);
}
......@@ -315,7 +317,9 @@ public final class Def_Function extends Definition implements IParameterisedAssi
if (block != null) {
bridgeScope.addSubScope(block.getLocation(), block);
}
bridgeScope.addSubScope(formalParList.getLocation(), formalParList);
if (formalParList != null) {
bridgeScope.addSubScope(formalParList.getLocation(), formalParList);
}
}
@Override
......
......@@ -244,10 +244,12 @@ public final class ClassTypeBody extends TTCN3Scope implements IReferenceChainEl
final Definition localDefinition = definitions.getDefinition(name);
final VisibilityModifier modifier = definition.getVisibilityModifier();
if (modifier == VisibilityModifier.Public || modifier == VisibilityModifier.Protected) {
if (definition instanceof Def_Function && localDefinition instanceof Def_Function) {
if (! ((Def_Function)definition).getFormalParameterList().isSame(timestamp, ((Def_Function)localDefinition).getFormalParameterList())) {
localDefinition.getIdentifier().getLocation().reportSemanticError(FORMALPARAMSDIFFER);
definition.getIdentifier().getLocation().reportSemanticError(OVERRIDDENFORMALPARAM);
if (definition instanceof Def_Function && localDefinition instanceof Def_Function ) {
if (! isConstructor(definition)) {
if (! ((Def_Function)definition).getFormalParameterList().isSame(timestamp, ((Def_Function)localDefinition).getFormalParameterList())) {
localDefinition.getIdentifier().getLocation().reportSemanticError(FORMALPARAMSDIFFER);
definition.getIdentifier().getLocation().reportSemanticError(OVERRIDDENFORMALPARAM);
}
}
} else {
localDefinition.getIdentifier().getLocation().reportSemanticError(MessageFormat.format(
......@@ -355,7 +357,7 @@ public final class ClassTypeBody extends TTCN3Scope implements IReferenceChainEl
for (Definition def : definitions) {
if (def instanceof Def_Function) {
final Def_Function funcDef = (Def_Function)def;
if (funcDef.getIdentifier().getName().equals("create")) {
if (isConstructor(funcDef)) {
def.getLocation().reportSingularSemanticError(TRAITCONSTRUCTOR);
myType.setIsErroneous(true);
}
......@@ -371,6 +373,13 @@ public final class ClassTypeBody extends TTCN3Scope implements IReferenceChainEl
}
}
private boolean isConstructor(Definition defFunction) {
if (! (defFunction instanceof Def_Function))
return false;
return defFunction.getIdentifier().getName().equals("create");
}
public Identifier getIdentifier() {
return identifier;
}
......
......@@ -9229,12 +9229,11 @@ pr_ClassConstructorDef returns[Def_Function def_func]
StatementBlock statementBlock = null;
}:
( id = pr_CreateKeyword
LPAREN
pstart = pl_LParen
( pl = pr_FunctionFormalParList {
parameters = $pl.parList;
parameters.setLocation(getLocation( $pl.start, $pl.stop));
})?
RPAREN
pstop = pl_RParen
(
pr_ExtKeyword LPAREN
pr_FunctionFormalParList
......@@ -9253,6 +9252,8 @@ pr_ClassConstructorDef returns[Def_Function def_func]
( sb=pr_StatementBlock { statementBlock = $sb.statementblock; } )?
)
{
if(parameters == null) { parameters = new FormalParameterList(new ArrayList<FormalParameter>()); }
parameters.setLocation(getLocation( $pstart.start, $pstop.stop));
Identifier id = new Identifier( Identifier_type.ID_TTCN, "create", getLocation($id.start, $id.stop ) );
$def_func = new Def_Function(id, parameters, null, null, null, null, null, false, null, statementBlock);
$def_func.setLocation(getLocation( $id.start, $sb.stop));
......
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