Search code examples
c++compiler-constructionllvmlanguage-design

Declare string type for parameter in LLVM


How would I declare a string parameter in the LLVM C++ API? For example, to declare a double parameter, I would do this:

argTypes.push_back(Type::getDoubleTy(*context))

Is it possible to do this for a string? I understand that a string is an array of i8, but I just need to know the correct function call.


Solution

  • As you said, a string can be represented as a pointer to an array of i8 elements, so, for representing a string parameter, you would generally use i8* because parameters are typically pointers to some data:

    argTypes.push_back(Type::getInt8PtrTy(*context));
    

    Remember that this representation assumes that your strings are null-terminated (C strings). If you want to pass strings that also contain their length (C++ std::string wise), you'd usually pass a struct that contains both a pointer to the data (i8*) and the length (i64 or some other integer type).