Skip to content

Commit 12642f9

Browse files
committed
Fixed the generated C# when an unsigned enum is assigned a negative value.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 7a9c3bf commit 12642f9

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

build/InstallMono.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
OS=$(uname -s)
22
if [ "$OS" == "Darwin" ]; then
3-
wget -O mono.pkg https://download.mono-project.com/archive/5.2.0/macos-10-universal/MonoFramework-MDK-5.2.0.215.macos10.xamarin.universal.pkg
3+
wget -O mono.pkg https://download.mono-project.com/archive/5.18.0/macos-10-universal/MonoFramework-MDK-5.18.0.240.macos10.xamarin.universal.pkg
44
sudo installer -pkg mono.pkg -target /
55
export PATH=$PATH:/Library/Frameworks/Mono.framework/Versions/Current/bin
66
elif [ "$OS" == "Linux" ]; then

src/Generator/Passes/HandleDefaultParamValuesPass.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ private bool CheckForDefaultPointer(Type desugared, ref string result)
129129
Class @class;
130130
if (desugared.GetFinalPointee().TryGetClass(out @class) && @class.IsValueType)
131131
{
132-
result = string.Format("new {0}()",
133-
new CSharpTypePrinter(Context).VisitClassDecl(@class));
132+
result = $"new {@class.Visit(new CSharpTypePrinter(Context))}()";
134133
return true;
135134
}
136135

@@ -253,15 +252,40 @@ private bool CheckForEnumValue(Type desugared, Statement statement,
253252
if (call != null && statement.String != "0")
254253
{
255254
var @params = regexFunctionParams.Match(statement.String).Groups[1].Value;
256-
result = TranslateEnumExpression(call, desugared, @params);
255+
result = TranslateEnumExpression(desugared, @params);
256+
return true;
257+
}
258+
259+
if (desugared.TryGetEnum(out Enumeration @enum) &&
260+
int.TryParse(statement.String, out int value))
261+
{
262+
var typePrinter = new CSharpTypePrinter(Context);
263+
var printedEnum = @enum.Visit(typePrinter);
264+
if (value < 0)
265+
switch (@enum.BuiltinType.Type)
266+
{
267+
case PrimitiveType.UShort:
268+
case PrimitiveType.UInt:
269+
case PrimitiveType.ULong:
270+
case PrimitiveType.ULongLong:
271+
case PrimitiveType.UInt128:
272+
result = $@"({printedEnum}) unchecked(({
273+
@enum.BuiltinType.Visit(typePrinter)}) {
274+
statement.String})";
275+
break;
276+
default:
277+
result = $"({printedEnum}) ({statement.String})";
278+
break;
279+
}
280+
else
281+
result = $"({printedEnum}) {statement.String}";
257282
return true;
258283
}
259284

260285
return false;
261286
}
262287

263-
private string TranslateEnumExpression(Function function,
264-
Type desugared, string @params)
288+
private string TranslateEnumExpression(Type desugared, string @params)
265289
{
266290
if (@params.Contains("::"))
267291
return regexDoubleColon.Replace(@params, desugared + ".");

tests/CSharp/CSharp.Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public void TestDefaultArguments()
229229
methodsWithDefaultValues.DefaultValueType();
230230
methodsWithDefaultValues.DefaultChar();
231231
methodsWithDefaultValues.DefaultEmptyChar();
232+
methodsWithDefaultValues.DefaultEmptyEnum();
232233
methodsWithDefaultValues.DefaultRefTypeBeforeOthers();
233234
methodsWithDefaultValues.DefaultRefTypeAfterOthers();
234235
methodsWithDefaultValues.DefaultRefTypeBeforeAndAfterOthers(0, null);

tests/CSharp/CSharp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ void MethodsWithDefaultValues::defaultEmptyChar(char c)
584584
{
585585
}
586586

587+
void MethodsWithDefaultValues::defaultEmptyEnum(Empty e)
588+
{
589+
}
590+
587591
void MethodsWithDefaultValues::defaultRefTypeBeforeOthers(Foo foo, int i, Bar::Items item)
588592
{
589593
}

tests/CSharp/CSharp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ class DLL_API DefaultZeroMappedToEnum
379379
DefaultZeroMappedToEnum(int* = 0);
380380
};
381381

382+
enum class Empty : unsigned long long int
383+
{
384+
};
385+
382386
class DLL_API MethodsWithDefaultValues : public Quux
383387
{
384388
public:
@@ -404,6 +408,7 @@ class DLL_API MethodsWithDefaultValues : public Quux
404408
void defaultValueType(QGenericArgument valueType = QGenericArgument());
405409
void defaultChar(char c = 'a');
406410
void defaultEmptyChar(char c = 0);
411+
void defaultEmptyEnum(Empty e = Empty(-1));
407412
void defaultRefTypeBeforeOthers(Foo foo = Foo(), int i = 5, Bar::Items item = Bar::Item2);
408413
void defaultRefTypeAfterOthers(int i = 5, Bar::Items item = Bar::Item2, Foo foo = Foo());
409414
void defaultRefTypeBeforeAndAfterOthers(int i = 5, Foo foo = Foo(), Bar::Items item = Bar::Item2, Baz baz = Baz());

0 commit comments

Comments
 (0)