JuliaInterop / Clang.jl

C binding generator and Julia interface to libclang
https://juliainterop.github.io/Clang.jl/
MIT License
217 stars 68 forks source link

`auto_mutability` doesn't take structs into account #490

Open VarLad opened 2 months ago

VarLad commented 2 months ago

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?

Gnimuc commented 2 months ago

Could you share the C example code?

VarLad commented 2 months ago
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 commented 2 months ago

@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?)

Gnimuc commented 2 months ago

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 commented 2 months ago

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

wait. does it only happen to StructMutualRef?

Gnimuc commented 2 months ago

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 commented 2 months ago

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