Skip to content

Commit

Permalink
Move newScope out of AST nodes to dsymbolsem.d (dlang#16880)
Browse files Browse the repository at this point in the history
  • Loading branch information
dchidindu5 authored and thewilsonator committed Oct 7, 2024
1 parent 725d880 commit d4a0921
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 137 deletions.
115 changes: 0 additions & 115 deletions compiler/src/dmd/attrib.d
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ extern (C++) abstract class AttribDeclaration : Dsymbol
return sc2;
}

/****************************************
* A hook point to supply scope for members.
* addMember, setScope, importAll, semantic, semantic2 and semantic3 will use this.
*/
Scope* newScope(Scope* sc)
{
return sc;
}

override void addComment(const(char)* comment)
{
Expand Down Expand Up @@ -201,28 +193,6 @@ extern (C++) class StorageClassDeclaration : AttribDeclaration
return new StorageClassDeclaration(stc, Dsymbol.arraySyntaxCopy(decl));
}

override Scope* newScope(Scope* sc)
{
StorageClass scstc = sc.stc;
/* These sets of storage classes are mutually exclusive,
* so choose the innermost or most recent one.
*/
if (stc & (STC.auto_ | STC.scope_ | STC.static_ | STC.extern_ | STC.manifest))
scstc &= ~(STC.auto_ | STC.scope_ | STC.static_ | STC.extern_ | STC.manifest);
if (stc & (STC.auto_ | STC.scope_ | STC.static_ | STC.manifest | STC.gshared))
scstc &= ~(STC.auto_ | STC.scope_ | STC.static_ | STC.manifest | STC.gshared);
if (stc & (STC.const_ | STC.immutable_ | STC.manifest))
scstc &= ~(STC.const_ | STC.immutable_ | STC.manifest);
if (stc & (STC.gshared | STC.shared_))
scstc &= ~(STC.gshared | STC.shared_);
if (stc & (STC.safe | STC.trusted | STC.system))
scstc &= ~(STC.safe | STC.trusted | STC.system);
scstc |= stc;
//printf("scstc = x%llx\n", scstc);
return createNewScope(sc, scstc, sc.linkage, sc.cppmangle,
sc.visibility, sc.explicitVisibility, sc.aligndecl, sc.inlining);
}

override final bool oneMember(out Dsymbol ps, Identifier ident)
{
bool t = Dsymbol.oneMembers(decl, ps, ident);
Expand Down Expand Up @@ -285,25 +255,6 @@ extern (C++) final class DeprecatedDeclaration : StorageClassDeclaration
return new DeprecatedDeclaration(msg.syntaxCopy(), Dsymbol.arraySyntaxCopy(decl));
}

/**
* Provides a new scope with `STC.deprecated_` and `Scope.depdecl` set
*
* Calls `StorageClassDeclaration.newScope` (as it must be called or copied
* in any function overriding `newScope`), then set the `Scope`'s depdecl.
*
* Returns:
* Always a new scope, to use for this `DeprecatedDeclaration`'s members.
*/
override Scope* newScope(Scope* sc)
{
auto scx = super.newScope(sc);
// The enclosing scope is deprecated as well
if (scx == sc)
scx = sc.push();
scx.depdecl = this;
return scx;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -338,11 +289,6 @@ extern (C++) final class LinkDeclaration : AttribDeclaration
return new LinkDeclaration(loc, linkage, Dsymbol.arraySyntaxCopy(decl));
}

override Scope* newScope(Scope* sc)
{
return createNewScope(sc, sc.stc, this.linkage, sc.cppmangle, sc.visibility, sc.explicitVisibility,
sc.aligndecl, sc.inlining);
}

override const(char)* toChars() const
{
Expand Down Expand Up @@ -385,12 +331,6 @@ extern (C++) final class CPPMangleDeclaration : AttribDeclaration
return new CPPMangleDeclaration(loc, cppmangle, Dsymbol.arraySyntaxCopy(decl));
}

override Scope* newScope(Scope* sc)
{
return createNewScope(sc, sc.stc, LINK.cpp, cppmangle, sc.visibility, sc.explicitVisibility,
sc.aligndecl, sc.inlining);
}

override const(char)* toChars() const
{
return toString().ptr;
Expand Down Expand Up @@ -461,18 +401,6 @@ extern (C++) final class CPPNamespaceDeclaration : AttribDeclaration
this.loc, this.ident, this.exp, Dsymbol.arraySyntaxCopy(this.decl), this.cppnamespace);
}

/**
* Returns:
* A copy of the parent scope, with `this` as `namespace` and C++ linkage
*/
override Scope* newScope(Scope* sc)
{
auto scx = sc.copy();
scx.linkage = LINK.cpp;
scx.namespace = this;
return scx;
}

override const(char)* toChars() const
{
return toString().ptr;
Expand Down Expand Up @@ -544,13 +472,6 @@ extern (C++) final class VisibilityDeclaration : AttribDeclaration
return new VisibilityDeclaration(this.loc, visibility, Dsymbol.arraySyntaxCopy(decl));
}

override Scope* newScope(Scope* sc)
{
if (pkg_identifiers)
dsymbolSemantic(this, sc);
return createNewScope(sc, sc.stc, sc.linkage, sc.cppmangle, this.visibility, 1, sc.aligndecl, sc.inlining);
}

override const(char)* kind() const
{
return "visibility attribute";
Expand Down Expand Up @@ -620,11 +541,6 @@ extern (C++) final class AlignDeclaration : AttribDeclaration
Dsymbol.arraySyntaxCopy(decl));
}

override Scope* newScope(Scope* sc)
{
return createNewScope(sc, sc.stc, sc.linkage, sc.cppmangle, sc.visibility, sc.explicitVisibility, this, sc.inlining);
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -693,17 +609,6 @@ extern (C++) final class PragmaDeclaration : AttribDeclaration
return new PragmaDeclaration(loc, ident, Expression.arraySyntaxCopy(args), Dsymbol.arraySyntaxCopy(decl));
}

override Scope* newScope(Scope* sc)
{
if (ident == Id.Pinline)
{
// We keep track of this pragma inside scopes,
// then it's evaluated on demand in function semantic
return createNewScope(sc, sc.stc, sc.linkage, sc.cppmangle, sc.visibility, sc.explicitVisibility, sc.aligndecl, this);
}
return sc;
}

override const(char)* kind() const
{
return "pragma";
Expand Down Expand Up @@ -1004,13 +909,6 @@ extern(C++) final class ForwardingAttribDeclaration : AttribDeclaration
sym.symtab = new DsymbolTable();
}

/**************************************
* Use the ForwardingScopeDsymbol as the parent symbol for members.
*/
override Scope* newScope(Scope* sc)
{
return sc.push(sym);
}

override inout(ForwardingAttribDeclaration) isForwardingAttribDeclaration() inout
{
Expand Down Expand Up @@ -1087,18 +985,6 @@ extern (C++) final class UserAttributeDeclaration : AttribDeclaration
return new UserAttributeDeclaration(Expression.arraySyntaxCopy(this.atts), Dsymbol.arraySyntaxCopy(decl));
}

override Scope* newScope(Scope* sc)
{
Scope* sc2 = sc;
if (atts && atts.length)
{
// create new one for changes
sc2 = sc.copy();
sc2.userAttribDecl = this;
}
return sc2;
}

extern (D) static Expressions* concat(Expressions* udas1, Expressions* udas2)
{
Expressions* udas;
Expand Down Expand Up @@ -1178,7 +1064,6 @@ int foreachUdaNoSemantic(Dsymbol sym, int delegate(Expression) dg)
return 0;
}


/**
* Returns: true if the given expression is an enum from `core.attribute` named `id`
*/
Expand Down
11 changes: 0 additions & 11 deletions compiler/src/dmd/attrib.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class AttribDeclaration : public Dsymbol
Dsymbols *decl; // array of Dsymbol's

virtual Dsymbols *include(Scope *sc);
virtual Scope *newScope(Scope *sc);
void addComment(const utf8_t *comment) override;
const char *kind() const override;
bool oneMember(Dsymbol *&ps, Identifier *ident) override;
Expand All @@ -48,7 +47,6 @@ class StorageClassDeclaration : public AttribDeclaration
StorageClass stc;

StorageClassDeclaration *syntaxCopy(Dsymbol *s) override;
Scope *newScope(Scope *sc) override;
bool oneMember(Dsymbol *&ps, Identifier *ident) override final;
StorageClassDeclaration *isStorageClassDeclaration() override { return this; }

Expand All @@ -62,7 +60,6 @@ class DeprecatedDeclaration final : public StorageClassDeclaration
const char *msgstr;

DeprecatedDeclaration *syntaxCopy(Dsymbol *s) override;
Scope *newScope(Scope *sc) override;
void accept(Visitor *v) override { v->visit(this); }
};

Expand All @@ -73,7 +70,6 @@ class LinkDeclaration final : public AttribDeclaration

static LinkDeclaration *create(const Loc &loc, LINK p, Dsymbols *decl);
LinkDeclaration *syntaxCopy(Dsymbol *s) override;
Scope *newScope(Scope *sc) override;
const char *toChars() const override;
void accept(Visitor *v) override { v->visit(this); }
};
Expand All @@ -84,7 +80,6 @@ class CPPMangleDeclaration final : public AttribDeclaration
CPPMANGLE cppmangle;

CPPMangleDeclaration *syntaxCopy(Dsymbol *s) override;
Scope *newScope(Scope *sc) override;
const char *toChars() const override;
void accept(Visitor *v) override { v->visit(this); }
};
Expand All @@ -95,7 +90,6 @@ class CPPNamespaceDeclaration final : public AttribDeclaration
Expression *exp;

CPPNamespaceDeclaration *syntaxCopy(Dsymbol *s) override;
Scope *newScope(Scope *sc) override;
const char *toChars() const override;
void accept(Visitor *v) override { v->visit(this); }
};
Expand All @@ -107,7 +101,6 @@ class VisibilityDeclaration final : public AttribDeclaration
DArray<Identifier*> pkg_identifiers;

VisibilityDeclaration *syntaxCopy(Dsymbol *s) override;
Scope *newScope(Scope *sc) override;
const char *kind() const override;
const char *toPrettyChars(bool unused) override;
VisibilityDeclaration *isVisibilityDeclaration() override { return this; }
Expand All @@ -121,7 +114,6 @@ class AlignDeclaration final : public AttribDeclaration
structalign_t salign;

AlignDeclaration *syntaxCopy(Dsymbol *s) override;
Scope *newScope(Scope *sc) override;
void accept(Visitor *v) override { v->visit(this); }
};

Expand All @@ -146,7 +138,6 @@ class PragmaDeclaration final : public AttribDeclaration
Expressions *args; // array of Expression's

PragmaDeclaration *syntaxCopy(Dsymbol *s) override;
Scope *newScope(Scope *sc) override;
const char *kind() const override;
void accept(Visitor *v) override { v->visit(this); }
};
Expand Down Expand Up @@ -200,7 +191,6 @@ class ForwardingAttribDeclaration final : public AttribDeclaration
public:
ForwardingScopeDsymbol *sym;

Scope *newScope(Scope *sc) override;
ForwardingAttribDeclaration *isForwardingAttribDeclaration() override { return this; }
void accept(Visitor *v) override { v->visit(this); }
};
Expand Down Expand Up @@ -230,7 +220,6 @@ class UserAttributeDeclaration final : public AttribDeclaration
Expressions *atts;

UserAttributeDeclaration *syntaxCopy(Dsymbol *s) override;
Scope *newScope(Scope *sc) override;
const char *kind() const override;
void accept(Visitor *v) override { v->visit(this); }
};
1 change: 1 addition & 0 deletions compiler/src/dmd/dsymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,5 @@ namespace dmd
Dsymbol *search(Dsymbol *d, const Loc &loc, Identifier *ident, SearchOptFlags flags = (SearchOptFlags)SearchOpt::localsOnly);
void setScope(Dsymbol *d, Scope *sc);
void importAll(Dsymbol *d, Scope *sc);
Scope* newScope(Dsymbol *d, Scope *sc);
}
Loading

0 comments on commit d4a0921

Please sign in to comment.