LLVM Discussion Forums

Creating a struct containing a pointer to such a struct

I’m currently using LLVM to build a JIT. There are some C structs that I would like to be able to use in my JIT’d IR. One of them has the following layout:

struct myStruct {
    int depth;
    myStruct* parent;
}

When compiling with clang and using -S -emit-llvm, I get the following, which seems absolutely reasonable:

type myStruct = { i32, myStruct* } 

Alright. Now, if I want to do the same using the LLVM API, I’m not quite sure how I should do it. The following (expectedly) does not work:

auto intType = IntegerType::get(context, 32); // 32 bits integer
Type* myStructPtrType = nullptr; // Pointer to myStruct

// The following crashes because myStructPtrType is null: 
auto myStructType = StructType::create(context, { intType, myStructPtrType }, "myStruct"); // myStruct

myStructPtrType = PointerType::get(myStructType, 0); // Initialise the pointer type now

I don’t really know how to proceed here.
Any suggestions are welcome.

Someone answered somewhere else, so here’s the solution :slight_smile:

auto intType = IntegerType::get(context, 32); // 32 bits integer
auto myStructType = StructType::create(context, "myStruct"); // Create opaque type
auto myStructPtrType = PointerType::get(myStructType, 0); // Initialise the pointer type now
myStructType->setBody({ intType, myStructPtrType }, /* packed */ false); // Set the body of the aggregate