[cfe-dev] injecting a member function

John McCall rjmccall at apple.com
Thu Feb 17 23:58:45 PST 2011

On Feb 17, 2011, at 11:16 PM, Eric Niebler wrote:

> For my work on MS and Borland properties, I need to be able transform
> post-inc and post-dec expressions like "obj.prop++" into some equivalent
> expression that uses getter and setter methods. (Prefix ops are simple
> because they don't require the creation of a temporary to store the
> result of the expression.)
> I'm thinking of approaching the problem like this: "obj.prop++" is
> replaced with "obj.__builtin_postinc_Obj_Prop()", where that member is
> generated on-demand and injected in the Obj class. The member would
> essentially be:
>  PropTy Obj::__builtin_postinc_Obj_Prop() {
>    PropTy tmp = this->getProp();
>    this->setProp(tmp + 1);
>    return tmp;
>  }
> Two questions:
> 1) Is this the correct approach? Or is there a simpler/easier way?
> 2) Is there any precedent in clang for this? I'm looking for code I can
> crib from, or routines I could reuse.

I think you want to make some sort of PropertyAccessOperator
expression.  It would work basically like BinaryConditionalOperator
does, which is to say, it would bind the result of an expression
(the base of the property) to an OpaqueValueExpr, then perform
an arbitrary expression using that.  For source fidelity, it would also
preserve the original member expression (and RHS, where applicable).

As a more concrete example, "obj.prop++" would look something like this:

  (PropertyAccessOperator int
    # The original operand, for source fidelity
    (MemberExpr int lvalue property
      (DeclRefExpr "obj" Obj lvalue))
    # The opaque value expression
    (OpaqueValueExpr 0xabcdef Obj lvalue)
    # The expression whose result the OpaqueValueExpr will be bound to
    (DeclRefExpr "obj" Obj lvalue)
    # The expression to evaluate, expressed in terms of the OVE
    (CXXMemberCallExpr void
      (MemberExpr void(int) .setBase
        (OpaqueValueExpr 0xabcdef Obj lvalue))
      (CXXMemberCallExpr int
        (MemberExpr PropTy() .getBase
          (OpaqueValueExpr 0xabcdef Obj lvalue)))))

Hmm.  Unfortunately, I'm not sure how to indicate what the result of the
operation should be.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110217/61cecdb2/attachment.html>

More information about the cfe-dev mailing list