2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2023 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * https://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * https://www.boost.org/LICENSE_1_0.txt
8 * https://github.com/dlang/dmd/blob/master/src/dmd/dsymbol.h
13 #include "root/port.h"
16 #include "arraytypes.h"
19 class CPPNamespaceDeclaration
;
24 class ThisDeclaration
;
25 class BitFieldDeclaration
;
26 class TypeInfoDeclaration
;
27 class TupleDeclaration
;
28 class AliasDeclaration
;
29 class AggregateDeclaration
;
30 class EnumDeclaration
;
31 class ClassDeclaration
;
32 class InterfaceDeclaration
;
33 class StructDeclaration
;
34 class UnionDeclaration
;
35 class FuncDeclaration
;
36 class FuncAliasDeclaration
;
37 class OverDeclaration
;
38 class FuncLiteralDeclaration
;
39 class CtorDeclaration
;
40 class PostBlitDeclaration
;
41 class DtorDeclaration
;
42 class StaticCtorDeclaration
;
43 class StaticDtorDeclaration
;
44 class SharedStaticCtorDeclaration
;
45 class SharedStaticDtorDeclaration
;
46 class InvariantDeclaration
;
47 class UnitTestDeclaration
;
50 class AttribDeclaration
;
51 class VisibilityDeclaration
;
60 class ForwardingScopeDsymbol
;
61 class TemplateDeclaration
;
62 class TemplateInstance
;
64 class ForwardingAttribDeclaration
;
67 class WithScopeSymbol
;
68 class ArrayScopeSymbol
;
69 class SymbolDeclaration
;
71 class ExpressionDsymbol
;
75 class StaticIfDeclaration
;
78 typedef union tree_node Symbol
;
87 Ungag(unsigned old
) : oldgag(old
) {}
88 ~Ungag() { global
.gag
= oldgag
; }
91 enum class ThreeState
: uint8_t
93 none
, // value not yet computed
98 void dsymbolSemantic(Dsymbol
*dsym
, Scope
*sc
);
99 void semantic2(Dsymbol
*dsym
, Scope
*sc
);
100 void semantic3(Dsymbol
*dsym
, Scope
* sc
);
118 /* State of symbol in winding its way through the passes of the compiler
120 enum class PASS
: uint8_t
122 initial
, // initial state
123 semantic
, // semantic() started
124 semanticdone
, // semantic() done
125 semantic2
, // semantic2() started
126 semantic2done
, // semantic2() done
127 semantic3
, // semantic3() started
128 semantic3done
, // semantic3() done
129 inline_
, // inline started
130 inlinedone
, // inline done
131 obj
// toObjFile() run
136 PASSinit
, // initial state
137 PASSsemantic
, // semantic() started
138 PASSsemanticdone
, // semantic() done
139 PASSsemantic2
, // semantic2() started
140 PASSsemantic2done
, // semantic2() done
141 PASSsemantic3
, // semantic3() started
142 PASSsemantic3done
, // semantic3() done
143 PASSinline
, // inline started
144 PASSinlinedone
, // inline done
145 PASSobj
// toObjFile() run
148 /* Flags for symbol search
152 IgnoreNone
= 0x00, // default
153 IgnorePrivateImports
= 0x01, // don't search private imports
154 IgnoreErrors
= 0x02, // don't give error messages
155 IgnoreAmbiguous
= 0x04, // return NULL if ambiguous
156 SearchLocalsOnly
= 0x08, // only look at locals (don't search imports)
157 SearchImportsOnly
= 0x10, // only look in imports
158 SearchUnqualifiedModule
= 0x20, // the module scope search is unqualified,
159 // meaning don't search imports in that scope,
160 // because qualified module searches search
162 IgnoreSymbolVisibility
= 0x80, // also find private and package protected symbols
163 TagNameSpace
= 0x100, // search ImportC tag symbol table
170 unsigned fieldOffset
;
178 struct DsymbolAttributes
;
180 class Dsymbol
: public ASTNode
185 Symbol
*csym
; // symbol for code generator
186 Loc loc
; // where defined
187 Scope
*_scope
; // !=NULL means context to use for semantic()
188 const utf8_t
*prettystring
;
190 DsymbolAttributes
* atts
;
192 d_bool errors
; // this symbol failed to pass semantic()
194 unsigned short localNum
; // perturb mangled name to avoid collisions with those in FuncDeclaration.localsymtab
195 static Dsymbol
*create(Identifier
*);
196 const char *toChars() const override
;
197 DeprecatedDeclaration
* depdecl();
198 CPPNamespaceDeclaration
* cppnamespace();
199 UserAttributeDeclaration
* userAttribDecl();
200 DeprecatedDeclaration
* depdecl(DeprecatedDeclaration
* dd
);
201 CPPNamespaceDeclaration
* cppnamespace(CPPNamespaceDeclaration
* ns
);
202 UserAttributeDeclaration
* userAttribDecl(UserAttributeDeclaration
* uad
);
203 virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments
205 const char *locToChars();
206 bool equals(const RootObject
* const o
) const override
;
207 bool isAnonymous() const;
208 bool checkDeprecated(const Loc
&loc
, Scope
*sc
);
211 Module
*getAccessModule();
212 Dsymbol
*pastMixin();
214 Dsymbol
*toParent2();
215 Dsymbol
*toParentDecl();
216 Dsymbol
*toParentLocal();
217 Dsymbol
*toParentP(Dsymbol
*p1
, Dsymbol
*p2
= NULL
);
218 TemplateInstance
*isInstantiated();
219 bool followInstantiationContext(Dsymbol
*p1
, Dsymbol
*p2
= NULL
);
220 TemplateInstance
*isSpeculative();
221 Ungag
ungagSpeculative();
223 // kludge for template.isSymbol()
224 DYNCAST
dyncast() const override final
{ return DYNCAST_DSYMBOL
; }
226 virtual Identifier
*getIdent();
227 virtual const char *toPrettyChars(bool QualifyTypes
= false);
228 virtual const char *kind() const;
229 virtual Dsymbol
*toAlias(); // resolve real symbol
230 virtual Dsymbol
*toAlias2();
231 virtual void addMember(Scope
*sc
, ScopeDsymbol
*sds
);
232 virtual void setScope(Scope
*sc
);
233 virtual void importAll(Scope
*sc
);
234 virtual Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= IgnoreNone
);
235 virtual bool overloadInsert(Dsymbol
*s
);
236 virtual uinteger_t
size(const Loc
&loc
);
237 virtual bool isforwardRef();
238 virtual AggregateDeclaration
*isThis(); // is a 'this' required to access the member
239 virtual bool isExport() const; // is Dsymbol exported?
240 virtual bool isImportedSymbol() const; // is Dsymbol imported?
241 virtual bool isDeprecated() const; // is Dsymbol deprecated?
242 virtual bool isOverloadable() const;
243 virtual LabelDsymbol
*isLabel(); // is this a LabelDsymbol?
244 AggregateDeclaration
*isMember(); // is toParent() an AggregateDeclaration?
245 AggregateDeclaration
*isMember2(); // is toParent2() an AggregateDeclaration?
246 AggregateDeclaration
*isMemberDecl(); // is toParentDecl() an AggregateDeclaration?
247 AggregateDeclaration
*isMemberLocal(); // is toParentLocal() an AggregateDeclaration?
248 ClassDeclaration
*isClassMember(); // isMember() is a ClassDeclaration?
249 virtual Type
*getType(); // is this a type?
250 virtual bool needThis(); // need a 'this' pointer?
251 virtual Visibility
visible();
252 virtual Dsymbol
*syntaxCopy(Dsymbol
*s
); // copy only syntax trees
253 virtual bool oneMember(Dsymbol
**ps
, Identifier
*ident
);
254 virtual void setFieldOffset(AggregateDeclaration
*ad
, FieldState
& fieldState
, bool isunion
);
255 virtual bool hasPointers();
256 virtual bool hasStaticCtorOrDtor();
257 virtual void addObjcSymbols(ClassDeclarations
*, ClassDeclarations
*) { }
258 virtual void checkCtorConstInit() { }
260 virtual void addComment(const utf8_t
*comment
);
261 const utf8_t
*comment(); // current value of comment
263 UnitTestDeclaration
*ddocUnittest();
264 void ddocUnittest(UnitTestDeclaration
*);
268 // Eliminate need for dynamic_cast
269 virtual Package
*isPackage() { return NULL
; }
270 virtual Module
*isModule() { return NULL
; }
271 virtual EnumMember
*isEnumMember() { return NULL
; }
272 virtual TemplateDeclaration
*isTemplateDeclaration() { return NULL
; }
273 virtual TemplateInstance
*isTemplateInstance() { return NULL
; }
274 virtual TemplateMixin
*isTemplateMixin() { return NULL
; }
275 virtual ForwardingAttribDeclaration
*isForwardingAttribDeclaration() { return NULL
; }
276 virtual Nspace
*isNspace() { return NULL
; }
277 virtual Declaration
*isDeclaration() { return NULL
; }
278 virtual StorageClassDeclaration
*isStorageClassDeclaration(){ return NULL
; }
279 virtual ExpressionDsymbol
*isExpressionDsymbol() { return NULL
; }
280 virtual AliasAssign
*isAliasAssign() { return NULL
; }
281 virtual ThisDeclaration
*isThisDeclaration() { return NULL
; }
282 virtual BitFieldDeclaration
*isBitFieldDeclaration() { return NULL
; }
283 virtual TypeInfoDeclaration
*isTypeInfoDeclaration() { return NULL
; }
284 virtual TupleDeclaration
*isTupleDeclaration() { return NULL
; }
285 virtual AliasDeclaration
*isAliasDeclaration() { return NULL
; }
286 virtual AggregateDeclaration
*isAggregateDeclaration() { return NULL
; }
287 virtual FuncDeclaration
*isFuncDeclaration() { return NULL
; }
288 virtual FuncAliasDeclaration
*isFuncAliasDeclaration() { return NULL
; }
289 virtual OverDeclaration
*isOverDeclaration() { return NULL
; }
290 virtual FuncLiteralDeclaration
*isFuncLiteralDeclaration() { return NULL
; }
291 virtual CtorDeclaration
*isCtorDeclaration() { return NULL
; }
292 virtual PostBlitDeclaration
*isPostBlitDeclaration() { return NULL
; }
293 virtual DtorDeclaration
*isDtorDeclaration() { return NULL
; }
294 virtual StaticCtorDeclaration
*isStaticCtorDeclaration() { return NULL
; }
295 virtual StaticDtorDeclaration
*isStaticDtorDeclaration() { return NULL
; }
296 virtual SharedStaticCtorDeclaration
*isSharedStaticCtorDeclaration() { return NULL
; }
297 virtual SharedStaticDtorDeclaration
*isSharedStaticDtorDeclaration() { return NULL
; }
298 virtual InvariantDeclaration
*isInvariantDeclaration() { return NULL
; }
299 virtual UnitTestDeclaration
*isUnitTestDeclaration() { return NULL
; }
300 virtual NewDeclaration
*isNewDeclaration() { return NULL
; }
301 virtual VarDeclaration
*isVarDeclaration() { return NULL
; }
302 virtual VersionSymbol
*isVersionSymbol() { return NULL
; }
303 virtual DebugSymbol
*isDebugSymbol() { return NULL
; }
304 virtual ClassDeclaration
*isClassDeclaration() { return NULL
; }
305 virtual StructDeclaration
*isStructDeclaration() { return NULL
; }
306 virtual UnionDeclaration
*isUnionDeclaration() { return NULL
; }
307 virtual InterfaceDeclaration
*isInterfaceDeclaration() { return NULL
; }
308 virtual ScopeDsymbol
*isScopeDsymbol() { return NULL
; }
309 virtual ForwardingScopeDsymbol
*isForwardingScopeDsymbol() { return NULL
; }
310 virtual WithScopeSymbol
*isWithScopeSymbol() { return NULL
; }
311 virtual ArrayScopeSymbol
*isArrayScopeSymbol() { return NULL
; }
312 virtual Import
*isImport() { return NULL
; }
313 virtual EnumDeclaration
*isEnumDeclaration() { return NULL
; }
314 virtual SymbolDeclaration
*isSymbolDeclaration() { return NULL
; }
315 virtual AttribDeclaration
*isAttribDeclaration() { return NULL
; }
316 virtual AnonDeclaration
*isAnonDeclaration() { return NULL
; }
317 virtual CPPNamespaceDeclaration
*isCPPNamespaceDeclaration() { return NULL
; }
318 virtual VisibilityDeclaration
*isVisibilityDeclaration() { return NULL
; }
319 virtual OverloadSet
*isOverloadSet() { return NULL
; }
320 virtual MixinDeclaration
*isMixinDeclaration() { return NULL
; }
321 virtual StaticAssert
*isStaticAssert() { return NULL
; }
322 virtual StaticIfDeclaration
*isStaticIfDeclaration() { return NULL
; }
323 void accept(Visitor
*v
) override
{ v
->visit(this); }
326 // Dsymbol that generates a scope
328 class ScopeDsymbol
: public Dsymbol
331 Dsymbols
*members
; // all Dsymbol's in this scope
332 DsymbolTable
*symtab
; // members[] sorted into table
333 unsigned endlinnum
; // the linnumber of the statement after the scope (0 if unknown)
336 Dsymbols
*importedScopes
; // imported Dsymbol's
337 Visibility::Kind
*visibilities
; // array of `Visibility.Kind`, one for each import
339 BitArray accessiblePackages
, privateAccessiblePackages
;
342 ScopeDsymbol
*syntaxCopy(Dsymbol
*s
) override
;
343 Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= SearchLocalsOnly
) override
;
344 virtual void importScope(Dsymbol
*s
, Visibility visibility
);
345 virtual bool isPackageAccessible(Package
*p
, Visibility visibility
, int flags
= 0);
346 bool isforwardRef() override final
;
347 static void multiplyDefined(const Loc
&loc
, Dsymbol
*s1
, Dsymbol
*s2
);
348 const char *kind() const override
;
349 FuncDeclaration
*findGetMembers();
350 virtual Dsymbol
*symtabInsert(Dsymbol
*s
);
351 virtual Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
);
352 bool hasStaticCtorOrDtor() override
;
354 ScopeDsymbol
*isScopeDsymbol() override final
{ return this; }
355 void accept(Visitor
*v
) override
{ v
->visit(this); }
358 // With statement scope
360 class WithScopeSymbol final
: public ScopeDsymbol
363 WithStatement
*withstate
;
365 Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= SearchLocalsOnly
) override
;
367 WithScopeSymbol
*isWithScopeSymbol() override
{ return this; }
368 void accept(Visitor
*v
) override
{ v
->visit(this); }
371 // Array Index/Slice scope
373 class ArrayScopeSymbol final
: public ScopeDsymbol
376 RootObject
*arrayContent
;
380 Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= IgnoreNone
) override
;
382 ArrayScopeSymbol
*isArrayScopeSymbol() override
{ return this; }
383 void accept(Visitor
*v
) override
{ v
->visit(this); }
388 class OverloadSet final
: public Dsymbol
391 Dsymbols a
; // array of Dsymbols
393 void push(Dsymbol
*s
);
394 OverloadSet
*isOverloadSet() override
{ return this; }
395 const char *kind() const override
;
396 void accept(Visitor
*v
) override
{ v
->visit(this); }
399 // Forwarding ScopeDsymbol
401 class ForwardingScopeDsymbol final
: public ScopeDsymbol
404 Dsymbol
*symtabInsert(Dsymbol
*s
) override
;
405 Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
) override
;
406 void importScope(Dsymbol
*s
, Visibility visibility
) override
;
407 const char *kind() const override
;
409 ForwardingScopeDsymbol
*isForwardingScopeDsymbol() override
{ return this; }
412 class ExpressionDsymbol final
: public Dsymbol
417 ExpressionDsymbol
*isExpressionDsymbol() override
{ return this; }
420 // Table of Dsymbol's
422 class DsymbolTable final
: public RootObject
427 // Look up Identifier. Return Dsymbol if found, NULL if not.
428 Dsymbol
*lookup(Identifier
const * const ident
);
430 // Look for Dsymbol in table. If there, return it. If not, insert s and return that.
431 void update(Dsymbol
*s
);
433 // Insert Dsymbol in table. Return NULL if already there.
434 Dsymbol
*insert(Dsymbol
*s
);
435 Dsymbol
*insert(Identifier
const * const ident
, Dsymbol
*s
); // when ident and s are not the same
437 // Number of symbols in symbol table
438 size_t length() const;