Skip to content

CSharpExpressionPrinter: Recurse into operands #1745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2023
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
9 changes: 2 additions & 7 deletions src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,8 @@ public string VisitExpression(ExpressionObsolete expr)
case StatementClass.BinaryOperator:
var binaryOperator = (BinaryOperatorObsolete)expr;

var lhsResult = binaryOperator.LHS.String;
if (binaryOperator.LHS.Declaration is Enumeration.Item)
lhsResult = binaryOperator.LHS.Declaration.Visit(typePrinter).Type;

var rhsResult = binaryOperator.RHS.String;
if (binaryOperator.RHS.Declaration is Enumeration.Item)
rhsResult = binaryOperator.RHS.Declaration.Visit(typePrinter).Type;
var lhsResult = VisitExpression(binaryOperator.LHS);
var rhsResult = VisitExpression(binaryOperator.RHS);

return $"{lhsResult} {binaryOperator.OpcodeStr} {rhsResult}";
case StatementClass.ConstructorReference:
Expand Down
46 changes: 27 additions & 19 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ public virtual void GenerateNamespaceFunctionsAndVariables(DeclarationContext co
.Any();

using (PushWriteBlock(BlockKind.Functions, $"public unsafe partial {(isStruct ? "struct" : "class")} {parentName}", NewLineKind.BeforeNextBlock))
{
{
using (PushWriteBlock(BlockKind.InternalsClass, GetClassInternalHead(new Class { Name = parentName }), NewLineKind.BeforeNextBlock))
{
{
// Generate all the internal function declarations.
foreach (var function in context.Functions)
{
Expand Down Expand Up @@ -301,7 +301,7 @@ template.OriginalNamespace is Class &&
template.Name);

using (PushWriteBlock(BlockKind.Namespace, namespaceName, NewLineKind.BeforeNextBlock))
{
{
var generated = GetGeneratedClasses(template, specializations);

foreach (var nestedTemplate in template.Classes.Where(
Expand Down Expand Up @@ -1615,19 +1615,27 @@ private void GenerateVariable(Class @class, Variable variable)
var variableType = variable.Type.Visit(TypePrinter);
TypePrinter.PopMarshalKind();

var signature = $"public static {variableType} {variable.Name}";
bool hasInitializer = variable.Initializer != null && !string.IsNullOrWhiteSpace(variable.Initializer.String);

if (variable.Initializer != null && !string.IsNullOrWhiteSpace(variable.Initializer.String))
GeneratePropertyGetterForVariableWithInitializer(variable, signature);
if (hasInitializer && variable.QualifiedType.Qualifiers.IsConst &&
(variable.Type.Desugar() is BuiltinType || variableType.ToString() == "string"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your version might be better actually since it will take type maps into account.

Write($"public const {variableType} {variable.Name} = {variable.Initializer.String};");
else
{
using (WriteBlock(signature))
var signature = $"public static {variableType} {variable.Name}";

if (hasInitializer)
GeneratePropertyGetterForVariableWithInitializer(variable, signature);
else
{
GeneratePropertyGetter(variable, @class);
using (WriteBlock(signature))
{
GeneratePropertyGetter(variable, @class);

if (!variable.QualifiedType.Qualifiers.IsConst &&
!(variable.Type.Desugar() is ArrayType))
GeneratePropertySetter(variable, @class);
if (!variable.QualifiedType.Qualifiers.IsConst &&
!(variable.Type.Desugar() is ArrayType))
GeneratePropertySetter(variable, @class);
}
}
}

Expand Down Expand Up @@ -1780,7 +1788,7 @@ public void GenerateVTable(Class @class)
return __vtables;
}}

set {{
set {{
__vtables = value;
}}", trimIndentation: true);
}
Expand Down Expand Up @@ -2296,13 +2304,13 @@ private void GenerateDisposeMethods(Class @class)
// Normally, calling the native dtor should be controlled by whether or not we
// we own the underlying instance. (i.e. Helpers.OwnsNativeInstanceIdentifier).
// However, there are 2 situations when the caller needs to have direct control
//
//
// 1. When we have a virtual dtor on the native side we detour the vtable entry
// even when we don't own the underlying native instance. I think we do this
// so that the managed side can null out the __Instance pointer and remove the
// instance from the NativeToManagedMap. Of course, this is somewhat half-hearted
// since we can't/don't do this when there's no virtual dtor available to detour.
// Anyway, we must be able to call the native dtor in this case even if we don't
// Anyway, we must be able to call the native dtor in this case even if we don't
/// own the underlying native instance.
//
// 2. When we we pass a disposable object to a function "by value" then the callee
Expand All @@ -2313,7 +2321,7 @@ private void GenerateDisposeMethods(Class @class)
// ....
// compiler generates call to f.dtor() at the end of function
// }
//
//
// IDisposable.Dispose() and Object.Finalize() set callNativeDtor = Helpers.OwnsNativeInstanceIdentifier
WriteLine("if (callNativeDtor)");
if (@class.IsDependent || dtor.IsVirtual)
Expand All @@ -2334,7 +2342,7 @@ c is ClassTemplateSpecialization ?
}

// If we have any fields holding references to unmanaged memory allocated here, free the
// referenced memory. Don't rely on testing if the field's IntPtr is IntPtr.Zero since
// referenced memory. Don't rely on testing if the field's IntPtr is IntPtr.Zero since
// unmanaged memory isn't always initialized and/or a reference may be owned by the
// native side.
//
Expand Down Expand Up @@ -2540,7 +2548,7 @@ private void Generate__CopyValue(Class @class, string @internal)
using (WriteBlock($"private static void* __CopyValue({@internal} native)"))
{
var copyCtorMethod = @class.Methods.FirstOrDefault(method => method.IsCopyConstructor);

if (@class.HasNonTrivialCopyConstructor && copyCtorMethod != null && copyCtorMethod.IsGenerated)
{
// Allocate memory for a new native object and call the ctor.
Expand Down Expand Up @@ -2848,7 +2856,7 @@ private void GenerateEquals(Class @class)
private void GenerateGetHashCode(Class @class)
{
using (WriteBlock("public override int GetHashCode()"))
{
{
if ([email protected])
WriteLine($"return {Helpers.InstanceIdentifier}.GetHashCode();");
else
Expand Down Expand Up @@ -2999,7 +3007,7 @@ private void GenerateClassConstructor(Method method, Class @class)
// Copy any string references owned by the source to the new instance so we
// don't have to ref count them.
// If there is no property or no setter then this instance can never own the native
// memory. Worry about the case where there's only a setter (write-only) when we
// memory. Worry about the case where there's only a setter (write-only) when we
// understand the use case and how it can occur.
foreach (var prop in @class.GetConstCharFieldProperties())
{
Expand Down
4 changes: 4 additions & 0 deletions tests/dotnet/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,3 +1278,7 @@ extern "C"
void DLL_API FunctionWithFlagsAsDefaultParameter(int defaultParam)
{
}

void DLL_API FunctionWithConstFlagsAsDefaultParameter(int defaultParam)
{
}
6 changes: 6 additions & 0 deletions tests/dotnet/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1569,3 +1569,9 @@ extern "C"
} // extern "C"

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

const int ConstFlag1 = 1;
const int ConstFlag2 = 2;
const int ConstFlag3 = 4;

void DLL_API FunctionWithConstFlagsAsDefaultParameter(int defaultParam = ConstFlag1 | ConstFlag2 | ConstFlag3);