You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# // typedef different names# typedef struct my_opaque_struct my_opaque_t;aliasMyOpaqueStruct=Void# note: it should be `MyOpaqueT` ?# // typedef different name, + func# typedef struct my_opaque_struct my_opaque_t;# void my_func(my_opaque_t *obj);aliasMyOpaqueStruct=Voidfunmy_func(obj : MyOpaqueT)
typeMyOpaqueT=Pointer(Void)
# // typedef same name# typedef struct my_opaque_t my_opaque_t;aliasMyOpaqueT=Void# // typedef same name, + func# typedef struct my_opaque_t my_opaque_t;# void my_func(my_opaque_t *obj);aliasMyOpaqueT=Voidfunmy_func(obj : MyOpaqueT)
typeMyOpaqueT=Pointer(Void)
We can see that the typedef generates the alias, and the function declaration generates the fun and the type.
Looking at the code, I've found that the type is generated when mapping the function argument: the argument type is parsed as a PointerType to a StructOrUnion. In the mapping of the pointer, a check is made for an opaque type, succeeds, then creates a new typedef with the same name as the alias.
I'm not sure how we could properly fix this, as I like the way it auto creates a type of Pointer(Void) for opaque types. Maybe we should rename the generated type and add Ptr (or _) at the end, to ensure there's no conflict.
The example would be:
I'm not really sure how this issue should be fixed .but some has to be removed/renamed ...
(IMO it's a bad idea to typedef a struct with it's name but I've seen this kind of code in many libs)
Here is another example for the issue:
$ cat <<EOF > /tmp/test.hstruct SomeOpaque;typedef int SomeOpaque;SomeOpaque *some_function(struct SomeOpaque);EOF
$ crystal run src/main.cr <<EOF@[Include("/tmp/test.h", prefix: ["some"], remove_prefix: false)]lib TestendEOF
lib Test
fun some_function(x0 : SomeOpaque) : SomeOpaque*alias SomeOpaque = Void
alias SomeOpaque = LibC::Int
end
Generates:
I've made a little program to test this (bew/crystal_lib - test.cr):
Outputs:
We can see that the
typedef
generates thealias
, and the function declaration generates thefun
and thetype
.Looking at the code, I've found that the
type
is generated when mapping the function argument: the argument type is parsed as aPointerType to a StructOrUnion
. In the mapping of the pointer, a check is made for an opaque type, succeeds, then creates a new typedef with the same name as thealias
.I'm not sure how we could properly fix this, as I like the way it auto creates a
type
ofPointer(Void)
for opaque types. Maybe we should rename the generatedtype
and addPtr
(or_
) at the end, to ensure there's no conflict.The example would be:
The text was updated successfully, but these errors were encountered: