-
Notifications
You must be signed in to change notification settings - Fork 528
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
|
@@ -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( | ||
|
@@ -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")) | ||
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); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -1780,7 +1788,7 @@ public void GenerateVTable(Class @class) | |
return __vtables; | ||
}} | ||
|
||
set {{ | ||
set {{ | ||
__vtables = value; | ||
}}", trimIndentation: true); | ||
} | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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. | ||
// | ||
|
@@ -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. | ||
|
@@ -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 | ||
|
@@ -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()) | ||
{ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use https://github.com/mono/CppSharp/blob/main/src/AST/TypeExtensions.cs#L385 maybe?
There was a problem hiding this comment.
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.