GitLab will be shut down on June 25 to move to a new facility. https://www.eclipsestatus.io/incidents/5ffy27gwcbx7

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

OOP: implicit constructor default values; explicit constructor checks; constructor tests


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 877be60a
...@@ -374,6 +374,17 @@ type class BadClauses { ...@@ -374,6 +374,17 @@ type class BadClauses {
} }
} }
// class with implicit constructor
type class ImplicitConstructorClass {
var integer m_int1;
var charstring m_str1 := "abc";
const float m_float1;
const integer m_int2 := 1; // should not be included in the constructor param list
var template integer vt_int3;
var integer @property m_prop1;
var @internal integer @property m_prop2; // should not be included in the constructor param list
}
// invalid: 'value' can only be used in properties (and in some cases of advanced matching) // invalid: 'value' can only be used in properties (and in some cases of advanced matching)
function f_invalid_value() { function f_invalid_value() {
var integer vl_x := value; var integer vl_x := value;
...@@ -448,7 +459,7 @@ function f_with_exception(integer pl_int) return integer exception(integer) { ...@@ -448,7 +459,7 @@ function f_with_exception(integer pl_int) return integer exception(integer) {
} }
testcase tc_basicSyntax() runs on CT { testcase tc_basicSyntax() runs on CT {
var GrandSubClass vl_gsc := GrandSubClass.create; var GrandSubClass vl_gsc := GrandSubClass.create(1, { a := 10 });
var integer vl_int1 := vl_gsc.vl_c; // correct : own class member var integer vl_int1 := vl_gsc.vl_c; // correct : own class member
var integer vl_int2 := vl_gsc.vl_b; // correct : inherited from direct parent var integer vl_int2 := vl_gsc.vl_b; // correct : inherited from direct parent
...@@ -457,7 +468,7 @@ testcase tc_basicSyntax() runs on CT { ...@@ -457,7 +468,7 @@ testcase tc_basicSyntax() runs on CT {
var charstring vl_int4 := vl_gsc.vl_d; // incorrect : nonexisting member var charstring vl_int4 := vl_gsc.vl_d; // incorrect : nonexisting member
var charstring vl_int5 := vl_gsc.vl_a; // incorrect : type mismatch var charstring vl_int5 := vl_gsc.vl_a; // incorrect : type mismatch
var OuterClass vl_outer := OuterClass.create; var OuterClass vl_outer := OuterClass.create(1);
var OuterClass vl_outer2; var OuterClass vl_outer2;
if (vl_outer of OuterClass) { if (vl_outer of OuterClass) {
vl_outer := vl_outer2; vl_outer := vl_outer2;
...@@ -473,13 +484,17 @@ testcase tc_basicSyntax() runs on CT { ...@@ -473,13 +484,17 @@ testcase tc_basicSyntax() runs on CT {
var object vl_trait := MinimalTraitClass.create; var object vl_trait := MinimalTraitClass.create;
var MinimalAbstractClass vl_abstract := MinimalAbstractClass.create; var MinimalAbstractClass vl_abstract := MinimalAbstractClass.create;
var SubFunctionClass vl_sub := SubFunctionClass.create; var SubFunctionClass vl_sub := SubFunctionClass.create(0);
var integer vl_subint := vl_sub.m_const; var integer vl_subint := vl_sub.m_const;
var PropertyClass vl_prop := PropertyClass.create; var PropertyClass vl_prop := PropertyClass.create(0, 1, "2", 3, "4", 5.0, "6", 7, 8.0, 9);
// invalid: cannot assign a value to a property that has no setter // invalid: cannot assign a value to a property that has no setter
vl_prop.nosetter := 10; vl_prop.nosetter := 10;
// implicit constructor use
var ImplicitConstructorClass vl_impl := ImplicitConstructorClass.create(1, "abc", 3.14); // too few params
var ImplicitConstructorClass vl_impl2 := ImplicitConstructorClass.create(1, "xyz", 3333, 2, 5); // type mismatch
} }
} }
\ No newline at end of file
...@@ -97,6 +97,16 @@ type class MembersClass { ...@@ -97,6 +97,16 @@ type class MembersClass {
} }
} }
type class ImplicitConstructorClass {
var integer m_int1;
var charstring m_str1 := "abc";
const float m_float1;
const integer m_int2 := 1; // should not be included in the constructor param list
var template integer vt_int3;
var integer @property m_prop1;
var @internal integer @property m_prop2; // should not be included in the constructor param list
}
type class @trait TraitClass { type class @trait TraitClass {
public function @abstract f_func(in integer pl_in); public function @abstract f_func(in integer pl_in);
} }
...@@ -433,7 +443,7 @@ testcase tc_basicSyntax() runs on CT { ...@@ -433,7 +443,7 @@ testcase tc_basicSyntax() runs on CT {
vl_sub5.f_get_all_sum(); vl_sub5.f_get_all_sum();
// class of-operator // class of-operator
var OuterClass vl_outerClass := OuterClass.create; var OuterClass vl_outerClass := OuterClass.create(0, "0");
if (vl_outerClass of OuterClass) { if (vl_outerClass of OuterClass) {
vl_outerClass.m_string := "xyz"; vl_outerClass.m_string := "xyz";
} }
...@@ -446,6 +456,9 @@ testcase tc_basicSyntax() runs on CT { ...@@ -446,6 +456,9 @@ testcase tc_basicSyntax() runs on CT {
// nested class member reference // nested class member reference
var float vl_float := vl_outerClass.NestedClass.DeeplyNested.EvenDeeper.Core.f_inside(1); var float vl_float := vl_outerClass.NestedClass.DeeplyNested.EvenDeeper.Core.f_inside(1);
// implicit constructor use
var ImplicitConstructorClass vl_impl := ImplicitConstructorClass.create(1, "xyz", 3.14, 2, 5);
} }
control { control {
......
...@@ -24,7 +24,6 @@ import org.eclipse.titan.designer.AST.INamedNode; ...@@ -24,7 +24,6 @@ import org.eclipse.titan.designer.AST.INamedNode;
import org.eclipse.titan.designer.AST.IReferenceChain; import org.eclipse.titan.designer.AST.IReferenceChain;
import org.eclipse.titan.designer.AST.ISubReference; import org.eclipse.titan.designer.AST.ISubReference;
import org.eclipse.titan.designer.AST.IType; import org.eclipse.titan.designer.AST.IType;
import org.eclipse.titan.designer.AST.IType.Type_type;
import org.eclipse.titan.designer.AST.IValue; import org.eclipse.titan.designer.AST.IValue;
import org.eclipse.titan.designer.AST.Identifier; import org.eclipse.titan.designer.AST.Identifier;
import org.eclipse.titan.designer.AST.Location; import org.eclipse.titan.designer.AST.Location;
......
...@@ -29,6 +29,7 @@ import org.eclipse.titan.designer.AST.Scope; ...@@ -29,6 +29,7 @@ import org.eclipse.titan.designer.AST.Scope;
import org.eclipse.titan.designer.AST.Type; import org.eclipse.titan.designer.AST.Type;
import org.eclipse.titan.designer.AST.TypeCompatibilityInfo; import org.eclipse.titan.designer.AST.TypeCompatibilityInfo;
import org.eclipse.titan.designer.AST.TypeCompatibilityInfo.Chain; import org.eclipse.titan.designer.AST.TypeCompatibilityInfo.Chain;
import org.eclipse.titan.designer.AST.Value;
import org.eclipse.titan.designer.AST.TTCN3.Expected_Value_type; import org.eclipse.titan.designer.AST.TTCN3.Expected_Value_type;
import org.eclipse.titan.designer.AST.TTCN3.TemplateRestriction.Restriction_type; import org.eclipse.titan.designer.AST.TTCN3.TemplateRestriction.Restriction_type;
import org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Const; import org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Const;
...@@ -44,6 +45,8 @@ import org.eclipse.titan.designer.AST.TTCN3.statements.Assignment_Statement; ...@@ -44,6 +45,8 @@ import org.eclipse.titan.designer.AST.TTCN3.statements.Assignment_Statement;
import org.eclipse.titan.designer.AST.TTCN3.statements.StatementBlock; import org.eclipse.titan.designer.AST.TTCN3.statements.StatementBlock;
import org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template; import org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template;
import org.eclipse.titan.designer.AST.TTCN3.templates.SpecificValue_Template; import org.eclipse.titan.designer.AST.TTCN3.templates.SpecificValue_Template;
import org.eclipse.titan.designer.AST.TTCN3.templates.TTCN3Template;
import org.eclipse.titan.designer.AST.TTCN3.templates.TemplateInstance;
import org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value; import org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value;
import org.eclipse.titan.designer.compiler.JavaGenData; import org.eclipse.titan.designer.compiler.JavaGenData;
import org.eclipse.titan.designer.parsers.CompilationTimeStamp; import org.eclipse.titan.designer.parsers.CompilationTimeStamp;
...@@ -280,6 +283,7 @@ public final class Class_Type extends Type implements ITypeWithComponents { ...@@ -280,6 +283,7 @@ public final class Class_Type extends Type implements ITypeWithComponents {
if (def instanceof Def_Var || def instanceof Def_Var_Template || if (def instanceof Def_Var || def instanceof Def_Var_Template ||
def instanceof Def_Const || def instanceof Def_Template) { def instanceof Def_Const || def instanceof Def_Template) {
boolean isTemplate = false; boolean isTemplate = false;
Value defval = null;
if (def instanceof Def_Template || def instanceof Def_Var_Template) { if (def instanceof Def_Template || def instanceof Def_Var_Template) {
isTemplate = true; isTemplate = true;
} }
...@@ -295,6 +299,20 @@ public final class Class_Type extends Type implements ITypeWithComponents { ...@@ -295,6 +299,20 @@ public final class Class_Type extends Type implements ITypeWithComponents {
continue; continue;
} }
} }
TTCN3Template template = null;
if (def instanceof Def_Var_Template) {
template = ((Def_Var_Template)def).getInitialValue();
}
if (def instanceof Def_Var) {
defval = ((Def_Var) def).getInitialValue();
template = new SpecificValue_Template(defval);
}
TemplateInstance instance = null;
if (template != null) {
instance = new TemplateInstance(null, null, template);
}
final IType deftype = def.getType(timestamp); final IType deftype = def.getType(timestamp);
if (deftype instanceof Property_Type) { if (deftype instanceof Property_Type) {
if (((Property_Type)deftype).isInternal()) { if (((Property_Type)deftype).isInternal()) {
...@@ -307,7 +325,8 @@ public final class Class_Type extends Type implements ITypeWithComponents { ...@@ -307,7 +325,8 @@ public final class Class_Type extends Type implements ITypeWithComponents {
} }
final FormalParameter fp = new FormalParameter(Restriction_type.TR_NONE, final FormalParameter fp = new FormalParameter(Restriction_type.TR_NONE,
isTemplate ? Assignment_type.A_PAR_TEMP_IN : Assignment_type.A_PAR_VAL_IN, isTemplate ? Assignment_type.A_PAR_TEMP_IN : Assignment_type.A_PAR_VAL_IN,
(Type)def.getType(timestamp), new Identifier(Identifier_type.ID_TTCN, name), null, null); (Type)def.getType(timestamp), new Identifier(Identifier_type.ID_TTCN, name),
instance, null);
fpList.add(fp); fpList.add(fp);
Reference refLeft = new Reference(new Identifier(Identifier_type.ID_TTCN, def.getIdentifier().getName()), Ref_Type.REF_THIS); Reference refLeft = new Reference(new Identifier(Identifier_type.ID_TTCN, def.getIdentifier().getName()), Ref_Type.REF_THIS);
FieldSubReference subref = new FieldSubReference(def.getIdentifier()); FieldSubReference subref = new FieldSubReference(def.getIdentifier());
......
...@@ -4924,11 +4924,15 @@ pr_CreateOpEnd [Reference temporalReference] ...@@ -4924,11 +4924,15 @@ pr_CreateOpEnd [Reference temporalReference]
Value name = null; Value name = null;
Value location = null; Value location = null;
boolean isAlive = false; boolean isAlive = false;
boolean hasParen = false;
ParsedActualParameters params = new ParsedActualParameters(); ParsedActualParameters params = new ParsedActualParameters();
}: }:
( col = pr_Dot ( col = pr_Dot
a = pr_CreateKeyword { endcol = $a.stop; } a = pr_CreateKeyword {
( pr_LParen endcol = $a.stop;
params.setLocation(getLocation( $a.start, $a.stop ));
}
( lp = pr_LParen
( pr_NotUsedSymbol ( pr_NotUsedSymbol
pr_Comma pr_Comma
l = pr_SingleExpression { l = pr_SingleExpression {
...@@ -4940,7 +4944,7 @@ pr_CreateOpEnd [Reference temporalReference] ...@@ -4940,7 +4944,7 @@ pr_CreateOpEnd [Reference temporalReference]
template.setLocation(getLocation( $n.start, $n.stop)); template.setLocation(getLocation( $n.start, $n.stop));
TemplateInstance instance = new TemplateInstance(null, null, template); TemplateInstance instance = new TemplateInstance(null, null, template);
params.addUnnamedParameter(instance); params.addUnnamedParameter(instance);
params.setLocation(getLocation( $n.start, $n.stop)); params.setLocation(getLocation( $lp.start, $n.stop));
} }
( pr_Comma ( pr_Comma
l2 = pr_SingleExpression { l2 = pr_SingleExpression {
...@@ -4949,11 +4953,13 @@ pr_CreateOpEnd [Reference temporalReference] ...@@ -4949,11 +4953,13 @@ pr_CreateOpEnd [Reference temporalReference]
template2.setLocation(getLocation( $l2.start, $l2.stop)); template2.setLocation(getLocation( $l2.start, $l2.stop));
TemplateInstance instance2 = new TemplateInstance(null, null, template2); TemplateInstance instance2 = new TemplateInstance(null, null, template2);
params.addUnnamedParameter(instance2); params.addUnnamedParameter(instance2);
params.setLocation(getLocation( $n.start, $l2.stop)); params.setLocation(getLocation( $lp.start, $l2.stop));
} }
)? )?
) )
b = pr_RParen { endcol = $b.stop; } b = pr_RParen {
endcol = $b.stop;
}
)? )?
( c = ALIVE { endcol = $c; isAlive = true; } )? ( c = ALIVE { endcol = $c; isAlive = true; } )?
{ {
...@@ -4962,15 +4968,18 @@ pr_CreateOpEnd [Reference temporalReference] ...@@ -4962,15 +4968,18 @@ pr_CreateOpEnd [Reference temporalReference]
} }
| col2 = pr_Dot | col2 = pr_Dot
a2 = pr_CreateKeyword { endcol = $a2.stop; } a2 = pr_CreateKeyword { endcol = $a2.stop; }
( pr_LParen ( lp2 = pr_LParen
( (
par = pr_FunctionActualParList { par = pr_FunctionActualParList {
params = $par.parsedParameters; params = $par.parsedParameters;
params.setLocation(getLocation($par.start, $par.stop)); params.setLocation(getLocation($par.start, $par.stop));
} }
)? )?
b2 = pr_RParen { endcol = $b2.stop; } b2 = pr_RParen {
{ endcol = $b2.stop;
if (params.getInstances().getNofTis() == 0) {
params.setLocation(getLocation( $lp2.start, $b2.stop ));
}
$value = new UndefCreateExpression($temporalReference, params); $value = new UndefCreateExpression($temporalReference, params);
$value.setLocation( getLocation( $temporalReference.getLocation(), endcol ) ); $value.setLocation( getLocation( $temporalReference.getLocation(), endcol ) );
} }
......
...@@ -179,7 +179,7 @@ public class OOP_Semantic_tests { ...@@ -179,7 +179,7 @@ public class OOP_Semantic_tests {
private ArrayList<MarkerToCheck> oopNegative_ttcn_initializer() { private ArrayList<MarkerToCheck> oopNegative_ttcn_initializer() {
//oopNegativeSemanticTest.ttcn //oopNegativeSemanticTest.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(93); ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(96);
int lineNum = 33; int lineNum = 33;
markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 10; lineNum += 10;
...@@ -310,7 +310,7 @@ public class OOP_Semantic_tests { ...@@ -310,7 +310,7 @@ public class OOP_Semantic_tests {
markersToCheck.add(new MarkerToCheck("Class functions cannot have a `runs on` clause", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Class functions cannot have a `runs on` clause", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("Class functions cannot have a `system` clause", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Class functions cannot have a `system` clause", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("Class functions cannot have an `mtc` clause", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Class functions cannot have an `mtc` clause", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 7; lineNum += 18;
markersToCheck.add(new MarkerToCheck("Invalid `value` reference", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Invalid `value` reference", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 11; lineNum += 11;
markersToCheck.add(new MarkerToCheck("Type mismatch: a value of type `charstring' was expected instead of `integer'", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Type mismatch: a value of type `charstring' was expected instead of `integer'", lineNum, IMarker.SEVERITY_ERROR));
...@@ -340,8 +340,13 @@ public class OOP_Semantic_tests { ...@@ -340,8 +340,13 @@ public class OOP_Semantic_tests {
markersToCheck.add(new MarkerToCheck("An abstract class cannot be instantiated", ++lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("An abstract class cannot be instantiated", ++lineNum, IMarker.SEVERITY_ERROR));
lineNum += 3; lineNum += 3;
markersToCheck.add(new MarkerToCheck("Private member is inaccessible due to its protection level", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Private member is inaccessible due to its protection level", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 5; lineNum += 2;
markersToCheck.add(new MarkerToCheck("Character string value was expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 3;
markersToCheck.add(new MarkerToCheck("Cannot assign a value to a property without a setter", lineNum, IMarker.SEVERITY_ERROR)); markersToCheck.add(new MarkerToCheck("Cannot assign a value to a property without a setter", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 3;
markersToCheck.add(new MarkerToCheck("Too few parameters: at least 4 was expected instaed of 3", lineNum, IMarker.SEVERITY_ERROR));
markersToCheck.add(new MarkerToCheck("float value was expected", ++lineNum, IMarker.SEVERITY_ERROR));
return markersToCheck; return markersToCheck;
} }
......
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