This commit is contained in:
Ivan Bondarev 2024-01-04 20:59:10 +01:00
parent 909f5599b8
commit 481192485c
2 changed files with 28 additions and 0 deletions

View file

@ -2205,6 +2205,7 @@ namespace PascalABCCompiler.NETGenerator
{
MethodBuilder mb = helper.GetMethod(func).mi as MethodBuilder;
IAttributeNode[] attrs = func.Attributes;
List<CustomAttributeBuilder> returnValueAttrs = new List<CustomAttributeBuilder>();
for (int i = 0; i < attrs.Length; i++)
{
@ -2216,6 +2217,7 @@ namespace PascalABCCompiler.NETGenerator
{
var constr = (attrs[i].AttributeConstructor is ICompiledConstructorNode) ? (attrs[i].AttributeConstructor as ICompiledConstructorNode).constructor_info : helper.GetConstructor(attrs[i].AttributeConstructor).cnstr;
if (attrs[i].Arguments.Length > 0 && helper.GetTypeReference(attrs[i].AttributeType).tp.FullName == "System.Runtime.InteropServices.MarshalAsAttribute")
try
{
mb.SetMarshal(UnmanagedMarshal.DefineUnmanagedMarshal((UnmanagedType)attrs[i].Arguments[0].value));
@ -2224,10 +2226,20 @@ namespace PascalABCCompiler.NETGenerator
{
throw new PascalABCCompiler.Errors.CommonCompilerError(ex.Message.Replace(", переданный для DefineUnmanagedMarshal,",""), attrs[i].Location.document.file_name, attrs[i].Location.begin_line_num, attrs[i].Location.begin_column_num);
}
else
{
returnValueAttrs.Add(cab);
}
}
else
mb.SetCustomAttribute(cab);
}
if (returnValueAttrs.Count > 0)
{
ParameterBuilder pb = mb.DefineParameter(0, ParameterAttributes.Retval, null);
foreach (var attr in returnValueAttrs)
pb.SetCustomAttribute(attr);
}
foreach (IParameterNode pn in func.parameters)
{
ParamInfo pi = helper.GetParameter(pn);

16
TestSuite/attributes6.pas Normal file
View file

@ -0,0 +1,16 @@
type
[System.AttributeUsage(System.AttributeTargets.ReturnValue)]
TestAttribute = sealed class(System.Attribute)
end;
procedure dummy := exit;
[Result: Test]
function f1 := 0;
begin
var mi := System.Type.GetType('attributes6.Program').GetMethod('f1');
var attrs := mi.ReturnTypeCustomAttributes.GetCustomAttributes(true);
assert((attrs.Length = 1) and (attrs[0].GetType().FullName = 'attributes6.TestAttribute'));
end.