www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ImportC

reply Emmanuel <emmankoko519 gmail.com> writes:
Hi all,

I have been trying to make .di files clean as a lot of dirty code 
gets emitted in them when generated from C code.

```
typedef unsigned int uint;
```
Very allowed in C libs and becomes
```
alias unit = unit;
```

Issues opened on that and other unwanted code in the .di files.

We realize that the D generated for the simple example above is 
invalid because the alias semantics is broken by self aliasing 
and also the uint is a token.

I decided to do a little work on this by appending all D keywords 
in C files that are used as identifiers with ‘_’ .  Then we make 
a loud noise in the man pages about that. If any C identifier 
happens to be a D keyword, use a `_` appended version when 
referencing it from D.

So that we can get
```
alias uint_ = uint;
```

It does also affect declarations.

C could have
```
int version = 5 ;
```

When you want to reference it from D,

```
if (_version == 5) // version variable is from C
     // do something
```

It was a little buggy initially but I’ve been able to register it 
at points that now correctly does that with no code breaking. But 
I don’t know how welcoming that will be to the D community. But 
this looks like the best fix for now and I think will save us 
from future trouble. I’ve discussed with Dennis Korpel as well.

Happy to hear what you guys think!
Dec 05
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
It may be better to go with a prefix instead.

__c_

The double underscore is reserved so it won't conflict.
Dec 05
next sibling parent Sergey <kornburn yandex.ru> writes:
On Saturday, 6 December 2025 at 06:56:22 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 It may be better to go with a prefix instead.

 __c_

 The double underscore is reserved so it won't conflict.
Sounds good. Overall idea also seems reasonable. And maybe it could be even used for some tooling integration.
Dec 06
prev sibling parent Emmanuel <emmankoko519 gmail.com> writes:
On Saturday, 6 December 2025 at 06:56:22 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 It may be better to go with a prefix instead.

 __c_

 The double underscore is reserved so it won't conflict.
I am a little unsure about using double underscores, I think it will make C symbols in the symbol table a bit weird. so maybe simple to just follow D style.
Dec 07
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
We've been using a _ postfix in the DMD source code for avoiding collisions
with 
keywords. Not many people do that, so it seems to be effective.
Dec 06
parent Emmanuel <emmankoko519 gmail.com> writes:
On Saturday, 6 December 2025 at 21:29:09 UTC, Walter Bright wrote:
 We've been using a _ postfix in the DMD source code for 
 avoiding collisions with keywords. Not many people do that, so 
 it seems to be effective.
yessss, I think using the _ postfix might be it.
Dec 07