Improve address operator (unary &).
Until now the address operator only worked with simple variables.
Now it works with arbitrary expressions (that are lvalues or function
names). So for example this now works:
struct S { int a[10]};
int f(struct S* p) {
return &p->a[3];
}
diff --git a/libacc/acc.cpp b/libacc/acc.cpp
index 535361c..7e6dde6 100644
--- a/libacc/acc.cpp
+++ b/libacc/acc.cpp
@@ -4450,10 +4450,8 @@
unary();
doPointer();
} else if (t == '&') {
- VariableInfo* pVI = VI(tok);
- pGen->leaR0((int) pVI->pAddress, createPtrType(pVI->pType),
- ET_RVALUE);
- next();
+ unary();
+ doAddressOf();
} else if (t == EOF ) {
error("Unexpected EOF.");
} else if (t == ';') {
@@ -4656,6 +4654,16 @@
}
}
+ void doAddressOf() {
+ Type* pR0 = pGen->getR0Type();
+ bool isFuncPtr = pR0->tag == TY_POINTER && pR0->pHead->tag == TY_FUNC;
+ if ((! isFuncPtr) && pGen->getR0ExpressionType() != ET_LVALUE) {
+ error("Expected an lvalue");
+ }
+ Type* pR0Type = pGen->getR0Type();
+ pGen->setR0ExpressionType(ET_RVALUE);
+ }
+
/* Recursive descent parser for binary operations.
*/
void binaryOp(int level) {
@@ -5293,7 +5301,7 @@
void checkLVal() {
if (pGen->getR0ExpressionType() != ET_LVALUE) {
- error("Expected an lval");
+ error("Expected an lvalue");
}
}