]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/dmd/dsymbol.h
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / d / dmd / dsymbol.h
1
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
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 struct AA;
75 #ifdef IN_GCC
76 typedef union tree_node Symbol;
77 #else
78 struct Symbol;
79 #endif
80
81 struct Ungag
82 {
83 unsigned oldgag;
84
85 Ungag(unsigned old) : oldgag(old) {}
86 ~Ungag() { global.gag = oldgag; }
87 };
88
89 void dsymbolSemantic(Dsymbol *dsym, Scope *sc);
90 void semantic2(Dsymbol *dsym, Scope *sc);
91 void semantic3(Dsymbol *dsym, Scope* sc);
92
93 struct Visibility
94 {
95 enum Kind
96 {
97 undefined,
98 none, // no access
99 private_,
100 package_,
101 protected_,
102 public_,
103 export_
104 };
105 Kind kind;
106 Package *pkg;
107 };
108
109 /* State of symbol in winding its way through the passes of the compiler
110 */
111 enum PASS
112 {
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
123 };
124
125 /* Flags for symbol search
126 */
127 enum
128 {
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
138 // their imports
139 IgnoreSymbolVisibility = 0x80, // also find private and package protected symbols
140 TagNameSpace = 0x100, // search ImportC tag symbol table
141 };
142
143 struct FieldState
144 {
145 unsigned offset;
146
147 unsigned fieldOffset;
148 unsigned bitOffset;
149 unsigned fieldSice;
150 bool inFlight;
151 };
152
153 class Dsymbol : public ASTNode
154 {
155 public:
156 Identifier *ident;
157 Dsymbol *parent;
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()
167 PASS semanticRun;
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)
172
173 static Dsymbol *create(Identifier *);
174 const char *toChars() const;
175 virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments
176 Loc getLoc();
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);
185 Module *getModule();
186 Module *getAccessModule();
187 Dsymbol *pastMixin();
188 Dsymbol *toParent();
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();
197
198 // kludge for template.isSymbol()
199 DYNCAST dyncast() const { return DYNCAST_DSYMBOL; }
200
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() { }
235
236 virtual void addComment(const utf8_t *comment);
237
238 bool inNonRoot();
239
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); }
294 };
295
296 // Dsymbol that generates a scope
297
298 class ScopeDsymbol : public Dsymbol
299 {
300 public:
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)
304
305 private:
306 Dsymbols *importedScopes; // imported Dsymbol's
307 Visibility::Kind *visibilities; // array of `Visibility.Kind`, one for each import
308
309 BitArray accessiblePackages, privateAccessiblePackages;
310
311 public:
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);
316 bool isforwardRef();
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();
323
324 ScopeDsymbol *isScopeDsymbol() { return this; }
325 void accept(Visitor *v) { v->visit(this); }
326 };
327
328 // With statement scope
329
330 class WithScopeSymbol : public ScopeDsymbol
331 {
332 public:
333 WithStatement *withstate;
334
335 Dsymbol *search(const Loc &loc, Identifier *ident, int flags = SearchLocalsOnly);
336
337 WithScopeSymbol *isWithScopeSymbol() { return this; }
338 void accept(Visitor *v) { v->visit(this); }
339 };
340
341 // Array Index/Slice scope
342
343 class ArrayScopeSymbol : public ScopeDsymbol
344 {
345 private:
346 RootObject *arrayContent;
347 public:
348 Scope *sc;
349
350 Dsymbol *search(const Loc &loc, Identifier *ident, int flags = IgnoreNone);
351
352 ArrayScopeSymbol *isArrayScopeSymbol() { return this; }
353 void accept(Visitor *v) { v->visit(this); }
354 };
355
356 // Overload Sets
357
358 class OverloadSet : public Dsymbol
359 {
360 public:
361 Dsymbols a; // array of Dsymbols
362
363 void push(Dsymbol *s);
364 OverloadSet *isOverloadSet() { return this; }
365 const char *kind() const;
366 void accept(Visitor *v) { v->visit(this); }
367 };
368
369 // Forwarding ScopeDsymbol
370
371 class ForwardingScopeDsymbol : public ScopeDsymbol
372 {
373 public:
374 ScopeDsymbol *forward;
375
376 Dsymbol *symtabInsert(Dsymbol *s);
377 Dsymbol *symtabLookup(Dsymbol *s, Identifier *id);
378 void importScope(Dsymbol *s, Visibility visibility);
379 const char *kind() const;
380
381 ForwardingScopeDsymbol *isForwardingScopeDsymbol() { return this; }
382 };
383
384 class ExpressionDsymbol : public Dsymbol
385 {
386 public:
387 Expression *exp;
388
389 ExpressionDsymbol *isExpressionDsymbol() { return this; }
390 };
391
392 // Table of Dsymbol's
393
394 class DsymbolTable : public RootObject
395 {
396 public:
397 AA *tab;
398
399 // Look up Identifier. Return Dsymbol if found, NULL if not.
400 Dsymbol *lookup(Identifier const * const ident);
401
402 // Look for Dsymbol in table. If there, return it. If not, insert s and return that.
403 void update(Dsymbol *s);
404
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
408
409 // Number of symbols in symbol table
410 size_t length() const;
411 };