determining aggregate member from MEM_REF
Richard Biener
richard.guenther@gmail.com
Fri Feb 16 11:22:00 GMT 2018
On Thu, Feb 15, 2018 at 6:28 PM, Martin Sebor <msebor@gmail.com> wrote:
> There are APIs to determine the base object and an offset
> into it from all sorts of expressions, including ARRAY_REF,
> COMPONENT_REF, and MEM_REF, but none of those I know about
> makes it also possible to discover the member being referred
> to.
>
> Is there an API that I'm missing or a combination of calls
> to some that would let me determine the (approximate) member
> and/or element of an aggregate from a MEM_REF expression,
> plus the offset from its beginning?
>
> Say, given
>
> struct A
> {
> void *p;
> char b[3][9];
> } a[2];
>
> and an expression like
>
> a[1].b[2] + 3
>
> represented as the expr
>
> MEM_REF (char[9], a, 69)
&MEM_REF (&a, 69)
you probably mean.
> where offsetof (struct A, a[1].b[2]) == 66
>
> I'd like to be able to determine that expr refers to the field
> b of struct A, and more specifically, b[2], plus 3. It's not
> important what the index into the array a is, or any other
> arrays on the way to b.
There is code in initializer folding that searches for a field in
a CONSTRUCTOR by base and offset. There's no existing
helper that gives you exactly what you want -- I guess you'd
ideally want to have a path to the refered object. But it may
be possible to follow what fold_ctor_reference does and build
such a helper.
> I realize the reference can be ambiguous in some cases (arrays
> of structs with multiple array members) and so the result wouldn't
> be guaranteed to be 100% reliable. It would only be used in
> diagnostics. (I think with some effort the type of the MEM_REF
> could be used to disambiguate the majority (though not all) of
> these references in practice.)
Given you have the address of the MEM_REF in your example above
the type of the MEM_REF doesn't mean anything.
I think ambiguity only happens with unions given MEM_REF offsets
are constant.
Note that even the type of 'a' might not be correct as it may have had
a different dynamic type.
So not sure what context you are trying to use this in diagnostics.
Richard.
>
> Thanks
> Martin
More information about the Gcc
mailing list