]> git.ipfire.org Git - people/ms/gcc.git/blob - gcc/d/dmd/dsymbol.h
88110e1f5d2f4d6834491dacc36eb0d5b7c164f7
[people/ms/gcc.git] / gcc / d / dmd / dsymbol.h
1
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 */
10
11 #pragma once
12
13 #include "root/port.h"
14 #include "ast_node.h"
15 #include "globals.h"
16 #include "arraytypes.h"
17 #include "visitor.h"
18
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 struct AA;
76 #ifdef IN_GCC
77 typedef union tree_node Symbol;
78 #else
79 struct Symbol;
80 #endif
81
82 struct Ungag
83 {
84 unsigned oldgag;
85
86 Ungag(unsigned old) : oldgag(old) {}
87 ~Ungag() { global.gag = oldgag; }
88 };
89
90 enum class ThreeState : uint8_t
91 {
92 none, // value not yet computed
93 no, // value is false
94 yes, // value is true
95 };
96
97 void dsymbolSemantic(Dsymbol *dsym, Scope *sc);
98 void semantic2(Dsymbol *dsym, Scope *sc);
99 void semantic3(Dsymbol *dsym, Scope* sc);
100
101 struct Visibility
102 {
103 enum Kind
104 {
105 undefined,
106 none, // no access
107 private_,
108 package_,
109 protected_,
110 public_,
111 export_
112 };
113 Kind kind;
114 Package *pkg;
115 };
116
117 /* State of symbol in winding its way through the passes of the compiler
118 */
119 enum class PASS : uint8_t
120 {
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
131 };
132
133 enum
134 {
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
145 };
146
147 /* Flags for symbol search
148 */
149 enum
150 {
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
160 // their imports
161 IgnoreSymbolVisibility = 0x80, // also find private and package protected symbols
162 TagNameSpace = 0x100, // search ImportC tag symbol table
163 };
164
165 struct FieldState
166 {
167 unsigned offset;
168
169 unsigned fieldOffset;
170 unsigned fieldSize;
171 unsigned fieldAlign;
172 unsigned bitOffset;
173
174 bool inFlight;
175 };
176
177 struct DsymbolAttributes;
178
179 class Dsymbol : public ASTNode
180 {
181 public:
182 Identifier *ident;
183 Dsymbol *parent;
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;
188 private:
189 DsymbolAttributes* atts;
190 public:
191 bool errors; // this symbol failed to pass semantic()
192 PASS semanticRun;
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
203 Loc getLoc();
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);
212 Module *getModule();
213 bool isCsymbol();
214 Module *getAccessModule();
215 Dsymbol *pastMixin();
216 Dsymbol *toParent();
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();
225
226 // kludge for template.isSymbol()
227 DYNCAST dyncast() const override final { return DYNCAST_DSYMBOL; }
228
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() { }
263
264 virtual void addComment(const utf8_t *comment);
265 const utf8_t *comment(); // current value of comment
266
267 UnitTestDeclaration *ddocUnittest();
268 void ddocUnittest(UnitTestDeclaration *);
269
270 bool inNonRoot();
271
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); }
327 };
328
329 // Dsymbol that generates a scope
330
331 class ScopeDsymbol : public Dsymbol
332 {
333 public:
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)
337
338 private:
339 Dsymbols *importedScopes; // imported Dsymbol's
340 Visibility::Kind *visibilities; // array of `Visibility.Kind`, one for each import
341
342 BitArray accessiblePackages, privateAccessiblePackages;
343
344 public:
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;
356
357 ScopeDsymbol *isScopeDsymbol() override final { return this; }
358 void accept(Visitor *v) override { v->visit(this); }
359 };
360
361 // With statement scope
362
363 class WithScopeSymbol final : public ScopeDsymbol
364 {
365 public:
366 WithStatement *withstate;
367
368 Dsymbol *search(const Loc &loc, Identifier *ident, int flags = SearchLocalsOnly) override;
369
370 WithScopeSymbol *isWithScopeSymbol() override { return this; }
371 void accept(Visitor *v) override { v->visit(this); }
372 };
373
374 // Array Index/Slice scope
375
376 class ArrayScopeSymbol final : public ScopeDsymbol
377 {
378 private:
379 RootObject *arrayContent;
380 public:
381 Scope *sc;
382
383 Dsymbol *search(const Loc &loc, Identifier *ident, int flags = IgnoreNone) override;
384
385 ArrayScopeSymbol *isArrayScopeSymbol() override { return this; }
386 void accept(Visitor *v) override { v->visit(this); }
387 };
388
389 // Overload Sets
390
391 class OverloadSet final : public Dsymbol
392 {
393 public:
394 Dsymbols a; // array of Dsymbols
395
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); }
400 };
401
402 // Forwarding ScopeDsymbol
403
404 class ForwardingScopeDsymbol final : public ScopeDsymbol
405 {
406 public:
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;
411
412 ForwardingScopeDsymbol *isForwardingScopeDsymbol() override { return this; }
413 };
414
415 class ExpressionDsymbol final : public Dsymbol
416 {
417 public:
418 Expression *exp;
419
420 ExpressionDsymbol *isExpressionDsymbol() override { return this; }
421 };
422
423 // Table of Dsymbol's
424
425 class DsymbolTable final : public RootObject
426 {
427 public:
428 AA *tab;
429
430 // Look up Identifier. Return Dsymbol if found, NULL if not.
431 Dsymbol *lookup(Identifier const * const ident);
432
433 // Look for Dsymbol in table. If there, return it. If not, insert s and return that.
434 void update(Dsymbol *s);
435
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
439
440 // Number of symbols in symbol table
441 size_t length() const;
442 };