3

I'm working on C++ code on a target that has multiple address spaces. The code I would like to use has pointers to members:

struct foo {
    int bar;
    int baz;
};

typedef int foo::*foo_member;
foo_member m = &foo::bar;

#define AS1 __attribute__((address_space(1)))

int main()
{
    foo AS1* f = /* ... */;
    f->*m = 4;
}

So far so good. However, things get funny when I try to get a pointer to the actual member:

int main()
{
    foo AS1* f = /* ... */;
    int AS1* x = &(f->*m);
}

Clang 3.7.0 (tags/RELEASE_370/final) complains:

test.cpp:14:11: error: cannot initialize a variable of type
      '__attribute__((address_space(1))) int *' with an rvalue of type 'int *'
        int AS1* x = &(f->*m);
                 ^   ~~~~~~~~

It seems that the address-of operator (or the member dereference?) drops the address space qualifier.

How can I use pointers to members with address spaces while ensuring that the address space is not dropped?

zneak
  • 134,922
  • 42
  • 253
  • 328
  • Have you tried putting the attribute in the pointer type? `using foo_member = int foo::* AS1;`, or `using foo_member = int AS1 foo::*;`? – Kerrek SB Dec 24 '15 at 14:18
  • @KerrekSB, it's unclear to me too where the `AS1` qualifier should go, but either the error stays the same or it complains that you cannot initialize it with `&foo::bar`. – zneak Dec 24 '15 at 14:23
  • 1
    Does just `&(f->bar)` work? Maybe it has nothing to do with the pointer-to-member? – Kerrek SB Dec 24 '15 at 14:24
  • @KerrekSB, yes, it returns a pointer in address space 1. – zneak Dec 24 '15 at 14:25
  • Looks like a compiler bug to me. – n. m. could be an AI Dec 24 '15 at 14:30
  • Yeah, I think so too. I asked on cfe-dev late last night but the post hasn't gone through yet. I asked here to see if anyone had a better idea. – zneak Dec 24 '15 at 14:30
  • In case anyone's wondering, I've been inspecting with `-Xclang -ast-dump` it looks like it bases the result of `->*` entirely with the right-hand side operand. If it was possible to initialize an `int AS1 foo::*`, we would get an lvalue of the correct type. It looks like we're officially in the realm of compiler bugs. – zneak Dec 24 '15 at 16:16
  • @KerrekSB The bug is in SemaExprCXX.cpp around line 4614. There should be something like `Result = Context.getAddrSpaceQualType(Result, LHSType.getAddressSpace())` to make sure that the result lives in the same address space as the left-hand side. I'm building LLVM, will report later if it works. – zneak Dec 24 '15 at 16:43

1 Answers1

0

It's a compiler bug. It's been reported and a patch has been submitted.

zneak
  • 134,922
  • 42
  • 253
  • 328