]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/dmd/nspace.c
2bd6b96981352d9e4a4f12e53bd5f9e272975119
2 // Compiler implementation of the D programming language
3 // Copyright: Copyright (C) 2014-2018 by The D Language Foundation, All Rights Reserved
4 // Authors: Walter Bright, http://www.digitalmars.com
5 // License: http://boost.org/LICENSE_1_0.txt
6 // Source: https://github.com/D-Programming-Language/dmd/blob/master/src/nspace.c
16 #include "identifier.h"
19 /* This implements namespaces.
22 Nspace::Nspace(Loc loc
, Identifier
*ident
, Dsymbols
*members
)
25 //printf("Nspace::Nspace(ident = %s)\n", ident->toChars());
27 this->members
= members
;
30 Dsymbol
*Nspace::syntaxCopy(Dsymbol
*)
32 Nspace
*ns
= new Nspace(loc
, ident
, NULL
);
33 return ScopeDsymbol::syntaxCopy(ns
);
36 void Nspace::addMember(Scope
*sc
, ScopeDsymbol
*sds
)
38 ScopeDsymbol::addMember(sc
, sds
);
42 symtab
= new DsymbolTable();
43 // The namespace becomes 'imported' into the enclosing scope
44 for (Scope
*sce
= sc
; 1; sce
= sce
->enclosing
)
46 ScopeDsymbol
*sds2
= sce
->scopesym
;
49 sds2
->importScope(this, Prot(PROTpublic
));
55 sc
->linkage
= LINKcpp
; // namespaces default to C++ linkage
57 for (size_t i
= 0; i
< members
->dim
; i
++)
59 Dsymbol
*s
= (*members
)[i
];
60 //printf("add %s to scope %s\n", s->toChars(), toChars());
61 s
->addMember(sc
, this);
67 void Nspace::setScope(Scope
*sc
)
69 ScopeDsymbol::setScope(sc
);
74 sc
->linkage
= LINKcpp
; // namespaces default to C++ linkage
76 for (size_t i
= 0; i
< members
->dim
; i
++)
78 Dsymbol
*s
= (*members
)[i
];
85 void Nspace::semantic(Scope
*sc
)
87 if (semanticRun
!= PASSinit
)
97 semanticRun
= PASSsemantic
;
103 sc
->linkage
= LINKcpp
; // note that namespaces imply C++ linkage
106 for (size_t i
= 0; i
< members
->dim
; i
++)
108 Dsymbol
*s
= (*members
)[i
];
112 for (size_t i
= 0; i
< members
->dim
; i
++)
114 Dsymbol
*s
= (*members
)[i
];
119 semanticRun
= PASSsemanticdone
;
122 void Nspace::semantic2(Scope
*sc
)
124 if (semanticRun
>= PASSsemantic2
)
126 semanticRun
= PASSsemantic2
;
131 sc
->linkage
= LINKcpp
;
132 for (size_t i
= 0; i
< members
->dim
; i
++)
134 Dsymbol
*s
= (*members
)[i
];
141 void Nspace::semantic3(Scope
*sc
)
143 if (semanticRun
>= PASSsemantic3
)
145 semanticRun
= PASSsemantic3
;
149 sc
->linkage
= LINKcpp
;
150 for (size_t i
= 0; i
< members
->dim
; i
++)
152 Dsymbol
*s
= (*members
)[i
];
159 const char *Nspace::kind()
164 bool Nspace::oneMember(Dsymbol
**ps
, Identifier
*ident
)
166 return Dsymbol::oneMember(ps
, ident
);
169 Dsymbol
*Nspace::search(const Loc
&loc
, Identifier
*ident
, int flags
)
171 //printf("%s::Nspace::search('%s')\n", toChars(), ident->toChars());
172 if (_scope
&& !symtab
)
175 if (!members
|| !symtab
) // opaque or semantic() is not yet called
177 error("is forward referenced when looking for '%s'", ident
->toChars());
181 return ScopeDsymbol::search(loc
, ident
, flags
);
184 int Nspace::apply(Dsymbol_apply_ft_t fp
, void *param
)
188 for (size_t i
= 0; i
< members
->dim
; i
++)
190 Dsymbol
*s
= (*members
)[i
];
193 if (s
->apply(fp
, param
))
201 bool Nspace::hasPointers()
203 //printf("Nspace::hasPointers() %s\n", toChars());
207 for (size_t i
= 0; i
< members
->dim
; i
++)
209 Dsymbol
*s
= (*members
)[i
];
210 //printf(" s = %s %s\n", s->kind(), s->toChars());
211 if (s
->hasPointers())
220 void Nspace::setFieldOffset(AggregateDeclaration
*ad
, unsigned *poffset
, bool isunion
)
222 //printf("Nspace::setFieldOffset() %s\n", toChars());
223 if (_scope
) // if fwd reference
224 semantic(NULL
); // try to resolve it
227 for (size_t i
= 0; i
< members
->dim
; i
++)
229 Dsymbol
*s
= (*members
)[i
];
230 //printf("\t%s\n", s->toChars());
231 s
->setFieldOffset(ad
, poffset
, isunion
);