[cfe-dev] clang, unknown identifiers, and ahead of time compilation
Axel Naumann
Axel.Naumann at cern.ch
Thu Sep 2 05:55:47 PDT 2010
Hi Sebastian,
thanks for your quick answer!
Sebastian Redl wrote on 09/02/2010 01:00 AM:
> 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.
I know, otherwise it would be trivial to implement what we need :-)
> 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.
As I said, all unknown IDs we can have a definition for at runtime are
pointers. So at (ahead-of-time) compile time we assume they are
pointers; if they are not, the expression will be evaluated as invalid
at runtime anyway.
> Another example, a<b>(c)+d could be a rather weird but valid expression
[...]
Yes, I once asked Bjarne Stroustrup whether he cares about how difficult
it is to parse C++. Guess his answer :-)
> 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.
Fantastic! Brilliant idea, Sebastian. We'll give that a try. I am sure
we will have follow-up questions ;-)
> 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?)
Yes, we know it's ugly. It's all (well, some of :-) the pain of python
combined with the powers of C++. But at least people will get an error -
only it'll be at run time.
Cheers, Axel.
More information about the cfe-dev
mailing list