Search code examples
rustllvmllvm-c++-api

How to get LLVMTypeRef of a struct?


Despite the tag, I'm actually using the llvm-sys Rust library, but I believe the API should be the same.

I'm using LLVM to compile a toy programming language I'm creating. In the RTS library for my language, I have a structure MyStruct. In the emitted LLVM, this becomes

%struct.MyStruct = /* definition of struct */

From the documentation, its unclear how I'm meant to get an LLVMTypeRef corresponding to this structure:

let myStructTypeRef: LLVMTypeRef = /* unsure how to write this code */
let myStructVariable: LLVMValueRef = LLVMBuildAlloca(builder, myStructTypeRef, "myStruct");

Solution

  • You use the LLVMGetTypeByName function

    https://llvm.org/doxygen/group__LLVMCCoreModule.html#ga29066e749e093fba6fedb5efa0962177