2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2024 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
;
76 class CAsmDeclaration
;
79 typedef union tree_node Symbol
;
88 Ungag(unsigned old
) : oldgag(old
) {}
89 ~Ungag() { global
.gag
= oldgag
; }
92 enum class ThreeState
: uint8_t
94 none
, // value not yet computed
101 void dsymbolSemantic(Dsymbol
*dsym
, Scope
*sc
);
102 void semantic2(Dsymbol
*dsym
, Scope
*sc
);
103 void semantic3(Dsymbol
*dsym
, Scope
* sc
);
105 void asmSemantic(CAsmDeclaration
*ad
, Scope
*sc
);
107 void gccAsmSemantic(CAsmDeclaration
*ad
, Scope
*sc
);
126 /* State of symbol in winding its way through the passes of the compiler
128 enum class PASS
: uint8_t
130 initial
, // initial state
131 semantic
, // semantic() started
132 semanticdone
, // semantic() done
133 semantic2
, // semantic2() started
134 semantic2done
, // semantic2() done
135 semantic3
, // semantic3() started
136 semantic3done
, // semantic3() done
137 inline_
, // inline started
138 inlinedone
, // inline done
139 obj
// toObjFile() run
144 PASSinit
, // initial state
145 PASSsemantic
, // semantic() started
146 PASSsemanticdone
, // semantic() done
147 PASSsemantic2
, // semantic2() started
148 PASSsemantic2done
, // semantic2() done
149 PASSsemantic3
, // semantic3() started
150 PASSsemantic3done
, // semantic3() done
151 PASSinline
, // inline started
152 PASSinlinedone
, // inline done
153 PASSobj
// toObjFile() run
156 /* Flags for symbol search
158 typedef unsigned SearchOptFlags
;
159 enum class SearchOpt
: SearchOptFlags
161 all
= 0x00, // default
162 ignorePrivateImports
= 0x01, // don't search private imports
163 ignoreErrors
= 0x02, // don't give error messages
164 ignoreAmbiguous
= 0x04, // return NULL if ambiguous
165 localsOnly
= 0x08, // only look at locals (don't search imports)
166 importsOnly
= 0x10, // only look in imports
167 unqualifiedModule
= 0x20, // the module scope search is unqualified,
168 // meaning don't search imports in that scope,
169 // because qualified module searches search
171 tagNameSpace
= 0x40, // search ImportC tag symbol table
172 ignoreVisibility
= 0x80, // also find private and package protected symbols
179 unsigned fieldOffset
;
187 struct DsymbolAttributes
;
189 class Dsymbol
: public ASTNode
194 Symbol
*csym
; // symbol for code generator
195 Loc loc
; // where defined
196 Scope
*_scope
; // !=NULL means context to use for semantic()
197 const utf8_t
*prettystring
;
199 DsymbolAttributes
* atts
;
201 d_bool errors
; // this symbol failed to pass semantic()
203 unsigned short localNum
; // perturb mangled name to avoid collisions with those in FuncDeclaration.localsymtab
204 static Dsymbol
*create(Identifier
*);
205 const char *toChars() const override
;
206 DeprecatedDeclaration
* depdecl();
207 CPPNamespaceDeclaration
* cppnamespace();
208 UserAttributeDeclaration
* userAttribDecl();
209 DeprecatedDeclaration
* depdecl(DeprecatedDeclaration
* dd
);
210 CPPNamespaceDeclaration
* cppnamespace(CPPNamespaceDeclaration
* ns
);
211 UserAttributeDeclaration
* userAttribDecl(UserAttributeDeclaration
* uad
);
212 virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments
214 const char *locToChars();
215 bool equals(const RootObject
* const o
) const override
;
216 bool isAnonymous() const;
219 Module
*getAccessModule();
220 Dsymbol
*pastMixin();
222 Dsymbol
*toParent2();
223 Dsymbol
*toParentDecl();
224 Dsymbol
*toParentLocal();
225 Dsymbol
*toParentP(Dsymbol
*p1
, Dsymbol
*p2
= NULL
);
226 TemplateInstance
*isInstantiated();
227 bool followInstantiationContext(Dsymbol
*p1
, Dsymbol
*p2
= NULL
);
228 TemplateInstance
*isSpeculative();
229 Ungag
ungagSpeculative();
231 // kludge for template.isSymbol()
232 DYNCAST
dyncast() const override final
{ return DYNCAST_DSYMBOL
; }
234 virtual Identifier
*getIdent();
235 virtual const char *toPrettyChars(bool QualifyTypes
= false);
236 virtual const char *kind() const;
237 virtual Dsymbol
*toAlias(); // resolve real symbol
238 virtual Dsymbol
*toAlias2();
239 virtual bool overloadInsert(Dsymbol
*s
);
240 virtual uinteger_t
size(const Loc
&loc
);
241 virtual bool isforwardRef();
242 virtual AggregateDeclaration
*isThis(); // is a 'this' required to access the member
243 virtual bool isExport() const; // is Dsymbol exported?
244 virtual bool isImportedSymbol() const; // is Dsymbol imported?
245 virtual bool isDeprecated() const; // is Dsymbol deprecated?
246 virtual bool isOverloadable() const;
247 virtual LabelDsymbol
*isLabel(); // is this a LabelDsymbol?
248 AggregateDeclaration
*isMember(); // is toParent() an AggregateDeclaration?
249 AggregateDeclaration
*isMember2(); // is toParent2() an AggregateDeclaration?
250 AggregateDeclaration
*isMemberDecl(); // is toParentDecl() an AggregateDeclaration?
251 AggregateDeclaration
*isMemberLocal(); // is toParentLocal() an AggregateDeclaration?
252 ClassDeclaration
*isClassMember(); // isMember() is a ClassDeclaration?
253 virtual Type
*getType(); // is this a type?
254 virtual bool needThis(); // need a 'this' pointer?
255 virtual Visibility
visible();
256 virtual Dsymbol
*syntaxCopy(Dsymbol
*s
); // copy only syntax trees
257 virtual bool oneMember(Dsymbol
*&ps
, Identifier
*ident
);
258 virtual bool hasPointers();
259 virtual bool hasStaticCtorOrDtor();
260 virtual void addObjcSymbols(ClassDeclarations
*, ClassDeclarations
*) { }
261 virtual void checkCtorConstInit() { }
263 virtual void addComment(const utf8_t
*comment
);
264 const utf8_t
*comment(); // current value of comment
266 UnitTestDeclaration
*ddocUnittest();
267 void ddocUnittest(UnitTestDeclaration
*);
271 // Eliminate need for dynamic_cast
272 virtual Package
*isPackage() { return NULL
; }
273 virtual Module
*isModule() { return NULL
; }
274 virtual EnumMember
*isEnumMember() { return NULL
; }
275 virtual TemplateDeclaration
*isTemplateDeclaration() { return NULL
; }
276 virtual TemplateInstance
*isTemplateInstance() { return NULL
; }
277 virtual TemplateMixin
*isTemplateMixin() { return NULL
; }
278 virtual ForwardingAttribDeclaration
*isForwardingAttribDeclaration() { return NULL
; }
279 virtual Nspace
*isNspace() { return NULL
; }
280 virtual Declaration
*isDeclaration() { return NULL
; }
281 virtual StorageClassDeclaration
*isStorageClassDeclaration(){ return NULL
; }
282 virtual ExpressionDsymbol
*isExpressionDsymbol() { return NULL
; }
283 virtual AliasAssign
*isAliasAssign() { return NULL
; }
284 virtual ThisDeclaration
*isThisDeclaration() { return NULL
; }
285 virtual BitFieldDeclaration
*isBitFieldDeclaration() { return NULL
; }
286 virtual TypeInfoDeclaration
*isTypeInfoDeclaration() { return NULL
; }
287 virtual TupleDeclaration
*isTupleDeclaration() { return NULL
; }
288 virtual AliasDeclaration
*isAliasDeclaration() { return NULL
; }
289 virtual AggregateDeclaration
*isAggregateDeclaration() { return NULL
; }
290 virtual FuncDeclaration
*isFuncDeclaration() { return NULL
; }
291 virtual FuncAliasDeclaration
*isFuncAliasDeclaration() { return NULL
; }
292 virtual OverDeclaration
*isOverDeclaration() { return NULL
; }
293 virtual FuncLiteralDeclaration
*isFuncLiteralDeclaration() { return NULL
; }
294 virtual CtorDeclaration
*isCtorDeclaration() { return NULL
; }
295 virtual PostBlitDeclaration
*isPostBlitDeclaration() { return NULL
; }
296 virtual DtorDeclaration
*isDtorDeclaration() { return NULL
; }
297 virtual StaticCtorDeclaration
*isStaticCtorDeclaration() { return NULL
; }
298 virtual StaticDtorDeclaration
*isStaticDtorDeclaration() { return NULL
; }
299 virtual SharedStaticCtorDeclaration
*isSharedStaticCtorDeclaration() { return NULL
; }
300 virtual SharedStaticDtorDeclaration
*isSharedStaticDtorDeclaration() { return NULL
; }
301 virtual InvariantDeclaration
*isInvariantDeclaration() { return NULL
; }
302 virtual UnitTestDeclaration
*isUnitTestDeclaration() { return NULL
; }
303 virtual NewDeclaration
*isNewDeclaration() { return NULL
; }
304 virtual VarDeclaration
*isVarDeclaration() { return NULL
; }
305 virtual VersionSymbol
*isVersionSymbol() { return NULL
; }
306 virtual DebugSymbol
*isDebugSymbol() { return NULL
; }
307 virtual ClassDeclaration
*isClassDeclaration() { return NULL
; }
308 virtual StructDeclaration
*isStructDeclaration() { return NULL
; }
309 virtual UnionDeclaration
*isUnionDeclaration() { return NULL
; }
310 virtual InterfaceDeclaration
*isInterfaceDeclaration() { return NULL
; }
311 virtual ScopeDsymbol
*isScopeDsymbol() { return NULL
; }
312 virtual ForwardingScopeDsymbol
*isForwardingScopeDsymbol() { return NULL
; }
313 virtual WithScopeSymbol
*isWithScopeSymbol() { return NULL
; }
314 virtual ArrayScopeSymbol
*isArrayScopeSymbol() { return NULL
; }
315 virtual Import
*isImport() { return NULL
; }
316 virtual EnumDeclaration
*isEnumDeclaration() { return NULL
; }
317 virtual SymbolDeclaration
*isSymbolDeclaration() { return NULL
; }
318 virtual AttribDeclaration
*isAttribDeclaration() { return NULL
; }
319 virtual AnonDeclaration
*isAnonDeclaration() { return NULL
; }
320 virtual CPPNamespaceDeclaration
*isCPPNamespaceDeclaration() { return NULL
; }
321 virtual VisibilityDeclaration
*isVisibilityDeclaration() { return NULL
; }
322 virtual OverloadSet
*isOverloadSet() { return NULL
; }
323 virtual MixinDeclaration
*isMixinDeclaration() { return NULL
; }
324 virtual StaticAssert
*isStaticAssert() { return NULL
; }
325 virtual StaticIfDeclaration
*isStaticIfDeclaration() { return NULL
; }
326 virtual CAsmDeclaration
*isCAsmDeclaration() { return NULL
; }
327 void accept(Visitor
*v
) override
{ v
->visit(this); }
330 // Dsymbol that generates a scope
332 class ScopeDsymbol
: public Dsymbol
335 Dsymbols
*members
; // all Dsymbol's in this scope
336 DsymbolTable
*symtab
; // members[] sorted into table
337 unsigned endlinnum
; // the linnumber of the statement after the scope (0 if unknown)
338 Dsymbols
*importedScopes
; // imported Dsymbol's
339 Visibility::Kind
*visibilities
; // array of `Visibility.Kind`, one for each import
342 BitArray accessiblePackages
, privateAccessiblePackages
;
345 ScopeDsymbol
*syntaxCopy(Dsymbol
*s
) override
;
346 virtual void importScope(Dsymbol
*s
, Visibility visibility
);
347 virtual bool isPackageAccessible(Package
*p
, Visibility visibility
, SearchOptFlags flags
= (SearchOptFlags
)SearchOpt::all
);
348 bool isforwardRef() override final
;
349 static void multiplyDefined(const Loc
&loc
, Dsymbol
*s1
, Dsymbol
*s2
);
350 const char *kind() const override
;
351 virtual Dsymbol
*symtabInsert(Dsymbol
*s
);
352 virtual Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
);
353 bool hasStaticCtorOrDtor() override
;
355 ScopeDsymbol
*isScopeDsymbol() override final
{ return this; }
356 void accept(Visitor
*v
) override
{ v
->visit(this); }
359 // With statement scope
361 class WithScopeSymbol final
: public ScopeDsymbol
364 WithStatement
*withstate
;
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
;
378 ArrayScopeSymbol
*isArrayScopeSymbol() override
{ return this; }
379 void accept(Visitor
*v
) override
{ v
->visit(this); }
384 class OverloadSet final
: public Dsymbol
387 Dsymbols a
; // array of Dsymbols
389 void push(Dsymbol
*s
);
390 OverloadSet
*isOverloadSet() override
{ return this; }
391 const char *kind() const override
;
392 void accept(Visitor
*v
) override
{ v
->visit(this); }
395 // Forwarding ScopeDsymbol
397 class ForwardingScopeDsymbol final
: public ScopeDsymbol
400 Dsymbol
*symtabInsert(Dsymbol
*s
) override
;
401 Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
) override
;
402 void importScope(Dsymbol
*s
, Visibility visibility
) override
;
403 const char *kind() const override
;
405 ForwardingScopeDsymbol
*isForwardingScopeDsymbol() override
{ return this; }
408 class ExpressionDsymbol final
: public Dsymbol
413 ExpressionDsymbol
*isExpressionDsymbol() override
{ return this; }
416 class CAsmDeclaration final
: public Dsymbol
419 Expression
*code
; // string expression
421 CAsmDeclaration
*isCAsmDeclaration() override
{ return this; }
422 void accept(Visitor
*v
) override
{ v
->visit(this); }
425 // Table of Dsymbol's
427 class DsymbolTable final
: public RootObject
432 // Look up Identifier. Return Dsymbol if found, NULL if not.
433 Dsymbol
*lookup(Identifier
const * const ident
);
435 // Look for Dsymbol in table. If there, return it. If not, insert s and return that.
436 void update(Dsymbol
*s
);
438 // Insert Dsymbol in table. Return NULL if already there.
439 Dsymbol
*insert(Dsymbol
*s
);
440 Dsymbol
*insert(Identifier
const * const ident
, Dsymbol
*s
); // when ident and s are not the same
442 // Number of symbols in symbol table
443 size_t length() const;
448 void addMember(Dsymbol
*dsym
, Scope
*sc
, ScopeDsymbol
*sds
);
449 Dsymbol
*search(Dsymbol
*d
, const Loc
&loc
, Identifier
*ident
, SearchOptFlags flags
= (SearchOptFlags
)SearchOpt::localsOnly
);
450 void setScope(Dsymbol
*d
, Scope
*sc
);
451 void importAll(Dsymbol
*d
, Scope
*sc
);