Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Csharpc/CodeGen/FunctionCodeGen.UnsafeAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,20 @@ private LLVMValueRef EmitUnsafeAccessor(IMethodSymbol target, AttributeData attr
}
case 0: // constructor: the return type is the type to create
{
if (S(target.ReturnType) is not INamedTypeSymbol { TypeKind: TypeKind.Class or TypeKind.Struct } owner || !_m.IsUserType(owner))
if (S(target.ReturnType) is not INamedTypeSymbol { TypeKind: TypeKind.Class or TypeKind.Struct } owner)
throw CompileError.Unsupported($"[UnsafeAccessor] constructor of '{target.ReturnType}'", inv);
var parameterTypes = target.Parameters.Select(p => S(p.Type)).ToArray();
// DateTime's private DateTime(ulong dateData) (CoreLib's own, absent from the reference assemblies): csharpc's DateTime
// is that `_dateData` (corelib/DateTimeOps.cs), so the value is the argument itself (#99).
if (BclValueTypes.IsDateTime(owner) && parameterTypes is [{ SpecialType: SpecialType.System_UInt64 }])
return EmitArgument(args[0], target);
var ctor = owner.InstanceConstructors.FirstOrDefault(c => c.Parameters.Select(p => S(p.Type)).SequenceEqual(parameterTypes, SymbolEqualityComparer.Default))
?? throw CompileError.Unsupported($"[UnsafeAccessor] constructor of '{owner}' not found", inv);
// A library type (DateTime, a corelib shadow, a type with intrinsic constructors): created as `new T(...)` would be, so its
// own representation applies (#99); a constructor csharpc has no implementation for stays that explicit diagnostic.
if (!_m.IsUserType(owner))
return EmitObjectCreation(new IlObjectCreationOperation(inv.Syntax, ctor,
[.. args.Select((a, i) => (IArgumentOperation)new IlArgumentOperation(a.Syntax, ctor.Parameters[i], a.Value))]));
if (owner.TypeKind == TypeKind.Struct)
{
// A struct is created in place: a zeroed value whose address is the constructor's `this` (the corelib's
Expand Down
44 changes: 44 additions & 0 deletions tests/samples/1558_unsafe_accessor_library_ctor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// [UnsafeAccessor(UnsafeAccessorKind.Constructor)] of library types (#99): created as `new T(...)` is; DateTime's private
// constructor from its raw data too. (Not generic types: .NET's runtime rejects a constructor accessor of List<int>.)
using System;
using System.Runtime.CompilerServices;
using System.Text;

static class Program
{
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
static extern DateTime NewDateTime(long ticks);

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
static extern DateTime NewDateTimeKind(long ticks, DateTimeKind kind);

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
static extern TimeSpan NewTimeSpan(int hours, int minutes, int seconds);

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
static extern StringBuilder NewBuilder(string value);

// DateTime's private constructor from its raw data (ticks | kind << 62).
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
static extern DateTime NewDateTimeData(ulong dateData);

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
static extern Version NewVersion(int major, int minor);

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
static extern Guid NewGuid(string text);

static void Main()
{
Console.WriteLine(NewDateTime(TimeSpan.TicksPerDay).Day);
var utc = NewDateTimeKind(638000000000000000, DateTimeKind.Utc);
Console.WriteLine($"{utc.Ticks} {utc.Kind} {utc.Year}");
Console.WriteLine(NewTimeSpan(1, 2, 3));
Console.WriteLine(NewBuilder("ab").Append('c'));
var local = NewDateTimeData(630822816000000000UL | (2UL << 62));
Console.WriteLine($"{local.Ticks} {local.Kind} {NewDateTimeData(5).Ticks} {NewDateTimeData(5).Kind}");
Console.WriteLine(NewVersion(4, 2));
Console.WriteLine(NewGuid("00112233-4455-6677-8899-aabbccddeeff"));
try { NewDateTime(-1); } catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.ParamName); }
}
}
8 changes: 8 additions & 0 deletions tests/samples/1558_unsafe_accessor_library_ctor.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
2
638000000000000000 Utc 2022
01:02:03
abc
630822816000000000 Local 5 Unspecified
4.2
00112233-4455-6677-8899-aabbccddeeff
ticks
Loading