简体   繁体   中英

How to lookup a type used in a module by name in LLVM?

In LLVM, it is possible to lookup a function by name within a module using Function* Module::getFunction(StringRef Name) const .

Similarly, it is possible to lookup a global variable by name within a module using GlobalVariable* Module::getGlobalVariable(StringRef Name) const .

How can I do the same thing with a type defined within a module?

Types are different from Functions and global variables because those two exist within a Module, and are things that will eventually have some bytes in the executable or whatever is generated. Types aren't like that.

Types occupy no space, exist within the Context and aren't necessarily findable. They generally can be found, though: If you want to find the singleton 42-bit integer type, you ask IntegerType::get() , and if you want to find a struct, you ask StructType, either by structure or by name .

(Keep in mind that not every struct type can be found by asking LLVM. In my case, I once had a problem where a caller needed to make a pointer to a struct, but the struct type was still just a placeholder at that point in the code, without either a name or a definition. I ended up finding the right LLVM type by asking my own code, rather than by asking LLVM, and I think many people do similar things.)

StructType *StructType::getTypeByName(Context, Name) . If you don't have a Context, use MyModule.getContext() . doxygen

Offhand I don't think other types can have names.

I think the statement

... a type defined within a module

could only apply to identified structs because there can only be a single identified struct with a given name in a particular LLVMContext. Literal struct types are uniqued structurally and primitives like an i8 already sought of 'exist', so all you do is get them and use them to annotate values. Maybe that's why they're created with a get method.

You can get the identified structs in an llvm::Module using the Module::getIdentifiedStructTypes method.

If walking over a module and identifying all the types used in that module is something that interests you, definitely have a look at TypeFinder.cpp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM