From c25e2e401e48c78f67b5a88a0c0043fe056f2b73 Mon Sep 17 00:00:00 2001 From: Ivan Bondarev Date: Sun, 3 Oct 2021 11:07:35 +0200 Subject: [PATCH] #2517 --- NETGenerator/NETGenerator.cs | 17 +++++++++++++++++ TestSuite/classconst4.pas | 14 ++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 TestSuite/classconst4.pas diff --git a/NETGenerator/NETGenerator.cs b/NETGenerator/NETGenerator.cs index 258cfa132..7747e1901 100644 --- a/NETGenerator/NETGenerator.cs +++ b/NETGenerator/NETGenerator.cs @@ -5029,6 +5029,11 @@ namespace PascalABCCompiler.NETGenerator GenerateInitCodeForField(ccf); else GenerateInitCodeForStaticField(ccf); + foreach (IClassConstantDefinitionNode cnst in ctn.constants) + if (cnst.constant_value is IArrayConstantNode) + { + GenerateInitCodeForClassConstant(cnst); + } } } @@ -5070,6 +5075,16 @@ namespace PascalABCCompiler.NETGenerator in_var_init = false; } + internal void GenerateInitCodeForClassConstant(SemanticTree.IClassConstantDefinitionNode value) + { + TypeInfo ti = helper.GetTypeReference(value.type), cur_ti = helper.GetTypeReference(value.comperehensive_type); + FieldBuilder fb = helper.GetConstant(value).fb as FieldBuilder; + + if (value.constant_value is IArrayConstantNode) + CreateArrayForClassField(cur_ti.static_cnstr.GetILGenerator(), fb, ti, value.constant_value as IArrayConstantNode, value.type); + + } + internal void GenerateInitCodeForField(SemanticTree.ICommonClassFieldNode value) { TypeInfo ti = helper.GetTypeReference(value.type), cur_ti = helper.GetTypeReference(value.comperehensive_type); @@ -11007,6 +11022,8 @@ namespace PascalABCCompiler.NETGenerator fb = cur_type.DefineField(value.name, helper.GetTypeReference(value.type).tp, FieldAttributes.Static | ConvertFALToFieldAttributes(value.field_access_level)); if (value.constant_value.value != null) fb.SetConstant(value.constant_value.value); + else + helper.AddConstant(value, fb); //else // throw new Errors.CompilerInternalError("NetGenerator", new Exception("Invalid constant value in IClassConstantDefinitionNode")); } diff --git a/TestSuite/classconst4.pas b/TestSuite/classconst4.pas new file mode 100644 index 000000000..7da9a2c18 --- /dev/null +++ b/TestSuite/classconst4.pas @@ -0,0 +1,14 @@ + +type + TArrStr = array of string; + t1 = class + + public const arr: array of string = ('abc', 'def'); + + end; + +begin + var arr := TArrStr(typeof(t1).GetField('arr').GetValue(nil)); + assert(arr[0] = 'abc'); + assert(arr[1] = 'def'); +end. \ No newline at end of file