Skip to content

Commit e5d33b0

Browse files
committed
Phase 112 Part 1: Operator type safety - use enum classes directly
Changed FuncState operator method signatures to accept enum types: - prefix(UnOpr op, ...) - was prefix(int op, ...) - infix(BinOpr op, ...) - was infix(int op, ...) - posfix(BinOpr op, ...) - was posfix(int op, ...) Impact: - Eliminated 6 redundant static_cast operations (enum→int→enum roundtrip) - Improved type safety - prevents passing invalid operator values - Self-documenting function signatures - Zero performance impact - enums compile to same underlying type Files changed: - src/compiler/lparser.h (function signatures) - src/compiler/lcode.cpp (implementations) - src/compiler/parser.cpp (call sites - removed casts) Performance: 4.49s avg (baseline 4.20s, variance 4.01-4.95s) All tests passing: "final OK !!!"
1 parent 17291bf commit e5d33b0

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

src/compiler/lcode.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,8 +1485,7 @@ int FuncState::getlabel() {
14851485
return getPC();
14861486
}
14871487

1488-
void FuncState::prefix(int opr, expdesc *e, int line) {
1489-
UnOpr op = static_cast<UnOpr>(opr);
1488+
void FuncState::prefix(UnOpr op, expdesc *e, int line) {
14901489
expdesc ef;
14911490
ef.setKind(VKINT);
14921491
ef.setIntValue(0);
@@ -1495,7 +1494,7 @@ void FuncState::prefix(int opr, expdesc *e, int line) {
14951494
dischargevars(e);
14961495
switch (op) {
14971496
case UnOpr::OPR_MINUS: case UnOpr::OPR_BNOT: /* use 'ef' as fake 2nd operand */
1498-
if (constfolding(cast_int(opr + LUA_OPUNM), e, &ef))
1497+
if (constfolding(cast_int(op) + LUA_OPUNM, e, &ef))
14991498
break;
15001499
/* else */ /* FALLTHROUGH */
15011500
case UnOpr::OPR_LEN:
@@ -1506,8 +1505,7 @@ void FuncState::prefix(int opr, expdesc *e, int line) {
15061505
}
15071506
}
15081507

1509-
void FuncState::infix(int opr, expdesc *v) {
1510-
BinOpr op = static_cast<BinOpr>(opr);
1508+
void FuncState::infix(BinOpr op, expdesc *v) {
15111509
dischargevars(v);
15121510
switch (op) {
15131511
case BinOpr::OPR_AND: {
@@ -1551,10 +1549,9 @@ void FuncState::infix(int opr, expdesc *v) {
15511549
}
15521550
}
15531551

1554-
void FuncState::posfix(int opr, expdesc *e1, expdesc *e2, int line) {
1555-
BinOpr op = static_cast<BinOpr>(opr);
1552+
void FuncState::posfix(BinOpr op, expdesc *e1, expdesc *e2, int line) {
15561553
dischargevars(e2);
1557-
if (foldbinop(op) && constfolding(cast_int(opr + LUA_OPADD), e1, e2))
1554+
if (foldbinop(op) && constfolding(cast_int(op) + LUA_OPADD, e1, e2))
15581555
return; /* done by folding */
15591556
switch (op) {
15601557
case BinOpr::OPR_AND: {

src/compiler/lparser.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,10 @@ class FuncState {
577577
void patchtohere(int list);
578578
void concat(int *l1, int l2);
579579
int getlabel();
580-
// Note: prefix, infix, posfix use UnOpr/BinOpr types from lcode.h
581-
// We use int here to avoid circular dependency, will cast in implementation
582-
void prefix(int op, expdesc *v, int line);
583-
void infix(int op, expdesc *v);
584-
void posfix(int op, expdesc *v1, expdesc *v2, int line);
580+
// Operator functions use strongly-typed enum classes for type safety
581+
void prefix(UnOpr op, expdesc *v, int line);
582+
void infix(BinOpr op, expdesc *v);
583+
void posfix(BinOpr op, expdesc *v1, expdesc *v2, int line);
585584
void settablesize(int pcpos, unsigned ra, unsigned asize, unsigned hsize);
586585
void setlist(int base, int nelems, int tostore);
587586
void finish();

src/compiler/parser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ BinOpr Parser::subexpr( expdesc *v, int limit) {
879879
int line = ls->getLineNumber();
880880
ls->nextToken(); /* skip operator */
881881
subexpr(v, UNARY_PRIORITY);
882-
fs->prefix(static_cast<int>(uop), v, line);
882+
fs->prefix(uop, v, line);
883883
}
884884
else simpleexp(v);
885885
/* expand while operators have priorities higher than 'limit' */
@@ -889,10 +889,10 @@ BinOpr Parser::subexpr( expdesc *v, int limit) {
889889
BinOpr nextop;
890890
int line = ls->getLineNumber();
891891
ls->nextToken(); /* skip operator */
892-
fs->infix(static_cast<int>(op), v);
892+
fs->infix(op, v);
893893
/* read sub-expression with higher priority */
894894
nextop = subexpr(&v2, priority[static_cast<int>(op)].right);
895-
fs->posfix(static_cast<int>(op), v, &v2, line);
895+
fs->posfix(op, v, &v2, line);
896896
op = nextop;
897897
}
898898
leavelevel(ls);

0 commit comments

Comments
 (0)