d: Merge upstream dmd, druntime f1a045928e
[official-gcc.git] / gcc / d / dmd / dsymbol.h
blobd029c00fc1ca7dd8be4ba82591de35cb01b40736
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
9 */
11 #pragma once
13 #include "root/port.h"
14 #include "ast_node.h"
15 #include "globals.h"
16 #include "arraytypes.h"
17 #include "visitor.h"
19 class CPPNamespaceDeclaration;
20 class Identifier;
21 struct Scope;
22 class DsymbolTable;
23 class Declaration;
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;
48 class NewDeclaration;
49 class VarDeclaration;
50 class AttribDeclaration;
51 class VisibilityDeclaration;
52 class Package;
53 class Module;
54 class Import;
55 class Type;
56 class TypeTuple;
57 class WithStatement;
58 class LabelDsymbol;
59 class ScopeDsymbol;
60 class ForwardingScopeDsymbol;
61 class TemplateDeclaration;
62 class TemplateInstance;
63 class TemplateMixin;
64 class ForwardingAttribDeclaration;
65 class Nspace;
66 class EnumMember;
67 class WithScopeSymbol;
68 class ArrayScopeSymbol;
69 class SymbolDeclaration;
70 class Expression;
71 class ExpressionDsymbol;
72 class AliasAssign;
73 class OverloadSet;
74 class StaticAssert;
75 class StaticIfDeclaration;
76 struct AA;
77 #ifdef IN_GCC
78 typedef union tree_node Symbol;
79 #else
80 struct Symbol;
81 #endif
83 struct Ungag
85 unsigned oldgag;
87 Ungag(unsigned old) : oldgag(old) {}
88 ~Ungag() { global.gag = oldgag; }
91 enum class ThreeState : uint8_t
93 none, // value not yet computed
94 no, // value is false
95 yes, // value is true
98 void dsymbolSemantic(Dsymbol *dsym, Scope *sc);
99 void semantic2(Dsymbol *dsym, Scope *sc);
100 void semantic3(Dsymbol *dsym, Scope* sc);
102 struct Visibility
104 enum Kind
106 undefined,
107 none, // no access
108 private_,
109 package_,
110 protected_,
111 public_,
112 export_
114 Kind kind;
115 Package *pkg;
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
134 enum
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
162 // their imports
163 tagNameSpace = 0x40, // search ImportC tag symbol table
164 ignoreVisibility = 0x80, // also find private and package protected symbols
167 struct FieldState
169 unsigned offset;
171 unsigned fieldOffset;
172 unsigned fieldSize;
173 unsigned fieldAlign;
174 unsigned bitOffset;
176 d_bool inFlight;
179 struct DsymbolAttributes;
181 class Dsymbol : public ASTNode
183 public:
184 Identifier *ident;
185 Dsymbol *parent;
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;
190 private:
191 DsymbolAttributes* atts;
192 public:
193 d_bool errors; // this symbol failed to pass semantic()
194 PASS semanticRun;
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
205 Loc getLoc();
206 const char *locToChars();
207 bool equals(const RootObject * const o) const override;
208 bool isAnonymous() const;
209 Module *getModule();
210 bool isCsymbol();
211 Module *getAccessModule();
212 Dsymbol *pastMixin();
213 Dsymbol *toParent();
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 *);
261 bool inNonRoot();
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
325 public:
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
332 private:
333 BitArray accessiblePackages, privateAccessiblePackages;
335 public:
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
354 public:
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
366 public:
367 RootObject *arrayContent;
369 ArrayScopeSymbol *isArrayScopeSymbol() override { return this; }
370 void accept(Visitor *v) override { v->visit(this); }
373 // Overload Sets
375 class OverloadSet final : public Dsymbol
377 public:
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
390 public:
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
401 public:
402 Expression *exp;
404 ExpressionDsymbol *isExpressionDsymbol() override { return this; }
407 // Table of Dsymbol's
409 class DsymbolTable final : public RootObject
411 public:
412 AA *tab;
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);