Extracted Dsymbol.hasPointers to visitor in dsymbolsem (#21140)

This commit is contained in:
Matthew Qiu 2025-04-08 04:20:01 -04:00 committed by GitHub
parent 857d4a64c8
commit 69b2b10aef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 61 additions and 44 deletions

View file

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

View file

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

View file

@ -198,6 +198,12 @@ bool isAbstract(ClassDeclaration cd)
return dmd.dsymbolsem.isAbstract(cd); return dmd.dsymbolsem.isAbstract(cd);
} }
bool hasPointers(Dsymbol d)
{
import dmd.dsymbolsem;
return dmd.dsymbolsem.hasPointers(d);
}
/*********************************************************** /***********************************************************
* dtemplate.d * dtemplate.d
*/ */

View file

@ -1035,12 +1035,6 @@ extern (C++) class VarDeclaration : Declaration
vbitoffset < bitoffset + tbitsize; vbitoffset < bitoffset + tbitsize;
} }
override final bool hasPointers()
{
//printf("VarDeclaration::hasPointers() %s, ty = %d\n", toChars(), type.ty);
return (!isDataseg() && type.hasPointers());
}
/************************************* /*************************************
* Return true if we can take the address of this variable. * Return true if we can take the address of this variable.
*/ */

View file

@ -298,7 +298,6 @@ public:
bool isThreadlocal() override final; bool isThreadlocal() override final;
bool isCTFE(); bool isCTFE();
bool isOverlappedWith(VarDeclaration *v); bool isOverlappedWith(VarDeclaration *v);
bool hasPointers() override final;
bool canTakeAddressOf(); bool canTakeAddressOf();
bool needsScopeDtor(); bool needsScopeDtor();
Dsymbol *toAlias() override final; Dsymbol *toAlias() override final;

View file

@ -158,13 +158,14 @@ extern (C++) class StructDeclaration : AggregateDeclaration
return; return;
foreach (vd; fields) foreach (vd; fields)
{ {
import dmd.dsymbolsem : hasPointers;
if (vd.storage_class & STC.ref_ || vd.hasPointers()) if (vd.storage_class & STC.ref_ || vd.hasPointers())
{ {
hasPointerField = true; hasPointerField = true;
hasUnsafeBitpatterns = true; hasUnsafeBitpatterns = true;
} }
if (vd._init && vd._init.isVoidInitializer() && vd.type.hasPointers()) if (vd._init && vd._init.isVoidInitializer() && vd.hasPointers())
hasVoidInitPointers = true; hasVoidInitPointers = true;
if (vd.storage_class & STC.system || vd.type.hasUnsafeBitpatterns()) if (vd.storage_class & STC.system || vd.type.hasUnsafeBitpatterns())

View file

@ -963,15 +963,6 @@ extern (C++) class Dsymbol : ASTNode
return true; return true;
} }
/*****************************************
* Is Dsymbol a variable that contains pointers?
*/
bool hasPointers()
{
//printf("Dsymbol::hasPointers() %s\n", toChars());
return false;
}
void addObjcSymbols(ClassDeclarations* classes, ClassDeclarations* categories) void addObjcSymbols(ClassDeclarations* classes, ClassDeclarations* categories)
{ {
} }

View file

@ -243,7 +243,6 @@ public:
virtual bool needThis(); // need a 'this' pointer? virtual bool needThis(); // need a 'this' pointer?
virtual Visibility visible(); virtual Visibility visible();
virtual Dsymbol *syntaxCopy(Dsymbol *s); // copy only syntax trees virtual Dsymbol *syntaxCopy(Dsymbol *s); // copy only syntax trees
virtual bool hasPointers();
virtual void addObjcSymbols(ClassDeclarations *, ClassDeclarations *) { } virtual void addObjcSymbols(ClassDeclarations *, ClassDeclarations *) { }
virtual void addComment(const utf8_t *comment); virtual void addComment(const utf8_t *comment);
@ -428,4 +427,5 @@ namespace dmd
Dsymbols *include(Dsymbol *d, Scope *sc); Dsymbols *include(Dsymbol *d, Scope *sc);
void setScope(Dsymbol *d, Scope *sc); void setScope(Dsymbol *d, Scope *sc);
void importAll(Dsymbol *d, Scope *sc); void importAll(Dsymbol *d, Scope *sc);
bool hasPointers(Dsymbol *d);
} }

View file

@ -953,6 +953,8 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
if (dsym.storage_class & STC.constscoperef) if (dsym.storage_class & STC.constscoperef)
dsym.storage_class |= STC.scope_; dsym.storage_class |= STC.scope_;
import dmd.typesem : hasPointers;
if (dsym.storage_class & STC.scope_) if (dsym.storage_class & STC.scope_)
{ {
STC stc = dsym.storage_class & (STC.static_ | STC.extern_ | STC.manifest | STC.gshared); STC stc = dsym.storage_class & (STC.static_ | STC.extern_ | STC.manifest | STC.gshared);
@ -8641,3 +8643,50 @@ private extern(C++) class FinalizeSizeVisitor : Visitor
sd.argTypes = target.toArgTypes(sd.type); sd.argTypes = target.toArgTypes(sd.type);
} }
} }
/*****************************************
* Is Dsymbol a variable that contains pointers?
*/
bool hasPointers(Dsymbol d)
{
scope v = new HasPointersVisitor();
d.accept(v);
return v.result;
}
private extern(C++) class HasPointersVisitor : Visitor
{
import dmd.mtype : Type;
alias visit = Visitor.visit;
bool result;
override void visit(AttribDeclaration ad)
{
result = ad.include(null).foreachDsymbol( (s) { return s.hasPointers(); } ) != 0;
}
override void visit(VarDeclaration vd)
{
import dmd.typesem : hasPointers;
result = (!vd.isDataseg() && vd.type.hasPointers());
}
override void visit(Dsymbol d)
{
//printf("Dsymbol::hasPointers() %s\n", toChars());
result = false;
}
override void visit(TemplateMixin tm)
{
//printf("TemplateMixin.hasPointers() %s\n", toChars());
result = tm.members.foreachDsymbol( (s) { return s.hasPointers(); } ) != 0;
}
override void visit(Nspace ns)
{
//printf("Nspace::hasPointers() %s\n", toChars());
result = ns.members.foreachDsymbol( (s) { return s.hasPointers(); } ) != 0;
}
}

View file

@ -5490,12 +5490,6 @@ extern (C++) final class TemplateMixin : TemplateInstance
return "mixin"; return "mixin";
} }
override bool hasPointers()
{
//printf("TemplateMixin.hasPointers() %s\n", toChars());
return members.foreachDsymbol( (s) { return s.hasPointers(); } ) != 0;
}
extern (D) bool findTempDecl(Scope* sc) extern (D) bool findTempDecl(Scope* sc)
{ {
// Follow qualifications to find the TemplateDeclaration // Follow qualifications to find the TemplateDeclaration

View file

@ -618,7 +618,6 @@ public:
virtual bool needThis(); virtual bool needThis();
virtual Visibility visible(); virtual Visibility visible();
virtual Dsymbol* syntaxCopy(Dsymbol* s); virtual Dsymbol* syntaxCopy(Dsymbol* s);
virtual bool hasPointers();
virtual void addObjcSymbols(Array<ClassDeclaration* >* classes, Array<ClassDeclaration* >* categories); virtual void addObjcSymbols(Array<ClassDeclaration* >* classes, Array<ClassDeclaration* >* categories);
virtual void addComment(const char* comment); virtual void addComment(const char* comment);
const char* comment(); const char* comment();
@ -1822,7 +1821,6 @@ public:
TypeQualified* tqual; TypeQualified* tqual;
TemplateInstance* syntaxCopy(Dsymbol* s) override; TemplateInstance* syntaxCopy(Dsymbol* s) override;
const char* kind() const override; const char* kind() const override;
bool hasPointers() override;
void accept(Visitor* v) override; void accept(Visitor* v) override;
}; };
@ -4916,7 +4914,6 @@ class Nspace final : public ScopeDsymbol
public: public:
Expression* identExp; Expression* identExp;
Nspace* syntaxCopy(Dsymbol* s) override; Nspace* syntaxCopy(Dsymbol* s) override;
bool hasPointers() override;
const char* kind() const override; const char* kind() const override;
void accept(Visitor* v) override; void accept(Visitor* v) override;
}; };
@ -6418,7 +6415,6 @@ class AttribDeclaration : public Dsymbol
public: public:
Array<Dsymbol* >* decl; Array<Dsymbol* >* decl;
const char* kind() const override; const char* kind() const override;
bool hasPointers() final override;
void addObjcSymbols(Array<ClassDeclaration* >* classes, Array<ClassDeclaration* >* categories) final override; void addObjcSymbols(Array<ClassDeclaration* >* classes, Array<ClassDeclaration* >* categories) final override;
void accept(Visitor* v) override; void accept(Visitor* v) override;
}; };
@ -6892,7 +6888,6 @@ public:
bool isThreadlocal() final override; bool isThreadlocal() final override;
bool isCTFE(); bool isCTFE();
bool isOverlappedWith(VarDeclaration* v); bool isOverlappedWith(VarDeclaration* v);
bool hasPointers() final override;
bool canTakeAddressOf(); bool canTakeAddressOf();
bool needsScopeDtor(); bool needsScopeDtor();
Dsymbol* toAlias() final override; Dsymbol* toAlias() final override;

View file

@ -78,12 +78,6 @@ extern (C++) final class Nspace : ScopeDsymbol
return ns; return ns;
} }
override bool hasPointers()
{
//printf("Nspace::hasPointers() %s\n", toChars());
return members.foreachDsymbol( (s) { return s.hasPointers(); } ) != 0;
}
override const(char)* kind() const override const(char)* kind() const
{ {
return "namespace"; return "namespace";

View file

@ -21,7 +21,6 @@ class Nspace final : public ScopeDsymbol
public: public:
Expression *identExp; Expression *identExp;
Nspace *syntaxCopy(Dsymbol *s) override; Nspace *syntaxCopy(Dsymbol *s) override;
bool hasPointers() override;
const char *kind() const override; const char *kind() const override;
void accept(Visitor *v) override { v->visit(this); } void accept(Visitor *v) override { v->visit(this); }
}; };

View file

@ -284,7 +284,6 @@ public:
TemplateMixin *syntaxCopy(Dsymbol *s) override; TemplateMixin *syntaxCopy(Dsymbol *s) override;
const char *kind() const override; const char *kind() const override;
bool hasPointers() override;
void accept(Visitor *v) override { v->visit(this); } void accept(Visitor *v) override { v->visit(this); }
}; };

View file

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

View file

@ -6686,6 +6686,7 @@ STC parameterStorageClass(TypeFunction tf, Type tthis, Parameter p, VarDeclarati
// Check escaping through `this` // Check escaping through `this`
if (tthis && tthis.isMutable()) if (tthis && tthis.isMutable())
{ {
import dmd.dsymbolsem : hasPointers;
foreach (VarDeclaration v; isAggregate(tthis).fields) foreach (VarDeclaration v; isAggregate(tthis).fields)
{ {
if (v.hasPointers()) if (v.hasPointers())
@ -6696,6 +6697,7 @@ STC parameterStorageClass(TypeFunction tf, Type tthis, Parameter p, VarDeclarati
// Check escaping through nested context // Check escaping through nested context
if (outerVars && tf.isMutable()) if (outerVars && tf.isMutable())
{ {
import dmd.dsymbolsem : hasPointers;
foreach (VarDeclaration v; *outerVars) foreach (VarDeclaration v; *outerVars)
{ {
if (v.hasPointers()) if (v.hasPointers())