| 1 |
/** |
| 2 |
* A scoped C++ namespace symbol |
| 3 |
* |
| 4 |
* D supports the following syntax to declare symbol(s) as being part of a |
| 5 |
* C++ namespace: |
| 6 |
* --- |
| 7 |
* extern (C++, "myNamespace") { /+ Symbols +/ } // String variant |
| 8 |
* extern (C++, SomeNamespace) { /+ Other symbols +/ } // Identifier variant |
| 9 |
* --- |
| 10 |
* The first form is an attribute and only affects mangling, and is implemented |
| 11 |
* in `dmd.attrib`. |
| 12 |
* The second form introduces a named scope and allows symbols to be refered |
| 13 |
* to with or without the namespace name, much like a named template mixin, |
| 14 |
* and is implemented in this module. |
| 15 |
* --- |
| 16 |
* extern (C++, Basket) |
| 17 |
* { |
| 18 |
* struct StrawBerry; |
| 19 |
* void swapFood (Strawberry* f1, Strawberry* f2); |
| 20 |
* } |
| 21 |
* void main () |
| 22 |
* { |
| 23 |
* Basket.StrawBerry fruit1; |
| 24 |
* StrawBerry fruit2; |
| 25 |
* Basket.swapFood(fruit1, fruit2); |
| 26 |
* swapFood(fruit1, fruit2); |
| 27 |
* } |
| 28 |
* --- |
| 29 |
* Hence the `Nspace` symbol implements the usual `ScopeDsymbol` semantics. |
| 30 |
* |
| 31 |
* Note that it implies `extern(C++)` so it cannot be used as a generic |
| 32 |
* named scope. Additionally, `Nspace` with the same `Identifier` can be |
| 33 |
* defined in different module (as C++ allows a namespace to be spread accross |
| 34 |
* translation units), but symbols in it should be considered |
| 35 |
* part of the same scope. Lastly, not all possible C++ namespace names |
| 36 |
* are valid D identifier. |
| 37 |
* |
| 38 |
* See_Also: https://github.com/dlang/dmd/pull/10031 |
| 39 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 40 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 41 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 42 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/nspace.d, _nspace.d) |
| 43 |
* Documentation: https://dlang.org/phobos/dmd_nspace.html |
| 44 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/nspace.d |
| 45 |
*/ |
| 46 |
|
| 47 |
module dmd.nspace; |
| 48 |
|
| 49 |
import dmd.arraytypes; |
| 50 |
import dmd.dsymbol; |
| 51 |
import dmd.expression; |
| 52 |
import dmd.identifier; |
| 53 |
import dmd.location; |
| 54 |
import dmd.visitor; |
| 55 |
import core.stdc.stdio; |
| 56 |
|
| 57 |
/// Ditto |
| 58 |
extern (C++) final class Nspace : ScopeDsymbol |
| 59 |
{ |
| 60 |
/** |
| 61 |
* Namespace identifier resolved during semantic. |
| 62 |
*/ |
| 63 |
Expression identExp; |
| 64 |
|
| 65 |
extern (D) this(Loc loc, Identifier ident, Expression identExp, Dsymbols* members) |
| 66 |
{ |
| 67 |
super(loc, ident); |
| 68 |
//printf("Nspace::Nspace(ident = %s)\n", ident.toChars()); |
| 69 |
this.dsym = DSYM.nspace; |
| 70 |
this.members = members; |
| 71 |
this.identExp = identExp; |
| 72 |
} |
| 73 |
|
| 74 |
override Nspace syntaxCopy(Dsymbol s) |
| 75 |
{ |
| 76 |
auto ns = new Nspace(loc, ident, identExp, null); |
| 77 |
ScopeDsymbol.syntaxCopy(ns); |
| 78 |
return ns; |
| 79 |
} |
| 80 |
|
| 81 |
override bool hasPointers() |
| 82 |
{ |
| 83 |
//printf("Nspace::hasPointers() %s\n", toChars()); |
| 84 |
return members.foreachDsymbol( (s) { return s.hasPointers(); } ) != 0; |
| 85 |
} |
| 86 |
|
| 87 |
override const(char)* kind() const |
| 88 |
{ |
| 89 |
return "namespace"; |
| 90 |
} |
| 91 |
|
| 92 |
override void accept(Visitor v) |
| 93 |
{ |
| 94 |
v.visit(this); |
| 95 |
} |
| 96 |
} |