[LLVMdev] Order of fiels and structure usage

Eli Friedman eli.friedman at gmail.com
Fri Sep 12 11:06:30 PDT 2008


On Fri, Sep 12, 2008 at 9:35 AM, Hendrik Boom <hendrik at topoi.pooq.com> wrote:
> I'd like to be able to make use of a structure type and its fields before
> it is completely defined.  To be specific, let me ask detailed questions
> at various stages in the construction of a recursive type.  I copy from
>
> http://llvm.org/docs/ProgrammersManual.html#TypeResolve
>
>    // Create the initial outer struct
>    PATypeHolder StructTy = OpaqueType::get();
>
> Is it possible to declare variables of type StructTy at this point?

I think you can, although you have to be careful; if you don't make
sure the variable eventually has a computable size, the module won't
be valid.

Declaring variables of type pointer to StructTy is completely safe.

>    std::vector<const Type*> Elts;
>    Elts.push_back(PointerType::get(StructTy));
>
> Is it possible to build an expression that uses the newly generated Elt
> as field-selector at this point?  I'm hoping yes, but I suspect No,
> because the elments of Elts* are clearly Type* instead of being a field.
> In particular, if I use the same type twice to make two fields, the
> corresponding elements of Elts will be indistinguishable.

I'm not following; are you trying to access the first member of NewSTy
here?  You can't use a type that hasn't been created yet.  You might
be able to pull some tricks with incomplete types or casts, though.

http://llvm.org/docs/LangRef.html#i_getelementptr and
http://llvm.org/docs/GetElementPtr.html might be useful here.

>    Elts.push_back(Type::Int32Ty);
>    StructType *NewSTy = StructType::get(Elts);
>
> Presumably at this point is is definitely possible to declare variables
> of type NewsTy and use field-selectors from NewSTy.  But it's a little
> too late for my purposes.

Basically, the rule for opaque types is that in a valid module, you
can do anything you could do with a declaration like "struct S;" in C.
 And in a module under construction, I'm pretty sure you can pull some
more tricks, like declaring variables with types of unknown size, or
accessing structs with members of unknown size.

-Eli



More information about the llvm-dev mailing list