Move Dsymbol.hasStaticCtorOrDtor to dsymbolsem (#20943)

This commit is contained in:
Matthew Qiu 2025-03-04 17:23:33 -05:00 committed by GitHub
parent ddc7eaf570
commit bc4c7e4452
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 63 additions and 59 deletions

View file

@ -115,11 +115,6 @@ extern (C++) abstract class AttribDeclaration : Dsymbol
return this.include(null).foreachDsymbol( (s) { return s.hasPointers(); } ) != 0;
}
override final bool hasStaticCtorOrDtor()
{
return this.include(null).foreachDsymbol( (s) { return s.hasStaticCtorOrDtor(); } ) != 0;
}
/****************************************
*/
override final void addObjcSymbols(ClassDeclarations* classes, ClassDeclarations* categories)

View file

@ -30,8 +30,6 @@ public:
Dsymbols *decl; // array of Dsymbol's
const char *kind() const override;
bool hasPointers() override final;
bool hasStaticCtorOrDtor() override final;
void accept(Visitor *v) override { v->visit(this); }
};

View file

@ -813,7 +813,6 @@ public:
bool isVirtual() const override final;
bool addPreInvariant() override final;
bool addPostInvariant() override final;
bool hasStaticCtorOrDtor() override final;
void accept(Visitor *v) override { v->visit(this); }
};
@ -835,7 +834,6 @@ public:
StaticDtorDeclaration *syntaxCopy(Dsymbol *) override;
AggregateDeclaration *isThis() override final;
bool isVirtual() const override final;
bool hasStaticCtorOrDtor() override final;
bool addPreInvariant() override final;
bool addPostInvariant() override final;

View file

@ -972,12 +972,6 @@ extern (C++) class Dsymbol : ASTNode
return false;
}
bool hasStaticCtorOrDtor()
{
//printf("Dsymbol::hasStaticCtorOrDtor() %s\n", toChars());
return false;
}
void addObjcSymbols(ClassDeclarations* classes, ClassDeclarations* categories)
{
}
@ -1491,24 +1485,6 @@ public:
return symtab.lookup(id);
}
/****************************************
* Return true if any of the members are static ctors or static dtors, or if
* any members have members that are.
*/
override bool hasStaticCtorOrDtor()
{
if (members)
{
for (size_t i = 0; i < members.length; i++)
{
Dsymbol member = (*members)[i];
if (member.hasStaticCtorOrDtor())
return true;
}
}
return false;
}
override void accept(Visitor v)
{
v.visit(this);

View file

@ -244,7 +244,6 @@ public:
virtual Visibility visible();
virtual Dsymbol *syntaxCopy(Dsymbol *s); // copy only syntax trees
virtual bool hasPointers();
virtual bool hasStaticCtorOrDtor();
virtual void addObjcSymbols(ClassDeclarations *, ClassDeclarations *) { }
virtual void addComment(const utf8_t *comment);
@ -337,7 +336,6 @@ public:
const char *kind() const override;
virtual Dsymbol *symtabInsert(Dsymbol *s);
virtual Dsymbol *symtabLookup(Dsymbol *s, Identifier *id);
bool hasStaticCtorOrDtor() override;
void accept(Visitor *v) override { v->visit(this); }
};
@ -432,4 +430,5 @@ namespace dmd
void importAll(Dsymbol *d, Scope *sc);
void addComment(Dsymbol *d, const char *comment);
bool oneMember(Dsymbol *d, Dsymbol *&ps, Identifier *ident);
bool hasStaticCtorOrDtor(Dsymbol *d);
}

View file

@ -8102,3 +8102,61 @@ private extern(C++) class OneMemberVisitor : Visitor
result = true;
}
}
/****************************************
* Return true if any of the members are static ctors or static dtors, or if
* any members have members that are.
*/
extern(C++) bool hasStaticCtorOrDtor(Dsymbol d)
{
scope v = new HasStaticCtorOrDtor();
d.accept(v);
return v.result;
}
private extern(C++) class HasStaticCtorOrDtor : Visitor
{
import dmd.mtype : Type;
alias visit = Visitor.visit;
bool result;
// attrib.d
override void visit(AttribDeclaration ad){
result = ad.include(null).foreachDsymbol( (s) { return s.hasStaticCtorOrDtor(); } ) != 0;
}
// dsymbol.d
override void visit(Dsymbol d){
//printf("Dsymbol::hasStaticCtorOrDtor() %s\n", toChars());
result = false;
}
override void visit(ScopeDsymbol sd) {
if (sd.members)
{
for (size_t i = 0; i < sd.members.length; i++)
{
Dsymbol member = (*(sd.members))[i];
if (member.hasStaticCtorOrDtor())
result = true;
return;
}
}
result = false;
}
// dtemplate.d
override void visit(TemplateDeclaration td) {
result = false; // don't scan uninstantiated templates
}
// func.d
override void visit(StaticCtorDeclaration scd) {
result = true;
}
override void visit(StaticDtorDeclaration sdd) @nogc nothrow pure @safe {
result = true;
}
}

View file

@ -53,7 +53,7 @@ import dmd.dinterpret;
import dmd.dmodule;
import dmd.dscope;
import dmd.dsymbol;
import dmd.dsymbolsem : dsymbolSemantic, checkDeprecated, aliasSemantic, search, search_correct, setScope, importAll, include;
import dmd.dsymbolsem : dsymbolSemantic, checkDeprecated, aliasSemantic, search, search_correct, setScope, importAll, include, hasStaticCtorOrDtor;
import dmd.errors;
import dmd.errorsink;
import dmd.expression;
@ -744,11 +744,6 @@ extern (C++) final class TemplateDeclaration : ScopeDsymbol
return true;
}
override bool hasStaticCtorOrDtor()
{
return false; // don't scan uninstantiated templates
}
override const(char)* kind() const
{
return (onemember && onemember.isAggregateDeclaration()) ? onemember.kind() : "template";

View file

@ -602,7 +602,6 @@ public:
virtual Visibility visible();
virtual Dsymbol* syntaxCopy(Dsymbol* s);
virtual bool hasPointers();
virtual bool hasStaticCtorOrDtor();
virtual void addObjcSymbols(Array<ClassDeclaration* >* classes, Array<ClassDeclaration* >* categories);
virtual void addComment(const char* comment);
const char* comment();
@ -712,7 +711,6 @@ public:
const char* kind() const override;
virtual Dsymbol* symtabInsert(Dsymbol* s);
virtual Dsymbol* symtabLookup(Dsymbol* s, Identifier* id);
bool hasStaticCtorOrDtor() override;
void accept(Visitor* v) override;
};
@ -1738,7 +1736,6 @@ public:
Array<RootObject* >* lastConstraintTiargs;
TemplateDeclaration* syntaxCopy(Dsymbol* __param_0_) override;
bool overloadInsert(Dsymbol* s) override;
bool hasStaticCtorOrDtor() override;
const char* kind() const override;
const char* toCharsNoConstraints() const;
Visibility visible() override;
@ -4153,7 +4150,6 @@ public:
bool isVirtual() const final override;
bool addPreInvariant() final override;
bool addPostInvariant() final override;
bool hasStaticCtorOrDtor() final override;
void accept(Visitor* v) override;
};
@ -4172,7 +4168,6 @@ public:
StaticDtorDeclaration* syntaxCopy(Dsymbol* s) override;
AggregateDeclaration* isThis() final override;
bool isVirtual() const final override;
bool hasStaticCtorOrDtor() final override;
bool addPreInvariant() final override;
bool addPostInvariant() final override;
void accept(Visitor* v) override;
@ -6398,7 +6393,6 @@ public:
Array<Dsymbol* >* decl;
const char* kind() const override;
bool hasPointers() final override;
bool hasStaticCtorOrDtor() final override;
void addObjcSymbols(Array<ClassDeclaration* >* classes, Array<ClassDeclaration* >* categories) final override;
void accept(Visitor* v) override;
};
@ -7550,6 +7544,8 @@ public:
void visit(StaticForeachDeclaration* sfd) override;
};
extern bool hasStaticCtorOrDtor(Dsymbol* d);
extern void lowerNonArrayAggregate(StaticForeach* sfe, Scope* sc);
class NrvoWalker final : public StatementRewriteWalker

View file

@ -1544,11 +1544,6 @@ extern (C++) class StaticCtorDeclaration : FuncDeclaration
return false;
}
override final bool hasStaticCtorOrDtor() @nogc nothrow pure @safe
{
return true;
}
override void accept(Visitor v)
{
v.visit(this);
@ -1618,11 +1613,6 @@ extern (C++) class StaticDtorDeclaration : FuncDeclaration
return false;
}
override final bool hasStaticCtorOrDtor()
{
return true;
}
override final bool addPreInvariant()
{
return false;

View file

@ -77,7 +77,6 @@ public:
TemplateDeclaration *syntaxCopy(Dsymbol *) override;
bool overloadInsert(Dsymbol *s) override;
bool hasStaticCtorOrDtor() override;
const char *kind() const override;
Visibility visible() override;

View file

@ -35,7 +35,7 @@ import dmd.dmodule;
import dmd.dscope;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dsymbolsem : include;
import dmd.dsymbolsem : include, hasStaticCtorOrDtor;
import dmd.dtemplate;
import dmd.errors;
import dmd.errorsink;