Skip to content

Commit

Permalink
CSharpExpressionPrinter: Wrap expression in parenthesis
Browse files Browse the repository at this point in the history
Wrap default parameter expressions in parentheses to ensure whole expressions are casted instead of only the first operand.
  • Loading branch information
trungnt2910 committed Jul 9, 2023
1 parent 9923d79 commit 355c222
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public string VisitParameter(Parameter parameter)
if (desugared.IsPrimitiveType() &&
(parameter.DefaultArgument.Declaration != null ||
parameter.DefaultArgument.Class == StatementClass.BinaryOperator))
return $"({desugared.Visit(typePrinter)}) {expression}";
return $"({desugared.Visit(typePrinter)}) ({expression})";
var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar();
if (finalType.TryGetClass(out var @class) && @class.IsInterface)
return $@"({@class.Visit(typePrinter)}) ({
@class.OriginalClass.Visit(typePrinter)}) {expression}";
@class.OriginalClass.Visit(typePrinter)}) ({expression})";
return expression;
}

Expand Down Expand Up @@ -88,4 +88,4 @@ public string ToString(Type type)

private readonly TypePrinter typePrinter;
}
}
}
6 changes: 5 additions & 1 deletion tests/dotnet/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,4 +1273,8 @@ extern "C"
s.field2 = 10;
return s;
}
} // extern "C"
} // extern "C"

void DLL_API FunctionWithFlagsAsDefaultParameter(int defaultParam)
{
}
2 changes: 2 additions & 0 deletions tests/dotnet/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1567,3 +1567,5 @@ extern "C"
DLL_API void takeConflictName(struct system* self);
DLL_API struct system freeFunctionReturnByValue();
} // extern "C"

void DLL_API FunctionWithFlagsAsDefaultParameter(int defaultParam = A | B);

0 comments on commit 355c222

Please sign in to comment.