Skip to content

Commit e345812

Browse files
committed
Change operation fn prototypes to take const references
1 parent 3401f3f commit e345812

File tree

2 files changed

+106
-108
lines changed

2 files changed

+106
-108
lines changed

eval.cpp

Lines changed: 101 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -512,31 +512,33 @@ namespace Sass {
512512
Expression::Concrete_Type l_type = lhs->concrete_type();
513513
Expression::Concrete_Type r_type = rhs->concrete_type();
514514

515+
int precision = ctx.precision;
516+
bool compressed = ctx.output_style == COMPRESSED;
515517
if (l_type == Expression::NUMBER && r_type == Expression::NUMBER) {
516-
Number* l_n = dynamic_cast<Number*>(lhs);
517-
Number* r_n = dynamic_cast<Number*>(rhs);
518-
return op_numbers(ctx, op_type, l_n, r_n);
518+
const Number* l_n = dynamic_cast<const Number*>(lhs);
519+
const Number* r_n = dynamic_cast<const Number*>(rhs);
520+
return op_numbers(ctx.mem, op_type, *l_n, *r_n, compressed, precision);
519521
}
520522
if (l_type == Expression::NUMBER && r_type == Expression::COLOR) {
521-
Number* l_n = dynamic_cast<Number*>(lhs);
522-
Color* r_c = dynamic_cast<Color*>(rhs);
523-
return op_number_color(ctx, op_type, l_n, r_c);
523+
const Number* l_n = dynamic_cast<const Number*>(lhs);
524+
const Color* r_c = dynamic_cast<const Color*>(rhs);
525+
return op_number_color(ctx.mem, op_type, *l_n, *r_c, compressed, precision);
524526
}
525527
if (l_type == Expression::COLOR && r_type == Expression::NUMBER) {
526-
Color* l_c = dynamic_cast<Color*>(lhs);
527-
Number* r_n = dynamic_cast<Number*>(rhs);
528-
return op_color_number(ctx, op_type, l_c, r_n);
528+
const Color* l_c = dynamic_cast<const Color*>(lhs);
529+
const Number* r_n = dynamic_cast<const Number*>(rhs);
530+
return op_color_number(ctx.mem, op_type, *l_c, *r_n, compressed, precision);
529531
}
530532
if (l_type == Expression::COLOR && r_type == Expression::COLOR) {
531-
Color* l_c = dynamic_cast<Color*>(lhs);
532-
Color* r_c = dynamic_cast<Color*>(rhs);
533-
return op_colors(ctx, op_type, l_c, r_c);
533+
const Color* l_c = dynamic_cast<const Color*>(lhs);
534+
const Color* r_c = dynamic_cast<const Color*>(rhs);
535+
return op_colors(ctx.mem, op_type, *l_c, *r_c, compressed, precision);
534536
}
535537

536538
To_Value to_value(ctx, ctx.mem);
537539
Value* v_l = dynamic_cast<Value*>(lhs->perform(&to_value));
538540
Value* v_r = dynamic_cast<Value*>(rhs->perform(&to_value));
539-
Value* ex = op_strings(ctx, op_type, v_l, v_r);
541+
Value* ex = op_strings(ctx.mem, op_type, *v_l, *v_r, compressed, precision);
540542
if (String_Constant* str = dynamic_cast<String_Constant*>(ex))
541543
{
542544
if (str->concrete_type() != Expression::STRING) return ex;
@@ -1102,48 +1104,48 @@ namespace Sass {
11021104
return *l < *r;
11031105
}
11041106

1105-
Value* Eval::op_numbers(Context& ctx, enum Sass_OP op, Number* l, Number* r, bool compressed, int precision)
1107+
Value* Eval::op_numbers(Memory_Manager<AST_Node>& mem, enum Sass_OP op, const Number& l, const Number& r, bool compressed, int precision)
11061108
{
1107-
double lv = l->value();
1108-
double rv = r->value();
1109+
double lv = l.value();
1110+
double rv = r.value();
11091111
if (op == Sass_OP::DIV && !rv) {
1110-
return new (ctx.mem) String_Quoted(l->pstate(), "Infinity");
1112+
return new (mem) String_Quoted(l.pstate(), "Infinity");
11111113
}
11121114
if (op == Sass_OP::MOD && !rv) {
1113-
error("division by zero", r->pstate());
1115+
error("division by zero", r.pstate());
11141116
}
11151117

1116-
Number tmp(*r);
1117-
tmp.normalize(l->find_convertible_unit());
1118-
string l_unit(l->unit());
1118+
Number tmp(r);
1119+
tmp.normalize(l.find_convertible_unit());
1120+
string l_unit(l.unit());
11191121
string r_unit(tmp.unit());
11201122
if (l_unit != r_unit && !l_unit.empty() && !r_unit.empty() &&
11211123
(op == Sass_OP::ADD || op == Sass_OP::SUB)) {
1122-
error("Incompatible units: '"+r_unit+"' and '"+l_unit+"'.", l->pstate());
1124+
error("Incompatible units: '"+r_unit+"' and '"+l_unit+"'.", l.pstate());
11231125
}
1124-
Number* v = new (ctx.mem) Number(*l);
1125-
v->pstate(l->pstate());
1126+
Number* v = new (mem) Number(l);
1127+
v->pstate(l.pstate());
11261128
if (l_unit.empty() && (op == Sass_OP::ADD || op == Sass_OP::SUB || op == Sass_OP::MOD)) {
1127-
v->numerator_units() = r->numerator_units();
1128-
v->denominator_units() = r->denominator_units();
1129+
v->numerator_units() = r.numerator_units();
1130+
v->denominator_units() = r.denominator_units();
11291131
}
11301132

11311133
if (op == Sass_OP::MUL) {
11321134
v->value(ops[op](lv, rv));
1133-
for (size_t i = 0, S = r->numerator_units().size(); i < S; ++i) {
1134-
v->numerator_units().push_back(r->numerator_units()[i]);
1135+
for (size_t i = 0, S = r.numerator_units().size(); i < S; ++i) {
1136+
v->numerator_units().push_back(r.numerator_units()[i]);
11351137
}
1136-
for (size_t i = 0, S = r->denominator_units().size(); i < S; ++i) {
1137-
v->denominator_units().push_back(r->denominator_units()[i]);
1138+
for (size_t i = 0, S = r.denominator_units().size(); i < S; ++i) {
1139+
v->denominator_units().push_back(r.denominator_units()[i]);
11381140
}
11391141
}
11401142
else if (op == Sass_OP::DIV) {
11411143
v->value(ops[op](lv, rv));
1142-
for (size_t i = 0, S = r->numerator_units().size(); i < S; ++i) {
1143-
v->denominator_units().push_back(r->numerator_units()[i]);
1144+
for (size_t i = 0, S = r.numerator_units().size(); i < S; ++i) {
1145+
v->denominator_units().push_back(r.numerator_units()[i]);
11441146
}
1145-
for (size_t i = 0, S = r->denominator_units().size(); i < S; ++i) {
1146-
v->numerator_units().push_back(r->denominator_units()[i]);
1147+
for (size_t i = 0, S = r.denominator_units().size(); i < S; ++i) {
1148+
v->numerator_units().push_back(r.denominator_units()[i]);
11471149
}
11481150
} else {
11491151
v->value(ops[op](lv, tmp.value()));
@@ -1152,121 +1154,117 @@ namespace Sass {
11521154
return v;
11531155
}
11541156

1155-
Value* Eval::op_number_color(Context& ctx, enum Sass_OP op, Number* l, Color* r, bool compressed, int precision)
1157+
Value* Eval::op_number_color(Memory_Manager<AST_Node>& mem, enum Sass_OP op, const Number& l, const Color& rh, bool compressed, int precision)
11561158
{
1157-
// TODO: currently SASS converts colors to standard form when adding to strings;
1158-
// when https://github.com/nex3/sass/issues/363 is added this can be removed to
1159-
// preserve the original value
1160-
r->disp("");
1161-
double lv = l->value();
1159+
Color r(rh);
1160+
r.disp("");
1161+
double lv = l.value();
11621162
switch (op) {
11631163
case Sass_OP::ADD:
11641164
case Sass_OP::MUL: {
1165-
return new (ctx.mem) Color(l->pstate(),
1166-
ops[op](lv, r->r()),
1167-
ops[op](lv, r->g()),
1168-
ops[op](lv, r->b()),
1169-
r->a());
1165+
return new (mem) Color(l.pstate(),
1166+
ops[op](lv, r.r()),
1167+
ops[op](lv, r.g()),
1168+
ops[op](lv, r.b()),
1169+
r.a());
11701170
} break;
11711171
case Sass_OP::SUB:
11721172
case Sass_OP::DIV: {
11731173
string sep(op == Sass_OP::SUB ? "-" : "/");
1174-
string color(r->to_string(compressed||!r->sixtuplet(), precision));
1175-
return new (ctx.mem) String_Quoted(l->pstate(),
1176-
l->to_string(compressed, precision)
1177-
+ sep
1178-
+ color);
1174+
string color(r.to_string(compressed||!r.sixtuplet(), precision));
1175+
return new (mem) String_Quoted(l.pstate(),
1176+
l.to_string(compressed, precision)
1177+
+ sep
1178+
+ color);
11791179
} break;
11801180
case Sass_OP::MOD: {
1181-
error("cannot divide a number by a color", r->pstate());
1181+
error("cannot divide a number by a color", r.pstate());
11821182
} break;
11831183
default: break; // caller should ensure that we don't get here
11841184
}
11851185
// unreachable
1186-
return l;
1186+
return new (mem) Color(rh);
11871187
}
11881188

1189-
Value* Eval::op_color_number(Context& ctx, enum Sass_OP op, Color* l, Number* r, bool compressed, int precision)
1189+
Value* Eval::op_color_number(Memory_Manager<AST_Node>& mem, enum Sass_OP op, const Color& l, const Number& r, bool compressed, int precision)
11901190
{
1191-
double rv = r->value();
1192-
if (op == Sass_OP::DIV && !rv) error("division by zero", r->pstate());
1193-
return new (ctx.mem) Color(l->pstate(),
1194-
ops[op](l->r(), rv),
1195-
ops[op](l->g(), rv),
1196-
ops[op](l->b(), rv),
1197-
l->a());
1191+
double rv = r.value();
1192+
if (op == Sass_OP::DIV && !rv) error("division by zero", r.pstate());
1193+
return new (mem) Color(l.pstate(),
1194+
ops[op](l.r(), rv),
1195+
ops[op](l.g(), rv),
1196+
ops[op](l.b(), rv),
1197+
l.a());
11981198
}
11991199

1200-
Value* Eval::op_colors(Context& ctx, enum Sass_OP op, Color* l, Color* r, bool compressed, int precision)
1200+
Value* Eval::op_colors(Memory_Manager<AST_Node>& mem, enum Sass_OP op, const Color& l, const Color& r, bool compressed, int precision)
12011201
{
1202-
if (l->a() != r->a()) {
1203-
error("alpha channels must be equal when combining colors", r->pstate());
1204-
}
1205-
if ((op == Sass_OP::DIV || op == Sass_OP::MOD) &&
1206-
(!r->r() || !r->g() ||!r->b())) {
1207-
error("division by zero", r->pstate());
1208-
}
1209-
return new (ctx.mem) Color(l->pstate(),
1210-
ops[op](l->r(), r->r()),
1211-
ops[op](l->g(), r->g()),
1212-
ops[op](l->b(), r->b()),
1213-
l->a());
1202+
if (l.a() != r.a()) {
1203+
error("alpha channels must be equal when combining colors", r.pstate());
1204+
}
1205+
if (op == Sass_OP::DIV && (!r.r() || !r.g() ||!r.b())) {
1206+
error("division by zero", r.pstate());
1207+
}
1208+
return new (mem) Color(l.pstate(),
1209+
ops[op](l.r(), r.r()),
1210+
ops[op](l.g(), r.g()),
1211+
ops[op](l.b(), r.b()),
1212+
l.a());
12141213
}
12151214

1216-
Value* Eval::op_strings(Context& ctx, enum Sass_OP op, Value* lhs, Value*rhs, bool compressed, int precision)
1215+
Value* Eval::op_strings(Memory_Manager<AST_Node>& mem, enum Sass_OP op, Value& lhs, Value& rhs, bool compressed, int precision)
12171216
{
1218-
To_String to_string(&ctx);
1219-
Expression::Concrete_Type ltype = lhs->concrete_type();
1220-
Expression::Concrete_Type rtype = rhs->concrete_type();
1217+
Expression::Concrete_Type ltype = lhs.concrete_type();
1218+
Expression::Concrete_Type rtype = rhs.concrete_type();
12211219

1222-
String_Quoted* lqstr = dynamic_cast<String_Quoted*>(lhs);
1223-
String_Quoted* rqstr = dynamic_cast<String_Quoted*>(rhs);
1220+
String_Quoted* lqstr = dynamic_cast<String_Quoted*>(&lhs);
1221+
String_Quoted* rqstr = dynamic_cast<String_Quoted*>(&rhs);
12241222

1225-
string lstr(lqstr ? lqstr->value() : lhs->to_string(compressed, precision));
1226-
string rstr(rqstr ? rqstr->value() : rhs->to_string(compressed, precision));
1223+
string lstr(lqstr ? lqstr->value() : lhs.to_string(compressed, precision));
1224+
string rstr(rqstr ? rqstr->value() : rhs.to_string(compressed, precision));
12271225

1228-
bool l_str_quoted = ((Sass::String*)lhs) && ((Sass::String*)lhs)->sass_fix_1291();
1229-
bool r_str_quoted = ((Sass::String*)rhs) && ((Sass::String*)rhs)->sass_fix_1291();
1226+
bool l_str_quoted = ((Sass::String*)&lhs) && ((Sass::String*)&lhs)->sass_fix_1291();
1227+
bool r_str_quoted = ((Sass::String*)&rhs) && ((Sass::String*)&rhs)->sass_fix_1291();
12301228
bool l_str_color = ltype == Expression::STRING && names_to_colors.count(lstr) && !l_str_quoted;
12311229
bool r_str_color = rtype == Expression::STRING && names_to_colors.count(rstr) && !r_str_quoted;
12321230

12331231
if (l_str_color && r_str_color) {
1234-
Color* l_c = name_to_color(lstr);
1235-
Color* r_c = name_to_color(rstr);
1236-
return op_colors(ctx, op, l_c, r_c);
1232+
const Color* c_l = name_to_color(lstr);
1233+
const Color* c_r = name_to_color(rstr);
1234+
return op_colors(mem, op,*c_l, *c_r, compressed, precision);
12371235
}
12381236
else if (l_str_color && rtype == Expression::COLOR) {
1239-
Color* l_c = name_to_color(lstr);
1240-
Color* r_c = dynamic_cast<Color*>(rhs);
1241-
return op_colors(ctx, op, l_c, r_c);
1237+
const Color* c_l = name_to_color(lstr);
1238+
const Color* c_r = dynamic_cast<const Color*>(&rhs);
1239+
return op_colors(mem, op, *c_l, *c_r, compressed, precision);
12421240
}
12431241
else if (ltype == Expression::COLOR && r_str_color) {
1244-
Color* l_c = dynamic_cast<Color*>(lhs);
1245-
Color* r_c = name_to_color(rstr);
1246-
return op_colors(ctx, op, l_c, r_c);
1242+
const Color* c_l = dynamic_cast<const Color*>(&lhs);
1243+
const Color* c_r = name_to_color(rstr);
1244+
return op_colors(mem, op, *c_l, *c_r, compressed, precision);
12471245
}
12481246
else if (l_str_color && rtype == Expression::NUMBER) {
1249-
Color* l_c = name_to_color(lstr);
1250-
Number* r_n = dynamic_cast<Number*>(rhs);
1251-
return op_color_number(ctx, op, l_c, r_n);
1247+
const Color* c_l = name_to_color(lstr);
1248+
const Number* n_r = dynamic_cast<const Number*>(&rhs);
1249+
return op_color_number(mem, op, *c_l, *n_r, compressed, precision);
12521250
}
12531251
else if (ltype == Expression::NUMBER && r_str_color) {
1254-
Number* l_n = dynamic_cast<Number*>(lhs);
1255-
Color* r_c = name_to_color(rstr);
1256-
return op_number_color(ctx, op, l_n, r_c);
1252+
const Number* n_l = dynamic_cast<const Number*>(&lhs);
1253+
const Color* c_r = name_to_color(rstr);
1254+
return op_number_color(mem, op, *n_l, *c_r, compressed, precision);
12571255
}
1258-
if (op == Sass_OP::MUL) error("invalid operands for multiplication", lhs->pstate());
1259-
if (op == Sass_OP::MOD) error("invalid operands for modulo", lhs->pstate());
1256+
if (op == Sass_OP::MUL) error("invalid operands for multiplication", lhs.pstate());
1257+
if (op == Sass_OP::MOD) error("invalid operands for modulo", lhs.pstate());
12601258
string sep;
12611259
switch (op) {
12621260
case Sass_OP::SUB: sep = "-"; break;
12631261
case Sass_OP::DIV: sep = "/"; break;
12641262
default: break;
12651263
}
1266-
if (ltype == Expression::NULL_VAL) error("invalid null operation: \"null plus "+quote(unquote(rstr), '"')+"\".", lhs->pstate());
1267-
if (rtype == Expression::NULL_VAL) error("invalid null operation: \""+quote(unquote(lstr), '"')+" plus null\".", lhs->pstate());
1264+
if (ltype == Expression::NULL_VAL) error("invalid null operation: \"null plus "+quote(unquote(rstr), '"')+"\".", lhs.pstate());
1265+
if (rtype == Expression::NULL_VAL) error("invalid null operation: \""+quote(unquote(lstr), '"')+" plus null\".", rhs.pstate());
12681266
string result((lstr) + sep + (rstr));
1269-
String_Quoted* str = new (ctx.mem) String_Quoted(lhs->pstate(), result);
1267+
String_Quoted* str = new (mem) String_Quoted(lhs.pstate(), result);
12701268
str->quote_mark(0);
12711269
return str;
12721270
}

eval.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ namespace Sass {
8989
static bool eq(Expression*, Expression*);
9090
static bool lt(Expression*, Expression*);
9191
// -- arithmetic on the combinations that matter
92-
static Value* op_numbers(Context&, enum Sass_OP, Number*, Number*, bool compressed = false, int precision = 5);
93-
static Value* op_number_color(Context&, enum Sass_OP, Number*, Color*, bool compressed = false, int precision = 5);
94-
static Value* op_color_number(Context&, enum Sass_OP, Color*, Number*, bool compressed = false, int precision = 5);
95-
static Value* op_colors(Context&, enum Sass_OP, Color*, Color*, bool compressed = false, int precision = 5);
96-
static Value* op_strings(Context&, enum Sass_OP, Value*, Value*, bool compressed = false, int precision = 5);
92+
static Value* op_numbers(Memory_Manager<AST_Node>&, enum Sass_OP, const Number&, const Number&, bool compressed = false, int precision = 5);
93+
static Value* op_number_color(Memory_Manager<AST_Node>&, enum Sass_OP, const Number&, const Color&, bool compressed = false, int precision = 5);
94+
static Value* op_color_number(Memory_Manager<AST_Node>&, enum Sass_OP, const Color&, const Number&, bool compressed = false, int precision = 5);
95+
static Value* op_colors(Memory_Manager<AST_Node>&, enum Sass_OP, const Color&, const Color&, bool compressed = false, int precision = 5);
96+
static Value* op_strings(Memory_Manager<AST_Node>&, enum Sass_OP, Value&, Value&, bool compressed = false, int precision = 5);
9797

9898
private:
9999
string interpolation(Expression* s);

0 commit comments

Comments
 (0)