ELF: Redefine parseExpr to parse an expression. NFC.
Previously the function reads an operator and the rest of
the expressions. This patch makes it to actually parse an expression
which starts with a primary pexression followed by other expressions
concatenated with operators.
llvm-svn: 266912
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 1000856..0db250c 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -64,8 +64,7 @@
return Tok;
}
-static uint64_t parseExpr(uint64_t Lhs, int MinPrec,
- ArrayRef<StringRef> &Tokens, uint64_t Dot);
+static uint64_t parseExpr(ArrayRef<StringRef> &Tokens, uint64_t Dot);
// This is a part of the operator-precedence parser to evaluate
// arithmetic expressions in SECTIONS command. This function evaluates an
@@ -75,8 +74,7 @@
if (Tok == ".")
return Dot;
if (Tok == "(") {
- uint64_t V = parsePrimary(Tokens, Dot);
- V = parseExpr(V, 0, Tokens, Dot);
+ uint64_t V = parseExpr(Tokens, Dot);
if (Tokens.empty()) {
error(") expected");
} else {
@@ -111,8 +109,9 @@
// This is an operator-precedence parser to evaluate
// arithmetic expressions in SECTIONS command.
-static uint64_t parseExpr(uint64_t Lhs, int MinPrec,
- ArrayRef<StringRef> &Tokens, uint64_t Dot) {
+// Tokens should start with an operator.
+static uint64_t parseExpr1(uint64_t Lhs, int MinPrec,
+ ArrayRef<StringRef> &Tokens, uint64_t Dot) {
while (!Tokens.empty()) {
// Read an operator and an expression.
StringRef Op1 = Tokens.front();
@@ -129,7 +128,7 @@
StringRef Op2 = Tokens.front();
if (precedence(Op2) <= precedence(Op1))
break;
- Rhs = parseExpr(Rhs, precedence(Op2), Tokens, Dot);
+ Rhs = parseExpr1(Rhs, precedence(Op2), Tokens, Dot);
}
Lhs = apply(Op1, Lhs, Rhs);
@@ -137,10 +136,14 @@
return Lhs;
}
+static uint64_t parseExpr(ArrayRef<StringRef> &Tokens, uint64_t Dot) {
+ uint64_t V = parsePrimary(Tokens, Dot);
+ return parseExpr1(V, 0, Tokens, Dot);
+}
+
// Evaluates the expression given by list of tokens.
static uint64_t evaluate(ArrayRef<StringRef> Tokens, uint64_t Dot) {
- uint64_t V = parsePrimary(Tokens, Dot);
- V = parseExpr(V, 0, Tokens, Dot);
+ uint64_t V = parseExpr(Tokens, Dot);
if (!Tokens.empty())
error("stray token: " + Tokens[0]);
return V;