[LLVMdev] Mapping of class derivated and interfaces

Alkis Evlogimenos evlogimenos at gmail.com
Fri Aug 26 08:44:46 PDT 2005


On Fri, 2005-08-26 at 09:24 +0200, Nicola Lugato wrote:
> > In any case when you
> > want to obtain a base pointer to a "derived" object (where derivation
> > is implemented as you showed - with nesting) it's simply a matter of
> > using getelementptr to obtain a pointer to the nested base object and
> > using that.
> 
> Umm ok, but i've the strange feeling that i'm missing something..
> First a simple question: getelementptr with index 0 just give me the
> same pointer, right?
> 
> Ok, if it's so the example you gave me worked becouse the first
> element of "derived" is the "base" portion, thus having the same
> pointer. But what if it was not the first? For example with multiple
> derivation only one class can be in the first position:
> 
> %base1 = type { int }
> %base2 = type { sbyte }
> %derived = type { %base1, %base2, long }

This will not work with multiple inheritance but I assume we are talking
about Java, right?

> void myProc(base2 obj)
> {
> }
> 
> Now if you use get elementptr to obtain "base2" member of "derived" to
> pass it to myProc, then you're basically passing a different pointer,
> breaking instance identity among other things:
> 
> public Derived global_var = new ....;
> main()
> {
> myProc(d);
> }
> 
> void myProc(base2 obj)
> {
>    boolean b = (global_var == obj);  // this would be false!
> 
>    Derived alias = (Derived) obj; // this would break too btw
> }
> 
> So what am i missing ? maybe to deal with multiple ereditation one
> need some more "manadgement" code ? and does the optimizer still "see"
> all relations and code features?

I don't see why casting to a base class pointer will change the equality
test at all. global_var == obj will evaluate to true and alias will
point to global_var as expected. The fact that a pointer is "annotated"
with a type does not change the fact that it is a memory address :-)

-- 

Alkis




More information about the llvm-dev mailing list