[cfe-dev] clang, unknown identifiers, and ahead of time compilation

Sebastian Redl sebastian.redl at getdesigned.at
Wed Sep 1 16:00:21 PDT 2010


On Sep 1, 2010, at 12:54 PM, Axel Naumann wrote:

> Our future interpreter will need to support variables that are defined
> from some context at runtime. They are unknown at compile time. Like this:
> 
> int f() {
>  int ret = 0;
>  ret = h->D(); // h is unknown at compile time
>  return ret
> }

It all comes down to C++ making it impossible to unambiguously parse past identifiers of unknown kind. That is, unless you know whether the identifier in question names a variable, type or template, you simply cannot continue parsing.
For example, the simple expression a(b) is a function call if a is a function or object, but an explicit cast if it's a type. Sema::isTypeName() is called by the parser to make the distinction.
Another example, a<b>(c)+d could be a rather weird but valid expression if all identifiers are variables (test if a is less than b, and test whether the result is greater than c plus d), or a call to a template function followed by an addition if a is a template function and b is a type or constant and c and d are variables, or an explicit type cast followed by an addition if a is a class template and b is a type or constant and c and d are variable, or another weird expression if a, b and d are variables and c is a type (check if a is less than b, and then compare the result to d after the unary + operator has been applied and the result was cast to c).

Of course, you could just defined any unknown identifier to be a variable name. In addition, you could tell the AST that its type is dependent, to suppress all type checks. Finally, add your runtime-evaluation bit and it would probably work. Just remember that you could be suppressing diagnostics in a way that results in VERY unexpected behavior. (In the above example, what if the user meant to refer to the type C, not the runtime variable c?)

Sebastian



More information about the cfe-dev mailing list