Make readExpr return an Expr object instead of a vector of tokens.
authorRui Ueyama <ruiu@google.com>
Sun, 24 Jul 2016 18:19:40 +0000 (18:19 +0000)
committerRui Ueyama <ruiu@google.com>
Sun, 24 Jul 2016 18:19:40 +0000 (18:19 +0000)
commit708019c41bbaae92d4781b1141bd3156827ad416
tree5c432d9cdc610cac0e18e2fdccf9d8f94dd57cb1
parent85c25b4d508baee01ef8f5546649fcecf54fc1d0
Make readExpr return an Expr object instead of a vector of tokens.

Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.

The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.

The above mechanism worked fairly well, but there were a few
drawbacks.

One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.

The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.

This patch fixes the issues with a new approach.

Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.

In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.

Differential Revision: https://reviews.llvm.org/D22728

llvm-svn: 276574
lld/ELF/LinkerScript.cpp
lld/ELF/LinkerScript.h
lld/test/ELF/linkerscript-locationcounter.s