Skip to content
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

Struct declarations in C header files cause compilation errors in D modules. #143

Open
russel opened this issue Jun 4, 2017 · 7 comments
Labels

Comments

@russel
Copy link
Contributor

russel commented Jun 4, 2017

I have stripped this down from a real libdvbv5 problem. We have two C header files, one contains a declaration of a struct, the other the definition of the struct. So in a.h:

struct thingy;

and in b,h:

struct thingy { int i; };

Applying dstep we get the D modules as expected. a.d:

struct thingy;

and b.d

struct thingy { int i; }

Nothing seemingly wrong so far. But now let us introduce a D module using this type, c.d

import a;
import b;

struct blah {
    thingy* x;
}

Trying to compile this will lead to:

c.d(5): Error: a.thingy at a.d(1) conflicts with b.thingy at b.d(1)

I think I (sort of) understand what is happening here, and I feel this has to be considered as a DStep bug.

The struct declaration in a.h – which just states that there is a name of a struct that will be defined elsewhere, to be handled as and when – is becoming in a.d an empty struct definition. Because of the switch from header files to modules a.thingy is not the same as b.thingy in the D modules, whereas in the C header files thingy in a.h is just a declaration of thingy in b.h to keep the compiler happy. (thingy* only needs to know that thingy is a type name, it doesn't need the structure information, so a simple declaration of thingy is all that is required for C (and C++) codes.)

The problem is that DStep is only taking a local view of all statements in the C code. In the case of a struct (or class) declaration, with no definition, it is not correct to write out what in D becomes a definition.

The problem here clearly relates to Issue #139 . The problem can only be solved by DStep not generating struct and class statements for C struct and class declarations (only for struct and class definitions) and inserting the import statement for the generated module where the struct and class definitions are.

@ciechowoj
Copy link
Contributor

This is related to #101 as well.

@jacob-carlborg
Copy link
Owner

I think this can be solved with the assumption that all files will be passed to DStep at the same time.

@Robert-M-Muench
Copy link

Robert-M-Muench commented Apr 28, 2019

The current version still has this problem. I explicitly feed all files to DStep on the command line which results in DStep generating separate files. However, empty struct forward references are still generated.

Since the pattern mentioned by the OP is very common, I think DStep should support this and avoid generating unnecessary struct forward references/empty lines.

@jacob-carlborg
Copy link
Owner

The current version still has this problem.

The bug is still open 😉.

@Robert-M-Muench
Copy link

Yes, just wanted to confirm it :-)

Isn't the fix pretty simple? Just don't emit any lines of the form: ```struct mystruct;````

In my case, I commented all those lines and things work.

@jacob-carlborg
Copy link
Owner

Isn't the fix pretty simple? Just don't emit any lines of the form: ```struct mystruct;````

No. There are cases when no corresponding definition of the struct exists.

@Robert-M-Muench
Copy link

Well, then it could be cross-checked and only omitted if a struct is not found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants