From 12cad3fad662a2e9205f4c8076018c2081e6394b Mon Sep 17 00:00:00 2001 From: Ivan Bondarev Date: Sun, 30 Apr 2023 11:36:48 +0200 Subject: [PATCH] #2842 --- TestSuite/constructor2.pas | 27 ++++++++++++++++++ TestSuite/constructor3.pas | 28 +++++++++++++++++++ TestSuite/constructor4.pas | 21 ++++++++++++++ TestSuite/constructor5.pas | 27 ++++++++++++++++++ .../TreeConversion/compilation_context.cs | 14 +++++++--- .../TreeConversion/syntax_tree_visitor.cs | 2 +- 6 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 TestSuite/constructor2.pas create mode 100644 TestSuite/constructor3.pas create mode 100644 TestSuite/constructor4.pas create mode 100644 TestSuite/constructor5.pas diff --git a/TestSuite/constructor2.pas b/TestSuite/constructor2.pas new file mode 100644 index 000000000..ee9be3d9d --- /dev/null +++ b/TestSuite/constructor2.pas @@ -0,0 +1,27 @@ +var i: integer; +type + t1 = class + + private static function init_x: integer; + begin + Inc(i); + end; + private static x := init_x; + + static constructor; + + end; + +static constructor t1.Create; +begin + Inc(i); +end; + +begin + new t1; + assert(i = 2); + assert(typeof(t1).GetConstructors( + System.Reflection.BindingFlags.Static or + System.Reflection.BindingFlags.NonPublic + ).Length = 1); +end. \ No newline at end of file diff --git a/TestSuite/constructor3.pas b/TestSuite/constructor3.pas new file mode 100644 index 000000000..784006190 --- /dev/null +++ b/TestSuite/constructor3.pas @@ -0,0 +1,28 @@ +var i: integer; +type + t1 = class + + private static function init_x: integer; + begin + Inc(i); + end; + private static x := init_x; + private static y := init_x; + + static constructor; + + end; + +static constructor t1.Create; +begin + Inc(i); +end; + +begin + new t1; + assert(i = 3); + assert(typeof(t1).GetConstructors( + System.Reflection.BindingFlags.Static or + System.Reflection.BindingFlags.NonPublic + ).Length = 1); +end. \ No newline at end of file diff --git a/TestSuite/constructor4.pas b/TestSuite/constructor4.pas new file mode 100644 index 000000000..d30ef6c20 --- /dev/null +++ b/TestSuite/constructor4.pas @@ -0,0 +1,21 @@ +var i: integer; +type + t1 = class + + private static function init_x: integer; + begin + Inc(i); + end; + private static x := init_x; + + end; + + +begin + new t1; + assert(i = 1); + assert(typeof(t1).GetConstructors( + System.Reflection.BindingFlags.Static or + System.Reflection.BindingFlags.NonPublic + ).Length = 1); +end. \ No newline at end of file diff --git a/TestSuite/constructor5.pas b/TestSuite/constructor5.pas new file mode 100644 index 000000000..91b941a3d --- /dev/null +++ b/TestSuite/constructor5.pas @@ -0,0 +1,27 @@ +var i: integer; +type + t1 = class + + private static function init_x: integer; + begin + Inc(i); + end; + private static x := init_x; + + static constructor; + + end; + +static constructor t1.Create; +begin + Inc(i); +end; + +begin + new t1; + assert(i = 2); + assert(typeof(t1).GetConstructors( + System.Reflection.BindingFlags.Static or + System.Reflection.BindingFlags.NonPublic + ).Length = 1); +end. \ No newline at end of file diff --git a/TreeConverter/TreeConversion/compilation_context.cs b/TreeConverter/TreeConversion/compilation_context.cs index e4e40d80d..635b1dc84 100644 --- a/TreeConverter/TreeConversion/compilation_context.cs +++ b/TreeConverter/TreeConversion/compilation_context.cs @@ -2462,11 +2462,17 @@ namespace PascalABCCompiler.TreeConverter common_method_node stat_ctor; if (sil == null) { - stat_ctor = new common_method_node(stat_ctor_name, null, null, generic_def, + if (generic_def.static_constr != null) + stat_ctor = generic_def.static_constr; + else + { + stat_ctor = new common_method_node(stat_ctor_name, null, null, generic_def, SemanticTree.polymorphic_state.ps_static, SemanticTree.field_access_level.fal_public, null); - stat_ctor.is_constructor = true; - generic_def.add_name(stat_ctor_name, new SymbolInfo(stat_ctor)); - generic_def.methods.AddElement(stat_ctor); + stat_ctor.is_constructor = true; + generic_def.add_name(stat_ctor_name, new SymbolInfo(stat_ctor)); + generic_def.methods.AddElement(stat_ctor); + } + } else { diff --git a/TreeConverter/TreeConversion/syntax_tree_visitor.cs b/TreeConverter/TreeConversion/syntax_tree_visitor.cs index 763ece523..20a46d480 100644 --- a/TreeConverter/TreeConversion/syntax_tree_visitor.cs +++ b/TreeConverter/TreeConversion/syntax_tree_visitor.cs @@ -3292,7 +3292,7 @@ namespace PascalABCCompiler.TreeConverter { common_method_node static_constr = context.top_function as common_method_node; static_constr.is_constructor = true; - if (context.converted_type.IsPartial && context.converted_type.static_constr != null)//remove auto generated static constructor from partial class + if (/*context.converted_type.IsPartial && */context.converted_type.static_constr != null)//remove auto generated static constructor from partial class context.converted_type.methods.RemoveElement(context.converted_type.static_constr); static_constr.cont_type.static_constr = context.top_function as common_method_node; }