mirror of
https://github.com/dlang/dmd.git
synced 2025-05-09 04:26:31 +03:00
Extract dsymbol.addComment to dsymbolsem and turn it into a visitor (#17110)
Implemented a visitor in dsymbolsem.d to successfully relocate addComment functions from attrib.d.
This commit is contained in:
parent
059b1ab407
commit
0acada30cb
6 changed files with 74 additions and 53 deletions
|
@ -104,16 +104,6 @@ extern (C++) abstract class AttribDeclaration : Dsymbol
|
||||||
return sc2;
|
return sc2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override void addComment(const(char)* comment)
|
|
||||||
{
|
|
||||||
//printf("AttribDeclaration::addComment %s\n", comment);
|
|
||||||
if (comment)
|
|
||||||
{
|
|
||||||
this.include(null).foreachDsymbol( s => s.addComment(comment) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override const(char)* kind() const
|
override const(char)* kind() const
|
||||||
{
|
{
|
||||||
return "attribute";
|
return "attribute";
|
||||||
|
@ -653,20 +643,6 @@ extern (C++) class ConditionalDeclaration : AttribDeclaration
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override final void addComment(const(char)* comment)
|
|
||||||
{
|
|
||||||
/* Because addComment is called by the parser, if we called
|
|
||||||
* include() it would define a version before it was used.
|
|
||||||
* But it's no problem to drill down to both decl and elsedecl,
|
|
||||||
* so that's the workaround.
|
|
||||||
*/
|
|
||||||
if (comment)
|
|
||||||
{
|
|
||||||
decl .foreachDsymbol( s => s.addComment(comment) );
|
|
||||||
elsedecl.foreachDsymbol( s => s.addComment(comment) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override void accept(Visitor v)
|
override void accept(Visitor v)
|
||||||
{
|
{
|
||||||
v.visit(this);
|
v.visit(this);
|
||||||
|
@ -762,12 +738,6 @@ extern (C++) final class StaticForeachDeclaration : AttribDeclaration
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
override void addComment(const(char)* comment)
|
|
||||||
{
|
|
||||||
// do nothing
|
|
||||||
// change this to give semantics to documentation comments on static foreach declarations
|
|
||||||
}
|
|
||||||
|
|
||||||
override const(char)* kind() const
|
override const(char)* kind() const
|
||||||
{
|
{
|
||||||
return "static foreach";
|
return "static foreach";
|
||||||
|
|
|
@ -28,7 +28,6 @@ class AttribDeclaration : public Dsymbol
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Dsymbols *decl; // array of Dsymbol's
|
Dsymbols *decl; // array of Dsymbol's
|
||||||
void addComment(const utf8_t *comment) override;
|
|
||||||
const char *kind() const override;
|
const char *kind() const override;
|
||||||
bool oneMember(Dsymbol *&ps, Identifier *ident) override;
|
bool oneMember(Dsymbol *&ps, Identifier *ident) override;
|
||||||
bool hasPointers() override final;
|
bool hasPointers() override final;
|
||||||
|
@ -148,7 +147,6 @@ public:
|
||||||
|
|
||||||
ConditionalDeclaration *syntaxCopy(Dsymbol *s) override;
|
ConditionalDeclaration *syntaxCopy(Dsymbol *s) override;
|
||||||
bool oneMember(Dsymbol *&ps, Identifier *ident) override final;
|
bool oneMember(Dsymbol *&ps, Identifier *ident) override final;
|
||||||
void addComment(const utf8_t *comment) override final;
|
|
||||||
void accept(Visitor *v) override { v->visit(this); }
|
void accept(Visitor *v) override { v->visit(this); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -176,7 +174,6 @@ public:
|
||||||
|
|
||||||
StaticForeachDeclaration *syntaxCopy(Dsymbol *s) override;
|
StaticForeachDeclaration *syntaxCopy(Dsymbol *s) override;
|
||||||
bool oneMember(Dsymbol *&ps, Identifier *ident) override;
|
bool oneMember(Dsymbol *&ps, Identifier *ident) override;
|
||||||
void addComment(const utf8_t *comment) 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); }
|
||||||
};
|
};
|
||||||
|
|
|
@ -925,22 +925,8 @@ extern (C++) class Dsymbol : ASTNode
|
||||||
*/
|
*/
|
||||||
void addComment(const(char)* comment)
|
void addComment(const(char)* comment)
|
||||||
{
|
{
|
||||||
if (!comment || !*comment)
|
import dmd.dsymbolsem;
|
||||||
return;
|
dmd.dsymbolsem.addComment(this, comment);
|
||||||
|
|
||||||
//printf("addComment '%s' to Dsymbol %p '%s'\n", comment, this, toChars());
|
|
||||||
void* h = cast(void*)this; // just the pointer is the key
|
|
||||||
auto p = h in commentHashTable;
|
|
||||||
if (!p)
|
|
||||||
{
|
|
||||||
commentHashTable[h] = comment;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (strcmp(*p, comment) != 0)
|
|
||||||
{
|
|
||||||
// Concatenate the two
|
|
||||||
*p = Lexer.combineComments((*p).toDString(), comment.toDString(), true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// get documentation comment for this Dsymbol
|
/// get documentation comment for this Dsymbol
|
||||||
|
@ -958,7 +944,7 @@ extern (C++) class Dsymbol : ASTNode
|
||||||
/* Shell around addComment() to avoid disruption for the moment */
|
/* Shell around addComment() to avoid disruption for the moment */
|
||||||
final void comment(const(char)* comment) { addComment(comment); }
|
final void comment(const(char)* comment) { addComment(comment); }
|
||||||
|
|
||||||
private extern (D) __gshared const(char)*[void*] commentHashTable;
|
extern (D) __gshared const(char)*[void*] commentHashTable;
|
||||||
|
|
||||||
|
|
||||||
/**********************************
|
/**********************************
|
||||||
|
|
|
@ -433,4 +433,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);
|
||||||
|
void addComment(Dsymbol *d, const char *comment);
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,6 +53,7 @@ import dmd.init;
|
||||||
import dmd.initsem;
|
import dmd.initsem;
|
||||||
import dmd.intrange;
|
import dmd.intrange;
|
||||||
import dmd.hdrgen;
|
import dmd.hdrgen;
|
||||||
|
import dmd.lexer;
|
||||||
import dmd.location;
|
import dmd.location;
|
||||||
import dmd.mtype;
|
import dmd.mtype;
|
||||||
import dmd.mustuse;
|
import dmd.mustuse;
|
||||||
|
@ -64,6 +65,7 @@ import dmd.parse;
|
||||||
debug import dmd.printast;
|
debug import dmd.printast;
|
||||||
import dmd.root.array;
|
import dmd.root.array;
|
||||||
import dmd.root.filename;
|
import dmd.root.filename;
|
||||||
|
import dmd.root.string;
|
||||||
import dmd.common.outbuffer;
|
import dmd.common.outbuffer;
|
||||||
import dmd.root.rmem;
|
import dmd.root.rmem;
|
||||||
import dmd.rootobject;
|
import dmd.rootobject;
|
||||||
|
@ -7875,3 +7877,57 @@ Lfail:
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern (C++) void addComment(Dsymbol d, const(char)* comment)
|
||||||
|
{
|
||||||
|
scope v = new AddCommentVisitor(comment);
|
||||||
|
d.accept(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern (C++) class AddCommentVisitor: Visitor
|
||||||
|
{
|
||||||
|
alias visit = Visitor.visit;
|
||||||
|
|
||||||
|
const(char)* comment;
|
||||||
|
|
||||||
|
this(const(char)* comment)
|
||||||
|
{
|
||||||
|
this.comment = comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
override void visit(Dsymbol d)
|
||||||
|
{
|
||||||
|
if (!comment || !*comment)
|
||||||
|
return;
|
||||||
|
|
||||||
|
//printf("addComment '%s' to Dsymbol %p '%s'\n", comment, this, toChars());
|
||||||
|
void* h = cast(void*)d; // just the pointer is the key
|
||||||
|
auto p = h in d.commentHashTable;
|
||||||
|
if (!p)
|
||||||
|
{
|
||||||
|
d.commentHashTable[h] = comment;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (strcmp(*p, comment) != 0)
|
||||||
|
{
|
||||||
|
// Concatenate the two
|
||||||
|
*p = Lexer.combineComments((*p).toDString(), comment.toDString(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
override void visit(AttribDeclaration atd)
|
||||||
|
{
|
||||||
|
if (comment)
|
||||||
|
{
|
||||||
|
atd.include(null).foreachDsymbol( s => s.addComment(comment) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
override void visit(ConditionalDeclaration cd)
|
||||||
|
{
|
||||||
|
if (comment)
|
||||||
|
{
|
||||||
|
cd.decl .foreachDsymbol( s => s.addComment(comment) );
|
||||||
|
cd.elsedecl.foreachDsymbol( s => s.addComment(comment) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
override void visit(StaticForeachDeclaration sfd) {}
|
||||||
|
}
|
||||||
|
|
|
@ -6204,7 +6204,6 @@ class AttribDeclaration : public Dsymbol
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Array<Dsymbol* >* decl;
|
Array<Dsymbol* >* decl;
|
||||||
void addComment(const char* comment) override;
|
|
||||||
const char* kind() const override;
|
const char* kind() const override;
|
||||||
bool oneMember(Dsymbol*& ps, Identifier* ident) override;
|
bool oneMember(Dsymbol*& ps, Identifier* ident) override;
|
||||||
bool hasPointers() final override;
|
bool hasPointers() final override;
|
||||||
|
@ -6314,7 +6313,6 @@ public:
|
||||||
Array<Dsymbol* >* elsedecl;
|
Array<Dsymbol* >* elsedecl;
|
||||||
ConditionalDeclaration* syntaxCopy(Dsymbol* s) override;
|
ConditionalDeclaration* syntaxCopy(Dsymbol* s) override;
|
||||||
bool oneMember(Dsymbol*& ps, Identifier* ident) final override;
|
bool oneMember(Dsymbol*& ps, Identifier* ident) final override;
|
||||||
void addComment(const char* comment) final override;
|
|
||||||
void accept(Visitor* v) override;
|
void accept(Visitor* v) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -6340,7 +6338,6 @@ public:
|
||||||
Array<Dsymbol* >* cache;
|
Array<Dsymbol* >* cache;
|
||||||
StaticForeachDeclaration* syntaxCopy(Dsymbol* s) override;
|
StaticForeachDeclaration* syntaxCopy(Dsymbol* s) override;
|
||||||
bool oneMember(Dsymbol*& ps, Identifier* ident) override;
|
bool oneMember(Dsymbol*& ps, Identifier* ident) override;
|
||||||
void addComment(const char* comment) override;
|
|
||||||
const char* kind() const override;
|
const char* kind() const override;
|
||||||
void accept(Visitor* v) override;
|
void accept(Visitor* v) override;
|
||||||
};
|
};
|
||||||
|
@ -7393,6 +7390,20 @@ public:
|
||||||
void visit(StaticForeachDeclaration* sfd) override;
|
void visit(StaticForeachDeclaration* sfd) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern void addComment(Dsymbol* d, const char* comment);
|
||||||
|
|
||||||
|
class AddCommentVisitor : public Visitor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using Visitor::visit;
|
||||||
|
const char* comment;
|
||||||
|
AddCommentVisitor(const char* comment);
|
||||||
|
void visit(Dsymbol* d) override;
|
||||||
|
void visit(AttribDeclaration* atd) override;
|
||||||
|
void visit(ConditionalDeclaration* cd) override;
|
||||||
|
void visit(StaticForeachDeclaration* sfd) override;
|
||||||
|
};
|
||||||
|
|
||||||
class NrvoWalker final : public StatementRewriteWalker
|
class NrvoWalker final : public StatementRewriteWalker
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue