Clang.jl icon indicating copy to clipboard operation
Clang.jl copied to clipboard

`auto_mutability` doesn't take structs into account

Open VarLad opened this issue 9 months ago • 7 comments

There are a few structs which take a pointer to another struct as one of its fields. For example, in a certain generated code:

mutable struct A
  x::Ptr{B}
  y::C
end
struct B
  x::Ptr{B}
  y::D
end

In such cases, wouldn't it make more sense if B was mutable, and for auto_mutability to make B mutable as well?

VarLad avatar May 01 '24 23:05 VarLad

Could you share the C example code?

Gnimuc avatar May 02 '24 02:05 Gnimuc

typedef struct WGPUChainedStruct {
    struct WGPUChainedStruct const * next;
    WGPUSType sType;
} WGPUChainedStruct WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUBufferDescriptor {
    WGPUChainedStruct const * nextInChain;
    WGPU_NULLABLE char const * label;
    WGPUBufferUsageFlags usage;
    uint64_t size;
    WGPUBool mappedAtCreation;
} WGPUBufferDescriptor WGPU_STRUCTURE_ATTRIBUTE;

from https://github.com/webgpu-native/webgpu-headers/blob/d02fec1b96af29695b9f5659a91067a241f40b04/webgpu.h#L746

VarLad avatar May 02 '24 03:05 VarLad

@Gnimuc I believe we've to edit mutability.jl for StructForwardDecl, TypedefFunction and UnionForwardDecl? Does the current code allow this? (I saw it was just checking the nodes, so, can it work if we just edit https://github.com/JuliaInterop/Clang.jl/blob/master/src/generator/mutability.jl#L38 to include the above types to be checked?)

VarLad avatar May 03 '24 14:05 VarLad

https://github.com/JuliaInterop/Clang.jl/blob/5a1cc29c154ed925f01e59dfd705cbf8042158e4/src/generator/passes.jl#L770-L791

This is the current rule which is used to workaround the following case:

https://github.com/JuliaInterop/Clang.jl/blob/5a1cc29c154ed925f01e59dfd705cbf8042158e4/gen/generator.toml#L118-L125

You can add more reasonable rules to should_tweak or directly in the pass function.

Gnimuc avatar May 03 '24 15:05 Gnimuc

struct B x::Ptr{B} y::D end

wait. does it only happen to StructMutualRef?

Gnimuc avatar May 03 '24 16:05 Gnimuc

https://github.com/JuliaInterop/Clang.jl/blob/5a1cc29c154ed925f01e59dfd705cbf8042158e4/src/generator/passes.jl#L819

~~What happens if you remove StructMutualRef in this line?~~

This actually do check those StructMutualRef types..

Gnimuc avatar May 03 '24 16:05 Gnimuc

I think we need to add new rules for non-function-proto types.

Gnimuc avatar May 03 '24 16:05 Gnimuc