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
150 typedef uint SearchOptFlags
;
151 enum class SearchOpt
: SearchOptFlags
153 all
= 0x00, // default
154 ignorePrivateImports
= 0x01, // don't search private imports
155 ignoreErrors
= 0x02, // don't give error messages
156 ignoreAmbiguous
= 0x04, // return NULL if ambiguous
157 localsOnly
= 0x08, // only look at locals (don't search imports)
158 importsOnly
= 0x10, // only look in imports
159 unqualifiedModule
= 0x20, // the module scope search is unqualified,
160 // meaning don't search imports in that scope,
161 // because qualified module searches search
163 tagNameSpace
= 0x40, // search ImportC tag symbol table
164 ignoreVisibility
= 0x80, // also find private and package protected symbols
171 unsigned fieldOffset
;
179 struct DsymbolAttributes
;
181 class Dsymbol
: public ASTNode
186 Symbol
*csym
; // symbol for code generator
187 Loc loc
; // where defined
188 Scope
*_scope
; // !=NULL means context to use for semantic()
189 const utf8_t
*prettystring
;
191 DsymbolAttributes
* atts
;
193 d_bool errors
; // this symbol failed to pass semantic()
195 unsigned short localNum
; // perturb mangled name to avoid collisions with those in FuncDeclaration.localsymtab
196 static Dsymbol
*create(Identifier
*);
197 const char *toChars() const override
;
198 DeprecatedDeclaration
* depdecl();
199 CPPNamespaceDeclaration
* cppnamespace();
200 UserAttributeDeclaration
* userAttribDecl();
201 DeprecatedDeclaration
* depdecl(DeprecatedDeclaration
* dd
);
202 CPPNamespaceDeclaration
* cppnamespace(CPPNamespaceDeclaration
* ns
);
203 UserAttributeDeclaration
* userAttribDecl(UserAttributeDeclaration
* uad
);
204 virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments
206 const char *locToChars();
207 bool equals(const RootObject
* const o
) const override
;
208 bool isAnonymous() const;
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 bool overloadInsert(Dsymbol
*s
);
232 virtual uinteger_t
size(const Loc
&loc
);
233 virtual bool isforwardRef();
234 virtual AggregateDeclaration
*isThis(); // is a 'this' required to access the member
235 virtual bool isExport() const; // is Dsymbol exported?
236 virtual bool isImportedSymbol() const; // is Dsymbol imported?
237 virtual bool isDeprecated() const; // is Dsymbol deprecated?
238 virtual bool isOverloadable() const;
239 virtual LabelDsymbol
*isLabel(); // is this a LabelDsymbol?
240 AggregateDeclaration
*isMember(); // is toParent() an AggregateDeclaration?
241 AggregateDeclaration
*isMember2(); // is toParent2() an AggregateDeclaration?
242 AggregateDeclaration
*isMemberDecl(); // is toParentDecl() an AggregateDeclaration?
243 AggregateDeclaration
*isMemberLocal(); // is toParentLocal() an AggregateDeclaration?
244 ClassDeclaration
*isClassMember(); // isMember() is a ClassDeclaration?
245 virtual Type
*getType(); // is this a type?
246 virtual bool needThis(); // need a 'this' pointer?
247 virtual Visibility
visible();
248 virtual Dsymbol
*syntaxCopy(Dsymbol
*s
); // copy only syntax trees
249 virtual bool oneMember(Dsymbol
**ps
, Identifier
*ident
);
250 virtual bool hasPointers();
251 virtual bool hasStaticCtorOrDtor();
252 virtual void addObjcSymbols(ClassDeclarations
*, ClassDeclarations
*) { }
253 virtual void checkCtorConstInit() { }
255 virtual void addComment(const utf8_t
*comment
);
256 const utf8_t
*comment(); // current value of comment
258 UnitTestDeclaration
*ddocUnittest();
259 void ddocUnittest(UnitTestDeclaration
*);
263 // Eliminate need for dynamic_cast
264 virtual Package
*isPackage() { return NULL
; }
265 virtual Module
*isModule() { return NULL
; }
266 virtual EnumMember
*isEnumMember() { return NULL
; }
267 virtual TemplateDeclaration
*isTemplateDeclaration() { return NULL
; }
268 virtual TemplateInstance
*isTemplateInstance() { return NULL
; }
269 virtual TemplateMixin
*isTemplateMixin() { return NULL
; }
270 virtual ForwardingAttribDeclaration
*isForwardingAttribDeclaration() { return NULL
; }
271 virtual Nspace
*isNspace() { return NULL
; }
272 virtual Declaration
*isDeclaration() { return NULL
; }
273 virtual StorageClassDeclaration
*isStorageClassDeclaration(){ return NULL
; }
274 virtual ExpressionDsymbol
*isExpressionDsymbol() { return NULL
; }
275 virtual AliasAssign
*isAliasAssign() { return NULL
; }
276 virtual ThisDeclaration
*isThisDeclaration() { return NULL
; }
277 virtual BitFieldDeclaration
*isBitFieldDeclaration() { return NULL
; }
278 virtual TypeInfoDeclaration
*isTypeInfoDeclaration() { return NULL
; }
279 virtual TupleDeclaration
*isTupleDeclaration() { return NULL
; }
280 virtual AliasDeclaration
*isAliasDeclaration() { return NULL
; }
281 virtual AggregateDeclaration
*isAggregateDeclaration() { return NULL
; }
282 virtual FuncDeclaration
*isFuncDeclaration() { return NULL
; }
283 virtual FuncAliasDeclaration
*isFuncAliasDeclaration() { return NULL
; }
284 virtual OverDeclaration
*isOverDeclaration() { return NULL
; }
285 virtual FuncLiteralDeclaration
*isFuncLiteralDeclaration() { return NULL
; }
286 virtual CtorDeclaration
*isCtorDeclaration() { return NULL
; }
287 virtual PostBlitDeclaration
*isPostBlitDeclaration() { return NULL
; }
288 virtual DtorDeclaration
*isDtorDeclaration() { return NULL
; }
289 virtual StaticCtorDeclaration
*isStaticCtorDeclaration() { return NULL
; }
290 virtual StaticDtorDeclaration
*isStaticDtorDeclaration() { return NULL
; }
291 virtual SharedStaticCtorDeclaration
*isSharedStaticCtorDeclaration() { return NULL
; }
292 virtual SharedStaticDtorDeclaration
*isSharedStaticDtorDeclaration() { return NULL
; }
293 virtual InvariantDeclaration
*isInvariantDeclaration() { return NULL
; }
294 virtual UnitTestDeclaration
*isUnitTestDeclaration() { return NULL
; }
295 virtual NewDeclaration
*isNewDeclaration() { return NULL
; }
296 virtual VarDeclaration
*isVarDeclaration() { return NULL
; }
297 virtual VersionSymbol
*isVersionSymbol() { return NULL
; }
298 virtual DebugSymbol
*isDebugSymbol() { return NULL
; }
299 virtual ClassDeclaration
*isClassDeclaration() { return NULL
; }
300 virtual StructDeclaration
*isStructDeclaration() { return NULL
; }
301 virtual UnionDeclaration
*isUnionDeclaration() { return NULL
; }
302 virtual InterfaceDeclaration
*isInterfaceDeclaration() { return NULL
; }
303 virtual ScopeDsymbol
*isScopeDsymbol() { return NULL
; }
304 virtual ForwardingScopeDsymbol
*isForwardingScopeDsymbol() { return NULL
; }
305 virtual WithScopeSymbol
*isWithScopeSymbol() { return NULL
; }
306 virtual ArrayScopeSymbol
*isArrayScopeSymbol() { return NULL
; }
307 virtual Import
*isImport() { return NULL
; }
308 virtual EnumDeclaration
*isEnumDeclaration() { return NULL
; }
309 virtual SymbolDeclaration
*isSymbolDeclaration() { return NULL
; }
310 virtual AttribDeclaration
*isAttribDeclaration() { return NULL
; }
311 virtual AnonDeclaration
*isAnonDeclaration() { return NULL
; }
312 virtual CPPNamespaceDeclaration
*isCPPNamespaceDeclaration() { return NULL
; }
313 virtual VisibilityDeclaration
*isVisibilityDeclaration() { return NULL
; }
314 virtual OverloadSet
*isOverloadSet() { return NULL
; }
315 virtual MixinDeclaration
*isMixinDeclaration() { return NULL
; }
316 virtual StaticAssert
*isStaticAssert() { return NULL
; }
317 virtual StaticIfDeclaration
*isStaticIfDeclaration() { return NULL
; }
318 void accept(Visitor
*v
) override
{ v
->visit(this); }
321 // Dsymbol that generates a scope
323 class ScopeDsymbol
: public Dsymbol
326 Dsymbols
*members
; // all Dsymbol's in this scope
327 DsymbolTable
*symtab
; // members[] sorted into table
328 unsigned endlinnum
; // the linnumber of the statement after the scope (0 if unknown)
329 Dsymbols
*importedScopes
; // imported Dsymbol's
330 Visibility::Kind
*visibilities
; // array of `Visibility.Kind`, one for each import
333 BitArray accessiblePackages
, privateAccessiblePackages
;
336 ScopeDsymbol
*syntaxCopy(Dsymbol
*s
) override
;
337 virtual void importScope(Dsymbol
*s
, Visibility visibility
);
338 virtual bool isPackageAccessible(Package
*p
, Visibility visibility
, SearchOptFlags flags
= (SearchOptFlags
)SearchOpt::all
);
339 bool isforwardRef() override final
;
340 static void multiplyDefined(const Loc
&loc
, Dsymbol
*s1
, Dsymbol
*s2
);
341 const char *kind() const override
;
342 virtual Dsymbol
*symtabInsert(Dsymbol
*s
);
343 virtual Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
);
344 bool hasStaticCtorOrDtor() override
;
346 ScopeDsymbol
*isScopeDsymbol() override final
{ return this; }
347 void accept(Visitor
*v
) override
{ v
->visit(this); }
350 // With statement scope
352 class WithScopeSymbol final
: public ScopeDsymbol
355 WithStatement
*withstate
;
358 WithScopeSymbol
*isWithScopeSymbol() override
{ return this; }
359 void accept(Visitor
*v
) override
{ v
->visit(this); }
362 // Array Index/Slice scope
364 class ArrayScopeSymbol final
: public ScopeDsymbol
367 RootObject
*arrayContent
;
369 ArrayScopeSymbol
*isArrayScopeSymbol() override
{ return this; }
370 void accept(Visitor
*v
) override
{ v
->visit(this); }
375 class OverloadSet final
: public Dsymbol
378 Dsymbols a
; // array of Dsymbols
380 void push(Dsymbol
*s
);
381 OverloadSet
*isOverloadSet() override
{ return this; }
382 const char *kind() const override
;
383 void accept(Visitor
*v
) override
{ v
->visit(this); }
386 // Forwarding ScopeDsymbol
388 class ForwardingScopeDsymbol final
: public ScopeDsymbol
391 Dsymbol
*symtabInsert(Dsymbol
*s
) override
;
392 Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
) override
;
393 void importScope(Dsymbol
*s
, Visibility visibility
) override
;
394 const char *kind() const override
;
396 ForwardingScopeDsymbol
*isForwardingScopeDsymbol() override
{ return this; }
399 class ExpressionDsymbol final
: public Dsymbol
404 ExpressionDsymbol
*isExpressionDsymbol() override
{ return this; }
407 // Table of Dsymbol's
409 class DsymbolTable final
: public RootObject
414 // Look up Identifier. Return Dsymbol if found, NULL if not.
415 Dsymbol
*lookup(Identifier
const * const ident
);
417 // Look for Dsymbol in table. If there, return it. If not, insert s and return that.
418 void update(Dsymbol
*s
);
420 // Insert Dsymbol in table. Return NULL if already there.
421 Dsymbol
*insert(Dsymbol
*s
);
422 Dsymbol
*insert(Identifier
const * const ident
, Dsymbol
*s
); // when ident and s are not the same
424 // Number of symbols in symbol table
425 size_t length() const;
428 void addMember(Dsymbol
*dsym
, Scope
*sc
, ScopeDsymbol
*sds
);
429 Dsymbol
*search(Dsymbol
*d
, const Loc
&loc
, Identifier
*ident
, SearchOptFlags flags
= (SearchOptFlags
)SearchOpt::localsOnly
);
430 bool checkDeprecated(Dsymbol
*d
, const Loc
&loc
, Scope
*sc
);
431 void setScope(Dsymbol
*d
, Scope
*sc
);
432 void importAll(Dsymbol
*d
, Scope
*sc
);
433 void setFieldOffset(Dsymbol
*d
, AggregateDeclaration
*ad
, FieldState
& fieldState
, bool isunion
);