Anonymous Structures

Hello,

I am writing a pass which must insert calls to an external library.
One of the parameters to functions in this library is a very large,
complex structure (let's call it Foo). Foo has many fields, strange
target-dependent alignment requirements, and is likely to change many
times in the future. The internal representation of Foo is important
to this library, but the code my pass generates will only deal with
Foo-pointers.

For these reasons, I do not want my pass to emit a definition for
struct Foo. However, to add an external reference to the library
functions, I must supply function signatures that include this
structure type. Casting to/from void pointers works, but ultimately
generates suboptimal code.

Is there any way to refer to this structure by name, instead of
specifying its contents? Is this a place where I should use
OpaqueType? Could anyone provide examples please?

Thank you,

When your pass inserts calls, try declaring the function declarations to take i8* or %Foo* (where type %Foo = opaque). LLVM will unify the declaration's type when linking with the module that defines the symbol.

— Gordon

Thanks, that works.

And, I might as well provide a code snippet for google to find:

module->addTypeName("foo", OpaqueType::get() );
const Type *struct_foo = module->getTypeByName("foo");

It's really that easy.
Nick