jwiegley / c2hsc

Utility for creating .hsc files from C API header files
BSD 3-Clause "New" or "Revised" License
26 stars 15 forks source link

Duplicate opaque_t struct #15

Closed nh2 closed 10 years ago

nh2 commented 10 years ago

bug.hs:

struct MyTypeImpl;
typedef struct MyTypeImpl* MyType;

Then run c2hsc --prefix=Bug bug.h.

We obtain:

{-# OPTIONS_GHC -fno-warn-unused-imports #-}
#include <bindings.dsl.h>
#include "bug.h"
module Bug.Bug where
import Foreign.Ptr
#strict_import

{- struct MyTypeImpl; -}
#opaque_t struct MyTypeImpl
{- typedef struct MyTypeImpl * MyType; -}
#opaque_t struct MyTypeImpl
#synonym_t MyType , <struct MyTypeImpl>

Notice the duplicate #opaque_t struct MyTypeImpl

nh2 commented 10 years ago

Also it might be interesting to note that in https://github.com/jwiegley/c2hsc/blob/cc4d02/Main.hs#L388 all declSpecs are traversed in reverse.

nh2 commented 10 years ago

I think the issue is in

              -- If the type is a typedef, record the equivalence so we can
              -- look it up later
              case declSpecs of
                CStorageSpec (CTypedef _):_ -> do
                  when (declMatches fm dx) $ do
                    appendHsc $ "{- " ++ P.render (pretty dx) ++ " -}"
                    appendType declSpecs nm

                  dname <- declSpecTypeName declSpecs
                  unless (null dname || dname == "<" ++ nm ++ ">") $ do
                    when (declMatches fm dx) $
                      appendHsc $ "#synonym_t " ++ nm ++ " , " ++ dname

The appendType is the part that emits the #opaque_t for the typedef. Should that even happen for a typedef, or should #synonym_t be the only thing emitted from it?

nh2 commented 10 years ago

I think these are some cases we have to deal with:


struct MyTypeImpl;
typedef struct MyTypeImpl* MyType;

typedef struct MyStruct {
  int x;
} MyStructType;

typedef struct MyStructEmpty MyStructEmptyType;
jwiegley commented 10 years ago

Well, if the typedef actually declares the type, we need to omit it; but if it was previously declared, we shouldn't. I think I'll have to keep a table to recall which types have been seen as declared already.

jwiegley commented 10 years ago

nh2: working now

jwiegley commented 10 years ago

I mean, @nh2 :)

nh2 commented 10 years ago

Thanks!