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
;
77 typedef union tree_node Symbol
;
86 Ungag(unsigned old
) : oldgag(old
) {}
87 ~Ungag() { global
.gag
= oldgag
; }
90 enum class ThreeState
: uint8_t
92 none
, // value not yet computed
97 void dsymbolSemantic(Dsymbol
*dsym
, Scope
*sc
);
98 void semantic2(Dsymbol
*dsym
, Scope
*sc
);
99 void semantic3(Dsymbol
*dsym
, Scope
* sc
);
117 /* State of symbol in winding its way through the passes of the compiler
119 enum class PASS
: uint8_t
121 initial
, // initial state
122 semantic
, // semantic() started
123 semanticdone
, // semantic() done
124 semantic2
, // semantic2() started
125 semantic2done
, // semantic2() done
126 semantic3
, // semantic3() started
127 semantic3done
, // semantic3() done
128 inline_
, // inline started
129 inlinedone
, // inline done
130 obj
// toObjFile() run
135 PASSinit
, // initial state
136 PASSsemantic
, // semantic() started
137 PASSsemanticdone
, // semantic() done
138 PASSsemantic2
, // semantic2() started
139 PASSsemantic2done
, // semantic2() done
140 PASSsemantic3
, // semantic3() started
141 PASSsemantic3done
, // semantic3() done
142 PASSinline
, // inline started
143 PASSinlinedone
, // inline done
144 PASSobj
// toObjFile() run
147 /* Flags for symbol search
151 IgnoreNone
= 0x00, // default
152 IgnorePrivateImports
= 0x01, // don't search private imports
153 IgnoreErrors
= 0x02, // don't give error messages
154 IgnoreAmbiguous
= 0x04, // return NULL if ambiguous
155 SearchLocalsOnly
= 0x08, // only look at locals (don't search imports)
156 SearchImportsOnly
= 0x10, // only look in imports
157 SearchUnqualifiedModule
= 0x20, // the module scope search is unqualified,
158 // meaning don't search imports in that scope,
159 // because qualified module searches search
161 IgnoreSymbolVisibility
= 0x80, // also find private and package protected symbols
162 TagNameSpace
= 0x100, // search ImportC tag symbol table
169 unsigned fieldOffset
;
177 struct DsymbolAttributes
;
179 class Dsymbol
: public ASTNode
184 Symbol
*csym
; // symbol for code generator
185 Loc loc
; // where defined
186 Scope
*_scope
; // !=NULL means context to use for semantic()
187 const utf8_t
*prettystring
;
189 DsymbolAttributes
* atts
;
191 bool errors
; // this symbol failed to pass semantic()
193 unsigned short localNum
; // perturb mangled name to avoid collisions with those in FuncDeclaration.localsymtab
194 static Dsymbol
*create(Identifier
*);
195 const char *toChars() const override
;
196 DeprecatedDeclaration
* depdecl();
197 CPPNamespaceDeclaration
* cppnamespace();
198 UserAttributeDeclaration
* userAttribDecl();
199 DeprecatedDeclaration
* depdecl(DeprecatedDeclaration
* dd
);
200 CPPNamespaceDeclaration
* cppnamespace(CPPNamespaceDeclaration
* ns
);
201 UserAttributeDeclaration
* userAttribDecl(UserAttributeDeclaration
* uad
);
202 virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments
204 const char *locToChars();
205 bool equals(const RootObject
* const o
) const override
;
206 bool isAnonymous() const;
207 void error(const Loc
&loc
, const char *format
, ...);
208 void error(const char *format
, ...);
209 void deprecation(const Loc
&loc
, const char *format
, ...);
210 void deprecation(const char *format
, ...);
211 bool checkDeprecated(const Loc
&loc
, Scope
*sc
);
214 Module
*getAccessModule();
215 Dsymbol
*pastMixin();
217 Dsymbol
*toParent2();
218 Dsymbol
*toParentDecl();
219 Dsymbol
*toParentLocal();
220 Dsymbol
*toParentP(Dsymbol
*p1
, Dsymbol
*p2
= NULL
);
221 TemplateInstance
*isInstantiated();
222 bool followInstantiationContext(Dsymbol
*p1
, Dsymbol
*p2
= NULL
);
223 TemplateInstance
*isSpeculative();
224 Ungag
ungagSpeculative();
226 // kludge for template.isSymbol()
227 DYNCAST
dyncast() const override final
{ return DYNCAST_DSYMBOL
; }
229 virtual Identifier
*getIdent();
230 virtual const char *toPrettyChars(bool QualifyTypes
= false);
231 virtual const char *kind() const;
232 virtual Dsymbol
*toAlias(); // resolve real symbol
233 virtual Dsymbol
*toAlias2();
234 virtual void addMember(Scope
*sc
, ScopeDsymbol
*sds
);
235 virtual void setScope(Scope
*sc
);
236 virtual void importAll(Scope
*sc
);
237 virtual Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= IgnoreNone
);
238 virtual bool overloadInsert(Dsymbol
*s
);
239 virtual uinteger_t
size(const Loc
&loc
);
240 virtual bool isforwardRef();
241 virtual AggregateDeclaration
*isThis(); // is a 'this' required to access the member
242 virtual bool isExport() const; // is Dsymbol exported?
243 virtual bool isImportedSymbol() const; // is Dsymbol imported?
244 virtual bool isDeprecated() const; // is Dsymbol deprecated?
245 virtual bool isOverloadable() const;
246 virtual LabelDsymbol
*isLabel(); // is this a LabelDsymbol?
247 AggregateDeclaration
*isMember(); // is toParent() an AggregateDeclaration?
248 AggregateDeclaration
*isMember2(); // is toParent2() an AggregateDeclaration?
249 AggregateDeclaration
*isMemberDecl(); // is toParentDecl() an AggregateDeclaration?
250 AggregateDeclaration
*isMemberLocal(); // is toParentLocal() an AggregateDeclaration?
251 ClassDeclaration
*isClassMember(); // isMember() is a ClassDeclaration?
252 virtual Type
*getType(); // is this a type?
253 virtual bool needThis(); // need a 'this' pointer?
254 virtual Visibility
visible();
255 virtual Dsymbol
*syntaxCopy(Dsymbol
*s
); // copy only syntax trees
256 virtual bool oneMember(Dsymbol
**ps
, Identifier
*ident
);
257 virtual void setFieldOffset(AggregateDeclaration
*ad
, FieldState
& fieldState
, bool isunion
);
258 virtual bool hasPointers();
259 virtual bool hasStaticCtorOrDtor();
260 virtual void addLocalClass(ClassDeclarations
*) { }
261 virtual void addObjcSymbols(ClassDeclarations
*, ClassDeclarations
*) { }
262 virtual void checkCtorConstInit() { }
264 virtual void addComment(const utf8_t
*comment
);
265 const utf8_t
*comment(); // current value of comment
267 UnitTestDeclaration
*ddocUnittest();
268 void ddocUnittest(UnitTestDeclaration
*);
272 // Eliminate need for dynamic_cast
273 virtual Package
*isPackage() { return NULL
; }
274 virtual Module
*isModule() { return NULL
; }
275 virtual EnumMember
*isEnumMember() { return NULL
; }
276 virtual TemplateDeclaration
*isTemplateDeclaration() { return NULL
; }
277 virtual TemplateInstance
*isTemplateInstance() { return NULL
; }
278 virtual TemplateMixin
*isTemplateMixin() { return NULL
; }
279 virtual ForwardingAttribDeclaration
*isForwardingAttribDeclaration() { return NULL
; }
280 virtual Nspace
*isNspace() { return NULL
; }
281 virtual Declaration
*isDeclaration() { return NULL
; }
282 virtual StorageClassDeclaration
*isStorageClassDeclaration(){ return NULL
; }
283 virtual ExpressionDsymbol
*isExpressionDsymbol() { return NULL
; }
284 virtual AliasAssign
*isAliasAssign() { return NULL
; }
285 virtual ThisDeclaration
*isThisDeclaration() { return NULL
; }
286 virtual BitFieldDeclaration
*isBitFieldDeclaration() { return NULL
; }
287 virtual TypeInfoDeclaration
*isTypeInfoDeclaration() { return NULL
; }
288 virtual TupleDeclaration
*isTupleDeclaration() { return NULL
; }
289 virtual AliasDeclaration
*isAliasDeclaration() { return NULL
; }
290 virtual AggregateDeclaration
*isAggregateDeclaration() { return NULL
; }
291 virtual FuncDeclaration
*isFuncDeclaration() { return NULL
; }
292 virtual FuncAliasDeclaration
*isFuncAliasDeclaration() { return NULL
; }
293 virtual OverDeclaration
*isOverDeclaration() { return NULL
; }
294 virtual FuncLiteralDeclaration
*isFuncLiteralDeclaration() { return NULL
; }
295 virtual CtorDeclaration
*isCtorDeclaration() { return NULL
; }
296 virtual PostBlitDeclaration
*isPostBlitDeclaration() { return NULL
; }
297 virtual DtorDeclaration
*isDtorDeclaration() { return NULL
; }
298 virtual StaticCtorDeclaration
*isStaticCtorDeclaration() { return NULL
; }
299 virtual StaticDtorDeclaration
*isStaticDtorDeclaration() { return NULL
; }
300 virtual SharedStaticCtorDeclaration
*isSharedStaticCtorDeclaration() { return NULL
; }
301 virtual SharedStaticDtorDeclaration
*isSharedStaticDtorDeclaration() { return NULL
; }
302 virtual InvariantDeclaration
*isInvariantDeclaration() { return NULL
; }
303 virtual UnitTestDeclaration
*isUnitTestDeclaration() { return NULL
; }
304 virtual NewDeclaration
*isNewDeclaration() { return NULL
; }
305 virtual VarDeclaration
*isVarDeclaration() { return NULL
; }
306 virtual VersionSymbol
*isVersionSymbol() { return NULL
; }
307 virtual DebugSymbol
*isDebugSymbol() { return NULL
; }
308 virtual ClassDeclaration
*isClassDeclaration() { return NULL
; }
309 virtual StructDeclaration
*isStructDeclaration() { return NULL
; }
310 virtual UnionDeclaration
*isUnionDeclaration() { return NULL
; }
311 virtual InterfaceDeclaration
*isInterfaceDeclaration() { return NULL
; }
312 virtual ScopeDsymbol
*isScopeDsymbol() { return NULL
; }
313 virtual ForwardingScopeDsymbol
*isForwardingScopeDsymbol() { return NULL
; }
314 virtual WithScopeSymbol
*isWithScopeSymbol() { return NULL
; }
315 virtual ArrayScopeSymbol
*isArrayScopeSymbol() { return NULL
; }
316 virtual Import
*isImport() { return NULL
; }
317 virtual EnumDeclaration
*isEnumDeclaration() { return NULL
; }
318 virtual SymbolDeclaration
*isSymbolDeclaration() { return NULL
; }
319 virtual AttribDeclaration
*isAttribDeclaration() { return NULL
; }
320 virtual AnonDeclaration
*isAnonDeclaration() { return NULL
; }
321 virtual CPPNamespaceDeclaration
*isCPPNamespaceDeclaration() { return NULL
; }
322 virtual VisibilityDeclaration
*isVisibilityDeclaration() { return NULL
; }
323 virtual OverloadSet
*isOverloadSet() { return NULL
; }
324 virtual CompileDeclaration
*isCompileDeclaration() { return NULL
; }
325 virtual StaticAssert
*isStaticAssert() { return NULL
; }
326 void accept(Visitor
*v
) override
{ v
->visit(this); }
329 // Dsymbol that generates a scope
331 class ScopeDsymbol
: public Dsymbol
334 Dsymbols
*members
; // all Dsymbol's in this scope
335 DsymbolTable
*symtab
; // members[] sorted into table
336 unsigned endlinnum
; // the linnumber of the statement after the scope (0 if unknown)
339 Dsymbols
*importedScopes
; // imported Dsymbol's
340 Visibility::Kind
*visibilities
; // array of `Visibility.Kind`, one for each import
342 BitArray accessiblePackages
, privateAccessiblePackages
;
345 ScopeDsymbol
*syntaxCopy(Dsymbol
*s
) override
;
346 Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= SearchLocalsOnly
) override
;
347 virtual void importScope(Dsymbol
*s
, Visibility visibility
);
348 virtual bool isPackageAccessible(Package
*p
, Visibility visibility
, int flags
= 0);
349 bool isforwardRef() override final
;
350 static void multiplyDefined(const Loc
&loc
, Dsymbol
*s1
, Dsymbol
*s2
);
351 const char *kind() const override
;
352 FuncDeclaration
*findGetMembers();
353 virtual Dsymbol
*symtabInsert(Dsymbol
*s
);
354 virtual Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
);
355 bool hasStaticCtorOrDtor() override
;
357 ScopeDsymbol
*isScopeDsymbol() override final
{ return this; }
358 void accept(Visitor
*v
) override
{ v
->visit(this); }
361 // With statement scope
363 class WithScopeSymbol final
: public ScopeDsymbol
366 WithStatement
*withstate
;
368 Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= SearchLocalsOnly
) override
;
370 WithScopeSymbol
*isWithScopeSymbol() override
{ return this; }
371 void accept(Visitor
*v
) override
{ v
->visit(this); }
374 // Array Index/Slice scope
376 class ArrayScopeSymbol final
: public ScopeDsymbol
379 RootObject
*arrayContent
;
383 Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= IgnoreNone
) override
;
385 ArrayScopeSymbol
*isArrayScopeSymbol() override
{ return this; }
386 void accept(Visitor
*v
) override
{ v
->visit(this); }
391 class OverloadSet final
: public Dsymbol
394 Dsymbols a
; // array of Dsymbols
396 void push(Dsymbol
*s
);
397 OverloadSet
*isOverloadSet() override
{ return this; }
398 const char *kind() const override
;
399 void accept(Visitor
*v
) override
{ v
->visit(this); }
402 // Forwarding ScopeDsymbol
404 class ForwardingScopeDsymbol final
: public ScopeDsymbol
407 Dsymbol
*symtabInsert(Dsymbol
*s
) override
;
408 Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
) override
;
409 void importScope(Dsymbol
*s
, Visibility visibility
) override
;
410 const char *kind() const override
;
412 ForwardingScopeDsymbol
*isForwardingScopeDsymbol() override
{ return this; }
415 class ExpressionDsymbol final
: public Dsymbol
420 ExpressionDsymbol
*isExpressionDsymbol() override
{ return this; }
423 // Table of Dsymbol's
425 class DsymbolTable final
: public RootObject
430 // Look up Identifier. Return Dsymbol if found, NULL if not.
431 Dsymbol
*lookup(Identifier
const * const ident
);
433 // Look for Dsymbol in table. If there, return it. If not, insert s and return that.
434 void update(Dsymbol
*s
);
436 // Insert Dsymbol in table. Return NULL if already there.
437 Dsymbol
*insert(Dsymbol
*s
);
438 Dsymbol
*insert(Identifier
const * const ident
, Dsymbol
*s
); // when ident and s are not the same
440 // Number of symbols in symbol table
441 size_t length() const;