Find template types of std::map

Hi,

for a current project I'm required to get the types of the template arguments key and value for std::map<K,V>. I've noticed that the STL implementation used by Clang defines the type of the map as

     %"class.std::map" = type { %"class.std::_Rb_tree" }

which then is further defined and finally ends as

     %"struct.std::_Rb_tree_node_base" = type {
         i32,
         %"struct.std::_Rb_tree_node_base"*,
         %"struct.std::_Rb_tree_node_base"*
     }

However, indifferent what K and V are and how many different maps are instantiated, it always ends at this definition. But, I also noticed, that

     %"struct.std::pair" = type { i32, double }

is defined, which then provides the information I need.

Now the problem arises to connect %"class.std::map" to %"struct.std::pair" correctly. To solve this I looked at the IR and noticed, that the std::pair's are always defined in the reverse order of the definitions of std::map. So I think about using some index to find the correct std::pair.

From this idea two issues arise:

1. I feel that this approach is not that stable as it should be, for instance, I think that the ordering of instantiation I'm making use of may change.

2. I'm unable to retrieve all defined named types from the module. I know there is LLVMContextImpl but this class is a forward declaration and therefore I'm not able to access LLVMContextImpl::NamedStructTypes.

Thanks for any suggestions.

Best regards,
Sebastian

Sebastian Dreßler wrote

Now the problem arises to connect %"class.std::map" to
%"struct.std::pair" correctly. To solve this I looked at the IR and
noticed, that the std::pair's are always defined in the reverse order of
the definitions of std::map. So I think about using some index to find
the correct std::pair.

I just thought again about this approach: this actually is the most
inelegant way I could come up with. The following might be better: I will
try to use the Clang library directly to convert the C++ type string, e. g.
std::string, int, ..., to a LLVM type.

However, any other suggestions are welcome. Maybe someone else already had
this sort of problem?

Best regards,
Sebastian

Trying to dig template arguments out of LLVM IR types is a bad idea.
Try a C++ demangler on a related function name, or the debug
information.

-Eli

Eli Friedman-2 wrote

Hi,

for a current project I'm required to get the types of the template
arguments key and value for std::map&lt;K,V&gt;. I've noticed that the
STL
implementation used by Clang defines the type of the map as

    %"class.std::map" = type { %"class.std::_Rb_tree" }

Trying to dig template arguments out of LLVM IR types is a bad idea.
Try a C++ demangler on a related function name, or the debug
information.

Thanks for the hint, I'll try that. However, when I successfully determined
the name of the template parameters, I'll have to find the LLVM IR std::pair
definition nevertheless. So, is there any function providing all names of
named structs from a module? When I dump the module, I'm able to see all
those structs, but I didn't find a function for e.g. iterating them.

Best,
Sebastian

Sebastian Dreßler wrote

So, is there any function providing all names of named structs from a
module? When I dump the module, I'm able to see all those structs, but I
didn't find a function for e.g. iterating them.

Just realized, that there is llvm::TypeFinder, which I've never seen before,
but does exactly that what I want.

Thanks for the help,
Sebastian