This commit is contained in:
Ivan Bondarev 2023-01-22 12:03:23 +01:00
parent 22b77e38af
commit fa6d69cbdd
3 changed files with 16 additions and 0 deletions

View file

@ -65,6 +65,7 @@ namespace PascalABCCompiler.NETGenerator
public static MethodInfo GCHandleAllocPinned;
public static MethodInfo OffsetToStringDataProperty;
public static MethodInfo StringLengthMethod;
public static MethodInfo CharToString;
public static ConstructorInfo IndexOutOfRangeConstructor;
public static ConstructorInfo ParamArrayAttributeConstructor;
public static MethodInfo StringCopyMethod;
@ -126,6 +127,7 @@ namespace PascalABCCompiler.NETGenerator
GCHandleFreeMethod = typeof(GCHandle).GetMethod("Free");
GetTypeFromHandleMethod = typeof(Type).GetMethod("GetTypeFromHandle");
StringCopyMethod = typeof(string).GetMethod("Copy");
CharToString = typeof(char).GetMethod("ToString", BindingFlags.Static | BindingFlags.Public);
}
public static bool IsStandType(Type t)

View file

@ -11529,6 +11529,8 @@ namespace PascalABCCompiler.NETGenerator
}
if (vi.lb.LocalType == TypeFactory.ObjectType && get_current_meth.ReturnType.IsValueType)
il.Emit(OpCodes.Box, get_current_meth.ReturnType);
else if (vi.lb.LocalType == TypeFactory.StringType && get_current_meth.ReturnType == TypeFactory.CharType)
il.Emit(OpCodes.Call, TypeFactory.CharToString);
il.Emit(OpCodes.Stloc, vi.lb);
}
else if (vi.kind == VarKind.vkGlobal)
@ -11542,6 +11544,8 @@ namespace PascalABCCompiler.NETGenerator
}
if (vi.fb.FieldType == TypeFactory.ObjectType && get_current_meth.ReturnType.IsValueType)
il.Emit(OpCodes.Box, get_current_meth.ReturnType);
else if (vi.fb.FieldType == TypeFactory.StringType && get_current_meth.ReturnType == TypeFactory.CharType)
il.Emit(OpCodes.Call, TypeFactory.CharToString);
il.Emit(OpCodes.Stsfld, vi.fb);
}
else
@ -11555,6 +11559,8 @@ namespace PascalABCCompiler.NETGenerator
}
if (vi.fb.FieldType == TypeFactory.ObjectType && get_current_meth.ReturnType.IsValueType)
il.Emit(OpCodes.Box, get_current_meth.ReturnType);
else if (vi.fb.FieldType == TypeFactory.StringType && get_current_meth.ReturnType == TypeFactory.CharType)
il.Emit(OpCodes.Call, TypeFactory.CharToString);
il.Emit(OpCodes.Stfld, vi.fb);
}
labels.Push(leave_label);

8
TestSuite/foreach11.pas Normal file
View file

@ -0,0 +1,8 @@
begin
var l := new List<string>();
foreach var s: string in 'abc' do
l.Add(s);
assert(l[0] = 'a');
assert(l[1] = 'b');
assert(l[1] = 'c');
end.