Skip to content

Commit

Permalink
Move BinExp::scaleFactor into a free function
Browse files Browse the repository at this point in the history
  • Loading branch information
yebblies committed Feb 20, 2014
1 parent c0af90a commit 87cdea2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
43 changes: 22 additions & 21 deletions src/cast.c
Expand Up @@ -2128,43 +2128,44 @@ Expression *CondExp::inferType(Type *t, int flag, Scope *sc, TemplateParameters
* Scale addition/subtraction to/from pointer.
*/

Expression *BinExp::scaleFactor(Scope *sc)
Expression *scaleFactor(BinExp *be, Scope *sc)
{
d_uns64 stride;
Type *t1b = e1->type->toBasetype();
Type *t2b = e2->type->toBasetype();
Type *t1b = be->e1->type->toBasetype();
Type *t2b = be->e2->type->toBasetype();
Expression *eoff;

if (t1b->ty == Tpointer && t2b->isintegral())
{ // Need to adjust operator by the stride
{
// Need to adjust operator by the stride
// Replace (ptr + int) with (ptr + (int * stride))
Type *t = Type::tptrdiff_t;

stride = t1b->nextOf()->size(loc);
d_uns64 stride = t1b->nextOf()->size(be->loc);
if (!t->equals(t2b))
e2 = e2->castTo(sc, t);
eoff = e2;
e2 = new MulExp(loc, e2, new IntegerExp(Loc(), stride, t));
e2->type = t;
type = e1->type;
be->e2 = be->e2->castTo(sc, t);
eoff = be->e2;
be->e2 = new MulExp(be->loc, be->e2, new IntegerExp(Loc(), stride, t));
be->e2->type = t;
be->type = be->e1->type;
}
else if (t2b->ty == Tpointer && t1b->isintegral())
{ // Need to adjust operator by the stride
{
// Need to adjust operator by the stride
// Replace (int + ptr) with (ptr + (int * stride))
Type *t = Type::tptrdiff_t;
Expression *e;

stride = t2b->nextOf()->size(loc);
d_uns64 stride = t2b->nextOf()->size(be->loc);
if (!t->equals(t1b))
e = e1->castTo(sc, t);
e = be->e1->castTo(sc, t);
else
e = e1;
e = be->e1;
eoff = e;
e = new MulExp(loc, e, new IntegerExp(Loc(), stride, t));
e = new MulExp(be->loc, e, new IntegerExp(Loc(), stride, t));
e->type = t;
type = e2->type;
e1 = e2;
e2 = e;
be->type = be->e2->type;
be->e1 = be->e2;
be->e2 = e;
}
else
assert(0);
Expand All @@ -2176,12 +2177,12 @@ Expression *BinExp::scaleFactor(Scope *sc)
;
else if (sc->func->setUnsafe())
{
error("pointer arithmetic not allowed in @safe functions");
be->error("pointer arithmetic not allowed in @safe functions");
return new ErrorExp();
}
}

return this;
return be;
}

/**************************************
Expand Down
8 changes: 4 additions & 4 deletions src/expression.c
Expand Up @@ -6469,7 +6469,7 @@ Expression *BinAssignExp::semantic(Scope *sc)
if ((op == TOKaddass || op == TOKminass) &&
e1->type->toBasetype()->ty == Tpointer &&
e2->type->toBasetype()->isintegral())
return scaleFactor(sc);
return scaleFactor(this, sc);

typeCombine(sc);
if (arith)
Expand Down Expand Up @@ -10228,7 +10228,7 @@ Expression *PostExp::semantic(Scope *sc)
e1->checkScalar();
e1->checkNoBool();
if (e1->type->ty == Tpointer)
e = scaleFactor(sc);
e = scaleFactor(this, sc);
else
e2 = e2->castTo(sc, e1->type);
e->type = e1->type;
Expand Down Expand Up @@ -11412,7 +11412,7 @@ Expression *AddExp::semantic(Scope *sc)
if (tb1->ty == Tpointer && e2->type->isintegral() ||
tb2->ty == Tpointer && e1->type->isintegral())
{
e = scaleFactor(sc);
e = scaleFactor(this, sc);
}
else if (tb1->ty == Tpointer && tb2->ty == Tpointer)
{
Expand Down Expand Up @@ -11528,7 +11528,7 @@ Expression *MinExp::semantic(Scope *sc)
return e;
}
else if (t2->isintegral())
e = scaleFactor(sc);
e = scaleFactor(this, sc);
else
{ error("can't subtract %s from pointer", t2->toChars());
return new ErrorExp();
Expand Down
2 changes: 1 addition & 1 deletion src/expression.h
Expand Up @@ -107,6 +107,7 @@ Expression *ctfeInterpret(Expression *);
Expression *inlineCopy(Expression *e, Scope *sc);
Expression *op_overload(Expression *e, Scope *sc);
Type *toStaticArrayType(SliceExp *e);
Expression *scaleFactor(BinExp *be, Scope *sc);

/* Run CTFE on the expression, but allow the expression to be a TypeExp
* or a tuple containing a TypeExp. (This is required by pragma(msg)).
Expand Down Expand Up @@ -782,7 +783,6 @@ class BinExp : public Expression
Expression *semantic(Scope *sc);
Expression *semanticp(Scope *sc);
Expression *checkComplexOpAssign(Scope *sc);
Expression *scaleFactor(Scope *sc);
Expression *typeCombine(Scope *sc);
int isunsigned();
Expression *incompatibleTypes();
Expand Down

0 comments on commit 87cdea2

Please sign in to comment.