[cfe-dev] Novice seeks guidance initializing an Expr in Clang

Scott Schurr s.scott.schurr at gmail.com
Sun Sep 29 21:11:28 PDT 2013


Hi clang experts,

I'm not a compiler whiz, and I've never worked with Clang before.  I'm
attempting to hack Clang.  I'm having trouble building an Expr that does
what I want.  I'm looking for guidance.

Here's the background.  I'm working on a prototype of a potential C++ trait
that Richard Smith suggested to me.  It's a nullary trait that returns true
if it's evaluated in a context that requires constant evaluation.  It
returns false in all other contexts.  I want it to work like this:

  constexpr bool value1 = __is_constant_evaluation(void);
  static_assert(value1 == true, "Expected constant evaluation");

  bool value2 = __is_constant_evaluation(void);
  assert(value2 == false);

I'm just making a prototype.  The bogus (void) gives me a way to leverage
the infrastructure around UnaryTypeTrait.  The (void) is ignored.

So, I'm at the point where I need to make an Expr to return from the
Parser.  I've derived a type from Expr, modeling after the
UnaryTypeTraitExpr class.  But I can't figure out how to configure the Expr
so that the Expr gets re-evaluated every time it is evaluated, rather than
remembering the first result.

It seems to me that the Expr needs to be re-evaluated each time because
from one context to the next it will have a different result.  That is, the
result of __is_constant_evaluation(void) is a consequence of where its
result goes (e.g., value1 or value2), it's not a consequence of any passed
parameter.

Any guidance is appreciated.  I'd prefer suggestions for how to write the
code.  But I'll also listen to advice suggesting that what I'm attempting
is too hard for a novice.  Or a suggestion that I'm taking the wrong tack
altogether would be great if you want to point me in a different direction.

Thanks for reading.

Scott Schurr
S.Scott.Schurr at gmail.com

p.s. This is only the first of two problems that look hard.  The second
hard problem (that I know of) will be getting the constexpr-ness of the
context to the Expr when it evaluates so it can return true under the right
circumstances.  But I'm trying to solve just one problem at a time.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130929/34d6ec9a/attachment.html>


More information about the cfe-dev mailing list