| 1 |
|
| 2 |
/* Compiler implementation of the D programming language |
| 3 |
* Copyright (C) 1999-2025 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 |
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 |
| 83 |
|
| 84 |
struct Ungag |
| 85 |
{ |
| 86 |
unsigned oldgag; |
| 87 |
|
| 88 |
Ungag(unsigned old) : oldgag(old) {} |
| 89 |
~Ungag() { global.gag = oldgag; } |
| 90 |
}; |
| 91 |
|
| 92 |
enum class ThreeState : uint8_t |
| 93 |
{ |
| 94 |
none, // value not yet computed |
| 95 |
no, // value is false |
| 96 |
yes, // value is true |
| 97 |
}; |
| 98 |
|
| 99 |
namespace dmd |
| 100 |
{ |
| 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); |
| 108 |
} |
| 109 |
|
| 110 |
struct Visibility |
| 111 |
{ |
| 112 |
enum Kind |
| 113 |
{ |
| 114 |
undefined, |
| 115 |
none, // no access |
| 116 |
private_, |
| 117 |
package_, |
| 118 |
protected_, |
| 119 |
public_, |
| 120 |
export_ |
| 121 |
}; |
| 122 |
Kind kind; |
| 123 |
Package *pkg; |
| 124 |
}; |
| 125 |
|
| 126 |
/* State of symbol in winding its way through the passes of the compiler |
| 127 |
*/ |
| 128 |
enum class PASS : uint8_t |
| 129 |
{ |
| 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 |
| 140 |
}; |
| 141 |
|
| 142 |
/* Flags for symbol search |
| 143 |
*/ |
| 144 |
typedef unsigned SearchOptFlags; |
| 145 |
enum class SearchOpt : SearchOptFlags |
| 146 |
{ |
| 147 |
all = 0x00, // default |
| 148 |
ignorePrivateImports = 0x01, // don't search private imports |
| 149 |
ignoreErrors = 0x02, // don't give error messages |
| 150 |
ignoreAmbiguous = 0x04, // return NULL if ambiguous |
| 151 |
localsOnly = 0x08, // only look at locals (don't search imports) |
| 152 |
importsOnly = 0x10, // only look in imports |
| 153 |
unqualifiedModule = 0x20, // the module scope search is unqualified, |
| 154 |
// meaning don't search imports in that scope, |
| 155 |
// because qualified module searches search |
| 156 |
// their imports |
| 157 |
tagNameSpace = 0x40, // search ImportC tag symbol table |
| 158 |
ignoreVisibility = 0x80, // also find private and package protected symbols |
| 159 |
}; |
| 160 |
|
| 161 |
struct FieldState |
| 162 |
{ |
| 163 |
unsigned offset; |
| 164 |
|
| 165 |
unsigned fieldOffset; |
| 166 |
unsigned fieldSize; |
| 167 |
unsigned fieldAlign; |
| 168 |
unsigned bitOffset; |
| 169 |
|
| 170 |
d_bool inFlight; |
| 171 |
}; |
| 172 |
|
| 173 |
struct DsymbolAttributes; |
| 174 |
|
| 175 |
class Dsymbol : public ASTNode |
| 176 |
{ |
| 177 |
public: |
| 178 |
Identifier *ident; |
| 179 |
Dsymbol *parent; |
| 180 |
Symbol *csym; // symbol for code generator |
| 181 |
Scope *_scope; // !=NULL means context to use for semantic() |
| 182 |
private: |
| 183 |
DsymbolAttributes* atts; |
| 184 |
public: |
| 185 |
Loc loc; // where defined |
| 186 |
unsigned short localNum; // perturb mangled name to avoid collisions with those in FuncDeclaration.localsymtab |
| 187 |
|
| 188 |
bool errors() const; |
| 189 |
PASS semanticRun() const; |
| 190 |
PASS semanticRun(PASS v); |
| 191 |
private: |
| 192 |
unsigned char bitfields; |
| 193 |
unsigned char dsym; |
| 194 |
public: |
| 195 |
static Dsymbol *create(Identifier *); |
| 196 |
const char *toChars() const final override; |
| 197 |
DeprecatedDeclaration* depdecl(); |
| 198 |
CPPNamespaceDeclaration* cppnamespace(); |
| 199 |
UserAttributeDeclaration* userAttribDecl(); |
| 200 |
DeprecatedDeclaration* depdecl(DeprecatedDeclaration* dd); |
| 201 |
CPPNamespaceDeclaration* cppnamespace(CPPNamespaceDeclaration* ns); |
| 202 |
UserAttributeDeclaration* userAttribDecl(UserAttributeDeclaration* uad); |
| 203 |
virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments |
| 204 |
bool equals(const RootObject * const o) const override; |
| 205 |
bool isAnonymous() const; |
| 206 |
Module *getModule(); |
| 207 |
bool isCsymbol(); |
| 208 |
Module *getAccessModule(); |
| 209 |
Dsymbol *pastMixin(); |
| 210 |
Dsymbol *toParent(); |
| 211 |
Dsymbol *toParent2(); |
| 212 |
Dsymbol *toParentDecl(); |
| 213 |
Dsymbol *toParentLocal(); |
| 214 |
Dsymbol *toParentP(Dsymbol *p1, Dsymbol *p2 = nullptr); |
| 215 |
TemplateInstance *isInstantiated(); |
| 216 |
bool followInstantiationContext(Dsymbol *p1, Dsymbol *p2 = nullptr); |
| 217 |
TemplateInstance *isSpeculative(); |
| 218 |
Ungag ungagSpeculative(); |
| 219 |
|
| 220 |
// kludge for template.isSymbol() |
| 221 |
DYNCAST dyncast() const override final { return DYNCAST_DSYMBOL; } |
| 222 |
|
| 223 |
virtual Identifier *getIdent(); |
| 224 |
virtual const char *toPrettyChars(bool QualifyTypes = false); |
| 225 |
virtual const char *kind() const; |
| 226 |
virtual Dsymbol *toAlias(); // resolve real symbol |
| 227 |
virtual Dsymbol *toAlias2(); |
| 228 |
virtual bool overloadInsert(Dsymbol *s); |
| 229 |
virtual uinteger_t size(Loc loc); |
| 230 |
virtual bool isforwardRef(); |
| 231 |
virtual AggregateDeclaration *isThis(); // is a 'this' required to access the member |
| 232 |
virtual bool isExport() const; // is Dsymbol exported? |
| 233 |
virtual bool isImportedSymbol() const; // is Dsymbol imported? |
| 234 |
virtual bool isDeprecated() const; // is Dsymbol deprecated? |
| 235 |
virtual bool isOverloadable() const; |
| 236 |
virtual LabelDsymbol *isLabel(); // is this a LabelDsymbol? |
| 237 |
AggregateDeclaration *isMember(); // is toParent() an AggregateDeclaration? |
| 238 |
AggregateDeclaration *isMember2(); // is toParent2() an AggregateDeclaration? |
| 239 |
AggregateDeclaration *isMemberDecl(); // is toParentDecl() an AggregateDeclaration? |
| 240 |
AggregateDeclaration *isMemberLocal(); // is toParentLocal() an AggregateDeclaration? |
| 241 |
ClassDeclaration *isClassMember(); // isMember() is a ClassDeclaration? |
| 242 |
virtual Type *getType(); // is this a type? |
| 243 |
virtual bool needThis(); // need a 'this' pointer? |
| 244 |
virtual Visibility visible(); |
| 245 |
virtual Dsymbol *syntaxCopy(Dsymbol *s); // copy only syntax trees |
| 246 |
virtual bool hasPointers(); |
| 247 |
virtual void addObjcSymbols(ClassDeclarations *, ClassDeclarations *) { } |
| 248 |
|
| 249 |
virtual void addComment(const utf8_t *comment); |
| 250 |
const utf8_t *comment(); // current value of comment |
| 251 |
|
| 252 |
UnitTestDeclaration *ddocUnittest(); |
| 253 |
void ddocUnittest(UnitTestDeclaration *); |
| 254 |
|
| 255 |
bool inNonRoot(); |
| 256 |
|
| 257 |
// Eliminate need for dynamic_cast |
| 258 |
Package *isPackage(); |
| 259 |
Module *isModule(); |
| 260 |
EnumMember *isEnumMember(); |
| 261 |
TemplateDeclaration *isTemplateDeclaration(); |
| 262 |
TemplateInstance *isTemplateInstance(); |
| 263 |
TemplateMixin *isTemplateMixin(); |
| 264 |
ForwardingAttribDeclaration *isForwardingAttribDeclaration(); |
| 265 |
Nspace *isNspace(); |
| 266 |
Declaration *isDeclaration(); |
| 267 |
StorageClassDeclaration *isStorageClassDeclaration(); |
| 268 |
ExpressionDsymbol *isExpressionDsymbol(); |
| 269 |
AliasAssign *isAliasAssign(); |
| 270 |
ThisDeclaration *isThisDeclaration(); |
| 271 |
BitFieldDeclaration *isBitFieldDeclaration(); |
| 272 |
TypeInfoDeclaration *isTypeInfoDeclaration(); |
| 273 |
TupleDeclaration *isTupleDeclaration(); |
| 274 |
AliasDeclaration *isAliasDeclaration(); |
| 275 |
AggregateDeclaration *isAggregateDeclaration(); |
| 276 |
FuncDeclaration *isFuncDeclaration(); |
| 277 |
FuncAliasDeclaration *isFuncAliasDeclaration(); |
| 278 |
OverDeclaration *isOverDeclaration(); |
| 279 |
FuncLiteralDeclaration *isFuncLiteralDeclaration(); |
| 280 |
CtorDeclaration *isCtorDeclaration(); |
| 281 |
PostBlitDeclaration *isPostBlitDeclaration(); |
| 282 |
DtorDeclaration *isDtorDeclaration(); |
| 283 |
StaticCtorDeclaration *isStaticCtorDeclaration(); |
| 284 |
StaticDtorDeclaration *isStaticDtorDeclaration(); |
| 285 |
SharedStaticCtorDeclaration *isSharedStaticCtorDeclaration(); |
| 286 |
SharedStaticDtorDeclaration *isSharedStaticDtorDeclaration(); |
| 287 |
InvariantDeclaration *isInvariantDeclaration(); |
| 288 |
UnitTestDeclaration *isUnitTestDeclaration(); |
| 289 |
NewDeclaration *isNewDeclaration(); |
| 290 |
VarDeclaration *isVarDeclaration(); |
| 291 |
VersionSymbol *isVersionSymbol(); |
| 292 |
DebugSymbol *isDebugSymbol(); |
| 293 |
ClassDeclaration *isClassDeclaration(); |
| 294 |
StructDeclaration *isStructDeclaration(); |
| 295 |
UnionDeclaration *isUnionDeclaration(); |
| 296 |
InterfaceDeclaration *isInterfaceDeclaration(); |
| 297 |
ScopeDsymbol *isScopeDsymbol(); |
| 298 |
ForwardingScopeDsymbol *isForwardingScopeDsymbol(); |
| 299 |
WithScopeSymbol *isWithScopeSymbol(); |
| 300 |
ArrayScopeSymbol *isArrayScopeSymbol(); |
| 301 |
Import *isImport(); |
| 302 |
EnumDeclaration *isEnumDeclaration(); |
| 303 |
SymbolDeclaration *isSymbolDeclaration(); |
| 304 |
AttribDeclaration *isAttribDeclaration(); |
| 305 |
AnonDeclaration *isAnonDeclaration(); |
| 306 |
CPPNamespaceDeclaration *isCPPNamespaceDeclaration(); |
| 307 |
VisibilityDeclaration *isVisibilityDeclaration(); |
| 308 |
OverloadSet *isOverloadSet(); |
| 309 |
MixinDeclaration *isMixinDeclaration(); |
| 310 |
StaticAssert *isStaticAssert(); |
| 311 |
StaticIfDeclaration *isStaticIfDeclaration(); |
| 312 |
CAsmDeclaration *isCAsmDeclaration(); |
| 313 |
void accept(Visitor *v) override { v->visit(this); } |
| 314 |
}; |
| 315 |
|
| 316 |
// Dsymbol that generates a scope |
| 317 |
|
| 318 |
class ScopeDsymbol : public Dsymbol |
| 319 |
{ |
| 320 |
public: |
| 321 |
Dsymbols *members; // all Dsymbol's in this scope |
| 322 |
DsymbolTable *symtab; // members[] sorted into table |
| 323 |
unsigned endlinnum; // the linnumber of the statement after the scope (0 if unknown) |
| 324 |
Dsymbols *importedScopes; // imported Dsymbol's |
| 325 |
Visibility::Kind *visibilities; // array of `Visibility.Kind`, one for each import |
| 326 |
|
| 327 |
private: |
| 328 |
BitArray accessiblePackages, privateAccessiblePackages; |
| 329 |
|
| 330 |
public: |
| 331 |
ScopeDsymbol *syntaxCopy(Dsymbol *s) override; |
| 332 |
virtual void importScope(Dsymbol *s, Visibility visibility); |
| 333 |
virtual bool isPackageAccessible(Package *p, Visibility visibility, SearchOptFlags flags = (SearchOptFlags)SearchOpt::all); |
| 334 |
bool isforwardRef() override final; |
| 335 |
static void multiplyDefined(Loc loc, Dsymbol *s1, Dsymbol *s2); |
| 336 |
const char *kind() const override; |
| 337 |
virtual Dsymbol *symtabInsert(Dsymbol *s); |
| 338 |
virtual Dsymbol *symtabLookup(Dsymbol *s, Identifier *id); |
| 339 |
|
| 340 |
void accept(Visitor *v) override { v->visit(this); } |
| 341 |
}; |
| 342 |
|
| 343 |
// With statement scope |
| 344 |
|
| 345 |
class WithScopeSymbol final : public ScopeDsymbol |
| 346 |
{ |
| 347 |
public: |
| 348 |
WithStatement *withstate; |
| 349 |
|
| 350 |
|
| 351 |
void accept(Visitor *v) override { v->visit(this); } |
| 352 |
}; |
| 353 |
|
| 354 |
// Array Index/Slice scope |
| 355 |
|
| 356 |
class ArrayScopeSymbol final : public ScopeDsymbol |
| 357 |
{ |
| 358 |
public: |
| 359 |
RootObject *arrayContent; |
| 360 |
|
| 361 |
void accept(Visitor *v) override { v->visit(this); } |
| 362 |
}; |
| 363 |
|
| 364 |
// Overload Sets |
| 365 |
|
| 366 |
class OverloadSet final : public Dsymbol |
| 367 |
{ |
| 368 |
public: |
| 369 |
Dsymbols a; // array of Dsymbols |
| 370 |
|
| 371 |
void push(Dsymbol *s); |
| 372 |
const char *kind() const override; |
| 373 |
void accept(Visitor *v) override { v->visit(this); } |
| 374 |
}; |
| 375 |
|
| 376 |
// Forwarding ScopeDsymbol |
| 377 |
|
| 378 |
class ForwardingScopeDsymbol final : public ScopeDsymbol |
| 379 |
{ |
| 380 |
public: |
| 381 |
Dsymbol *symtabInsert(Dsymbol *s) override; |
| 382 |
Dsymbol *symtabLookup(Dsymbol *s, Identifier *id) override; |
| 383 |
void importScope(Dsymbol *s, Visibility visibility) override; |
| 384 |
const char *kind() const override; |
| 385 |
|
| 386 |
}; |
| 387 |
|
| 388 |
class ExpressionDsymbol final : public Dsymbol |
| 389 |
{ |
| 390 |
public: |
| 391 |
Expression *exp; |
| 392 |
|
| 393 |
}; |
| 394 |
|
| 395 |
class CAsmDeclaration final : public Dsymbol |
| 396 |
{ |
| 397 |
public: |
| 398 |
Expression *code; // string expression |
| 399 |
|
| 400 |
void accept(Visitor *v) override { v->visit(this); } |
| 401 |
}; |
| 402 |
|
| 403 |
// Table of Dsymbol's |
| 404 |
|
| 405 |
class DsymbolTable final : public RootObject |
| 406 |
{ |
| 407 |
public: |
| 408 |
AA *tab; |
| 409 |
|
| 410 |
// Look up Identifier. Return Dsymbol if found, NULL if not. |
| 411 |
Dsymbol *lookup(Identifier const * const ident); |
| 412 |
|
| 413 |
// Look for Dsymbol in table. If there, return it. If not, insert s and return that. |
| 414 |
void update(Dsymbol *s); |
| 415 |
|
| 416 |
// Insert Dsymbol in table. Return NULL if already there. |
| 417 |
Dsymbol *insert(Dsymbol *s); |
| 418 |
Dsymbol *insert(Identifier const * const ident, Dsymbol *s); // when ident and s are not the same |
| 419 |
|
| 420 |
// Number of symbols in symbol table |
| 421 |
size_t length() const; |
| 422 |
}; |
| 423 |
|
| 424 |
namespace dmd |
| 425 |
{ |
| 426 |
void addMember(Dsymbol *dsym, Scope *sc, ScopeDsymbol *sds); |
| 427 |
Dsymbol *search(Dsymbol *d, Loc loc, Identifier *ident, SearchOptFlags flags = (SearchOptFlags)SearchOpt::localsOnly); |
| 428 |
Dsymbols *include(Dsymbol *d, Scope *sc); |
| 429 |
void setScope(Dsymbol *d, Scope *sc); |
| 430 |
void importAll(Dsymbol *d, Scope *sc); |
| 431 |
void addComment(Dsymbol *d, const char *comment); |
| 432 |
bool oneMember(Dsymbol *d, Dsymbol *&ps, Identifier *ident); |
| 433 |
bool hasStaticCtorOrDtor(Dsymbol *d); |
| 434 |
} |