Match: Add pattern for `(a ? b : 0) | (a ? 0 : c)` into `a ? b : c` [PR103660]
[official-gcc.git] / gcc / d / dmd / dsymbol.h
blobf8454354fed536cfd29c7ad05e0926ccb3c4f3a4
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
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 class CAsmDeclaration;
77 struct AA;
78 #ifdef IN_GCC
79 typedef union tree_node Symbol;
80 #else
81 struct Symbol;
82 #endif
84 struct Ungag
86 unsigned oldgag;
88 Ungag(unsigned old) : oldgag(old) {}
89 ~Ungag() { global.gag = oldgag; }
92 enum class ThreeState : uint8_t
94 none, // value not yet computed
95 no, // value is false
96 yes, // value is true
99 namespace dmd
101 void dsymbolSemantic(Dsymbol *dsym, Scope *sc);
102 void semantic2(Dsymbol *dsym, Scope *sc);
103 void semantic3(Dsymbol *dsym, Scope* sc);
104 // in iasm.d
105 void asmSemantic(CAsmDeclaration *ad, Scope *sc);
106 // in iasmgcc.d
107 void gccAsmSemantic(CAsmDeclaration *ad, Scope *sc);
110 struct Visibility
112 enum Kind
114 undefined,
115 none, // no access
116 private_,
117 package_,
118 protected_,
119 public_,
120 export_
122 Kind kind;
123 Package *pkg;
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
142 enum
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
170 // their imports
171 tagNameSpace = 0x40, // search ImportC tag symbol table
172 ignoreVisibility = 0x80, // also find private and package protected symbols
175 struct FieldState
177 unsigned offset;
179 unsigned fieldOffset;
180 unsigned fieldSize;
181 unsigned fieldAlign;
182 unsigned bitOffset;
184 d_bool inFlight;
187 struct DsymbolAttributes;
189 class Dsymbol : public ASTNode
191 public:
192 Identifier *ident;
193 Dsymbol *parent;
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;
198 private:
199 DsymbolAttributes* atts;
200 public:
201 d_bool errors; // this symbol failed to pass semantic()
202 PASS semanticRun;
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
213 Loc getLoc();
214 const char *locToChars();
215 bool equals(const RootObject * const o) const override;
216 bool isAnonymous() const;
217 Module *getModule();
218 bool isCsymbol();
219 Module *getAccessModule();
220 Dsymbol *pastMixin();
221 Dsymbol *toParent();
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 *);
269 bool inNonRoot();
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
334 public:
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
341 private:
342 BitArray accessiblePackages, privateAccessiblePackages;
344 public:
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
363 public:
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
375 public:
376 RootObject *arrayContent;
378 ArrayScopeSymbol *isArrayScopeSymbol() override { return this; }
379 void accept(Visitor *v) override { v->visit(this); }
382 // Overload Sets
384 class OverloadSet final : public Dsymbol
386 public:
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
399 public:
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
410 public:
411 Expression *exp;
413 ExpressionDsymbol *isExpressionDsymbol() override { return this; }
416 class CAsmDeclaration final : public Dsymbol
418 public:
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
429 public:
430 AA *tab;
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;
446 namespace dmd
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);