Skip to content

Commit

Permalink
[Refactoring] Remove Expression::toCBuffer and use free function instead
Browse files Browse the repository at this point in the history
Directly call Expression::accept() in PrettyPrintVisitor
  • Loading branch information
9rnsr committed Aug 9, 2014
1 parent 2ddae5e commit 3984ff4
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/attrib.c
Expand Up @@ -561,7 +561,7 @@ void DeprecatedDeclaration::setScope(Scope *sc)
void DeprecatedDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring("deprecated(");
msg->toCBuffer(buf, hgs);
::toCBuffer(msg, buf, hgs);
buf->writestring(") ");
AttribDeclaration::toCBuffer(buf, hgs);
}
Expand Down Expand Up @@ -1481,7 +1481,7 @@ void CompileDeclaration::semantic(Scope *sc)
void CompileDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring("mixin(");
exp->toCBuffer(buf, hgs);
::toCBuffer(exp, buf, hgs);
buf->writestring(");");
buf->writenl();
}
Expand Down
3 changes: 2 additions & 1 deletion src/cond.c
Expand Up @@ -25,6 +25,7 @@
#include "mtype.h"
#include "scope.h"
#include "arraytypes.h"
#include "hdrgen.h"

int findCondition(Strings *ids, Identifier *ident)
{
Expand Down Expand Up @@ -395,6 +396,6 @@ int StaticIfCondition::include(Scope *sc, ScopeDsymbol *sds)
void StaticIfCondition::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring("static if (");
exp->toCBuffer(buf, hgs);
::toCBuffer(exp, buf, hgs);
buf->writeByte(')');
}
2 changes: 1 addition & 1 deletion src/declaration.c
Expand Up @@ -1818,7 +1818,7 @@ void VarDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
buf->writestring(" = ");
ExpInitializer *ie = init->isExpInitializer();
if (ie && (ie->exp->op == TOKconstruct || ie->exp->op == TOKblit))
((AssignExp *)ie->exp)->e2->toCBuffer(buf, hgs);
::toCBuffer(((AssignExp *)ie->exp)->e2, buf, hgs);
else
::toCBuffer(init, buf, hgs);
}
Expand Down
2 changes: 1 addition & 1 deletion src/doc.c
Expand Up @@ -1078,7 +1078,7 @@ void toDocBuffer(Dsymbol *s, OutBuffer *buf, Scope *sc)
HdrGenState hgs;
hgs.ddoc = true;
buf->writestring(" if (");
td->constraint->toCBuffer(buf, &hgs);
::toCBuffer(td->constraint, buf, &hgs);
buf->writeByte(')');
}

Expand Down
2 changes: 1 addition & 1 deletion src/enum.c
Expand Up @@ -566,7 +566,7 @@ void EnumMember::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
if (value)
{
buf->writestring(" = ");
value->toCBuffer(buf, hgs);
::toCBuffer(value, buf, hgs);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/expression.c
Expand Up @@ -1941,7 +1941,7 @@ char *Expression::toChars()
{
OutBuffer buf;
HdrGenState hgs;
toCBuffer(&buf, &hgs);
toCBuffer(this, &buf, &hgs);
return buf.extractString();
}

Expand Down
5 changes: 0 additions & 5 deletions src/expression.h
Expand Up @@ -104,7 +104,6 @@ elem *toElem(Expression *e, IRState *irs);
MATCH implicitConvTo(Expression *e, Type *t);
Expression *implicitCastTo(Expression *e, Scope *sc, Type *t);
Expression *castTo(Expression *e, Scope *sc, Type *t);
void toCBuffer(Expression *e, OutBuffer *buf, HdrGenState *hgs);
Expression *ctfeInterpret(Expression *);
Expression *inlineCopy(Expression *e, Scope *sc);
Expression *op_overload(Expression *e, Scope *sc);
Expand Down Expand Up @@ -178,10 +177,6 @@ class Expression : public RootObject
virtual real_t toImaginary();
virtual complex_t toComplex();
virtual StringExp *toStringExp();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
::toCBuffer(this, buf, hgs);
}
virtual void toMangleBuffer(OutBuffer *buf);
virtual int isLvalue();
virtual Expression *toLvalue(Scope *sc, Expression *e);
Expand Down
2 changes: 1 addition & 1 deletion src/func.c
Expand Up @@ -4496,7 +4496,7 @@ void FuncLiteralDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
if (rs && rs->exp)
{
buf->writestring(" => ");
rs->exp->toCBuffer(buf, hgs);
::toCBuffer(rs->exp, buf, hgs);
}
else
{
Expand Down
74 changes: 38 additions & 36 deletions src/hdrgen.c
Expand Up @@ -121,7 +121,7 @@ class PrettyPrintVisitor : public Visitor
{
if (s->exp)
{
s->exp->toCBuffer(buf, hgs);
s->exp->accept(this);
if (s->exp->op != TOKdeclaration)
{
buf->writeByte(';');
Expand All @@ -140,7 +140,7 @@ class PrettyPrintVisitor : public Visitor
void visit(CompileStatement *s)
{
buf->writestring("mixin(");
s->exp->toCBuffer(buf, hgs);
s->exp->accept(this);
buf->writestring(");");
if (!hgs->forStmtInit)
buf->writenl();
Expand Down Expand Up @@ -196,7 +196,7 @@ class PrettyPrintVisitor : public Visitor
buf->writestring(" = ");
ExpInitializer *ie = v->init->isExpInitializer();
if (ie && (ie->exp->op == TOKconstruct || ie->exp->op == TOKblit))
((AssignExp *)ie->exp)->e2->toCBuffer(buf, hgs);
((AssignExp *)ie->exp)->e2->accept(this);
else
v->init->accept(this);
}
Expand Down Expand Up @@ -246,7 +246,7 @@ class PrettyPrintVisitor : public Visitor
void visit(WhileStatement *s)
{
buf->writestring("while (");
s->condition->toCBuffer(buf, hgs);
s->condition->accept(this);
buf->writeByte(')');
buf->writenl();
if (s->body)
Expand All @@ -260,7 +260,7 @@ class PrettyPrintVisitor : public Visitor
if (s->body)
s->body->accept(this);
buf->writestring("while (");
s->condition->toCBuffer(buf, hgs);
s->condition->accept(this);
buf->writestring(");");
buf->writenl();
}
Expand All @@ -279,13 +279,13 @@ class PrettyPrintVisitor : public Visitor
if (s->condition)
{
buf->writeByte(' ');
s->condition->toCBuffer(buf, hgs);
s->condition->accept(this);
}
buf->writeByte(';');
if (s->increment)
{
buf->writeByte(' ');
s->increment->toCBuffer(buf, hgs);
s->increment->accept(this);
}
buf->writeByte(')');
buf->writenl();
Expand Down Expand Up @@ -315,7 +315,7 @@ class PrettyPrintVisitor : public Visitor
buf->writestring(a->ident->toChars());
}
buf->writestring("; ");
s->aggr->toCBuffer(buf, hgs);
s->aggr->accept(this);
buf->writeByte(')');
buf->writenl();
buf->writeByte('{');
Expand All @@ -339,9 +339,9 @@ class PrettyPrintVisitor : public Visitor
buf->writestring(s->arg->ident->toChars());

buf->writestring("; ");
s->lwr->toCBuffer(buf, hgs);
s->lwr->accept(this);
buf->writestring(" .. ");
s->upr->toCBuffer(buf, hgs);
s->upr->accept(this);
buf->writeByte(')');
buf->writenl();
buf->writeByte('{');
Expand All @@ -368,7 +368,7 @@ class PrettyPrintVisitor : public Visitor
}
buf->writestring(" = ");
}
s->condition->toCBuffer(buf, hgs);
s->condition->accept(this);
buf->writeByte(')');
buf->writenl();
if (!s->ifbody->isScopeStatement())
Expand Down Expand Up @@ -453,7 +453,7 @@ class PrettyPrintVisitor : public Visitor
void visit(SwitchStatement *s)
{
buf->writestring(s->isFinal ? "final switch (" : "switch (");
s->condition->toCBuffer(buf, hgs);
s->condition->accept(this);
buf->writeByte(')');
buf->writenl();
if (s->body)
Expand All @@ -478,7 +478,7 @@ class PrettyPrintVisitor : public Visitor
void visit(CaseStatement *s)
{
buf->writestring("case ");
s->exp->toCBuffer(buf, hgs);
s->exp->accept(this);
buf->writeByte(':');
buf->writenl();
s->statement->accept(this);
Expand All @@ -487,9 +487,9 @@ class PrettyPrintVisitor : public Visitor
void visit(CaseRangeStatement *s)
{
buf->writestring("case ");
s->first->toCBuffer(buf, hgs);
s->first->accept(this);
buf->writestring(": .. case ");
s->last->toCBuffer(buf, hgs);
s->last->accept(this);
buf->writeByte(':');
buf->writenl();
s->statement->accept(this);
Expand All @@ -514,7 +514,7 @@ class PrettyPrintVisitor : public Visitor
if (s->exp)
{
buf->writeByte(' ');
s->exp->toCBuffer(buf, hgs);
s->exp->accept(this);
}
buf->writeByte(';');
buf->writenl();
Expand All @@ -530,7 +530,7 @@ class PrettyPrintVisitor : public Visitor
{
buf->printf("return ");
if (s->exp)
s->exp->toCBuffer(buf, hgs);
s->exp->accept(this);
buf->writeByte(';');
buf->writenl();
}
Expand Down Expand Up @@ -565,7 +565,7 @@ class PrettyPrintVisitor : public Visitor
if (s->exp)
{
buf->writeByte('(');
s->exp->toCBuffer(buf, hgs);
s->exp->accept(this);
buf->writeByte(')');
}
if (s->body)
Expand All @@ -578,7 +578,7 @@ class PrettyPrintVisitor : public Visitor
void visit(WithStatement *s)
{
buf->writestring("with (");
s->exp->toCBuffer(buf, hgs);
s->exp->accept(this);
buf->writestring(")");
buf->writenl();
if (s->body)
Expand Down Expand Up @@ -630,7 +630,7 @@ class PrettyPrintVisitor : public Visitor
void visit(ThrowStatement *s)
{
buf->printf("throw ");
s->exp->toCBuffer(buf, hgs);
s->exp->accept(this);
buf->writeByte(';');
buf->writenl();
}
Expand Down Expand Up @@ -669,17 +669,17 @@ class PrettyPrintVisitor : public Visitor
{
buf->writestring(t->toChars());
if (t->next &&
t->value != TOKmin &&
t->value != TOKcomma &&
t->next->value != TOKcomma &&
t->value != TOKlbracket &&
t->next->value != TOKlbracket &&
t->next->value != TOKrbracket &&
t->value != TOKlparen &&
t->next->value != TOKlparen &&
t->next->value != TOKrparen &&
t->value != TOKdot &&
t->next->value != TOKdot)
t->value != TOKmin &&
t->value != TOKcomma &&
t->next->value != TOKcomma &&
t->value != TOKlbracket &&
t->next->value != TOKlbracket &&
t->next->value != TOKrbracket &&
t->value != TOKlparen &&
t->next->value != TOKlparen &&
t->next->value != TOKrparen &&
t->value != TOKdot &&
t->next->value != TOKdot)
{
buf->writeByte(' ');
}
Expand Down Expand Up @@ -917,7 +917,7 @@ class PrettyPrintVisitor : public Visitor
void visit(TypeTypeof *t)
{
buf->writestring("typeof(");
t->exp->toCBuffer(buf, hgs);
t->exp->accept(this);
buf->writeByte(')');
visitTypeQualifiedHelper(t);
}
Expand Down Expand Up @@ -1441,7 +1441,7 @@ class PrettyPrintVisitor : public Visitor
if (e->e0)
{
buf->writeByte('(');
e->e0->toCBuffer(buf, hgs);
e->e0->accept(this);
buf->writestring(", tuple(");
argsToCBuffer(buf, e->exps, hgs);
buf->writestring("))");
Expand Down Expand Up @@ -1618,7 +1618,7 @@ class PrettyPrintVisitor : public Visitor
* This is ok since types in constructor calls
* can never depend on parens anyway
*/
e->e1->toCBuffer(buf, hgs);
e->e1->accept(this);
}
else
expToCBuffer(buf, hgs, e->e1, precedence[e->op]);
Expand Down Expand Up @@ -2014,6 +2014,8 @@ void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, PREC pr)
assert(precedence[e->op] != PREC_zero);
assert(pr != PREC_zero);

PrettyPrintVisitor v(buf, hgs);

//if (precedence[e->op] == 0) e->print();
/* Despite precedence, we don't allow a<b<c expressions.
* They must be parenthesized.
Expand All @@ -2022,11 +2024,11 @@ void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, PREC pr)
(pr == PREC_rel && precedence[e->op] == pr))
{
buf->writeByte('(');
e->toCBuffer(buf, hgs);
e->accept(&v);
buf->writeByte(')');
}
else
e->toCBuffer(buf, hgs);
e->accept(&v);
}

/**************************************************
Expand Down
1 change: 1 addition & 0 deletions src/hdrgen.h
Expand Up @@ -28,5 +28,6 @@ struct HdrGenState
void toCBuffer(Statement *s, OutBuffer *buf, HdrGenState *hgs);
void toCBuffer(Type *t, OutBuffer *buf, Identifier *ident, HdrGenState *hgs);
void toCBuffer(Initializer *iz, OutBuffer *buf, HdrGenState *hgs);
void toCBuffer(Expression *e, OutBuffer *buf, HdrGenState *hgs);

void functionToBufferFull(TypeFunction *tf, OutBuffer *buf, Identifier *ident, HdrGenState* hgs, TemplateDeclaration *td);
2 changes: 1 addition & 1 deletion src/mtype.c
Expand Up @@ -9433,7 +9433,7 @@ void Parameter::argsToCBuffer(OutBuffer *buf, HdrGenState *hgs, Parameters *argu
if (arg->defaultArg)
{
argbuf.writestring(" = ");
arg->defaultArg->toCBuffer(&argbuf, hgs);
::toCBuffer(arg->defaultArg, &argbuf, hgs);
}
buf->write(&argbuf);
}
Expand Down
4 changes: 2 additions & 2 deletions src/staticassert.c
Expand Up @@ -130,11 +130,11 @@ void StaticAssert::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring(kind());
buf->writeByte('(');
exp->toCBuffer(buf, hgs);
::toCBuffer(exp, buf, hgs);
if (msg)
{
buf->writestring(", ");
msg->toCBuffer(buf, hgs);
::toCBuffer(msg, buf, hgs);
}
buf->writestring(");");
buf->writenl();
Expand Down

0 comments on commit 3984ff4

Please sign in to comment.