This commit is contained in:
Ivan Bondarev 2021-10-03 11:07:35 +02:00
parent ec18666893
commit c25e2e401e
2 changed files with 31 additions and 0 deletions

View file

@ -5029,6 +5029,11 @@ namespace PascalABCCompiler.NETGenerator
GenerateInitCodeForField(ccf); GenerateInitCodeForField(ccf);
else else
GenerateInitCodeForStaticField(ccf); 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; 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) internal void GenerateInitCodeForField(SemanticTree.ICommonClassFieldNode value)
{ {
TypeInfo ti = helper.GetTypeReference(value.type), cur_ti = helper.GetTypeReference(value.comperehensive_type); 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)); fb = cur_type.DefineField(value.name, helper.GetTypeReference(value.type).tp, FieldAttributes.Static | ConvertFALToFieldAttributes(value.field_access_level));
if (value.constant_value.value != null) if (value.constant_value.value != null)
fb.SetConstant(value.constant_value.value); fb.SetConstant(value.constant_value.value);
else
helper.AddConstant(value, fb);
//else //else
// throw new Errors.CompilerInternalError("NetGenerator", new Exception("Invalid constant value in IClassConstantDefinitionNode")); // throw new Errors.CompilerInternalError("NetGenerator", new Exception("Invalid constant value in IClassConstantDefinitionNode"));
} }

14
TestSuite/classconst4.pas Normal file
View file

@ -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.