2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * http://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * http://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
;
76 typedef union tree_node Symbol
;
85 Ungag(unsigned old
) : oldgag(old
) {}
86 ~Ungag() { global
.gag
= oldgag
; }
89 void dsymbolSemantic(Dsymbol
*dsym
, Scope
*sc
);
90 void semantic2(Dsymbol
*dsym
, Scope
*sc
);
91 void semantic3(Dsymbol
*dsym
, Scope
* sc
);
109 /* State of symbol in winding its way through the passes of the compiler
113 PASSinit
, // initial state
114 PASSsemantic
, // semantic() started
115 PASSsemanticdone
, // semantic() done
116 PASSsemantic2
, // semantic2() started
117 PASSsemantic2done
, // semantic2() done
118 PASSsemantic3
, // semantic3() started
119 PASSsemantic3done
, // semantic3() done
120 PASSinline
, // inline started
121 PASSinlinedone
, // inline done
122 PASSobj
// toObjFile() run
125 /* Flags for symbol search
129 IgnoreNone
= 0x00, // default
130 IgnorePrivateImports
= 0x01, // don't search private imports
131 IgnoreErrors
= 0x02, // don't give error messages
132 IgnoreAmbiguous
= 0x04, // return NULL if ambiguous
133 SearchLocalsOnly
= 0x08, // only look at locals (don't search imports)
134 SearchImportsOnly
= 0x10, // only look in imports
135 SearchUnqualifiedModule
= 0x20, // the module scope search is unqualified,
136 // meaning don't search imports in that scope,
137 // because qualified module searches search
139 IgnoreSymbolVisibility
= 0x80, // also find private and package protected symbols
140 TagNameSpace
= 0x100, // search ImportC tag symbol table
147 unsigned fieldOffset
;
153 class Dsymbol
: public ASTNode
158 /// C++ namespace this symbol belongs to
159 CPPNamespaceDeclaration
*namespace_
;
160 Symbol
*csym
; // symbol for code generator
161 Symbol
*isym
; // import version of csym
162 const utf8_t
*comment
; // documentation comment for this Dsymbol
163 Loc loc
; // where defined
164 Scope
*_scope
; // !=NULL means context to use for semantic()
165 const utf8_t
*prettystring
;
166 bool errors
; // this symbol failed to pass semantic()
168 unsigned short localNum
; // perturb mangled name to avoid collisions with those in FuncDeclaration.localsymtab
169 DeprecatedDeclaration
*depdecl
; // customized deprecation message
170 UserAttributeDeclaration
*userAttribDecl
; // user defined attributes
171 UnitTestDeclaration
*ddocUnittest
; // !=NULL means there's a ddoc unittest associated with this symbol (only use this with ddoc)
173 static Dsymbol
*create(Identifier
*);
174 const char *toChars() const;
175 virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments
177 const char *locToChars();
178 bool equals(const RootObject
*o
) const;
179 bool isAnonymous() const;
180 void error(const Loc
&loc
, const char *format
, ...);
181 void error(const char *format
, ...);
182 void deprecation(const Loc
&loc
, const char *format
, ...);
183 void deprecation(const char *format
, ...);
184 bool checkDeprecated(const Loc
&loc
, Scope
*sc
);
186 Module
*getAccessModule();
187 Dsymbol
*pastMixin();
189 Dsymbol
*toParent2();
190 Dsymbol
*toParentDecl();
191 Dsymbol
*toParentLocal();
192 Dsymbol
*toParentP(Dsymbol
*p1
, Dsymbol
*p2
= NULL
);
193 TemplateInstance
*isInstantiated();
194 bool followInstantiationContext(Dsymbol
*p1
, Dsymbol
*p2
= NULL
);
195 TemplateInstance
*isSpeculative();
196 Ungag
ungagSpeculative();
198 // kludge for template.isSymbol()
199 DYNCAST
dyncast() const { return DYNCAST_DSYMBOL
; }
201 virtual Identifier
*getIdent();
202 virtual const char *toPrettyChars(bool QualifyTypes
= false);
203 virtual const char *kind() const;
204 virtual Dsymbol
*toAlias(); // resolve real symbol
205 virtual Dsymbol
*toAlias2();
206 virtual void addMember(Scope
*sc
, ScopeDsymbol
*sds
);
207 virtual void setScope(Scope
*sc
);
208 virtual void importAll(Scope
*sc
);
209 virtual Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= IgnoreNone
);
210 virtual bool overloadInsert(Dsymbol
*s
);
211 virtual d_uns64
size(const Loc
&loc
);
212 virtual bool isforwardRef();
213 virtual AggregateDeclaration
*isThis(); // is a 'this' required to access the member
214 virtual bool isExport() const; // is Dsymbol exported?
215 virtual bool isImportedSymbol() const; // is Dsymbol imported?
216 virtual bool isDeprecated() const; // is Dsymbol deprecated?
217 virtual bool isOverloadable() const;
218 virtual LabelDsymbol
*isLabel(); // is this a LabelDsymbol?
219 AggregateDeclaration
*isMember(); // is toParent() an AggregateDeclaration?
220 AggregateDeclaration
*isMember2(); // is toParent2() an AggregateDeclaration?
221 AggregateDeclaration
*isMemberDecl(); // is toParentDecl() an AggregateDeclaration?
222 AggregateDeclaration
*isMemberLocal(); // is toParentLocal() an AggregateDeclaration?
223 ClassDeclaration
*isClassMember(); // isMember() is a ClassDeclaration?
224 virtual Type
*getType(); // is this a type?
225 virtual bool needThis(); // need a 'this' pointer?
226 virtual Visibility
visible();
227 virtual Dsymbol
*syntaxCopy(Dsymbol
*s
); // copy only syntax trees
228 virtual bool oneMember(Dsymbol
**ps
, Identifier
*ident
);
229 virtual void setFieldOffset(AggregateDeclaration
*ad
, FieldState
& fieldState
, bool isunion
);
230 virtual bool hasPointers();
231 virtual bool hasStaticCtorOrDtor();
232 virtual void addLocalClass(ClassDeclarations
*) { }
233 virtual void addObjcSymbols(ClassDeclarations
*, ClassDeclarations
*) { }
234 virtual void checkCtorConstInit() { }
236 virtual void addComment(const utf8_t
*comment
);
240 // Eliminate need for dynamic_cast
241 virtual Package
*isPackage() { return NULL
; }
242 virtual Module
*isModule() { return NULL
; }
243 virtual EnumMember
*isEnumMember() { return NULL
; }
244 virtual TemplateDeclaration
*isTemplateDeclaration() { return NULL
; }
245 virtual TemplateInstance
*isTemplateInstance() { return NULL
; }
246 virtual TemplateMixin
*isTemplateMixin() { return NULL
; }
247 virtual ForwardingAttribDeclaration
*isForwardingAttribDeclaration() { return NULL
; }
248 virtual Nspace
*isNspace() { return NULL
; }
249 virtual Declaration
*isDeclaration() { return NULL
; }
250 virtual StorageClassDeclaration
*isStorageClassDeclaration(){ return NULL
; }
251 virtual ExpressionDsymbol
*isExpressionDsymbol() { return NULL
; }
252 virtual AliasAssign
*isAliasAssign() { return NULL
; }
253 virtual ThisDeclaration
*isThisDeclaration() { return NULL
; }
254 virtual BitFieldDeclaration
*isBitFieldDeclaration() { return NULL
; }
255 virtual TypeInfoDeclaration
*isTypeInfoDeclaration() { return NULL
; }
256 virtual TupleDeclaration
*isTupleDeclaration() { return NULL
; }
257 virtual AliasDeclaration
*isAliasDeclaration() { return NULL
; }
258 virtual AggregateDeclaration
*isAggregateDeclaration() { return NULL
; }
259 virtual FuncDeclaration
*isFuncDeclaration() { return NULL
; }
260 virtual FuncAliasDeclaration
*isFuncAliasDeclaration() { return NULL
; }
261 virtual OverDeclaration
*isOverDeclaration() { return NULL
; }
262 virtual FuncLiteralDeclaration
*isFuncLiteralDeclaration() { return NULL
; }
263 virtual CtorDeclaration
*isCtorDeclaration() { return NULL
; }
264 virtual PostBlitDeclaration
*isPostBlitDeclaration() { return NULL
; }
265 virtual DtorDeclaration
*isDtorDeclaration() { return NULL
; }
266 virtual StaticCtorDeclaration
*isStaticCtorDeclaration() { return NULL
; }
267 virtual StaticDtorDeclaration
*isStaticDtorDeclaration() { return NULL
; }
268 virtual SharedStaticCtorDeclaration
*isSharedStaticCtorDeclaration() { return NULL
; }
269 virtual SharedStaticDtorDeclaration
*isSharedStaticDtorDeclaration() { return NULL
; }
270 virtual InvariantDeclaration
*isInvariantDeclaration() { return NULL
; }
271 virtual UnitTestDeclaration
*isUnitTestDeclaration() { return NULL
; }
272 virtual NewDeclaration
*isNewDeclaration() { return NULL
; }
273 virtual VarDeclaration
*isVarDeclaration() { return NULL
; }
274 virtual VersionSymbol
*isVersionSymbol() { return NULL
; }
275 virtual DebugSymbol
*isDebugSymbol() { return NULL
; }
276 virtual ClassDeclaration
*isClassDeclaration() { return NULL
; }
277 virtual StructDeclaration
*isStructDeclaration() { return NULL
; }
278 virtual UnionDeclaration
*isUnionDeclaration() { return NULL
; }
279 virtual InterfaceDeclaration
*isInterfaceDeclaration() { return NULL
; }
280 virtual ScopeDsymbol
*isScopeDsymbol() { return NULL
; }
281 virtual ForwardingScopeDsymbol
*isForwardingScopeDsymbol() { return NULL
; }
282 virtual WithScopeSymbol
*isWithScopeSymbol() { return NULL
; }
283 virtual ArrayScopeSymbol
*isArrayScopeSymbol() { return NULL
; }
284 virtual Import
*isImport() { return NULL
; }
285 virtual EnumDeclaration
*isEnumDeclaration() { return NULL
; }
286 virtual SymbolDeclaration
*isSymbolDeclaration() { return NULL
; }
287 virtual AttribDeclaration
*isAttribDeclaration() { return NULL
; }
288 virtual AnonDeclaration
*isAnonDeclaration() { return NULL
; }
289 virtual CPPNamespaceDeclaration
*isCPPNamespaceDeclaration() { return NULL
; }
290 virtual VisibilityDeclaration
*isVisibilityDeclaration() { return NULL
; }
291 virtual OverloadSet
*isOverloadSet() { return NULL
; }
292 virtual CompileDeclaration
*isCompileDeclaration() { return NULL
; }
293 void accept(Visitor
*v
) { v
->visit(this); }
296 // Dsymbol that generates a scope
298 class ScopeDsymbol
: public Dsymbol
301 Dsymbols
*members
; // all Dsymbol's in this scope
302 DsymbolTable
*symtab
; // members[] sorted into table
303 unsigned endlinnum
; // the linnumber of the statement after the scope (0 if unknown)
306 Dsymbols
*importedScopes
; // imported Dsymbol's
307 Visibility::Kind
*visibilities
; // array of `Visibility.Kind`, one for each import
309 BitArray accessiblePackages
, privateAccessiblePackages
;
312 ScopeDsymbol
*syntaxCopy(Dsymbol
*s
);
313 Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= SearchLocalsOnly
);
314 virtual void importScope(Dsymbol
*s
, Visibility visibility
);
315 virtual bool isPackageAccessible(Package
*p
, Visibility visibility
, int flags
= 0);
317 static void multiplyDefined(const Loc
&loc
, Dsymbol
*s1
, Dsymbol
*s2
);
318 const char *kind() const;
319 FuncDeclaration
*findGetMembers();
320 virtual Dsymbol
*symtabInsert(Dsymbol
*s
);
321 virtual Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
);
322 bool hasStaticCtorOrDtor();
324 ScopeDsymbol
*isScopeDsymbol() { return this; }
325 void accept(Visitor
*v
) { v
->visit(this); }
328 // With statement scope
330 class WithScopeSymbol
: public ScopeDsymbol
333 WithStatement
*withstate
;
335 Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= SearchLocalsOnly
);
337 WithScopeSymbol
*isWithScopeSymbol() { return this; }
338 void accept(Visitor
*v
) { v
->visit(this); }
341 // Array Index/Slice scope
343 class ArrayScopeSymbol
: public ScopeDsymbol
346 RootObject
*arrayContent
;
350 Dsymbol
*search(const Loc
&loc
, Identifier
*ident
, int flags
= IgnoreNone
);
352 ArrayScopeSymbol
*isArrayScopeSymbol() { return this; }
353 void accept(Visitor
*v
) { v
->visit(this); }
358 class OverloadSet
: public Dsymbol
361 Dsymbols a
; // array of Dsymbols
363 void push(Dsymbol
*s
);
364 OverloadSet
*isOverloadSet() { return this; }
365 const char *kind() const;
366 void accept(Visitor
*v
) { v
->visit(this); }
369 // Forwarding ScopeDsymbol
371 class ForwardingScopeDsymbol
: public ScopeDsymbol
374 ScopeDsymbol
*forward
;
376 Dsymbol
*symtabInsert(Dsymbol
*s
);
377 Dsymbol
*symtabLookup(Dsymbol
*s
, Identifier
*id
);
378 void importScope(Dsymbol
*s
, Visibility visibility
);
379 const char *kind() const;
381 ForwardingScopeDsymbol
*isForwardingScopeDsymbol() { return this; }
384 class ExpressionDsymbol
: public Dsymbol
389 ExpressionDsymbol
*isExpressionDsymbol() { return this; }
392 // Table of Dsymbol's
394 class DsymbolTable
: public RootObject
399 // Look up Identifier. Return Dsymbol if found, NULL if not.
400 Dsymbol
*lookup(Identifier
const * const ident
);
402 // Look for Dsymbol in table. If there, return it. If not, insert s and return that.
403 void update(Dsymbol
*s
);
405 // Insert Dsymbol in table. Return NULL if already there.
406 Dsymbol
*insert(Dsymbol
*s
);
407 Dsymbol
*insert(Identifier
const * const ident
, Dsymbol
*s
); // when ident and s are not the same
409 // Number of symbols in symbol table
410 size_t length() const;