Skip to content

Commit 8528837

Browse files
committed
Silly fixes: fix #2298, fix #2238, fix #2128
1 parent 852c12a commit 8528837

File tree

5 files changed

+93
-14
lines changed

5 files changed

+93
-14
lines changed

src/Core/Silk.NET.BuildTools/Common/Functions/Function.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public class Function : IEquatable<Function>, IProfileConstituent
101101
/// members.
102102
/// </summary>
103103
public bool IsReadOnly { get; set; }
104+
105+
/// <summary>
106+
/// Whether this method is an override.
107+
/// </summary>
108+
public bool IsOverride { get; set; }
104109

105110
/// <summary>
106111
/// Prefix to invocations of this function e.g. "@this->". May be null.
@@ -212,6 +217,11 @@ private void GetDeclarationString(StringBuilder sb,
212217
sb.Append("unsafe ");
213218
}
214219

220+
if (IsOverride)
221+
{
222+
sb.Append("override ");
223+
}
224+
215225
if (partial)
216226
{
217227
sb.Append("partial ");

src/Core/Silk.NET.BuildTools/Common/Utilities.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -625,21 +625,31 @@ public static string ToNativeName(this Decl decl)
625625
}
626626

627627
public static bool IsProbablyABitmask(this Enums.Enum @enum)
628-
=> @enum.Tokens.Count > 1 && // there is more than one token
629-
// at least approx 50% of the tokens have only one bit set
630-
@enum.Tokens.Count(x => BitOperations.PopCount(ParseToken(x.Value, @enum)) == 1)
631-
>= MathF.Floor(@enum.Tokens.Count / 2f) &&
632-
// it's not sequential (1, 2, 3)
633-
!@enum.IsSequential();
634-
635-
private static bool IsSequential(this Enums.Enum @enum)
628+
{
629+
var parsed = @enum.Tokens.Select(x => ParseToken(x.Value, @enum)).ToArray();
630+
var popcnt1Cnt = parsed.Count(x => BitOperations.PopCount(x) == 1);
631+
// there is more than one token
632+
return @enum.Tokens.Count > 1 &&
633+
// at least approx 50% of the tokens have only one bit set
634+
popcnt1Cnt >= MathF.Floor(@enum.Tokens.Count / 2f) &&
635+
// it's not sequential (1, 2, 3)
636+
!parsed.IsSeeminglySequential() ||
637+
// alternatively, all items that are not MaxValue or 0 have popcnt == 1
638+
// https://github.com/dotnet/Silk.NET/issues/2238
639+
parsed.Count(x => x is not 0 and not 0x7FFFFFFF) == popcnt1Cnt &&
640+
// and there are more than two items with popcnt == 1
641+
popcnt1Cnt > 2;
642+
}
643+
644+
// renamed from IsSequential as suggested by this comment:
645+
// https://discord.com/channels/521092042781229087/587346162802229298/1284528132807987212
646+
private static bool IsSeeminglySequential(this ulong[] @enum)
636647
{
637648
const int maxMisses = 1;
638649
var misses = 0;
639-
for (var i = 0; i < @enum.Tokens.Count; i++)
650+
for (var i = 0; i < @enum.Length; i++)
640651
{
641-
if (ParseToken(@enum.Tokens[i].Value, @enum) != (ulong)(i - misses) &&
642-
ParseToken(@enum.Tokens[i].Value, @enum) != (ulong)((i - misses) + 1))
652+
if (@enum[i] != (ulong)(i - misses) && @enum[i] != (ulong)(i - misses + 1))
643653
{
644654
misses++;
645655
}

src/Core/Silk.NET.BuildTools/Converters/Readers/VulkanReader.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
8+
using System.Text;
89
using Silk.NET.BuildTools.Common;
910
using Silk.NET.BuildTools.Common.Enums;
1011
using Silk.NET.BuildTools.Common.Functions;
@@ -148,6 +149,14 @@ IReadOnlyList<Struct> GetAllAliasesFromName(string? api, string structName)
148149
continue;
149150
}
150151

152+
var toString = new Function
153+
{
154+
Accessibility = Accessibility.Public,
155+
IsOverride = true,
156+
ReturnType = new Type { Name = "string" },
157+
Name = "ToString"
158+
};
159+
151160
var @struct = new Struct
152161
{
153162
Fields = s.Members.Select
@@ -179,6 +188,10 @@ IReadOnlyList<Struct> GetAllAliasesFromName(string? api, string structName)
179188
}.WithFixedFieldFixup09072020()
180189
)
181190
.ToList(),
191+
Functions = new List<ImplementedFunction>
192+
{
193+
new(toString, new StringBuilder("return Handle.ToString();"), toString, false)
194+
},
182195
Name = Naming.TranslateLite(TrimName(s.Name, task), prefix),
183196
NativeName = s.Name,
184197
ProfileName = s.Api

src/Core/Silk.NET.BuildTools/Cpp/Clang.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ Type GetType(ClangSharp.Type type, out Count count, ref FlowDirection flow, out
679679
if (type is ArrayType arrayType)
680680
{
681681
ret = GetType(arrayType.ElementType, out var currentCount, ref flow, out _);
682-
ret.IndirectionLevels++;
682+
ret.IndirectionLevels++; // TODO this is wrong for >2 dims!
683683
var asize = arrayType.Handle.ArraySize;
684684
if (asize != -1)
685685
{
@@ -812,7 +812,13 @@ Type GetType(ClangSharp.Type type, out Count count, ref FlowDirection flow, out
812812
}
813813
else
814814
{
815-
ret = new Type { Name = elaboratedType.NamedType.AsString };
815+
var name = elaboratedType.NamedType.AsString;
816+
if (name.LastIndexOf("::", StringComparison.Ordinal) is not -1 and var v)
817+
{
818+
name = name[(v + 2)..];
819+
}
820+
821+
ret = new Type { Name = name };
816822
}
817823
}
818824
else if (type is FunctionType functionType)

src/Core/Silk.NET.Core/Loader/DefaultPathResolver.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class DefaultPathResolver : PathResolver
3333
/// - <see cref="MainModuleDirectoryResolver" />
3434
/// - <see cref="RuntimesFolderResolver" />
3535
/// - <see cref="NativePackageResolver" />
36+
/// - <see cref="SilkDirectoryResolver"/>
3637
/// </summary>
3738
public DefaultPathResolver() => Resolvers = new()
3839
{
@@ -42,7 +43,8 @@ public class DefaultPathResolver : PathResolver
4243
BaseDirectoryResolver,
4344
MainModuleDirectoryResolver,
4445
RuntimesFolderResolver,
45-
NativePackageResolver
46+
NativePackageResolver,
47+
SilkDirectoryResolver
4648
};
4749

4850
/// <summary>
@@ -96,6 +98,44 @@ public class DefaultPathResolver : PathResolver
9698
return Enumerable.Empty<string>();
9799
};
98100

101+
/// <summary>
102+
/// A resolver that returns a path to a file in Silk.NET's <see cref="Assembly.Location"/> and/or
103+
/// <see cref="Assembly.CodeBase"/> directory with the given name.
104+
/// </summary>
105+
public static readonly Func<string, IEnumerable<string>> SilkDirectoryResolver = name =>
106+
{
107+
try
108+
{
109+
var asmLocation = typeof(DefaultPathResolver).Assembly.Location;
110+
// check that name doesn't have a directory name, we only want raw filenames so that the Path.Combine
111+
// doesn't blow up.
112+
if (!string.IsNullOrWhiteSpace(Path.GetDirectoryName(name)) && !string.IsNullOrWhiteSpace(asmLocation) && File.Exists(asmLocation))
113+
{
114+
asmLocation = Path.GetDirectoryName(asmLocation);
115+
if (asmLocation is not null)
116+
{
117+
return Enumerable.Repeat(Path.Combine(asmLocation, name), 1);
118+
}
119+
}
120+
121+
asmLocation = typeof(DefaultPathResolver).Assembly.CodeBase;
122+
if (!string.IsNullOrWhiteSpace(Path.GetDirectoryName(name)) && !string.IsNullOrWhiteSpace(asmLocation) && File.Exists(asmLocation))
123+
{
124+
asmLocation = Path.GetDirectoryName(asmLocation);
125+
if (asmLocation is not null)
126+
{
127+
return Enumerable.Repeat(Path.Combine(asmLocation, name), 1);
128+
}
129+
}
130+
}
131+
catch
132+
{
133+
// not supported on the WASI-SDK
134+
}
135+
136+
return Enumerable.Empty<string>();
137+
};
138+
99139
/// <summary>
100140
/// A resolver that, given an absolute or relative path, searches for a "runtimes" folder in the directory
101141
/// represented by the given name for a file matching the given path's file name in one of the applicable

0 commit comments

Comments
 (0)