D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7961 - Add support for C++ namespaces
Summary: Add support for C++ namespaces
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: Lionello Lunesu
URL:
Keywords: preapproved
Depends on:
Blocks:
 
Reported: 2012-04-21 05:57 UTC by Dejan Lekic
Modified: 2014-06-24 02:13 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Dejan Lekic 2012-04-21 05:57:52 UTC
On 12/14/2011 1:47 PM, Dejan Lekic wrote:
 As subject says. I am pretty much happy with the current C++ support. The
 only thing I need is to be able to call functions from namespace(s). Any
 plans to add this feature?


Walter's response:

I hadn't planned to, but it's a good idea. I suggest adding it as an
enhancement 
request on bugzilla.


I forgot about it, but lately I need namespace support again, so I am filing this feature request here. :) This is, I believe, a very important feature as many people deal with C++ libraries lately.
Comment 1 Lionello Lunesu 2013-11-08 01:28:20 UTC
I already looked into this. Could have it done by Monday.
Comment 2 yebblies 2013-11-08 03:23:12 UTC
How do you plan to do it on win32?
Comment 3 Lionello Lunesu 2013-11-09 22:13:59 UTC
(In reply to comment #2)
> How do you plan to do it on win32?

https://en.wikipedia.org/wiki/Visual_C%2B%2B_name_mangling
Comment 4 yebblies 2013-11-09 22:21:37 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > How do you plan to do it on win32?
> 
> https://en.wikipedia.org/wiki/Visual_C%2B%2B_name_mangling

I mean, how do you plan to implement this given that on win32, C++ name mangling is done by the dmc backend code rather than in the frontend like all other platforms.
Comment 5 Lionello Lunesu 2013-11-09 23:57:31 UTC
(In reply to comment #4)
> (In reply to comment #3)
> > (In reply to comment #2)
> > > How do you plan to do it on win32?
> > 
> > https://en.wikipedia.org/wiki/Visual_C%2B%2B_name_mangling
> 
> I mean, how do you plan to implement this given that on win32, C++ name
> mangling is done by the dmc backend code rather than in the frontend like all
> other platforms.

Oh, you're way ahead on me on that one. I had not noticed the difference. But since the code is available for both, it should be possible to do, one way or the other. Any ideas?

Or perhaps it's time to make win32 more like win64. And get rid of optlink in the process :)

What we have to decide first, though, is how to declare the namespaces in D. UDA?

I don't think we should be using the module name, since that's completely unrelated in C++ and D. Declaring functions as statics in a struct could work, but would conflict with actual nested methods.
Comment 6 yebblies 2013-11-10 00:14:48 UTC
(In reply to comment #5)
> 
> Oh, you're way ahead on me on that one. I had not noticed the difference. But
> since the code is available for both, it should be possible to do, one way or
> the other. Any ideas?
> 

https://github.com/D-Programming-Language/dmd/pull/2074

Or possibly, adapting the code in there to use the existing mangling interface, and not rely on https://github.com/D-Programming-Language/dmd/pull/2356

> Or perhaps it's time to make win32 more like win64. And get rid of optlink in
> the process :)
> 

Yeah I'd stay away from that mess.

> What we have to decide first, though, is how to declare the namespaces in D.
> UDA?
> 
> I don't think we should be using the module name, since that's completely
> unrelated in C++ and D. Declaring functions as statics in a struct could work,
> but would conflict with actual nested methods.

pragma(cpp_namespace, "blah") ...

pragma(cpp_namespace, "blah", "nested") ...
or maybe
pragma(cpp_namespace, ["blah", "nested"]) ...
Comment 7 Jacob Carlborg 2013-11-10 02:26:21 UTC
Isn't a C++ namespace only a mangle thing? In that case can't we just use pragma(mangle) and create a library function which helps making it a bit more high level:

pragam(managle, namespace("foo::bar")) void x ();
Comment 8 yebblies 2013-11-10 04:17:13 UTC
(In reply to comment #7)
> Isn't a C++ namespace only a mangle thing? In that case can't we just use
> pragma(mangle) and create a library function which helps making it a bit more
> high level:
> 
> pragam(managle, namespace("foo::bar")) void x ();

We would have to mangle in the function name and type as well.  Not really something you want to do manually.
Comment 9 Jacob Carlborg 2013-11-10 04:22:30 UTC
(In reply to comment #8)

> We would have to mangle in the function name and type as well.  Not really
> something you want to do manually.

No, right. This doesn't work either due to forward reference:

pragma(mangle, namespace!("bar", foo)) void foo ();

Is this something we want to, and can, make work? I'm just trying to come up with a solution that doesn't require a compiler change or a change that can be applied more generally.
Comment 10 yebblies 2013-11-10 04:43:55 UTC
(In reply to comment #9)
> (In reply to comment #8)
> 
> > We would have to mangle in the function name and type as well.  Not really
> > something you want to do manually.
> 
> No, right. This doesn't work either due to forward reference:
> 
> pragma(mangle, namespace!("bar", foo)) void foo ();
> 
> Is this something we want to, and can, make work? I'm just trying to come up
> with a solution that doesn't require a compiler change or a change that can be
> applied more generally.

> pragma(mangle, namespace!("bar", "foo", void function())) extern(C++) void foo ();

The way to do it is to generate both the pragma(mangle) args and the function signature from the same source.  But really, pragma(mangle) is too blunt a tool for this.

Especially considering you'd usually want to apply the namespace to a whole bunch of declarations.

It might also be worth considering the behavior of

pragma(cpp_namespace, "foo")
pragma(cpp_namespace, "bar")
extern(C++) void baz();
Comment 11 Andrei Alexandrescu 2013-11-10 07:51:10 UTC
Guess we could use extern(DMC++) vs extern(C++) etc.
Comment 12 Jacob Carlborg 2013-11-11 02:40:48 UTC
(In reply to comment #5)

> What we have to decide first, though, is how to declare the namespaces in D.
> UDA?

I like the UDA idea, if a library solution cannot work. It will look quite similar to C++:

@namespace("foo")
{
    @namespace("bar")
    {
        void x ();
    }
}

It can also support a less verbose syntax for nested namespaces:

@namespace("foo::bar") void x ();

Or

@namespace("foo", "bar") void x ();

Or implement AST macros and do it with library code :)
Comment 13 Lionello Lunesu 2013-11-14 08:45:13 UTC
https://github.com/D-Programming-Language/dmd/pull/2767

Using @namespace, like so:

struct namespace{string s;}
extern(C++):
void noattrib(int);
@namespace("test")
{
  void attrib(T);
  struct T;
}

Would personally love to have "::" support as well, but should we allow @namespace("::") to reset? Might get too complicated. Current system maps nicely to C++.
Comment 14 Jesse Phillips 2014-06-24 02:13:12 UTC
Resolving because of DIP 61 pull request: https://github.com/D-Programming-Language/dmd/pull/3517