/****************************************************************************** * Copyright (c) 2000-2021 Ericsson Telecom AB * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html * * Contributors: * Magyari, Miklos * ******************************************************************************/ module classesNegativeSemantic { type component CT { port PT pt_PT; } type port PT message { inout integer; } with { extension "internal" } type union MyUnion { integer a, charstring b }; type class CorrectClass1 { } type class BadInheritance extends MyUnion { } type class MinimalClass { } type class @trait BaseClass { } type class @abstract MinimalAbstractClass { } type class @final MinimalFinalClass { } type class @trait MinimalTraitClass { } type class BadExtends extends MinimalClass, MinimalAbstractClass, MinimalFinalClass, MinimalTraitClass { // dummy } type class @trait BadExtends2 extends MinimalClass, MinimalTraitClass { // dummy } type class BadExtends3 extends MinimalFinalClass { } type class RepeatedExtends extends MinimalClass, BaseClass, MinimalClass { } // self extend type class @trait TC {} type class AA extends DD, TC, DD { } type class BB extends AA { } type class CC extends BB { } type class DD extends CC { } type class EE extends EE { } type class @trait BadTraitClass { public var integer m_int := 0; public timer Timer; create (charstring pl_str) { var float vl_float := 1.0; } private function @abstract f_valid(integer pl_int); public function f_invalid_non_abstract(in charstring pl_cstr); private function @abstract f_invalid(integer pl_int) { // this should not be here } } type class SuperClass { var integer vl_a; template MyUnion t_union := { a := 10 } } type class SubClass extends SuperClass { public var integer vl_b; } type class GrandSubClass extends SubClass { var integer vl_c; template MyUnion t_union2 := { nonexist := 10 } // non-existing template member } type class GrandSubClass2 extends SubClass { var charstring vl_b; } type class BaseFunctionClass { public function f_get_one() return integer { return 1; } } type class OuterClass { private var integer m_int := 0; class NestedClass { const charstring c_text := "abc"; class InnerClass { private template MyUnion t_union; public var integer badInteger := "xyz"; public class EvenDeeper { public const charstring chr := "abc"; public class Core { public function f_inside(in integer pl_int) return charstring { return this.chr; } } } } } } type class ReuseClass { var charstring vl_orig := "abc"; function f_reuse_id(in integer pl_param) { var integer vl_orig := 1; vl_orig := 2; vl_orig := "abc"; // type mismatch: local definition takes precedence } } const integer cg_uninitConst1; type class SubFunctionClass extends BaseFunctionClass { private const integer m_const := 1; private const integer m_uninitConst2; public function f_get_one() return integer { // FIXME : incomplete marker return m_const; } } // an abstract class can have a finally block type class @abstract FinallyClass { public const integer m_const := 0; } finally { log(this.m_const); } // a trait class cannot have a finally block type class @trait FinallyClass2 { public function f_pub(); } finally { log(this.m_const); } // bad combination of class modifiers type class @final @abstract BadClass1 { } type class @final @trait BadClass3 { } type class @abstract @trait BadClass2 { } // class with this type class ClassWithThis { public var integer vl_a; public var integer vl_b; public function pl_dummy(in integer pl_int) return integer { return 1; } public function f_dummy2(in charstring pl_dummy) { var charstring vl_int := this.pl_dummy(1); } create (integer pl_a, float pl_b) { this.vl_a := pl_a; this.vl_b := pl_b; } } function f_fake_super() return charstring { return "abc"; } // bad super references function f_notinclass(in integer pl_int) { var charstring vl_ch := super.f_fake_super(); } type class FuncClass extends BaseFunctionClass { public function f_get_one() return charstring { return "xyz"; } private function f_test_super() { // type mismatches var integer vl_int := f_get_one(); var charstring vl_cs:= super.f_get_one(); } public function f_dummy(in integer pl_int) return float { return 0.1; } } // private methods can be overridden in subclass // protected and public classes can only be overriden with the same formal param list type class ClassWithPrivate { private function f_override(in float pl_float) return integer { return 1; } function f_override2(in float pl_float) return integer { return 1; } public function f_override3(in float pl_float) return integer { return 1; } public function f_override4() { } function f_override5() { } } type class ClassWithPrivateExt extends ClassWithPrivate { public function f_override(charstring pl_chr) return charstring { return pl_chr; } public function f_override2(in float pl_float) return charstring { return pl_float; } public function f_override3(charstring pl_float) return charstring { return pl_float; } function f_override4() { } private function f_override5() { } } // function with class parameter function f_class_param(FuncClass pl_fun) { var integer vl_int := pl_fun.f_dummy(1); // type mismatch var charstring vl_chr := pl_fun.f_get_one(); // correct } // class function with `runs on`, `mtc` and `system` clause type class BadClauses { public function f_with_runson() runs on CT { } public function f_with_mtc() mtc CT return integer { return 1; } public function f_with_system() system CT { } public function f_with_more() runs on CT mtc CT system CT { } } testcase tc_basicSyntax() runs on CT { var GrandSubClass vl_gsc := GrandSubClass.create; 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_int3 := vl_gsc.vl_a; // correct : inherited from upper parent var charstring vl_int4 := vl_gsc.vl_d; // incorrect : nonexisting member var charstring vl_int5 := vl_gsc.vl_a; // incorrect : type mismatch var OuterClass vl_outer := OuterClass.create; var OuterClass vl_outer2; if (vl_outer of OuterClass) { vl_outer := vl_outer2; } var integer vl_integer := vl_outer of OuterClass; // type mismatch var boolean vl_boolean := vl_outer of BaseFunctionClass; // semanticall correct // nested class member reference var octetstring vl_octet := vl_outer.NestedClass.InnerClass.EvenDeeper.Core.f_inside(1) // instantiating abstract and trait classes is not allowed var object vl_trait := MinimalTraitClass.create; var MinimalAbstractClass vl_abstract := MinimalAbstractClass.create; } }